Hi all,
I need a templated kernel that use a c++ class as parameter. So i try a short code like this
#include <cstring>
#include <iostream>
typedef struct
{
double ljWell;
int n;
double Rc;
int first;
int second;
} ljt;
class A
{
public:
ljt data;
__device__ void test()
{
printf("just an int please n:%d\n",0);//works
printf("just an int please n:%d\n",this->data.n);//crash
}
__host__ void fillData()
{
std::cout<<"A allocate data:"<<cudaMallocManaged((void**)&data,sizeof(ljt))<<"\n";
cudaDeviceSynchronize();
data.ljWell = 10.0;
data.n = 5;
data.Rc = 3.2;
data.first = 1;
data.second = 0;
}
};
template <typename T>
__global__ void foo(T* f)
{
f->test();
}
int main()
{
auto toto = new A;
toto->fillData();
std::cout<<"CPU data read n:"<<toto->data.n<<"\n";
foo<<<1,1>>>(toto);
cudaDeviceSynchronize();
cudaError_t code = cudaGetLastError( );
std::cout<<"Kernel result :"<<code<<"\n";
delete(toto);
return 0;
}
the allocation of data works on CPU side however, if i try to read any value from the kernel foo(), an error 719 occur saying that bad memory access occur.
Wherre i’m wrong?