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's a simple CUDA program that performs a matrix multiplication and explains its parts:
#include <cuda_runtime.h> __global__ void matrixMultiplication(float *A, float *B, float *C, int rows, int cols) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (i < rows && j < cols) { float sum = 0; for (int k = 0; k < rows; k++) { sum += A[i * rows + k] * B[k * cols + j]; } C[i * rows + j] = sum; } } int main() { int rows = 4; int cols = 4; float *A, *B, *C; cudaMalloc((void **)&A, rows * cols * sizeof(float)); cudaMalloc((void **)&B, rows * cols * sizeof(float)); cudaMalloc((void **)&C, rows * cols * sizeof(float)); // Initialize data for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { A[i * rows + j] = i * j; B[i * cols + j] = i + j; } } // Launch kernel int blockSize = 16; int gridSize = (rows + blockSize - 1) / blockSize; matrixMultiplication<<<gridSize, blockSize>>>(A, B, C, rows, cols); // Copy result from device to host cudaMemcpy(C, A, rows * cols * sizeof(float), cudaMemcpyDeviceToHost); // Print result for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%.2f ", C[i * rows + j]); } printf("\n"); } // Free memory cudaFree(A); cudaFree(B); cudaFree(C); return 0; }Let's go through each part of the code:
- The kernel function:
__global__ void matrixMultiplication(float *A, float *B, float *C, int rows, int cols) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (i < rows && j < cols) { float sum = 0; for (int k = 0; k < rows; k++) { sum += A[i * rows + k] * B[k * cols + j]; } C[i * rows + j] = sum; } }This function performs a matrix multiplication between two matrices
AandBand stores the result in matrixC. It's declared as a global function, which means it can be executed on the GPU. The function takes five arguments:AandBare the two input matrices.Cis the output matrix.rowsandcolsare the dimensions of the matrices.
- The main function:
int main() { int rows = 4; int cols = 4; float *A, *B, *C; cudaMalloc((void **)&A, rows * cols * sizeof(float)); cudaMalloc((void **)&B, rows * cols * sizeof(float)); cudaMalloc((void **)&C, rows * cols * sizeof(float)); // Initialize data for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { A[i * rows + j] = i * j; B[i * cols + j] = i + j; } } // La
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.