1

我是 PyCUDA 的新手。我想从用声明__device__的函数调用用声明的函数__global__。我怎样才能在 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)  

上面的代码给了我以下错误

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".  
]  
4

1 回答 1

1

您应该matMul在引用它之前声明您的函数。你可以这样做:

  __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
}  

,或者只是将整个matMul功能移到之前matAdd

于 2012-08-10T15:47:01.747 回答