Skip to content

Commit

Permalink
Update to spawn workers for all logical cores and expose global funct…
Browse files Browse the repository at this point in the history
…ion to allow developer override
  • Loading branch information
jackryanservia committed Feb 22, 2024
1 parent a7ade0d commit 7b36887
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 48 deletions.
24 changes: 2 additions & 22 deletions js/node/node-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isMainThread, parentPort, workerData, Worker } from 'worker_threads';
import os from 'os';
import wasm_ from '../../compiled/_node_bindings/plonk_wasm.cjs';
import { fileURLToPath } from 'url';
import { workers } from '../../../lib/proof-system/workers.js';
let url = import.meta.url;
let filename = url !== undefined ? fileURLToPath(url) : __filename;

Expand Down Expand Up @@ -82,7 +83,7 @@ async function withThreadPool(run) {
async function initThreadPool() {
if (!isMainThread) return;
workersReady = new Promise((resolve) => (workersReadyResolve = resolve));
await wasm.initThreadPool(getEfficientNumWorkers(), filename);
await wasm.initThreadPool(workers.numWorkers ?? os.cpus().length, filename);
await workersReady;
workersReady = undefined;
}
Expand Down Expand Up @@ -126,24 +127,3 @@ async function terminateWorkers() {
() => (wasmWorkers = undefined)
);
}

// Return the most efficient number of workers for the platform we're running
// on. This is required because of an issue with Apple silicon that's outlined
// here: https://bugs.chromium.org/p/chromium/issues/detail?id=1228686
function getEfficientNumWorkers() {
let cpus = os.cpus();
let numCpus = cpus.length;
let cpuModel = cpus[0].model;

let numWorkers =
{
'Apple M1': 2,
'Apple M1 Pro': numCpus === 10 ? 3 : 2,
'Apple M1 Max': 3,
'Apple M1 Ultra': 7,
'Apple M2': 2,
'Apple M2 Pro': 2,
}[cpuModel] || numCpus - 1;

return numWorkers;
}
24 changes: 0 additions & 24 deletions js/web/num-workers.js

This file was deleted.

4 changes: 2 additions & 2 deletions js/web/web-backend.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import plonkWasm from '../../../web_bindings/plonk_wasm.js';
import { workerSpec } from './worker-spec.js';
import { getEfficientNumWorkers } from './num-workers.js';
import {
srcFromFunctionModule,
inlineWorker,
waitForMessage,
} from './worker-helpers.js';
import o1jsWebSrc from 'string:../../../web_bindings/o1js_web.bc.js';
import { workers } from '../../../lib/proof-system/workers.js';

export { initO1, withThreadPool };

Expand Down Expand Up @@ -55,7 +55,7 @@ async function withThreadPool(run) {
if (workerPromise === undefined)
throw Error('need to initialize worker first');
let worker = await workerPromise;
numWorkers ??= await getEfficientNumWorkers();
numWorkers ??= workers.numWorkers ?? navigator.hardwareConcurrency;
await workerCall(worker, 'initThreadPool', numWorkers);
let result;
try {
Expand Down

0 comments on commit 7b36887

Please sign in to comment.