我正在使用pycuda,我想知道是否有相应的函数cudaMemcpyToSymbol
我想将常量从主机复制到设备,如下所示
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy
from sys import path
from struct import *
from gpustruct import GPUStruct
if __name__ == '__main__':
# list devices
ndevices = cuda.Device.count()
print '{} devices found'.format(ndevices)
for i in xrange(ndevices):
print ' ', cuda.Device(i).name()
# compile device.cu
mod = SourceModule('''
__device__ __constant__ int CONSTd;
struct Results
{
float *A;
float *B;
float *C;
};
struct fin
{
float *N;
};
__global__ void test(Results *src,fin *dest){
int i=blockIdx.x *blockDim.x + threadIdx.x;
src->C[i]=src->A[i]+src->B[i]+dest->N[i]+CONSTd;
}''',
nvcc='/opt/cuda65/bin/nvcc',
)
kern = mod.get_function("test")
CONSTANTE=5
src_gpu = GPUStruct([(numpy.int32,'*A', numpy.ones(10,dtype=numpy.int32)),(numpy.int32,'*B', numpy.ones(10,dtype=numpy.int32)),(numpy.int32,'*C', numpy.zeros(10,dtype=numpy.int32))])
test_gpu = GPUStruct([(numpy.int32,'*N', numpy.array(10*[5],dtype=numpy.int32))])
#something like this:
**cudaMemcpyToSymbol(CONSTd, &CONSTANTE, sizeof(int));**
src_gpu.copy_to_gpu()
test_gpu.copy_to_gpu()
kern(src_gpu.get_ptr(),test_gpu.get_ptr(),block=(10,1,1),grid=(1,1))
src_gpu.copy_from_gpu()
print(src_gpu)
答案 0 :(得分:2)
PyCUDA实现直接遵循CUDA驱动程序API,因此您可以使用您可以找到的任何驱动程序API代码作为模型,但需要做两件事:
db.collectionName.find({
"participants": ObjectId("5530b6208ab0478c2bd23292")
, "participants": ObjectId("5534a588a12ae5ac11dc6f6b")
, "messages": {
"$elemMatch": {
"participants": ObjectId("5530b6208ab0478c2bd23292")
}
}
, "messages": {
"$elemMatch": {
"participants": ObjectId("5534a588a12ae5ac11dc6f6b")
}
}
}, {
"messages.$.participants": 1
})
.pretty()
检索指向已编译源模块中符号的设备指针module.get_global()
将值复制到该指针。请注意,PyCUDA API要求对象支持Python缓冲区协议。在实践中,这意味着您应该在Python端使用driver.memcpy_htod
或类似的东西。这实际上是numpy.ndarray
在幕后做的事情。