简体   繁体   中英

Using Template classes in cuda / c++ code

I'm new to coding in cuda, (and not massively experienced in c++) so i've been reading around to try and find a solution to this problem, but dont generally understand what people try and explain, and have yet to get it working.

Basically, I have a .cu file which contains two things:

template <class ModelType>
__global__ void Stepkernel(ModelType *particles)

and:

template <class ModelType>
void runTest(ModelType *particles)

I then have a header file, SamplerI.h which I didn't write, but i'm trying to include a call to the void function above, so, below all the #includes i have:

template <class ModelType>
void runTest(ModelType *particles);

and then later in the header theres a function where i've included a call to the above.

the header and associated files are compiled in a library libdnest, i compile the .cu file with nvcc -c step.cu, and then link with:

g++ -o main main.cpp step.o -ldnest

Now, if the template isnt present (ie i just have a void function with no mention of ModelType) all of this goes great, and it compiles and runs, but as soon as i try and include the template I get the following compile error:

../../include/SamplerImpl.h: In member function ‘bool       DNest3::Sampler<ModelType>::step() [with ModelType = Banana]’:
../../include/SamplerImpl.h:121:   instantiated from ‘void DNest3::Sampler<ModelType>::run() [with ModelType = Banana]’
main.cpp:37:   instantiated from here
../../include/SamplerImpl.h:159: error: no matching function for call to     ‘runTest(Banana*)’

and i have no idea what to do to try and fix it...

Does anyone have any ideas? If i havn't explained well enough let me know and i'll try and include more info, I dont really know whats important.

Cheers Lindley

You need to have template in the same source file that uses it for the compiler to instantiate it.

Note that C++11 introduces "extern" but those are not supported by all compilers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM