omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / algebra / bench_mixed_algebra.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* MA-2 benchmark: Omega-X (ternary W x int8 x -> int32 y) realizations on one |
| 18 | * pinned Cortex-X925 core of the Grace CPU. spec/mixed-algebra-ma2.md |
| 19 | * |
| 20 | * Grid: n in {1024, 4096, 16384}, m in {1, 64, 4096}, weight sparsity |
| 21 | * (fraction of zeros) in {0, 0.3, 0.6, 0.9}. For each cell and realization: |
| 22 | * pack cost (separate), per-call latency samples (best and median of >= 9 |
| 23 | * interleaved blocks, CLOCK_MONOTONIC), bytes read, footprint, and the |
| 24 | * roofline floor = bytes moved / read bandwidth measured by the stream kernel |
| 25 | * in this binary at the matching working-set size (hot, repeated calls). |
| 26 | * Shared machine: each block reads /proc/thread-self/schedstat run_delay and |
| 27 | * is retried when the thread waited for a CPU more than 2% of the block. |
| 28 | * Output: one JSON receipt (argv[1]); every run output is checked bit-exact |
| 29 | * against the naive oracle before timing. |
| 30 | * |
| 31 | * Usage: bench_mixed_algebra OUT.json (env OMA_BENCH_CPU pins a cpu id; |
| 32 | * OMA_BENCH_QUICK=1 shrinks the grid for a smoke run). */ |
| 33 | #define _GNU_SOURCE |
| 34 | #include "algebra/realize_common.h" |
| 35 | |
| 36 | #include <arm_neon.h> |
| 37 | #include <dirent.h> |
| 38 | #include <errno.h> |
| 39 | #include <math.h> |
| 40 | #include <sched.h> |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | #include <sys/utsname.h> |
| 45 | #include <time.h> |
| 46 | #include <unistd.h> |
| 47 | |
| 48 | #define NSAMPLES 9 |
| 49 | #define MAX_RETRY 8 |
| 50 | #define TARGET_BLOCK_NS 300000.0 /* 0.3 ms per timed block */ |
| 51 | #define MAX_RZ 16 |
| 52 | |
| 53 | static uint64_t g_rng = 0x6d61332d62656e63ULL; |
| 54 | static uint64_t rnd(void) { |
| 55 | uint64_t z = (g_rng += 0x9e3779b97f4a7c15ULL); |
| 56 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; |
| 57 | z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; |
| 58 | return z ^ (z >> 31); |
| 59 | } |
| 60 | |
| 61 | static double now_ns(void) { |
| 62 | struct timespec ts; |
| 63 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 64 | return (double)ts.tv_sec * 1e9 + (double)ts.tv_nsec; |
| 65 | } |
| 66 | |
| 67 | static uint64_t run_delay_ns(void) { |
| 68 | FILE *f = fopen("/proc/thread-self/schedstat", "r"); |
| 69 | unsigned long long a = 0, b = 0; |
| 70 | if (f) { |
| 71 | if (fscanf(f, "%llu %llu", &a, &b) != 2) b = 0; |
| 72 | fclose(f); |
| 73 | } |
| 74 | return (uint64_t)b; |
| 75 | } |
| 76 | |
| 77 | static void read_loadavg(char *buf, size_t cap) { |
| 78 | FILE *f = fopen("/proc/loadavg", "r"); |
| 79 | buf[0] = 0; |
| 80 | if (f) { |
| 81 | double a, b, c; |
| 82 | if (fscanf(f, "%lf %lf %lf", &a, &b, &c) == 3) snprintf(buf, cap, "%.2f %.2f %.2f", a, b, c); |
| 83 | fclose(f); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static int read_thermal_c(void) { |
| 88 | FILE *f = fopen("/sys/class/thermal/thermal_zone0/temp", "r"); |
| 89 | long v = -1000; |
| 90 | if (f) { |
| 91 | if (fscanf(f, "%ld", &v) != 1) v = -1000; |
| 92 | fclose(f); |
| 93 | } |
| 94 | return (int)(v / 1000); |
| 95 | } |
| 96 | |
| 97 | /* ---- core selection: X925 = MIDR part 0xd85, A725 = 0xd87 ---- */ |
| 98 | static int cpu_part[256]; |
| 99 | static int ncpu_seen; |
| 100 | |
| 101 | static void read_cpu_parts(void) { |
| 102 | FILE *f = fopen("/proc/cpuinfo", "r"); |
| 103 | char line[256]; |
| 104 | int cur = -1; |
| 105 | for (int i = 0; i < 256; i++) cpu_part[i] = -1; |
| 106 | if (!f) return; |
| 107 | while (fgets(line, sizeof line, f)) { |
| 108 | int v; |
| 109 | unsigned part; |
| 110 | if (sscanf(line, "processor : %d", &v) == 1) cur = v; |
| 111 | else if (sscanf(line, "CPU part : %x", &part) == 1 && cur >= 0 && cur < 256) { |
| 112 | cpu_part[cur] = (int)part; |
| 113 | if (cur + 1 > ncpu_seen) ncpu_seen = cur + 1; |
| 114 | } |
| 115 | } |
| 116 | fclose(f); |
| 117 | } |
| 118 | |
| 119 | static int read_cpu_idle(unsigned long long idle[256], unsigned long long total[256]) { |
| 120 | FILE *f = fopen("/proc/stat", "r"); |
| 121 | char line[512]; |
| 122 | if (!f) return -1; |
| 123 | while (fgets(line, sizeof line, f)) { |
| 124 | int c; |
| 125 | unsigned long long u, ni, s, id, io, irq, sirq, st; |
| 126 | if (sscanf(line, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu", &c, &u, &ni, &s, &id, &io, &irq, |
| 127 | &sirq, &st) == 9 && c >= 0 && c < 256) { |
| 128 | idle[c] = id + io; |
| 129 | total[c] = u + ni + s + id + io + irq + sirq + st; |
| 130 | } |
| 131 | } |
| 132 | fclose(f); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int pick_x925_cpu(char *why, size_t cap) { |
| 137 | const char *env = getenv("OMA_BENCH_CPU"); |
| 138 | if (env && *env) { |
| 139 | int c = atoi(env); |
| 140 | snprintf(why, cap, "OMA_BENCH_CPU=%d", c); |
| 141 | return c; |
| 142 | } |
| 143 | unsigned long long i0[256] = {0}, t0[256] = {0}, i1[256] = {0}, t1[256] = {0}; |
| 144 | read_cpu_idle(i0, t0); |
| 145 | struct timespec ts = {0, 300000000}; |
| 146 | nanosleep(&ts, NULL); |
| 147 | read_cpu_idle(i1, t1); |
| 148 | int best = -1; |
| 149 | double best_idle = -1; |
| 150 | for (int c = 0; c < ncpu_seen; c++) { |
| 151 | if (cpu_part[c] != 0xd85) continue; |
| 152 | double dt = (double)(t1[c] - t0[c]); |
| 153 | double idle = dt > 0 ? (double)(i1[c] - i0[c]) / dt : 0; |
| 154 | if (idle > best_idle) { best_idle = idle; best = c; } |
| 155 | } |
| 156 | snprintf(why, cap, "most idle Cortex-X925 over 0.3 s (%.0f%% idle)", best_idle * 100.0); |
| 157 | return best; |
| 158 | } |
| 159 | |
| 160 | /* ---- stream-read bandwidth ---- */ |
| 161 | typedef struct { size_t bytes; double gbs; } bw_level; |
| 162 | #define NBW 12 |
| 163 | static bw_level g_bw[NBW]; |
| 164 | |
| 165 | /* Three read kernels; the floor uses the best of them at each size. |
| 166 | * A: ld1 x4 groups, B: independent q loads, C: four concurrent streams. */ |
| 167 | #define XOR8_REDUCE() \ |
| 168 | uint8x16_t s = veorq_u8(veorq_u8(veorq_u8(a0, a1), veorq_u8(a2, a3)), \ |
| 169 | veorq_u8(veorq_u8(a4, a5), veorq_u8(a6, a7))); \ |
| 170 | return vgetq_lane_u64(vreinterpretq_u64_u8(s), 0) ^ vgetq_lane_u64(vreinterpretq_u64_u8(s), 1) |
| 171 | |
| 172 | static uint64_t stream_a(const uint8_t *p, size_t bytes) { |
| 173 | uint8x16_t a0 = vdupq_n_u8(0), a1 = a0, a2 = a0, a3 = a0, a4 = a0, a5 = a0, a6 = a0, a7 = a0; |
| 174 | for (size_t i = 0; i < bytes; i += 128) { |
| 175 | uint8x16x4_t u = vld1q_u8_x4(p + i), v = vld1q_u8_x4(p + i + 64); |
| 176 | a0 = veorq_u8(a0, u.val[0]); a1 = veorq_u8(a1, u.val[1]); |
| 177 | a2 = veorq_u8(a2, u.val[2]); a3 = veorq_u8(a3, u.val[3]); |
| 178 | a4 = veorq_u8(a4, v.val[0]); a5 = veorq_u8(a5, v.val[1]); |
| 179 | a6 = veorq_u8(a6, v.val[2]); a7 = veorq_u8(a7, v.val[3]); |
| 180 | } |
| 181 | XOR8_REDUCE(); |
| 182 | } |
| 183 | |
| 184 | static uint64_t stream_b(const uint8_t *p, size_t bytes) { |
| 185 | uint8x16_t a0 = vdupq_n_u8(0), a1 = a0, a2 = a0, a3 = a0, a4 = a0, a5 = a0, a6 = a0, a7 = a0; |
| 186 | for (size_t i = 0; i < bytes; i += 128) { |
| 187 | a0 = veorq_u8(a0, vld1q_u8(p + i)); a1 = veorq_u8(a1, vld1q_u8(p + i + 16)); |
| 188 | a2 = veorq_u8(a2, vld1q_u8(p + i + 32)); a3 = veorq_u8(a3, vld1q_u8(p + i + 48)); |
| 189 | a4 = veorq_u8(a4, vld1q_u8(p + i + 64)); a5 = veorq_u8(a5, vld1q_u8(p + i + 80)); |
| 190 | a6 = veorq_u8(a6, vld1q_u8(p + i + 96)); a7 = veorq_u8(a7, vld1q_u8(p + i + 112)); |
| 191 | } |
| 192 | XOR8_REDUCE(); |
| 193 | } |
| 194 | |
| 195 | static uint64_t stream_c(const uint8_t *p, size_t bytes) { |
| 196 | size_t q = bytes / 4; |
| 197 | const uint8_t *p1 = p + q, *p2 = p + 2 * q, *p3 = p + 3 * q; |
| 198 | uint8x16_t a0 = vdupq_n_u8(0), a1 = a0, a2 = a0, a3 = a0, a4 = a0, a5 = a0, a6 = a0, a7 = a0; |
| 199 | for (size_t i = 0; i < q; i += 32) { |
| 200 | a0 = veorq_u8(a0, vld1q_u8(p + i)); a1 = veorq_u8(a1, vld1q_u8(p + i + 16)); |
| 201 | a2 = veorq_u8(a2, vld1q_u8(p1 + i)); a3 = veorq_u8(a3, vld1q_u8(p1 + i + 16)); |
| 202 | a4 = veorq_u8(a4, vld1q_u8(p2 + i)); a5 = veorq_u8(a5, vld1q_u8(p2 + i + 16)); |
| 203 | a6 = veorq_u8(a6, vld1q_u8(p3 + i)); a7 = veorq_u8(a7, vld1q_u8(p3 + i + 16)); |
| 204 | } |
| 205 | XOR8_REDUCE(); |
| 206 | } |
| 207 | |
| 208 | static volatile uint64_t g_sink; |
| 209 | |
| 210 | static void measure_bandwidth(void) { |
| 211 | static const size_t sizes[NBW] = {16u << 10, 48u << 10, 256u << 10, 1u << 20, 1536u << 10, 4u << 20, |
| 212 | 8u << 20, 16u << 20, 32u << 20, 64u << 20, 128u << 20, 256u << 20}; |
| 213 | uint64_t (*const kern[3])(const uint8_t *, size_t) = {stream_a, stream_b, stream_c}; |
| 214 | uint8_t *buf = oma_rz_alloc(sizes[NBW - 1]); |
| 215 | if (!buf) { fprintf(stderr, "bandwidth buffer oom\n"); exit(2); } |
| 216 | for (size_t i = 0; i < sizes[NBW - 1]; i++) buf[i] = (uint8_t)i; |
| 217 | for (int l = 0; l < NBW; l++) { |
| 218 | size_t sz = sizes[l]; |
| 219 | size_t passes = (size_t)((64u << 20) / sz); |
| 220 | if (passes < 1) passes = 1; |
| 221 | double best = 1e30; |
| 222 | for (int k = 0; k < 3; k++) { |
| 223 | g_sink ^= kern[k](buf, sz); |
| 224 | for (int s = 0, tries = 0; s < 7; s++) { |
| 225 | uint64_t d0 = run_delay_ns(); |
| 226 | double t0 = now_ns(); |
| 227 | for (size_t p = 0; p < passes; p++) g_sink ^= kern[k](buf, sz); |
| 228 | double t = now_ns() - t0; |
| 229 | uint64_t d = run_delay_ns() - d0; |
| 230 | if ((double)d * 50.0 > t && ++tries < 20) { s--; continue; } |
| 231 | if (t < best) best = t; |
| 232 | } |
| 233 | } |
| 234 | g_bw[l].bytes = sz; |
| 235 | g_bw[l].gbs = (double)sz * (double)passes / best; |
| 236 | } |
| 237 | free(buf); |
| 238 | } |
| 239 | |
| 240 | static int bw_level_for(size_t ws) { |
| 241 | for (int l = 0; l < NBW; l++) |
| 242 | if (g_bw[l].bytes >= ws) return l; |
| 243 | return NBW - 1; |
| 244 | } |
| 245 | |
| 246 | /* ---- energy (aien_spbm cpu_p = the X925 cluster, microjoules) ---- */ |
| 247 | static char g_energy_path[640]; |
| 248 | |
| 249 | static void find_energy_meter(void) { |
| 250 | DIR *d = opendir("/sys/class/hwmon"); |
| 251 | struct dirent *e; |
| 252 | if (!d) return; |
| 253 | while ((e = readdir(d))) { |
| 254 | if (e->d_name[0] == '.') continue; |
| 255 | char p[600], name[64] = {0}; |
| 256 | snprintf(p, sizeof p, "/sys/class/hwmon/%s/name", e->d_name); |
| 257 | FILE *f = fopen(p, "r"); |
| 258 | if (!f) continue; |
| 259 | if (!fgets(name, sizeof name, f)) name[0] = 0; |
| 260 | fclose(f); |
| 261 | if (strncmp(name, "aien_spbm", 9) != 0) continue; |
| 262 | for (int k = 1; k <= 8; k++) { |
| 263 | char lab[64] = {0}; |
| 264 | snprintf(p, sizeof p, "/sys/class/hwmon/%s/energy%d_label", e->d_name, k); |
| 265 | f = fopen(p, "r"); |
| 266 | if (!f) continue; |
| 267 | if (!fgets(lab, sizeof lab, f)) lab[0] = 0; |
| 268 | fclose(f); |
| 269 | if (strncmp(lab, "cpu_p", 5) == 0) { |
| 270 | snprintf(g_energy_path, sizeof g_energy_path, "/sys/class/hwmon/%s/energy%d_input", e->d_name, k); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | closedir(d); |
| 276 | } |
| 277 | |
| 278 | static int read_energy_uj(double *uj) { |
| 279 | if (!g_energy_path[0]) return -1; |
| 280 | FILE *f = fopen(g_energy_path, "r"); |
| 281 | unsigned long long v; |
| 282 | if (!f) return -1; |
| 283 | int ok = fscanf(f, "%llu", &v) == 1; |
| 284 | fclose(f); |
| 285 | if (!ok) return -1; |
| 286 | *uj = (double)v; |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* ---- per-cell measurement ---- */ |
| 291 | typedef struct { |
| 292 | int ok, verified; |
| 293 | double pack_min, pack_med, pack_noise; |
| 294 | int pack_reps; |
| 295 | double samples[NSAMPLES]; |
| 296 | double tmin, tmed, q25, q75; |
| 297 | int retries, forced; |
| 298 | long batch; |
| 299 | size_t weight_bytes, footprint, working_set, scratch; |
| 300 | double floor_ns, floor_gbs; |
| 301 | size_t floor_level; |
| 302 | double pct_floor; |
| 303 | uint64_t nnz; |
| 304 | } cell_res; |
| 305 | |
| 306 | static int cmp_d(const void *a, const void *b) { |
| 307 | double x = *(const double *)a, y = *(const double *)b; |
| 308 | return x < y ? -1 : x > y; |
| 309 | } |
| 310 | |
| 311 | static double quantile(const double *sorted, int n, double q) { |
| 312 | double pos = q * (n - 1); |
| 313 | int i = (int)pos; |
| 314 | double f = pos - i; |
| 315 | return i + 1 < n ? sorted[i] * (1 - f) + sorted[i + 1] * f : sorted[i]; |
| 316 | } |
| 317 | |
| 318 | static int timed_block(const oma_rz_impl *im, const oma_rz_plan *p, const int8_t *x, int32_t *y, long batch, |
| 319 | double *per_call, uint64_t *delay) { |
| 320 | uint64_t d0 = run_delay_ns(); |
| 321 | double t0 = now_ns(); |
| 322 | for (long b = 0; b < batch; b++) im->run(p, x, y); |
| 323 | double t = now_ns() - t0; |
| 324 | *delay = run_delay_ns() - d0; |
| 325 | *per_call = t / (double)batch; |
| 326 | return (double)*delay * 50.0 > t; /* contended: waited > 2% of the block */ |
| 327 | } |
| 328 | |
| 329 | static unsigned long long g_retries_total, g_forced_total, g_mismatch_total, g_runs_checked; |
| 330 | |
| 331 | static void bench_cell(size_t n, size_t m, double sp, FILE *out, int *first_row, int quick) { |
| 332 | size_t nr = oma_rz_count(); |
| 333 | int8_t *w = malloc(m * n), *x = malloc(n); |
| 334 | int32_t *yref = malloc(m * 4), *y = malloc(m * 4); |
| 335 | if (!w || !x || !yref || !y) { fprintf(stderr, "oom\n"); exit(2); } |
| 336 | for (size_t i = 0; i < m * n; i++) { |
| 337 | uint64_t r = rnd(); |
| 338 | double u = (double)(r >> 11) * (1.0 / 9007199254740992.0); |
| 339 | w[i] = u < sp ? 0 : ((r & 1) ? 1 : -1); |
| 340 | } |
| 341 | for (size_t j = 0; j < n; j++) x[j] = (int8_t)(uint8_t)rnd(); |
| 342 | oma_rz_oracle(w, m, n, x, yref); |
| 343 | |
| 344 | oma_rz_plan plans[MAX_RZ]; |
| 345 | cell_res res[MAX_RZ]; |
| 346 | memset(plans, 0, sizeof plans); |
| 347 | memset(res, 0, sizeof res); |
| 348 | for (size_t r = 0; r < nr; r++) { |
| 349 | const oma_rz_impl *im = oma_rz_get(r); |
| 350 | if (n > im->max_n) continue; |
| 351 | double pk[8]; |
| 352 | int npk = 0; |
| 353 | double t0 = now_ns(); |
| 354 | int rc = im->pack(&plans[r], w, m, n); |
| 355 | pk[npk++] = now_ns() - t0; |
| 356 | if (rc) { fprintf(stderr, "%s pack rc %d\n", im->id, rc); continue; } |
| 357 | int extra = quick ? 2 : (pk[0] < 5e6 ? 6 : 2); |
| 358 | for (int e = 0; e < extra; e++) { |
| 359 | oma_rz_plan tmp; |
| 360 | memset(&tmp, 0, sizeof tmp); |
| 361 | t0 = now_ns(); |
| 362 | im->pack(&tmp, w, m, n); |
| 363 | pk[npk++] = now_ns() - t0; |
| 364 | oma_rz_free(&tmp); |
| 365 | } |
| 366 | qsort(pk, (size_t)npk, sizeof pk[0], cmp_d); |
| 367 | res[r].pack_min = pk[0]; |
| 368 | res[r].pack_reps = npk; |
| 369 | res[r].pack_med = pk[npk / 2]; |
| 370 | res[r].pack_noise = npk >= 4 ? (quantile(pk, npk, 0.75) - quantile(pk, npk, 0.25)) / res[r].pack_med |
| 371 | : (pk[npk - 1] - pk[0]) / res[r].pack_med; |
| 372 | /* correctness before timing */ |
| 373 | memset(y, 0x5a, m * 4); |
| 374 | im->run(&plans[r], x, y); |
| 375 | g_runs_checked++; |
| 376 | res[r].verified = memcmp(y, yref, m * 4) == 0; |
| 377 | if (!res[r].verified) { g_mismatch_total++; fprintf(stderr, "MISMATCH %s n=%zu m=%zu\n", im->id, n, m); continue; } |
| 378 | res[r].ok = 1; |
| 379 | res[r].weight_bytes = plans[r].weight_bytes; |
| 380 | res[r].footprint = plans[r].footprint_bytes; |
| 381 | res[r].working_set = plans[r].weight_bytes + plans[r].scratch_bytes + n + m * 4; |
| 382 | res[r].nnz = plans[r].nnz; |
| 383 | res[r].scratch = plans[r].scratch_bytes; |
| 384 | /* calibrate batch */ |
| 385 | im->run(&plans[r], x, y); |
| 386 | t0 = now_ns(); |
| 387 | im->run(&plans[r], x, y); |
| 388 | double one = now_ns() - t0; |
| 389 | long batch = (long)(TARGET_BLOCK_NS / (one > 1 ? one : 1)); |
| 390 | if (batch < 1) batch = 1; |
| 391 | if (batch > 200000) batch = 200000; |
| 392 | res[r].batch = batch; |
| 393 | } |
| 394 | /* interleaved rounds */ |
| 395 | for (int s = 0; s < NSAMPLES; s++) |
| 396 | for (size_t r = 0; r < nr; r++) { |
| 397 | if (!res[r].ok) continue; |
| 398 | const oma_rz_impl *im = oma_rz_get(r); |
| 399 | double pc; |
| 400 | uint64_t dl; |
| 401 | int tries = 0; |
| 402 | while (timed_block(im, &plans[r], x, y, res[r].batch, &pc, &dl)) { |
| 403 | res[r].retries++; |
| 404 | g_retries_total++; |
| 405 | if (++tries >= MAX_RETRY) { res[r].forced++; g_forced_total++; break; } |
| 406 | } |
| 407 | res[r].samples[s] = pc; |
| 408 | } |
| 409 | /* post-timing re-verification */ |
| 410 | for (size_t r = 0; r < nr; r++) { |
| 411 | if (!res[r].ok) continue; |
| 412 | const oma_rz_impl *im = oma_rz_get(r); |
| 413 | memset(y, 0x5a, m * 4); |
| 414 | im->run(&plans[r], x, y); |
| 415 | g_runs_checked++; |
| 416 | if (memcmp(y, yref, m * 4) != 0) { res[r].verified = 0; g_mismatch_total++; } |
| 417 | } |
| 418 | const char *best_id = "-"; |
| 419 | double best_med = 1e300; |
| 420 | for (size_t r = 0; r < nr; r++) { |
| 421 | const oma_rz_impl *im = oma_rz_get(r); |
| 422 | if (!res[r].ok) { |
| 423 | fprintf(out, "%s {\"n\": %zu, \"m\": %zu, \"sparsity\": %.2f, \"rz\": \"%s\", \"eligible\": false, " |
| 424 | "\"verified\": %s, \"why\": \"%s\"}", |
| 425 | *first_row ? "" : ",\n", n, m, sp, im->id, res[r].verified ? "true" : "false", |
| 426 | n > im->max_n ? "n above the realization's max_n" : "failed verification"); |
| 427 | *first_row = 0; |
| 428 | continue; |
| 429 | } |
| 430 | cell_res *c = &res[r]; |
| 431 | double srt[NSAMPLES]; |
| 432 | memcpy(srt, c->samples, sizeof srt); |
| 433 | qsort(srt, NSAMPLES, sizeof srt[0], cmp_d); |
| 434 | c->tmin = srt[0]; |
| 435 | c->tmed = quantile(srt, NSAMPLES, 0.5); |
| 436 | c->q25 = quantile(srt, NSAMPLES, 0.25); |
| 437 | c->q75 = quantile(srt, NSAMPLES, 0.75); |
| 438 | int lv = bw_level_for(c->working_set); |
| 439 | c->floor_level = g_bw[lv].bytes; |
| 440 | c->floor_gbs = g_bw[lv].gbs; |
| 441 | c->floor_ns = (double)c->working_set / c->floor_gbs; /* bytes / (bytes per ns) */ |
| 442 | c->pct_floor = 100.0 * c->floor_ns / c->tmin; |
| 443 | double noise = (c->q75 - c->q25) / c->tmed; |
| 444 | if (!im->weak_baseline && c->tmed < best_med) { best_med = c->tmed; best_id = im->id; } |
| 445 | fprintf(out, |
| 446 | "%s {\"n\": %zu, \"m\": %zu, \"sparsity\": %.2f, \"rz\": \"%s\", \"family\": \"%s\", \"eligible\": true, " |
| 447 | "\"verified\": %s, \"exact\": %d, \"weak_baseline\": %d, \"nnz\": %llu, \"weight_bytes\": %zu, " |
| 448 | "\"bits_per_weight\": %.3f, \"footprint_bytes\": %zu, \"working_set_bytes\": %zu, " |
| 449 | "\"floor_level_bytes\": %zu, \"floor_gbs\": %.2f, \"floor_ps\": %.0f, \"min_ps\": %.0f, " |
| 450 | "\"median_ps\": %.0f, \"q25_ps\": %.0f, \"q75_ps\": %.0f, \"noise_rel\": %.4f, " |
| 451 | "\"pct_floor_best\": %.1f, \"pct_floor_median\": %.1f, \"calls_per_s\": %.1f, \"gmac_per_s\": %.3f, " |
| 452 | "\"pack_min_ps\": %.0f, \"pack_ps\": %.0f, \"pack_noise_rel\": %.4f, \"pack_reps\": %d, \"per_call_total_ps\": %.0f, \"batch\": %ld, " |
| 453 | "\"samples\": %d, \"retries\": %d, \"forced\": %d}", |
| 454 | *first_row ? "" : ",\n", n, m, sp, im->id, im->family, c->verified ? "true" : "false", im->exact, |
| 455 | im->weak_baseline, (unsigned long long)c->nnz, c->weight_bytes, |
| 456 | 8.0 * (double)c->weight_bytes / ((double)m * (double)n), c->footprint, c->working_set, |
| 457 | c->floor_level, c->floor_gbs, c->floor_ns * 1e3, c->tmin * 1e3, c->tmed * 1e3, c->q25 * 1e3, |
| 458 | c->q75 * 1e3, noise, c->pct_floor, 100.0 * c->floor_ns / c->tmed, 1e9 / c->tmed, |
| 459 | (double)m * (double)n / c->tmed, c->pack_min * 1e3, c->pack_med * 1e3, c->pack_noise, c->pack_reps, |
| 460 | (c->pack_med + c->tmed) * 1e3, c->batch, NSAMPLES, c->retries, c->forced); |
| 461 | *first_row = 0; |
| 462 | } |
| 463 | fprintf(stderr, "n=%5zu m=%4zu sp=%.1f best(amortized)=%-12s %10.0f ns", n, m, sp, best_id, best_med); |
| 464 | for (size_t r = 0; r < nr; r++) |
| 465 | if (res[r].ok) fprintf(stderr, " %s=%.0f(%.0f%%)", oma_rz_get(r)->id, res[r].tmed, res[r].pct_floor); |
| 466 | fputc('\n', stderr); |
| 467 | for (size_t r = 0; r < nr; r++) oma_rz_free(&plans[r]); |
| 468 | free(w); free(x); free(yref); free(y); |
| 469 | } |
| 470 | |
| 471 | /* Energy per call on the X925 cluster meter: idle window then run window |
| 472 | * per realization; dynamic energy = E_run - P_idle * t_run. The meter |
| 473 | * covers the whole cluster (10 cores) and updates about every 0.1 s, so |
| 474 | * values are indicative on a shared machine. */ |
| 475 | static void energy_shape(size_t n, size_t m, double sp, FILE *out, int *first) { |
| 476 | int8_t *w = malloc(m * n), *x = malloc(n); |
| 477 | int32_t *y = malloc(m * 4); |
| 478 | if (!w || !x || !y) exit(2); |
| 479 | for (size_t i = 0; i < m * n; i++) { |
| 480 | uint64_t r = rnd(); |
| 481 | double u = (double)(r >> 11) * (1.0 / 9007199254740992.0); |
| 482 | w[i] = u < sp ? 0 : ((r & 1) ? 1 : -1); |
| 483 | } |
| 484 | for (size_t j = 0; j < n; j++) x[j] = (int8_t)(uint8_t)rnd(); |
| 485 | for (size_t r = 0; r < oma_rz_count(); r++) { |
| 486 | const oma_rz_impl *im = oma_rz_get(r); |
| 487 | oma_rz_plan p; |
| 488 | memset(&p, 0, sizeof p); |
| 489 | if (n > im->max_n || im->pack(&p, w, m, n)) continue; |
| 490 | double e0, e1, e2; |
| 491 | struct timespec ts = {0, 400000000}; |
| 492 | if (read_energy_uj(&e0)) { oma_rz_free(&p); break; } |
| 493 | double ti0 = now_ns(); |
| 494 | nanosleep(&ts, NULL); |
| 495 | read_energy_uj(&e1); |
| 496 | double ti = now_ns() - ti0; |
| 497 | double tr0 = now_ns(), tr; |
| 498 | long calls = 0; |
| 499 | do { |
| 500 | im->run(&p, x, y); |
| 501 | calls++; |
| 502 | tr = now_ns() - tr0; |
| 503 | } while (tr < 8e8); |
| 504 | read_energy_uj(&e2); |
| 505 | double p_idle_w = (e1 - e0) / (ti / 1e3); /* uJ per us = W */ |
| 506 | double dyn_uj = (e2 - e1) - p_idle_w * (tr / 1e3); |
| 507 | fprintf(out, "%s {\"n\": %zu, \"m\": %zu, \"sparsity\": %.2f, \"rz\": \"%s\", \"calls\": %ld, " |
| 508 | "\"run_s\": %.3f, \"cluster_idle_w\": %.3f, \"cluster_run_w\": %.3f, " |
| 509 | "\"dynamic_nj_per_call\": %.1f, \"total_nj_per_call\": %.1f}", |
| 510 | *first ? "" : ",\n", n, m, sp, im->id, calls, tr / 1e9, p_idle_w, (e2 - e1) / (tr / 1e3), |
| 511 | dyn_uj * 1e3 / (double)calls, (e2 - e1) * 1e3 / (double)calls); |
| 512 | *first = 0; |
| 513 | oma_rz_free(&p); |
| 514 | } |
| 515 | free(w); free(x); free(y); |
| 516 | } |
| 517 | |
| 518 | int main(int argc, char **argv) { |
| 519 | if (argc < 2) { fprintf(stderr, "usage: %s OUT.json\n", argv[0]); return 2; } |
| 520 | int quick = getenv("OMA_BENCH_QUICK") && atoi(getenv("OMA_BENCH_QUICK")) > 0; |
| 521 | read_cpu_parts(); |
| 522 | char why[128]; |
| 523 | int cpu = pick_x925_cpu(why, sizeof why); |
| 524 | if (cpu < 0) { fprintf(stderr, "no Cortex-X925 cpu found\n"); return 2; } |
| 525 | cpu_set_t set; |
| 526 | CPU_ZERO(&set); |
| 527 | CPU_SET(cpu, &set); |
| 528 | if (sched_setaffinity(0, sizeof set, &set) != 0) { perror("sched_setaffinity"); return 2; } |
| 529 | int part = cpu_part[cpu]; |
| 530 | if (part != 0xd85) { fprintf(stderr, "cpu %d is not a Cortex-X925 (part 0x%x)\n", cpu, part); return 2; } |
| 531 | char la0[64], la1[64]; |
| 532 | read_loadavg(la0, sizeof la0); |
| 533 | int th0 = read_thermal_c(); |
| 534 | double wall0 = now_ns(); |
| 535 | fprintf(stderr, "pinned to cpu %d (Cortex-X925, %s), loadavg %s\n", cpu, why, la0); |
| 536 | measure_bandwidth(); |
| 537 | for (int l = 0; l < NBW; l++) fprintf(stderr, " stream read %8zu KiB: %.1f GB/s\n", g_bw[l].bytes >> 10, g_bw[l].gbs); |
| 538 | find_energy_meter(); |
| 539 | |
| 540 | char tmp_path[1024]; |
| 541 | snprintf(tmp_path, sizeof tmp_path, "%s.rows.tmp", argv[1]); |
| 542 | FILE *rows = fopen(tmp_path, "w+"); |
| 543 | if (!rows) { perror(tmp_path); return 2; } |
| 544 | static const size_t ns_full[] = {1024, 4096, 16384}, ms_full[] = {1, 64, 4096}; |
| 545 | static const double sps_full[] = {0.0, 0.3, 0.6, 0.9}; |
| 546 | size_t nn = quick ? 2 : 3, nm = quick ? 2 : 3, nsp = quick ? 2 : 4; |
| 547 | int first = 1; |
| 548 | for (size_t a = 0; a < nn; a++) |
| 549 | for (size_t b = 0; b < nm; b++) |
| 550 | for (size_t c = 0; c < nsp; c++) bench_cell(ns_full[a], ms_full[b], sps_full[c], rows, &first, quick); |
| 551 | double grid_s = (now_ns() - wall0) / 1e9; |
| 552 | |
| 553 | FILE *en = tmpfile(); |
| 554 | int efirst = 1; |
| 555 | double ec; |
| 556 | int energy_ok = g_energy_path[0] && read_energy_uj(&ec) == 0; |
| 557 | if (energy_ok && !quick) { |
| 558 | energy_shape(16384, 4096, 0.3, en, &efirst); |
| 559 | energy_shape(1024, 4096, 0.3, en, &efirst); |
| 560 | } |
| 561 | read_loadavg(la1, sizeof la1); |
| 562 | int th1 = read_thermal_c(); |
| 563 | |
| 564 | time_t now = time(NULL); |
| 565 | struct tm tmv; |
| 566 | gmtime_r(&now, &tmv); |
| 567 | char run_id[64]; |
| 568 | strftime(run_id, sizeof run_id, "%Y%m%dT%H%M%SZ", &tmv); |
| 569 | const char *commit = getenv("OMA_BENCH_COMMIT"), *dirty = getenv("OMA_BENCH_DIRTY"), |
| 570 | *bsha = getenv("OMA_BENCH_BIN_SHA"); |
| 571 | struct utsname un; |
| 572 | uname(&un); |
| 573 | FILE *o = fopen(argv[1], "w"); |
| 574 | if (!o) { perror(argv[1]); return 2; } |
| 575 | fprintf(o, "{\n \"schema\": \"OMEGA_MIXED_ALGEBRA_MA2_BENCH_V1\",\n \"run_id\": \"%s-%s\",\n", run_id, |
| 576 | commit ? commit : "unknown"); |
| 577 | fprintf(o, " \"operation\": \"Omega-X: y = W.x, W in {-1,0,+1}^(m x n), x int8^n, y int32^m, exact\",\n"); |
| 578 | fprintf(o, " \"run_commit\": \"%s\",\n \"tree_dirty\": %s,\n \"bench_binary_sha256\": \"%s\",\n", |
| 579 | commit ? commit : "unknown", (dirty && atoi(dirty) > 0) ? "true" : "false", bsha ? bsha : "unknown"); |
| 580 | fprintf(o, " \"compiler\": \"gcc %s, -std=c11 -O2 -march=armv8.6-a+dotprod+i8mm+sve\",\n", __VERSION__); |
| 581 | fprintf(o, " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\", \"timed_cpu\": %d, " |
| 582 | "\"timed_core\": \"Cortex-X925 (MIDR part 0x%x)\", \"cpu_choice\": \"%s\", \"loadavg_start\": \"%s\", " |
| 583 | "\"loadavg_end\": \"%s\", \"thermal_c_start\": %d, \"thermal_c_end\": %d, \"grid_wall_s\": %.1f, " |
| 584 | "\"shared_machine\": \"other sessions may run; blocks with run_delay > 2%% of the block were retried " |
| 585 | "(up to %d times), costs are best-of and median of %d interleaved blocks\"},\n", |
| 586 | un.sysname, un.release, un.machine, cpu, part, why, la0, la1, th0, th1, grid_s, MAX_RETRY, NSAMPLES); |
| 587 | fprintf(o, " \"bandwidth\": ["); |
| 588 | for (int l = 0; l < NBW; l++) |
| 589 | fprintf(o, "%s{\"bytes\": %zu, \"read_gbs\": %.2f}", l ? ", " : "", g_bw[l].bytes, g_bw[l].gbs); |
| 590 | fprintf(o, "],\n \"bandwidth_method\": \"best of three NEON xor-reduce read kernels (ld1x4, independent q loads, 4 concurrent streams), hot buffer of the given size, best of 7 each, >= 64 MiB read per sample; roofline floor = working_set_bytes / read_gbs at the smallest measured size >= working set\",\n"); |
| 591 | fprintf(o, " \"realizations\": [\n"); |
| 592 | for (size_t r = 0; r < oma_rz_count(); r++) { |
| 593 | const oma_rz_impl *im = oma_rz_get(r); |
| 594 | fprintf(o, " {\"id\": \"%s\", \"family\": \"%s\", \"exact\": %d, \"weak_baseline\": %d, \"max_n\": %zu, " |
| 595 | "\"label\": \"%s\"}%s\n", |
| 596 | im->id, im->family, im->exact, im->weak_baseline, im->max_n, im->label, |
| 597 | r + 1 < oma_rz_count() ? "," : ""); |
| 598 | } |
| 599 | fprintf(o, " ],\n \"correctness\": {\"runs_checked_against_oracle\": %llu, \"mismatches\": %llu},\n", |
| 600 | g_runs_checked, g_mismatch_total); |
| 601 | /* top-level copies of the oracle counts (the MA-3-label runs carry them only in \"correctness\") */ |
| 602 | fprintf(o, " \"oracle_checks\": %llu,\n \"oracle_mismatches\": %llu,\n", g_runs_checked, g_mismatch_total); |
| 603 | fprintf(o, " \"contention\": {\"blocks_retried\": %llu, \"blocks_forced_after_%d_retries\": %llu},\n", |
| 604 | g_retries_total, MAX_RETRY, g_forced_total); |
| 605 | fprintf(o, " \"cost_table\": [\n"); |
| 606 | fflush(rows); |
| 607 | rewind(rows); |
| 608 | char buf[8192]; |
| 609 | size_t k; |
| 610 | while ((k = fread(buf, 1, sizeof buf, rows)) > 0) fwrite(buf, 1, k, o); |
| 611 | fclose(rows); |
| 612 | remove(tmp_path); |
| 613 | fprintf(o, "\n ],\n"); |
| 614 | fprintf(o, " \"energy\": {\"meter\": \"%s\", \"scope\": \"aien_spbm cpu_p = whole Cortex-X925 cluster (10 cores), " |
| 615 | "updates about every 0.1 s; idle 0.4 s then run 0.8 s per realization; dynamic = run minus idle power; " |
| 616 | "indicative only on a shared machine\", \"rows\": [\n", |
| 617 | energy_ok ? g_energy_path : "not readable"); |
| 618 | fflush(en); |
| 619 | rewind(en); |
| 620 | while ((k = fread(buf, 1, sizeof buf, en)) > 0) fwrite(buf, 1, k, o); |
| 621 | fclose(en); |
| 622 | fprintf(o, "\n ]},\n \"label\": \"MA-2 cost table; consumed by the stand-in selector (oma_select), not wired to rx_costmodel\"\n}\n"); |
| 623 | fclose(o); |
| 624 | fprintf(stderr, "wrote %s (grid %.1f s, total %.1f s, mismatches %llu, retried blocks %llu)\n", argv[1], grid_s, |
| 625 | (now_ns() - wall0) / 1e9, g_mismatch_total, g_retries_total); |
| 626 | return g_mismatch_total ? 1 : 0; |
| 627 | } |