andersch.dev

<2024-10-26 Sat>

CUDA

CUDA (Compute Unified Device Architecture) is a computing platform for NVIDIA GPUs that allows writing code for GPGPU tasks.

The CUDA toolchain includes:

Code Examples

#include <stdio.h>

/* __global__, __device__, and __host__ specify where functions are executed */

__global__ void helloFromGPU() { // kernel function running on gpu
    printf("Hello from GPU! Thread ID: %d\n", threadIdx.x);
}

int main() {
    printf("Hello from CPU!\n");
    helloFromGPU<<<1, 10>>>(); // launch kernel with 1 block and 10 threads
    cudaDeviceSynchronize();   // wait for gpu to finish
    return 0;
}
nvcc hello_world.cu -o hello_world
./hello_world

Resources