![]() |
VOOZH | about |
__host__ specifier is a function type qualifier used to explicitly communicate to the compiler that a specific function is designed to reside and execute on the Host (CPU). It serves as a boundary marker, ensuring that the logic within the function is handled by the standard system processor rather than the parallel cores of the GPU.
By default, every function in a .cu file is considered a __host__ function unless specified otherwise. This means standard C++ code behaves as host code automatically.
Example: This example demonstrates how a function is explicitly marked to run on the CPU to perform standard setup tasks.
Output
The Host (CPU) is initializing the application...
Explanation:
__device__ acts as the counterpart to the host. It declares a function that is executed on the Device (GPU) and is only callable from other GPU-side code, such as a __global__ kernel. These are typically used as "helper functions" to perform repeated calculations across thousands of parallel threads.
Output
Result from GPU: 15
Explanation:
In many cases, we need a utility function (like a math formula) to work on both the CPU and the GPU. Instead of writing the code twice, CUDA allows to use both specifiers together. This allows compiler to generate two versions of the same function.
Example: This code uses a single function definition that works on both the CPU and the GPU.
Output
CPU Square: 16.00
GPU Square: 16.00
Explanation: