Lowering StridedMemoryView
attribues to typed efficient C/C++/Cython accessible values
#180
Labels
cuda.core
Everything related to the cuda.core module
enhancement
Any code-related improvements
P1
Medium priority - Should do
Milestone
Currently the attributes of
StridedMemoryView
are as follows:cuda-python/cuda_core/cuda/core/experimental/_memoryview.pyx
Lines 24 to 32 in 8c841cd
There is a todo in the code noting that this is worth converting to Cython types. Would also support this recommendation
When accessing things like
shape
orstrides
in Cython, one ideally wants a C array type of some form (pointer, typed-memoryview, dynamic array, etc.) that can easily be iterated over in Cfor
-loops. As these attributes are currently accessing them requires calling the CPython API to get the length, each value, coerce them to C friendly types, etc.This is especially important for things like
ptr
, which gets accessed regularly. So having a fast access C-type really helpsIf we look at the Python Buffer Protocol (PEP 3118), they have the following definition for
Py_buffer
(their equivalent type):They also require users to call
PyObject_GetBuffer
to produce a buffer object andPyBuffer_Release
to release a buffer object. This handles any memory allocation/deallocation forshape
,strides
, etc.. It also handles refcounting forobj
. This functionality is wonderful to useThe lack of these semantics has made working with
DLPack
a choreThinking about how to map the C-like
struct
above to Cython/Python. A few things stick outSome of these Cython can translate between Python/Cython/C like
Py_ssize_t
(effectivelyssize_t
in C (PEP 353)) to Pythonint
's when needed.Others can be coerced like
char*
tobytes
(though usually one will want todecode
/encode
to/fromstr
)Still others could be translated well by Cython as long as they are typed appropriately. For example
void*
doesn't translate well to Python. Howeveruintptr_t
does behave like a pointer in C (sometimes with a cast) and like a Pythonint
. Cython will handle the translation for us. Similarlybint
for readonly works better when capturing Python'sbool
semantics while still working in C.With a typed
memoryview
, it is possible to wranglePy_ssize_t*
into something better behaved likePy_ssize_t[::1]
, which can then move more easily between Python & Cython.Also note that
format
above is a string specifying the format type according to the Python Buffer Protocol. NumPy is also able to consume and produce such format stringsThis led RAPIDS to this approach:
If
StridedMemoryView
is used with C/C++ directly, it may make sense to actually type a public Cstruct
in Cython. Then this could be leveraged in C/C++ code that can hand such objects to or receive them from CUDA-Python. For exampleThough there can be other valid ways to go
The text was updated successfully, but these errors were encountered: