我有三个文件:a.cu,b.cu和c.h.我希望有所有编译单元访问调试变量,所以我宣布他们在CH EXTERN,只有在a.cu定义,每个答案在这里Global Variable within Multiple Files:先前在cuda C++中定义的extern全局设备变量
a.cu
#include "c.h"
#include <stdio.h>
#include <iostream>
bool h_debug; //works fine
//__device__ bool d_debug; //POINT A
int main(int argc, char* argv[])
{
h_debug = (argc > 1 ? true : false);
std::cout<<"Host: "<<(h_debug ? "true" : "false")<<std::endl;
cudaMemcpyToSymbol(d_debug, &h_debug, sizeof(bool));
std::cout<<cudaGetErrorString(cudaGetLastError())<<std::endl;
cudaDeviceSynchronize();
}
b.cu
#include "c.h"
#include "stdio.h"
__global__
void myKernel(){
if(d_debug){
printf("device debug on\n");
}
else{
printf("device debug off\n");
}
}
CH
#ifndef MAIN_H
#define MAIN_H
extern __device__ bool d_debug;
extern bool h_debug;
#endif /* MAIN_H */
在生产线标记点A,如果我有它的评论,代码编译,但我得到一个CUDA错误运行时,如人们所预料的:
$ nvcc a.cu b.cu -o globalTest
$ globalTest
Host: true
invalid device symbol
如果我取消注释行来定义d_debug,我得到一个编译错误是没有意义的,我...
$ nvcc a.cu b.cu -o globalTest
a.cu:1:32: warning: unknown option after ‘#pragma GCC diagnostic’ kind [-Wpragmas]
a.cu:6:13: error: redefinition of ‘bool d_debug’
c.h:4:13: error: ‘bool d_debug’ previously declared here
为什么ñ像全局主机变量一样工作?我应该如何创建一个可由所有编译单元访问的全局设备变量?
您需要将设备链接合并到编辑序列中。否则,a.cu和b.cu具有* d_debug设备变量的*独立*副本。这里讨论的'cuda'标签有很多问题。 –
谢谢。知道问什么问题有很大帮助:) –
这[问题](http://stackoverflow.com/questions/24617318/cuda-constant-memory-value-not-correct)和链接的重复项通常涵盖相同的基础。 '__device__'变量与'__constant__'变量具有相同类型的默认*编译单元范围,解决方案方法相同。 –