15
15
1. numpy - numpy.ndarray
16
16
2. pycuda - pycuda.gpuarray
17
17
3. pyopencl - pyopencl.array
18
+ 4. numba - numba.cuda.cudadrv.devicearray.DeviceNDArray
18
19
19
20
"""
20
21
@@ -58,26 +59,27 @@ def _cc_to_af_array(in_ptr, ndim, in_shape, in_dtype, is_device=False, copy = Tr
58
59
else :
59
60
raise RuntimeError ("Unsupported ndim" )
60
61
62
+
63
+ _nptype_to_aftype = {'b1' : Dtype .b8 ,
64
+ 'u1' : Dtype .u8 ,
65
+ 'u2' : Dtype .u16 ,
66
+ 'i2' : Dtype .s16 ,
67
+ 's4' : Dtype .u32 ,
68
+ 'i4' : Dtype .s32 ,
69
+ 'f4' : Dtype .f32 ,
70
+ 'c8' : Dtype .c32 ,
71
+ 's8' : Dtype .u64 ,
72
+ 'i8' : Dtype .s64 ,
73
+ 'f8' : Dtype .f64 ,
74
+ 'c16' : Dtype .c64 }
75
+
61
76
try :
62
77
import numpy as np
63
78
from numpy import ndarray as NumpyArray
64
79
from .data import reorder
65
80
66
81
AF_NUMPY_FOUND = True
67
82
68
- _nptype_to_aftype = {'b1' : Dtype .b8 ,
69
- 'u1' : Dtype .u8 ,
70
- 'u2' : Dtype .u16 ,
71
- 'i2' : Dtype .s16 ,
72
- 's4' : Dtype .u32 ,
73
- 'i4' : Dtype .s32 ,
74
- 'f4' : Dtype .f32 ,
75
- 'c8' : Dtype .c32 ,
76
- 's8' : Dtype .u64 ,
77
- 'i8' : Dtype .s64 ,
78
- 'f8' : Dtype .f64 ,
79
- 'c16' : Dtype .c64 }
80
-
81
83
def np_to_af_array (np_arr , copy = True ):
82
84
"""
83
85
Convert numpy.ndarray to arrayfire.Array.
@@ -222,6 +224,48 @@ def pyopencl_to_af_array(pycl_arr, copy=True):
222
224
except :
223
225
AF_PYOPENCL_FOUND = False
224
226
227
+ try :
228
+ import numba
229
+ from numba import cuda
230
+ NumbaCudaArray = cuda .cudadrv .devicearray .DeviceNDArray
231
+ AF_NUMBA_FOUND = True
232
+
233
+ def numba_to_af_array (nb_arr , copy = True ):
234
+ """
235
+ Convert numba.gpuarray to arrayfire.Array
236
+
237
+ Parameters
238
+ -----------
239
+ nb_arr : numba.cuda.cudadrv.devicearray.DeviceNDArray()
240
+
241
+ copy : Bool specifying if array is to be copied.
242
+ Default is true.
243
+ Can only be False if array is fortran contiguous.
244
+
245
+ Returns
246
+ ----------
247
+ af_arr : arrayfire.Array()
248
+
249
+ Note
250
+ ----------
251
+ The input array is copied to af.Array
252
+ """
253
+
254
+ in_ptr = nb_arr .device_ctypes_pointer .value
255
+ in_shape = nb_arr .shape
256
+ in_dtype = _nptype_to_aftype [nb_arr .dtype .str [1 :]]
257
+
258
+ if not copy and not nb_arr .flags .f_contiguous :
259
+ raise RuntimeError ("Copy can only be False when arr.flags.f_contiguous is True" )
260
+
261
+ if (nb_arr .is_f_contiguous ()):
262
+ return _fc_to_af_array (in_ptr , in_shape , in_dtype , True , copy )
263
+ elif (nb_arr .is_c_contiguous ()):
264
+ return _cc_to_af_array (in_ptr , nb_arr .ndim , in_shape , in_dtype , True , copy )
265
+ else :
266
+ return numba_to_af_array (nb_arr .copy ())
267
+ except :
268
+ AF_NUMBA_FOUND = False
225
269
226
270
def to_array (in_array , copy = True ):
227
271
"""
@@ -231,8 +275,13 @@ def to_array(in_array, copy = True):
231
275
-------------
232
276
233
277
in_array : array like object
234
- Can be one of numpy.ndarray, pycuda.GPUArray, pyopencl.Array, array.array, list
235
-
278
+ Can be one of the following:
279
+ - numpy.ndarray
280
+ - pycuda.GPUArray
281
+ - pyopencl.Array
282
+ - numba.cuda.cudadrv.devicearray.DeviceNDArray
283
+ - array.array
284
+ - list
236
285
copy : Bool specifying if array is to be copied.
237
286
Default is true.
238
287
Can only be False if array is fortran contiguous.
@@ -248,4 +297,6 @@ def to_array(in_array, copy = True):
248
297
return pycuda_to_af_array (in_array , copy )
249
298
if AF_PYOPENCL_FOUND and isinstance (in_array , OpenclArray ):
250
299
return pyopencl_to_af_array (in_array , copy )
300
+ if AF_NUMBA_FOUND and isinstance (in_array , NumbaCudaArray ):
301
+ return numba_to_af_array (in_array , copy )
251
302
return Array (src = in_array )
0 commit comments