arc_lupus
arc_lupus

Reputation: 4114

Declaration of cuda variables in header which is included in cpp project

I have a class containing several variables only available when compiling with nvcc, such as thrust::device_vector<>. The class declaration itself is in a header file, the implementation of it is in a .cu-file. This class is to be included in the main project which is compiled using g++, which means that the compilation of the cuda-variables will also be done using g++. This fails, obviously. Therefore, I was wondering if there are solutions for that, i.e. that my class contains variables such as thrust::device_vector<>, but still can be included in the full project?
Can I somehow declare class variables only in the .cu-file, but not in the header file?

Upvotes: 0

Views: 353

Answers (1)

talonmies
talonmies

Reputation: 72339

that my class contains variables such as thrust::device_vector<>, but still can be included in the full project?

You have to compile thrust containing code with nvcc. There is no alternative. If you include headers which include thrust into vanilla C++17 code and compile with a C++ compiler, you will get errors.

Can I somehow declare class variables only in the .cu-file, but not in the header file?

You can do that, and it would be an obvious solution. But this then poses an equally obvious question of why you actually need the structure in the vanilla C++17 code in the first place. And if the intention is to use it in the C++17 code, then you are back where you started.

I see no alternative but to refactor your code. You either need to use inheritance from a common portable structure with C++ and CUDA specializations and some way to copy construct or similar between them, or quarantine/warp all the C++17 code outside of the CUDA codepaths and use nvcc as your base compiler, and compile the C++17 separately with g++.

No amount of magical thinking can solve this in the way you seem to be imagining.

Upvotes: 1

Related Questions