poker-flopsolve 0.1.0 → 0.3.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 +39 -6
- package/dist/browser.js +664 -362
- package/dist/browser.js.map +1 -1
- package/dist/engine-worker.d.ts +2 -0
- package/dist/engine-worker.js +57 -0
- package/dist/engine-worker.js.map +1 -0
- package/dist/index.d.ts +74 -3
- package/dist/index.js +672 -362
- package/dist/index.js.map +1 -1
- package/dist/worker.js +362 -343
- package/dist/worker.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/engine-worker.ts
|
|
2
|
+
import { parentPort, workerData } from "worker_threads";
|
|
3
|
+
|
|
4
|
+
// src/engine.ts
|
|
5
|
+
var wasm = globalThis.WebAssembly;
|
|
6
|
+
var ENGINE_CONTROL = { generation: 0, done: 1, command: 2, mode: 3, traverser: 4, ready: 5, size: 8 };
|
|
7
|
+
|
|
8
|
+
// src/engine-worker.ts
|
|
9
|
+
var setup = workerData;
|
|
10
|
+
var { WebAssembly: wasm2 } = globalThis;
|
|
11
|
+
var memory = setup.memory;
|
|
12
|
+
var instance = new wasm2.Instance(setup.module, { env: { memory, abort: () => {
|
|
13
|
+
throw new Error("engine abort");
|
|
14
|
+
} } });
|
|
15
|
+
var engine = instance.exports;
|
|
16
|
+
engine.init(setup.header);
|
|
17
|
+
engine.setMaxActions(setup.maxActions);
|
|
18
|
+
engine.setExchange(...setup.exchange);
|
|
19
|
+
engine.setShard(...setup.shard);
|
|
20
|
+
engine.setPhase(0, 1);
|
|
21
|
+
var control = new Int32Array(setup.control);
|
|
22
|
+
var settings = new Float64Array(setup.settings);
|
|
23
|
+
var entries = setup.entryNodes.length;
|
|
24
|
+
var rowBytes = setup.stride * 4;
|
|
25
|
+
var partials = new Float32Array(memory.buffer, setup.partial, entries * setup.stride);
|
|
26
|
+
var [, reach] = setup.exchange;
|
|
27
|
+
var generation = Atomics.load(control, ENGINE_CONTROL.generation);
|
|
28
|
+
Atomics.add(control, ENGINE_CONTROL.ready, 1);
|
|
29
|
+
Atomics.notify(control, ENGINE_CONTROL.ready);
|
|
30
|
+
for (; ; ) {
|
|
31
|
+
Atomics.wait(control, ENGINE_CONTROL.generation, generation);
|
|
32
|
+
generation = Atomics.load(control, ENGINE_CONTROL.generation);
|
|
33
|
+
if (Atomics.load(control, ENGINE_CONTROL.command) === 1) break;
|
|
34
|
+
const mode = Atomics.load(control, ENGINE_CONTROL.mode);
|
|
35
|
+
const traverser = Atomics.load(control, ENGINE_CONTROL.traverser);
|
|
36
|
+
const [positive, negative, sum, weight, clamp] = settings;
|
|
37
|
+
for (let entry = 0; entry < entries; entry++) {
|
|
38
|
+
const slot = reach + entry * 2 * rowBytes;
|
|
39
|
+
const out = engine.runChance(
|
|
40
|
+
mode,
|
|
41
|
+
traverser,
|
|
42
|
+
setup.entryNodes[entry],
|
|
43
|
+
slot,
|
|
44
|
+
slot + rowBytes,
|
|
45
|
+
positive,
|
|
46
|
+
negative,
|
|
47
|
+
sum,
|
|
48
|
+
weight,
|
|
49
|
+
clamp
|
|
50
|
+
);
|
|
51
|
+
partials.set(new Float32Array(memory.buffer, out, setup.stride), entry * setup.stride);
|
|
52
|
+
}
|
|
53
|
+
Atomics.add(control, ENGINE_CONTROL.done, 1);
|
|
54
|
+
Atomics.notify(control, ENGINE_CONTROL.done);
|
|
55
|
+
}
|
|
56
|
+
parentPort?.close();
|
|
57
|
+
//# sourceMappingURL=engine-worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/engine-worker.ts","../src/engine.ts"],"sourcesContent":["/**\n * An engine worker: one thread of a WebAssembly solve.\n *\n * Purpose: walk this thread's slice of the runouts at the split round, over\n * the memory every thread shares, when the main thread signals. The protocol\n * is src/parallel.ts's: the main thread collects the reach each entry needs,\n * every thread walks its slice from each entry, the main thread adds the\n * slices up and finishes. Here all of it happens in shared WebAssembly\n * memory, so nothing is copied between threads but a few numbers of control.\n */\n\nimport { parentPort, workerData } from 'node:worker_threads';\nimport type { EngineExports, EngineWorkerSetup } from './engine.ts';\nimport { ENGINE_CONTROL } from './engine.ts';\n\nconst setup = workerData as EngineWorkerSetup;\nconst { WebAssembly: wasm } = globalThis as unknown as {\n\tWebAssembly: { Instance: new (module: unknown, imports: object) => { exports: Record<string, unknown> } };\n};\nconst memory = setup.memory as { buffer: SharedArrayBuffer };\nconst instance = new wasm.Instance(setup.module, { env: { memory, abort: () => { throw new Error('engine abort'); } } });\nconst engine = instance.exports as unknown as EngineExports;\nengine.init(setup.header);\nengine.setMaxActions(setup.maxActions);\nengine.setExchange(...setup.exchange);\nengine.setShard(...setup.shard);\nengine.setPhase(0, 1);\n\nconst control = new Int32Array(setup.control);\nconst settings = new Float64Array(setup.settings);\nconst entries = setup.entryNodes.length;\nconst rowBytes = setup.stride * 4;\nconst partials = new Float32Array(memory.buffer, setup.partial, entries * setup.stride);\nconst [, reach] = setup.exchange;\n\n// Latch the generation before announcing readiness, so no dispatch is missed.\nlet generation = Atomics.load(control, ENGINE_CONTROL.generation);\nAtomics.add(control, ENGINE_CONTROL.ready, 1);\nAtomics.notify(control, ENGINE_CONTROL.ready);\n\nfor (;;) {\n\tAtomics.wait(control, ENGINE_CONTROL.generation, generation);\n\tgeneration = Atomics.load(control, ENGINE_CONTROL.generation);\n\tif (Atomics.load(control, ENGINE_CONTROL.command) === 1) break;\n\tconst mode = Atomics.load(control, ENGINE_CONTROL.mode);\n\tconst traverser = Atomics.load(control, ENGINE_CONTROL.traverser);\n\tconst [positive, negative, sum, weight, clamp] = settings;\n\tfor (let entry = 0; entry < entries; entry++) {\n\t\tconst slot = reach + entry * 2 * rowBytes;\n\t\tconst out = engine.runChance(\n\t\t\tmode, traverser, setup.entryNodes[entry], slot, slot + rowBytes, positive, negative, sum, weight, clamp,\n\t\t);\n\t\tpartials.set(new Float32Array(memory.buffer, out, setup.stride), entry * setup.stride);\n\t}\n\tAtomics.add(control, ENGINE_CONTROL.done, 1);\n\tAtomics.notify(control, ENGINE_CONTROL.done);\n}\n\nparentPort?.close();\n","/**\n * The WebAssembly traversal engine, set up.\n *\n * Purpose: lay a prepared spot out in one WebAssembly memory for\n * `assembly/engine.ts`, and give the solver its storage and a root traversal.\n *\n * The engine keeps the accumulators in its own memory as 32-bit floats; the\n * solver's `regret` and `strategySum` are views onto them, so everything that\n * reads storage from JavaScript — strategy queries, warm starts, `valuesAt` —\n * sees the very same numbers. It runs single-threaded, with 32-bit storage,\n * where WebAssembly SIMD is available; otherwise the JavaScript traversal in\n * cfr.ts runs.\n */\n\nimport { COMBOS, DECK } from './cards.ts';\nimport type { ActiveLists, Layout, PlayerIndex, RootTraversal } from './cfr.ts';\nimport { dcfrFactors, Mode, treeDepth } from './cfr.ts';\nimport type { HandSpace } from './hands.ts';\nimport type { Runouts } from './runouts.ts';\nimport type { Tree } from './tree.ts';\nimport { ENGINE_WASM } from './wasm/engine.ts';\nimport { ENGINE_SHARED_WASM } from './wasm/engine-shared.ts';\nimport type { ShardPlan } from './parallel.ts';\n\n/** The parts of the WebAssembly API used here, typed locally: the project builds without DOM typings. */\ninterface WasmApi {\n\tvalidate(bytes: Uint8Array): boolean;\n\tModule: new (bytes: Uint8Array) => object;\n\tInstance: new (module: object, imports: object) => { exports: Record<string, unknown> };\n\tMemory: new (descriptor: { initial: number; maximum?: number; shared?: boolean }) => { buffer: ArrayBuffer };\n}\n\nconst wasm = (globalThis as unknown as { WebAssembly?: WasmApi }).WebAssembly;\nconst modules: Record<'plain' | 'shared', object | null | undefined> = { plain: undefined, shared: undefined };\n\n/** The compiled engine, over plain or shared memory; null where unavailable. */\nexport function compiledEngine(kind: 'plain' | 'shared'): object | null {\n\tif (modules[kind] !== undefined) return modules[kind]!;\n\tmodules[kind] = null;\n\ttry {\n\t\tif (!wasm) return null;\n\t\tconst text = kind === 'plain' ? ENGINE_WASM : ENGINE_SHARED_WASM;\n\t\tconst bytes = Uint8Array.from(atob(text), (c) => c.charCodeAt(0));\n\t\tif (!wasm.validate(bytes)) return null;\n\t\tmodules[kind] = new wasm.Module(bytes);\n\t} catch {\n\t\tmodules[kind] = null;\n\t}\n\treturn modules[kind]!;\n}\n\nfunction compiled(): object | null {\n\treturn compiledEngine('plain');\n}\n\n/** Whether this platform can run the WebAssembly engine. */\nexport function engineAvailable(): boolean {\n\treturn compiled() !== null;\n}\n\n/** What the engine needs from a prepared spot. */\nexport interface EngineInput {\n\treadonly tree: Tree;\n\treadonly runouts: Runouts;\n\treadonly players: readonly [PlayerIndex, PlayerIndex];\n\treadonly layout: Layout;\n\treadonly active: ActiveLists;\n\treadonly hands: HandSpace;\n\treadonly runoutWeight: Float64Array;\n\treadonly chipScale: number;\n}\n\n/** A running engine. */\nexport interface Engine {\n\t/** The accumulators, as views onto the engine's memory. */\n\treadonly regret: Float32Array;\n\treadonly strategySum: Float32Array;\n\t/** Install as the solver's root traversal; `dcfr` picks the update rule. */\n\troot(dcfr: boolean): RootTraversal;\n\t/** Release worker threads, if any. */\n\tclose(): void;\n\t/** Threads doing the work, this one included. */\n\treadonly threads: number;\n}\n\n/** What an engine worker needs; see src/engine-worker.ts. */\nexport interface EngineWorkerSetup {\n\treadonly memory: unknown;\n\treadonly module: unknown;\n\treadonly header: number;\n\treadonly maxActions: number;\n\treadonly exchange: readonly [number, number, number, number];\n\treadonly shard: readonly [number, number, number];\n\treadonly entryNodes: Int32Array;\n\treadonly rootMe: number;\n\treadonly rootOpp: number;\n\treadonly partial: number;\n\treadonly stride: number;\n\treadonly control: SharedArrayBuffer;\n\treadonly settings: SharedArrayBuffer;\n}\n\n/** How a pool starts its workers; the platform layer supplies it. */\nexport type SpawnEngineWorker = (setup: EngineWorkerSetup, onError: (error: Error) => void) => { terminate(): void };\n\n/** Control slots shared with engine workers. */\nexport const ENGINE_CONTROL = { generation: 0, done: 1, command: 2, mode: 3, traverser: 4, ready: 5, size: 8 } as const;\n\n/**\n * Lay a spot out in a fresh memory and instantiate the engine over it, or\n * null where unavailable. With `pool`, the memory is shared and that many\n * threads split the runouts at the plan's round, as src/parallel.ts does for\n * the JavaScript engine.\n */\nexport function createEngine(\n\tinput: EngineInput,\n\tpool?: { plan: ShardPlan; threads: number; spawn: SpawnEngineWorker },\n): Engine | null {\n\tconst shared = pool !== undefined;\n\tconst compiledModule = compiledEngine(shared ? 'shared' : 'plain');\n\tif (!compiledModule || !wasm) return null;\n\tconst { tree, runouts, layout, active, hands } = input;\n\tconst threads = pool ? pool.threads : 1;\n\n\tconst flatten = (parts: readonly Int32Array[]): [Int32Array, Int32Array] => {\n\t\tconst bases = new Int32Array(parts.length + 1);\n\t\tfor (let i = 0; i < parts.length; i++) bases[i + 1] = bases[i] + parts[i].length;\n\t\tconst flat = new Int32Array(bases[parts.length]);\n\t\tparts.forEach((part, i) => flat.set(part, bases[i]));\n\t\treturn [bases, flat];\n\t};\n\tconst [upRound, upStart] = flatten(hands.upStart);\n\tconst [childRound, childStart] = flatten(runouts.childStart);\n\tconst [orbitRound, orbitStart] = flatten(runouts.orbitStart);\n\tconst maxCount = Math.max(4, ...active.count);\n\tconst stride = Math.ceil(maxCount / 4) * 4;\n\tconst maxActions = Math.max(1, layout.maxActions);\n\tconst depths = treeDepth(tree) + 2;\n\tconst rowBytes = stride * 4;\n\tconst depthBytes = rowBytes * (3 + 2 * maxActions);\n\n\tconst pieces: { data: ArrayBufferView | null; bytes: number }[] = [];\n\tconst add = (data: ArrayBufferView | null, bytes = data ? data.byteLength : 0): number => {\n\t\tpieces.push({ data, bytes: Math.ceil(Math.max(bytes, 4) / 16) * 16 });\n\t\treturn pieces.length - 1;\n\t};\n\t// Shared by every thread, in header order.\n\tconst common = [\n\t\tadd(tree.kind), add(tree.round), add(Uint8Array.from(tree.player, (p) => (p < 0 ? 255 : p))), add(tree.firstEdge),\n\t\tadd(tree.numEdges), add(tree.edgeChild), add(tree.payoff), add(layout.offset), add(layout.rowStart),\n\t\tadd(active.roundBase), add(active.start), add(Int32Array.from(active.count)), add(active.cardA), add(active.cardB),\n\t\tadd(active.strength), add(active.tieStart), add(active.ties), add(upRound), add(upStart), add(hands.up),\n\t\tadd(hands.sameStart), add(hands.same), add(childRound), add(childStart), add(orbitRound), add(orbitStart),\n\t\tadd(input.runoutWeight),\n\t\tadd(null, layout.totalSlots * 4), // regret\n\t\tadd(null, layout.totalSlots * 4), // strategy sums\n\t];\n\tconst REGRET = common[27];\n\tconst SUMS = common[28];\n\t// Each thread's own.\n\tconst own = Array.from({ length: threads }, () => ({\n\t\tbuffers: add(null, depths * depthBytes),\n\t\tcardSums: add(null, 52 * 8 + stride * 8),\n\t\trootMe: add(null, rowBytes),\n\t\trootOpp: add(null, rowBytes),\n\t\theader: add(null, 40 * 4),\n\t}));\n\t// The exchange between threads.\n\tconst entries = pool ? pool.plan.entryNodes.length : 0;\n\tconst entryIndexOf = pool ? add(pool.plan.entryIndexOf) : -1;\n\tconst reach = pool ? add(null, entries * 2 * rowBytes) : -1;\n\tconst values = pool ? add(null, entries * rowBytes) : -1;\n\tconst partials = pool ? add(null, (threads - 1) * entries * rowBytes) : -1;\n\n\tconst offsets: number[] = [];\n\tlet total = 64;\n\tfor (const piece of pieces) {\n\t\toffsets.push(total);\n\t\ttotal += piece.bytes;\n\t}\n\tif (total > 0xffff0000) return null; // past wasm32's 4 GB\n\tconst pages = Math.ceil(total / 65536) + 1;\n\tconst memory = shared ? new wasm.Memory({ initial: pages, maximum: pages, shared: true }) : new wasm.Memory({ initial: pages });\n\tconst bytes = new Uint8Array(memory.buffer);\n\tpieces.forEach((piece, i) => {\n\t\tif (piece.data) bytes.set(new Uint8Array(piece.data.buffer, piece.data.byteOffset, piece.data.byteLength), offsets[i]);\n\t});\n\tfor (const mine of own) {\n\t\tconst head = new Uint32Array(memory.buffer, offsets[mine.header], 40);\n\t\tcommon.forEach((slot, i) => (head[i] = offsets[slot]));\n\t\thead[29] = offsets[mine.buffers];\n\t\thead[30] = depthBytes;\n\t\thead[31] = rowBytes;\n\t\thead[32] = offsets[mine.cardSums];\n\t\thead[33] = input.chipScale;\n\t}\n\n\tconst instance = new wasm.Instance(compiledModule, { env: { memory, abort: () => { throw new Error('engine abort'); } } });\n\tconst exports = instance.exports as unknown as EngineExports;\n\texports.init(offsets[own[0].header]);\n\texports.setMaxActions(maxActions);\n\n\tconst regret = new Float32Array(memory.buffer, offsets[REGRET], layout.totalSlots);\n\tconst strategySum = new Float32Array(memory.buffer, offsets[SUMS], layout.totalSlots);\n\tconst me = new Float32Array(memory.buffer, offsets[own[0].rootMe], stride);\n\tconst opp = new Float32Array(memory.buffer, offsets[own[0].rootOpp], stride);\n\tconst result = new Float64Array(COMBOS);\n\tconst rootList = (player: 0 | 1): [number, number] => {\n\t\tconst slot = active.roundBase[0] * 2 + player;\n\t\treturn [active.start[slot], active.count[slot]];\n\t};\n\tconst settingsFor = (mode: number, iterWeight: number, dcfr: boolean): [number, number, number, number, number] => {\n\t\tif (mode === Mode.Train && dcfr) {\n\t\t\tconst { keepPositive, keepNegative, keepSum } = dcfrFactors(iterWeight);\n\t\t\treturn [keepPositive, keepNegative, keepSum, 1, 0];\n\t\t}\n\t\treturn [1, 1, 1, iterWeight, dcfr ? 0 : 1];\n\t};\n\n\t// Threads: workers over the same memory, each with its own header.\n\tlet control: Int32Array | null = null;\n\tlet settings: Float64Array | null = null;\n\tconst workers: { terminate(): void }[] = [];\n\tlet failure: Error | null = null;\n\tif (pool) {\n\t\tconst plan = pool.plan;\n\t\tconst controlBuffer = new SharedArrayBuffer(ENGINE_CONTROL.size * 4);\n\t\tconst settingsBuffer = new SharedArrayBuffer(8 * 8);\n\t\tcontrol = new Int32Array(controlBuffer);\n\t\tsettings = new Float64Array(settingsBuffer);\n\t\texports.setExchange(offsets[entryIndexOf], offsets[reach], offsets[values], stride);\n\t\tfor (let w = 1; w < threads; w++) {\n\t\t\tconst setup: EngineWorkerSetup = {\n\t\t\t\tmemory, module: compiledModule, header: offsets[own[w].header], maxActions,\n\t\t\t\texchange: [offsets[entryIndexOf], offsets[reach], offsets[values], stride],\n\t\t\t\tshard: [plan.round, plan.bounds[w], plan.bounds[w + 1]],\n\t\t\t\tentryNodes: plan.entryNodes,\n\t\t\t\trootMe: offsets[own[w].rootMe], rootOpp: offsets[own[w].rootOpp],\n\t\t\t\tpartial: offsets[partials] + (w - 1) * entries * rowBytes,\n\t\t\t\tstride, control: controlBuffer, settings: settingsBuffer,\n\t\t\t};\n\t\t\tworkers.push(pool.spawn(setup, (error) => {\n\t\t\t\tfailure ??= error;\n\t\t\t\tAtomics.store(control!, ENGINE_CONTROL.ready, threads);\n\t\t\t\tAtomics.add(control!, ENGINE_CONTROL.done, threads);\n\t\t\t\tAtomics.notify(control!, ENGINE_CONTROL.done);\n\t\t\t\tAtomics.notify(control!, ENGINE_CONTROL.ready);\n\t\t\t}));\n\t\t}\n\t\tfor (;;) {\n\t\t\tconst ready = Atomics.load(control, ENGINE_CONTROL.ready);\n\t\t\tif (ready >= threads - 1 || failure) break;\n\t\t\tAtomics.wait(control, ENGINE_CONTROL.ready, ready, 20);\n\t\t}\n\t\tif (failure) {\n\t\t\tfor (const worker of workers) worker.terminate();\n\t\t\tthrow failure;\n\t\t}\n\t}\n\n\tconst run = (mode: number, traverser: 0 | 1, iterWeight: number, dcfr: boolean): Float64Array => {\n\t\tfor (const player of [0, 1] as const) {\n\t\t\tconst [from, count] = rootList(player);\n\t\t\tconst into = player === traverser ? me : opp;\n\t\t\tconst weights = input.players[player].weights;\n\t\t\tfor (let i = 0; i < count; i++) into[i] = weights[active.list[from + i]];\n\t\t}\n\t\tconst [positive, negative, sum, weight, clamp] = settingsFor(mode, iterWeight, dcfr);\n\t\tlet at: number;\n\t\tif (!pool) {\n\t\t\tat = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);\n\t\t} else {\n\t\t\tconst plan = pool.plan;\n\t\t\t// Collect: walk above the split, recording what the workers need.\n\t\t\texports.setShard(plan.round, 0, 0);\n\t\t\texports.setPhase(1, 0);\n\t\t\texports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);\n\t\t\t// Every thread walks its slice of the split round.\n\t\t\tsettings!.set([positive, negative, sum, weight, clamp]);\n\t\t\tAtomics.store(control!, ENGINE_CONTROL.done, 0);\n\t\t\tAtomics.store(control!, ENGINE_CONTROL.mode, mode);\n\t\t\tAtomics.store(control!, ENGINE_CONTROL.traverser, traverser);\n\t\t\tAtomics.store(control!, ENGINE_CONTROL.command, 0);\n\t\t\tAtomics.add(control!, ENGINE_CONTROL.generation, 1);\n\t\t\tAtomics.notify(control!, ENGINE_CONTROL.generation);\n\t\t\texports.setShard(plan.round, plan.bounds[0], plan.bounds[1]);\n\t\t\texports.setPhase(0, 1);\n\t\t\tconst valueView = new Float32Array(memory.buffer, offsets[values], entries * stride);\n\t\t\tfor (let entry = 0; entry < entries; entry++) {\n\t\t\t\tconst slot = offsets[reach] + entry * 2 * rowBytes;\n\t\t\t\tconst out = exports.runChance(mode, traverser, plan.entryNodes[entry], slot, slot + rowBytes, positive, negative, sum, weight, clamp);\n\t\t\t\tvalueView.set(new Float32Array(memory.buffer, out, stride), entry * stride);\n\t\t\t}\n\t\t\tfor (;;) {\n\t\t\t\tconst done = Atomics.load(control!, ENGINE_CONTROL.done);\n\t\t\t\tif (done >= threads - 1 || failure) break;\n\t\t\t\tAtomics.wait(control!, ENGINE_CONTROL.done, done, 50);\n\t\t\t}\n\t\t\tif (failure) throw failure;\n\t\t\tconst partialView = new Float32Array(memory.buffer, offsets[partials], (threads - 1) * entries * stride);\n\t\t\tfor (let w = 0; w < threads - 1; w++) {\n\t\t\t\tfor (let i = 0; i < entries * stride; i++) valueView[i] += partialView[w * entries * stride + i];\n\t\t\t}\n\t\t\t// Finish above the split from the totals, now applying updates.\n\t\t\texports.setShard(plan.round, 0, 0);\n\t\t\texports.setPhase(2, 1);\n\t\t\tat = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);\n\t\t\texports.setPhase(0, 1);\n\t\t\texports.setShard(-1, 0, 0);\n\t\t}\n\t\tconst [, count] = rootList(traverser);\n\t\tconst values32 = new Float32Array(memory.buffer, at, count);\n\t\tfor (let i = 0; i < count; i++) result[i] = values32[i];\n\t\treturn result;\n\t};\n\n\treturn {\n\t\tregret,\n\t\tstrategySum,\n\t\tthreads,\n\t\troot(dcfr) {\n\t\t\treturn (mode, traverser, iterWeight) => run(mode, traverser, iterWeight, dcfr);\n\t\t},\n\t\tclose() {\n\t\t\tif (!control) return;\n\t\t\tAtomics.store(control, ENGINE_CONTROL.command, 1);\n\t\t\tAtomics.add(control, ENGINE_CONTROL.generation, 1);\n\t\t\tAtomics.notify(control, ENGINE_CONTROL.generation);\n\t\t\tfor (const worker of workers) worker.terminate();\n\t\t},\n\t};\n}\n\n/** The engine's exports. */\nexport interface EngineExports {\n\tinit(header: number): void;\n\tsetMaxActions(count: number): void;\n\tsetShard(round: number, start: number, end: number): void;\n\tsetExchange(entries: number, reach: number, values: number, stride: number): void;\n\tsetPhase(phase: number, apply: number): void;\n\trun(mode: number, traverser: number, me: number, opp: number, positive: number, negative: number, sum: number, weight: number, clamp: number): number;\n\trunChance(mode: number, traverser: number, node: number, me: number, opp: number, positive: number, negative: number, sum: number, weight: number, clamp: number): number;\n}\n"],"mappings":";AAWA,SAAS,YAAY,kBAAkB;;;ACqBvC,IAAM,OAAQ,WAAoD;AA0E3D,IAAM,iBAAiB,EAAE,YAAY,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,WAAW,GAAG,OAAO,GAAG,MAAM,EAAE;;;AD3F7G,IAAM,QAAQ;AACd,IAAM,EAAE,aAAaA,MAAK,IAAI;AAG9B,IAAM,SAAS,MAAM;AACrB,IAAM,WAAW,IAAIA,MAAK,SAAS,MAAM,QAAQ,EAAE,KAAK,EAAE,QAAQ,OAAO,MAAM;AAAE,QAAM,IAAI,MAAM,cAAc;AAAG,EAAE,EAAE,CAAC;AACvH,IAAM,SAAS,SAAS;AACxB,OAAO,KAAK,MAAM,MAAM;AACxB,OAAO,cAAc,MAAM,UAAU;AACrC,OAAO,YAAY,GAAG,MAAM,QAAQ;AACpC,OAAO,SAAS,GAAG,MAAM,KAAK;AAC9B,OAAO,SAAS,GAAG,CAAC;AAEpB,IAAM,UAAU,IAAI,WAAW,MAAM,OAAO;AAC5C,IAAM,WAAW,IAAI,aAAa,MAAM,QAAQ;AAChD,IAAM,UAAU,MAAM,WAAW;AACjC,IAAM,WAAW,MAAM,SAAS;AAChC,IAAM,WAAW,IAAI,aAAa,OAAO,QAAQ,MAAM,SAAS,UAAU,MAAM,MAAM;AACtF,IAAM,CAAC,EAAE,KAAK,IAAI,MAAM;AAGxB,IAAI,aAAa,QAAQ,KAAK,SAAS,eAAe,UAAU;AAChE,QAAQ,IAAI,SAAS,eAAe,OAAO,CAAC;AAC5C,QAAQ,OAAO,SAAS,eAAe,KAAK;AAE5C,WAAS;AACR,UAAQ,KAAK,SAAS,eAAe,YAAY,UAAU;AAC3D,eAAa,QAAQ,KAAK,SAAS,eAAe,UAAU;AAC5D,MAAI,QAAQ,KAAK,SAAS,eAAe,OAAO,MAAM,EAAG;AACzD,QAAM,OAAO,QAAQ,KAAK,SAAS,eAAe,IAAI;AACtD,QAAM,YAAY,QAAQ,KAAK,SAAS,eAAe,SAAS;AAChE,QAAM,CAAC,UAAU,UAAU,KAAK,QAAQ,KAAK,IAAI;AACjD,WAAS,QAAQ,GAAG,QAAQ,SAAS,SAAS;AAC7C,UAAM,OAAO,QAAQ,QAAQ,IAAI;AACjC,UAAM,MAAM,OAAO;AAAA,MAClB;AAAA,MAAM;AAAA,MAAW,MAAM,WAAW,KAAK;AAAA,MAAG;AAAA,MAAM,OAAO;AAAA,MAAU;AAAA,MAAU;AAAA,MAAU;AAAA,MAAK;AAAA,MAAQ;AAAA,IACnG;AACA,aAAS,IAAI,IAAI,aAAa,OAAO,QAAQ,KAAK,MAAM,MAAM,GAAG,QAAQ,MAAM,MAAM;AAAA,EACtF;AACA,UAAQ,IAAI,SAAS,eAAe,MAAM,CAAC;AAC3C,UAAQ,OAAO,SAAS,eAAe,IAAI;AAC5C;AAEA,YAAY,MAAM;","names":["wasm"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -293,6 +293,46 @@ interface TerminalScratch {
|
|
|
293
293
|
readonly win: Float64Array;
|
|
294
294
|
}
|
|
295
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Hand-space index structures and terminal kernels.
|
|
298
|
+
*
|
|
299
|
+
* Purpose: let a traversal carry each player's reach and values as a dense
|
|
300
|
+
* vector over the hands that player can hold on the current board — entry `i`
|
|
301
|
+
* of their active list (`buildActiveLists` in cfr.ts) — instead of a sparse
|
|
302
|
+
* 1326-wide array reached through a list of live combinations. Every loop
|
|
303
|
+
* then runs `0..n` over contiguous memory.
|
|
304
|
+
*
|
|
305
|
+
* What a traversal needs to move between boards and to settle terminals:
|
|
306
|
+
*
|
|
307
|
+
* - At a deal, which parent hand each child hand is, for the stored board
|
|
308
|
+
* and for every real deal it stands for (relabelled by the orbit's map).
|
|
309
|
+
* Reach is gathered and values scatter-added through these.
|
|
310
|
+
* - At a fold, which opposing hand is the very same combination as each of
|
|
311
|
+
* mine, for the inclusion–exclusion that removes it.
|
|
312
|
+
*
|
|
313
|
+
* A showdown needs nothing here: on a final board the active lists are
|
|
314
|
+
* already sorted by strength, with each entry's strength and cards beside it.
|
|
315
|
+
*/
|
|
316
|
+
|
|
317
|
+
/** Everything a hand-space traversal looks up. */
|
|
318
|
+
interface HandSpace {
|
|
319
|
+
/**
|
|
320
|
+
* Deals: for round `r`, orbit member `m` (an index into
|
|
321
|
+
* `runouts.orbitMap[r]`) and player `p`, `up` from `upStart[r][m * 2 + p]`
|
|
322
|
+
* holds, for each of the player's hands on the stored child board, the
|
|
323
|
+
* index of the parent-board hand it is — through the member's relabelling.
|
|
324
|
+
*/
|
|
325
|
+
readonly upStart: readonly Int32Array[];
|
|
326
|
+
readonly up: Uint16Array;
|
|
327
|
+
/**
|
|
328
|
+
* Folds: for each list slot `(roundBase[r] + board) * 2 + p`, from
|
|
329
|
+
* `sameStart[slot]`, the index in the other player's list of each of this
|
|
330
|
+
* player's hands, or -1 where they cannot hold it.
|
|
331
|
+
*/
|
|
332
|
+
readonly sameStart: Int32Array;
|
|
333
|
+
readonly same: Int32Array;
|
|
334
|
+
}
|
|
335
|
+
|
|
296
336
|
/**
|
|
297
337
|
* Vector CFR+ over a fixed board.
|
|
298
338
|
*
|
|
@@ -325,7 +365,17 @@ interface PlayerIndex {
|
|
|
325
365
|
}
|
|
326
366
|
/** Slot offsets for every decision node. */
|
|
327
367
|
interface Layout {
|
|
368
|
+
/**
|
|
369
|
+
* Where each decision node's storage starts. Its block on a board is
|
|
370
|
+
* `actions` rows of the owner's hands live there, in hand-list order —
|
|
371
|
+
* entry `i` of their active list — so a row is as long as that list and
|
|
372
|
+
* blocked hands take no room. See `blockBase`.
|
|
373
|
+
*/
|
|
328
374
|
readonly offset: Int32Array;
|
|
375
|
+
/** For each list slot `(roundBase[r] + board) * 2 + p`: rows before this board's. */
|
|
376
|
+
readonly rowStart: Int32Array;
|
|
377
|
+
/** First list index of each round, as `ActiveLists.roundBase`. */
|
|
378
|
+
readonly roundBase: Int32Array;
|
|
329
379
|
/**
|
|
330
380
|
* First block of each decision node, else -1. A block is one node on one
|
|
331
381
|
* board — `actions * combosOfItsOwner` slots, exactly what one visit
|
|
@@ -364,6 +414,8 @@ interface Context {
|
|
|
364
414
|
readonly chipScale: number;
|
|
365
415
|
/** Which combinations each player can hold on each board. */
|
|
366
416
|
readonly active: ActiveLists;
|
|
417
|
+
/** How hand-space vectors move between boards and meet at terminals. */
|
|
418
|
+
readonly hands: HandSpace;
|
|
367
419
|
/**
|
|
368
420
|
* Board completions still to come after each round, with four cards already
|
|
369
421
|
* removed for the two hands. A hand that ends early would otherwise be
|
|
@@ -425,10 +477,19 @@ interface ActiveLists {
|
|
|
425
477
|
* Hands that mirror each other on a board with symmetry left, whose
|
|
426
478
|
* accumulators are kept equal, indexed by the same slot as `start`:
|
|
427
479
|
* `ties[tieStart[slot]..tieStart[slot + 1]]` is a run of classes, each its
|
|
428
|
-
* size then that many
|
|
480
|
+
* size then that many positions in the board's hand list. Empty almost
|
|
481
|
+
* everywhere.
|
|
429
482
|
*/
|
|
430
483
|
readonly tieStart: Int32Array;
|
|
431
484
|
readonly ties: Int32Array;
|
|
485
|
+
/** Each entry's two cards, beside `list`. */
|
|
486
|
+
readonly cardA: Uint8Array;
|
|
487
|
+
readonly cardB: Uint8Array;
|
|
488
|
+
/**
|
|
489
|
+
* Each entry's strength on its final board, beside `list`; final-board
|
|
490
|
+
* lists are sorted by it. Zero elsewhere.
|
|
491
|
+
*/
|
|
492
|
+
readonly strength: Int32Array;
|
|
432
493
|
}
|
|
433
494
|
declare const Mode: {
|
|
434
495
|
readonly Train: 0;
|
|
@@ -813,8 +874,10 @@ interface SolveOptions {
|
|
|
813
874
|
readonly exploitEvery?: number;
|
|
814
875
|
readonly onProgress?: (info: Progress) => void;
|
|
815
876
|
/**
|
|
816
|
-
* Accumulator storage. Unset,
|
|
817
|
-
*
|
|
877
|
+
* Accumulator storage. Unset, 'f32' — what the WebAssembly engine runs —
|
|
878
|
+
* or 'i16' for a spot too big for it under `maxBytes`. 'f64' is there on
|
|
879
|
+
* request and runs the JavaScript engine; measured, it buys no accuracy
|
|
880
|
+
* an exploitability target can see.
|
|
818
881
|
*
|
|
819
882
|
* 'i16' stores regrets and strategy sums as 16-bit integers with one scale
|
|
820
883
|
* per node and board, about halving memory. It is for fitting a spot, not
|
|
@@ -894,6 +957,12 @@ interface SolveOptions {
|
|
|
894
957
|
* either way, to the bit; this exists to compare the two. Default true.
|
|
895
958
|
*/
|
|
896
959
|
readonly simd?: boolean;
|
|
960
|
+
/**
|
|
961
|
+
* Which traversal runs. Unset, the WebAssembly engine wherever it applies
|
|
962
|
+
* — one thread, 32-bit storage, WebAssembly SIMD available — and the
|
|
963
|
+
* JavaScript one otherwise. 'js' forces JavaScript, to compare the two.
|
|
964
|
+
*/
|
|
965
|
+
readonly engine?: 'js';
|
|
897
966
|
}
|
|
898
967
|
/** See `SolveOptions.approximations`. */
|
|
899
968
|
interface Approximations {
|
|
@@ -986,6 +1055,8 @@ interface SolveRun {
|
|
|
986
1055
|
readonly completed: number;
|
|
987
1056
|
/** Iterations behind the strategy, counting any a warm start inherited. */
|
|
988
1057
|
readonly totalIterations: number;
|
|
1058
|
+
/** Which traversal runs: the WebAssembly engine or the JavaScript one. */
|
|
1059
|
+
readonly engine: 'wasm' | 'js';
|
|
989
1060
|
/** What a warm start carried over, or null without one. */
|
|
990
1061
|
readonly warmStart: WarmStartReport | null;
|
|
991
1062
|
/** Run `count` more iterations. */
|