本文整理汇总了Python中pycuda.gpuarray.GPUArray方法的典型用法代码示例。如果您正苦于以下问题:Python gpuarray.GPUArray方法的具体用法?Python gpuarray.GPUArray怎么用?Python gpuarray.GPUArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycuda.gpuarray
的用法示例。
在下文中一共展示了gpuarray.GPUArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setOrig
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def setOrig(self, img):
"""
To set the original image from a given CPU or GPU array.
If it is a GPU array, it will NOT be copied.
Note that the most efficient method is to write directly over
self.devOrig with some kernel and then run self.updateOrig()
"""
assert img.shape == (self.h, self.w), \
"Got a {} image in a {} correlation routine!".format(
img.shape, (self.h, self.w))
if isinstance(img, np.ndarray):
self.debug(3, "Setting original image from ndarray")
self.devOrig.set(img)
elif isinstance(img, gpuarray.GPUArray):
self.debug(3, "Setting original image from GPUArray")
self.devOrig = img
else:
self.debug(0, "Error ! Unknown type of data given to setOrig()")
raise ValueError
self.updateOrig()
示例2: setFields
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def setFields(self, fieldsX, fieldsY):
"""
Method to give the fields to identify with the routine.
This is necessary only once and can be done multiple times, but the routine
have to be initialized with .prepare(), causing a slight overhead
Takes a tuple/list of 2 (gpu)arrays[Nfields,x,y] (one for displacement
along x and one along y)
"""
self.debug(2, "Setting fields")
if isinstance(fieldsX, np.ndarray):
self.devFieldsX.set(fieldsX)
self.devFieldsY.set(fieldsY)
elif isinstance(fieldsX, gpuarray.GPUArray):
self.devFieldsX = fieldsX
self.devFieldsY = fieldsY
self.fields = True
示例3: setImage
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def setImage(self, img_d):
"""
Set the image to compare with the original
Note that calling this method is not necessary: you can do .getDisp(image)
This will automatically call this method first
"""
assert img_d.shape == (self.h, self.w), \
"Got a {} image in a {} correlation routine!".format(
img_d.shape, (self.h, self.w))
if isinstance(img_d, np.ndarray):
self.debug(3, "Creating texture from numpy array")
self.array_d = cuda.matrix_to_array(img_d, "C")
elif isinstance(img_d, gpuarray.GPUArray):
self.debug(3, "Creating texture from gpuarray")
self.array_d = cuda.gpuarray_to_array(img_d, "C")
else:
self.debug(0, "Error ! Unknown type of data given to .setImage()")
raise ValueError
self.tex_d.set_array(self.array_d)
self.devX.set(np.zeros(self.Nfields, dtype=np.float32))
示例4: as_contiguous
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def as_contiguous(self):
"""
Creates a new GPUArray with the same dimensions, but using contiguous memory
Returns:
New contiguous GPUArray with separate underlying device allocation
"""
contig_tensor = GPUArray(self.tensor.shape, dtype=self.tensor.dtype)
src_strides = [s // self.tensor.dtype.itemsize for s in self.tensor.strides]
dst_strides = [s // contig_tensor.dtype.itemsize for s in contig_tensor.strides]
kernel = _get_copy_transpose_kernel(self.tensor.dtype,
self.tensor.shape,
range(len(self.tensor.shape)))
params = [contig_tensor.gpudata, self.tensor.gpudata] + list(kernel.args)
params = params + src_strides + dst_strides
kernel.prepared_async_call(kernel.grid, kernel.block, None, *params)
return contig_tensor
示例5: rand_cuda
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def rand_cuda(shape):
"""Create random values in the range [0, 1] in a GPUArray.
Parameters
----------
shape : tuple
Size of the random array.
Returns
-------
gpuarray
Random floats from 0 to 1 in GPUArray.
Examples
--------
>>> a = rand_cuda((2, 2))
[[ 0.80916596, 0.82687163],
[ 0.03921388, 0.44197764]]
>>> type(a)
<class 'pycuda.gpuarray.GPUArray'>
"""
return pycuda.curandom.rand(shape, dtype=float32)
示例6: get_cuda
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def get_cuda(a):
"""Return GPUArray from GPU memory as NumPy array.
Parameters
----------
a : gpuarray
Data on the GPU memory to retrieve.
Returns
-------
array
The GPUArray returned to RAM as a NumPy array.
Examples
--------
>>> a = give_cuda([1, 2, 3])
>>> b = get_cuda(a)
[ 1., 2., 3.]
>>> type(b)
<class 'numpy.ndarray'>
"""
return a.get()
示例7: zeros_cuda
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def zeros_cuda(shape):
"""Create GPUArray of zeros directly on GPU memory.
Parameters
----------
shape : tuple
Dimensions of the GPUArray.
Returns
-------
gpuarray
GPUArray of zeros.
Examples
--------
>>> a = zeros_cuda((3, 2))
[[ 0., 0.],
[ 0., 0.],
[ 0., 0.]]
>>> type(a)
<class 'pycuda.gpuarray.GPUArray'>
"""
return cuda_array.zeros(shape, dtype=float32)
示例8: hstack_cuda
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def hstack_cuda(a, b, dim=4):
"""Stack two GPUArrays horizontally.
Parameters
----------
a : gpuarray
First GPUArray.
b : gpuarray
Second GPUArray.
Returns
-------
gpuarray
Horizontally stacked GPUArrays.
"""
m, n = a.shape
o = b.shape[1]
nx = int(ceil((n + o) / dim))
ny = int(ceil(m / dim))
func = mod.get_function('hstack_cuda')
c = pycuda.gpuarray.empty((m, n + o), dtype=float32)
func(int32(m), int32(n), int32(o), a, b, c, block=(dim, dim, 1), grid=(nx, ny, 1))
return c
示例9: setMask
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def setMask(self, mask):
self.debug(3, "Setting the mask")
assert mask.shape == (self.h, self.w), \
"Got a {} mask in a {} routine.".format(mask.shape, (self.h, self.w))
if not mask.dtype == np.float32:
self.debug(2, "Converting the mask to float32")
mask = mask.astype(np.float32)
if isinstance(mask, np.ndarray):
self.maskArray = cuda.matrix_to_array(mask, 'C')
elif isinstance(mask, gpuarray.GPUArray):
self.maskArray = cuda.gpuarray_to_array(mask, 'C')
else:
self.debug(0, "Error! Mask data type not understood")
raise ValueError
self.texMask.set_array(self.maskArray)
示例10: setDisp
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def setDisp(self, X):
assert X.shape == (self.Nfields,), \
"Incorrect initialization of the parameters"
if isinstance(X, gpuarray.GPUArray):
self.devX = X
elif isinstance(X, np.ndarray):
self.devX.set(X)
else:
self.debug(0, "Error! Unknown type of data given to CorrelStage.setDisp")
raise ValueError
示例11: __call__
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def __call__(self, buffer_alloc):
"""
Allocates the GPUTensor object as a view of a pre-allocated buffer.
Arguments:
buffer_alloc (DeviceAllocation): Memory handle returned by pycuda
allocator
"""
tensor_description = self.tensor_description
layout = tensor_description.layout
dtype = self.transformer.storage_dtype(tensor_description.dtype)
if layout:
gpudata = int(buffer_alloc) + (layout.offset * dtype.itemsize)
strides = tuple([s * dtype.itemsize for s in layout.strides])
new_tensor = GPUArray(layout.shape,
dtype,
gpudata=gpudata,
strides=strides)
else:
gpudata = int(buffer_alloc) + tensor_description.offset
new_tensor = GPUArray(tensor_description.shape,
dtype,
gpudata=gpudata,
strides=tensor_description.strides)
self._tensor = new_tensor
self.transformer.tensors[self.tensor_name] = self._tensor
示例12: get
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def get(self, tensor):
"""
Copy the device tensor to a numpy array.
Arguments:
tensor (np.ndarray): Optional output array
Returns:
Numpy array containing tensor data
"""
if np.sum(self.tensor.strides) != 0:
if self.is_contiguous or self.tensor.shape == () or np.prod(self.tensor.shape) == 1:
contig_tensor = self.tensor
else:
# Need to do memcpy from contiguous device memory
contig_tensor = self.as_contiguous()
if tensor is None:
if contig_tensor.shape != self.tensor_description.shape:
return contig_tensor.get().reshape(self.tensor_description.shape)
else:
return contig_tensor.get()
tensor[:] = contig_tensor.get()
else:
# Tensor is just a broadcasted scalar, get scalar value and fill output array
view = GPUArray((1, ), dtype=self.tensor.dtype, gpudata=self.tensor.gpudata)[0]
value = view.get()
if tensor is None:
out = np.ndarray(self.tensor.shape, dtype=self.tensor.dtype)
out.fill(value)
return out
tensor.fill(value)
return tensor
示例13: to_array
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def to_array(in_array, copy = True):
"""
Helper function to convert input from a different module to af.Array
Parameters
-------------
in_array : array like object
Can be one of the following:
- numpy.ndarray
- pycuda.GPUArray
- pyopencl.Array
- numba.cuda.cudadrv.devicearray.DeviceNDArray
- array.array
- list
copy : Bool specifying if array is to be copied.
Default is true.
Can only be False if array is fortran contiguous.
Returns
--------------
af.Array of same dimensions as input after copying the data from the input
"""
if AF_NUMPY_FOUND and isinstance(in_array, NumpyArray):
return np_to_af_array(in_array, copy)
if AF_PYCUDA_FOUND and isinstance(in_array, CudaArray):
return pycuda_to_af_array(in_array, copy)
if AF_PYOPENCL_FOUND and isinstance(in_array, OpenclArray):
return pyopencl_to_af_array(in_array, copy)
if AF_NUMBA_FOUND and isinstance(in_array, NumbaCudaArray):
return numba_to_af_array(in_array, copy)
return Array(src=in_array)
示例14: fill_normal
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def fill_normal(self, ary, mean=0, stdv=1):
"""
Fill ary with normally distributed random numbers.
Arguments:
ary (Tensor): Tensor to fill with random values
mean (float): Mean value. Default 0
stdv (float): standard deviation value. Default 1
"""
self.pcg.fill_normal(p_gpuarray(ary.shape, ary.dtype, gpudata=ary.gpudata))
if not all([mean == 0, stdv == 1]):
ary[:] = ary * stdv + mean
示例15: give_cuda
# 需要导入模块: from pycuda import gpuarray [as 别名]
# 或者: from pycuda.gpuarray import GPUArray [as 别名]
def give_cuda(a, type='real'):
"""Give a list or an array to GPU memory.
Parameters
----------
a : array, list
Data to send to the GPU memory.
type : str
'real' or 'complex'.
Returns
-------
gpuarray
GPUArray version of the input array.
Examples
--------
>>> a = give_cuda([[1., 2., 3.], [4., 5., 6.]])
[[ 1., 2., 3.],
[ 4., 5., 6.]]
>>> type(a)
<class 'pycuda.gpuarray.GPUArray'>
>>> a.shape
(2, 3)
>>> a.dtype
'float32'
>>> a.reshape((1, 6))
[[ 1., 2., 3., 4., 5., 6.]]
"""
if type == 'real':
return cuda_array.to_gpu(array(a).astype(float32))
elif type == 'complex':
return cuda_array.to_gpu(array(a).astype(complex64))
else:
raise NotImplementedError