neurokernel.tools.gpu.set_by_inds

neurokernel.tools.gpu.set_by_inds(dest_gpu, ind, src_gpu, ind_which='dest')[source]

Set values in a GPUArray by index.

Parameters:
  • dest_gpu (pycuda.gpuarray.GPUArray) – GPUArray instance to modify.
  • ind (pycuda.gpuarray.GPUArray or numpy.ndarray) – 1D array of element indices to set. Must have an integer dtype.
  • src_gpu (pycuda.gpuarray.GPUArray) – GPUArray instance from which to set values.
  • ind_which (str) – If set to ‘dest’, set the elements in dest_gpu with indices ind to the successive values in src_gpu; the lengths of ind and src_gpu must be equal. If set to ‘src’, set the successive values in dest_gpu to the values in src_gpu with indices ind; the lengths of ind and dest_gpu must be equal.

Examples

>>> import pycuda.gpuarray as gpuarray
>>> import pycuda.autoinit
>>> import numpy as np
>>> from nk.tools.gpu import set_by_inds
>>> dest_gpu = gpuarray.to_gpu(np.arange(5, dtype=np.float32))
>>> ind = gpuarray.to_gpu(np.array([0, 2, 4]))
>>> src_gpu = gpuarray.to_gpu(np.array([1, 1, 1], dtype=np.float32))
>>> set_by_inds(dest_gpu, ind, src_gpu, 'dest')
>>> np.allclose(dest_gpu.get(), np.array([1, 1, 1, 3, 1], dtype=np.float32))
True
>>> dest_gpu = gpuarray.to_gpu(np.zeros(3, dtype=np.float32))
>>> ind = gpuarray.to_gpu(np.array([0, 2, 4]))
>>> src_gpu = gpuarray.to_gpu(np.arange(5, dtype=np.float32))
>>> set_by_inds(dest_gpu, ind, src_gpu, 'src')
>>> np.allclose(dest_gpu.get(), np.array([0, 2, 4], dtype=np.float32))
True

Notes

Only supports 1D index arrays.

May not be efficient for certain index patterns because of lack of inability to coalesce memory operations.