From d208716cc6e105c0f3d64daefe004f2f99c20842 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 21 May 2026 14:14:34 +0100 Subject: [PATCH 1/4] chore: setup autoresearch for vec_sub optimization --- tests/benchmark_vec_sub.c | 138 ++++++++++++++++++++++++++++++++++++++ tests/meson.build | 7 ++ 2 files changed, 145 insertions(+) create mode 100644 tests/benchmark_vec_sub.c diff --git a/tests/benchmark_vec_sub.c b/tests/benchmark_vec_sub.c new file mode 100644 index 0000000..ed2d36e --- /dev/null +++ b/tests/benchmark_vec_sub.c @@ -0,0 +1,138 @@ +/* + * @file benchmark_vec_sub.c + * @brief Benchmark vec_sub across vector sizes with correctness validation. + * + * Uses CLOCK_MONOTONIC_RAW for nanosecond precision. + * Runs multiple repeats per size and reports median to reduce noise. + */ + +#include "linal.h" +#include +#include +#include +#include +#include +#include + +#define REPEATS 20 +#define EPSILON 1e-6 + +static const size_t sizes[] = { + 65536, 262144, 1048576, 4194304, 16777216, 67108864 +}; +static const int n_sizes = sizeof(sizes) / sizeof(sizes[0]); + +static void fill_vector(Vector *v, uint64_t seed) +{ + double *d = v->data; + uint64_t state = seed; + for (size_t i = 0; i < v->size; i++) { + state = state * 6364136223846793005ULL + 1442695040888963407ULL; + d[i] = (double)((state >> 11) & 0x3FFFFF) / (double)(1 << 21) - 1.0; + } +} + +static double +median_double(double *arr, int n) +{ + /* Simple insertion sort for small n */ + for (int i = 1; i < n; i++) { + double key = arr[i]; + int j = i - 1; + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } + if (n % 2 == 0) + return (arr[n / 2 - 1] + arr[n / 2]) / 2.0; + return arr[n / 2]; +} + +static bool +validate(const Vector *a, const Vector *b, const Vector *result) +{ + for (size_t i = 0; i < a->size; i++) { + double expected = a->data[i] - b->data[i]; + if (fabs(result->data[i] - expected) > EPSILON) { + return false; + } + } + return true; +} + +int main(void) +{ + bool all_valid = true; + double primary_us = 0.0; + + printf("=== vec_sub benchmark (median of %d runs) ===\n", REPEATS); + printf("%-12s %12s %10s\n", "Size", "Time(us)", "Valid"); + + double times[REPEATS]; + + for (int s = 0; s < n_sizes; s++) { + size_t dim = sizes[s]; + + Vector a = vec_create(dim); + Vector b = vec_create(dim); + Vector result = vec_create(dim); + + if (!a.data || !b.data || !result.data) { + printf("FAIL allocation %zu\n", dim); + all_valid = false; + continue; + } + + fill_vector(&a, 42 + s * 7); + fill_vector(&b, 99 + s * 13); + + /* Correctness check */ + int rc = vec_sub(a, b, &result); + bool valid = (rc == 0) && validate(&a, &b, &result); + + if (!valid) { + printf("FAIL %zu: validation error (rc=%d)\n", dim, rc); + all_valid = false; + continue; + } + + /* Warmup */ + vec_sub(a, b, &result); + vec_sub(a, b, &result); + vec_sub(a, b, &result); + + /* Timed runs */ + for (int r = 0; r < REPEATS; r++) { + struct timespec ts, te; + clock_gettime(CLOCK_MONOTONIC_RAW, &ts); + vec_sub(a, b, &result); + clock_gettime(CLOCK_MONOTONIC_RAW, &te); + + double ns = (double)(te.tv_sec - ts.tv_sec) * 1e9 + + (double)(te.tv_nsec - ts.tv_nsec); + times[r] = ns / 1000.0; /* Convert to µs */ + } + + double median_us = median_double(times, REPEATS); + + if (s == n_sizes - 1) + primary_us = median_us; + + printf("%12zu %12.3f %5s\n", dim, median_us, "OK"); + + vec_free(&a); + vec_free(&b); + vec_free(&result); + } + + printf("\n=== Correctness: %s ===\n", all_valid ? "PASS" : "FAIL"); + + if (all_valid) + printf("METRIC total_us=%.3f\n", primary_us); + else + printf("METRIC total_us=999999.000\n"); + + return EXIT_SUCCESS; +} diff --git a/tests/meson.build b/tests/meson.build index 319764a..87a1930 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -154,6 +154,13 @@ benchmark_vec_add = executable( include_directories: [public_headers, generated_headers], ) +benchmark_vec_sub = executable( + 'benchmark_vec_sub', + files('benchmark_vec_sub.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 8e082257888e403563e2a8fe480c3e25191812aa Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 21 May 2026 14:16:17 +0100 Subject: [PATCH 2/4] Apply vec_add pattern: OpenMP parallel (2 threads), block scheduling (163840), prefetch, omp simd safelen(2) aligned(32/16) Result: {"status":"keep","total_us":74900.7} --- src/linal_vector.c | 50 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/linal_vector.c b/src/linal_vector.c index 42a4c30..d875514 100644 --- a/src/linal_vector.c +++ b/src/linal_vector.c @@ -162,7 +162,11 @@ vec_add(const Vector a, const Vector b, Vector *result) /** * @brief Subtract two vectors element-wise. + * + * Uses OpenMP parallel + SIMD for large vectors (2 threads to limit + * memory bandwidth pressure) and omp simd for the serial path. */ +__attribute__((optimize("O3"))) int vec_sub(const Vector a, const Vector b, Vector *result) { @@ -178,8 +182,50 @@ vec_sub(const Vector a, const Vector b, Vector *result) return -1; } - for (size_t i = 0; i < a.size; i++) { - result->data[i] = a.data[i] - b.data[i]; + size_t count = a.size; + +#if defined(_OPENMP) + /* 2 threads — balances parallelism with memory bandwidth for 3-array access */ + if (count >= 262144) { + const double *A = a.data; + const double *B = b.data; + double *R = result->data; + /* Process in 64K blocks for cache-friendly static scheduling */ + size_t block_size = 163840; + size_t full_blocks = count / block_size; + size_t tail_start = full_blocks * block_size; +#pragma omp parallel for num_threads(2) schedule(static, 2) + for (size_t block = 0; block < full_blocks; block++) { + size_t start = block * block_size; + /* Prefetch next 2 blocks for deeper lookahead */ + size_t pf1 = start + block_size; + size_t pf2 = start + 2 * block_size; + __builtin_prefetch(&A[pf1], 0, 3); + __builtin_prefetch(&B[pf1], 0, 3); + __builtin_prefetch(&R[pf1], 1, 3); + if (pf2 < count) { + __builtin_prefetch(&A[pf2], 0, 1); + __builtin_prefetch(&B[pf2], 0, 1); + __builtin_prefetch(&R[pf2], 1, 1); + } + #pragma omp simd safelen(2) aligned(A:32) aligned(B:32) aligned(R:32) + for (size_t i = start; i < start + block_size; i++) { + R[i] = A[i] - B[i]; + } + } + /* Tail without prefetch */ + #pragma omp simd safelen(2) aligned(A:16) aligned(B:16) aligned(R:16) + for (size_t i = tail_start; i < count; i++) { + R[i] = A[i] - B[i]; + } + } else +#endif + { + /* Serial SIMD path */ + #pragma omp simd safelen(2) + for (size_t i = 0; i < count; i++) { + result->data[i] = a.data[i] - b.data[i]; + } } return 0; From 7b734b9d69986d5393a6292217aca1a4d6ce802b Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 21 May 2026 14:18:51 +0100 Subject: [PATCH 3/4] =?UTF-8?q?120K=20block=20size=20(122880)=20with=202?= =?UTF-8?q?=20threads:=2073886=20=C2=B5s=20(1.4%=20vs=20baseline).=20Bette?= =?UTF-8?q?r=20than=20163840=20block=20size.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Result: {"status":"keep","total_us":73886.5} --- src/linal_vector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/linal_vector.c b/src/linal_vector.c index d875514..ec6cc6a 100644 --- a/src/linal_vector.c +++ b/src/linal_vector.c @@ -190,8 +190,8 @@ vec_sub(const Vector a, const Vector b, Vector *result) const double *A = a.data; const double *B = b.data; double *R = result->data; - /* Process in 64K blocks for cache-friendly static scheduling */ - size_t block_size = 163840; + /* Process in 120K blocks for cache-friendly static scheduling */ + size_t block_size = 122880; size_t full_blocks = count / block_size; size_t tail_start = full_blocks * block_size; #pragma omp parallel for num_threads(2) schedule(static, 2) From 6e52324fbe91d79003c648f6bc66370084cc9f55 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 21 May 2026 14:19:43 +0100 Subject: [PATCH 4/4] =?UTF-8?q?112K=20block=20size=20(114688)=20with=202?= =?UTF-8?q?=20threads:=2073558=20=C2=B5s=20(1.9%=20vs=20baseline,=200.4%?= =?UTF-8?q?=20vs=20120K).=20New=20best!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Result: {"status":"keep","total_us":73558.3} --- src/linal_vector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/linal_vector.c b/src/linal_vector.c index ec6cc6a..e1adc87 100644 --- a/src/linal_vector.c +++ b/src/linal_vector.c @@ -190,8 +190,8 @@ vec_sub(const Vector a, const Vector b, Vector *result) const double *A = a.data; const double *B = b.data; double *R = result->data; - /* Process in 120K blocks for cache-friendly static scheduling */ - size_t block_size = 122880; + /* Process in 112K blocks for cache-friendly static scheduling */ + size_t block_size = 114688; size_t full_blocks = count / block_size; size_t tail_start = full_blocks * block_size; #pragma omp parallel for num_threads(2) schedule(static, 2)