I am starting to use CUDA and thrust for a project of mine, so I am still new to some of it aspects. I ran into the following problem.
I have to separate .cu files which I want to use with a common header file which has a struct that both of them shall be able to use. It is something like this:
////////Global.h
#ifndef global_h
#define global_h
struct globalParam
{
uint64_t spaceToUse;
globalParams() : spaceToUse(1024*1024*1024) {}
};
globalParam glParam;
#endif
The First .cu file looks like this:
////////firstcufile.cu
#ifndef firstcufile_cu
#define firstcufile_cu
#include "Global.h"
template<typename T>
QVector<T> GPUcalculateNormSq(const QVector<T>& real, const QVector<T>& imag)
{
QVector<T> result (real.size());
uint64_t maxSpace = glParam.spaceToUse;
//Some Code to use thrust and using tops maxSpace bytes.
return result;
}
template QVector<float> GPUcalculateNormSq(const QVector<float>& real, const QVector<float>& imag);
template QVector<double> GPUcalculateNormSq(const QVector<double>& real, const QVector<double>& imag);
#endif
The second .cu file looks like this:
////////secondcufile.cu
#ifndef secondcufile_cu
#define secondcufile_cu
#include "Global.h"
template<typename T>
double getMean(const T& vec)
{
uint64_t spaceNeededOnGPU = vec.size() * sizeof (T);
uint64_t maxSpace = glParam.spaceToUse;
//Some code to make sure tops maxSpace bytes on GPU
double sum = thrust::reduce(std::begin(vec), std::end(vec));
return sum / vec.size();
}
template double getMean(const QVector<float>& vec);
#endif
Now the error I get is:
secondcufilecuda_d.o:(.bss+0x18): multiple definition of `glParam'
firstcufilecuda_d.o:(.bss+0x18): first defined here
The functions above seem similar, but thats becauseI tried to make them as simple as possible. It would be possible to write everything into a single .cu file, but I would like to split it up if possible.
What am I doing wrong with the linker? I am compiling and linking from within a Qt Creator project. Let me know if you need my lines from the .pro file to know how I use the nvcc compiler.