poker-flopsolve 0.2.0 → 0.3.1

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/dist/index.js CHANGED
@@ -2078,135 +2078,11 @@ function makeSolver(ctx) {
2078
2078
  };
2079
2079
  }
2080
2080
 
2081
- // src/warm.ts
2082
- var SIZE_TOLERANCE = 1.35;
2083
- var REGRET_STEPS2 = 32767;
2084
- var SUM_STEPS2 = 65535;
2085
- function matchNodes(to, from) {
2086
- const match = new Int32Array(to.nodeCount).fill(-1);
2087
- const walk2 = (a, b) => {
2088
- if (to.kind[a] !== from.kind[b] || to.round[a] !== from.round[b]) return;
2089
- if (to.kind[a] === NodeKind.Chance) {
2090
- walk2(to.edgeChild[to.firstEdge[a]], from.edgeChild[from.firstEdge[b]]);
2091
- return;
2092
- }
2093
- if (to.kind[a] !== NodeKind.Decision || to.player[a] !== from.player[b]) return;
2094
- match[a] = b;
2095
- for (let e = 0; e < to.numEdges[a]; e++) {
2096
- const edge = to.firstEdge[a] + e;
2097
- const other = matchEdge(to, edge, a, from, b);
2098
- if (other >= 0) walk2(to.edgeChild[edge], from.edgeChild[other]);
2099
- }
2100
- };
2101
- walk2(0, 0);
2102
- return match;
2103
- }
2104
- function matchEdge(to, edge, node, from, other) {
2105
- const kind = to.edgeAction[edge];
2106
- const sized = kind === ActionKind.Bet || kind === ActionKind.Raise;
2107
- const size = to.edgeTarget[edge] / to.pot[node];
2108
- let best = -1;
2109
- let bestGap = Infinity;
2110
- for (let e = 0; e < from.numEdges[other]; e++) {
2111
- const candidate = from.firstEdge[other] + e;
2112
- if (from.edgeAction[candidate] !== kind) continue;
2113
- if (!sized) return candidate;
2114
- const gap = Math.abs(Math.log(from.edgeTarget[candidate] / from.pot[other] / size));
2115
- if (gap < bestGap) {
2116
- bestGap = gap;
2117
- best = candidate;
2118
- }
2119
- }
2120
- return bestGap <= Math.log(SIZE_TOLERANCE) ? best : -1;
2121
- }
2122
- function readBlock(store, scale, block, base, length, out) {
2123
- const factor = scale ? scale[block] : 1;
2124
- for (let i = 0; i < length; i++) out[i] = store[base + i] * factor;
2125
- }
2126
- function writeBlock(store, scale, steps, block, base, length, values) {
2127
- if (!scale) {
2128
- for (let i = 0; i < length; i++) store[base + i] = values[i];
2129
- return;
2130
- }
2131
- let largest = 0;
2132
- for (let i = 0; i < length; i++) {
2133
- const magnitude = Math.abs(values[i]);
2134
- if (magnitude > largest) largest = magnitude;
2135
- }
2136
- const step = largest > 0 ? steps / largest : 0;
2137
- for (let i = 0; i < length; i++) store[base + i] = Math.round(values[i] * step);
2138
- scale[block] = largest / steps;
2139
- }
2140
- function realCards(runouts, round, board) {
2141
- const width = runouts.width[round];
2142
- return Array.from(runouts.cards[round].subarray(board * width, (board + 1) * width));
2143
- }
2144
- function warmStart(to, from, averages) {
2145
- const a = to.runouts.initial;
2146
- const b = from.runouts.initial;
2147
- if (a.length !== b.length || a.some((card, i) => card !== b[i])) {
2148
- throw new Error("A warm start must come from a solve of the same board");
2149
- }
2150
- const match = matchNodes(to.tree, from.tree);
2151
- const { tree } = to;
2152
- const width = to.layout.maxActions * COMBOS;
2153
- const oldRegret = new Float64Array(width);
2154
- const oldSum = new Float64Array(width);
2155
- const newRegret = new Float64Array(width);
2156
- const newSum = new Float64Array(width);
2157
- const hasRegret = to.regret.length > 0 && from.regret.length > 0;
2158
- const position = new Int32Array(COMBOS).fill(-1);
2159
- let matched = 0;
2160
- for (const node of tree.decisionNodes) {
2161
- const old = match[node];
2162
- if (old < 0) continue;
2163
- matched++;
2164
- const round = tree.round[node];
2165
- const owner = tree.player[node];
2166
- const actions = tree.numEdges[node];
2167
- const oldActions = from.tree.numEdges[old];
2168
- const actionOf = new Int32Array(actions).fill(-1);
2169
- for (let e = 0; e < actions; e++) {
2170
- const other = matchEdge(tree, tree.firstEdge[node] + e, node, from.tree, old);
2171
- if (other >= 0) actionOf[e] = other - from.tree.firstEdge[old];
2172
- }
2173
- for (let board = 0; board < to.runouts.counts[round]; board++) {
2174
- const resolved = resolveBoard(from.runouts, realCards(to.runouts, round, board));
2175
- const oldSlot = (from.active.roundBase[round] + resolved.board) * 2 + owner;
2176
- const oldFrom = from.active.start[oldSlot];
2177
- const oldCount = from.active.count[oldSlot];
2178
- const oldBlock = from.layout.blockOffset[old] + resolved.board;
2179
- const oldBase = from.layout.offset[old] + oldActions * from.layout.rowStart[oldSlot];
2180
- const oldLength = oldActions * oldCount;
2181
- if (hasRegret) readBlock(from.regret, from.regretScale, oldBlock, oldBase, oldLength, oldRegret);
2182
- readBlock(from.strategySum, from.sumScale, oldBlock, oldBase, oldLength, oldSum);
2183
- for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = j;
2184
- const newSlot = (to.active.roundBase[round] + board) * 2 + owner;
2185
- const newFrom = to.active.start[newSlot];
2186
- const count = to.active.count[newSlot];
2187
- const length = actions * count;
2188
- newRegret.fill(0, 0, length);
2189
- newSum.fill(0, 0, length);
2190
- for (let i = 0; i < count; i++) {
2191
- const j = position[resolved.combos[to.active.list[newFrom + i]]];
2192
- if (j < 0) continue;
2193
- for (let e = 0; e < actions; e++) {
2194
- const action = actionOf[e];
2195
- if (action < 0) continue;
2196
- newRegret[e * count + i] = oldRegret[action * oldCount + j];
2197
- newSum[e * count + i] = oldSum[action * oldCount + j];
2198
- }
2199
- }
2200
- for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = -1;
2201
- const block = to.layout.blockOffset[node] + board;
2202
- const base = to.layout.offset[node] + actions * to.layout.rowStart[newSlot];
2203
- if (hasRegret) writeBlock(to.regret, to.regretScale, REGRET_STEPS2, block, base, length, newRegret);
2204
- if (averages) writeBlock(to.strategySum, to.sumScale, SUM_STEPS2, block, base, length, newSum);
2205
- tieBlock(to, node, board);
2206
- }
2207
- }
2208
- return { matchedNodes: matched, decisionNodes: tree.decisionNodes.length, averages };
2209
- }
2081
+ // src/wasm/engine.ts
2082
+ var ENGINE_WASM = "AGFzbQEAAAABTglgAX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AjDiACIANqQQJ0aigCACEEA0AgB0EATgR/Iw4gASAHakECdGooAgAgBEoFQQALBEAgDSAAIAdBAnRqKgIAuyIOoCENIyAgASAHaiIIIwxqLQAAQQN0aiIKIAorAwAgDqA5AwAjICMNIAhqLQAAQQN0aiIIIAgrAwAgDqA5AwAgB0EBayEHDAELCyAGIAJBAnRqIAUgCSACQQN0aisDACANIyAgAiADaiIEIwxqLQAAQQN0aisDACMgIw0gBGotAABBA3RqKwMAoKGhorY4AgAgAkEBayECDAELCyMgQQBBoAP8CwALDgAgACQiIAEkIyACJCQLDQAgACQlIAFBAEckJgsGACAAJDELFQAgACQnIAEkKCACJCkgA0ECdCQqCysAIAAkKyAFJCwgBiQtIAckLiAIJC8gCUEARyQwIAEgAkEAIAMgBEEAEAALKwAgACQrIAQkLCAFJC0gBiQuIAckLyAIQQBHJDAgAUEAQQAgAiADQQAQAAv2AQAgACgCACQAIAAoAgQkASAAKAIIJAIgACgCDCQDIAAoAhAkBCAAKAIUJAUgACgCGCQGIAAoAhwkByAAKAIgJAggACgCJCQJIAAoAigkCiAAKAIsJAsgACgCMCQMIAAoAjQkDSAAKAI4JA4gACgCPCQPIABBQGsoAgAkECAAKAJEJBEgACgCSCQSIAAoAkwkEyAAKAJQJBQgACgCVCQVIAAoAlgkFiAAKAJcJBcgACgCYCQYIAAoAmQkGSAAKAJoJBogACgCbCQbIAAoAnAkHCAAKAJ0JB0gACgCeCQeIAAoAnwkHyAAKAKAASQgIAAoAoQBuCQhCw==";
2083
+
2084
+ // src/wasm/engine-shared.ts
2085
+ var ENGINE_SHARED_WASM = "AGFzbQEAAAABTglgAX8AYAZ/f39/f38Bf2AFf39/f38AYAd/f39/f3x/AGADf39/AGACf38AYAR/f39/AGAKf39/f399fX19fwF/YAl/f39/fX19fX8BfwISAQNlbnYGbWVtb3J5AgMAgIAEAwsKAQIDBAUABgcIAAaOAjJ/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAA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CSACQQN0aisDACANIyAgAiADaiIEIwxqLQAAQQN0aisDACMgIw0gBGotAABBA3RqKwMAoKGhorY4AgAgAkEBayECDAELCyMgQQBBoAP8CwALDgAgACQiIAEkIyACJCQLDQAgACQlIAFBAEckJgsGACAAJDELFQAgACQnIAEkKCACJCkgA0ECdCQqCysAIAAkKyAFJCwgBiQtIAckLiAIJC8gCUEARyQwIAEgAkEAIAMgBEEAEAALKwAgACQrIAQkLCAFJC0gBiQuIAckLyAIQQBHJDAgAUEAQQAgAiADQQAQAAv2AQAgACgCACQAIAAoAgQkASAAKAIIJAIgACgCDCQDIAAoAhAkBCAAKAIUJAUgACgCGCQGIAAoAhwkByAAKAIgJAggACgCJCQJIAAoAigkCiAAKAIsJAsgACgCMCQMIAAoAjQkDSAAKAI4JA4gACgCPCQPIABBQGsoAgAkECAAKAJEJBEgACgCSCQSIAAoAkwkEyAAKAJQJBQgACgCVCQVIAAoAlgkFiAAKAJcJBcgACgCYCQYIAAoAmQkGSAAKAJoJBogACgCbCQbIAAoAnAkHCAAKAJ0JB0gACgCeCQeIAAoAnwkHyAAKAKAASQgIAAoAoQBuCQhCw==";
2210
2086
 
2211
2087
  // src/platform/node.ts
2212
2088
  import { availableParallelism } from "os";
@@ -2215,13 +2091,27 @@ import { Worker } from "worker_threads";
2215
2091
  var PLATFORM = "node";
2216
2092
  var THREADS_AVAILABLE = true;
2217
2093
  var DEFAULT_MAX_BYTES = 6 * 1024 * 1024 * 1024;
2094
+ function workerFlags() {
2095
+ return process.execArgv.filter((flag) => !flag.startsWith("--input-type"));
2096
+ }
2218
2097
  function defaultWorkerCount() {
2219
2098
  const cores = availableParallelism();
2220
2099
  return cores < 2 ? 1 : Math.max(2, cores - 1);
2221
2100
  }
2222
2101
  function spawnWorker(setup, onError) {
2223
2102
  const url = new URL(import.meta.url.endsWith(".ts") ? "../worker.ts" : "./worker.js", import.meta.url);
2224
- const thread = new Worker(fileURLToPath(url), { workerData: setup });
2103
+ const thread = new Worker(fileURLToPath(url), { workerData: setup, execArgv: workerFlags() });
2104
+ thread.on("error", onError);
2105
+ thread.unref();
2106
+ return {
2107
+ terminate() {
2108
+ void thread.terminate();
2109
+ }
2110
+ };
2111
+ }
2112
+ function spawnEngineWorker(setup, onError) {
2113
+ const url = new URL(import.meta.url.endsWith(".ts") ? "../engine-worker.ts" : "./engine-worker.js", import.meta.url);
2114
+ const thread = new Worker(fileURLToPath(url), { workerData: setup, execArgv: workerFlags() });
2225
2115
  thread.on("error", onError);
2226
2116
  thread.unref();
2227
2117
  return {
@@ -2243,6 +2133,8 @@ var CONTROL = {
2243
2133
  size: 8
2244
2134
  };
2245
2135
  var COMMAND = { work: 0, exit: 1 };
2136
+ var WORKER_START_MS = 3e4;
2137
+ var WORKER_START_ERROR = "Worker threads did not start within 30 s. Run with workers: 1, or check that worker threads can load this package.";
2246
2138
  function planShards(tree, runouts, workers) {
2247
2139
  if (workers < 2) return null;
2248
2140
  let round = -1;
@@ -2303,10 +2195,12 @@ function startPool(ctx, solver, plan, payload, workers) {
2303
2195
  });
2304
2196
  threads.push(thread);
2305
2197
  }
2198
+ const deadline = Date.now() + WORKER_START_MS;
2306
2199
  for (; ; ) {
2307
2200
  const ready = Atomics.load(control, CONTROL.ready);
2308
2201
  if (ready >= spawned || failure) break;
2309
- Atomics.wait(control, CONTROL.ready, ready, 20);
2202
+ if (Date.now() > deadline) failure = new Error(WORKER_START_ERROR);
2203
+ else Atomics.wait(control, CONTROL.ready, ready, 20);
2310
2204
  }
2311
2205
  if (failure) {
2312
2206
  for (const thread of threads) thread.terminate();
@@ -2396,6 +2290,388 @@ function startPool(ctx, solver, plan, payload, workers) {
2396
2290
  };
2397
2291
  }
2398
2292
 
2293
+ // src/engine.ts
2294
+ var wasm2 = globalThis.WebAssembly;
2295
+ var modules = { plain: void 0, shared: void 0 };
2296
+ function compiledEngine(kind) {
2297
+ if (modules[kind] !== void 0) return modules[kind];
2298
+ modules[kind] = null;
2299
+ try {
2300
+ if (!wasm2) return null;
2301
+ const text = kind === "plain" ? ENGINE_WASM : ENGINE_SHARED_WASM;
2302
+ const bytes = Uint8Array.from(atob(text), (c) => c.charCodeAt(0));
2303
+ if (!wasm2.validate(bytes)) return null;
2304
+ modules[kind] = new wasm2.Module(bytes);
2305
+ } catch {
2306
+ modules[kind] = null;
2307
+ }
2308
+ return modules[kind];
2309
+ }
2310
+ var ENGINE_CONTROL = { generation: 0, done: 1, command: 2, mode: 3, traverser: 4, ready: 5, size: 8 };
2311
+ function createEngine(input, pool) {
2312
+ const shared = pool !== void 0;
2313
+ const compiledModule = compiledEngine(shared ? "shared" : "plain");
2314
+ if (!compiledModule || !wasm2) return null;
2315
+ const { tree, runouts, layout, active, hands } = input;
2316
+ const threads = pool ? pool.threads : 1;
2317
+ const flatten = (parts) => {
2318
+ const bases = new Int32Array(parts.length + 1);
2319
+ for (let i = 0; i < parts.length; i++) bases[i + 1] = bases[i] + parts[i].length;
2320
+ const flat = new Int32Array(bases[parts.length]);
2321
+ parts.forEach((part, i) => flat.set(part, bases[i]));
2322
+ return [bases, flat];
2323
+ };
2324
+ const [upRound, upStart] = flatten(hands.upStart);
2325
+ const [childRound, childStart] = flatten(runouts.childStart);
2326
+ const [orbitRound, orbitStart] = flatten(runouts.orbitStart);
2327
+ const maxCount = Math.max(4, ...active.count);
2328
+ const stride = Math.ceil(maxCount / 4) * 4;
2329
+ const maxActions = Math.max(1, layout.maxActions);
2330
+ const depths = treeDepth2(tree) + 2;
2331
+ const rowBytes = stride * 4;
2332
+ const depthBytes = rowBytes * (3 + 2 * maxActions);
2333
+ const pieces = [];
2334
+ const add = (data, bytes2 = data ? data.byteLength : 0) => {
2335
+ pieces.push({ data, bytes: Math.ceil(Math.max(bytes2, 4) / 16) * 16 });
2336
+ return pieces.length - 1;
2337
+ };
2338
+ const common = [
2339
+ add(tree.kind),
2340
+ add(tree.round),
2341
+ add(Uint8Array.from(tree.player, (p) => p < 0 ? 255 : p)),
2342
+ add(tree.firstEdge),
2343
+ add(tree.numEdges),
2344
+ add(tree.edgeChild),
2345
+ add(tree.payoff),
2346
+ add(layout.offset),
2347
+ add(layout.rowStart),
2348
+ add(active.roundBase),
2349
+ add(active.start),
2350
+ add(Int32Array.from(active.count)),
2351
+ add(active.cardA),
2352
+ add(active.cardB),
2353
+ add(active.strength),
2354
+ add(active.tieStart),
2355
+ add(active.ties),
2356
+ add(upRound),
2357
+ add(upStart),
2358
+ add(hands.up),
2359
+ add(hands.sameStart),
2360
+ add(hands.same),
2361
+ add(childRound),
2362
+ add(childStart),
2363
+ add(orbitRound),
2364
+ add(orbitStart),
2365
+ add(input.runoutWeight),
2366
+ add(null, layout.totalSlots * 4),
2367
+ // regret
2368
+ add(null, layout.totalSlots * 4)
2369
+ // strategy sums
2370
+ ];
2371
+ const REGRET = common[27];
2372
+ const SUMS = common[28];
2373
+ const own = Array.from({ length: threads }, () => ({
2374
+ buffers: add(null, depths * depthBytes),
2375
+ cardSums: add(null, 52 * 8 + stride * 8),
2376
+ rootMe: add(null, rowBytes),
2377
+ rootOpp: add(null, rowBytes),
2378
+ header: add(null, 40 * 4)
2379
+ }));
2380
+ const entries = pool ? pool.plan.entryNodes.length : 0;
2381
+ const entryIndexOf = pool ? add(pool.plan.entryIndexOf) : -1;
2382
+ const reach = pool ? add(null, entries * 2 * rowBytes) : -1;
2383
+ const values = pool ? add(null, entries * rowBytes) : -1;
2384
+ const partials = pool ? add(null, (threads - 1) * entries * rowBytes) : -1;
2385
+ const offsets = [];
2386
+ let total = 64;
2387
+ for (const piece of pieces) {
2388
+ offsets.push(total);
2389
+ total += piece.bytes;
2390
+ }
2391
+ if (total > 4294901760) return null;
2392
+ const pages = Math.ceil(total / 65536) + 1;
2393
+ const memory = shared ? new wasm2.Memory({ initial: pages, maximum: pages, shared: true }) : new wasm2.Memory({ initial: pages });
2394
+ const bytes = new Uint8Array(memory.buffer);
2395
+ pieces.forEach((piece, i) => {
2396
+ if (piece.data) bytes.set(new Uint8Array(piece.data.buffer, piece.data.byteOffset, piece.data.byteLength), offsets[i]);
2397
+ });
2398
+ for (const mine of own) {
2399
+ const head = new Uint32Array(memory.buffer, offsets[mine.header], 40);
2400
+ common.forEach((slot, i) => head[i] = offsets[slot]);
2401
+ head[29] = offsets[mine.buffers];
2402
+ head[30] = depthBytes;
2403
+ head[31] = rowBytes;
2404
+ head[32] = offsets[mine.cardSums];
2405
+ head[33] = input.chipScale;
2406
+ }
2407
+ const instance = new wasm2.Instance(compiledModule, { env: { memory, abort: () => {
2408
+ throw new Error("engine abort");
2409
+ } } });
2410
+ const exports = instance.exports;
2411
+ exports.init(offsets[own[0].header]);
2412
+ exports.setMaxActions(maxActions);
2413
+ const regret = new Float32Array(memory.buffer, offsets[REGRET], layout.totalSlots);
2414
+ const strategySum = new Float32Array(memory.buffer, offsets[SUMS], layout.totalSlots);
2415
+ const me = new Float32Array(memory.buffer, offsets[own[0].rootMe], stride);
2416
+ const opp = new Float32Array(memory.buffer, offsets[own[0].rootOpp], stride);
2417
+ const result = new Float64Array(COMBOS);
2418
+ const rootList = (player) => {
2419
+ const slot = active.roundBase[0] * 2 + player;
2420
+ return [active.start[slot], active.count[slot]];
2421
+ };
2422
+ const settingsFor = (mode, iterWeight, dcfr) => {
2423
+ if (mode === Mode.Train && dcfr) {
2424
+ const { keepPositive, keepNegative, keepSum } = dcfrFactors(iterWeight);
2425
+ return [keepPositive, keepNegative, keepSum, 1, 0];
2426
+ }
2427
+ return [1, 1, 1, iterWeight, dcfr ? 0 : 1];
2428
+ };
2429
+ let control = null;
2430
+ let settings = null;
2431
+ const workers = [];
2432
+ let failure = null;
2433
+ if (pool) {
2434
+ const plan = pool.plan;
2435
+ const controlBuffer = new SharedArrayBuffer(ENGINE_CONTROL.size * 4);
2436
+ const settingsBuffer = new SharedArrayBuffer(8 * 8);
2437
+ control = new Int32Array(controlBuffer);
2438
+ settings = new Float64Array(settingsBuffer);
2439
+ exports.setExchange(offsets[entryIndexOf], offsets[reach], offsets[values], stride);
2440
+ for (let w = 1; w < threads; w++) {
2441
+ const setup = {
2442
+ memory,
2443
+ module: compiledModule,
2444
+ header: offsets[own[w].header],
2445
+ maxActions,
2446
+ exchange: [offsets[entryIndexOf], offsets[reach], offsets[values], stride],
2447
+ shard: [plan.round, plan.bounds[w], plan.bounds[w + 1]],
2448
+ entryNodes: plan.entryNodes,
2449
+ rootMe: offsets[own[w].rootMe],
2450
+ rootOpp: offsets[own[w].rootOpp],
2451
+ partial: offsets[partials] + (w - 1) * entries * rowBytes,
2452
+ stride,
2453
+ control: controlBuffer,
2454
+ settings: settingsBuffer
2455
+ };
2456
+ workers.push(pool.spawn(setup, (error) => {
2457
+ failure ??= error;
2458
+ Atomics.store(control, ENGINE_CONTROL.ready, threads);
2459
+ Atomics.add(control, ENGINE_CONTROL.done, threads);
2460
+ Atomics.notify(control, ENGINE_CONTROL.done);
2461
+ Atomics.notify(control, ENGINE_CONTROL.ready);
2462
+ }));
2463
+ }
2464
+ const deadline = Date.now() + WORKER_START_MS;
2465
+ for (; ; ) {
2466
+ const ready = Atomics.load(control, ENGINE_CONTROL.ready);
2467
+ if (ready >= threads - 1 || failure) break;
2468
+ if (Date.now() > deadline) failure = new Error(WORKER_START_ERROR);
2469
+ else Atomics.wait(control, ENGINE_CONTROL.ready, ready, 20);
2470
+ }
2471
+ if (failure) {
2472
+ for (const worker of workers) worker.terminate();
2473
+ throw failure;
2474
+ }
2475
+ }
2476
+ const run = (mode, traverser, iterWeight, dcfr) => {
2477
+ for (const player of [0, 1]) {
2478
+ const [from, count2] = rootList(player);
2479
+ const into = player === traverser ? me : opp;
2480
+ const weights = input.players[player].weights;
2481
+ for (let i = 0; i < count2; i++) into[i] = weights[active.list[from + i]];
2482
+ }
2483
+ const [positive, negative, sum, weight, clamp] = settingsFor(mode, iterWeight, dcfr);
2484
+ let at;
2485
+ if (!pool) {
2486
+ at = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
2487
+ } else {
2488
+ const plan = pool.plan;
2489
+ exports.setShard(plan.round, 0, 0);
2490
+ exports.setPhase(1, 0);
2491
+ exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
2492
+ settings.set([positive, negative, sum, weight, clamp]);
2493
+ Atomics.store(control, ENGINE_CONTROL.done, 0);
2494
+ Atomics.store(control, ENGINE_CONTROL.mode, mode);
2495
+ Atomics.store(control, ENGINE_CONTROL.traverser, traverser);
2496
+ Atomics.store(control, ENGINE_CONTROL.command, 0);
2497
+ Atomics.add(control, ENGINE_CONTROL.generation, 1);
2498
+ Atomics.notify(control, ENGINE_CONTROL.generation);
2499
+ exports.setShard(plan.round, plan.bounds[0], plan.bounds[1]);
2500
+ exports.setPhase(0, 1);
2501
+ const valueView = new Float32Array(memory.buffer, offsets[values], entries * stride);
2502
+ for (let entry = 0; entry < entries; entry++) {
2503
+ const slot = offsets[reach] + entry * 2 * rowBytes;
2504
+ const out = exports.runChance(mode, traverser, plan.entryNodes[entry], slot, slot + rowBytes, positive, negative, sum, weight, clamp);
2505
+ valueView.set(new Float32Array(memory.buffer, out, stride), entry * stride);
2506
+ }
2507
+ for (; ; ) {
2508
+ const done = Atomics.load(control, ENGINE_CONTROL.done);
2509
+ if (done >= threads - 1 || failure) break;
2510
+ Atomics.wait(control, ENGINE_CONTROL.done, done, 50);
2511
+ }
2512
+ if (failure) throw failure;
2513
+ const partialView = new Float32Array(memory.buffer, offsets[partials], (threads - 1) * entries * stride);
2514
+ for (let w = 0; w < threads - 1; w++) {
2515
+ for (let i = 0; i < entries * stride; i++) valueView[i] += partialView[w * entries * stride + i];
2516
+ }
2517
+ exports.setShard(plan.round, 0, 0);
2518
+ exports.setPhase(2, 1);
2519
+ at = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
2520
+ exports.setPhase(0, 1);
2521
+ exports.setShard(-1, 0, 0);
2522
+ }
2523
+ const [, count] = rootList(traverser);
2524
+ const values32 = new Float32Array(memory.buffer, at, count);
2525
+ for (let i = 0; i < count; i++) result[i] = values32[i];
2526
+ return result;
2527
+ };
2528
+ return {
2529
+ regret,
2530
+ strategySum,
2531
+ threads,
2532
+ root(dcfr) {
2533
+ return (mode, traverser, iterWeight) => run(mode, traverser, iterWeight, dcfr);
2534
+ },
2535
+ close() {
2536
+ if (!control) return;
2537
+ Atomics.store(control, ENGINE_CONTROL.command, 1);
2538
+ Atomics.add(control, ENGINE_CONTROL.generation, 1);
2539
+ Atomics.notify(control, ENGINE_CONTROL.generation);
2540
+ for (const worker of workers) worker.terminate();
2541
+ }
2542
+ };
2543
+ }
2544
+
2545
+ // src/warm.ts
2546
+ var SIZE_TOLERANCE = 1.35;
2547
+ var REGRET_STEPS2 = 32767;
2548
+ var SUM_STEPS2 = 65535;
2549
+ function matchNodes(to, from) {
2550
+ const match = new Int32Array(to.nodeCount).fill(-1);
2551
+ const walk2 = (a, b) => {
2552
+ if (to.kind[a] !== from.kind[b] || to.round[a] !== from.round[b]) return;
2553
+ if (to.kind[a] === NodeKind.Chance) {
2554
+ walk2(to.edgeChild[to.firstEdge[a]], from.edgeChild[from.firstEdge[b]]);
2555
+ return;
2556
+ }
2557
+ if (to.kind[a] !== NodeKind.Decision || to.player[a] !== from.player[b]) return;
2558
+ match[a] = b;
2559
+ for (let e = 0; e < to.numEdges[a]; e++) {
2560
+ const edge = to.firstEdge[a] + e;
2561
+ const other = matchEdge(to, edge, a, from, b);
2562
+ if (other >= 0) walk2(to.edgeChild[edge], from.edgeChild[other]);
2563
+ }
2564
+ };
2565
+ walk2(0, 0);
2566
+ return match;
2567
+ }
2568
+ function matchEdge(to, edge, node, from, other) {
2569
+ const kind = to.edgeAction[edge];
2570
+ const sized = kind === ActionKind.Bet || kind === ActionKind.Raise;
2571
+ const size = to.edgeTarget[edge] / to.pot[node];
2572
+ let best = -1;
2573
+ let bestGap = Infinity;
2574
+ for (let e = 0; e < from.numEdges[other]; e++) {
2575
+ const candidate = from.firstEdge[other] + e;
2576
+ if (from.edgeAction[candidate] !== kind) continue;
2577
+ if (!sized) return candidate;
2578
+ const gap = Math.abs(Math.log(from.edgeTarget[candidate] / from.pot[other] / size));
2579
+ if (gap < bestGap) {
2580
+ bestGap = gap;
2581
+ best = candidate;
2582
+ }
2583
+ }
2584
+ return bestGap <= Math.log(SIZE_TOLERANCE) ? best : -1;
2585
+ }
2586
+ function readBlock(store, scale, block, base, length, out) {
2587
+ const factor = scale ? scale[block] : 1;
2588
+ for (let i = 0; i < length; i++) out[i] = store[base + i] * factor;
2589
+ }
2590
+ function writeBlock(store, scale, steps, block, base, length, values) {
2591
+ if (!scale) {
2592
+ for (let i = 0; i < length; i++) store[base + i] = values[i];
2593
+ return;
2594
+ }
2595
+ let largest = 0;
2596
+ for (let i = 0; i < length; i++) {
2597
+ const magnitude = Math.abs(values[i]);
2598
+ if (magnitude > largest) largest = magnitude;
2599
+ }
2600
+ const step = largest > 0 ? steps / largest : 0;
2601
+ for (let i = 0; i < length; i++) store[base + i] = Math.round(values[i] * step);
2602
+ scale[block] = largest / steps;
2603
+ }
2604
+ function realCards(runouts, round, board) {
2605
+ const width = runouts.width[round];
2606
+ return Array.from(runouts.cards[round].subarray(board * width, (board + 1) * width));
2607
+ }
2608
+ function warmStart(to, from, averages) {
2609
+ const a = to.runouts.initial;
2610
+ const b = from.runouts.initial;
2611
+ if (a.length !== b.length || a.some((card, i) => card !== b[i])) {
2612
+ throw new Error("A warm start must come from a solve of the same board");
2613
+ }
2614
+ const match = matchNodes(to.tree, from.tree);
2615
+ const { tree } = to;
2616
+ const width = to.layout.maxActions * COMBOS;
2617
+ const oldRegret = new Float64Array(width);
2618
+ const oldSum = new Float64Array(width);
2619
+ const newRegret = new Float64Array(width);
2620
+ const newSum = new Float64Array(width);
2621
+ const hasRegret = to.regret.length > 0 && from.regret.length > 0;
2622
+ const position = new Int32Array(COMBOS).fill(-1);
2623
+ let matched = 0;
2624
+ for (const node of tree.decisionNodes) {
2625
+ const old = match[node];
2626
+ if (old < 0) continue;
2627
+ matched++;
2628
+ const round = tree.round[node];
2629
+ const owner = tree.player[node];
2630
+ const actions = tree.numEdges[node];
2631
+ const oldActions = from.tree.numEdges[old];
2632
+ const actionOf = new Int32Array(actions).fill(-1);
2633
+ for (let e = 0; e < actions; e++) {
2634
+ const other = matchEdge(tree, tree.firstEdge[node] + e, node, from.tree, old);
2635
+ if (other >= 0) actionOf[e] = other - from.tree.firstEdge[old];
2636
+ }
2637
+ for (let board = 0; board < to.runouts.counts[round]; board++) {
2638
+ const resolved = resolveBoard(from.runouts, realCards(to.runouts, round, board));
2639
+ const oldSlot = (from.active.roundBase[round] + resolved.board) * 2 + owner;
2640
+ const oldFrom = from.active.start[oldSlot];
2641
+ const oldCount = from.active.count[oldSlot];
2642
+ const oldBlock = from.layout.blockOffset[old] + resolved.board;
2643
+ const oldBase = from.layout.offset[old] + oldActions * from.layout.rowStart[oldSlot];
2644
+ const oldLength = oldActions * oldCount;
2645
+ if (hasRegret) readBlock(from.regret, from.regretScale, oldBlock, oldBase, oldLength, oldRegret);
2646
+ readBlock(from.strategySum, from.sumScale, oldBlock, oldBase, oldLength, oldSum);
2647
+ for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = j;
2648
+ const newSlot = (to.active.roundBase[round] + board) * 2 + owner;
2649
+ const newFrom = to.active.start[newSlot];
2650
+ const count = to.active.count[newSlot];
2651
+ const length = actions * count;
2652
+ newRegret.fill(0, 0, length);
2653
+ newSum.fill(0, 0, length);
2654
+ for (let i = 0; i < count; i++) {
2655
+ const j = position[resolved.combos[to.active.list[newFrom + i]]];
2656
+ if (j < 0) continue;
2657
+ for (let e = 0; e < actions; e++) {
2658
+ const action = actionOf[e];
2659
+ if (action < 0) continue;
2660
+ newRegret[e * count + i] = oldRegret[action * oldCount + j];
2661
+ newSum[e * count + i] = oldSum[action * oldCount + j];
2662
+ }
2663
+ }
2664
+ for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = -1;
2665
+ const block = to.layout.blockOffset[node] + board;
2666
+ const base = to.layout.offset[node] + actions * to.layout.rowStart[newSlot];
2667
+ if (hasRegret) writeBlock(to.regret, to.regretScale, REGRET_STEPS2, block, base, length, newRegret);
2668
+ if (averages) writeBlock(to.strategySum, to.sumScale, SUM_STEPS2, block, base, length, newSum);
2669
+ tieBlock(to, node, board);
2670
+ }
2671
+ }
2672
+ return { matchedNodes: matched, decisionNodes: tree.decisionNodes.length, averages };
2673
+ }
2674
+
2399
2675
  // src/index.ts
2400
2676
  var DEFAULT_CHIP_SCALE = 100;
2401
2677
  var OPS_PER_SECOND = 2e8;
@@ -2534,7 +2810,7 @@ var PRECISION_BYTES = { f64: 8, f32: 4, i16: 2 };
2534
2810
  function createRun(spot, options = {}) {
2535
2811
  const maxBytes = spot.source.maxBytes ?? DEFAULT_MAX_BYTES;
2536
2812
  const { totalSlots, bytes } = spot.estimate;
2537
- const precision = options.analysisOnly ? "f32" : options.precision ?? (totalSlots < 4e6 && bytes * 2 <= maxBytes ? "f64" : bytes <= maxBytes ? "f32" : "i16");
2813
+ const precision = options.analysisOnly ? "f32" : options.precision ?? (bytes <= maxBytes ? "f32" : "i16");
2538
2814
  const relevant = new Uint8Array(COMBOS);
2539
2815
  for (const combo of spot.players[0].combos) relevant[combo] = 1;
2540
2816
  for (const combo of spot.players[1].combos) relevant[combo] = 1;
@@ -2547,20 +2823,34 @@ function createRun(spot, options = {}) {
2547
2823
  const width = PRECISION_BYTES[precision];
2548
2824
  const quantised = precision === "i16";
2549
2825
  const active = buildActiveLists(spot.runouts, spot.players, showdown);
2826
+ const hands = buildHandSpace(spot.runouts, spot.players, active);
2827
+ const engine = options.engine !== "js" && precision === "f32" && !options.analysisOnly && options.simd !== false ? createEngine(
2828
+ {
2829
+ tree: spot.tree,
2830
+ runouts: spot.runouts,
2831
+ players: spot.players,
2832
+ layout,
2833
+ active,
2834
+ hands,
2835
+ runoutWeight: weights,
2836
+ chipScale: spot.chipScale
2837
+ },
2838
+ plan ? { plan, threads: requested, spawn: spawnEngineWorker } : void 0
2839
+ ) : null;
2550
2840
  const ctx = {
2551
2841
  tree: spot.tree,
2552
2842
  runouts: spot.runouts,
2553
2843
  players: spot.players,
2554
2844
  layout,
2555
2845
  showdown,
2556
- regret: options.analysisOnly ? new Float32Array(0) : makeRegretStore(precision, allocate(layout.totalSlots * width)),
2557
- strategySum: makeSumStore(precision, allocate(layout.totalSlots * width)),
2846
+ regret: engine ? engine.regret : options.analysisOnly ? new Float32Array(0) : makeRegretStore(precision, allocate(layout.totalSlots * width)),
2847
+ strategySum: engine ? engine.strategySum : makeSumStore(precision, allocate(layout.totalSlots * width)),
2558
2848
  regretScale: quantised ? new Float64Array(allocate(layout.blocks * 8)) : null,
2559
2849
  sumScale: quantised ? new Float64Array(allocate(layout.blocks * 8)) : null,
2560
2850
  scratch: makeTerminalScratch(),
2561
2851
  chipScale: spot.chipScale,
2562
2852
  active,
2563
- hands: buildHandSpace(spot.runouts, spot.players, active),
2853
+ hands,
2564
2854
  runoutWeight: weights,
2565
2855
  shardStart: 0,
2566
2856
  shardEnd: plan ? plan.boards : 0,
@@ -2586,17 +2876,20 @@ function createRun(spot, options = {}) {
2586
2876
  offset = options.warmIteration ?? source.totalIterations;
2587
2877
  }
2588
2878
  const solver = makeSolver(ctx);
2589
- const pool = plan ? startPool(ctx, solver, plan, { config: spot.source, dcfr: ctx.dcfr, precision, approximations: options.approximations, simd: ctx.simd }, requested) : null;
2879
+ const pool = plan && !engine ? startPool(ctx, solver, plan, { config: spot.source, dcfr: ctx.dcfr, precision, approximations: options.approximations, simd: ctx.simd }, requested) : null;
2590
2880
  if (pool) solver.useRoot(pool.traverse);
2881
+ if (engine) solver.useRoot(engine.root(ctx.dcfr));
2882
+ const threadCount = pool ? pool.workers : engine ? engine.threads : 1;
2591
2883
  let completed = 0;
2592
2884
  return {
2593
2885
  spot,
2594
2886
  solver,
2595
- workers: pool ? pool.workers : 1,
2887
+ workers: threadCount,
2596
2888
  get completed() {
2597
2889
  return completed;
2598
2890
  },
2599
2891
  warmStart: warm,
2892
+ engine: engine ? "wasm" : "js",
2600
2893
  get totalIterations() {
2601
2894
  return offset + completed;
2602
2895
  },
@@ -2608,6 +2901,7 @@ function createRun(spot, options = {}) {
2608
2901
  close() {
2609
2902
  solver.useRoot(null);
2610
2903
  pool?.close();
2904
+ engine?.close();
2611
2905
  },
2612
2906
  exploitabilityPercent() {
2613
2907
  return 100 * solver.exploitability() / (spot.potUnits / spot.chipScale);
@@ -2615,7 +2909,7 @@ function createRun(spot, options = {}) {
2615
2909
  trueExploitabilityPercent() {
2616
2910
  if (!spot.runouts.approximate) return this.exploitabilityPercent();
2617
2911
  const real = createRun(defineSpot({ ...spot.source, runoutBuckets: false }), {
2618
- workers: pool ? pool.workers : 1,
2912
+ workers: threadCount,
2619
2913
  precision
2620
2914
  });
2621
2915
  try {
@@ -2626,7 +2920,7 @@ function createRun(spot, options = {}) {
2626
2920
  }
2627
2921
  },
2628
2922
  snapshot(history = [], stoppedAt = "iterations") {
2629
- return makeSolution(spot, ctx, solver, completed, offset + completed, [...history], stoppedAt, pool ? pool.workers : 1);
2923
+ return makeSolution(spot, ctx, solver, completed, offset + completed, [...history], stoppedAt, threadCount);
2630
2924
  }
2631
2925
  };
2632
2926
  }