I have an existing C++ program, and I want to migrate it to the GPU version. The kernel function needs to access class objects defined in the host function. For example, a stringstream object will be used in threads. However, it fails to pass the compiling in Cuda. How could the kernel function access this kind of class objects defined in the host functions?
Here is an example.
#include <cstdio>
#include <sstream>
using namespace std;
__global__ void kernel(stringstream * sstr)
{
printf("%s\n", sstr->str());
}
int main(int argc, char ** argv)
{
stringstream * sstr;
cudaMallocManaged(&sstr, sizeof(stringstream));
*sstr << "Hello world\n";
kernel<<<32, 32>>>(sstr);
cudaDeviceSynchronize();
cudaFree(sstr);
return 0;
}
I got the following compile error.
$ nvcc -o bin src.cu
src.cu(8): warning: non-POD class type passed through ellipsis
src.cu(8): error: calling a __host__ function("std::__cxx11::basic_stringstream<char, ::std::char_traits<char> , std::allocator<char> > ::str const") from a __global__ function("kernel") is not allowed
src.cu(8): error: identifier "std::__cxx11::basic_stringstream<char, ::std::char_traits<char> , std::allocator<char> > ::str const" is undefined in device code
src.cu(8): error: calling a __host__ function("std::__cxx11::basic_string<char, ::std::char_traits<char> , std::allocator<char> > ::~basic_string") from a __global__ function("kernel") is not allowed
src.cu(8): error: identifier "std::__cxx11::basic_string<char, ::std::char_traits<char> , std::allocator<char> > ::~basic_string" is undefined in device code
4 errors detected in the compilation of "/tmp/tmpxft_00003bd0_00000000-8_src.cpp1.ii".