From 03cb59da758f0bf6dd2a8b7845b4c5c0af62e67d Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 1 May 2026 12:36:44 +0100 Subject: [PATCH 1/6] chore: initialize autoresearch for mat_transpose optimization --- tests/meson.build | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/meson.build b/tests/meson.build index 58042bc..ace7745 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -125,6 +125,14 @@ test_numerics = executable( include_directories: [public_headers, generated_headers], ) +# Benchmarks +benchmark_mat_transpose = executable( + 'benchmark_mat_transpose', + files('benchmark_mat_transpose.c'), + dependencies: [linal_dep], + include_directories: [public_headers, generated_headers], +) + # Run all tests test('mat_create', test_mat_create) test('mat_memory', test_mat_memory) From 077a4ebf485482bb46cbe58095be9b8231315a76 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 1 May 2026 12:46:04 +0100 Subject: [PATCH 2/6] =?UTF-8?q?OpenMP=20parallel=20block=20transpose.=208?= =?UTF-8?q?=20threads=20for=20large=20matrices,=204=20for=20medium.=205.8x?= =?UTF-8?q?=20speedup=20(1.17=E2=86=920.20ms).=20Each=20thread=20has=20its?= =?UTF-8?q?=20own=20stack=20buffer.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Result: {"status":"keep","mat_transpose_ms":0.20144,"transpose_256_ms":0.081,"transpose_64_ms":0.005} --- src/linal.c | 112 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 29 deletions(-) diff --git a/src/linal.c b/src/linal.c index 627f043..717ffd9 100644 --- a/src/linal.c +++ b/src/linal.c @@ -353,41 +353,95 @@ mat_transpose(const Matrix m, Matrix *result) size_t rows = m.rows; size_t cols = m.cols; + size_t total = rows * cols; - /* Block size tuned for L1 cache (~32KB). 64×64 doubles ≈ 32KB. */ + /* Block size tuned for L1 cache. 64×64 doubles = 32KB. */ #define TRANSPOSE_BLOCK 64 if (rows > TRANSPOSE_BLOCK && cols > TRANSPOSE_BLOCK) { - const double *M = m.data; - double *R = result->data; - /* Buffered block transpose: load block row-by-row (sequential reads), - * then write transposed with sequential dest writes. - * Block size chosen so buffer fits in L1 cache (~32KB for 64x64). */ - double buf[TRANSPOSE_BLOCK * TRANSPOSE_BLOCK]; - for (size_t ii = 0; ii < rows; ii += TRANSPOSE_BLOCK) { - size_t i_end = - (ii + TRANSPOSE_BLOCK < rows) - ? ii + TRANSPOSE_BLOCK : rows; - size_t bh = i_end - ii; - for (size_t jj = 0; jj < cols; jj += TRANSPOSE_BLOCK) { - size_t j_end = (jj + TRANSPOSE_BLOCK < cols) - ? jj + TRANSPOSE_BLOCK - : cols; - size_t bw = j_end - jj; - - /* Load source block into buffer row-by-row */ - for (size_t bi = 0; bi < bh; bi++) { - const double *src = M + (ii + bi) * cols + jj; - double *dst_buf = buf + bi * bw; - memcpy(dst_buf, src, bw * sizeof(double)); - } + const double *M = m.data; + double *R = result->data; - /* Write transposed: outer=bj(dest row), inner=bi(dest col) - * This gives sequential writes along each dest row. */ - for (size_t bj = 0; bj < bw; bj++) { - double *dst_row = R + (jj + bj) * rows + ii; +#if defined(_OPENMP) + /* Parallel block transpose for large matrices. + * Each thread gets its own buffer via alloca. + * Threshold: 262144 elements (512×512) to amortize thread overhead. */ + if (total >= 262144) { + int linal_omp_threads = (total < 1048576) ? 4 : 8; +#pragma omp parallel for num_threads(linal_omp_threads) schedule(static) + for (long bii = 0; bii < (long)(rows / TRANSPOSE_BLOCK + + (rows % TRANSPOSE_BLOCK > 0)); + bii++) { + size_t ii = (size_t)bii * TRANSPOSE_BLOCK; + size_t i_end = (ii + TRANSPOSE_BLOCK < rows) + ? ii + TRANSPOSE_BLOCK + : rows; + size_t bh = i_end - ii; + double buf[TRANSPOSE_BLOCK * TRANSPOSE_BLOCK]; + + for (size_t jj = 0; jj < cols; + jj += TRANSPOSE_BLOCK) { + size_t j_end = + (jj + TRANSPOSE_BLOCK < cols) + ? jj + TRANSPOSE_BLOCK + : cols; + size_t bw = j_end - jj; + + /* Load source block row-by-row (sequential) */ + for (size_t bi = 0; bi < bh; bi++) { + memcpy(buf + bi * bw, + M + (ii + bi) * cols + jj, + bw * sizeof(double)); + } + + /* Write transposed: sequential dest writes */ + for (size_t bj = 0; bj < bw; bj++) { + double *dst_row = + R + (jj + bj) * rows + ii; + for (size_t bi = 0; bi < bh; + bi++) { + dst_row[bi] = + buf[bi * bw + bj]; + } + } + } + } + } else +#endif + { + /* Serial: same buffered block transpose */ + double buf[TRANSPOSE_BLOCK * TRANSPOSE_BLOCK]; + for (size_t ii = 0; ii < rows; + ii += TRANSPOSE_BLOCK) { + size_t i_end = + (ii + TRANSPOSE_BLOCK < rows) + ? ii + TRANSPOSE_BLOCK + : rows; + size_t bh = i_end - ii; + for (size_t jj = 0; jj < cols; + jj += TRANSPOSE_BLOCK) { + size_t j_end = + (jj + TRANSPOSE_BLOCK < cols) + ? jj + TRANSPOSE_BLOCK + : cols; + size_t bw = j_end - jj; + + /* Load source block row-by-row (sequential) */ for (size_t bi = 0; bi < bh; bi++) { - dst_row[bi] = buf[bi * bw + bj]; + memcpy(buf + bi * bw, + M + (ii + bi) * cols + jj, + bw * sizeof(double)); + } + + /* Write transposed: sequential dest writes */ + for (size_t bj = 0; bj < bw; bj++) { + double *dst_row = + R + (jj + bj) * rows + ii; + for (size_t bi = 0; bi < bh; + bi++) { + dst_row[bi] = + buf[bi * bw + bj]; + } } } } From e5721f9684c50a2f398b57d16b0ef8f7837c3ec2 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 1 May 2026 12:48:19 +0100 Subject: [PATCH 3/6] =?UTF-8?q?Adaptive=20threads:=202=20for=20256=C2=B2,?= =?UTF-8?q?=204=20for=20512=C2=B2,=2012=20for=201024=C2=B2.=20Lower=20thre?= =?UTF-8?q?shold=20(65536).=200.18ms=20(-10%=20vs=20prev).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Result: {"status":"keep","mat_transpose_ms":0.18468,"transpose_256_ms":0.064,"transpose_64_ms":0.004} --- src/linal.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/linal.c b/src/linal.c index 717ffd9..83e173b 100644 --- a/src/linal.c +++ b/src/linal.c @@ -364,10 +364,16 @@ mat_transpose(const Matrix m, Matrix *result) #if defined(_OPENMP) /* Parallel block transpose for large matrices. - * Each thread gets its own buffer via alloca. - * Threshold: 262144 elements (512×512) to amortize thread overhead. */ - if (total >= 262144) { - int linal_omp_threads = (total < 1048576) ? 4 : 8; + * Each thread gets its own stack buffer. + * Threshold: 65536 elements (256×256). */ + if (total >= 65536) { + int linal_omp_threads; + if (total < 262144) + linal_omp_threads = 2; + else if (total < 1048576) + linal_omp_threads = 4; + else + linal_omp_threads = 12; #pragma omp parallel for num_threads(linal_omp_threads) schedule(static) for (long bii = 0; bii < (long)(rows / TRANSPOSE_BLOCK + (rows % TRANSPOSE_BLOCK > 0)); From 7fd66f355039a3012fea7f22dc711f97c0d9fab4 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 1 May 2026 12:57:02 +0100 Subject: [PATCH 4/6] =?UTF-8?q?AVX2+FMA=20target=20+=2048=C3=9748=20block?= =?UTF-8?q?=20size.=200.15ms=20(-17%=20vs=20prev).=20AVX2=20enables=20wide?= =?UTF-8?q?r=20SIMD=20for=20memcpy/inner=20loop.=2048=20blocks=20=3D=20mor?= =?UTF-8?q?e=20parallelism.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Result: {"status":"keep","mat_transpose_ms":0.14722,"transpose_256_ms":0.079,"transpose_64_ms":0.004} --- src/linal.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/linal.c b/src/linal.c index 83e173b..9468f69 100644 --- a/src/linal.c +++ b/src/linal.c @@ -328,6 +328,8 @@ mat_scale(const Matrix m, double scalar, Matrix *result) * dimensions m.cols x m.rows) * @return 0 on success, -1 if input is invalid or dimensions mismatch */ +#pragma GCC push_options +#pragma GCC target("avx2,fma") __attribute__((optimize("O3"))) int mat_transpose(const Matrix m, Matrix *result) @@ -355,8 +357,9 @@ mat_transpose(const Matrix m, Matrix *result) size_t cols = m.cols; size_t total = rows * cols; - /* Block size tuned for L1 cache. 64×64 doubles = 32KB. */ -#define TRANSPOSE_BLOCK 64 + /* Block size: 48×48 doubles = 18KB. Fits L1, more blocks than 64 for + * better parallelism with 12 threads. */ +#define TRANSPOSE_BLOCK 48 if (rows > TRANSPOSE_BLOCK && cols > TRANSPOSE_BLOCK) { const double *M = m.data; @@ -465,6 +468,7 @@ mat_transpose(const Matrix m, Matrix *result) return 0; } +#pragma GCC pop_options /** * @brief Subtract two matrices element-wise (A - B). From e85ab53e6b01aa187e1dbdc1b48c96541666bca7 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 1 May 2026 13:19:42 +0100 Subject: [PATCH 5/6] =?UTF-8?q?44=C3=9744=20block=20size.=200.14ms=20(-5%?= =?UTF-8?q?=20vs=2048=20block).=20More=20blocks=20(23.5/dim=20vs=2021.3)?= =?UTF-8?q?=20=3D=20better=20thread=20distribution=20with=2012=20threads.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Result: {"status":"keep","mat_transpose_ms":0.14242,"transpose_256_ms":0.059,"transpose_64_ms":0.003} --- src/linal.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/linal.c b/src/linal.c index 9468f69..b8df13a 100644 --- a/src/linal.c +++ b/src/linal.c @@ -357,9 +357,9 @@ mat_transpose(const Matrix m, Matrix *result) size_t cols = m.cols; size_t total = rows * cols; - /* Block size: 48×48 doubles = 18KB. Fits L1, more blocks than 64 for - * better parallelism with 12 threads. */ -#define TRANSPOSE_BLOCK 48 + /* Block size: 44×44 doubles = 15.5KB. Fits L1, 23.5 blocks/dimension + * for 1024 — fine-grained parallelism with 12 threads. */ +#define TRANSPOSE_BLOCK 44 if (rows > TRANSPOSE_BLOCK && cols > TRANSPOSE_BLOCK) { const double *M = m.data; From f224ed40e733513f4e87f699b06a9ea869f87110 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 1 May 2026 13:47:50 +0100 Subject: [PATCH 6/6] =?UTF-8?q?#pragma=20GCC=20unroll=204=20on=20inner=20w?= =?UTF-8?q?rite=20loop.=200.140ms=20(-1.6%=20vs=200.142ms):=20marginal=20b?= =?UTF-8?q?ut=20positive=20=E2=80=94=20explicit=20unroll=20helps=20compile?= =?UTF-8?q?r=20interleave=20strided=20buffer=20reads.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Result: {"status":"keep","mat_transpose_ms":0.14017,"transpose_256_ms":0.083,"transpose_64_ms":0.005} --- src/linal.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/linal.c b/src/linal.c index b8df13a..5a67f9c 100644 --- a/src/linal.c +++ b/src/linal.c @@ -403,7 +403,9 @@ mat_transpose(const Matrix m, Matrix *result) bw * sizeof(double)); } - /* Write transposed: sequential dest writes */ + /* Write transposed: sequential dest writes. + * Explicit unroll for more ILP in strided buffer reads. */ +#pragma GCC unroll 4 for (size_t bj = 0; bj < bw; bj++) { double *dst_row = R + (jj + bj) * rows + ii;