defuss-multicore 0.0.3 → 0.1.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/LICENSE +22 -0
- package/README.md +144 -28
- package/dist/index-BxtfU1K8.cjs +952 -0
- package/dist/index-XVLcdKzC.mjs +938 -0
- package/dist/index.cjs +18 -619
- package/dist/index.d.cts +65 -103
- package/dist/index.d.mts +65 -103
- package/dist/index.mjs +2 -610
- package/dist/pool-browser-DkpTlaVd.mjs +41 -0
- package/dist/pool-browser-nGIRBxcR.cjs +43 -0
- package/dist/{pool-node-DUsc5lQk.cjs → pool-node-BQjchaw0.cjs} +6 -4
- package/dist/{pool-node-CmCaEceF.mjs → pool-node-D7dWSlZP.mjs} +5 -3
- package/dist/transfer-B05kHQLB.cjs +15 -0
- package/dist/transfer-DPiKlTfq.mjs +12 -0
- package/dist/worker-CneL12oi.d.cts +108 -0
- package/dist/worker-CneL12oi.d.mts +108 -0
- package/dist/worker.cjs +58 -0
- package/dist/worker.d.cts +2 -0
- package/dist/worker.d.mts +2 -0
- package/dist/worker.mjs +56 -0
- package/package.json +34 -10
- package/dist/pool-DhG3ybwo.mjs +0 -151
- package/dist/pool-DqNSYahJ.cjs +0 -153
- package/dist/pool-browser-BsBdOUwc.cjs +0 -43
- package/dist/pool-browser-IHMOtzRq.mjs +0 -41
package/dist/index.mjs
CHANGED
|
@@ -1,610 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const warnIfClosure = (fn) => {
|
|
5
|
-
const src = fn.toString();
|
|
6
|
-
const bodyMatch = src.match(/\{([\s\S]*)\}$/);
|
|
7
|
-
if (!bodyMatch) return;
|
|
8
|
-
const body = bodyMatch[1];
|
|
9
|
-
if (/\bthis\b/.test(body) && src.startsWith("(")) {
|
|
10
|
-
console.warn(
|
|
11
|
-
"[defuss-multicore] Warning: function references `this` which will be undefined in a worker context."
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
const serializeFunction = (fn) => {
|
|
16
|
-
warnIfClosure(fn);
|
|
17
|
-
const fnString = fn.toString();
|
|
18
|
-
return `
|
|
19
|
-
'use strict';
|
|
20
|
-
|
|
21
|
-
const __fn = ${fnString};
|
|
22
|
-
let __nodeParentPort = null;
|
|
23
|
-
|
|
24
|
-
const __isTypedArray = (v) =>
|
|
25
|
-
ArrayBuffer.isView(v) && !(v instanceof DataView);
|
|
26
|
-
|
|
27
|
-
const __getTransferables = (value) => {
|
|
28
|
-
if (__isTypedArray(value)) return [value.buffer];
|
|
29
|
-
if (value instanceof ArrayBuffer) return [value];
|
|
30
|
-
if (Array.isArray(value)) {
|
|
31
|
-
const transfers = [];
|
|
32
|
-
for (let i = 0; i < value.length; i++) {
|
|
33
|
-
if (__isTypedArray(value[i])) transfers.push(value[i].buffer);
|
|
34
|
-
else if (value[i] instanceof ArrayBuffer) transfers.push(value[i]);
|
|
35
|
-
}
|
|
36
|
-
return transfers;
|
|
37
|
-
}
|
|
38
|
-
return [];
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const __dispatch = (msg, transfer) => {
|
|
42
|
-
if (typeof postMessage === 'function') {
|
|
43
|
-
postMessage(msg, transfer ?? []);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (__nodeParentPort) {
|
|
48
|
-
__nodeParentPort.postMessage(msg, transfer ?? []);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
throw new Error('Node worker parentPort is not ready.');
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const __handleMessage = (data) => {
|
|
56
|
-
if (data.type === 'execute') {
|
|
57
|
-
try {
|
|
58
|
-
const result = __fn(...data.args);
|
|
59
|
-
const transfer = __getTransferables(result);
|
|
60
|
-
const msg = { type: 'result', id: data.id, value: result };
|
|
61
|
-
__dispatch(msg, transfer);
|
|
62
|
-
} catch (err) {
|
|
63
|
-
const msg = {
|
|
64
|
-
type: 'error',
|
|
65
|
-
id: data.id,
|
|
66
|
-
error: err instanceof Error ? err.message : String(err),
|
|
67
|
-
stack: err instanceof Error ? err.stack : undefined,
|
|
68
|
-
};
|
|
69
|
-
__dispatch(msg);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
// 'abort' type is a no-op; the caller terminates the worker externally
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// Browser Web Worker
|
|
76
|
-
if (typeof self !== 'undefined' && typeof self.onmessage !== 'undefined') {
|
|
77
|
-
self.onmessage = (e) => __handleMessage(e.data);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Node.js worker_threads
|
|
81
|
-
if (typeof postMessage !== 'function') {
|
|
82
|
-
import('node:worker_threads')
|
|
83
|
-
.then(({ parentPort }) => {
|
|
84
|
-
__nodeParentPort = parentPort ?? null;
|
|
85
|
-
if (__nodeParentPort) {
|
|
86
|
-
__nodeParentPort.on('message', __handleMessage);
|
|
87
|
-
}
|
|
88
|
-
})
|
|
89
|
-
.catch(() => {
|
|
90
|
-
// Not in worker_threads context - ignore
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
`.trim();
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const partitionArgs = (args, cores) => {
|
|
97
|
-
const perWorker = Array.from({ length: cores }, () => []);
|
|
98
|
-
for (const arg of args) {
|
|
99
|
-
if (Array.isArray(arg)) {
|
|
100
|
-
const chunks = splitArray(arg, cores);
|
|
101
|
-
for (let w = 0; w < cores; w++) {
|
|
102
|
-
perWorker[w].push(chunks[w]);
|
|
103
|
-
}
|
|
104
|
-
} else if (isTypedArray(arg)) {
|
|
105
|
-
const chunks = splitTypedArray(arg, cores);
|
|
106
|
-
for (let w = 0; w < cores; w++) {
|
|
107
|
-
perWorker[w].push(chunks[w]);
|
|
108
|
-
}
|
|
109
|
-
} else {
|
|
110
|
-
for (let w = 0; w < cores; w++) {
|
|
111
|
-
perWorker[w].push(arg);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return perWorker;
|
|
116
|
-
};
|
|
117
|
-
const splitArray = (arr, n) => {
|
|
118
|
-
const len = arr.length;
|
|
119
|
-
const base = Math.floor(len / n);
|
|
120
|
-
const remainder = len % n;
|
|
121
|
-
const chunks = [];
|
|
122
|
-
let offset = 0;
|
|
123
|
-
for (let i = 0; i < n; i++) {
|
|
124
|
-
const size = base + (i < remainder ? 1 : 0);
|
|
125
|
-
chunks.push(arr.slice(offset, offset + size));
|
|
126
|
-
offset += size;
|
|
127
|
-
}
|
|
128
|
-
return chunks;
|
|
129
|
-
};
|
|
130
|
-
const splitTypedArray = (arr, n) => {
|
|
131
|
-
const len = arr.length;
|
|
132
|
-
const base = Math.floor(len / n);
|
|
133
|
-
const remainder = len % n;
|
|
134
|
-
const chunks = [];
|
|
135
|
-
let offset = 0;
|
|
136
|
-
for (let i = 0; i < n; i++) {
|
|
137
|
-
const size = base + (i < remainder ? 1 : 0);
|
|
138
|
-
chunks.push(arr.subarray(offset, offset + size));
|
|
139
|
-
offset += size;
|
|
140
|
-
}
|
|
141
|
-
return chunks;
|
|
142
|
-
};
|
|
143
|
-
const shouldFallback = (args, threshold) => {
|
|
144
|
-
let maxLen = 0;
|
|
145
|
-
for (const arg of args) {
|
|
146
|
-
if (Array.isArray(arg)) {
|
|
147
|
-
maxLen = Math.max(maxLen, arg.length);
|
|
148
|
-
} else if (isTypedArray(arg)) {
|
|
149
|
-
maxLen = Math.max(maxLen, arg.length);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return maxLen < threshold;
|
|
153
|
-
};
|
|
154
|
-
const detectTransferables = (args) => {
|
|
155
|
-
const buffers = [];
|
|
156
|
-
for (const arg of args) {
|
|
157
|
-
if (arg instanceof ArrayBuffer) {
|
|
158
|
-
buffers.push(arg);
|
|
159
|
-
} else if (isTypedArray(arg)) {
|
|
160
|
-
const ta = arg;
|
|
161
|
-
if (ta.byteOffset === 0 && ta.byteLength === ta.buffer.byteLength) {
|
|
162
|
-
buffers.push(ta.buffer);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return buffers;
|
|
167
|
-
};
|
|
168
|
-
const getCoreCount = () => {
|
|
169
|
-
if (typeof navigator !== "undefined" && navigator.hardwareConcurrency) {
|
|
170
|
-
return navigator.hardwareConcurrency;
|
|
171
|
-
}
|
|
172
|
-
const nodeProcess = typeof process !== "undefined" ? process : null;
|
|
173
|
-
try {
|
|
174
|
-
const getBuiltinModule = nodeProcess?.getBuiltinModule;
|
|
175
|
-
if (typeof getBuiltinModule === "function") {
|
|
176
|
-
const os = getBuiltinModule("node:os");
|
|
177
|
-
return os.cpus().length;
|
|
178
|
-
}
|
|
179
|
-
} catch {
|
|
180
|
-
}
|
|
181
|
-
return 4;
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
const isBrowser = () => typeof globalThis !== "undefined" && typeof globalThis.Worker === "function";
|
|
185
|
-
let createPoolFn = null;
|
|
186
|
-
const getPoolFactory = async () => {
|
|
187
|
-
if (createPoolFn) return createPoolFn;
|
|
188
|
-
if (isBrowser()) {
|
|
189
|
-
const { createBrowserPool } = await import('./pool-browser-IHMOtzRq.mjs');
|
|
190
|
-
createPoolFn = createBrowserPool;
|
|
191
|
-
} else {
|
|
192
|
-
try {
|
|
193
|
-
const [{ createNodePool }, { Worker }] = await Promise.all([
|
|
194
|
-
import('./pool-node-CmCaEceF.mjs'),
|
|
195
|
-
import('node:worker_threads')
|
|
196
|
-
]);
|
|
197
|
-
createPoolFn = (script, config) => createNodePool(script, Worker, config);
|
|
198
|
-
} catch {
|
|
199
|
-
throw new Error(
|
|
200
|
-
"[defuss-multicore] node:worker_threads not available. Are you running in Node.js?"
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return createPoolFn;
|
|
205
|
-
};
|
|
206
|
-
const multicore = (fn, options) => {
|
|
207
|
-
const defaultCores = options?.cores ?? getCoreCount();
|
|
208
|
-
const threshold = options?.threshold ?? 1024;
|
|
209
|
-
const reduceFn = options?.reduce;
|
|
210
|
-
const eager = options?.eager ?? false;
|
|
211
|
-
const script = serializeFunction(fn);
|
|
212
|
-
let pool = null;
|
|
213
|
-
const getPool = async () => {
|
|
214
|
-
if (!pool) {
|
|
215
|
-
const factory = await getPoolFactory();
|
|
216
|
-
pool = factory(script, { maxWorkers: defaultCores });
|
|
217
|
-
}
|
|
218
|
-
return pool;
|
|
219
|
-
};
|
|
220
|
-
if (eager) {
|
|
221
|
-
getPool().then((p) => p.warmup(defaultCores));
|
|
222
|
-
}
|
|
223
|
-
const parallelFn = (...rawArgs) => {
|
|
224
|
-
let callOpts;
|
|
225
|
-
let args;
|
|
226
|
-
if (rawArgs.length > 0 && isCallOptions(rawArgs[rawArgs.length - 1])) {
|
|
227
|
-
callOpts = rawArgs[rawArgs.length - 1];
|
|
228
|
-
args = rawArgs.slice(0, -1);
|
|
229
|
-
} else {
|
|
230
|
-
args = rawArgs;
|
|
231
|
-
}
|
|
232
|
-
const cores = callOpts?.cores ?? defaultCores;
|
|
233
|
-
const signal = callOpts?.signal;
|
|
234
|
-
const transferOpt = callOpts?.transfer;
|
|
235
|
-
if (shouldFallback(args, threshold)) {
|
|
236
|
-
return createMainThreadResult(fn, args, reduceFn);
|
|
237
|
-
}
|
|
238
|
-
const partitioned = partitionArgs(args, cores);
|
|
239
|
-
const shouldTransfer = transferOpt !== false;
|
|
240
|
-
const workerPromises = partitioned.map((workerArgs) => {
|
|
241
|
-
const transfer = shouldTransfer ? detectTransferables(workerArgs) : [];
|
|
242
|
-
return getPool().then((p) => p.execute(workerArgs, transfer, signal));
|
|
243
|
-
});
|
|
244
|
-
return createParallelResult(workerPromises, reduceFn);
|
|
245
|
-
};
|
|
246
|
-
return parallelFn;
|
|
247
|
-
};
|
|
248
|
-
const createParallelResult = (promises, reduceFn) => {
|
|
249
|
-
const result = {
|
|
250
|
-
// PromiseLike: await resolves to R[] (or R if reduce is set)
|
|
251
|
-
then(onfulfilled, onrejected) {
|
|
252
|
-
const collected = Promise.all(promises).then((results) => {
|
|
253
|
-
if (reduceFn && results.length > 0) {
|
|
254
|
-
return results.reduce(reduceFn);
|
|
255
|
-
}
|
|
256
|
-
return results;
|
|
257
|
-
});
|
|
258
|
-
return collected.then(onfulfilled, onrejected);
|
|
259
|
-
},
|
|
260
|
-
// AsyncIterable: yields results as workers complete
|
|
261
|
-
[Symbol.asyncIterator]() {
|
|
262
|
-
let index = 0;
|
|
263
|
-
return {
|
|
264
|
-
async next() {
|
|
265
|
-
if (index >= promises.length) {
|
|
266
|
-
return { done: true, value: void 0 };
|
|
267
|
-
}
|
|
268
|
-
const value = await promises[index++];
|
|
269
|
-
return { done: false, value };
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
return result;
|
|
275
|
-
};
|
|
276
|
-
const createMainThreadResult = (fn, args, reduceFn) => {
|
|
277
|
-
let resultValue;
|
|
278
|
-
let error = null;
|
|
279
|
-
try {
|
|
280
|
-
resultValue = fn(...args);
|
|
281
|
-
} catch (e) {
|
|
282
|
-
error = e instanceof Error ? e : new Error(String(e));
|
|
283
|
-
resultValue = void 0;
|
|
284
|
-
}
|
|
285
|
-
const result = {
|
|
286
|
-
then(onfulfilled, onrejected) {
|
|
287
|
-
if (error) {
|
|
288
|
-
return Promise.reject(error).then(onfulfilled, onrejected);
|
|
289
|
-
}
|
|
290
|
-
const val = reduceFn ? resultValue : [resultValue];
|
|
291
|
-
return Promise.resolve(val).then(onfulfilled, onrejected);
|
|
292
|
-
},
|
|
293
|
-
[Symbol.asyncIterator]() {
|
|
294
|
-
let yielded = false;
|
|
295
|
-
return {
|
|
296
|
-
async next() {
|
|
297
|
-
if (error) throw error;
|
|
298
|
-
if (yielded) return { done: true, value: void 0 };
|
|
299
|
-
yielded = true;
|
|
300
|
-
return { done: false, value: resultValue };
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
return result;
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
const map = (array, fn, options) => {
|
|
309
|
-
const fnStr = fn.toString();
|
|
310
|
-
const chunkFn = new Function(
|
|
311
|
-
"chunk",
|
|
312
|
-
`const __mapFn = ${fnStr}; return chunk.map(__mapFn);`
|
|
313
|
-
);
|
|
314
|
-
const parallel = multicore(chunkFn, { threshold: 1024, ...options });
|
|
315
|
-
const result = parallel(array, options);
|
|
316
|
-
return {
|
|
317
|
-
then(onfulfilled, onrejected) {
|
|
318
|
-
return result.then(
|
|
319
|
-
(chunks) => chunks.flat ? chunks.flat() : [].concat(...chunks),
|
|
320
|
-
void 0
|
|
321
|
-
).then(onfulfilled, onrejected);
|
|
322
|
-
},
|
|
323
|
-
[Symbol.asyncIterator]() {
|
|
324
|
-
return result[Symbol.asyncIterator]();
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
};
|
|
328
|
-
const filter = (array, fn, options) => {
|
|
329
|
-
const fnStr = fn.toString();
|
|
330
|
-
const chunkFn = new Function(
|
|
331
|
-
"chunk",
|
|
332
|
-
`const __filterFn = ${fnStr}; return chunk.filter(__filterFn);`
|
|
333
|
-
);
|
|
334
|
-
const parallel = multicore(chunkFn, { threshold: 1024, ...options });
|
|
335
|
-
const result = parallel(array, options);
|
|
336
|
-
return {
|
|
337
|
-
then(onfulfilled, onrejected) {
|
|
338
|
-
return result.then(
|
|
339
|
-
(chunks) => chunks.flat ? chunks.flat() : [].concat(...chunks),
|
|
340
|
-
void 0
|
|
341
|
-
).then(onfulfilled, onrejected);
|
|
342
|
-
},
|
|
343
|
-
[Symbol.asyncIterator]() {
|
|
344
|
-
return result[Symbol.asyncIterator]();
|
|
345
|
-
}
|
|
346
|
-
};
|
|
347
|
-
};
|
|
348
|
-
const reduce = (array, fn, initial, options) => {
|
|
349
|
-
const fnStr = fn.toString();
|
|
350
|
-
const chunkFn = new Function(
|
|
351
|
-
"chunk",
|
|
352
|
-
`const __reduceFn = ${fnStr}; return chunk.reduce(__reduceFn);`
|
|
353
|
-
);
|
|
354
|
-
const parallel = multicore(chunkFn, { threshold: 1024, ...options });
|
|
355
|
-
const result = parallel(array, options);
|
|
356
|
-
return result.then((partials) => {
|
|
357
|
-
return partials.reduce(fn, initial);
|
|
358
|
-
});
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
const selectUnrollFactor = (dims) => {
|
|
362
|
-
if (dims < 16) return 4;
|
|
363
|
-
if (dims < 64) return 8;
|
|
364
|
-
return 16;
|
|
365
|
-
};
|
|
366
|
-
const opFn = {
|
|
367
|
-
add: (a, b) => a + b,
|
|
368
|
-
sub: (a, b) => a - b,
|
|
369
|
-
mul: (a, b) => a * b,
|
|
370
|
-
div: (a, b) => a / b
|
|
371
|
-
};
|
|
372
|
-
const createElementWiseKernel = (op, factor) => {
|
|
373
|
-
const fn = opFn[op];
|
|
374
|
-
return (a, b, out, offset, length) => {
|
|
375
|
-
const end = offset + length;
|
|
376
|
-
const unrolledEnd = offset + length - length % factor;
|
|
377
|
-
let j = offset;
|
|
378
|
-
if (factor === 4) {
|
|
379
|
-
for (; j < unrolledEnd; j += 4) {
|
|
380
|
-
out[j] = fn(a[j], b[j]);
|
|
381
|
-
out[j + 1] = fn(a[j + 1], b[j + 1]);
|
|
382
|
-
out[j + 2] = fn(a[j + 2], b[j + 2]);
|
|
383
|
-
out[j + 3] = fn(a[j + 3], b[j + 3]);
|
|
384
|
-
}
|
|
385
|
-
} else if (factor === 8) {
|
|
386
|
-
for (; j < unrolledEnd; j += 8) {
|
|
387
|
-
out[j] = fn(a[j], b[j]);
|
|
388
|
-
out[j + 1] = fn(a[j + 1], b[j + 1]);
|
|
389
|
-
out[j + 2] = fn(a[j + 2], b[j + 2]);
|
|
390
|
-
out[j + 3] = fn(a[j + 3], b[j + 3]);
|
|
391
|
-
out[j + 4] = fn(a[j + 4], b[j + 4]);
|
|
392
|
-
out[j + 5] = fn(a[j + 5], b[j + 5]);
|
|
393
|
-
out[j + 6] = fn(a[j + 6], b[j + 6]);
|
|
394
|
-
out[j + 7] = fn(a[j + 7], b[j + 7]);
|
|
395
|
-
}
|
|
396
|
-
} else {
|
|
397
|
-
for (; j < unrolledEnd; j += 16) {
|
|
398
|
-
out[j] = fn(a[j], b[j]);
|
|
399
|
-
out[j + 1] = fn(a[j + 1], b[j + 1]);
|
|
400
|
-
out[j + 2] = fn(a[j + 2], b[j + 2]);
|
|
401
|
-
out[j + 3] = fn(a[j + 3], b[j + 3]);
|
|
402
|
-
out[j + 4] = fn(a[j + 4], b[j + 4]);
|
|
403
|
-
out[j + 5] = fn(a[j + 5], b[j + 5]);
|
|
404
|
-
out[j + 6] = fn(a[j + 6], b[j + 6]);
|
|
405
|
-
out[j + 7] = fn(a[j + 7], b[j + 7]);
|
|
406
|
-
out[j + 8] = fn(a[j + 8], b[j + 8]);
|
|
407
|
-
out[j + 9] = fn(a[j + 9], b[j + 9]);
|
|
408
|
-
out[j + 10] = fn(a[j + 10], b[j + 10]);
|
|
409
|
-
out[j + 11] = fn(a[j + 11], b[j + 11]);
|
|
410
|
-
out[j + 12] = fn(a[j + 12], b[j + 12]);
|
|
411
|
-
out[j + 13] = fn(a[j + 13], b[j + 13]);
|
|
412
|
-
out[j + 14] = fn(a[j + 14], b[j + 14]);
|
|
413
|
-
out[j + 15] = fn(a[j + 15], b[j + 15]);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
for (; j < end; j++) {
|
|
417
|
-
out[j] = fn(a[j], b[j]);
|
|
418
|
-
}
|
|
419
|
-
};
|
|
420
|
-
};
|
|
421
|
-
const createDotKernel = (factor) => {
|
|
422
|
-
return (a, b, dims) => {
|
|
423
|
-
const unrolledEnd = dims - dims % factor;
|
|
424
|
-
let j = 0;
|
|
425
|
-
if (factor === 4) {
|
|
426
|
-
let acc02 = 0, acc16 = 0, acc22 = 0, acc32 = 0;
|
|
427
|
-
for (; j < unrolledEnd; j += 4) {
|
|
428
|
-
acc02 += a[j] * b[j];
|
|
429
|
-
acc16 += a[j + 1] * b[j + 1];
|
|
430
|
-
acc22 += a[j + 2] * b[j + 2];
|
|
431
|
-
acc32 += a[j + 3] * b[j + 3];
|
|
432
|
-
}
|
|
433
|
-
let sum2 = acc02 + acc16 + acc22 + acc32;
|
|
434
|
-
for (; j < dims; j++) sum2 += a[j] * b[j];
|
|
435
|
-
return sum2;
|
|
436
|
-
}
|
|
437
|
-
if (factor === 8) {
|
|
438
|
-
let acc02 = 0, acc16 = 0, acc22 = 0, acc32 = 0;
|
|
439
|
-
let acc42 = 0, acc52 = 0, acc62 = 0, acc72 = 0;
|
|
440
|
-
for (; j < unrolledEnd; j += 8) {
|
|
441
|
-
acc02 += a[j] * b[j];
|
|
442
|
-
acc16 += a[j + 1] * b[j + 1];
|
|
443
|
-
acc22 += a[j + 2] * b[j + 2];
|
|
444
|
-
acc32 += a[j + 3] * b[j + 3];
|
|
445
|
-
acc42 += a[j + 4] * b[j + 4];
|
|
446
|
-
acc52 += a[j + 5] * b[j + 5];
|
|
447
|
-
acc62 += a[j + 6] * b[j + 6];
|
|
448
|
-
acc72 += a[j + 7] * b[j + 7];
|
|
449
|
-
}
|
|
450
|
-
let sum2 = acc02 + acc16 + (acc22 + acc32) + (acc42 + acc52) + (acc62 + acc72);
|
|
451
|
-
for (; j < dims; j++) sum2 += a[j] * b[j];
|
|
452
|
-
return sum2;
|
|
453
|
-
}
|
|
454
|
-
let acc0 = 0, acc1 = 0, acc2 = 0, acc3 = 0;
|
|
455
|
-
let acc4 = 0, acc5 = 0, acc6 = 0, acc7 = 0;
|
|
456
|
-
let acc8 = 0, acc9 = 0, acc10 = 0, acc11 = 0;
|
|
457
|
-
let acc12 = 0, acc13 = 0, acc14 = 0, acc15 = 0;
|
|
458
|
-
for (; j < unrolledEnd; j += 16) {
|
|
459
|
-
acc0 += a[j] * b[j];
|
|
460
|
-
acc1 += a[j + 1] * b[j + 1];
|
|
461
|
-
acc2 += a[j + 2] * b[j + 2];
|
|
462
|
-
acc3 += a[j + 3] * b[j + 3];
|
|
463
|
-
acc4 += a[j + 4] * b[j + 4];
|
|
464
|
-
acc5 += a[j + 5] * b[j + 5];
|
|
465
|
-
acc6 += a[j + 6] * b[j + 6];
|
|
466
|
-
acc7 += a[j + 7] * b[j + 7];
|
|
467
|
-
acc8 += a[j + 8] * b[j + 8];
|
|
468
|
-
acc9 += a[j + 9] * b[j + 9];
|
|
469
|
-
acc10 += a[j + 10] * b[j + 10];
|
|
470
|
-
acc11 += a[j + 11] * b[j + 11];
|
|
471
|
-
acc12 += a[j + 12] * b[j + 12];
|
|
472
|
-
acc13 += a[j + 13] * b[j + 13];
|
|
473
|
-
acc14 += a[j + 14] * b[j + 14];
|
|
474
|
-
acc15 += a[j + 15] * b[j + 15];
|
|
475
|
-
}
|
|
476
|
-
let sum = acc0 + acc1 + acc2 + acc3 + (acc4 + acc5 + acc6 + acc7) + (acc8 + acc9 + acc10 + acc11) + (acc12 + acc13 + acc14 + acc15);
|
|
477
|
-
for (; j < dims; j++) sum += a[j] * b[j];
|
|
478
|
-
return sum;
|
|
479
|
-
};
|
|
480
|
-
};
|
|
481
|
-
const ewCache = /* @__PURE__ */ new Map();
|
|
482
|
-
const getElementWiseKernel = (op, factor) => {
|
|
483
|
-
const key = `${op}_${factor}`;
|
|
484
|
-
let kernel = ewCache.get(key);
|
|
485
|
-
if (!kernel) {
|
|
486
|
-
kernel = createElementWiseKernel(op, factor);
|
|
487
|
-
ewCache.set(key, kernel);
|
|
488
|
-
}
|
|
489
|
-
return kernel;
|
|
490
|
-
};
|
|
491
|
-
const dotCache = /* @__PURE__ */ new Map();
|
|
492
|
-
const getDotKernel = (factor) => {
|
|
493
|
-
let kernel = dotCache.get(factor);
|
|
494
|
-
if (!kernel) {
|
|
495
|
-
kernel = createDotKernel(factor);
|
|
496
|
-
dotCache.set(factor, kernel);
|
|
497
|
-
}
|
|
498
|
-
return kernel;
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
const dotProduct = (vectorsA, vectorsB, options) => {
|
|
502
|
-
const count = vectorsA.length;
|
|
503
|
-
if (count !== vectorsB.length) {
|
|
504
|
-
throw new RangeError(
|
|
505
|
-
`dotProduct: vectorsA.length (${count}) !== vectorsB.length (${vectorsB.length})`
|
|
506
|
-
);
|
|
507
|
-
}
|
|
508
|
-
if (count === 0) return new Float32Array(0);
|
|
509
|
-
const dims = vectorsA[0].length;
|
|
510
|
-
const factor = options?.unroll ?? selectUnrollFactor(dims);
|
|
511
|
-
const kernel = getDotKernel(factor);
|
|
512
|
-
const results = new Float32Array(count);
|
|
513
|
-
for (let i = 0; i < count; i++) {
|
|
514
|
-
results[i] = kernel(vectorsA[i], vectorsB[i], dims);
|
|
515
|
-
}
|
|
516
|
-
return results;
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
const matmul = (A, B, options) => {
|
|
520
|
-
const M = A.length;
|
|
521
|
-
if (M === 0) return [];
|
|
522
|
-
const K = A[0].length;
|
|
523
|
-
const bRows = B.length;
|
|
524
|
-
if (K !== bRows) {
|
|
525
|
-
throw new RangeError(
|
|
526
|
-
`matmul: A.cols (${K}) !== B.rows (${bRows})`
|
|
527
|
-
);
|
|
528
|
-
}
|
|
529
|
-
const N = B[0].length;
|
|
530
|
-
const factor = options?.unroll ?? selectUnrollFactor(K);
|
|
531
|
-
const dotKernel = getDotKernel(factor);
|
|
532
|
-
const Ctor = A[0].constructor;
|
|
533
|
-
const BT = new Array(N);
|
|
534
|
-
for (let col = 0; col < N; col++) {
|
|
535
|
-
const column = new B[0].constructor(K);
|
|
536
|
-
for (let row = 0; row < K; row++) {
|
|
537
|
-
column[row] = B[row][col];
|
|
538
|
-
}
|
|
539
|
-
BT[col] = column;
|
|
540
|
-
}
|
|
541
|
-
const C = new Array(M);
|
|
542
|
-
for (let i = 0; i < M; i++) {
|
|
543
|
-
const row = new Ctor(N);
|
|
544
|
-
const aRow = A[i];
|
|
545
|
-
for (let j = 0; j < N; j++) {
|
|
546
|
-
row[j] = dotKernel(aRow, BT[j], K);
|
|
547
|
-
}
|
|
548
|
-
C[i] = row;
|
|
549
|
-
}
|
|
550
|
-
return C;
|
|
551
|
-
};
|
|
552
|
-
|
|
553
|
-
const matadd = (A, B, options) => {
|
|
554
|
-
const rows = A.length;
|
|
555
|
-
if (rows !== B.length) {
|
|
556
|
-
throw new RangeError(`matadd: row count mismatch (${rows} vs ${B.length})`);
|
|
557
|
-
}
|
|
558
|
-
if (rows === 0) return [];
|
|
559
|
-
const cols = A[0].length;
|
|
560
|
-
const factor = options?.unroll ?? selectUnrollFactor(cols);
|
|
561
|
-
const kernel = getElementWiseKernel("add", factor);
|
|
562
|
-
const Ctor = A[0].constructor;
|
|
563
|
-
const C = new Array(rows);
|
|
564
|
-
for (let i = 0; i < rows; i++) {
|
|
565
|
-
const out = new Ctor(cols);
|
|
566
|
-
kernel(A[i], B[i], out, 0, cols);
|
|
567
|
-
C[i] = out;
|
|
568
|
-
}
|
|
569
|
-
return C;
|
|
570
|
-
};
|
|
571
|
-
|
|
572
|
-
const matsub = (A, B, options) => {
|
|
573
|
-
const rows = A.length;
|
|
574
|
-
if (rows !== B.length) {
|
|
575
|
-
throw new RangeError(`matsub: row count mismatch (${rows} vs ${B.length})`);
|
|
576
|
-
}
|
|
577
|
-
if (rows === 0) return [];
|
|
578
|
-
const cols = A[0].length;
|
|
579
|
-
const factor = options?.unroll ?? selectUnrollFactor(cols);
|
|
580
|
-
const kernel = getElementWiseKernel("sub", factor);
|
|
581
|
-
const Ctor = A[0].constructor;
|
|
582
|
-
const C = new Array(rows);
|
|
583
|
-
for (let i = 0; i < rows; i++) {
|
|
584
|
-
const out = new Ctor(cols);
|
|
585
|
-
kernel(A[i], B[i], out, 0, cols);
|
|
586
|
-
C[i] = out;
|
|
587
|
-
}
|
|
588
|
-
return C;
|
|
589
|
-
};
|
|
590
|
-
|
|
591
|
-
const matdiv = (A, B, options) => {
|
|
592
|
-
const rows = A.length;
|
|
593
|
-
if (rows !== B.length) {
|
|
594
|
-
throw new RangeError(`matdiv: row count mismatch (${rows} vs ${B.length})`);
|
|
595
|
-
}
|
|
596
|
-
if (rows === 0) return [];
|
|
597
|
-
const cols = A[0].length;
|
|
598
|
-
const factor = options?.unroll ?? selectUnrollFactor(cols);
|
|
599
|
-
const kernel = getElementWiseKernel("div", factor);
|
|
600
|
-
const Ctor = A[0].constructor;
|
|
601
|
-
const C = new Array(rows);
|
|
602
|
-
for (let i = 0; i < rows; i++) {
|
|
603
|
-
const out = new Ctor(cols);
|
|
604
|
-
kernel(A[i], B[i], out, 0, cols);
|
|
605
|
-
C[i] = out;
|
|
606
|
-
}
|
|
607
|
-
return C;
|
|
608
|
-
};
|
|
609
|
-
|
|
610
|
-
export { dotProduct, filter, getCoreCount as getPoolSize, map, matadd, matdiv, matmul, matsub, multicore, reduce };
|
|
1
|
+
export { a as createPool, d as dotProduct, f as filter, g as getPoolSize, m as map, b as matadd, e as matdiv, h as matmul, i as matsub, j as multicore, r as reduce, k as releaseArrayWorkers } from './index-XVLcdKzC.mjs';
|
|
2
|
+
export { t as transfer } from './transfer-DPiKlTfq.mjs';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { c as createPool } from './index-XVLcdKzC.mjs';
|
|
2
|
+
import './transfer-DPiKlTfq.mjs';
|
|
3
|
+
|
|
4
|
+
const createBrowserPool = (script, config) => {
|
|
5
|
+
const urls = /* @__PURE__ */ new Map();
|
|
6
|
+
const hooks = {
|
|
7
|
+
createWorker(source) {
|
|
8
|
+
const url = URL.createObjectURL(new Blob([source], { type: "text/javascript" }));
|
|
9
|
+
try {
|
|
10
|
+
const worker = new Worker(url);
|
|
11
|
+
urls.set(worker, url);
|
|
12
|
+
return worker;
|
|
13
|
+
} catch (e) {
|
|
14
|
+
URL.revokeObjectURL(url);
|
|
15
|
+
throw e;
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
postMessage(worker, data, transfer) {
|
|
19
|
+
worker.postMessage(data, transfer);
|
|
20
|
+
},
|
|
21
|
+
terminateWorker(worker) {
|
|
22
|
+
worker.terminate();
|
|
23
|
+
const url = urls.get(worker);
|
|
24
|
+
if (url) URL.revokeObjectURL(url);
|
|
25
|
+
urls.delete(worker);
|
|
26
|
+
},
|
|
27
|
+
onMessage(worker, handler) {
|
|
28
|
+
worker.onmessage = (e) => handler(e.data);
|
|
29
|
+
},
|
|
30
|
+
onError(worker, handler) {
|
|
31
|
+
worker.onerror = (e) => {
|
|
32
|
+
e.preventDefault();
|
|
33
|
+
handler(new Error(e.message));
|
|
34
|
+
};
|
|
35
|
+
worker.onmessageerror = () => handler(new Error("Worker message could not be decoded"));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return createPool(script, config ?? {}, hooks);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { createBrowserPool };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./index-BxtfU1K8.cjs');
|
|
4
|
+
require('./transfer-B05kHQLB.cjs');
|
|
5
|
+
|
|
6
|
+
const createBrowserPool = (script, config) => {
|
|
7
|
+
const urls = /* @__PURE__ */ new Map();
|
|
8
|
+
const hooks = {
|
|
9
|
+
createWorker(source) {
|
|
10
|
+
const url = URL.createObjectURL(new Blob([source], { type: "text/javascript" }));
|
|
11
|
+
try {
|
|
12
|
+
const worker = new Worker(url);
|
|
13
|
+
urls.set(worker, url);
|
|
14
|
+
return worker;
|
|
15
|
+
} catch (e) {
|
|
16
|
+
URL.revokeObjectURL(url);
|
|
17
|
+
throw e;
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
postMessage(worker, data, transfer) {
|
|
21
|
+
worker.postMessage(data, transfer);
|
|
22
|
+
},
|
|
23
|
+
terminateWorker(worker) {
|
|
24
|
+
worker.terminate();
|
|
25
|
+
const url = urls.get(worker);
|
|
26
|
+
if (url) URL.revokeObjectURL(url);
|
|
27
|
+
urls.delete(worker);
|
|
28
|
+
},
|
|
29
|
+
onMessage(worker, handler) {
|
|
30
|
+
worker.onmessage = (e) => handler(e.data);
|
|
31
|
+
},
|
|
32
|
+
onError(worker, handler) {
|
|
33
|
+
worker.onerror = (e) => {
|
|
34
|
+
e.preventDefault();
|
|
35
|
+
handler(new Error(e.message));
|
|
36
|
+
};
|
|
37
|
+
worker.onmessageerror = () => handler(new Error("Worker message could not be decoded"));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return index.createPool(script, config ?? {}, hooks);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
exports.createBrowserPool = createBrowserPool;
|