| 查看: 556 | 回复: 0 | ||
[求助]
多个.cu文件的工程怎样声明和使用常量内存(constant)?
|
|
(1)如下仅包含一个test.cu文件的cuda程序,使用了常量内存__constant__ float num1[40] 程序可以运行没有问题: //*****************test.cu********************************** #include<cuda_runtime.h> #include<windows.h> #include<iostream> using namespace std; const int nMax = 50; __constant__ float num1[40]; __global__ void exchangeKernel(float *aaa) { int offset = threadIdx.x + blockDim.x * blockIdx.x; aaa[offset] = num1[offset]; } int main(){ float *devA,tmp[40],res[40]; cudaMalloc((void**)&devA, 40*sizeof(float)); for (int i = 0; i < 40; i++)tmp = i*1.5f; cudaMemcpyToSymbol(num1, tmp, 40 * sizeof(float)); exchangeKernel << <4, 10 >> >(devA); cudaMemcpy(res, devA, 40 * sizeof(float), cudaMemcpyDeviceToHost); for (int i = 0; i < 40; i++){ cout << res << " " << endl; } cin >> res[1]; return 0; } //********************test.cu******************************* (2)如果想将上面的一个test.cu文件拆分为两个.cu文件(如test1.cu和test2.cu)怎样继续使用 常量内存__constant__ float num1[40] 我的大致想想法如下: test1.cu内容: //********************test1.cu******************************* __global__ void exchangeKernel(float *aaa) { int offset = threadIdx.x + blockDim.x * blockIdx.x; aaa[offset] = num1[offset]; } //********************test1.cu******************************* test2.cu内容: //********************test2.cu******************************* #include "cuda_runtime.h" #include "device_launch_parameters.h" #include<windows.h> #include<iostream> using namespace std; const int nMax = 50; __device__ __constant__ float num1[40]; //常量内存 __global__ void exchangeKernel(float *aaa); //函数声明 int main(){ float *devA,tmp[40],res[40]; cudaMalloc((void**)&devA, 40*sizeof(float)); for (int i = 0; i < 40; i++)tmp = i*1.5f; cudaMemcpyToSymbol(num1, tmp, 40 * sizeof(float)); exchangeKernel << <4, 10 >> >(devA); cudaMemcpy(res, devA, 40 * sizeof(float), cudaMemcpyDeviceToHost); for (int i = 0; i < 40; i++){ cout << res << " " << endl; } cin >> res[1]; return 0; } //********************test2.cu******************************* (3)但我的想法没有实现(提示错误为:error : identifier "num1" is undefined),求高人指点,如何在多个.cu文件的工程中声明和使用常量内存? |
» 猜你喜欢
275学硕081000服从调剂到其他专业,保不住本专业了
已经有8人回复
085600,320分求调剂
已经有4人回复
071000生物学调剂
已经有4人回复
材料与化工306分找调剂
已经有3人回复
一志愿北京科技材料科学与工程288分,求调剂
已经有10人回复
一志愿同济大学323分(080500)求调剂
已经有6人回复
一志愿085600中科院宁波所276分求调剂
已经有26人回复
材料专硕322分
已经有7人回复
材料调剂
已经有4人回复
294分080500材料科学与工程求调剂
已经有17人回复














回复此楼