-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add support for __cuda_array_interface__
Adapt ITK's PyBuffer approach to inject __cuda_array_interface__ in wrapped code. Closes #10
- Loading branch information
1 parent
2e0f434
commit b6ac52f
Showing
3 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
%extend itkCudaImage@CudaImageTypes@{ | ||
%pythoncode %{ | ||
@property | ||
def __cuda_array_interface__(self): | ||
_pixelType = "@PixelType@" | ||
_typestr = _get_type_string(_pixelType) | ||
return { | ||
'shape': (self.GetLargestPossibleRegion().GetSize()[0], self.GetLargestPossibleRegion().GetSize()[1], self.GetLargestPossibleRegion().GetSize()[2]), | ||
'data': (int(self.GetCudaDataManager().GetGPUBufferPointer()), False), | ||
'typestr': _typestr, | ||
'descr': [('', _typestr)], | ||
'version': 3, | ||
'stream': None, | ||
'strides': None | ||
} | ||
%} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
%pythoncode %{ | ||
def _get_type_string(itk_Image_type): | ||
"""Returns the type string of the ITK PixelType as defined in the NumPy array interface.""" | ||
|
||
# This is a Mapping from system byte order to typestr byte order. | ||
_byteorder_typestr = { | ||
"big":">", | ||
"little":"<", | ||
} | ||
import sys | ||
_byteorder = _byteorder_typestr[sys.byteorder] | ||
|
||
# This is a Mapping from itk pixel types to typestr. | ||
_itk_typestr = { | ||
"UC":"|u1", | ||
"US":_byteorder + "u2", | ||
"UI":_byteorder + "u4", | ||
"UL":_byteorder + "u8", | ||
"ULL":_byteorder + "u8", | ||
"SC":"|i1", | ||
"SS":_byteorder + "i2", | ||
"SI":_byteorder + "i4", | ||
"SL":_byteorder + "i8", | ||
"SLL":_byteorder + "i8", | ||
"F":_byteorder + "f4", | ||
"D":_byteorder + "f8", | ||
} | ||
import os | ||
if os.name == 'nt': | ||
_itk_typestr['UL'] = _byteorder + "u4" | ||
_itk_typestr['SL'] = _byteorder + "i4" | ||
|
||
try: | ||
return _itk_typestr[itk_Image_type] | ||
except KeyError as e: | ||
raise e | ||
%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters