Skip to content
TheChatArchiveThe Chat Archive
Metallama-2-70b-chat· MetaHugging Face Arena Human Preference 55K

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

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-2-13b-chat’s answer.
  1. User

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

  2. Meta 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:

    1. 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 A and B and stores the result in matrix C. It's declared as a global function, which means it can be executed on the GPU. The function takes five arguments:

    • A and B are the two input matrices.
    • C is the output matrix.
    • rows and cols are the dimensions of the matrices.
    1. 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
    

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