• Advertisement
Sign in to follow this  

[CUDA] Passing structure/class instance to kernel doesn't work if copy constructor is present

This topic is 2262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently observed weird behaviour in CUDA. It turns out that when a structure (or class) has a copy constructor in it, passing an instance of that structure (class) will fail. Take a look at the following code:


struct Test
{
float value1;
float value2;

Test() {}
};



__global__ void testuj(Test value, Test* data)
{
data[0].value1 = value.value2;
}



void KKK::doit2(void)
{
Test* data_host;
Test* data_dev;

data_host = (Test*)malloc(sizeof(Test));
cudaMalloc((void**)&data_dev, sizeof(Test));

Test t;
t.value1 = 5.0f;
t.value2 = 10.0f;

dim3 blocksNum(1, 1);
int threadsPerBlock = 1;
testuj<<<blocksNum, threadsPerBlock>>>(t, data_dev);

cudaMemcpy(data_host, data_dev, sizeof(Test), cudaMemcpyDeviceToHost);

printf("%f\n", data_host[0].value1);
}


it works perfectly fine. Print will output 10.0. However, if you add a copy constructor to Test class:


__device__ Test(const Test& t)
{
value1 = t.value1;
value2 = t.value2;
}


the program will print 0.0. Why is this happening? An interesting thing is that when I remove the __device__ qualifier it works fine.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement