Skip to content

Instantly share code, notes, and snippets.

@fjarri
Created May 21, 2014 05:11
Show Gist options
  • Save fjarri/15383a1ae367bfc6efe8 to your computer and use it in GitHub Desktop.
Save fjarri/15383a1ae367bfc6efe8 to your computer and use it in GitHub Desktop.
import numpy
import pycuda.autoinit
import pycuda.driver as drv
from pycuda.compiler import SourceModule
from pycuda.gpuarray import to_gpu
mod = SourceModule("""
typedef struct _pair
{
int first;
int second;
} pair;
__global__ void get_second(int *dest, pair src)
{
const int i = threadIdx.x;
dest[i] = src.second;
}
""")
get_second = mod.get_function("get_second")
dtype = numpy.dtype([('first', numpy.int32), ('second', numpy.int32)])
pair_arr = numpy.empty(1, dtype)
pair_arr['first'][0] = 1
pair_arr['second'][0] = 2
pair = pair_arr[0]
dest = to_gpu(numpy.empty(400, numpy.int32))
get_second(dest, pair, block=(400,1,1), grid=(1,1))
assert dest.get() == 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment