Skip to content
The Chat Archive
llama-2-13b-chat· Meta Arena Human Preference 55K

Write a simple CUDA program doing some computation, not just "hello world", and explain…

  1. User

    Write a simple CUDA program doing some computation, not just "hello world", and explain its parts.

  2. Assistant

    Sure! Here is a simple CUDA program that performs some computation and explains its parts:

    #include <cuda.h>
    #include <cuda_runtime.h>
    
    __global__ void myKernel(int *data, int n) {
      int i = blockIdx.x * blockDim.x + threadIdx.x;
      if (i < n) {
        data[i] = data[i] + 1;
      }
    }
    
    int main() {
      int *h_data; // host memory for data
      int *d_data; // device memory for data
      int n = 1024; // size of data
    
      // allocate host memory for data
      h_data = (int *)malloc(n * sizeof(int));
      for (int i = 0; i < n; i++) {
        h_data[i] = i;
      }
    
      // allocate device memory for data
      cudaMalloc((void **)&d_data, n * sizeof(int));
    
      // copy host data to device data
      cudaMemcpy(d_data, h_data, n * sizeof(int), cudaMemcpyHostToDevice);
    
      // set block size and grid size
      int blockSize = 16;
      int gridSize = (n + blockSize - 1) / blockSize;
    
      // launch kernel
      myKernel << <gridSize, blockSize >> >(d_data, n);
    
      // copy device data back to host
      cudaMemcpy(h_data, d_data, n * sizeof(int), cudaMemcpyDeviceToHost);
    
      // print results
      for (int i = 0; i < n; i++) {
        printf("%d ", h_data[i]);
      }
      printf("\n");
    
      // free device memory
      cudaFree(d_data);
    
      // free host memory
      free(h_data);
    
      return 0;
    }
    

    Here's an explanation of the parts of the program:

    1. __global__ keyword: This indicates that the function myKernel is a kernel function that will be executed on the GPU.
    2. int *data, int n: These are the input parameters for the kernel function. data is a pointer to an array of integers on the host, and n is the size of the array.
    3. if (i < n): This line checks if the current thread is executing a valid index in the array. If i is less than n, the kernel will perform some computation on the corresponding element in the array.
    4. data[i] = data[i] + 1: This line increments the value of the current element in the array by 1.
    5. cudaMalloc() and cudaMemcpy(): These functions are used to allocate and copy data between host and device memory. In this program, we allocate device memory for the array data and copy the host array h_data to the device array d_data.
    6. myKernel << <gridSize, blockSize >> >(d_data, n): This line launches the kernel function myKernel with the specified block size and grid size. The kernel will be executed on the GPU in parallel, with each block processing a portion of the array.
    7. cudaMemcpy(): This function is used to copy the result of the kernel function back to host memory. In this program, we copy the device array d_data back to the host array h_data.
    8. printf(): This line prints the result of the computation to the console.
    9. cudaFree(): This function is used to free the device memory allocated for the array data.
    10. free(): This function is used to free the host memory allocated for the array h_data.

    This program demonstrates a simple computation on a large array, using the CUDA kernel function myKernel. The kernel function performs a simple increment operation on each element in the array, and the host code copies the result back to host memory and prints it to the console.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.