多个.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] = 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[i] << " " << 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];
#include "kernel.cu"
int main(){
float *devA,tmp[40],res[40];
cudaMalloc((void**)&devA, 40*sizeof(float));
for (int i = 0; i < 40; i++)tmp[i] = 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[i] << " " << endl;
}
cin >> res[1];
return 0;
}
//********************test2.cu*******************************
(3)但我的想法没有实现(提示错误为:error : identifier "num1" is undefined),求高人指点,如何在多个.cu文件的工程中声明和使用常量内存?