@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
package/dist/iteration.d.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import type { IterationRoute } from './extract.ts';
|
|
2
|
-
import type { IterationAdmission, IterationStop } from './iteration-admission.ts';
|
|
3
|
-
import { type IterationArena, type IterationArenaOptions } from './iteration-layout.ts';
|
|
4
|
-
export { iterationArena, iterationArenaBytes, iterationArguments, iterationEntryBytes, iterationFit, iterationFrameShadow, iterationGranules } from './iteration-layout.ts';
|
|
5
|
-
export type { IterationArena, IterationArenaOptions } from './iteration-layout.ts';
|
|
6
|
-
/** Pool construction options. */
|
|
7
|
-
export interface IterationPoolOptions extends IterationArenaOptions {
|
|
8
|
-
/** Start of the arena, 64-byte aligned, in memory the caller keeps free while the pool is open. */
|
|
9
|
-
arenaBase: number;
|
|
10
|
-
/** Workers; defaults to up to four. */
|
|
11
|
-
width?: number;
|
|
12
|
-
}
|
|
13
|
-
/** One owner call's loop. */
|
|
14
|
-
export interface IterationJob {
|
|
15
|
-
/** Iterations to run. An entry value of zero runs 2^32 times in the program; refuse that upstream. */
|
|
16
|
-
trip: number;
|
|
17
|
-
/** The owner's frame local after its prologue; the image at `[frameBase, frameBase + size)` is copied. */
|
|
18
|
-
frameBase: number;
|
|
19
|
-
/** Loop-entry value of every local in `route.locals` that has an `argument`, except the frame. */
|
|
20
|
-
entry: Readonly<Record<number, number | bigint>>;
|
|
21
|
-
/** Runs iteration `k` serially on the owner frame. A throw is the program's trap. */
|
|
22
|
-
replay(k: number): void;
|
|
23
|
-
/** Test hook: `reverse` makes one worker run a batch last to first. Default `forward`. */
|
|
24
|
-
schedule?: 'forward' | 'reverse';
|
|
25
|
-
/** Test hook, unsound: validation checks to leave out. */
|
|
26
|
-
skip?: readonly ('raw' | 'war' | 'waw' | 'frame')[];
|
|
27
|
-
/**
|
|
28
|
-
* Runs iteration 0 through `replay` on the owner thread first and times it. That is the serial
|
|
29
|
-
* program's first iteration, so its writes stay and the batches cover iterations 1 to `trip - 1`
|
|
30
|
-
* from the owner frame it leaves. Called once with the time of that iteration, before anything
|
|
31
|
-
* is dispatched. A refusal runs the other iterations through `replay` and dispatches nothing.
|
|
32
|
-
* Without `admit` every iteration is dispatched.
|
|
33
|
-
*/
|
|
34
|
-
admit?: (plainIterationMs: number) => IterationAdmission;
|
|
35
|
-
/**
|
|
36
|
-
* Called after each batch that leaves iterations behind, with its wall (dispatch, validation,
|
|
37
|
-
* rollback and replay) and the serial time of its iterations at the time of iteration 0. True
|
|
38
|
-
* ends the dispatch and runs the rest through `replay`. Only used with `admit`.
|
|
39
|
-
*/
|
|
40
|
-
loses?: (batchWallMs: number, batchSerialMs: number) => boolean;
|
|
41
|
-
/** Like `loses`, for a batch slow enough that a second batch is not needed to confirm it. */
|
|
42
|
-
collapses?: (batchWallMs: number, batchSerialMs: number) => boolean;
|
|
43
|
-
}
|
|
44
|
-
/** What a run did. `regions`, `replayed`, `fallbacks` and `workers` parallel `ExecutionReport`. */
|
|
45
|
-
export interface IterationReport {
|
|
46
|
-
/** Workers that took part. */
|
|
47
|
-
workers: number;
|
|
48
|
-
/** Iterations run on workers, the regions of this mode. */
|
|
49
|
-
regions: number;
|
|
50
|
-
/** Dispatched iterations that were rolled back and rerun serially at the owner. */
|
|
51
|
-
replayed: number;
|
|
52
|
-
/** Iterations whose writes were undone before the rerun. */
|
|
53
|
-
rolledBack: number;
|
|
54
|
-
/** Batches whose first failure was a trap or an exhausted undo log, not a dependency. */
|
|
55
|
-
fallbacks: number;
|
|
56
|
-
/** Iterations that trapped on a worker. */
|
|
57
|
-
traps: number;
|
|
58
|
-
/** Iterations that exhausted their undo log. */
|
|
59
|
-
exhausted: number;
|
|
60
|
-
batches: number;
|
|
61
|
-
failedBatches: number;
|
|
62
|
-
undoEntries: number;
|
|
63
|
-
/** Ordered iteration pairs by hazard. */
|
|
64
|
-
conflicts: {
|
|
65
|
-
raw: number;
|
|
66
|
-
war: number;
|
|
67
|
-
waw: number;
|
|
68
|
-
frame: number;
|
|
69
|
-
};
|
|
70
|
-
/** From posting a batch to the last worker reply. */
|
|
71
|
-
dispatchMs: number;
|
|
72
|
-
validationMs: number;
|
|
73
|
-
rollbackMs: number;
|
|
74
|
-
replayMs: number;
|
|
75
|
-
/** Wasm time inside workers, summed. */
|
|
76
|
-
workerRunMs: number;
|
|
77
|
-
/** Bitmap scan and clear time inside workers, summed. */
|
|
78
|
-
workerScanMs: number;
|
|
79
|
-
/**
|
|
80
|
-
* Iterations the owner ran as the serial program without dispatching them: iteration 0, the
|
|
81
|
-
* rest of a loop after a stop, or all of them when admission refused the loop.
|
|
82
|
-
*/
|
|
83
|
-
serial: number;
|
|
84
|
-
/** What ended the dispatch early, or why admission refused the loop. */
|
|
85
|
-
stopped?: IterationStop;
|
|
86
|
-
/** What the admission callback saw and chose; set when the job had one. */
|
|
87
|
-
admission?: IterationAdmission;
|
|
88
|
-
}
|
|
89
|
-
/** What a worker returns for one iteration. */
|
|
90
|
-
export interface IterationOutcome {
|
|
91
|
-
index: number;
|
|
92
|
-
/** 0 ran, 1 trapped, 2 undo log exhausted */
|
|
93
|
-
status: number;
|
|
94
|
-
entries: number;
|
|
95
|
-
/** sorted write granules (32 bytes) from the undo log */
|
|
96
|
-
written: Int32Array;
|
|
97
|
-
/** [start, length] runs of read granules (64 bytes) */
|
|
98
|
-
reads: Int32Array;
|
|
99
|
-
/** written bit per frame byte, one i32 per 32-byte frame granule; empty when nothing was written */
|
|
100
|
-
frameWritten: Uint32Array;
|
|
101
|
-
/** bit per frame byte the iteration read before writing it, same layout; empty when none */
|
|
102
|
-
frameExposed: Uint32Array;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Index of the first outcome that cannot keep its writes, or -1. Writes land in place, so every
|
|
106
|
-
* ordered pair is checked three ways: RAW marks the reader, WAR the earlier reader, WAW the earlier
|
|
107
|
-
* writer. A fourth check covers the frame, which each iteration owns as a private copy: an
|
|
108
|
-
* iteration fails when it read a frame byte before writing it that an earlier iteration wrote, and
|
|
109
|
-
* counts once. Works on the returned lists, never on dense bitmaps.
|
|
110
|
-
*/
|
|
111
|
-
export declare function iterationVerdict(outcomes: readonly (Pick<IterationOutcome, 'status' | 'written' | 'reads'> & Partial<Pick<IterationOutcome, 'frameWritten' | 'frameExposed'>>)[], skip?: readonly ('raw' | 'war' | 'waw' | 'frame')[]): {
|
|
112
|
-
first: number;
|
|
113
|
-
firstFailure: number;
|
|
114
|
-
frameFirst: number;
|
|
115
|
-
conflicts: {
|
|
116
|
-
raw: number;
|
|
117
|
-
war: number;
|
|
118
|
-
waw: number;
|
|
119
|
-
frame: number;
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
/**
|
|
123
|
-
* Runs the loop of one owner call on worker threads. Each iteration gets a private stack slot and
|
|
124
|
-
* tracks its reads and writes inline. The owner validates a batch in order, rolls back the first
|
|
125
|
-
* failing iteration and every later one from the undo logs, and replays them through `replay`.
|
|
126
|
-
* The arena belongs to the pool until `close`.
|
|
127
|
-
*
|
|
128
|
-
* Each iteration runs on a private copy of the owner frame, taken from the owner frame when its
|
|
129
|
-
* batch is dispatched. The lowered module tracks the frame: a written bit per frame byte, and an
|
|
130
|
-
* exposed bit per 32-byte frame granule that the iteration read before it wrote the byte. An
|
|
131
|
-
* iteration fails validation, like a RAW pair, when it has an exposed granule that an earlier
|
|
132
|
-
* iteration of the same batch wrote (`conflicts.frame`); it and every later one in the batch roll
|
|
133
|
-
* back and replay. After a batch validates, each kept
|
|
134
|
-
* iteration's written frame bytes are copied into the owner frame in order, so the next batch
|
|
135
|
-
* starts from the serial frame and a replay runs on it. The guarantee: a read of an owner frame
|
|
136
|
-
* byte that the same iteration has not written yet, with a write by an earlier iteration of the
|
|
137
|
-
* batch to that byte, is detected, and an earlier batch's writes are seen through the copy.
|
|
138
|
-
* Excluded: a read of stack below the frame (callee frames, indeterminate before the callee writes
|
|
139
|
-
* them) is not tracked, and imported host functions that touch memory are not seen. The shadow
|
|
140
|
-
* is byte-exact for scalar accesses and ranges; a conservative read would only add reports.
|
|
141
|
-
*
|
|
142
|
-
* With `admit`, iteration 0 runs first on the owner and the batches cover the rest. Dispatch
|
|
143
|
-
* stops after a batch that exhausted an undo log, had a frame conflict, replayed more than half of
|
|
144
|
-
* its iterations, or (with `loses` and `collapses`) two batches in a row did not beat the serial
|
|
145
|
-
* time of their iterations or one took far longer than it; the rest of the loop runs through
|
|
146
|
-
* `replay` and `report.stopped` names the reason. Once an iteration fails, the workers start no
|
|
147
|
-
* further iteration of that batch, because it and every later one replay anyway.
|
|
148
|
-
*/
|
|
149
|
-
export declare class IterationPool {
|
|
150
|
-
#private;
|
|
151
|
-
readonly arena: IterationArena;
|
|
152
|
-
readonly width: number;
|
|
153
|
-
private constructor();
|
|
154
|
-
/** The report of the last run, also after a run that threw. */
|
|
155
|
-
get report(): IterationReport;
|
|
156
|
-
/** Lowers the route's worker module inline and starts `width` workers over `memory`. */
|
|
157
|
-
static open(route: IterationRoute, memory: WebAssembly.Memory, options: IterationPoolOptions): Promise<IterationPool>;
|
|
158
|
-
/** Runs the loop to completion with ordered validation. Blocks the calling thread. */
|
|
159
|
-
run(job: IterationJob): IterationReport;
|
|
160
|
-
/** Stops the workers. Safe to call twice. */
|
|
161
|
-
close(): Promise<void>;
|
|
162
|
-
[Symbol.asyncDispose](): Promise<void>;
|
|
163
|
-
}
|
package/dist/iteration.js
DELETED
|
@@ -1,435 +0,0 @@
|
|
|
1
|
-
import { availableParallelism } from 'node:os';
|
|
2
|
-
import { isSharedArrayBuffer } from 'node:util/types';
|
|
3
|
-
import { MessageChannel, Worker, receiveMessageOnPort } from 'node:worker_threads';
|
|
4
|
-
import { inlineGlobals, lowerInline } from "./inline.js";
|
|
5
|
-
import { iterationArena, iterationEntryBytes, iterationGranules } from "./iteration-layout.js";
|
|
6
|
-
export { iterationArena, iterationArenaBytes, iterationArguments, iterationEntryBytes, iterationFit, iterationFrameShadow, iterationGranules } from "./iteration-layout.js";
|
|
7
|
-
const newReport = (workers) => ({
|
|
8
|
-
workers,
|
|
9
|
-
regions: 0,
|
|
10
|
-
replayed: 0,
|
|
11
|
-
rolledBack: 0,
|
|
12
|
-
fallbacks: 0,
|
|
13
|
-
traps: 0,
|
|
14
|
-
exhausted: 0,
|
|
15
|
-
batches: 0,
|
|
16
|
-
failedBatches: 0,
|
|
17
|
-
undoEntries: 0,
|
|
18
|
-
conflicts: { raw: 0, war: 0, waw: 0, frame: 0 },
|
|
19
|
-
dispatchMs: 0,
|
|
20
|
-
validationMs: 0,
|
|
21
|
-
rollbackMs: 0,
|
|
22
|
-
replayMs: 0,
|
|
23
|
-
workerRunMs: 0,
|
|
24
|
-
workerScanMs: 0,
|
|
25
|
-
serial: 0
|
|
26
|
-
});
|
|
27
|
-
const shift = Math.log2(iterationGranules.read / iterationGranules.write);
|
|
28
|
-
// sorted write granules against sorted read runs
|
|
29
|
-
function hitsReads(writes, runs) {
|
|
30
|
-
let p = 0;
|
|
31
|
-
for (let i = 0; i < writes.length; i++) {
|
|
32
|
-
const g = writes[i] >> shift;
|
|
33
|
-
while (p < runs.length && runs[p] + runs[p + 1] <= g)
|
|
34
|
-
p += 2;
|
|
35
|
-
if (p >= runs.length)
|
|
36
|
-
return false;
|
|
37
|
-
if (runs[p] <= g)
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
function hitsWrites(a, b) {
|
|
43
|
-
let i = 0;
|
|
44
|
-
let j = 0;
|
|
45
|
-
while (i < a.length && j < b.length) {
|
|
46
|
-
if (a[i] === b[j])
|
|
47
|
-
return true;
|
|
48
|
-
if (a[i] < b[j])
|
|
49
|
-
i++;
|
|
50
|
-
else
|
|
51
|
-
j++;
|
|
52
|
-
}
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Index of the first outcome that cannot keep its writes, or -1. Writes land in place, so every
|
|
57
|
-
* ordered pair is checked three ways: RAW marks the reader, WAR the earlier reader, WAW the earlier
|
|
58
|
-
* writer. A fourth check covers the frame, which each iteration owns as a private copy: an
|
|
59
|
-
* iteration fails when it read a frame byte before writing it that an earlier iteration wrote, and
|
|
60
|
-
* counts once. Works on the returned lists, never on dense bitmaps.
|
|
61
|
-
*/
|
|
62
|
-
export function iterationVerdict(outcomes, skip = []) {
|
|
63
|
-
const conflicts = { raw: 0, war: 0, waw: 0, frame: 0 };
|
|
64
|
-
let frameFirst = outcomes.length;
|
|
65
|
-
let firstFailure = outcomes.length;
|
|
66
|
-
outcomes.forEach((o, k) => {
|
|
67
|
-
if (o.status)
|
|
68
|
-
firstFailure = Math.min(firstFailure, k);
|
|
69
|
-
});
|
|
70
|
-
let first = firstFailure;
|
|
71
|
-
for (let j = 0; j < outcomes.length; j++) {
|
|
72
|
-
for (let k = j + 1; k < outcomes.length; k++) {
|
|
73
|
-
const a = outcomes[j];
|
|
74
|
-
const b = outcomes[k];
|
|
75
|
-
if (!skip.includes('raw') && hitsReads(a.written, b.reads)) {
|
|
76
|
-
conflicts.raw++;
|
|
77
|
-
first = Math.min(first, k);
|
|
78
|
-
}
|
|
79
|
-
if (!skip.includes('war') && hitsReads(b.written, a.reads)) {
|
|
80
|
-
conflicts.war++;
|
|
81
|
-
first = Math.min(first, j);
|
|
82
|
-
}
|
|
83
|
-
if (!skip.includes('waw') && hitsWrites(a.written, b.written)) {
|
|
84
|
-
conflicts.waw++;
|
|
85
|
-
first = Math.min(first, j);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
if (!skip.includes('frame')) {
|
|
90
|
-
// the frame bytes written by the iterations before k, against the bytes k read unwritten
|
|
91
|
-
let union;
|
|
92
|
-
outcomes.forEach((o, k) => {
|
|
93
|
-
const exposed = o.frameExposed;
|
|
94
|
-
if (union && exposed?.length && exposed.some((word, w) => word & (union[w] ?? 0))) {
|
|
95
|
-
conflicts.frame++;
|
|
96
|
-
first = Math.min(first, k);
|
|
97
|
-
frameFirst = Math.min(frameFirst, k);
|
|
98
|
-
}
|
|
99
|
-
const written = o.frameWritten;
|
|
100
|
-
if (!written?.length)
|
|
101
|
-
return;
|
|
102
|
-
union ??= new Uint32Array(written.length);
|
|
103
|
-
written.forEach((word, w) => (union[w] |= word));
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
const none = outcomes.length;
|
|
107
|
-
return {
|
|
108
|
-
first: first === none ? -1 : first,
|
|
109
|
-
firstFailure: firstFailure === none ? -1 : firstFailure,
|
|
110
|
-
frameFirst: frameFirst === none ? -1 : frameFirst,
|
|
111
|
-
conflicts
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Runs the loop of one owner call on worker threads. Each iteration gets a private stack slot and
|
|
116
|
-
* tracks its reads and writes inline. The owner validates a batch in order, rolls back the first
|
|
117
|
-
* failing iteration and every later one from the undo logs, and replays them through `replay`.
|
|
118
|
-
* The arena belongs to the pool until `close`.
|
|
119
|
-
*
|
|
120
|
-
* Each iteration runs on a private copy of the owner frame, taken from the owner frame when its
|
|
121
|
-
* batch is dispatched. The lowered module tracks the frame: a written bit per frame byte, and an
|
|
122
|
-
* exposed bit per 32-byte frame granule that the iteration read before it wrote the byte. An
|
|
123
|
-
* iteration fails validation, like a RAW pair, when it has an exposed granule that an earlier
|
|
124
|
-
* iteration of the same batch wrote (`conflicts.frame`); it and every later one in the batch roll
|
|
125
|
-
* back and replay. After a batch validates, each kept
|
|
126
|
-
* iteration's written frame bytes are copied into the owner frame in order, so the next batch
|
|
127
|
-
* starts from the serial frame and a replay runs on it. The guarantee: a read of an owner frame
|
|
128
|
-
* byte that the same iteration has not written yet, with a write by an earlier iteration of the
|
|
129
|
-
* batch to that byte, is detected, and an earlier batch's writes are seen through the copy.
|
|
130
|
-
* Excluded: a read of stack below the frame (callee frames, indeterminate before the callee writes
|
|
131
|
-
* them) is not tracked, and imported host functions that touch memory are not seen. The shadow
|
|
132
|
-
* is byte-exact for scalar accesses and ranges; a conservative read would only add reports.
|
|
133
|
-
*
|
|
134
|
-
* With `admit`, iteration 0 runs first on the owner and the batches cover the rest. Dispatch
|
|
135
|
-
* stops after a batch that exhausted an undo log, had a frame conflict, replayed more than half of
|
|
136
|
-
* its iterations, or (with `loses` and `collapses`) two batches in a row did not beat the serial
|
|
137
|
-
* time of their iterations or one took far longer than it; the rest of the loop runs through
|
|
138
|
-
* `replay` and `report.stopped` names the reason. Once an iteration fails, the workers start no
|
|
139
|
-
* further iteration of that batch, because it and every later one replay anyway.
|
|
140
|
-
*/
|
|
141
|
-
export class IterationPool {
|
|
142
|
-
arena;
|
|
143
|
-
width;
|
|
144
|
-
#report;
|
|
145
|
-
#memory;
|
|
146
|
-
#links = [];
|
|
147
|
-
#control = new Int32Array(new SharedArrayBuffer(4));
|
|
148
|
-
#u8;
|
|
149
|
-
#u32;
|
|
150
|
-
#closed = false;
|
|
151
|
-
constructor(memory, arena, width) {
|
|
152
|
-
this.#memory = memory;
|
|
153
|
-
this.arena = arena;
|
|
154
|
-
this.width = width;
|
|
155
|
-
this.#report = newReport(0);
|
|
156
|
-
this.#u8 = new Uint8Array(memory.buffer);
|
|
157
|
-
this.#u32 = new Uint32Array(memory.buffer);
|
|
158
|
-
}
|
|
159
|
-
/** The report of the last run, also after a run that threw. */
|
|
160
|
-
get report() {
|
|
161
|
-
return this.#report;
|
|
162
|
-
}
|
|
163
|
-
/** Lowers the route's worker module inline and starts `width` workers over `memory`. */
|
|
164
|
-
static async open(route, memory, options) {
|
|
165
|
-
if (!route.eligible || !route.module || !route.iterate)
|
|
166
|
-
throw new TypeError('iteration route is not eligible');
|
|
167
|
-
const memoryBytes = memory.buffer.byteLength;
|
|
168
|
-
if (!isSharedArrayBuffer(memory.buffer))
|
|
169
|
-
throw new TypeError('iteration pool requires shared memory');
|
|
170
|
-
const width = options.width ?? Math.min(4, availableParallelism());
|
|
171
|
-
if (!Number.isInteger(width) || width < 1 || width > 64)
|
|
172
|
-
throw new RangeError('invalid worker count');
|
|
173
|
-
const frameSize = route.frame?.size ?? 0;
|
|
174
|
-
const arena = iterationArena(options.arenaBase, memoryBytes, {
|
|
175
|
-
...options,
|
|
176
|
-
frameBytes: frameSize
|
|
177
|
-
});
|
|
178
|
-
if (arena.base + arena.bytes > arena.memoryBytes)
|
|
179
|
-
throw new RangeError('arena does not fit in memory');
|
|
180
|
-
if (frameSize >= arena.slotBytes)
|
|
181
|
-
throw new RangeError('slot is smaller than the frame');
|
|
182
|
-
const lowered = lowerInline(route.module, {
|
|
183
|
-
readGranule: iterationGranules.read,
|
|
184
|
-
writeGranule: iterationGranules.write,
|
|
185
|
-
frame: frameSize > 0
|
|
186
|
-
});
|
|
187
|
-
const module = new WebAssembly.Module(Uint8Array.from(lowered));
|
|
188
|
-
const pool = new IterationPool(memory, arena, width);
|
|
189
|
-
pool.#u8.fill(0, arena.stamp, arena.stamp + 64);
|
|
190
|
-
pool.#u8.fill(0, arena.rmaps, arena.wmaps + arena.batch * arena.wsize);
|
|
191
|
-
const extension = import.meta.url.endsWith('.ts') ? 'ts' : 'js';
|
|
192
|
-
try {
|
|
193
|
-
await Promise.all(Array.from({ length: width }, () => {
|
|
194
|
-
const channel = new MessageChannel();
|
|
195
|
-
const signal = new SharedArrayBuffer(4);
|
|
196
|
-
const worker = new Worker(new URL(`./iteration-worker.${extension}`, import.meta.url), {
|
|
197
|
-
workerData: {
|
|
198
|
-
module,
|
|
199
|
-
memory,
|
|
200
|
-
arena,
|
|
201
|
-
globals: inlineGlobals,
|
|
202
|
-
iterate: route.iterate,
|
|
203
|
-
locals: route.locals,
|
|
204
|
-
frameSize,
|
|
205
|
-
port: channel.port2,
|
|
206
|
-
signal,
|
|
207
|
-
control: pool.#control.buffer
|
|
208
|
-
},
|
|
209
|
-
transferList: [channel.port2]
|
|
210
|
-
});
|
|
211
|
-
pool.#links.push({
|
|
212
|
-
worker,
|
|
213
|
-
port: channel.port1,
|
|
214
|
-
signal: new Int32Array(signal)
|
|
215
|
-
});
|
|
216
|
-
return new Promise((resolve, reject) => {
|
|
217
|
-
worker.once('message', () => resolve());
|
|
218
|
-
worker.once('error', reject);
|
|
219
|
-
});
|
|
220
|
-
}));
|
|
221
|
-
}
|
|
222
|
-
catch (error) {
|
|
223
|
-
await pool.close();
|
|
224
|
-
throw error;
|
|
225
|
-
}
|
|
226
|
-
return pool;
|
|
227
|
-
}
|
|
228
|
-
// blocks the owner thread until the worker replies, the idiom of TaskPool.join
|
|
229
|
-
#join(link) {
|
|
230
|
-
for (;;) {
|
|
231
|
-
const seen = Atomics.load(link.signal, 0);
|
|
232
|
-
const message = receiveMessageOnPort(link.port);
|
|
233
|
-
if (message)
|
|
234
|
-
return message.message;
|
|
235
|
-
Atomics.wait(link.signal, 0, seen);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
/** Runs the loop to completion with ordered validation. Blocks the calling thread. */
|
|
239
|
-
run(job) {
|
|
240
|
-
if (this.#closed)
|
|
241
|
-
throw new Error('iteration pool is closed');
|
|
242
|
-
if (this.#memory.buffer.byteLength !== this.arena.memoryBytes)
|
|
243
|
-
throw new Error('memory changed size while the iteration pool was open');
|
|
244
|
-
if (!Number.isSafeInteger(job.trip) || job.trip < 0)
|
|
245
|
-
throw new RangeError('invalid trip count');
|
|
246
|
-
const A = this.arena;
|
|
247
|
-
const report = (this.#report = newReport(Math.min(this.width, Math.max(job.trip, 0))));
|
|
248
|
-
let plainMs = 0;
|
|
249
|
-
let from = 0;
|
|
250
|
-
if (job.admit && job.trip > 0) {
|
|
251
|
-
// iteration 0 is the serial program's own first iteration, so its writes stay
|
|
252
|
-
const t = performance.now();
|
|
253
|
-
report.serial = 1;
|
|
254
|
-
job.replay(0);
|
|
255
|
-
plainMs = performance.now() - t;
|
|
256
|
-
from = 1;
|
|
257
|
-
report.admission = job.admit(plainMs);
|
|
258
|
-
if (!report.admission.admitted) {
|
|
259
|
-
report.stopped = 'no-admission';
|
|
260
|
-
for (let k = 1; k < job.trip; k++) {
|
|
261
|
-
report.serial++;
|
|
262
|
-
job.replay(k);
|
|
263
|
-
}
|
|
264
|
-
return report;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
let stopped = false;
|
|
268
|
-
let slow = 0;
|
|
269
|
-
for (let first = from; first < job.trip; first += A.batch) {
|
|
270
|
-
const count = Math.min(A.batch, job.trip - first);
|
|
271
|
-
if (stopped) {
|
|
272
|
-
const t = performance.now();
|
|
273
|
-
try {
|
|
274
|
-
for (let k = 0; k < count; k++) {
|
|
275
|
-
report.serial++;
|
|
276
|
-
job.replay(first + k);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
finally {
|
|
280
|
-
report.replayMs += performance.now() - t;
|
|
281
|
-
}
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
284
|
-
report.batches++;
|
|
285
|
-
const t0 = performance.now();
|
|
286
|
-
const outcomes = this.#dispatch(first, count, job, report);
|
|
287
|
-
report.regions += outcomes.length;
|
|
288
|
-
const t1 = performance.now();
|
|
289
|
-
report.dispatchMs += t1 - t0;
|
|
290
|
-
for (const o of outcomes) {
|
|
291
|
-
report.undoEntries += o.entries;
|
|
292
|
-
if (o.status === 1)
|
|
293
|
-
report.traps++;
|
|
294
|
-
if (o.status === 2)
|
|
295
|
-
report.exhausted++;
|
|
296
|
-
}
|
|
297
|
-
const v = iterationVerdict(outcomes, job.skip);
|
|
298
|
-
report.conflicts.raw += v.conflicts.raw;
|
|
299
|
-
report.conflicts.war += v.conflicts.war;
|
|
300
|
-
report.conflicts.waw += v.conflicts.waw;
|
|
301
|
-
report.conflicts.frame += v.conflicts.frame;
|
|
302
|
-
const t2 = performance.now();
|
|
303
|
-
report.validationMs += t2 - t1;
|
|
304
|
-
let replays = 0;
|
|
305
|
-
if (v.first < 0)
|
|
306
|
-
this.#commitFrame(job, outcomes, count);
|
|
307
|
-
else {
|
|
308
|
-
replays = count - v.first;
|
|
309
|
-
report.failedBatches++;
|
|
310
|
-
if (v.firstFailure === v.first)
|
|
311
|
-
report.fallbacks++;
|
|
312
|
-
this.#rollback(first, outcomes, v.first);
|
|
313
|
-
report.rolledBack += outcomes.length - v.first;
|
|
314
|
-
this.#commitFrame(job, outcomes, v.first);
|
|
315
|
-
const t3 = performance.now();
|
|
316
|
-
report.rollbackMs += t3 - t2;
|
|
317
|
-
try {
|
|
318
|
-
for (let k = v.first; k < count; k++) {
|
|
319
|
-
if (k < outcomes.length)
|
|
320
|
-
report.replayed++;
|
|
321
|
-
else
|
|
322
|
-
report.serial++;
|
|
323
|
-
job.replay(first + k);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
finally {
|
|
327
|
-
report.replayMs += performance.now() - t3;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
const wallMs = performance.now() - t0;
|
|
331
|
-
const serialMs = count * plainMs;
|
|
332
|
-
if (report.admission) {
|
|
333
|
-
report.admission.batchWallMs = wallMs;
|
|
334
|
-
report.admission.batchSerialMs = serialMs;
|
|
335
|
-
}
|
|
336
|
-
// the first batch also pays for waking the workers, so a batch that is merely slow is not a
|
|
337
|
-
// verdict until a second one confirms it
|
|
338
|
-
slow = job.admit && job.loses?.(wallMs, serialMs) ? slow + 1 : 0;
|
|
339
|
-
const collapsed = job.admit && job.collapses?.(wallMs, serialMs);
|
|
340
|
-
const stop = outcomes.some((o) => o.status === 2)
|
|
341
|
-
? 'log-exhausted'
|
|
342
|
-
: v.frameFirst >= 0
|
|
343
|
-
? 'frame-carried dependence'
|
|
344
|
-
: replays * 2 > count
|
|
345
|
-
? 'dependency'
|
|
346
|
-
: (slow >= 2 || collapsed) && first + count < job.trip
|
|
347
|
-
? 'no-admission'
|
|
348
|
-
: undefined;
|
|
349
|
-
if (stop) {
|
|
350
|
-
report.stopped ??= stop;
|
|
351
|
-
stopped = true;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
return report;
|
|
355
|
-
}
|
|
356
|
-
#dispatch(first, count, job, report) {
|
|
357
|
-
this.#control[0] = 0;
|
|
358
|
-
Atomics.store(this.#u32, this.arena.stamp / 4, 0);
|
|
359
|
-
const used = this.#links.slice(0, Math.min(this.width, count));
|
|
360
|
-
const message = {
|
|
361
|
-
first,
|
|
362
|
-
count,
|
|
363
|
-
entry: job.entry,
|
|
364
|
-
frameBase: job.frameBase,
|
|
365
|
-
reverse: job.schedule === 'reverse'
|
|
366
|
-
};
|
|
367
|
-
for (const link of used)
|
|
368
|
-
link.port.postMessage(message);
|
|
369
|
-
const outcomes = [];
|
|
370
|
-
let failure;
|
|
371
|
-
for (const link of used) {
|
|
372
|
-
const reply = this.#join(link);
|
|
373
|
-
if (reply.error !== undefined)
|
|
374
|
-
failure ??= reply.error;
|
|
375
|
-
else {
|
|
376
|
-
outcomes.push(...reply.done);
|
|
377
|
-
report.workerRunMs += reply.runMs;
|
|
378
|
-
report.workerScanMs += reply.scanMs;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
if (failure !== undefined)
|
|
382
|
-
throw new Error(`iteration worker failed: ${failure}`);
|
|
383
|
-
return outcomes.sort((a, b) => a.index - b.index);
|
|
384
|
-
}
|
|
385
|
-
// the written frame bytes of the first `kept` iterations of a batch, in order, into the owner frame
|
|
386
|
-
#commitFrame(job, outcomes, kept) {
|
|
387
|
-
const A = this.arena;
|
|
388
|
-
if (!A.frameBytes)
|
|
389
|
-
return;
|
|
390
|
-
for (let k = 0; k < kept; k++) {
|
|
391
|
-
const o = outcomes[k];
|
|
392
|
-
const slot = o.index - outcomes[0].index;
|
|
393
|
-
const frame = A.slots + (slot + 1) * A.slotBytes - A.frameBytes;
|
|
394
|
-
o.frameWritten.forEach((word, g) => {
|
|
395
|
-
for (let b = 0; b < 32; b++)
|
|
396
|
-
if ((word >>> b) & 1) {
|
|
397
|
-
const at = 32 * g + b;
|
|
398
|
-
this.#u8[job.frameBase + at] = this.#u8[frame + at];
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
// newest stamp first across every iteration being rolled back
|
|
404
|
-
#rollback(first, outcomes, from) {
|
|
405
|
-
const A = this.arena;
|
|
406
|
-
const entries = [];
|
|
407
|
-
for (let k = from; k < outcomes.length; k++) {
|
|
408
|
-
const o = outcomes[k];
|
|
409
|
-
const base = A.logs + (o.index - first) * A.logBytes;
|
|
410
|
-
for (let e = 0; e < o.entries; e++) {
|
|
411
|
-
const at = base + e * iterationEntryBytes;
|
|
412
|
-
entries.push({
|
|
413
|
-
address: this.#u32[at / 4],
|
|
414
|
-
stamp: this.#u32[at / 4 + 1],
|
|
415
|
-
data: at + 8
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
entries.sort((a, b) => b.stamp - a.stamp);
|
|
420
|
-
for (const e of entries)
|
|
421
|
-
this.#u8.copyWithin(e.address, e.data, e.data + iterationGranules.write);
|
|
422
|
-
}
|
|
423
|
-
/** Stops the workers. Safe to call twice. */
|
|
424
|
-
async close() {
|
|
425
|
-
if (this.#closed)
|
|
426
|
-
return;
|
|
427
|
-
this.#closed = true;
|
|
428
|
-
await Promise.all(this.#links.map((link) => link.worker.terminate()));
|
|
429
|
-
for (const link of this.#links)
|
|
430
|
-
link.port.close();
|
|
431
|
-
}
|
|
432
|
-
[Symbol.asyncDispose]() {
|
|
433
|
-
return this.close();
|
|
434
|
-
}
|
|
435
|
-
}
|