Skip to content

Commit 849087e

Browse files
committed
Use numpy for array allocation instead of cython.view.array
1 parent 819c586 commit 849087e

4 files changed

Lines changed: 266 additions & 202 deletions

File tree

‎cuda_bindings/cuda/bindings/_v2/nvrtc.pyx‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
# SPDX-License-Identifier: Apache-2.0
44
#
55
# This code was automatically generated across versions from 12.9.0 to 13.3.0. Do not modify it directly.
6-
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=a36c7e54cf29166832dd9aebc1fa71cc3649498794a2846e707396419caebe10
6+
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=370c109c32bc68fce1daa6d02b41abf3f021d9ca99f7686976a9a338430c3649
77

88

99
# <<<< PREAMBLE CONTENT >>>>
1010

1111
cimport cpython as _cyb_cpython
1212
cimport cpython.buffer as _cyb_cpython_buffer
13-
from cython cimport view as _cyb_view
1413
from libc.stdint cimport intptr_t
1514
from libc.stdlib cimport (
1615
calloc as _cyb_calloc,
@@ -657,6 +656,8 @@ cpdef tuple version():
657656
cpdef int get_num_supported_archs() except? -1:
658657
"""nvrtcGetNumSupportedArchs sets the output parameter ``num_archs`` with the number of architectures supported by NVRTC. This can then be used to pass an array to ``nvrtcGetSupportedArchs`` to get the supported architectures.
659658
659+
see ``nvrtcGetSupportedArchs``.
660+
660661
Returns:
661662
int: number of supported architectures.
662663
@@ -672,6 +673,8 @@ cpdef int get_num_supported_archs() except? -1:
672673
cpdef object get_supported_archs():
673674
"""nvrtcGetSupportedArchs populates the array passed via the output parameter ``supported_archs`` with the architectures supported by NVRTC. The array is sorted in the ascending order. The size of the array to be passed can be determined using ``nvrtcGetNumSupportedArchs``.
674675
676+
see ``nvrtcGetNumSupportedArchs``.
677+
675678
Returns:
676679
int: sorted array of supported architectures.
677680
@@ -681,10 +684,10 @@ cpdef object get_supported_archs():
681684
with nogil:
682685
__status__ = nvrtcGetNumSupportedArchs(&numArchs)
683686
check_status(__status__)
684-
if numArchs == 0:
685-
return _cyb_view.array(shape=(1,), itemsize=sizeof(int), format="i", mode="c")[:0]
686-
cdef _cyb_view.array supported_archs = _cyb_view.array(shape=(numArchs,), itemsize=sizeof(int), format="i", mode="c")
687-
cdef int *supported_archs_ptr = <int *>(supported_archs.data)
687+
cdef object _supported_archs_alloc_ = _numpy.empty(max(numArchs, 1), dtype=_numpy.int32)
688+
cdef intptr_t _supported_archs_data_ = <intptr_t>_supported_archs_alloc_.ctypes.data
689+
cdef int *supported_archs_ptr = <int *>_supported_archs_data_
690+
cdef object supported_archs = _supported_archs_alloc_[:numArchs]
688691
with nogil:
689692
__status__ = nvrtcGetSupportedArchs(supported_archs_ptr)
690693
check_status(__status__)
@@ -881,6 +884,9 @@ cpdef bytes get_optix_ir(intptr_t prog):
881884
cpdef size_t get_program_log_size(intptr_t prog) except? 0:
882885
"""nvrtcGetProgramLogSize sets ``log_size_ret`` with the size of the log generated by the previous compilation of ``prog`` (including the trailing ``NULL``).
883886
887+
Note that compilation log may be generated with warnings and informative
888+
messages, even when the compilation of ``prog`` succeeds.
889+
884890
Args:
885891
prog (intptr_t): CUDA Runtime Compilation program.
886892
@@ -925,6 +931,9 @@ cpdef bytes get_program_log(intptr_t prog):
925931
cpdef add_name_expression(intptr_t prog, name_expression):
926932
"""nvrtcAddNameExpression notes the given name expression denoting the address of a global function or device/__constant__ variable.
927933
934+
The identical name expression string must be provided on a subsequent call
935+
to nvrtcGetLoweredName to extract the lowered name.
936+
928937
Args:
929938
prog (intptr_t): CUDA Runtime Compilation program.
930939
name_expression (str): constant expression denoting the
@@ -961,6 +970,10 @@ cpdef size_t get_pch_heap_size() except? 0:
961970
cpdef set_pch_heap_size(size_t size):
962971
"""set the size of the PCH Heap.
963972
973+
The requested size may be rounded up to a platform dependent alignment
974+
(e.g. page size). If the PCH Heap has already been allocated, the heap
975+
memory will be freed and a new PCH Heap will be allocated.
976+
964977
Args:
965978
size (size_t): requested size of the PCH Heap, in bytes.
966979
@@ -974,6 +987,20 @@ cpdef set_pch_heap_size(size_t size):
974987
cpdef int get_pch_create_status(intptr_t prog) except? -1:
975988
"""returns the PCH creation status.
976989
990+
NVRTC_SUCCESS indicates that the PCH was successfully created.
991+
NVRTC_ERROR_NO_PCH_CREATE_ATTEMPTED indicates that no PCH creation was
992+
attempted, either because PCH functionality was not requested during the
993+
preceding nvrtcCompileProgram call, or automatic PCH processing was
994+
requested, and compiler chose not to create a PCH file.
995+
NVRTC_ERROR_PCH_CREATE_HEAP_EXHAUSTED indicates that a PCH file could
996+
potentially have been created, but the compiler ran out space in the PCH
997+
heap. In this scenario, the :func:`get_pch_heap_size_required` can be used
998+
to query the required heap size, the heap can be reallocated for this size
999+
with :func:`set_pch_heap_size` and PCH creation may be reattempted again
1000+
invoking :func:`compile_program` with a new NVRTC program instance.
1001+
NVRTC_ERROR_PCH_CREATE indicates that an error condition prevented the PCH
1002+
file from being created.
1003+
9771004
Args:
9781005
prog (intptr_t): CUDA Runtime Compilation program.
9791006

‎cuda_bindings/cuda/bindings/cudla.pyx‎

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
# This code was automatically generated across versions from 1.5.0 to 13.3.0. Do not modify it directly.
5-
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=3c177b7a0328c0f6f16067c8c9f4e5a002bd019e8c17c017ba9f77af21da8d75
5+
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=5c0d6715f105108fd83cc471fd0978ecab1f1cdfdd78f41ac3bcf7dc1a470ab5
66

77

88
# <<<< PREAMBLE CONTENT >>>>
99

1010
cimport cpython as _cyb_cpython
1111
cimport cpython.buffer as _cyb_cpython_buffer
12-
from cython cimport view as _cyb_view
12+
from cpython.memoryview cimport PyMemoryView_FromMemory as _cyb_PyMemoryView_FromMemory
1313
from libc.stdint cimport (
1414
intptr_t,
1515
uint32_t,
@@ -604,19 +604,21 @@ cdef class ModuleTensorDescriptor:
604604
@property
605605
def stride(self):
606606
"""~_numpy.uint32: (array of length 8)."""
607-
cdef _cyb_view.array arr = _cyb_view.array(shape=(8,), itemsize=sizeof(uint32_t), format="I", mode="c", allocate_buffer=False)
608-
arr.data = <char *>(&(self._ptr[0].stride))
609-
return _numpy.asarray(arr)
607+
cdef object _mv_ = _cyb_PyMemoryView_FromMemory(
608+
<char *>(&(self._ptr[0].stride)),
609+
<Py_ssize_t>(sizeof(uint32_t) * (8)),
610+
_cyb_cpython_buffer.PyBUF_WRITE if not self._readonly else _cyb_cpython_buffer.PyBUF_READ,
611+
)
612+
return _numpy.frombuffer(_mv_, dtype=_numpy.uint32)
610613

611614
@stride.setter
612615
def stride(self, val):
613616
if self._readonly:
614617
raise ValueError("This ModuleTensorDescriptor instance is read-only")
615618
if len(val) != 8:
616619
raise ValueError(f"Expected length { 8 } for field stride, got {len(val)}")
617-
cdef _cyb_view.array arr = _cyb_view.array(shape=(8,), itemsize=sizeof(uint32_t), format="I", mode="c")
618-
arr[:] = _numpy.asarray(val, dtype=_numpy.uint32)
619-
_cyb_memcpy(<void *>(&(self._ptr[0].stride)), <void *>(arr.data), sizeof(uint32_t) * len(val))
620+
_val_ = _numpy.ascontiguousarray(_numpy.asarray(val, dtype=_numpy.uint32))
621+
_cyb_memcpy(<void *>(&(self._ptr[0].stride)), <void *><intptr_t>(_val_.ctypes.data), sizeof(uint32_t) * (8))
620622

621623
@staticmethod
622624
def from_buffer(buffer):
@@ -1344,10 +1346,13 @@ cdef class SignalEvents:
13441346
def dev_ptrs(self):
13451347
"""int: """
13461348
if self._ptr[0].devPtrs == NULL or self._ptr[0].numEvents == 0:
1347-
return _cyb_view.array(shape=(1,), itemsize=sizeof(intptr_t), format="q", mode="c")[:0]
1348-
cdef _cyb_view.array arr = _cyb_view.array(shape=(self._ptr[0].numEvents,), itemsize=sizeof(intptr_t), format="q", mode="c", allocate_buffer=False)
1349-
arr.data = <char *>(self._ptr[0].devPtrs)
1350-
return arr
1349+
return _numpy.empty(0, dtype=_numpy.intp)
1350+
cdef object _mv_ = _cyb_PyMemoryView_FromMemory(
1351+
<char *>(self._ptr[0].devPtrs),
1352+
<Py_ssize_t>(self._ptr[0].numEvents * sizeof(intptr_t)),
1353+
_cyb_cpython_buffer.PyBUF_WRITE,
1354+
)
1355+
return _numpy.frombuffer(_mv_, dtype=_numpy.intp)
13511356

13521357
@dev_ptrs.setter
13531358
def dev_ptrs(self, val):
@@ -1357,13 +1362,9 @@ cdef class SignalEvents:
13571362
self._ptr[0].numEvents = _n
13581363
if _n == 0:
13591364
return
1360-
cdef _cyb_view.array arr = _cyb_view.array(shape=(_n,), itemsize=sizeof(intptr_t), format="q", mode="c")
1361-
cdef intptr_t[:] mv = arr
1362-
cdef Py_ssize_t i
1363-
for i in range(_n):
1364-
mv[i] = val[i]
1365-
self._ptr[0].devPtrs = <uint64_t**><intptr_t>(arr.data)
1366-
self._refs["dev_ptrs"] = arr
1365+
_arr_ = _numpy.ascontiguousarray(_numpy.asarray(val, dtype=_numpy.intp))
1366+
self._ptr[0].devPtrs = <uint64_t**><intptr_t>_arr_.ctypes.data
1367+
self._refs["dev_ptrs"] = _arr_
13671368

13681369
@property
13691370
def eof_fences(self):
@@ -1530,10 +1531,13 @@ cdef class Task:
15301531
def output_tensor(self):
15311532
"""int: """
15321533
if self._ptr[0].outputTensor == NULL or self._ptr[0].numOutputTensors == 0:
1533-
return _cyb_view.array(shape=(1,), itemsize=sizeof(intptr_t), format="q", mode="c")[:0]
1534-
cdef _cyb_view.array arr = _cyb_view.array(shape=(self._ptr[0].numOutputTensors,), itemsize=sizeof(intptr_t), format="q", mode="c", allocate_buffer=False)
1535-
arr.data = <char *>(self._ptr[0].outputTensor)
1536-
return arr
1534+
return _numpy.empty(0, dtype=_numpy.intp)
1535+
cdef object _mv_ = _cyb_PyMemoryView_FromMemory(
1536+
<char *>(self._ptr[0].outputTensor),
1537+
<Py_ssize_t>(self._ptr[0].numOutputTensors * sizeof(intptr_t)),
1538+
_cyb_cpython_buffer.PyBUF_WRITE,
1539+
)
1540+
return _numpy.frombuffer(_mv_, dtype=_numpy.intp)
15371541

15381542
@output_tensor.setter
15391543
def output_tensor(self, val):
@@ -1543,22 +1547,21 @@ cdef class Task:
15431547
self._ptr[0].numOutputTensors = _n
15441548
if _n == 0:
15451549
return
1546-
cdef _cyb_view.array arr = _cyb_view.array(shape=(_n,), itemsize=sizeof(intptr_t), format="q", mode="c")
1547-
cdef intptr_t[:] mv = arr
1548-
cdef Py_ssize_t i
1549-
for i in range(_n):
1550-
mv[i] = val[i]
1551-
self._ptr[0].outputTensor = <uint64_t**><intptr_t>(arr.data)
1552-
self._refs["output_tensor"] = arr
1550+
_arr_ = _numpy.ascontiguousarray(_numpy.asarray(val, dtype=_numpy.intp))
1551+
self._ptr[0].outputTensor = <uint64_t**><intptr_t>_arr_.ctypes.data
1552+
self._refs["output_tensor"] = _arr_
15531553

15541554
@property
15551555
def input_tensor(self):
15561556
"""int: """
15571557
if self._ptr[0].inputTensor == NULL or self._ptr[0].numInputTensors == 0:
1558-
return _cyb_view.array(shape=(1,), itemsize=sizeof(intptr_t), format="q", mode="c")[:0]
1559-
cdef _cyb_view.array arr = _cyb_view.array(shape=(self._ptr[0].numInputTensors,), itemsize=sizeof(intptr_t), format="q", mode="c", allocate_buffer=False)
1560-
arr.data = <char *>(self._ptr[0].inputTensor)
1561-
return arr
1558+
return _numpy.empty(0, dtype=_numpy.intp)
1559+
cdef object _mv_ = _cyb_PyMemoryView_FromMemory(
1560+
<char *>(self._ptr[0].inputTensor),
1561+
<Py_ssize_t>(self._ptr[0].numInputTensors * sizeof(intptr_t)),
1562+
_cyb_cpython_buffer.PyBUF_WRITE,
1563+
)
1564+
return _numpy.frombuffer(_mv_, dtype=_numpy.intp)
15621565

15631566
@input_tensor.setter
15641567
def input_tensor(self, val):
@@ -1568,13 +1571,9 @@ cdef class Task:
15681571
self._ptr[0].numInputTensors = _n
15691572
if _n == 0:
15701573
return
1571-
cdef _cyb_view.array arr = _cyb_view.array(shape=(_n,), itemsize=sizeof(intptr_t), format="q", mode="c")
1572-
cdef intptr_t[:] mv = arr
1573-
cdef Py_ssize_t i
1574-
for i in range(_n):
1575-
mv[i] = val[i]
1576-
self._ptr[0].inputTensor = <uint64_t**><intptr_t>(arr.data)
1577-
self._refs["input_tensor"] = arr
1574+
_arr_ = _numpy.ascontiguousarray(_numpy.asarray(val, dtype=_numpy.intp))
1575+
self._ptr[0].inputTensor = <uint64_t**><intptr_t>_arr_.ctypes.data
1576+
self._refs["input_tensor"] = _arr_
15781577

15791578
@property
15801579
def wait_events(self):

‎cuda_bindings/cuda/bindings/cufile.pyx‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
# SPDX-License-Identifier: Apache-2.0
44
#
55
# This code was automatically generated across versions from 12.9.1 to 13.3.0. Do not modify it directly.
6-
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=df46a6921d93f83249134c7705b2809f57145b6fb72f6f40c4657ecd1b443b81
6+
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=b46f5a16585b36d52e8bf6a7ca3009d8c2350427cff03f68ab4b558b93c905fc
77

88

99
# <<<< PREAMBLE CONTENT >>>>
1010

1111
cimport cpython as _cyb_cpython
1212
cimport cpython.buffer as _cyb_cpython_buffer
1313
cimport cpython.memoryview as _cyb_cpython_memoryview
14-
from cython cimport view as _cyb_view
14+
from cpython.memoryview cimport PyMemoryView_FromMemory as _cyb_PyMemoryView_FromMemory
1515
from libc.stdint cimport (
1616
intptr_t,
1717
uint64_t,
@@ -2532,36 +2532,40 @@ cdef class StatsLevel2:
25322532
@property
25332533
def read_size_kb_hist(self):
25342534
"""~_numpy.uint64: (array of length 32)."""
2535-
cdef _cyb_view.array arr = _cyb_view.array(shape=(32,), itemsize=sizeof(uint64_t), format="Q", mode="c", allocate_buffer=False)
2536-
arr.data = <char *>(&(self._ptr[0].read_size_kb_hist))
2537-
return _numpy.asarray(arr)
2535+
cdef object _mv_ = _cyb_PyMemoryView_FromMemory(
2536+
<char *>(&(self._ptr[0].read_size_kb_hist)),
2537+
<Py_ssize_t>(sizeof(uint64_t) * (32)),
2538+
_cyb_cpython_buffer.PyBUF_WRITE if not self._readonly else _cyb_cpython_buffer.PyBUF_READ,
2539+
)
2540+
return _numpy.frombuffer(_mv_, dtype=_numpy.uint64)
25382541

25392542
@read_size_kb_hist.setter
25402543
def read_size_kb_hist(self, val):
25412544
if self._readonly:
25422545
raise ValueError("This StatsLevel2 instance is read-only")
25432546
if len(val) != 32:
25442547
raise ValueError(f"Expected length { 32 } for field read_size_kb_hist, got {len(val)}")
2545-
cdef _cyb_view.array arr = _cyb_view.array(shape=(32,), itemsize=sizeof(uint64_t), format="Q", mode="c")
2546-
arr[:] = _numpy.asarray(val, dtype=_numpy.uint64)
2547-
_cyb_memcpy(<void *>(&(self._ptr[0].read_size_kb_hist)), <void *>(arr.data), sizeof(uint64_t) * len(val))
2548+
_val_ = _numpy.ascontiguousarray(_numpy.asarray(val, dtype=_numpy.uint64))
2549+
_cyb_memcpy(<void *>(&(self._ptr[0].read_size_kb_hist)), <void *><intptr_t>(_val_.ctypes.data), sizeof(uint64_t) * (32))
25482550

25492551
@property
25502552
def write_size_kb_hist(self):
25512553
"""~_numpy.uint64: (array of length 32)."""
2552-
cdef _cyb_view.array arr = _cyb_view.array(shape=(32,), itemsize=sizeof(uint64_t), format="Q", mode="c", allocate_buffer=False)
2553-
arr.data = <char *>(&(self._ptr[0].write_size_kb_hist))
2554-
return _numpy.asarray(arr)
2554+
cdef object _mv_ = _cyb_PyMemoryView_FromMemory(
2555+
<char *>(&(self._ptr[0].write_size_kb_hist)),
2556+
<Py_ssize_t>(sizeof(uint64_t) * (32)),
2557+
_cyb_cpython_buffer.PyBUF_WRITE if not self._readonly else _cyb_cpython_buffer.PyBUF_READ,
2558+
)
2559+
return _numpy.frombuffer(_mv_, dtype=_numpy.uint64)
25552560

25562561
@write_size_kb_hist.setter
25572562
def write_size_kb_hist(self, val):
25582563
if self._readonly:
25592564
raise ValueError("This StatsLevel2 instance is read-only")
25602565
if len(val) != 32:
25612566
raise ValueError(f"Expected length { 32 } for field write_size_kb_hist, got {len(val)}")
2562-
cdef _cyb_view.array arr = _cyb_view.array(shape=(32,), itemsize=sizeof(uint64_t), format="Q", mode="c")
2563-
arr[:] = _numpy.asarray(val, dtype=_numpy.uint64)
2564-
_cyb_memcpy(<void *>(&(self._ptr[0].write_size_kb_hist)), <void *>(arr.data), sizeof(uint64_t) * len(val))
2567+
_val_ = _numpy.ascontiguousarray(_numpy.asarray(val, dtype=_numpy.uint64))
2568+
_cyb_memcpy(<void *>(&(self._ptr[0].write_size_kb_hist)), <void *><intptr_t>(_val_.ctypes.data), sizeof(uint64_t) * (32))
25652569

25662570
@staticmethod
25672571
def from_buffer(buffer):

0 commit comments

Comments
 (0)