liveBook · Manning

Global function qualifier

As explained in Grokking GPU Programming

__global__ is a CUDA function qualifier that declares a function callable from the host and executable on the device as a kernel.

__global__ marks a CUDA function as a kernel: it is typically called from the host and executes on the device. A regular function runs once per call; a kernel runs the same instructions across its launched threads. section 2.3

#include <stdio.h>
#include <cuda_runtime.h>
 
__global__ void helloKernel() {    #A
    printf("Hello, Parallel World!\n");    #B
}
 
int main() {
    helloKernel<<<1, 8>>>();    #C
    cudaDeviceSynchronize();    #D
    return 0;
}

See the qualifier on helloKernel and the launch that runs it with eight threads; each thread prints the same message. section 2.1

How Grokking GPU Programming covers it

How deep it goes
Defines the qualifier and explains kernel execution with an analogy and print example, while deferring other function qualifiers.
Examples
Uses a hello-world kernel launched with eight threads, each printing the same message.
What it assumes
Assumes basic C functions, loops, and pointers; introduces CUDA kernels and host/device roles from the beginning.
What you can do after
Build a CUDA kernel marked with __global__ and launch it to run across multiple threads.
How it connects
Connects CUDA kernels to host and device execution, thread launches, and the __device__ and __host__ qualifiers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.