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.h
| 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 (one copy for every C/CUDA implementation). |
| 18 | * Contract: canonical bitmap, bit k (u64 word k/64, bit k%64) set iff 2k+1 is prime, 0 <= k < (limit+1)/2, |
| 19 | * tail bits zero; prime 2 is implicit when limit >= 2. */ |
| 20 | #ifndef PRIME_RACE_IMPL_H |
| 21 | #define PRIME_RACE_IMPL_H |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | typedef struct pr_impl { |
| 28 | const char *name; /* e.g. "cpu-c-base"; label printed as "aien-<name>" */ |
| 29 | const char *algorithm; /* base|wheel|other */ |
| 30 | const char *faithful; /* yes|no */ |
| 31 | int bits; /* storage bits per flag */ |
| 32 | const char *environment; /* linux-host-cpu | linux-hosted-gb10-native | linux-hosted-gb10-cuda */ |
| 33 | int (*setup)(void **state, uint64_t limit); /* untimed one-time setup, 0 = ok */ |
| 34 | int (*pass)(void *state, uint64_t limit); /* timed: fresh sieve from scratch, complete, host-readable; 0 = ok */ |
| 35 | int (*export_bitmap)(void *state, uint64_t limit, uint64_t *out); /* canonical words of last pass, 0 = ok */ |
| 36 | uint64_t (*threads)(void *state); /* threads that ran the sieve in last pass */ |
| 37 | int (*detail_json)(void *state, FILE *f); /* optional (may be NULL): writes one JSON object */ |
| 38 | void (*teardown)(void *state); |
| 39 | } pr_impl; |
| 40 | |
| 41 | static inline uint64_t pr_odd_count(uint64_t limit) { return (limit + 1) / 2; } |
| 42 | static inline size_t pr_words64(uint64_t limit) { return (size_t)((pr_odd_count(limit) + 63) / 64); } |
| 43 | |
| 44 | #define PR_MAX_LIMIT (1ULL << 40) |
| 45 | |
| 46 | /* Full CLI: <impl> [--limit N=1000000] [--min-seconds S=5] [--bitmap-out PATH] [--report-out PATH] |
| 47 | * [--audit-passes K]. Exit 0 ok, 2 execution failure (no stdout line), 64 usage error. */ |
| 48 | int pr_main(int argc, char **argv, const pr_impl *impl); |
| 49 | |
| 50 | /* ---- hooks exposed for host tests (pure functions, no process state) ---- */ |
| 51 | typedef struct pr_args { |
| 52 | uint64_t limit; |
| 53 | double min_seconds; |
| 54 | const char *bitmap_out; |
| 55 | const char *report_out; |
| 56 | uint64_t audit_passes; /* 0 = timed mode */ |
| 57 | } pr_args; |
| 58 | |
| 59 | /* 0 = ok; -1 = usage error with a message in err. */ |
| 60 | int pr_parse_args(int argc, char **argv, pr_args *a, char *err, size_t errn); |
| 61 | |
| 62 | /* Upstream line "aien-<name>;<passes>;<elapsed %.6f>;<threads>;algorithm=..,faithful=..,bits=..". |
| 63 | * Returns length written, or -1 if it does not fit. */ |
| 64 | int pr_format_line(char *buf, size_t n, const pr_impl *impl, uint64_t passes, double elapsed_s, uint64_t threads); |
| 65 | |
| 66 | /* 1 only if the stdout line may be printed: no failure, passes > 0, elapsed >= min_seconds. */ |
| 67 | int pr_line_allowed(int failed, uint64_t passes, double elapsed_s, double min_seconds); |
| 68 | |
| 69 | /* JSON string literal with hand-written escaping (quotes included). */ |
| 70 | void pr_json_string(FILE *f, const char *s); |
| 71 | |
| 72 | typedef struct pr_report { |
| 73 | const pr_impl *impl; |
| 74 | void *state; /* for threads()/detail_json(); may be NULL */ |
| 75 | uint64_t threads; |
| 76 | uint64_t limit; |
| 77 | double min_seconds; |
| 78 | const char *mode; /* "timed" | "audit" */ |
| 79 | uint64_t passes; |
| 80 | double elapsed_s; |
| 81 | const char *status; /* "COMPLETE" | "EXEC_FAILED" */ |
| 82 | const char *error; /* NULL when ok */ |
| 83 | } pr_report; |
| 84 | |
| 85 | /* Writes the "aien-prime-race/impl-report/v1" JSON object. Returns 0 if the stream had no error. */ |
| 86 | int pr_write_report(FILE *f, const pr_report *r); |
| 87 | |
| 88 | #endif |