pycuda+numpy accessing the passed string


I’ve read various examples of working with strings but most of the items I’m finding are c++ cuda bindings. I’m pretty sure once I have a working example I’ll understand what I’m doing wrong with respect to the pointers.

This question was very close to what I’m after but the one response given doesn’t do what I would expect and has no explanation.

Here is my very simple starter:

## test.py
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy as np

mod = SourceModule("""
__global__ void test(char **Q) {
    printf("x=%d, y=%d, val=%s\n", threadIdx.x, threadIdx.y, Q);
}
""")
q = np.array(('AA', 'BB'), dtype=str)
q_gpu = cuda.mem_alloc(q.nbytes)
cuda.memcpy_htod(q_gpu, q)
print(f'q/Q: {q}')
func = mod.get_function("test")
func(q_gpu, block=(q.size,1,1))

This gives:

python3 test.py
q/Q: ['AA' 'BB']
x=0, y=0, val=A
x=1, y=0, val=A

What I’m aiming for is each X dimension to access each of the inputs, in full.

q/Q: ['AA' 'BB']
x=0, y=0, val=AA
x=1, y=0, val=BB

Input values are fixed length.

My eventual aim is to loop over the characters of a single element of q/Q in each cuda thread (e.g. for c in Q) so actual completed strings aren’t completely necessary but knowing I’m at the start of the correct one is.



Source link

Leave a Comment

Your email address will not be published. Required fields are marked *