← Back to examples

Parallel Matrix Multiply

Computing C = A × B for 8×8 matrices. Each output cell C[i][j] is an independent dot product — computing one never needs another. That makes matrix multiply embarrassingly parallel: split the rows across workers and they all fill in at once.

C[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j] + ... + A[i][n-1]*B[n-1][j] // every C[i][j] is independent → compute them all in parallel
64 output cells. Serial = 1 worker, cell by cell. Parallel = 4 workers, each owns 2 rows.

Serial 0.0s

1 worker fills all 64 cells in order

Parallel 0.0s

4 workers, one row-band each, all at once
Press Run both. The parallel grid fills 4 row-bands simultaneously — watch it finish in a quarter of the time.

Why it's the textbook case: every worker only reads A and B (shared, no conflict) and only writes its own cells of C — no two workers ever touch the same cell, so there's no race condition. The work is uniform (every cell costs the same dot product), so simple static load balancing — a fixed band of rows each — is perfect. This is Lab 1 (OpenMP) and Lab 3 (CUDA, one thread per cell).