Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented function to make Cuda Arrays from numpy 2D or 3D array in…
…cluding complex and double support
  • Loading branch information
Roberto Zamora-Zamora committed Oct 20, 2015
1 parent 97dc06a commit d383870
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions pycuda/driver.py
Expand Up @@ -724,6 +724,73 @@ def matrix_to_array(matrix, order, allow_double_hack=False):

return ary

def np_to_array(nparray, order, allowSurfaceBind=False):

case = order in ["C","F"]
if not case:
raise LogicError("order must be either F or C")

dimension = len(nparray.shape)
if dimension == 2:
if order == "C": stride = 0
if order == "F": stride = -1
h, w = nparray.shape
d = 1
if allowSurfaceBind:
descrArr = ArrayDescriptor3D()
descrArr.width = w
descrArr.height = h
descrArr.depth = d
else:
descrArr = ArrayDescriptor()
descrArr.width = w
descrArr.height = h
elif dimension == 3:
if order == "C": stride = 1
if order == "F": stride = 1
d, h, w = nparray.shape
descrArr = ArrayDescriptor3D()
descrArr.width = w
descrArr.height = h
descrArr.depth = d
else:
raise LogicError("CUDArray dimensions 2 and 3 supported in CUDA at the moment ... ")

if nparray.dtype == np.complex64:
descrArr.format = array_format.SIGNED_INT32 # Reading data as int2 (hi=re,lo=im) structure
descrArr.num_channels = 2
elif nparray.dtype == np.float64:
descrArr.format = array_format.SIGNED_INT32 # Reading data as int2 (hi,lo) structure
descrArr.num_channels = 2
elif nparray.dtype == np.complex128:
descrArr.format = array_format.SIGNED_INT32 # Reading data as int4 (re=(hi,lo),im=(hi,lo)) structure
descrArr.num_channels = 4
else:
descrArr.format = dtype_to_array_format(nparray.dtype)
descrArr.num_channels = 1

if allowSurfaceBind:
if dimension==2: descrArr.flags |= array3d_flags.ARRAY3D_LAYERED
descrArr.flags |= array3d_flags.SURFACE_LDST

cudaArray = Array(descrArr)
if allowSurfaceBind or dimension==3:
copy3D = Memcpy3D()
copy3D.set_src_host(nparray)
copy3D.set_dst_array(cudaArray)
copy3D.width_in_bytes = copy3D.src_pitch = nparray.strides[stride]
copy3D.src_height = copy3D.height = h
copy3D.depth = d
copy3D()
return cudaArray
else:
copy2D = Memcpy2D()
copy2D.set_src_host(nparray)
copy2D.set_dst_array(cudaArray)
copy2D.width_in_bytes = copy2D.src_pitch = nparray.strides[stride]
copy2D.src_height = copy2D.height = h
copy2D(aligned=True)
return cudaArray

def make_multichannel_2d_array(ndarray, order):
"""Channel count has to be the first dimension of the C{ndarray}."""
Expand Down

0 comments on commit d383870

Please sign in to comment.