c++ - How to split class definition between multiple .cpp and .cu files? -
i've got class nested classes mixing both c++, cuda , thrust. want split member definitions across number of files.
// in cls.h: #include <thrust/device_vector.h> class cls { class foo { // define in foo.cu (include "cls.h") kernelwrapper(); } class bar { // define in bar.cu (include "cls.h") thrust::device_vector a; thruststuff(); } thrust::device_vector b; purecpp(); // define in cls.cpp (include "cls.h") morethrust(); // define in cls.cu (include "cls.h") } in each definition file #include "cls.h". however, getting assortment of compiler errors no matter try, purecpp referenced not defined.
i've read thrust can used
.cufiles. because parent classclsdeclares thrust-type variablesb(and hence#includesthrust/device_vector.h), force files#includecls.hmade.cufiles?where use
extern "c"in case? supposecls.cpprequire functions in.cufiles wrapped inextern "c",.cu.cucalls,morethrust()callingbar::thruststuff()i've been made aware members of classes don't work
extern "c", have writeextern "c"wrapper function each member function?
i'm utterly confused how make work - cocktail of #includes , extern "c"s need each file?
taking small example, compiled , ran fine me
/* inside file cls.h */ #pragma once #include <thrust/device_vector.h> #include <stdio.h> class cls { public: class foo { // define in foo.cu (include "cls.h") public: void kernelwrapper(); }; class bar { // define in bar.cu (include "cls.h") thrust::device_vector<int> a; public: void thruststuff(); }; public: void purecpp(); // define in cls.cpp (include "cls.h") void morethrust(); // define in cls.cu (include "cls.h") private: thrust::device_vector<int> b; }; /* inside file foo.cu */ #include "cls.h" void cls::foo::kernelwrapper() { printf("kernelwrapper\n"); } /* inside file bar.cu */ #include "cls.h" void cls::bar::thruststuff() { printf("thrust stuff\n"); } /* inside file cls.cpp */ #include "cls.h" void cls::purecpp() { printf("purecpp\n"); } /* inside file cls.cu */ #include "cls.h" void cls::morethrust() { printf("morethrust\n"); } /* inside file main.cpp */ #include "cls.h" int main() { cls a_class; a_class.purecpp(); a_class.morethrust(); cls::bar a_class_bar; a_class_bar.thruststuff(); cls::foo a_class_foo; a_class_foo.kernelwrapper(); } running prints
purecpp
morethrust
thrust stuff
kernelwrapper
if anything, i'd bet you're using ide , it's not compilling of files, while have class member declaration in header file, it'll never find corresponding definition. exact compilation commands different, me (on linux) used
nvcc -g -g -o0 -gencode arch=compute_20,code=sm_21 -odir "src" -m -o "src/bar.d" "../src/bar.cu" nvcc --device-c -g -o0 -g -gencode arch=compute_20,code=sm_21 -x cu -o "src/bar.o" "../src/bar.cu" nvcc -g -g -o0 -gencode arch=compute_20,code=sm_21 -odir "src" -m -o "src/cls.d" "../src/cls.cu" nvcc --device-c -g -o0 -g -gencode arch=compute_20,code=sm_21 -x cu -o "src/cls.o" "../src/cls.cu" nvcc -g -g -o0 -gencode arch=compute_20,code=sm_21 -odir "src" -m -o "src/foo.d" "../src/foo.cu" nvcc --device-c -g -o0 -g -gencode arch=compute_20,code=sm_21 -x cu -o "src/foo.o" "../src/foo.cu" nvcc -g -g -o0 -gencode arch=compute_20,code=sm_21 -odir "src" -m -o "src/main.d" "../src/main.cpp" nvcc -g -g -o0 --compile -x c++ -o "src/main.o" "../src/main.cpp" nvcc -g -g -o0 -gencode arch=compute_20,code=sm_21 -odir "src" -m -o "src/clscpp.d" "../src/cls.cpp" nvcc -g -g -o0 --compile -x c++ -o "src/clscpp.o" "../src/cls.cpp" nvcc --relocatable-device-code=true -gencode arch=compute_20,code=sm_21 -link -o "split_compilation" ./src/bar.o ./src/cls.o ./src/foo.o ./src/clscpp.o ./src/main.o the idea compile of source files , link them together. example, if didn't compile , link cls.cpp file, i'd linker error on calls purecpp.
also, note if you're using actual device code, you'll have have specify __device__ and/or __host__ member functions. see this other question
Comments
Post a Comment