I have three files: a.cu, b.cu, and c.h. I want to have debug variables accessible to all compilation units, so I declared them as extern in c.h, and defined only in a.cu, per the answer here Global Variable within Multiple Files:
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");
}
}
c.h
#ifndef MAIN_H
#define MAIN_H
extern __device__ bool d_debug;
extern bool h_debug;
#endif /* MAIN_H */
At the line marked POINT A, if I have it commented, the code compiles but I get a cuda error when running, as one might expect:
$ nvcc a.cu b.cu -o globalTest
$ globalTest
Host: true
invalid device symbol
If I un-comment the line to define the d_debug, I get a compiler error that doesn't make sense to me ...
$ 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
Why is it not working like the global host variable? How should I create a global device variable which is accessible by all compilation units?