poker-flopsolve 0.4.1 → 0.5.0-beta.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.
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Opt-in threads for the browser build: `poker-flopsolve/threads`.
3
+ *
4
+ * Purpose: let a page split a solve across cores, as Node does, without the
5
+ * main build depending on it. Nothing here runs unless it is imported and
6
+ * called; a solve that never asks for threads is the same, bit for bit, as
7
+ * one in a build without this entry.
8
+ *
9
+ * In a browser, call `enableThreads` once, inside a Web Worker on a
10
+ * cross-origin isolated page, and await it before solving:
11
+ *
12
+ * import { createRun, defineSpot } from 'poker-flopsolve';
13
+ * import { enableThreads } from 'poker-flopsolve/threads';
14
+ * const threads = await enableThreads(); // 1 if the page cannot thread
15
+ * const run = createRun(defineSpot(config), { workers: threads });
16
+ *
17
+ * In Node this resolves to the thread count straight away: Node starts
18
+ * threads with each solve and needs no preparation.
19
+ */
20
+ /**
21
+ * Start engine threads so that solves can use up to `count` threads, the
22
+ * calling one included, and resolve to how many they can use: 1 where the
23
+ * page cannot thread (see {@link threadsUnavailable}).
24
+ *
25
+ * Threads stay started between solves; a solve borrows them with
26
+ * `workers: n` and returns them on `close()`. Calling again with a smaller
27
+ * count stops the idle surplus, so `enableThreads(1)` stops them all.
28
+ *
29
+ * @param count threads wanted, the calling one included; defaults to the
30
+ * machine's core count (`navigator.hardwareConcurrency` in a browser)
31
+ */
32
+ declare function enableThreads(count?: number): Promise<number>;
33
+ /**
34
+ * Why threads cannot run here, or null if they can. In a browser they need
35
+ * a cross-origin isolated page (`Cross-Origin-Opener-Policy: same-origin`
36
+ * and `Cross-Origin-Embedder-Policy: require-corp`) and a Web Worker to
37
+ * solve in, since a page's main thread may not block.
38
+ */
39
+ declare function threadsUnavailable(): string | null;
40
+
41
+ export { enableThreads, threadsUnavailable };
@@ -0,0 +1,28 @@
1
+ // src/platform/node.ts
2
+ import { availableParallelism } from "os";
3
+ import { fileURLToPath } from "url";
4
+ import { Worker } from "worker_threads";
5
+ var DEFAULT_MAX_BYTES = 6 * 1024 * 1024 * 1024;
6
+ function defaultWorkerCount() {
7
+ const cores = availableParallelism();
8
+ return cores < 2 ? 1 : Math.max(2, cores - 1);
9
+ }
10
+ function threadsUnavailableReason() {
11
+ return null;
12
+ }
13
+ async function prepareThreads(count) {
14
+ return Math.max(1, Math.floor(count ?? defaultWorkerCount()));
15
+ }
16
+
17
+ // src/threads.ts
18
+ function enableThreads(count) {
19
+ return prepareThreads(count);
20
+ }
21
+ function threadsUnavailable() {
22
+ return threadsUnavailableReason();
23
+ }
24
+ export {
25
+ enableThreads,
26
+ threadsUnavailable
27
+ };
28
+ //# sourceMappingURL=threads.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/platform/node.ts","../src/threads.ts"],"sourcesContent":["/**\n * Node platform.\n *\n * Purpose: the one place the solver touches Node-only APIs. A browser build\n * swaps this module for `browser.ts`, which has the same exports; see\n * `scripts/browser-platform.ts`.\n */\n\nimport { availableParallelism } from 'node:os';\nimport { fileURLToPath } from 'node:url';\nimport { Worker } from 'node:worker_threads';\nimport type { WorkerSetup } from '../parallel.ts';\nimport type { EngineWorkerSetup } from '../engine.ts';\n\n/** Which build this is. */\nexport const PLATFORM = 'node';\n\n/**\n * The most threads a solve can use here: as many as asked, on either engine.\n * @param _engine whether the WebAssembly engine is to run\n */\nexport function threadLimit(_engine: boolean): number {\n\treturn Infinity;\n}\n\n/** Default ceiling on solver storage. */\nexport const DEFAULT_MAX_BYTES = 6 * 1024 * 1024 * 1024;\n\n/**\n * The parent's Node flags, minus any a worker started from a file rejects:\n * `--input-type` is for code given as a string (`node -e`), and a worker\n * inheriting it fails to start.\n */\nfunction workerFlags(): string[] {\n\treturn process.execArgv.filter((flag) => !flag.startsWith('--input-type'));\n}\n\n/** A spawned solver thread. */\nexport interface WorkerHandle {\n\tterminate(): void;\n}\n\n/**\n * Suggested worker count for this machine, the calling thread included.\n *\n * One less than the available parallelism, but never under two. Scaling stays\n * monotone right up to the core count — a flop solve measured 3.3x on 4\n * workers, 3.9x on 6 and 4.3x on 8 across 8 cores — so the only reason to hold\n * one back is leaving the machine responsive, and on two cores that would mean\n * not splitting at all.\n */\nexport function defaultWorkerCount(): number {\n\tconst cores = availableParallelism();\n\treturn cores < 2 ? 1 : Math.max(2, cores - 1);\n}\n\n/**\n * Start one solver thread on `worker.ts`.\n *\n * @param setup everything the worker needs to rebuild the solver\n * @param onError called if the thread dies, so the pool can stop waiting on it\n */\nexport function spawnWorker(setup: WorkerSetup, onError: (error: Error) => void): WorkerHandle {\n\t// From source the worker is TypeScript; from a build it sits beside this\n\t// bundle as JavaScript.\n\tconst url = new URL(import.meta.url.endsWith('.ts') ? '../worker.ts' : './worker.js', import.meta.url);\n\tconst thread = new Worker(fileURLToPath(url), { workerData: setup, execArgv: workerFlags() });\n\tthread.on('error', onError);\n\tthread.unref();\n\treturn {\n\t\tterminate() {\n\t\t\tvoid thread.terminate();\n\t\t},\n\t};\n}\n\n/** Start one WebAssembly engine thread on `engine-worker.ts`. */\nexport function spawnEngineWorker(setup: EngineWorkerSetup, onError: (error: Error) => void): WorkerHandle {\n\tconst url = new URL(import.meta.url.endsWith('.ts') ? '../engine-worker.ts' : './engine-worker.js', import.meta.url);\n\tconst thread = new Worker(fileURLToPath(url), { workerData: setup, execArgv: workerFlags() });\n\tthread.on('error', onError);\n\tthread.unref();\n\treturn {\n\t\tterminate() {\n\t\t\tvoid thread.terminate();\n\t\t},\n\t};\n}\n\n/**\n * Why a solve gets fewer threads than it asked for: never, in Node.\n * @param _engine whether the WebAssembly engine is to run\n */\nexport function threadAdvice(_engine: boolean): string {\n\treturn '';\n}\n\n/** Node always has worker threads. */\nexport function threadsUnavailableReason(): string | null {\n\treturn null;\n}\n\n/**\n * Nothing to start ahead of time in Node: threads start with each solve.\n * @param count threads wanted, this one included; defaults to {@link defaultWorkerCount}\n */\nexport async function prepareThreads(count?: number): Promise<number> {\n\treturn Math.max(1, Math.floor(count ?? defaultWorkerCount()));\n}\n","/**\n * Opt-in threads for the browser build: `poker-flopsolve/threads`.\n *\n * Purpose: let a page split a solve across cores, as Node does, without the\n * main build depending on it. Nothing here runs unless it is imported and\n * called; a solve that never asks for threads is the same, bit for bit, as\n * one in a build without this entry.\n *\n * In a browser, call `enableThreads` once, inside a Web Worker on a\n * cross-origin isolated page, and await it before solving:\n *\n * import { createRun, defineSpot } from 'poker-flopsolve';\n * import { enableThreads } from 'poker-flopsolve/threads';\n * const threads = await enableThreads(); // 1 if the page cannot thread\n * const run = createRun(defineSpot(config), { workers: threads });\n *\n * In Node this resolves to the thread count straight away: Node starts\n * threads with each solve and needs no preparation.\n */\n\nimport { prepareThreads, threadsUnavailableReason } from './platform/node.ts';\n\n/**\n * Start engine threads so that solves can use up to `count` threads, the\n * calling one included, and resolve to how many they can use: 1 where the\n * page cannot thread (see {@link threadsUnavailable}).\n *\n * Threads stay started between solves; a solve borrows them with\n * `workers: n` and returns them on `close()`. Calling again with a smaller\n * count stops the idle surplus, so `enableThreads(1)` stops them all.\n *\n * @param count threads wanted, the calling one included; defaults to the\n * machine's core count (`navigator.hardwareConcurrency` in a browser)\n */\nexport function enableThreads(count?: number): Promise<number> {\n\treturn prepareThreads(count);\n}\n\n/**\n * Why threads cannot run here, or null if they can. In a browser they need\n * a cross-origin isolated page (`Cross-Origin-Opener-Policy: same-origin`\n * and `Cross-Origin-Embedder-Policy: require-corp`) and a Web Worker to\n * solve in, since a page's main thread may not block.\n */\nexport function threadsUnavailable(): string | null {\n\treturn threadsUnavailableReason();\n}\n"],"mappings":";AAQA,SAAS,4BAA4B;AACrC,SAAS,qBAAqB;AAC9B,SAAS,cAAc;AAgBhB,IAAM,oBAAoB,IAAI,OAAO,OAAO;AAyB5C,SAAS,qBAA6B;AAC5C,QAAM,QAAQ,qBAAqB;AACnC,SAAO,QAAQ,IAAI,IAAI,KAAK,IAAI,GAAG,QAAQ,CAAC;AAC7C;AA4CO,SAAS,2BAA0C;AACzD,SAAO;AACR;AAMA,eAAsB,eAAe,OAAiC;AACrE,SAAO,KAAK,IAAI,GAAG,KAAK,MAAM,SAAS,mBAAmB,CAAC,CAAC;AAC7D;;;AC1EO,SAAS,cAAc,OAAiC;AAC9D,SAAO,eAAe,KAAK;AAC5B;AAQO,SAAS,qBAAoC;AACnD,SAAO,yBAAyB;AACjC;","names":[]}
@@ -0,0 +1,84 @@
1
+ import { SpotConfig, SolveOptions, PresetChanges, WasmStorage } from './index.js';
2
+ export { SettingChange, SizesCut, Street } from './index.js';
3
+
4
+ /**
5
+ * Opt-in 16-bit and compact storage on the WebAssembly engine:
6
+ * `poker-flopsolve/wasm-storage` (issue #84).
7
+ *
8
+ * Purpose: run `precision: 'i16'` and `compactStorage` solves on the
9
+ * WebAssembly engine, threads included, where they would otherwise run on
10
+ * the slower JavaScript one. They follow src/cfr.ts's 16-bit and compact
11
+ * updates step for step, in the engine's 32-bit vectors.
12
+ *
13
+ * import { createRun, defineSpot } from 'poker-flopsolve';
14
+ * import { wasmStorage } from 'poker-flopsolve/wasm-storage';
15
+ * const run = createRun(defineSpot(config), { precision: 'i16', wasmStorage });
16
+ *
17
+ * The engine's bytes live only here, so a build that never imports this
18
+ * entry is unchanged, and a solve that does not pass it runs as before.
19
+ */
20
+
21
+ /** The storage engine, to pass as `SolveOptions.wasmStorage`. */
22
+ declare const wasmStorage: WasmStorage;
23
+ /** Settings for `largeTree`. */
24
+ interface LargeTreeOptions {
25
+ /**
26
+ * 'i16' (the default): 16-bit storage, the more accurate; a
27
+ * `compactStorage` in the config is dropped. 'compact': `compactStorage`
28
+ * (8 bits with sums, or the config's own settings), about two thirds of
29
+ * the memory and a little slower, for a tab with less to give.
30
+ */
31
+ readonly storage?: 'i16' | 'compact';
32
+ /**
33
+ * Solve options of your own (workers, iterations, a target, ...), merged
34
+ * over the preset's: `approximations` field by field, so `chanceSampling`
35
+ * settings of your own replace the preset's and others join them. The
36
+ * engine stays the preset's.
37
+ */
38
+ readonly options?: SolveOptions;
39
+ }
40
+ /** What `largeTree` returns: the spot's settings and the solve's, and what they changed. */
41
+ interface LargeTree {
42
+ readonly config: SpotConfig;
43
+ readonly options: SolveOptions;
44
+ /**
45
+ * What the preset changed against the config it was given: the sizes
46
+ * `thinSizes` cut (see `SpotConfig.thinSizes` for what it keeps), the
47
+ * streets that lost any, the storage, engine and sampling switched on,
48
+ * and a `compactStorage` dropped for 16 bits.
49
+ */
50
+ readonly changes: PresetChanges;
51
+ }
52
+ /**
53
+ * A preset for a tree too big for memory, opt-in (issue #102): the
54
+ * combination of memory levers that measured best on the round's target T.
55
+ *
56
+ * const { config, options } = largeTree({ ...myConfig, maxBytes: 2.5 * 2 ** 30 }, { options: { workers: 4 } });
57
+ * const run = createRun(defineSpot(config), options);
58
+ *
59
+ * It returns `config` with `thinSizes` set, and solve options for 16-bit
60
+ * storage on the WebAssembly engine (`precision: 'i16'`, `wasmStorage`) with
61
+ * `chanceSampling` at its defaults; `storage: 'compact'` sets
62
+ * `compactStorage` instead of 16 bits. Nothing else is changed, and a solve
63
+ * that does not use it is unchanged. `maxBytes` stays the config's: the
64
+ * browser build's default, 512 MiB, is too little for T, so raise it there.
65
+ *
66
+ * Measured on T (As Kh 7d, pot 55, stack 975, 0.33 / 0.75 / 1.5 pot, a
67
+ * 0.75 raise and all-in, three aggressions, every street: 17.2 GiB at 32
68
+ * bits) on 4 threads. Against `thinSizes` alone at 32 bits solved for 600
69
+ * iterations (`bench/combos/`), the root play settled within 0.02% of pot
70
+ * of its final value in 155 s and ended at that reference's own floor
71
+ * (0.031% of pot lost at the root, 0.020% after a check), per-hand root EVs
72
+ * 0.05% of pot off on average. Storage 1.47 GiB, peak 1.97 GiB in Node
73
+ * and 1.87 GiB in Chromium, 0.25 and 0.23 s an iteration. Compact: 168 s,
74
+ * the same root play, 1.48 / 1.42 GiB peak. What `thinSizes` itself costs
75
+ * against the full tree was measured on T₀ against an exact solve: root
76
+ * play 0.04% of pot, per-hand root EVs about 1.5% of pot off on average;
77
+ * and on seven napkinpoker spots against postflop-solver's EVs, 0.04% of pot
78
+ * at OOP's root as for an exact solve, but 0.34% against 0.16% for IP's play
79
+ * after a check, whose 0.33 bet `thinSizes` cuts. See docs/APPROXIMATIONS.md for the combinations
80
+ * measured and left out.
81
+ */
82
+ declare function largeTree(config: SpotConfig, preset?: LargeTreeOptions): LargeTree;
83
+
84
+ export { type LargeTree, type LargeTreeOptions, PresetChanges, WasmStorage, largeTree, wasmStorage };