5

In order to call the same function from host code and GPU kernel, Do I have to keep the two copies of the same function as below:

int sum(int a, int b){
return a+b;
}

__device int sumGPU(int a, int b){
return a+b;
}

Or is there any technique to keep/manage a single copy of the function?

JBentley
  • 6,099
  • 5
  • 37
  • 72
Imran
  • 642
  • 6
  • 25

1 Answers1

19

You just have to add the __host__ keyword to be able to call call a function from host or device.

__host__ __device__ int sum(int a, int b){
  return a+b;
}
hubs
  • 1,779
  • 13
  • 19