@gmitch215/folio 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -460
- package/dist/boot.d.ts +1 -0
- package/dist/boot.js +1 -0
- package/dist/browser/index.d.ts +73 -0
- package/dist/browser/index.js +277 -0
- package/dist/browser.d.ts +1 -68
- package/dist/browser.js +1 -274
- package/dist/continuation.d.ts +1 -0
- package/dist/continuation.js +1 -0
- package/dist/{compiler.d.ts → execution/compiler.d.ts} +1 -1
- package/dist/{compiler.js → execution/compiler.js} +5 -12
- package/dist/{coordinator.d.ts → execution/coordinator.d.ts} +1 -1
- package/dist/{coordinator.js → execution/coordinator.js} +55 -8
- package/dist/{extract.d.ts → execution/extract.d.ts} +22 -13
- package/dist/{extract.js → execution/extract.js} +137 -10
- package/dist/execution/fold.d.ts +44 -0
- package/dist/execution/fold.js +105 -0
- package/dist/{guards.d.ts → execution/guards.d.ts} +2 -1
- package/dist/execution/runs.d.ts +44 -0
- package/dist/execution/runs.js +182 -0
- package/dist/{transaction.d.ts → execution/transaction.d.ts} +10 -0
- package/dist/execution/types.d.ts +221 -0
- package/dist/folds/affine-state.d.ts +142 -0
- package/dist/folds/affine-state.js +737 -0
- package/dist/folds/affine.d.ts +148 -0
- package/dist/folds/affine.js +562 -0
- package/dist/folds/boundaries.d.ts +48 -0
- package/dist/folds/boundaries.js +119 -0
- package/dist/folds/certified.d.ts +55 -0
- package/dist/folds/certified.js +178 -0
- package/dist/folds/certify.d.ts +87 -0
- package/dist/folds/certify.js +2611 -0
- package/dist/footprints/memo.d.ts +74 -0
- package/dist/footprints/memo.js +256 -0
- package/dist/footprints/ranges.d.ts +78 -0
- package/dist/footprints/ranges.js +263 -0
- package/dist/footprints/read-scan.d.ts +18 -0
- package/dist/footprints/read-scan.js +94 -0
- package/dist/footprints/read-windows.d.ts +77 -0
- package/dist/footprints/read-windows.js +245 -0
- package/dist/index.d.ts +16 -4
- package/dist/index.js +14 -2
- package/dist/iteration/admission.d.ts +149 -0
- package/dist/iteration/admission.js +201 -0
- package/dist/iteration/index.d.ts +239 -0
- package/dist/iteration/index.js +1210 -0
- package/dist/iteration/inline.d.ts +182 -0
- package/dist/{inline.js → iteration/inline.js} +277 -21
- package/dist/{iteration-owner.d.ts → iteration/owner.d.ts} +16 -3
- package/dist/{iteration-owner.js → iteration/owner.js} +6 -5
- package/dist/iteration/worker.js +216 -0
- package/dist/{managed.d.ts → managed/index.d.ts} +75 -3
- package/dist/managed/index.js +817 -0
- package/dist/managed/outbox.d.ts +128 -0
- package/dist/managed/outbox.js +156 -0
- package/dist/managed/pool.d.ts +221 -0
- package/dist/managed/pool.js +310 -0
- package/dist/managed/tasks.d.ts +154 -0
- package/dist/managed/tasks.js +183 -0
- package/dist/{bake.d.ts → memory/bake.d.ts} +12 -6
- package/dist/{bake.js → memory/bake.js} +9 -7
- package/dist/memory/continuation.d.ts +141 -0
- package/dist/memory/continuation.js +495 -0
- package/dist/memory/elastic-peer.d.ts +20 -0
- package/dist/memory/elastic-peer.js +39 -0
- package/dist/memory/elastic-sql.d.ts +11 -0
- package/dist/memory/elastic-sql.js +35 -0
- package/dist/memory/elastic.d.ts +130 -0
- package/dist/memory/elastic.js +475 -0
- package/dist/memory/growth-governor.d.ts +16 -0
- package/dist/memory/growth-governor.js +42 -0
- package/dist/memory/imports.d.ts +10 -0
- package/dist/memory/imports.js +89 -0
- package/dist/{iteration-layout.d.ts → memory/iteration-layout.d.ts} +61 -1
- package/dist/{iteration-layout.js → memory/iteration-layout.js} +54 -3
- package/dist/memory/lease.d.ts +45 -0
- package/dist/memory/lease.js +274 -0
- package/dist/{pages.d.ts → memory/pages.d.ts} +3 -3
- package/dist/memory.d.ts +5 -10
- package/dist/memory.js +5 -89
- package/dist/{context.d.ts → node/context.d.ts} +17 -3
- package/dist/{context.js → node/context.js} +60 -14
- package/dist/{host.d.ts → node/host.d.ts} +1 -1
- package/dist/{host.js → node/host.js} +2 -2
- package/dist/{node.d.ts → node/index.d.ts} +46 -49
- package/dist/{node.js → node/index.js} +101 -26
- package/dist/{tasks.d.ts → node/tasks.d.ts} +16 -17
- package/dist/{tasks.js → node/tasks.js} +12 -11
- package/dist/node/worker.d.ts +1 -0
- package/dist/{worker.js → node/worker.js} +2 -2
- package/dist/ownership/index.d.ts +161 -0
- package/dist/ownership/index.js +6565 -0
- package/dist/ownership/redirect.d.ts +30 -0
- package/dist/ownership/redirect.js +179 -0
- package/dist/ownership/slices.d.ts +44 -0
- package/dist/ownership/slices.js +72 -0
- package/dist/{frames.d.ts → remote/frames.d.ts} +6 -2
- package/dist/remote/frames.js +306 -0
- package/dist/{remote.d.ts → remote/index.d.ts} +7 -2
- package/dist/{remote.js → remote/index.js} +7 -2
- package/dist/routing/index.d.ts +203 -0
- package/dist/routing/index.js +671 -0
- package/dist/routing/worker.d.ts +1 -0
- package/dist/routing/worker.js +4 -0
- package/dist/slices/index.d.ts +126 -0
- package/dist/slices/index.js +441 -0
- package/dist/slices/worker.d.ts +1 -0
- package/dist/slices/worker.js +38 -0
- package/dist/workers.d.ts +5 -0
- package/dist/workers.js +5 -0
- package/package.json +32 -46
- package/dist/frames.js +0 -159
- package/dist/inline.d.ts +0 -68
- package/dist/iteration-admission.d.ts +0 -76
- package/dist/iteration-admission.js +0 -72
- package/dist/iteration-worker.js +0 -150
- package/dist/iteration.d.ts +0 -163
- package/dist/iteration.js +0 -435
- package/dist/managed.js +0 -421
- /package/dist/{browser-worker.d.ts → browser/worker.d.ts} +0 -0
- /package/dist/{browser-worker.js → browser/worker.js} +0 -0
- /package/dist/{guards.js → execution/guards.js} +0 -0
- /package/dist/{import-order.d.ts → execution/import-order.d.ts} +0 -0
- /package/dist/{import-order.js → execution/import-order.js} +0 -0
- /package/dist/{transaction.js → execution/transaction.js} +0 -0
- /package/dist/{iteration-worker.d.ts → execution/types.js} +0 -0
- /package/dist/{worker.d.ts → iteration/worker.d.ts} +0 -0
- /package/dist/{pages.js → memory/pages.js} +0 -0
- /package/dist/{values.d.ts → remote/values.d.ts} +0 -0
- /package/dist/{values.js → remote/values.js} +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { iterationSummaryShift, iterationSummaryWords } from "../memory/iteration-layout.js";
|
|
2
|
+
// one scan at a time per thread; the run being built lives here so the word loop stays flat
|
|
3
|
+
let start = -1;
|
|
4
|
+
let at = 0;
|
|
5
|
+
let runs = [];
|
|
6
|
+
let visited = 0;
|
|
7
|
+
let found = 0;
|
|
8
|
+
function visit(words, from, to) {
|
|
9
|
+
if (start >= 0 && at !== from) {
|
|
10
|
+
runs.push(start, at * 32 - start);
|
|
11
|
+
start = -1;
|
|
12
|
+
}
|
|
13
|
+
visited += to - from;
|
|
14
|
+
for (let k = from; k < to; k++) {
|
|
15
|
+
const w = words[k];
|
|
16
|
+
if (w === 0) {
|
|
17
|
+
if (start >= 0) {
|
|
18
|
+
runs.push(start, k * 32 - start);
|
|
19
|
+
start = -1;
|
|
20
|
+
}
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
words[k] = 0;
|
|
24
|
+
found++;
|
|
25
|
+
if (w === 0xffffffff) {
|
|
26
|
+
if (start < 0)
|
|
27
|
+
start = k * 32;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
for (let b = 0; b < 32; b++) {
|
|
31
|
+
if ((w >>> b) & 1) {
|
|
32
|
+
if (start < 0)
|
|
33
|
+
start = k * 32 + b;
|
|
34
|
+
}
|
|
35
|
+
else if (start >= 0) {
|
|
36
|
+
runs.push(start, k * 32 + b - start);
|
|
37
|
+
start = -1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
at = to;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Reads a read bitmap into `[start, length]` runs of granules and zeroes the words it reads. With
|
|
45
|
+
* a summary only the blocks of 64 words whose byte is non-zero are read, and every summary byte is
|
|
46
|
+
* zeroed; the summary must be a superset of the non-zero blocks (see `lowerInline`). Without one
|
|
47
|
+
* every word is read. The summary length is a multiple of 4.
|
|
48
|
+
*/
|
|
49
|
+
export function scanReads(words, summary, stats) {
|
|
50
|
+
start = -1;
|
|
51
|
+
at = 0;
|
|
52
|
+
runs = [];
|
|
53
|
+
visited = 0;
|
|
54
|
+
found = 0;
|
|
55
|
+
if (!summary)
|
|
56
|
+
visit(words, 0, words.length);
|
|
57
|
+
else {
|
|
58
|
+
const blocks = Math.ceil(words.length / iterationSummaryWords);
|
|
59
|
+
const wide = new Uint32Array(summary.buffer, summary.byteOffset, summary.length >> 2);
|
|
60
|
+
for (let i = 0; i < wide.length; i++) {
|
|
61
|
+
if (wide[i] === 0)
|
|
62
|
+
continue;
|
|
63
|
+
for (let b = 4 * i; b < 4 * i + 4; b++) {
|
|
64
|
+
if (!summary[b])
|
|
65
|
+
continue;
|
|
66
|
+
summary[b] = 0;
|
|
67
|
+
if (b < blocks)
|
|
68
|
+
visit(words, b * iterationSummaryWords, Math.min(words.length, (b + 1) * iterationSummaryWords));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (start >= 0)
|
|
73
|
+
runs.push(start, at * 32 - start);
|
|
74
|
+
if (stats) {
|
|
75
|
+
stats.words += visited;
|
|
76
|
+
stats.set += found;
|
|
77
|
+
}
|
|
78
|
+
const out = Int32Array.from(runs);
|
|
79
|
+
runs = [];
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Sets the summary bytes of the blocks that the address range `[low, low + bytes)` touches, in the
|
|
84
|
+
* summary of `mask + 1` bytes at `base`. A window the owner marks in the read bitmap itself is
|
|
85
|
+
* summarized the same way.
|
|
86
|
+
*/
|
|
87
|
+
export function markReadSummary(u8, base, mask, low, bytes) {
|
|
88
|
+
if (bytes <= 0)
|
|
89
|
+
return;
|
|
90
|
+
const first = Math.floor(low / 2 ** iterationSummaryShift);
|
|
91
|
+
const last = Math.floor((low + bytes - 1) / 2 ** iterationSummaryShift);
|
|
92
|
+
for (let b = first; b <= last; b++)
|
|
93
|
+
u8[base + (b & mask)] = 1;
|
|
94
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { IterationReadWindow, IterationReadWindows, ReadWindowCounts } from '../execution/types.ts';
|
|
2
|
+
export type { IterationReadWindow, IterationReadWindows, ReadWindowCounts };
|
|
3
|
+
/** What one iteration's outcome has to hold for learning. */
|
|
4
|
+
export interface ReadWindowSample {
|
|
5
|
+
index: number;
|
|
6
|
+
status: number;
|
|
7
|
+
/** sorted write granules (32 bytes) */
|
|
8
|
+
written: Int32Array;
|
|
9
|
+
/** [start, length] runs of read granules (64 bytes) */
|
|
10
|
+
reads: Int32Array;
|
|
11
|
+
}
|
|
12
|
+
/** Smallest window worth marking: four read granules for the strided one, two for the fixed one. */
|
|
13
|
+
export declare const readWindowMinimum: {
|
|
14
|
+
readonly strided: number;
|
|
15
|
+
readonly fixed: number;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Executed Wasm instructions of one load of at most 8 bytes, counted on the lowering with the slot
|
|
19
|
+
* checks and no frame shadow (one node per instruction). The windows go before the exempt check
|
|
20
|
+
* (`window` order) or after it (`exempt` order, the `exemptFirst` option). The frame shadow adds
|
|
21
|
+
* 10 to a tracked load and to a miss and nothing to a hit or an exempt load.
|
|
22
|
+
*/
|
|
23
|
+
export declare const readWindowInstructions: {
|
|
24
|
+
readonly tracked: 50;
|
|
25
|
+
readonly frame: 10;
|
|
26
|
+
readonly exempt: 15;
|
|
27
|
+
/** per window a load runs through without hitting it */
|
|
28
|
+
readonly guard: 6;
|
|
29
|
+
/** a hit in the first window, and in the second, by order */
|
|
30
|
+
readonly hit: {
|
|
31
|
+
readonly window: readonly [11, 17];
|
|
32
|
+
readonly exempt: readonly [21, 27];
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/** What a run of the window module is expected to cost or save, in instructions. */
|
|
36
|
+
export interface ReadWindowVerdict {
|
|
37
|
+
/** Instructions the hits would not execute, against the lowering with no windows. */
|
|
38
|
+
saved: number;
|
|
39
|
+
/** Instructions the misses, the exempt loads and the marking of the windows add. */
|
|
40
|
+
spent: number;
|
|
41
|
+
/** The order of the two checks that costs less for these counts. */
|
|
42
|
+
order: 'window' | 'exempt';
|
|
43
|
+
pay: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Whether read windows pay for a loop, and in which order, from the counts of a batch on them. A
|
|
47
|
+
* hit replaces the mark and the slot checks of a tracked load; a miss executes the window guards on
|
|
48
|
+
* top of a tracked load; an exempt load executes them too when the windows come first; and the
|
|
49
|
+
* owner marks every window granule before each iteration. Windows are armed when the instructions
|
|
50
|
+
* saved exceed those spent by at least `margin` of the instructions the counted tracked loads
|
|
51
|
+
* cost. A window of the wrong size is still sound, so this decides cost only.
|
|
52
|
+
*/
|
|
53
|
+
export declare function readWindowsPay(counts: ReadWindowCounts, windows: IterationReadWindows, frame: boolean, iterations: number, margin?: number): ReadWindowVerdict;
|
|
54
|
+
/** Fraction of the counted tracked loads' instructions a window run must save to be armed. */
|
|
55
|
+
export declare const readWindowMargin = 0.05;
|
|
56
|
+
/** Windows of iteration `k` inside `[0, memoryBytes)`, at most two. */
|
|
57
|
+
export declare function readWindowsAt(windows: IterationReadWindows, k: number, memoryBytes: number): IterationReadWindow[];
|
|
58
|
+
/**
|
|
59
|
+
* Whether every window of iterations `first` to `first + count - 1` stays clear of the forbidden
|
|
60
|
+
* ranges (the arena, the grown logs), as `[low, end)` pairs. A load in a window skips the checks
|
|
61
|
+
* that would have found an access to those ranges.
|
|
62
|
+
*/
|
|
63
|
+
export declare function readWindowsClear(windows: IterationReadWindows, first: number, count: number, memoryBytes: number, forbidden: readonly (readonly [number, number])[]): boolean;
|
|
64
|
+
/** Sets the read bits of every granule a window touches in the bitmap that starts at `base`. */
|
|
65
|
+
export declare function markReadWindow(u8: Uint8Array, base: number, low: number, bytes: number): void;
|
|
66
|
+
/**
|
|
67
|
+
* Windows from the read and write sets of tracked iterations that completed without a conflict.
|
|
68
|
+
* The strided one follows the largest read run of each iteration when the runs line up on one
|
|
69
|
+
* stride; when their lengths differ by more than a granule the window is the shortest one. The
|
|
70
|
+
* fixed one is the largest run every iteration read that is not inside the strided one. A window
|
|
71
|
+
* that another iteration wrote into, in the sample, is dropped: its reads would conflict. Needs
|
|
72
|
+
* three completed iterations.
|
|
73
|
+
*/
|
|
74
|
+
export declare function learnReadWindows(samples: readonly ReadWindowSample[]): {
|
|
75
|
+
windows?: IterationReadWindows;
|
|
76
|
+
reason?: string;
|
|
77
|
+
};
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { iterationGranules } from "../memory/iteration-layout.js";
|
|
2
|
+
const READ = iterationGranules.read;
|
|
3
|
+
const WRITE = iterationGranules.write;
|
|
4
|
+
/** Smallest window worth marking: four read granules for the strided one, two for the fixed one. */
|
|
5
|
+
export const readWindowMinimum = { strided: 4 * READ, fixed: 2 * READ };
|
|
6
|
+
/**
|
|
7
|
+
* Executed Wasm instructions of one load of at most 8 bytes, counted on the lowering with the slot
|
|
8
|
+
* checks and no frame shadow (one node per instruction). The windows go before the exempt check
|
|
9
|
+
* (`window` order) or after it (`exempt` order, the `exemptFirst` option). The frame shadow adds
|
|
10
|
+
* 10 to a tracked load and to a miss and nothing to a hit or an exempt load.
|
|
11
|
+
*/
|
|
12
|
+
export const readWindowInstructions = {
|
|
13
|
+
tracked: 50,
|
|
14
|
+
frame: 10,
|
|
15
|
+
exempt: 15,
|
|
16
|
+
/** per window a load runs through without hitting it */
|
|
17
|
+
guard: 6,
|
|
18
|
+
/** a hit in the first window, and in the second, by order */
|
|
19
|
+
hit: { window: [11, 17], exempt: [21, 27] }
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Whether read windows pay for a loop, and in which order, from the counts of a batch on them. A
|
|
23
|
+
* hit replaces the mark and the slot checks of a tracked load; a miss executes the window guards on
|
|
24
|
+
* top of a tracked load; an exempt load executes them too when the windows come first; and the
|
|
25
|
+
* owner marks every window granule before each iteration. Windows are armed when the instructions
|
|
26
|
+
* saved exceed those spent by at least `margin` of the instructions the counted tracked loads
|
|
27
|
+
* cost. A window of the wrong size is still sound, so this decides cost only.
|
|
28
|
+
*/
|
|
29
|
+
export function readWindowsPay(counts, windows, frame, iterations, margin = readWindowMargin) {
|
|
30
|
+
const c = readWindowInstructions;
|
|
31
|
+
const n = (windows.strided ? 1 : 0) + (windows.fixed ? 1 : 0);
|
|
32
|
+
const tracked = c.tracked + (frame ? c.frame : 0);
|
|
33
|
+
const bytes = (windows.strided?.bytes ?? 0) + (windows.fixed?.bytes ?? 0);
|
|
34
|
+
// one store per 512 bytes of window for the bitmap, one per 4 KiB for the summary
|
|
35
|
+
const marking = Math.ceil(bytes / 512 + bytes / 4096) * 4 * Math.max(iterations, 1);
|
|
36
|
+
const guards = c.guard * n;
|
|
37
|
+
const verdict = (order) => {
|
|
38
|
+
const [first, second] = c.hit[order];
|
|
39
|
+
const hit = n === 2 ? (first + second) / 2 : first;
|
|
40
|
+
const saved = counts.hits * (tracked - hit);
|
|
41
|
+
const spent = (counts.misses + (order === 'window' ? counts.exempt : 0)) * guards + marking;
|
|
42
|
+
return { saved, spent, order, pay: false };
|
|
43
|
+
};
|
|
44
|
+
const windowFirst = verdict('window');
|
|
45
|
+
const exemptFirst = verdict('exempt');
|
|
46
|
+
const best = exemptFirst.saved - exemptFirst.spent > windowFirst.saved - windowFirst.spent
|
|
47
|
+
? exemptFirst
|
|
48
|
+
: windowFirst;
|
|
49
|
+
const loads = counts.hits + counts.misses;
|
|
50
|
+
return { ...best, pay: best.saved - best.spent > margin * loads * tracked };
|
|
51
|
+
}
|
|
52
|
+
/** Fraction of the counted tracked loads' instructions a window run must save to be armed. */
|
|
53
|
+
export const readWindowMargin = 0.05;
|
|
54
|
+
/** Windows of iteration `k` inside `[0, memoryBytes)`, at most two. */
|
|
55
|
+
export function readWindowsAt(windows, k, memoryBytes) {
|
|
56
|
+
const found = [];
|
|
57
|
+
const s = windows.strided;
|
|
58
|
+
if (s) {
|
|
59
|
+
const low = READ * Math.floor((s.low + (k - s.index) * s.stride) / READ);
|
|
60
|
+
if (low >= 0 && low + s.bytes <= memoryBytes)
|
|
61
|
+
found.push({ low, bytes: s.bytes });
|
|
62
|
+
}
|
|
63
|
+
if (windows.fixed && windows.fixed.low + windows.fixed.bytes <= memoryBytes)
|
|
64
|
+
found.push(windows.fixed);
|
|
65
|
+
return found;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Whether every window of iterations `first` to `first + count - 1` stays clear of the forbidden
|
|
69
|
+
* ranges (the arena, the grown logs), as `[low, end)` pairs. A load in a window skips the checks
|
|
70
|
+
* that would have found an access to those ranges.
|
|
71
|
+
*/
|
|
72
|
+
export function readWindowsClear(windows, first, count, memoryBytes, forbidden) {
|
|
73
|
+
const wanted = (windows.strided ? 1 : 0) + (windows.fixed ? 1 : 0);
|
|
74
|
+
for (let k = first; k < first + count; k++) {
|
|
75
|
+
const found = readWindowsAt(windows, k, memoryBytes);
|
|
76
|
+
if (found.length !== wanted)
|
|
77
|
+
return false;
|
|
78
|
+
for (const w of found)
|
|
79
|
+
for (const [low, end] of forbidden)
|
|
80
|
+
if (w.low < end && w.low + w.bytes > low)
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
/** Sets the read bits of every granule a window touches in the bitmap that starts at `base`. */
|
|
86
|
+
export function markReadWindow(u8, base, low, bytes) {
|
|
87
|
+
const first = Math.floor(low / READ);
|
|
88
|
+
const last = Math.floor((low + bytes - 1) / READ);
|
|
89
|
+
for (let g = first; g <= last;) {
|
|
90
|
+
if ((g & 7) === 0 && g + 7 <= last) {
|
|
91
|
+
u8[base + (g >> 3)] = 0xff;
|
|
92
|
+
g += 8;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
u8[base + (g >> 3)] |= 1 << (g & 7);
|
|
96
|
+
g++;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// does a sorted list of write granules hold one that touches [low, end)
|
|
100
|
+
function writesTouch(sorted, low, end) {
|
|
101
|
+
let a = 0, b = sorted.length;
|
|
102
|
+
const from = Math.floor(low / WRITE);
|
|
103
|
+
while (a < b) {
|
|
104
|
+
const mid = (a + b) >> 1;
|
|
105
|
+
if (sorted[mid] < from)
|
|
106
|
+
a = mid + 1;
|
|
107
|
+
else
|
|
108
|
+
b = mid;
|
|
109
|
+
}
|
|
110
|
+
return a < sorted.length && sorted[a] * WRITE < end;
|
|
111
|
+
}
|
|
112
|
+
// the value in the open interval with the most trailing zero bits, so a power-of-two size wins
|
|
113
|
+
function pickStride(low, high) {
|
|
114
|
+
for (let step = 4096; step >= 1; step /= 2) {
|
|
115
|
+
const candidate = (Math.ceil(high / step) - 1) * step;
|
|
116
|
+
if (candidate > low && candidate < high)
|
|
117
|
+
return candidate;
|
|
118
|
+
}
|
|
119
|
+
return (low + high) / 2;
|
|
120
|
+
}
|
|
121
|
+
function intersectRuns(a, b) {
|
|
122
|
+
const out = [];
|
|
123
|
+
for (let i = 0, j = 0; i < a.length && j < b.length;) {
|
|
124
|
+
const low = Math.max(a[i], b[j]);
|
|
125
|
+
const end = Math.min(a[i] + a[i + 1], b[j] + b[j + 1]);
|
|
126
|
+
if (end > low)
|
|
127
|
+
out.push(low, end - low);
|
|
128
|
+
if (a[i] + a[i + 1] < b[j] + b[j + 1])
|
|
129
|
+
i += 2;
|
|
130
|
+
else
|
|
131
|
+
j += 2;
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
function subtractRange(runs, low, end) {
|
|
136
|
+
const out = [];
|
|
137
|
+
for (let i = 0; i < runs.length; i += 2) {
|
|
138
|
+
const from = runs[i];
|
|
139
|
+
const to = from + runs[i + 1];
|
|
140
|
+
if (to <= low || from >= end)
|
|
141
|
+
out.push(from, to - from);
|
|
142
|
+
else {
|
|
143
|
+
if (from < low)
|
|
144
|
+
out.push(from, low - from);
|
|
145
|
+
if (to > end)
|
|
146
|
+
out.push(end, to - end);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Windows from the read and write sets of tracked iterations that completed without a conflict.
|
|
153
|
+
* The strided one follows the largest read run of each iteration when the runs line up on one
|
|
154
|
+
* stride; when their lengths differ by more than a granule the window is the shortest one. The
|
|
155
|
+
* fixed one is the largest run every iteration read that is not inside the strided one. A window
|
|
156
|
+
* that another iteration wrote into, in the sample, is dropped: its reads would conflict. Needs
|
|
157
|
+
* three completed iterations.
|
|
158
|
+
*/
|
|
159
|
+
export function learnReadWindows(samples) {
|
|
160
|
+
const done = samples.filter((s) => s.status === 0).sort((a, b) => a.index - b.index);
|
|
161
|
+
if (done.length < 3)
|
|
162
|
+
return { reason: 'fewer than three completed iterations to learn from' };
|
|
163
|
+
const windows = {};
|
|
164
|
+
const why = [];
|
|
165
|
+
const runsOf = (s) => Array.from(s.reads);
|
|
166
|
+
const largest = done.map((s) => {
|
|
167
|
+
let start = -1, length = 0;
|
|
168
|
+
for (let i = 0; i < s.reads.length; i += 2)
|
|
169
|
+
if (s.reads[i + 1] > length) {
|
|
170
|
+
start = s.reads[i];
|
|
171
|
+
length = s.reads[i + 1];
|
|
172
|
+
}
|
|
173
|
+
return { start, length };
|
|
174
|
+
});
|
|
175
|
+
const others = (k, low, end) => done.some((s, j) => j !== k && writesTouch(s.written, low, end));
|
|
176
|
+
const lengths = largest.map((l) => l.length);
|
|
177
|
+
const maxLen = Math.max(...lengths);
|
|
178
|
+
const minLen = Math.min(...lengths);
|
|
179
|
+
if (minLen * READ < readWindowMinimum.strided)
|
|
180
|
+
why.push('no read run of 256 bytes in every iteration');
|
|
181
|
+
else {
|
|
182
|
+
// the first byte of iteration k lies in granule start[k], so each pair of iterations
|
|
183
|
+
// bounds the stride to an open interval of width 128 / distance; one value fits all
|
|
184
|
+
let low = -Infinity, high = Infinity;
|
|
185
|
+
for (let i = 0; i < done.length; i++)
|
|
186
|
+
for (let j = i + 1; j < done.length; j++) {
|
|
187
|
+
const span = done[j].index - done[i].index;
|
|
188
|
+
const delta = (largest[j].start - largest[i].start) * READ;
|
|
189
|
+
low = Math.max(low, (delta - READ) / span);
|
|
190
|
+
high = Math.min(high, (delta + READ) / span);
|
|
191
|
+
}
|
|
192
|
+
if (!(low < high))
|
|
193
|
+
why.push('the largest read runs do not follow one stride');
|
|
194
|
+
else {
|
|
195
|
+
const stride = pickStride(low, high);
|
|
196
|
+
const first = done[0].index;
|
|
197
|
+
let origin = -Infinity;
|
|
198
|
+
done.forEach((o, k) => {
|
|
199
|
+
origin = Math.max(origin, largest[k].start * READ - (o.index - first) * stride);
|
|
200
|
+
});
|
|
201
|
+
// off the stride's own granules a prediction can fall one granule either way
|
|
202
|
+
const ragged = maxLen - minLen > 1;
|
|
203
|
+
const pad = (stride / READ) % 1 === 0 && (ragged || maxLen === minLen) ? 0 : 1;
|
|
204
|
+
const strided = {
|
|
205
|
+
index: first,
|
|
206
|
+
low: origin - pad * READ,
|
|
207
|
+
stride,
|
|
208
|
+
// runs of unequal length (a compressed stream) share their shortest prefix
|
|
209
|
+
bytes: ((ragged ? minLen : maxLen) + 2 * pad) * READ
|
|
210
|
+
};
|
|
211
|
+
const at = (index) => READ * Math.floor((strided.low + (index - strided.index) * strided.stride) / READ);
|
|
212
|
+
const clash = done.some((o, k) => others(k, at(o.index), at(o.index) + strided.bytes));
|
|
213
|
+
if (clash)
|
|
214
|
+
why.push('another iteration wrote inside the strided window');
|
|
215
|
+
else
|
|
216
|
+
windows.strided = strided;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
let common = runsOf(done[0]);
|
|
220
|
+
for (let k = 1; k < done.length; k++)
|
|
221
|
+
common = intersectRuns(common, runsOf(done[k]));
|
|
222
|
+
if (windows.strided) {
|
|
223
|
+
const s = windows.strided;
|
|
224
|
+
done.forEach((o) => {
|
|
225
|
+
const low = READ * Math.floor((s.low + (o.index - s.index) * s.stride) / READ);
|
|
226
|
+
common = subtractRange(common, low / READ, (low + s.bytes) / READ);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
let best = -1;
|
|
230
|
+
for (let i = 0; i < common.length; i += 2)
|
|
231
|
+
if (common[i + 1] * READ >= readWindowMinimum.fixed &&
|
|
232
|
+
(best < 0 || common[i + 1] > common[best + 1]))
|
|
233
|
+
best = i;
|
|
234
|
+
if (best >= 0) {
|
|
235
|
+
const low = common[best] * READ;
|
|
236
|
+
const bytes = common[best + 1] * READ;
|
|
237
|
+
if (done.some((s) => writesTouch(s.written, low, low + bytes)))
|
|
238
|
+
why.push('an iteration wrote inside the fixed window');
|
|
239
|
+
else
|
|
240
|
+
windows.fixed = { low, bytes };
|
|
241
|
+
}
|
|
242
|
+
if (!windows.strided && !windows.fixed)
|
|
243
|
+
return { reason: why.join('; ') || 'no read window found' };
|
|
244
|
+
return { windows };
|
|
245
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
export { prepare } from './compiler.ts';
|
|
2
|
-
export type { LoopPlan, PreparedProgram } from './compiler.ts';
|
|
3
|
-
export
|
|
4
|
-
export
|
|
1
|
+
export { prepare, prepareContext } from './execution/compiler.ts';
|
|
2
|
+
export type { LoopPlan, PrepareOptions, PreparedContext, PreparedProgram } from './execution/compiler.ts';
|
|
3
|
+
export * from './execution/fold.ts';
|
|
4
|
+
export * from './folds/affine.ts';
|
|
5
|
+
export * from './folds/certified.ts';
|
|
6
|
+
export * from './folds/certify.ts';
|
|
7
|
+
export * from './node/context.ts';
|
|
8
|
+
export * from './node/host.ts';
|
|
9
|
+
export { Folio, suspending } from './node/index.ts';
|
|
10
|
+
export type { Call, ExecutionProfile, ExecutionReport, FolioOptions, ProgramInput, RunOptions } from './node/index.ts';
|
|
11
|
+
export * from './node/tasks.ts';
|
|
12
|
+
export * from './ownership/index.ts';
|
|
13
|
+
export * from './ownership/redirect.ts';
|
|
14
|
+
export * from './ownership/slices.ts';
|
|
15
|
+
export * from './routing/index.ts';
|
|
16
|
+
export * from './slices/index.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,14 @@
|
|
|
1
|
-
export { prepare } from "./compiler.js";
|
|
2
|
-
export
|
|
1
|
+
export { prepare, prepareContext } from "./execution/compiler.js";
|
|
2
|
+
export * from "./execution/fold.js";
|
|
3
|
+
export * from "./folds/affine.js";
|
|
4
|
+
export * from "./folds/certified.js";
|
|
5
|
+
export * from "./folds/certify.js";
|
|
6
|
+
export * from "./node/context.js";
|
|
7
|
+
export * from "./node/host.js";
|
|
8
|
+
export { Folio, suspending } from "./node/index.js";
|
|
9
|
+
export * from "./node/tasks.js";
|
|
10
|
+
export * from "./ownership/index.js";
|
|
11
|
+
export * from "./ownership/redirect.js";
|
|
12
|
+
export * from "./ownership/slices.js";
|
|
13
|
+
export * from "./routing/index.js";
|
|
14
|
+
export * from "./slices/index.js";
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { AdmissionStep, IterationAdmission, IterationStop } from '../execution/types.ts';
|
|
2
|
+
export type { AdmissionStep, IterationAdmission, IterationStop };
|
|
3
|
+
/** The constants of the decision. A re-calibration changes these and nothing else. */
|
|
4
|
+
export interface IterationAdmissionConstants {
|
|
5
|
+
/** Wall of the pooled path that does not shrink with the loop: dispatch, join, validation. */
|
|
6
|
+
fixedMs: number;
|
|
7
|
+
/** The pooled path has to beat the serial one by this factor, before and after each batch. */
|
|
8
|
+
margin: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Fitted on a Mac sweep of `scripts/research/iteration-admission.ts` and checked on the rig at widths
|
|
12
|
+
* 8 and 4; a re-calibration updates only these two numbers.
|
|
13
|
+
*
|
|
14
|
+
* `fixedMs` 2 is the smallest value at which no cell of the Mac sweep (18 cells, 4 KiB to 4 MiB
|
|
15
|
+
* by 8 to 128 iterations, width 8) dispatched and lost. The pooled wall minus the batch time
|
|
16
|
+
* measured 0.1 to 2.9 ms there with the input copy left out of the timing; the prototype's rows
|
|
17
|
+
* bracket 3 to 8 ms but include that copy. The loop is not worth dispatching when the serial time
|
|
18
|
+
* of its remaining iterations is below `fixedMs * margin`.
|
|
19
|
+
*
|
|
20
|
+
* `margin` 1.25 is the speedup a batch has to show over the serial time of its iterations for
|
|
21
|
+
* the loop to keep dispatching. A batch that misses it twice in a row ends the dispatch, and so
|
|
22
|
+
* does one batch whose wall passes `batchCollapse` times that serial time.
|
|
23
|
+
*/
|
|
24
|
+
export declare const iterationAdmissionConstants: IterationAdmissionConstants;
|
|
25
|
+
/**
|
|
26
|
+
* With the second chance on, a loop whose remaining serial time is under the floor but over this
|
|
27
|
+
* fraction of it is admitted as a probe: its first timed batch has to beat the serial time of its
|
|
28
|
+
* iterations by the margin, or the dispatch ends there. The floor was fitted on a build with the
|
|
29
|
+
* full read scan and no read windows, so it is the least certain number in the decision.
|
|
30
|
+
*/
|
|
31
|
+
export declare const probeFraction = 0.5;
|
|
32
|
+
export interface AdmitInput {
|
|
33
|
+
plainIterationMs: number;
|
|
34
|
+
trip: number;
|
|
35
|
+
width: number;
|
|
36
|
+
constants?: IterationAdmissionConstants;
|
|
37
|
+
secondChance?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Decides before the first dispatch from the measured serial time of iteration 0, which has
|
|
41
|
+
* already run. Admits when at least two iterations are left for at least two workers and their
|
|
42
|
+
* serial time exceeds `fixedMs * margin`. Whether dispatch pays is then checked per batch.
|
|
43
|
+
*/
|
|
44
|
+
export declare function admitIteration({ plainIterationMs: t0, trip, width, constants, secondChance }: AdmitInput): IterationAdmission;
|
|
45
|
+
/** True when a batch did not beat the serial time of its iterations by the margin. */
|
|
46
|
+
export declare function batchLoses(batchWallMs: number, batchSerialMs: number, constants?: IterationAdmissionConstants): boolean;
|
|
47
|
+
/** Batch wall over serial time past which one batch is a verdict, with no second batch to confirm. */
|
|
48
|
+
export declare const batchCollapse = 1.5;
|
|
49
|
+
/** True when a batch took clearly longer than the serial time of its iterations. */
|
|
50
|
+
export declare function batchCollapses(batchWallMs: number, batchSerialMs: number): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Test and sweep seam, module state and not an option. `scale` multiplies the measured time of
|
|
53
|
+
* iteration 0 before the first decision and `batchScale` the wall of each batch before the batch
|
|
54
|
+
* rule, and divides the bound of a batch; `force` admits any loop with an iteration left after
|
|
55
|
+
* iteration 0 and never stops on the batch rule or the bound. The structural stops
|
|
56
|
+
* (`log-exhausted`, `frame-carried dependence`, `dependency`) are not affected. `trace` records
|
|
57
|
+
* one entry per batch in the admission report. `gate` false keeps read windows armed whatever the
|
|
58
|
+
* counted batch says, so a sweep can compare the two.
|
|
59
|
+
* @internal
|
|
60
|
+
*/
|
|
61
|
+
export declare const admissionSeam: {
|
|
62
|
+
scale: number;
|
|
63
|
+
batchScale: number;
|
|
64
|
+
force: boolean;
|
|
65
|
+
trace: boolean;
|
|
66
|
+
gate: boolean;
|
|
67
|
+
estimator: "min" | "median" | "mean" | "q75";
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* The serial estimate the probe holds after a block: the lower of the last two block means, the
|
|
71
|
+
* median of every block mean so far, or the mean of every probed iteration.
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
export declare function probeEstimate(kind: 'min' | 'median' | 'mean' | 'q75', last: number, per: number, means: number[], mean: number): number;
|
|
75
|
+
/** `admitIteration` through the seam; what the host function calls. */
|
|
76
|
+
export declare function decideAdmission(plainIterationMs: number, trip: number, width: number, secondChance?: boolean): IterationAdmission;
|
|
77
|
+
/** The serial time per iteration as far as the run knows it, and the scale from tracked time. */
|
|
78
|
+
export interface IterationEstimate {
|
|
79
|
+
/** Serial milliseconds per iteration. */
|
|
80
|
+
estimateMs: number;
|
|
81
|
+
/** Serial time over tracked time, fixed by the first batch; undefined before it. */
|
|
82
|
+
kappa?: number;
|
|
83
|
+
/** The same scale for batches that run on read windows; fixed by the first of them. */
|
|
84
|
+
kappaWindows?: number;
|
|
85
|
+
/** The serial time both scales are fitted to: the estimate before the first batch. */
|
|
86
|
+
referenceMs?: number;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Updates the estimate from the worker-side times of the iterations a batch completed, with no
|
|
90
|
+
* host call per iteration. The first batch fixes `kappa` as the serial estimate over the least
|
|
91
|
+
* tracked time, at most 1 because a tracked iteration is never faster than a plain one. The least
|
|
92
|
+
* time, not the median, because the iteration that the estimate came from may be the smallest of
|
|
93
|
+
* a batch that is mostly large, and the median would then read its size as the tracking cost. The
|
|
94
|
+
* estimate is then the mean tracked time times `kappa`: a batch of iterations dearer than the
|
|
95
|
+
* earlier estimate raises it, and a batch of cheaper ones lowers it. When the first iteration was
|
|
96
|
+
* larger than every iteration of the batch, `kappa` is capped and the estimate is the tracked
|
|
97
|
+
* mean, an upper bound on the serial time. Times of zero or less are ignored. A batch on read
|
|
98
|
+
* windows is cheaper to track than the first, so it has its own scale, fitted the same way to the
|
|
99
|
+
* same reference the first time one runs; the unwindowed scale would read its time as serial time.
|
|
100
|
+
*/
|
|
101
|
+
export declare function refineEstimate(before: IterationEstimate, trackedMs: readonly number[], windows?: boolean): IterationEstimate;
|
|
102
|
+
/**
|
|
103
|
+
* Whether `remaining` more iterations at `perIterationMs` each are worth dispatching, through the
|
|
104
|
+
* seam. The pool asks it with the online estimate before each batch after the first and after the
|
|
105
|
+
* timed serial blocks of a refused loop.
|
|
106
|
+
*
|
|
107
|
+
* With `last`, the wall and size of the last timed batch, a loop under the floor still goes on
|
|
108
|
+
* when the batches it has seen say so: the pooled cost of what is left is the batch wall times the
|
|
109
|
+
* batches that cover it, and that times the margin has to be under their serial time. The floor
|
|
110
|
+
* prices the wake-up and the learning batch, which a loop that is running has already paid.
|
|
111
|
+
*/
|
|
112
|
+
export declare function decideRemaining(perIterationMs: number, remaining: number, width: number, last?: {
|
|
113
|
+
wallMs: number;
|
|
114
|
+
count: number;
|
|
115
|
+
}): boolean;
|
|
116
|
+
/** `batchLoses` through the seam; what the host function passes the pool. */
|
|
117
|
+
export declare function decideBatch(batchWallMs: number, batchSerialMs: number): boolean;
|
|
118
|
+
/** `batchCollapses` through the seam; what the host function passes the pool. */
|
|
119
|
+
export declare function decideCollapse(batchWallMs: number, batchSerialMs: number): boolean;
|
|
120
|
+
/** Speedup below which a batch cannot pay even with free dispatch, validation and joins. */
|
|
121
|
+
export declare const boundFloor = 1;
|
|
122
|
+
/**
|
|
123
|
+
* The best speedup a batch could reach: the serial time of the iterations it completed over the
|
|
124
|
+
* time `width` workers need for them at the tracked time they showed. Wake, join and validation
|
|
125
|
+
* only add to the wall, so a bound under one means the loop cannot win at this tracking cost.
|
|
126
|
+
*/
|
|
127
|
+
export declare function batchBound(perIterationMs: number, trackedMs: readonly number[], width: number): number;
|
|
128
|
+
/**
|
|
129
|
+
* A loop whose first batch bounds the speedup above this has tracking cost to spare: realized
|
|
130
|
+
* speedups ran at about half the bound (aes-ctr 1.2 of 2.3, lz4-compress 1.2 of 2.4,
|
|
131
|
+
* aes-cbc-decrypt 1.2 of 2.0 on the Mac sweep), so such a loop goes on to full batches on its
|
|
132
|
+
* read windows. A loop under it gets one short batch on them first.
|
|
133
|
+
*/
|
|
134
|
+
export declare const proofBound = 2;
|
|
135
|
+
/** True when the bound of a batch is under `boundFloor`, through the seam; what the host passes. */
|
|
136
|
+
export declare function decideBound(bound: number): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* The wall and the serial time of the next batch of `next` iterations, from a batch that ran. Its
|
|
139
|
+
* wall minus the time its workers needed for the iterations it completed is the cost that does
|
|
140
|
+
* not shrink with the batch: wake, join and validation. The next batch pays that plus the
|
|
141
|
+
* workers' share of its own iterations. Validation grows with the batch, so the wall comes out
|
|
142
|
+
* low and a projected loss is a loss.
|
|
143
|
+
*/
|
|
144
|
+
export declare function projectBatch(wallMs: number, trackedMs: readonly number[], perIterationMs: number, next: number, width: number): {
|
|
145
|
+
wallMs: number;
|
|
146
|
+
serialMs: number;
|
|
147
|
+
} | undefined;
|
|
148
|
+
/** True when a bound is low enough that the first batch on read windows should be a short one. */
|
|
149
|
+
export declare function decideProof(bound: number): boolean;
|