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 / bench / prime_race / prime_race_impl.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 | /* AIEN Prime Drag Race: implementation-side protocol. See prime_race_impl.h and bench/prime_race/README.md. */ |
| 18 | #include "prime_race_impl.h" |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <math.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <time.h> |
| 25 | |
| 26 | #ifndef PR_SOURCE_COMMIT |
| 27 | #define PR_SOURCE_COMMIT "unknown" |
| 28 | #endif |
| 29 | #ifndef PR_CC |
| 30 | #define PR_CC "unknown" |
| 31 | #endif |
| 32 | #ifndef PR_CFLAGS |
| 33 | #define PR_CFLAGS "unknown" |
| 34 | #endif |
| 35 | |
| 36 | static void set_err(char *err, size_t n, const char *fmt, const char *a) { |
| 37 | if (err && n) snprintf(err, n, fmt, a ? a : ""); |
| 38 | } |
| 39 | |
| 40 | static int parse_u64(const char *s, uint64_t *out) { |
| 41 | char *end = NULL; |
| 42 | if (!s || s[0] < '0' || s[0] > '9') return -1; /* rejects empty, sign, space */ |
| 43 | errno = 0; |
| 44 | unsigned long long v = strtoull(s, &end, 10); |
| 45 | if (errno != 0 || end == s || *end != '\0') return -1; |
| 46 | *out = (uint64_t)v; |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int parse_seconds(const char *s, double *out) { |
| 51 | char *end = NULL; |
| 52 | if (!s || !((s[0] >= '0' && s[0] <= '9') || s[0] == '.')) return -1; /* rejects nan/inf/sign */ |
| 53 | errno = 0; |
| 54 | double v = strtod(s, &end); |
| 55 | if (errno != 0 || end == s || *end != '\0' || !isfinite(v) || v < 0.0) return -1; |
| 56 | *out = v; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int pr_parse_args(int argc, char **argv, pr_args *a, char *err, size_t errn) { |
| 61 | a->limit = 1000000; |
| 62 | a->min_seconds = 5.0; |
| 63 | a->bitmap_out = NULL; |
| 64 | a->report_out = NULL; |
| 65 | a->audit_passes = 0; |
| 66 | int audit_given = 0; |
| 67 | for (int i = 1; i < argc; i++) { |
| 68 | const char *opt = argv[i]; |
| 69 | const char *val = (i + 1 < argc) ? argv[i + 1] : NULL; |
| 70 | if (strcmp(opt, "--limit") == 0 || strcmp(opt, "--min-seconds") == 0 || strcmp(opt, "--bitmap-out") == 0 || |
| 71 | strcmp(opt, "--report-out") == 0 || strcmp(opt, "--audit-passes") == 0) { |
| 72 | if (!val) { set_err(err, errn, "missing value for %s", opt); return -1; } |
| 73 | i++; |
| 74 | if (strcmp(opt, "--limit") == 0) { |
| 75 | if (parse_u64(val, &a->limit) != 0) { set_err(err, errn, "invalid --limit '%s'", val); return -1; } |
| 76 | if (a->limit > PR_MAX_LIMIT) { set_err(err, errn, "--limit '%s' exceeds 2^40", val); return -1; } |
| 77 | } else if (strcmp(opt, "--min-seconds") == 0) { |
| 78 | if (parse_seconds(val, &a->min_seconds) != 0) { set_err(err, errn, "invalid --min-seconds '%s'", val); return -1; } |
| 79 | } else if (strcmp(opt, "--audit-passes") == 0) { |
| 80 | if (parse_u64(val, &a->audit_passes) != 0 || a->audit_passes == 0) { |
| 81 | set_err(err, errn, "invalid --audit-passes '%s' (need integer >= 1)", val); |
| 82 | return -1; |
| 83 | } |
| 84 | audit_given = 1; |
| 85 | } else if (strcmp(opt, "--bitmap-out") == 0) { |
| 86 | if (val[0] == '\0') { set_err(err, errn, "empty %s", opt); return -1; } |
| 87 | a->bitmap_out = val; |
| 88 | } else { |
| 89 | if (val[0] == '\0') { set_err(err, errn, "empty %s", opt); return -1; } |
| 90 | a->report_out = val; |
| 91 | } |
| 92 | } else { |
| 93 | set_err(err, errn, "unknown argument '%s'", opt); |
| 94 | return -1; |
| 95 | } |
| 96 | } |
| 97 | if (audit_given && !a->bitmap_out) { |
| 98 | set_err(err, errn, "%s", "--audit-passes requires --bitmap-out"); |
| 99 | return -1; |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int pr_format_line(char *buf, size_t n, const pr_impl *impl, uint64_t passes, double elapsed_s, uint64_t threads) { |
| 105 | int w = snprintf(buf, n, "aien-%s;%llu;%.6f;%llu;algorithm=%s,faithful=%s,bits=%d", impl->name, |
| 106 | (unsigned long long)passes, elapsed_s, (unsigned long long)threads, impl->algorithm, |
| 107 | impl->faithful, impl->bits); |
| 108 | if (w < 0 || (size_t)w >= n) return -1; |
| 109 | return w; |
| 110 | } |
| 111 | |
| 112 | int pr_line_allowed(int failed, uint64_t passes, double elapsed_s, double min_seconds) { |
| 113 | if (failed) return 0; |
| 114 | if (passes == 0) return 0; |
| 115 | if (!isfinite(elapsed_s) || elapsed_s < min_seconds) return 0; |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | void pr_json_string(FILE *f, const char *s) { |
| 120 | fputc('"', f); |
| 121 | for (const unsigned char *p = (const unsigned char *)(s ? s : ""); *p; p++) { |
| 122 | switch (*p) { |
| 123 | case '"': fputs("\\\"", f); break; |
| 124 | case '\\': fputs("\\\\", f); break; |
| 125 | case '\n': fputs("\\n", f); break; |
| 126 | case '\r': fputs("\\r", f); break; |
| 127 | case '\t': fputs("\\t", f); break; |
| 128 | default: |
| 129 | if (*p < 0x20) fprintf(f, "\\u%04x", (unsigned)*p); |
| 130 | else fputc(*p, f); |
| 131 | } |
| 132 | } |
| 133 | fputc('"', f); |
| 134 | } |
| 135 | |
| 136 | int pr_write_report(FILE *f, const pr_report *r) { |
| 137 | const pr_impl *im = r->impl; |
| 138 | char label[256]; |
| 139 | snprintf(label, sizeof label, "aien-%s", im->name); |
| 140 | fputs("{\n \"schema\": \"aien-prime-race/impl-report/v1\",\n \"impl\": ", f); |
| 141 | pr_json_string(f, im->name); |
| 142 | fputs(",\n \"label\": ", f); |
| 143 | pr_json_string(f, label); |
| 144 | fputs(",\n \"algorithm\": ", f); |
| 145 | pr_json_string(f, im->algorithm); |
| 146 | fputs(",\n \"faithful\": ", f); |
| 147 | pr_json_string(f, im->faithful); |
| 148 | fprintf(f, ",\n \"bits\": %d,\n \"threads\": %llu,\n \"environment\": ", im->bits, (unsigned long long)r->threads); |
| 149 | pr_json_string(f, im->environment); |
| 150 | fprintf(f, ",\n \"limit\": %llu,\n \"min_seconds\": %.6f,\n \"mode\": ", (unsigned long long)r->limit, r->min_seconds); |
| 151 | pr_json_string(f, r->mode); |
| 152 | fprintf(f, ",\n \"passes\": %llu,\n \"elapsed_s\": %.6f,\n \"status\": ", (unsigned long long)r->passes, r->elapsed_s); |
| 153 | pr_json_string(f, r->status); |
| 154 | fputs(",\n \"error\": ", f); |
| 155 | if (r->error) pr_json_string(f, r->error); else fputs("null", f); |
| 156 | fputs(",\n \"build\": {\"source_commit\": ", f); |
| 157 | pr_json_string(f, PR_SOURCE_COMMIT); |
| 158 | fputs(", \"cc\": ", f); |
| 159 | pr_json_string(f, PR_CC); |
| 160 | fputs(", \"cflags\": ", f); |
| 161 | pr_json_string(f, PR_CFLAGS); |
| 162 | fputs("},\n \"detail\": ", f); |
| 163 | if (im->detail_json && r->state) { |
| 164 | if (im->detail_json(r->state, f) != 0) fputs("null", f); |
| 165 | } else { |
| 166 | fputs("{}", f); |
| 167 | } |
| 168 | fputs("\n}\n", f); |
| 169 | fflush(f); |
| 170 | return ferror(f) ? -1 : 0; |
| 171 | } |
| 172 | |
| 173 | static int64_t now_ns(void) { |
| 174 | struct timespec ts; |
| 175 | if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return -1; |
| 176 | return (int64_t)ts.tv_sec * 1000000000LL + (int64_t)ts.tv_nsec; |
| 177 | } |
| 178 | |
| 179 | static int write_words(FILE *f, const uint64_t *w, size_t n) { |
| 180 | return (n == 0 || fwrite(w, sizeof(uint64_t), n, f) == n) ? 0 : -1; |
| 181 | } |
| 182 | |
| 183 | int pr_main(int argc, char **argv, const pr_impl *impl) { |
| 184 | pr_args a; |
| 185 | char err[256] = ""; |
| 186 | if (pr_parse_args(argc, argv, &a, err, sizeof err) != 0) { |
| 187 | fprintf(stderr, "usage error: %s\n", err); |
| 188 | fprintf(stderr, "usage: %s [--limit N=1000000] [--min-seconds S=5] [--bitmap-out PATH] " |
| 189 | "[--report-out PATH] [--audit-passes K]\n", argc > 0 ? argv[0] : "impl"); |
| 190 | return 64; |
| 191 | } |
| 192 | |
| 193 | const int audit = a.audit_passes > 0; |
| 194 | char error[256] = ""; |
| 195 | int failed = 0; |
| 196 | uint64_t passes = 0; |
| 197 | double elapsed = 0.0; |
| 198 | void *st = NULL; |
| 199 | int have_state = 0; |
| 200 | uint64_t *words = NULL; |
| 201 | FILE *bf = NULL; |
| 202 | const size_t nw = pr_words64(a.limit); |
| 203 | |
| 204 | words = (uint64_t *)calloc(nw ? nw : 1, sizeof(uint64_t)); |
| 205 | if (!words) { failed = 1; snprintf(error, sizeof error, "cannot allocate export buffer"); goto report; } |
| 206 | if (impl->setup(&st, a.limit) != 0) { failed = 1; snprintf(error, sizeof error, "setup failed"); goto report; } |
| 207 | have_state = 1; |
| 208 | if (a.bitmap_out && audit) { |
| 209 | bf = fopen(a.bitmap_out, "wb"); |
| 210 | if (!bf) { failed = 1; snprintf(error, sizeof error, "cannot open bitmap-out"); goto report; } |
| 211 | } |
| 212 | |
| 213 | if (audit) { |
| 214 | for (uint64_t k = 0; k < a.audit_passes; k++) { |
| 215 | if (impl->pass(st, a.limit) != 0) { failed = 1; snprintf(error, sizeof error, "pass %llu failed", (unsigned long long)k); break; } |
| 216 | passes++; |
| 217 | if (impl->export_bitmap(st, a.limit, words) != 0) { failed = 1; snprintf(error, sizeof error, "export failed on pass %llu", (unsigned long long)k); break; } |
| 218 | if (write_words(bf, words, nw) != 0 || fflush(bf) != 0) { failed = 1; snprintf(error, sizeof error, "bitmap write failed"); break; } |
| 219 | } |
| 220 | } else { |
| 221 | int64_t t0 = now_ns(), t1 = t0; |
| 222 | if (t0 < 0) { failed = 1; snprintf(error, sizeof error, "clock failure"); goto report; } |
| 223 | for (;;) { |
| 224 | if (impl->pass(st, a.limit) != 0) { failed = 1; snprintf(error, sizeof error, "pass %llu failed", (unsigned long long)passes); break; } |
| 225 | passes++; |
| 226 | t1 = now_ns(); |
| 227 | if (t1 < 0) { failed = 1; snprintf(error, sizeof error, "clock failure"); break; } |
| 228 | if ((double)(t1 - t0) / 1e9 >= a.min_seconds) break; |
| 229 | } |
| 230 | elapsed = (double)(t1 - t0) / 1e9; |
| 231 | if (!failed && a.bitmap_out) { |
| 232 | /* untimed export of the final pass */ |
| 233 | FILE *of = fopen(a.bitmap_out, "wb"); |
| 234 | if (!of) { failed = 1; snprintf(error, sizeof error, "cannot open bitmap-out"); } |
| 235 | else { |
| 236 | if (impl->export_bitmap(st, a.limit, words) != 0) { failed = 1; snprintf(error, sizeof error, "export failed"); } |
| 237 | else if (write_words(of, words, nw) != 0) { failed = 1; snprintf(error, sizeof error, "bitmap write failed"); } |
| 238 | if (fclose(of) != 0 && !failed) { failed = 1; snprintf(error, sizeof error, "bitmap close failed"); } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | if (bf) { |
| 243 | if (fclose(bf) != 0 && !failed) { failed = 1; snprintf(error, sizeof error, "bitmap close failed"); } |
| 244 | bf = NULL; |
| 245 | } |
| 246 | if (!failed && !audit && !pr_line_allowed(0, passes, elapsed, a.min_seconds)) { |
| 247 | failed = 1; |
| 248 | snprintf(error, sizeof error, "timed run did not satisfy passes>0 and elapsed>=min_seconds"); |
| 249 | } |
| 250 | if (!failed && audit && passes != a.audit_passes) { failed = 1; snprintf(error, sizeof error, "audit pass count mismatch"); } |
| 251 | |
| 252 | report:; |
| 253 | if (bf) fclose(bf); |
| 254 | pr_report rep; |
| 255 | memset(&rep, 0, sizeof rep); |
| 256 | rep.impl = impl; |
| 257 | rep.state = have_state ? st : NULL; |
| 258 | rep.threads = have_state && impl->threads ? impl->threads(st) : 0; |
| 259 | rep.limit = a.limit; |
| 260 | rep.min_seconds = a.min_seconds; |
| 261 | rep.mode = audit ? "audit" : "timed"; |
| 262 | rep.passes = passes; |
| 263 | rep.elapsed_s = elapsed; |
| 264 | rep.status = failed ? "EXEC_FAILED" : "COMPLETE"; |
| 265 | rep.error = failed ? error : NULL; |
| 266 | |
| 267 | if (a.report_out) { |
| 268 | FILE *rf = fopen(a.report_out, "w"); |
| 269 | if (!rf || pr_write_report(rf, &rep) != 0 || fclose(rf) != 0) { |
| 270 | fprintf(stderr, "cannot write report to %s\n", a.report_out); |
| 271 | if (!failed) { failed = 1; snprintf(error, sizeof error, "report write failed"); } |
| 272 | } |
| 273 | } |
| 274 | uint64_t threads = rep.threads; |
| 275 | if (have_state && impl->teardown) impl->teardown(st); |
| 276 | free(words); |
| 277 | |
| 278 | if (failed) { |
| 279 | fprintf(stderr, "EXEC_FAILED: %s\n", error); |
| 280 | return 2; |
| 281 | } |
| 282 | if (!audit) { /* audit mode makes no timing claims: no upstream line */ |
| 283 | char line[512]; |
| 284 | if (pr_format_line(line, sizeof line, impl, passes, elapsed, threads) < 0) { |
| 285 | fprintf(stderr, "EXEC_FAILED: line too long\n"); |
| 286 | return 2; |
| 287 | } |
| 288 | printf("%s\n", line); |
| 289 | fflush(stdout); |
| 290 | } |
| 291 | return 0; |
| 292 | } |