← Blog

CUDA Programming Tutorial #1

· CUDA

This blog post contains notes I wrote while following the CUDA tutorial from Nvidia’s developer blog.

Introduction to CUDA C++

  • Converting C++ code on CPU to GPU code

    • add the __global__ specifier to turn a function into kernel code

      __global__
      void add(int n, float* sum, float* x, float* y){
      	for (int i=0;i<n;i++)
      		sum[i] = x[i]+y[i];
      }

      This specifier tells the C++ compiler that this is a function that runs on the GPU, but can be called from the host.

    • memory allocation

      We need to transfer data from the host to the device using api calls before launching the kernel. Allocation can be done with cudaMallocManaged(), which allocates memory to unified memory accessible from both the CPU and GPU.

      cudaMallocManaged(&x, N*sizeof(float));
      ... // do operations with allocated memory
      cudaFree(x);

      This can be done with cudaMallocManaged() , which will return a pointer that is accessible from both the host and device.

      When you’re done using the allocated memory, it’s important to free that space with cudaFree so you don’t run out of memory.

    • launching the kernel

      Use triple angle bracket syntax <<<>>> with the following arguments:

      • number of thread blocks
      • number of threads in a block: must be multiples of 32 (number of threads in a warp)
    • compiling

      To compile, we use nvcc , the cuda C++ compiler.

      nvcc /path/to/cuda/code.cu -o name/of/program
  • Thread Hierarchy

    • A single Nvidia GPU has multiple streaming multiprocessors (SMs) that divide the workload dispatched by the scheduler. Work is divided into thread blocks, and SMs are assigned multiple thread blocks that can run concurrently. A single thread block cannot however, be shared by multiple SMs.

    • Threads are the units of execution of GPU code. Rather than being executed in isolation, they are executed in groups, forming a hierarchy: threads form blocks, and blocks form grids. This is how the hardware organizes its workload, but how about the programmer?

    • The programmer determines the configuration of grids (such as the number of blocks and the size of the blocks) and how work should be divided with the kernel code. It’s not that each block of code sees a masked portion of the entire GPU array. Rather, the programmer assigns each thread’s workload based on the current thread’s information, using reserved variables (e.g. blockDim , threadIdx )

      __global__
      void add(int n, float *x, float *y)
      {
        int index = blockIdx.x * blockDim.x + threadIdx.x;
        int stride = blockDim.x * gridDim.x;
        for (int i = index; i < n; i += stride)
          y[i] = x[i] + y[i];
      }
  • Unified memory prefetching

    • In the tutorial, while increasing the thread count from 1 to 256 yields performance gain, it is not so when we increase the number of blocks. This is because of the memory bottleneck.

    • The kernel code needs to wait for the memory to be loaded in GPU memory before it can actually do its magic. In this case, the data transfer is slow because data is transferred per page, through individual page faults.

    • We can solve this issue by using a trick known as prefetching, which is transferring the data in batch, and making sure the data is there before the kernel code needs it.

      cudaMemPrefetchAsync(x, N*sizeof(float), 0, 0;

References