Example where we should use __device__ and __host__
c, cuda, gpu
Solution
The canonical example is using C++ classes in CUDA. In the CUDA C++ model, every member function of a class must be defined in both host and device code if that class is to be instantiated in both the device and host memory spaces.
The simplest possible case would be a trivial class:
class example
{
public:
float a, b;
example(float _a, float _b) : a(_a), b(_b) {};
}
It is not possible to use this in class in CUDA, you must define the constructor in both device and host code, so:
class example
{
public:
float a, b;
__device__ __host__
example(float _a, float _b) : a(_a), b(_b) {};
}
Problem
In `CUDA` combined `__device__` and `__host__` allows a function to be call from both the `device` and the `host`. My question is: Is any example that using both will be really preferable that just defining `__device__` or `__host__`?