从pycuda中的全局函数调用设备函数
我是PyCUDA的新手。我想从用从pycuda中的全局函数调用设备函数,cuda,pycuda,Cuda,Pycuda,我是PyCUDA的新手。我想从用\uuuu global\uuuu声明的函数调用用\uuuu device\uuuu声明的函数。在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
\uuuu global\uuuu
声明的函数调用用\uuuu device\uuuu
声明的函数。在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".
]
在引用之前,您应该声明
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
之前,您应该在引用它之前声明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
之前,我不确定我是否理解这个问题。在PyCUDA,您仍然在CUDA C中编写设备代码。如果您在C++中编写了宿主代码而不是Python,那么这一代码就没有什么不同。那么你在问什么?我不确定我是否理解这个问题。在PyCUDA,您仍然在CUDA C中编写设备代码。如果您在C++中编写了宿主代码而不是Python,那么这一代码就没有什么不同。那么,您要问的是什么呢?对于这种情况,这是可以接受的解决方案,但如果matMul是在单独的SourceModule类中定义的呢?同样的错误还在继续..,所以没有解决方案,除了尝试CUDA5.0,我正在使用CUDA5,我知道这个事实,但在PyCUDA中,我们如何编译单独的函数?因为如果我有太多的函数,那么就很难在一个对象中进行管理从技术上讲,你可以使用pycuda.compiler.compile
来微调子单元的编译,然后以某种方式将它们链接起来,但我还没能让它工作起来……是的……甚至我也这么认为,但我不能使用pycuda.compiler.compilet这是这种情况下可以接受的解决方案,但如果matMul是在单独的SourceModule类中定义的呢?同样的错误还在继续..,所以没有解决方案,除了尝试CUDA5.0,我正在使用CUDA5,我知道这个事实,但在PyCUDA中,我们如何编译单独的函数?因为如果我有太多的函数,那么在一个对象中管理起来就很困难了。从技术上讲,你可以使用pycuda.compiler.compile
来微调子单元的编译,然后以某种方式链接它们,但我还没能让它工作起来……是的……甚至我也这么认为,但我不能使用pycuda.compiler.compile