non __device__ methods in kernels Necessity to rewrite code to use in kernels


I am facing some inconveniences when trying to reuse some methods, which I implemented in the past, in kernels. For instance, imagine I have the following struct Integer:

[codebox]struct Integer


int m_int;


and the following method which squares the Integer object:

[codebox]void squareInteger(Integer& integer)


integer.m_int *= integer.m_int;


Now I want to reuse these codes (imagine that the real code is a big big library with lot of classes/structures and methods) in the kernels. So, I want to implement a kernel which squares each Integer object in a array:

[codebox]global void squareIntegerArray(Integer* array, unsigned int size)


unsigned int i = threadIdx.x;

if(i < size) squareInteger(array[i]);


The problem is that the method squareInteger called in the kernel squareIntegerArray was not defined as device. Therefore, I cannot reuse the methods previously implemented in the kernels.

  1. I would like to know how I can overcome this problem?

  2. Is there any way to wrapper squareInteger in a device method (say, squareInteger_device), so I can use it in the kernel?

  3. How C/C++ libraries can be reused in CUDA code, since their methods should have been specified device in order to run in the kernel?

You can’t just provide a wrapper. You’ll have to modify your declarations as device host so it can be called from both the CPU and the GPU.

Thank you. I thought there was a workaround to reuse non-CUDA code.