omega-feel
This project has not described itself yet.
Brought by drakestapleton
omega-feel / tests / oracle.wl
| 1 | (* oracle.wl: the reference for Omega's kernel tuner ("feel"), in exact integer arithmetic. |
| 2 | It makes synthetic timing traces (and one that interleaves two of them) from a 64-bit LCG, runs the tuner's state machine over |
| 3 | each, and gives (1) the SHA-256 of every trace as text, so a second generator can be checked |
| 4 | byte for byte, and (2) every decision the state machine takes. The Omega implementation must |
| 5 | print the same decisions from the same traces. Rules: SPEC.md, section 3. |
| 6 | The final value is one string; written to a file with a final newline it is tests/expected-wolfram.txt |
| 7 | (tests/seal.sh records the hashes of the scripts and their results). *) |
| 8 | lcgA = 6364136223846793005; lcgC = 1442695040888963407; M64 = 2^64; |
| 9 | (* scenario: name, seed, K, L, baseA, baseB, switch, spread, tailp, tailadd, wrongmod *) |
| 10 | scen = { |
| 11 | {"fast5", 1, 2, 6000, {100000, 95000}, {100000, 95000}, 0, {6000, 6000}, {0, 0}, {0, 0}, {0, 0}}, |
| 12 | {"slow10", 2, 2, 40000, {100000, 110000}, {100000, 110000}, 0, {6000, 6000}, {0, 0}, {0, 0}, {0, 0}}, |
| 13 | {"wrongfast", 3, 3, 12000, {100000, 50000, 93000}, {100000, 50000, 93000}, 0, {6000, 6000, 6000}, {0, 0, 0}, {0, 0, 0}, {0, 2, 0}}, |
| 14 | {"equal", 4, 2, 30000, {100000, 100000}, {100000, 100000}, 0, {6000, 6000}, {0, 0}, {0, 0}, {0, 0}}, |
| 15 | {"tailC", 5, 2, 20000, {100000, 95000}, {100000, 95000}, 0, {2000, 2000}, {0, 8}, {0, 900000}, {0, 0}}, |
| 16 | {"drift", 6, 2, 34000, {100000, 108000}, {100000, 92000}, 20000, {6000, 6000}, {0, 0}, {0, 0}, {0, 0}}, |
| 17 | {"tailI", 7, 2, 20000, {100000, 105000}, {100000, 105000}, 0, {2000, 2000}, {8, 0}, {900000, 0}, {0, 0}}, |
| 18 | {"twoA", 8, 2, 6000, {100000, 95000}, {100000, 95000}, 0, {6000, 6000}, {0, 0}, {0, 0}, {0, 0}}, |
| 19 | {"twoB", 9, 3, 6000, {40000, 44000, 30000}, {40000, 44000, 30000}, 0, {3000, 3000, 3000}, {0, 0, 0}, {0, 0, 0}, {0, 0, 3}}, |
| 20 | {"ties", 10, 2, 12000, {1000, 1000}, {1000, 1000}, 0, {3, 3}, {0, 0}, {0, 0}, {0, 0}}, |
| 21 | {"cap", 11, 2, 210000, {100000, 110000}, {100000, 110000}, 0, {6000, 6000}, {0, 0}, {0, 0}, {0, 0}}, |
| 22 | {"lateGain", 12, 2, 579, {100, 100}, {100, 90}, 323, {1, 1}, {0, 0}, {0, 0}, {0, 0}}, |
| 23 | {"earlyGain", 13, 2, 579, {100, 90}, {100, 100}, 323, {1, 1}, {0, 0}, {0, 0}, {0, 0}}, |
| 24 | {"equalSum", 14, 2, 131, {100, 69}, {100, 101}, 69, {1, 1}, {0, 0}, {0, 0}, {0, 0}}, |
| 25 | {"lateWrong", 25, 2, 3000, {100000, 95000}, {100000, 95000}, 0, {6000, 6000}, {0, 0}, {0, 0}, {0, 500}}, |
| 26 | {"meanWin", 17, 2, 3000, {100, 80}, {100, 80}, 0, {1, 1}, {0, 2}, {0, 21}, {0, 0}}}; |
| 27 | gen[{name_, seed_, K_, L_, bA_, bB_, sw_, spread_, tailp_, tailadd_, wrong_}] := Module[{x = seed, r, r2, ns, ok}, |
| 28 | Table[ |
| 29 | ns = Table[ |
| 30 | x = Mod[lcgA x + lcgC, M64]; r = Quotient[x, 2^33]; |
| 31 | x = Mod[lcgA x + lcgC, M64]; r2 = Quotient[x, 2^33]; |
| 32 | If[sw > 0 && i > sw, bB[[j]], bA[[j]]] + Mod[r, spread[[j]]] + |
| 33 | If[tailp[[j]] > 0 && Mod[r2, tailp[[j]]] == 0, tailadd[[j]], 0], {j, K}]; |
| 34 | ok = Total[Table[If[wrong[[j]] > 0 && Mod[i, wrong[[j]]] == 0, 0, 2^(j - 1)], {j, K}]]; |
| 35 | {ns, ok}, {i, L}]]; |
| 36 | (* trace text: "c CLASS K" declares a class, "t CLASS ns0 .. ns(K-1) okmask" is one launch of it *) |
| 37 | row[c_, r_] := StringJoin["t ", ToString[c], " ", Riffle[ToString /@ Append[r[[1]], r[[2]]], " "], "\n"]; |
| 38 | text[K_, data_] := StringJoin["c 0 ", ToString[K], "\n", Map[row[0, #] &, data]]; |
| 39 | (* two classes at once, launches alternating: class 0 then class 1 *) |
| 40 | text2[K0_, d0_, K1_, d1_] := StringJoin["c 0 ", ToString[K0], "\nc 1 ", ToString[K1], "\n", |
| 41 | MapThread[StringJoin[row[0, #1], row[1, #2]] &, {d0, d1}]]; |
| 42 | (* the state machine *) |
| 43 | HH = 12; NMIN = 32; NMAX = 256; VV = 3; REST0 = 1024; RESTMAX = 65536; WARM = 64; |
| 44 | run[K_, data_] := Module[{status = ConstantArray[0, K], inc = 0, mode = 0, rest = WARM, restlen = REST0, |
| 45 | next = 0, ch = -1, vcount = 0, k = 0, w = 0, l = 0, sI = 0, sC = 0, hI = 0, hC = 0, n = 0, pend = False, |
| 46 | tfirst = 0, z = 1, o = 0, events = {}, counts = ConstantArray[0, K], arm, shadow, a, found, tI, tC, t, d, |
| 47 | out, i = 0, ns, okmask, start}, |
| 48 | (* mode: 0 rest, 1 shadow, 2 duel *) |
| 49 | status[[1]] = 1; |
| 50 | start[] := (mode = 2; k = 0; w = 0; l = 0; sI = 0; sC = 0; hI = 0; hC = 0; n = 0); |
| 51 | Do[ |
| 52 | i++; {ns, okmask} = row; shadow = -1; pend = False; arm = inc; |
| 53 | If[mode == 0, |
| 54 | If[rest > 0, rest--, |
| 55 | found = -1; |
| 56 | Do[a = Mod[next + s, K]; If[found < 0 && a != inc && status[[a + 1]] != 2, found = a], {s, 1, K}]; |
| 57 | If[found < 0, rest = restlen, |
| 58 | ch = found; next = found; |
| 59 | If[status[[ch + 1]] == 0, mode = 1; vcount = 0, start[]]]]]; |
| 60 | If[mode == 1, shadow = ch]; |
| 61 | If[mode == 2, |
| 62 | If[EvenQ[k], z = Mod[z*1103515245 + 12345, 2^31]; o = BitGet[z, 16]]; |
| 63 | arm = If[(EvenQ[k] && o == 0) || (OddQ[k] && o == 1), inc, ch]; pend = True]; |
| 64 | counts[[arm + 1]]++; |
| 65 | If[shadow >= 0, |
| 66 | If[BitGet[okmask, shadow] == 0, |
| 67 | status[[ch + 1]] = 2; AppendTo[events, {"B", ch, i}]; mode = 0; rest = restlen, |
| 68 | vcount++; If[vcount == VV, status[[ch + 1]] = 1; AppendTo[events, {"M", ch, i}]; start[]]]]; |
| 69 | If[pend, |
| 70 | t = ns[[arm + 1]]; |
| 71 | If[EvenQ[k], tfirst = t, |
| 72 | If[arm == ch, tC = t; tI = tfirst, tC = tfirst; tI = t]; |
| 73 | sI += tI; sC += tC; If[tC < tI, w++]; If[tC > tI, l++]; n++; |
| 74 | If[n == NMAX/2, hI = sI; hC = sC]; |
| 75 | out = ""; |
| 76 | Which[ |
| 77 | n >= NMIN && l - w >= HH && sC >= sI, out = "R", |
| 78 | n >= NMAX, out = Which[ |
| 79 | hC < hI && sC - hC < sI - hI && w - l >= HH, "A", |
| 80 | 64 hC < 63 hI && 64 (sC - hC) < 63 (sI - hI), "Am", |
| 81 | True, "D"]]; |
| 82 | If[out != "", |
| 83 | AppendTo[events, {out, ch, i, n, w, l, sI, sC}]; |
| 84 | If[out == "A" || out == "Am", inc = ch; restlen = REST0, restlen = Min[2 restlen, RESTMAX]]; |
| 85 | rest = restlen; mode = 0]]; |
| 86 | k++], {row, data}]; |
| 87 | {events, {"F", inc, restlen, counts, status}}]; |
| 88 | res = Table[Module[{d = gen[s], K = s[[3]]}, |
| 89 | {s[[1]], Hash[StringToByteArray[text[K, d]], "SHA256", "HexString"], run[K, d]}], {s, scen}]; |
| 90 | (* the result: one string, one line per decision, in scenario order (the first line is the two-class trace's hash) *) |
| 91 | StringRiffle[Join[ |
| 92 | {"two sha256 " <> Hash[StringToByteArray[text2[2, gen[scen[[8]]], 3, gen[scen[[9]]]]], "SHA256", "HexString"]}, |
| 93 | Flatten[Map[Function[r, Join[ |
| 94 | {r[[1]] <> " sha256 " <> r[[2]]}, |
| 95 | (r[[1]] <> " " <> StringRiffle[ToString /@ #, " "]) & /@ r[[3, 1]], |
| 96 | {r[[1]] <> " " <> StringRiffle[ToString /@ Flatten[r[[3, 2]]], " "]}]], res]]], "\n"] |