Skip to content

Commit b6ac52f

Browse files
committed
ENH: Add support for __cuda_array_interface__
Adapt ITK's PyBuffer approach to inject __cuda_array_interface__ in wrapped code. Closes #10
1 parent 2e0f434 commit b6ac52f

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

wrapping/CudaImage.i.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%extend itkCudaImage@CudaImageTypes@{
2+
%pythoncode %{
3+
@property
4+
def __cuda_array_interface__(self):
5+
_pixelType = "@PixelType@"
6+
_typestr = _get_type_string(_pixelType)
7+
return {
8+
'shape': (self.GetLargestPossibleRegion().GetSize()[0], self.GetLargestPossibleRegion().GetSize()[1], self.GetLargestPossibleRegion().GetSize()[2]),
9+
'data': (int(self.GetCudaDataManager().GetGPUBufferPointer()), False),
10+
'typestr': _typestr,
11+
'descr': [('', _typestr)],
12+
'version': 3,
13+
'stream': None,
14+
'strides': None
15+
}
16+
%}
17+
};

wrapping/CudaImage.i.init

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
%pythoncode %{
2+
def _get_type_string(itk_Image_type):
3+
"""Returns the type string of the ITK PixelType as defined in the NumPy array interface."""
4+
5+
# This is a Mapping from system byte order to typestr byte order.
6+
_byteorder_typestr = {
7+
"big":">",
8+
"little":"<",
9+
}
10+
import sys
11+
_byteorder = _byteorder_typestr[sys.byteorder]
12+
13+
# This is a Mapping from itk pixel types to typestr.
14+
_itk_typestr = {
15+
"UC":"|u1",
16+
"US":_byteorder + "u2",
17+
"UI":_byteorder + "u4",
18+
"UL":_byteorder + "u8",
19+
"ULL":_byteorder + "u8",
20+
"SC":"|i1",
21+
"SS":_byteorder + "i2",
22+
"SI":_byteorder + "i4",
23+
"SL":_byteorder + "i8",
24+
"SLL":_byteorder + "i8",
25+
"F":_byteorder + "f4",
26+
"D":_byteorder + "f8",
27+
}
28+
import os
29+
if os.name == 'nt':
30+
_itk_typestr['UL'] = _byteorder + "u4"
31+
_itk_typestr['SL'] = _byteorder + "i4"
32+
33+
try:
34+
return _itk_typestr[itk_Image_type]
35+
except KeyError as e:
36+
raise e
37+
%}

wrapping/itkCudaImage.wrap

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
itk_wrap_class("itk::CudaImage" POINTER_WITH_CONST_POINTER)
1+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/CudaImage.i.init" "${CMAKE_CURRENT_BINARY_DIR}/CudaImage.i" @ONLY)
22

3+
itk_wrap_class("itk::CudaImage" POINTER_WITH_CONST_POINTER)
34
UNIQUE(types "UC;UL;${ITKM_IT};${WRAP_ITK_SCALAR}")
4-
foreach(d ${ITK_WRAP_IMAGE_DIMS})
5-
foreach(t ${types})
5+
foreach(t ${types})
6+
set(PixelType ${t})
7+
foreach(d ${ITK_WRAP_IMAGE_DIMS})
68
itk_wrap_template("${t}${d}" "${ITKT_${t}}, ${d}")
9+
set(CudaImageTypes ${t}${d})
10+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CudaImage.i.in ${CMAKE_CURRENT_BINARY_DIR}/CudaImage.i.temp @ONLY)
11+
file(READ ${CMAKE_CURRENT_BINARY_DIR}/CudaImage.i.temp CudaImageInterfaceTemp)
12+
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/CudaImage.i ${CudaImageInterfaceTemp})
713
endforeach()
814
endforeach()
915

@@ -17,3 +23,21 @@ itk_wrap_class("itk::CudaImage" POINTER_WITH_CONST_POINTER)
1723
endforeach()
1824

1925
itk_end_wrap_class()
26+
27+
# Add library files to be included at a submodule level and copy them into
28+
# ITK's wrapping typedef directory.
29+
# Another approach is to add CudaImage.i to the WRAPPER_SWIG_LIBRARY_FILES list
30+
# but then the %pythoncode from CudaImage.i.init gets only included in
31+
# itkCudaDataManagerPython.py even if the WRAPPER_SUBMODULE_ORDER is set.
32+
# Prefer using ITK_WRAP_PYTHON_SWIG_EXT to make sure the block is included in
33+
# the right file exclusively.
34+
set(ITK_WRAP_PYTHON_SWIG_EXT
35+
"%include CudaImage.i\n${ITK_WRAP_PYTHON_SWIG_EXT}")
36+
37+
file(COPY "${CMAKE_CURRENT_BINARY_DIR}/CudaImage.i"
38+
DESTINATION "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}")
39+
40+
# Make sure to rebuild the python file when CudaImage.i is modified.
41+
# Touching CudaImage.i directly does not force a rebuild because it is just
42+
# appended to the ITK_WRAP_PYTHON_SWIG_EXT variable
43+
file(TOUCH ${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/itkCudaImage.i)

0 commit comments

Comments
 (0)