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 / src / turing / history_selector.c

163 lines · 6983 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/* TURING ranking core and control arm: StarPU-style history-based selector
18 * (docs/turing/TURING_W0_PROPOSAL.md K.3, K.7, Fable item 7).
19 *
20 * Ranking core (shared with the Field selector v1): per (spec_id, footprint
21 * bucket) running mean of the observed per-call cost; the minimum running
22 * mean wins, lowest spec index on an exact tie. The footprint is what
23 * StarPU's history model hashes: the data sizes, here (n, m) plus the pack
24 * mode (packing per call is a different codelet cost). Sparsity changes
25 * values, not sizes, so it is not in the footprint.
26 *
27 * Control arm: among specs that can execute the query (n <= max_n, StarPU's
28 * can_execute), if any has no observation in the bucket, pick the next one in
29 * a seeded round-robin order (cold start / calibration); otherwise the ranking
30 * core. No filters beyond can_execute, no noise band, no receipts cited.
31 */
32#include "turing/select.h"
33#include "omega_native.h"
34
35#include <inttypes.h>
36#include <math.h>
37#include <stdio.h>
38#include <string.h>
39
40uint64_t turing_splitmix64(uint64_t *s) {
41 uint64_t st[1] = { *s }, out = 0;
42 (void)omega2_turing_om_splitmix64(&out, st, 1);
43 *s = st[0];
44 return out;
45}
46
47/* ------------------------------------------------------------ ranking core */
48
49void turing_bucket_reset(turing_hist_bucket *b, uint64_t n, uint64_t m, int pack) {
50 memset(b, 0, sizeof *b);
51 b->n = n;
52 b->m = m;
53 b->pack = pack;
54 for (uint32_t k = 0; k < TURING_MAX_SPECS; ++k) b->order[k] = k;
55}
56
57void turing_bucket_observe(turing_hist_bucket *b, size_t k, uint64_t cost_ps) {
58 uint32_t cnt[1] = { b->count[k] };
59 uint64_t mb[1] = { 0 }, u = 0;
60 memcpy(&mb[0], &b->mean_ps[k], 8);
61 (void)omega2_turing_om_bucket_observe(&u, cnt, 1, mb, 1, cost_ps);
62 b->count[k] = cnt[0];
63 memcpy(&b->mean_ps[k], &mb[0], 8);
64}
65
66int turing_rank_min_cost(const turing_hist_bucket *b, const int *allowed, size_t nspec) {
67 uint32_t alw[TURING_MAX_SPECS] = { 0 };
68 uint64_t best = UINT64_MAX;
69 size_t lim = nspec < TURING_MAX_SPECS ? nspec : TURING_MAX_SPECS;
70 for (size_t k = 0; k < lim; ++k) alw[k] = allowed[k] ? 1u : 0u;
71 (void)omega2_turing_om_rank_min_cost(&best, b->count, TURING_MAX_SPECS,
72 (const uint64_t *)b->mean_ps, TURING_MAX_SPECS,
73 alw, TURING_MAX_SPECS, nspec);
74 return best == UINT64_MAX ? -1 : (int)best;
75}
76
77/* ------------------------------------------------------------- control arm */
78
79void turing_history_init(turing_history *h, uint64_t seed) {
80 memset(h, 0, sizeof *h);
81 h->seed = seed;
82}
83
84static turing_hist_bucket *bucket_get(turing_history *h, const turing_store *st, uint64_t n, uint64_t m, int pack) {
85 for (size_t i = 0; i < h->nbucket; ++i)
86 if (h->bucket[i].n == n && h->bucket[i].m == m && h->bucket[i].pack == pack) return &h->bucket[i];
87 if (h->nbucket >= TURING_HIST_MAX_BUCKETS) return NULL;
88 turing_hist_bucket *b = &h->bucket[h->nbucket++];
89 turing_bucket_reset(b, n, m, pack);
90 /* Seeded Fisher-Yates over spec indices, one stream per bucket. */
91 uint64_t s = h->seed ^ (n * 0x100000001B3ull) ^ (m << 20) ^ (uint64_t)pack;
92 uint64_t u = 0;
93 (void)omega2_turing_om_fisher_yates(&u, b->order, TURING_MAX_SPECS, st->nspec, s);
94 return b;
95}
96
97int turing_history_observe(turing_history *h, const turing_store *st, size_t k, uint64_t n, uint64_t m, int pack,
98 uint64_t cost_ps) {
99 if (!h || !st || k >= st->nspec) return -1;
100 turing_hist_bucket *b = bucket_get(h, st, n, m, pack);
101 if (!b) return -1;
102 turing_bucket_observe(b, k, cost_ps);
103 return 0;
104}
105int turing_history_load(turing_history *h, const turing_store *st, const turing_query *skip) {
106 for (size_t i = 0; i < st->nev; ++i) {
107 const turing_evidence *e = &st->ev[i];
108 if (skip && e->n == skip->n && e->m == skip->m && e->sparsity_milli == skip->sparsity_milli) continue;
109 int k = -1;
110 for (size_t j = 0; j < st->nspec; ++j)
111 if (turing_digest_eq(&st->spec_id[j], &e->spec_id)) k = (int)j;
112 if (k < 0) return -1;
113 for (int pack = 0; pack < 2; ++pack)
114 if (turing_history_observe(h, st, (size_t)k, e->n, e->m, pack, turing_ev_cost(e, pack)) != 0) return -1;
115 }
116 return 0;
117}
118
119int turing_history_select(turing_history *h, const turing_store *st, const turing_query *q, turing_decision *d) {
120 if (!h || !st || !q || !d || st->nspec > TURING_MAX_CAND) return -1;
121 turing_hist_bucket *b = bucket_get(h, st, q->n, q->m, q->pack);
122 if (!b) return -1;
123 memset(d, 0, sizeof *d);
124 d->contract_digest = st->contract_digest;
125 snprintf(d->selector, sizeof d->selector, "%s", TURING_HISTORY_SELECTOR);
126 snprintf(d->constraints, sizeof d->constraints, "can_execute (n <= max_n)");
127 d->n = d->cell_n = q->n;
128 d->m = d->cell_m = q->m;
129 d->sparsity_milli = d->cell_sparsity_milli = q->sparsity_milli;
130 d->pack = q->pack;
131 d->exact_cell = 0; /* bucket = footprint, not a measured cell */
132 d->ncand = st->nspec;
133 d->chosen = -1;
134 snprintf(d->tie_resolution, sizeof d->tie_resolution, "none");
135 int can[TURING_MAX_CAND] = {0};
136 for (size_t k = 0; k < st->nspec; ++k) {
137 d->cand[k] = st->spec_id[k];
138 snprintf(d->cand_rz[k], sizeof d->cand_rz[k], "%s", st->spec[k].rz_id);
139 can[k] = q->n <= st->spec[k].max_n;
140 d->reason[k] = can[k] ? TURING_R_RANKED : TURING_R_MAX_N;
141 if (can[k] && b->count[k]) d->cost_ps[k] = (uint64_t)llround(b->mean_ps[k]);
142 }
143 /* Cold start: next unobserved spec in the seeded round-robin order. */
144 for (size_t t = 0; t < st->nspec; ++t) {
145 uint32_t k = b->order[(b->cursor + t) % st->nspec];
146 if (can[k] && b->count[k] == 0) {
147 b->cursor = (uint32_t)((b->cursor + t + 1) % st->nspec);
148 d->chosen = (int)k;
149 d->reason[k] = TURING_R_EXPLORE;
150 snprintf(d->verdict, sizeof d->verdict, "EXPLORE");
151 return (int)k;
152 }
153 }
154 int best = turing_rank_min_cost(b, can, st->nspec);
155 if (best < 0) {
156 snprintf(d->verdict, sizeof d->verdict, "NONE");
157 return -1;
158 }
159 d->chosen = best;
160 d->reason[best] = TURING_R_CHOSEN;
161 snprintf(d->verdict, sizeof d->verdict, "CHOSEN");
162 return best;
163}