24小时热门版块排行榜    

查看: 543  |  回复: 0

xuanyz

木虫 (小有名气)

[求助] 多个.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文件的工程中声明和使用常量内存?
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 xuanyz 的主题更新
信息提示
请填处理意见