1
votes

I'm newbie to PyCUDA. I want to call function declared with __device__ from function declared with __global__. How can I do this in pyCUDA?

import pycuda.driver as cuda  
from pycuda.compiler import SourceModule  
import numpy as n  
import pycuda.autoinit  
import pycuda.gpuarray as gp

d=gp.zeros(shape=(128,128),dtype=n.int32)  
h=n.zeros(shape=(128,128),dtype=n.int32)  
mod=SourceModule("""  
      __global__ void  matAdd(int *a)  
    {  
            int px=blockIdx.x*blockDim.x+threadIdx.x;  
            int py=blockIdx.y*blockDim.y+threadIdx.y;         
            a[px*128+py]+=1;   
            matMul(px);

    }  
      __device__ void matMul( int px)
    {
      px=5;
    }  

""")

m=mod.get_function("matAdd")  
m(d,block=(32,32,1),grid=(4,4))  
d.get(h)  

Above code is giving me following error

7-linux-i686.egg/pycuda/../include/pycuda kernel.cu]  
[stderr:  
kernel.cu(8): error: identifier "matMul" is undefined  

kernel.cu(12): warning: parameter "px" was set but never used  

1 error detected in the compilation of "/tmp/tmpxft_00002286_00000000-6_kernel.cpp1.ii".  
]  
1
I am not sure I understand the question. In PyCUDA, you still write the device code in CUDA C. It is no different to if you wrote the host code in C++ rather than Python. So what is it you are asking? - talonmies

1 Answers

1
votes

You should declare your matMul function before refering to it. You could do it like this:

  __device__ void matMul( int px); // declaration
  __global__ void  matAdd(int *a)  
{  
        int px=blockIdx.x*blockDim.x+threadIdx.x;  
        int py=blockIdx.y*blockDim.y+threadIdx.y;         
        a[px*128+py]+=1;   
        matMul(px);

}  
  __device__ void matMul( int px) // implementation
{
  px=5; // by the way, this assignment does not propagate outside this function
}  

, or just move whole matMul function to be before matAdd.