Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
dbee8c6
chore: add vec_add benchmark and autoresearch setup
aajll May 21, 2026
8c2b3d9
AVX2 unrolled 8-wide + ivdep: slight improvement at 67M (76280 vs 766…
aajll May 21, 2026
bb8ae18
omp simd safelen(2): 2.6% improvement at 67M (74626 vs 76625) — omp s…
aajll May 21, 2026
baf063c
omp parallel for simd num_threads(2) safelen(2): 3.7% improvement at …
aajll May 21, 2026
4d14da3
Block-based parallel (64K blocks): 74052 µs (3.4% vs baseline) — cach…
aajll May 21, 2026
3314d30
128K blocks: 73886 µs (3.6% vs baseline) — larger blocks reduce overh…
aajll May 21, 2026
3d3727c
128K blocks + block prefetch: 73746 µs (3.8% vs baseline) — prefetchi…
aajll May 21, 2026
f852753
96K blocks + prefetch: 73739 µs (3.8% vs baseline) — slightly better …
aajll May 21, 2026
80eb47e
Split loop (no branch): 74111 µs (3.3% vs baseline) — branch-free mai…
aajll May 21, 2026
7d4c7c0
80K split loop: 74037 µs (3.4% vs baseline) — slightly better than 96K
aajll May 21, 2026
ab97e37
88K split loop: 73891 µs (3.6% vs baseline) — better than 80K
aajll May 21, 2026
02664e7
2-block-ahead prefetch: 73949 µs (3.5% vs baseline) — deeper prefetch…
aajll May 21, 2026
4fe591b
aligned(16) hint: 74074 µs (3.3% vs baseline) — slightly better at 67…
aajll May 21, 2026
31b10f6
safelen(4)+aligned(16): 73780 µs (3.8% vs baseline) — slightly better…
aajll May 21, 2026
f82df5e
Tail safelen(4)+aligned: 73829 µs (3.7% vs baseline) — slightly bette…
aajll May 21, 2026
55bf7ce
96K blocks: 73891 µs (3.6% vs baseline) — better than 88K (73891 vs 7…
aajll May 21, 2026
44f744b
safelen(8): 74122 µs (3.1% vs baseline) — better at 67M (74122 vs 743…
aajll May 21, 2026
31e9f32
aligned(32): 73627 µs (3.9% vs baseline) — new best! 67M better (7362…
aajll May 21, 2026
005afe2
aligned(16) safe version: 73824 µs (3.6% vs baseline) — same at 67M a…
aajll May 21, 2026
b26b635
safelen(2)+aligned: 73805 µs (3.6% vs baseline) — better at 67M (7380…
aajll May 21, 2026
567760f
104K blocks: 73965 µs (3.5% vs baseline) — better at 67M (73965 vs 74…
aajll May 21, 2026
47a7400
Tail no safelen: 73869 µs (3.6% vs baseline) — same at 67M as safelen…
aajll May 21, 2026
3a60694
120K blocks: 73736 µs (3.7% vs baseline) — better at 67M (73736 vs 73…
aajll May 21, 2026
bbf7ede
Tail safelen(2): 73769 µs (3.7% vs baseline) — same at 67M as no safe…
aajll May 21, 2026
3999a91
108K blocks: 73803 µs (3.7% vs baseline) — better at 67M (73803 vs 73…
aajll May 21, 2026
a936462
512K threshold: 73842 µs (3.7% vs baseline) — better at 67M (73842 vs…
aajll May 21, 2026
1c8daa4
aligned(8) main loop: 73707 µs (3.8% vs baseline) — better at 67M (73…
aajll May 21, 2026
71f7c6e
112K blocks: 73858 µs (3.7% vs baseline) — better at 67M (73858 vs 74…
aajll May 21, 2026
39e3d43
R[pf2] prefetch: 73777 µs (3.7% vs baseline) — better at 67M (73777 v…
aajll May 21, 2026
5755eba
104K blocks: 73639 µs (3.9% vs baseline) — better at 67M (73639 vs 73…
aajll May 21, 2026
44a47b8
schedule(static,2): 73940 µs (3.5% vs baseline) — same at 67M (73940 …
aajll May 21, 2026
534a381
256K threshold: 73644 µs (3.9% vs baseline) — better at 67M (73644 vs…
aajll May 21, 2026
5df3977
128K blocks: 73913 µs (3.5% vs baseline) — better at 67M (73913 vs 74…
aajll May 21, 2026
a952a88
aligned(32) main loop: 73854 µs (3.6% vs baseline) — better at 67M (7…
aajll May 21, 2026
4d98017
160K blocks: 73775 µs (3.7% vs baseline) — better at 67M (73775 vs 73…
aajll May 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/linal_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ vec_free(Vector *v)

/**
* @brief Add 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_add(const Vector a, const Vector b, Vector *result)
{
Expand All @@ -107,8 +111,50 @@ vec_add(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;
Expand Down
138 changes: 138 additions & 0 deletions tests/benchmark_vec_add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* @file benchmark_vec_add.c
* @brief Benchmark vec_add 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 <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#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_add 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_add(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_add(a, b, &result);
vec_add(a, b, &result);
vec_add(a, b, &result);

/* Timed runs */
for (int r = 0; r < REPEATS; r++) {
struct timespec ts, te;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
vec_add(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;
}
7 changes: 7 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ benchmark_vec_norm_l2 = executable(
include_directories: [public_headers, generated_headers],
)

benchmark_vec_add = executable(
'benchmark_vec_add',
files('benchmark_vec_add.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)
Expand Down
Loading