编写了一个实现向量相加的函数 example3.cu,kernel函数是VecAdd_kernel.cu,这个大家都应该知道。用driver api实现。以下是用到模块的代码:
CUdevice cuDevice=0;
cuDeviceGet(&cuDevice,0);
CUcontext cuContext;
cuCtxCreate(&cuContext,0,cuDevice);
CUmodule cuModule;
cuModuleLoad(&cuModule,“VecAdd_kernel.cubin”);
CUfunction VecAdd;
cuModuleGetFunction(&VecAdd,cuModule,“VecAdd”);
//在host端分配内存
float* h_A = (float*)malloc(size);
float* h_B = (float*)malloc(size);
float* h_C = (float*)malloc(size);
//初始化数组的值
RandomInit(h_A, size);
RandomInit(h_B, size);
// 在device端分配显存
CUdeviceptr d_A;
culloc(&d_A,size);
CUdeviceptr d_B;
culloc(&d_B, size);
CUdeviceptr d_C;
cu**lloc(&d_C, size);
//将内存中的值读入显存
cuMemcpyHtoD(d_A,h_A, size);
cuMemcpyHtoD(d_B,h_B,size);
//启动kernel
int threadsPerBlock=256;
int threadsPerGrid=(N+threadsPerBlock-1)/threadsPerBlock;
cuFuncSetBlockShape(VecAdd,threadsPerBlock,1,1);
int offset=0;
cuParamSeti(VecAdd,offset,d_A);
offset+=sizeof(d_A);
cuParamSeti(VecAdd,offset,d_B);
offset+=sizeof(d_B);
cuParamSeti(VecAdd,offset,d_C);
offset+=sizeof(d_C);
cuParamSetSize(VecAdd,offset);
cuLaunchGrid(VecAdd,threadsPerGrid,1);
编译时报错
1>example_3.cu.obj : error LNK2019: 无法解析的外部符号 _cuMemFree@4,该符号在函数 “void __cdecl runTest(int,char * *)” (?runTest@@YAXHPAPAD@Z) 中被引用
还有很多和这类似的错误,不知道究竟是怎么回事,头文件已经引用了cuda.h和cuda_runtime.h
另外,在工程里面没有找到.cubin文件,是我的代码有问题还是环境配置有问题?我用的是cuda3.0