GPUFlightAll performance labs

Matrix Multiplication 1: CPU Baseline

Build a correct CPU matrix multiplication baseline before moving the same computation to CUDA. For each output element C[row, col], multiply one row of A by one column of B and sum the products.

Sign in to run this lab. GPU availability and credits are shown in the app.

Languages: C++, Python, Rust

Lesson

The editor contains one host function and no CUDA. Complete row-major matrix multiplication for N = 4096. GPUFlight allocates the inputs, checks hidden output locations, and reports the total elapsed time for your function. This is the baseline used by the CUDA chapters that follow.

The serial benchmark is intentionally larger than a hand calculation. First check a 2 by 2 example locally; the displayed runner defines the timed workload. CPU library performance is outside this comparison.

What you will learn

  • Implement row-major matrix multiplication with three nested loops.
  • Establish a CPU baseline for the same matrix shape used by later CUDA labs.
  • Distinguish correctness from the elapsed-time measurement you will compare later.

Starter code

    // Start every output element at zero.
    for (int row = 0; row < N; ++row) {
        for (int col = 0; col < N; ++col) {
            C[row * N + col] = 0.0f;
        }
    }

    // Accumulate one row of A across every column of B.
    for (int row = 0; row < N; ++row) {
        for (int k = 0; k < N; ++k) {
            const float a = A[row * N + k];
            for (int col = 0; col < N; ++col) {
                // TODO: add a * B[k, col] to C[row, col].
            }
        }
    }

Sign in to run