Search code examples
pythoncudagpgpupycuda

PyCUDA Passing variable by value to kernel


Should be simple enough; I literally want to send an int to the a SourceModule kernel declaration, where the C function

__global__......(int value,.....)

with the value being declared and called...

value = 256
...
...
func(value,...)

But I'm getting a variety of errors from pycuda that I'm using the wrong type.


Solution

  • The standard PyCUDA function interface requires argument have numpy dtypes, because it internally does mapping to C types under the hood. So for scalar arguments which are passed by value, you need to "cast" to a suitable numpy dtype first. Something like

    value = 256
    va = numpy.int32(value)
    
    func(va)
    

    should work. If you are passing single precision floating point values or arrays, make sure to explicitly use a dtype of np.float32, because numpy uses double precision by default and you will wind up with similar errors.