interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / turing / ty_workload.c

204 lines · 7646 bytes Raw Blame History
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/* ty_workload: one TY-5 workload process (docs/turing/TURING_YIELD_ENERGY_PROTOCOL_V0.md).
18 *
19 * Runs one MA-2 realization (oma_rz_get(i)->run, read-only use of src/algebra)
20 * back to back on one fixed shape, one thread per listed cpu, each thread
21 * pinned and holding its own packed plan (so every thread streams its own
22 * weights from DRAM). The process opens its own CPU PMU counters (tests/runtime/
23 * r15_measure, linked unchanged) before the worker threads exist, so the
24 * counts cover exactly this workload's threads.
25 *
26 * Handshake with the window tool (tests/turing/ty_energy_window.c):
27 * stdout "READY <pid> <weight_bytes> <footprint_bytes>\n" after packing and
28 * one oracle check, then a blocking read of one byte on stdin (GO), then the
29 * timed run of --secs seconds, then one JSON line with the result.
30 * No sleeping, no polling: the only waits are the blocking read and the joins.
31 *
32 * usage: ty_workload --tag A --rz R1_plain --m 16384 --n 16384 --cpus 5,6
33 * --secs 30 --seed 1 [--sparsity-milli 300]
34 */
35#ifndef _GNU_SOURCE
36#define _GNU_SOURCE
37#endif
38#include <inttypes.h>
39#include <pthread.h>
40#include <sched.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45
46#include "algebra/realize_common.h"
47#include "r15_measure.h"
48
49#define TYW_MAX_CPUS 10
50
51typedef struct {
52 const oma_rz_impl *im;
53 oma_rz_plan plan;
54 int8_t *x;
55 int32_t *y;
56 int cpu;
57 uint64_t deadline_ns;
58 uint64_t t0_ns, t1_ns, calls;
59 int pin_ok;
60} tyw_thread;
61
62static uint64_t tyw_rng;
63static uint64_t tyw_next(void) {
64 uint64_t z = (tyw_rng += 0x9E3779B97F4A7C15ull);
65 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
66 z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
67 return z ^ (z >> 31);
68}
69
70static int pin_to(int cpu) {
71 cpu_set_t s;
72 CPU_ZERO(&s);
73 CPU_SET(cpu, &s);
74 return sched_setaffinity(0, sizeof s, &s) == 0;
75}
76
77static void *worker(void *arg) {
78 tyw_thread *t = arg;
79 t->pin_ok = pin_to(t->cpu);
80 t->t0_ns = r15_now_ns();
81 uint64_t now = t->t0_ns;
82 while (now < t->deadline_ns) {
83 t->im->run(&t->plan, t->x, t->y);
84 t->calls++;
85 now = r15_now_ns();
86 }
87 t->t1_ns = now;
88 return NULL;
89}
90
91static void die(const char *m) {
92 fprintf(stderr, "ty_workload: %s\n", m);
93 exit(2);
94}
95
96int main(int argc, char **argv) {
97 const char *tag = "A", *rz = "R1_plain", *cpus = "5";
98 size_t m = 16384, n = 16384;
99 unsigned secs = 30, sp_milli = 300;
100 uint64_t seed = 1;
101 for (int i = 1; i + 1 < argc; i += 2) {
102 if (!strcmp(argv[i], "--tag")) tag = argv[i + 1];
103 else if (!strcmp(argv[i], "--rz")) rz = argv[i + 1];
104 else if (!strcmp(argv[i], "--m")) m = strtoull(argv[i + 1], NULL, 10);
105 else if (!strcmp(argv[i], "--n")) n = strtoull(argv[i + 1], NULL, 10);
106 else if (!strcmp(argv[i], "--cpus")) cpus = argv[i + 1];
107 else if (!strcmp(argv[i], "--secs")) secs = (unsigned)strtoul(argv[i + 1], NULL, 10);
108 else if (!strcmp(argv[i], "--seed")) seed = strtoull(argv[i + 1], NULL, 10);
109 else if (!strcmp(argv[i], "--sparsity-milli")) sp_milli = (unsigned)strtoul(argv[i + 1], NULL, 10);
110 else die("unknown argument");
111 }
112 const oma_rz_impl *im = oma_rz_find(rz);
113 if (!im) die("unknown realization id");
114 int cpu[TYW_MAX_CPUS], ncpu = 0;
115 for (const char *p = cpus; *p && ncpu < TYW_MAX_CPUS;) {
116 char *end;
117 long c = strtol(p, &end, 10);
118 if (end == p || c < 0) die("bad --cpus");
119 cpu[ncpu++] = (int)c;
120 p = *end == ',' ? end + 1 : end;
121 }
122 if (ncpu == 0 || secs == 0) die("need cpus and secs");
123
124 /* Canonical weights and input, generated once from the seed. */
125 int8_t *w = malloc(m * n);
126 int8_t *x = malloc(n);
127 if (!w || !x) die("out of memory");
128 tyw_rng = seed;
129 for (size_t i = 0; i < m * n; i++) {
130 uint64_t r = tyw_next();
131 w[i] = (int8_t)((r % 1000) < sp_milli ? 0 : ((r >> 32) & 1 ? 1 : -1));
132 }
133 for (size_t i = 0; i < n; i++) x[i] = (int8_t)(tyw_next() & 0xff);
134
135 /* Oracle check once, then one plan per thread (packed on that thread's cpu
136 * so first touch lands where it will be read). */
137 int32_t *yo = malloc(m * sizeof *yo);
138 if (!yo || oma_rz_oracle(w, m, n, x, yo) != OMA_RZ_OK) die("oracle failed");
139 tyw_thread th[TYW_MAX_CPUS];
140 memset(th, 0, sizeof th);
141 int oracle_ok = 1;
142 for (int k = 0; k < ncpu; k++) {
143 th[k].im = im;
144 th[k].cpu = cpu[k];
145 pin_to(cpu[k]);
146 if (im->pack(&th[k].plan, w, m, n) != OMA_RZ_OK) die("pack failed");
147 th[k].x = x;
148 th[k].y = calloc(m, sizeof(int32_t));
149 if (!th[k].y) die("out of memory");
150 im->run(&th[k].plan, x, th[k].y);
151 if (memcmp(th[k].y, yo, m * sizeof *yo) != 0) oracle_ok = 0;
152 }
153 free(w);
154 free(yo);
155 if (!oracle_ok) die("realization disagrees with oracle");
156
157 R15Pmu pmu;
158 char why[256] = "";
159 int pmu_ok = r15_pmu_open(&pmu, why, sizeof why) == 0;
160 printf("READY %d %zu %zu\n", (int)getpid(), th[0].plan.weight_bytes, th[0].plan.footprint_bytes);
161 fflush(stdout);
162
163 char go;
164 if (read(0, &go, 1) != 1) die("no GO byte");
165 if (pmu_ok) r15_pmu_start(&pmu);
166 uint64_t t_go = r15_now_ns(), deadline = t_go + (uint64_t)secs * 1000000000ull;
167 pthread_t tid[TYW_MAX_CPUS];
168 for (int k = 0; k < ncpu; k++) {
169 th[k].deadline_ns = deadline;
170 if (pthread_create(&tid[k], NULL, worker, &th[k]) != 0) die("pthread_create");
171 }
172 for (int k = 0; k < ncpu; k++) pthread_join(tid[k], NULL);
173 R15PmuRead pr;
174 memset(&pr, 0, sizeof pr);
175 if (pmu_ok) r15_pmu_stop(&pmu, &pr);
176 uint64_t t_done = r15_now_ns();
177
178 uint64_t calls = 0, t0 = th[0].t0_ns, t1 = th[0].t1_ns;
179 int pins = 1;
180 for (int k = 0; k < ncpu; k++) {
181 calls += th[k].calls;
182 if (th[k].t0_ns < t0) t0 = th[k].t0_ns;
183 if (th[k].t1_ns > t1) t1 = th[k].t1_ns;
184 pins &= th[k].pin_ok;
185 }
186 printf("{\"tag\":\"%s\",\"rz\":\"%s\",\"m\":%zu,\"n\":%zu,\"sparsity_milli\":%u,\"seed\":%" PRIu64
187 ",\"cpus\":\"%s\",\"threads\":%d,\"pinned\":%d,\"oracle_ok\":%d,\"weight_bytes\":%zu"
188 ",\"planned_ns\":%" PRIu64 ",\"go_ns\":%" PRIu64 ",\"t0_ns\":%" PRIu64 ",\"t1_ns\":%" PRIu64
189 ",\"done_ns\":%" PRIu64 ",\"calls\":%" PRIu64 ",\"thread_calls\":[",
190 tag, rz, m, n, sp_milli, seed, cpus, ncpu, pins, oracle_ok, th[0].plan.weight_bytes,
191 (uint64_t)((uint64_t)secs * 1000000000u), t_go, t0, t1, t_done, calls);
192 for (int k = 0; k < ncpu; k++) printf("%s%" PRIu64, k ? "," : "", th[k].calls);
193 printf("],\"pmu_open\":%d,\"pmu_why\":\"%s\"", pmu_ok, pmu_ok ? "" : why);
194 if (pmu_ok) r15_json_pmu(stdout, "pmu", &pmu, &pr);
195 printf("}\n");
196 fflush(stdout);
197 if (pmu_ok) r15_pmu_close(&pmu);
198 for (int k = 0; k < ncpu; k++) {
199 oma_rz_free(&th[k].plan);
200 free(th[k].y);
201 }
202 free(x);
203 return 0;
204}