Repository navigation
Expand file tree
/
Copy paththreadman.js
More file actions
365 lines (313 loc) · 12.6 KB
/
Copy paththreadman.js
File metadata and controls
365 lines (313 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
Copyright 2019 0KIMS association.
This file is part of wasmsnark (Web Assembly zkSnark Prover).
wasmsnark is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
wasmsnark is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with wasmsnark. If not, see <https://www.gnu.org/licenses/>.
*/
// const MEM_SIZE = 1000; // Memory size in 64K Pakes (512Mb)
const MEM_SIZE = 25; // Memory size in 64K Pakes (1600Kb)
import thread from "./threadman_thread.js";
import os from "os";
import Worker from "web-worker";
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject)=> {
this.reject = reject;
this.resolve = resolve;
});
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// WorkerSlot holds the native Worker and all per-worker state.
// Each call to startWorker() creates a fresh WorkerSlot instance.
// Message handlers close over the slot reference so that stale messages
// from a replaced worker are detected by a simple identity check
// (tm.pool[i] !== slot).
class WorkerSlot {
constructor(worker) {
this.worker = worker; // native Worker thread
this.initialized = false;
this.initializing= false;
this.working = false;
this.pendingDeferred = null;
this.onMsg = null; // stored so removeEventListener can be called on termination
this.onError = null;
}
}
let workerSource;
const threadStr = `(${thread.toString()})(self)`;
if(process.browser) {
if(globalThis?.Blob) {
const threadBytes= new TextEncoder().encode(threadStr);
const workerBlob = new Blob([threadBytes], { type: "application/javascript" }) ;
workerSource = URL.createObjectURL(workerBlob);
} else {
workerSource = "data:application/javascript;base64," + globalThis.btoa(threadStr);
}
} else {
workerSource = "data:application/javascript;base64," + Buffer.from(threadStr).toString("base64");
}
export default async function buildThreadManager(wasm, singleThread) {
const tm = new ThreadManager();
tm.memory = new WebAssembly.Memory({initial:MEM_SIZE});
tm.u8 = new Uint8Array(tm.memory.buffer);
tm.u32 = new Uint32Array(tm.memory.buffer);
const wasmModule = await WebAssembly.compile(wasm.code);
tm.instance = await WebAssembly.instantiate(wasmModule, {
env: {
"memory": tm.memory
}
});
if(process.browser && !globalThis?.Worker) {
singleThread = true;
}
tm.singleThread = singleThread;
tm.initalPFree = tm.u32[0]; // Save the Pointer to free space.
tm.pq = wasm.pq;
tm.pr = wasm.pr;
tm.pG1gen = wasm.pG1gen;
tm.pG1zero = wasm.pG1zero;
tm.pG2gen = wasm.pG2gen;
tm.pG2zero = wasm.pG2zero;
tm.pOneT = wasm.pOneT;
tm.code = wasm.code;
tm.wasmModule = wasmModule;
if (singleThread) {
tm.taskManager = thread();
await tm.taskManager([{
cmd: "INIT",
init: MEM_SIZE,
code: tm.code.slice()
}]);
tm.concurrency = 1;
} else {
// pool[i] is the active WorkerSlot at slot i, or null if the slot is empty.
tm.pool = [];
let concurrency = 2;
if (process.browser) {
if (typeof navigator === "object" && navigator.hardwareConcurrency) {
concurrency = navigator.hardwareConcurrency;
}
} else {
concurrency = os.cpus().length;
}
if(concurrency === 0){
concurrency = 2;
}
// Limit to 64 threads for memory reasons.
if (concurrency>64) concurrency=64;
tm.concurrency = concurrency;
}
return tm;
}
export class ThreadManager {
constructor() {
this.actionQueue = [];
this.oldPFree = 0;
}
// Build the message handler for a specific WorkerSlot.
// All state reads/writes go through `slot`; the stale check
// `tm.pool[slotIndex] !== slot` discards messages from replaced workers.
_makeOnMsg(slotIndex, slot) {
const tm = this;
return async function(e) {
const data = (e && e.data) ? e.data : e;
// Stale check: if pool[slotIndex] no longer points to this slot,
// the message is from a worker that was already replaced.
if (tm.pool[slotIndex] !== slot) {
if (data.status === "terminated") {
// Break the reference cycle so the slot and its WASM memory
// can be collected immediately rather than waiting for GC.
slot.worker.removeEventListener("message", slot.onMsg);
slot.worker.removeEventListener("error", slot.onError);
return;
}
if (!data.status && slot.working) {
// Stale task result: the slot was replaced (want_to_terminate raced
// with a task dispatch — pool[i] was nulled before the result came
// back). The result is still valid; resolve so the caller doesn't hang.
slot.working = false;
slot.pendingDeferred.resolve(data);
}
await tm.processWorks();
return;
}
if (data.error) {
slot.working = false;
slot.pendingDeferred.reject(new Error("Worker error: " + data.error));
if (slot.initializing) {
slot.initializing = false;
tm.pool[slotIndex] = null;
}
throw new Error("Worker error: " + data.error);
}
if (data.status) {
if (data.status === "initialized") {
slot.initializing = false;
slot.initialized = true;
} else if (data.status === "want_to_terminate") {
// 2-phase termination: the worker is idle and asking to close.
// Release the slot immediately so processWorks can fill it with a
// fresh worker if the queue needs one. The TERMINATE ack is sent
// to the old worker so it can close cleanly; its later "terminated"
// message will be stale (pool[slotIndex] !== slot) and ignored.
tm.pool[slotIndex] = null;
slot.worker.postMessage([{cmd: "TERMINATE"}]);
await tm.processWorks();
return;
} else if (data.status === "terminated") {
// Worker has fully closed. For the 2-phase path the slot was
// already nulled in want_to_terminate, so this message arrives
// stale and is handled above. For a direct TERMINATE
// (tm.terminate() at proof end) we clean up here.
slot.worker.removeEventListener("message", slot.onMsg);
slot.worker.removeEventListener("error", slot.onError);
tm.pool[slotIndex] = null;
if (slot.working) {
// Safety net: reject the pending deferred so the caller
// surfaces an error instead of hanging.
slot.pendingDeferred.reject(
new Error(`Worker at slot ${slotIndex} terminated unexpectedly while processing task`)
);
slot.working = false;
}
return;
}
// fall through for "initialized" so the INIT deferred is resolved below
}
slot.working = false;
slot.pendingDeferred.resolve(data);
await tm.processWorks();
};
}
_makeOnError(slotIndex, slot) {
const tm = this;
return function(e) {
if (tm.pool[slotIndex] === slot) {
slot.working = false;
slot.initialized = false;
if (slot.pendingDeferred) {
slot.pendingDeferred.reject(new Error("Worker error: " + e.message));
}
}
throw new Error("Worker error: " + e.message);
};
}
startWorker(slotIndex) {
const nativeWorker = new Worker(workerSource);
const slot = new WorkerSlot(nativeWorker);
this.pool[slotIndex] = slot;
slot.onMsg = this._makeOnMsg(slotIndex, slot);
slot.onError = this._makeOnError(slotIndex, slot);
nativeWorker.addEventListener("message", slot.onMsg);
nativeWorker.addEventListener("error", slot.onError);
slot.initializing = true;
// postAction sets slot.working = true synchronously before any await,
// so processWorks will not attempt to start this slot again.
this.postAction(slotIndex, [{
cmd: "INIT",
init: MEM_SIZE,
code: this.wasmModule,
}]).then(() => {
slot.initialized = true;
});
}
startSyncOp() {
if (this.oldPFree !== 0) throw new Error("Sync operation in progress");
this.oldPFree = this.u32[0];
}
endSyncOp() {
if (this.oldPFree === 0) throw new Error("No sync operation in progress");
this.u32[0] = this.oldPFree;
this.oldPFree = 0;
}
async postAction(slotIndex, e, transfers, _deferred) {
const slot = this.pool[slotIndex];
if (!slot || slot.working) {
throw new Error("Posting a job to a working worker");
}
slot.working = true;
slot.pendingDeferred = _deferred ? _deferred : new Deferred();
await slot.worker.postMessage(e, transfers);
return slot.pendingDeferred.promise;
}
async processWorks() {
// Dispatch queued tasks to ready workers.
for (let i = 0; i < this.concurrency && this.actionQueue.length > 0; i++) {
const slot = this.pool[i];
if (slot && slot.initialized && !slot.working) {
const work = this.actionQueue.shift();
await this.postAction(i, work.data, work.transfers, work.deferred);
}
}
// Start new workers for slots that need them.
if (this.actionQueue.length > 0) {
let initializingCount = 0;
for (let i = 0; i < this.concurrency; i++) {
const slot = this.pool[i];
if (slot) {
if (slot.initializing) initializingCount++;
// slot exists: skip whether initialized, initializing, or working
continue;
}
// slot is null: this slot is available to host a new worker
if (initializingCount >= this.actionQueue.length) break;
console.log(`Worker ${i} not initialized yet. Initializing...`);
initializingCount++;
this.startWorker(i);
}
}
}
async queueAction(actionData, transfers) {
const d = new Deferred();
if (this.singleThread) {
const res = this.taskManager(actionData);
d.resolve(res);
} else {
this.actionQueue.push({
data: actionData,
transfers: transfers,
deferred: d
});
await this.processWorks();
}
return d.promise;
}
resetMemory() {
this.u32[0] = this.initalPFree;
}
allocBuff(buff) {
const pointer = this.alloc(buff.byteLength);
this.setBuff(pointer, buff);
return pointer;
}
getBuff(pointer, length) {
return this.u8.slice(pointer, pointer + length);
}
setBuff(pointer, buffer) {
this.u8.set(new Uint8Array(buffer), pointer);
}
alloc(length) {
while (this.u32[0] & 3) this.u32[0]++; // Return always aligned pointers
const res = this.u32[0];
this.u32[0] += length;
return res;
}
async terminate() {
for (let i = 0; i < this.pool.length; i++) {
if (this.pool[i]) {
this.pool[i].worker.postMessage([{cmd: "TERMINATE"}]);
}
}
}
}