llama-2-13b-chat· Meta Arena Human Preference 55K
Write a simple CUDA program doing some computation, not just "hello world", and explain…
- User
Write a simple CUDA program doing some computation, not just "hello world", and explain its parts.
- 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:
__global__keyword: This indicates that the functionmyKernelis a kernel function that will be executed on the GPU.int *data, int n: These are the input parameters for the kernel function.datais a pointer to an array of integers on the host, andnis the size of the array.if (i < n): This line checks if the current thread is executing a valid index in the array. Ifiis less thann, the kernel will perform some computation on the corresponding element in the array.data[i] = data[i] + 1: This line increments the value of the current element in the array by 1.cudaMalloc()andcudaMemcpy(): These functions are used to allocate and copy data between host and device memory. In this program, we allocate device memory for the arraydataand copy the host arrayh_datato the device arrayd_data.myKernel << <gridSize, blockSize >> >(d_data, n): This line launches the kernel functionmyKernelwith 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.cudaMemcpy(): This function is used to copy the result of the kernel function back to host memory. In this program, we copy the device arrayd_databack to the host arrayh_data.printf(): This line prints the result of the computation to the console.cudaFree(): This function is used to free the device memory allocated for the arraydata.free(): This function is used to free the host memory allocated for the arrayh_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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.