Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 2496 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!