defuss-multicore 0.0.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/README.md +314 -0
- package/dist/index.cjs +601 -0
- package/dist/index.d.cts +162 -0
- package/dist/index.d.mts +162 -0
- package/dist/index.mjs +590 -0
- package/dist/pool-DhG3ybwo.mjs +151 -0
- package/dist/pool-DqNSYahJ.cjs +153 -0
- package/dist/pool-browser-BsBdOUwc.cjs +43 -0
- package/dist/pool-browser-IHMOtzRq.mjs +41 -0
- package/dist/pool-node-Chmgg6NZ.mjs +35 -0
- package/dist/pool-node-DKGzL78_.cjs +38 -0
- package/package.json +73 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/** Union of all typed array constructors supported for parallel ops */
|
|
2
|
+
type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array;
|
|
3
|
+
/** Any array-like container of numbers */
|
|
4
|
+
type NumericArray = number[] | TypedArray;
|
|
5
|
+
/** A matrix is an array of row vectors (each row is a NumericArray) */
|
|
6
|
+
type Matrix<T extends NumericArray = Float32Array> = T[];
|
|
7
|
+
/** Batch of vectors (same shape as Matrix — alias for clarity) */
|
|
8
|
+
type Vectors<T extends NumericArray = Float32Array> = T[];
|
|
9
|
+
interface PoolConfig {
|
|
10
|
+
/** Maximum number of workers in the pool (default: core count) */
|
|
11
|
+
maxWorkers?: number;
|
|
12
|
+
/** Kill idle workers after this many ms (default: 30 000) */
|
|
13
|
+
idleTimeoutMs?: number;
|
|
14
|
+
}
|
|
15
|
+
/** Abstract interface implemented by pool-browser and pool-node */
|
|
16
|
+
interface WorkerPool {
|
|
17
|
+
readonly size: number;
|
|
18
|
+
execute(args: unknown[], transfer?: ArrayBuffer[], signal?: AbortSignal): Promise<unknown>;
|
|
19
|
+
warmup(count?: number): void;
|
|
20
|
+
terminate(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
interface MulticoreOptions<R = unknown> {
|
|
23
|
+
/** Override core count (default: all available cores) */
|
|
24
|
+
cores?: number;
|
|
25
|
+
/** Combine partial results into a single value on the main thread */
|
|
26
|
+
reduce?: (a: R, b: R) => R;
|
|
27
|
+
/** Pre-spawn worker pool at definition time (default: lazy) */
|
|
28
|
+
eager?: boolean;
|
|
29
|
+
/** Minimum array length to use workers; below this, run on main thread (default: 1024) */
|
|
30
|
+
threshold?: number;
|
|
31
|
+
}
|
|
32
|
+
/** Per-call overrides passed as trailing argument */
|
|
33
|
+
interface CallOptions {
|
|
34
|
+
/** Override core count for this call */
|
|
35
|
+
cores?: number;
|
|
36
|
+
/** AbortSignal to cancel in-flight workers */
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
/** Force or disable Transferable detection (default: auto) */
|
|
39
|
+
transfer?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Dual interface: use as `PromiseLike` (await → R[]) or as `AsyncIterable`
|
|
43
|
+
* (for-await-of → yields each R as workers complete).
|
|
44
|
+
*/
|
|
45
|
+
interface ParallelResult<R> extends AsyncIterable<R>, PromiseLike<R[]> {
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Same as ParallelResult but when `reduce` is set, await resolves to a single R.
|
|
49
|
+
*/
|
|
50
|
+
interface ReducedParallelResult<R> extends AsyncIterable<R>, PromiseLike<R> {
|
|
51
|
+
}
|
|
52
|
+
/** The callable returned by `multicore(fn)` — same arg types, parallel return */
|
|
53
|
+
type ParallelFn<T extends (...args: unknown[]) => unknown> = (...args: [...Parameters<T>, CallOptions?]) => ParallelResult<ReturnType<T>>;
|
|
54
|
+
type ReducedParallelFn<T extends (...args: unknown[]) => unknown> = (...args: [...Parameters<T>, CallOptions?]) => ReducedParallelResult<ReturnType<T>>;
|
|
55
|
+
interface OpOptions extends CallOptions {
|
|
56
|
+
/** Force a specific unroll factor (default: auto-select by dimension) */
|
|
57
|
+
unroll?: 4 | 8 | 16;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns the number of available CPU cores.
|
|
62
|
+
* Browser: `navigator.hardwareConcurrency`
|
|
63
|
+
* Node: `os.cpus().length`
|
|
64
|
+
* Fallback: 4
|
|
65
|
+
*/
|
|
66
|
+
declare const getCoreCount: () => number;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Higher-order function that wraps a pure function for parallel execution.
|
|
70
|
+
*
|
|
71
|
+
* - Array/TypedArray args are auto-split across CPU cores
|
|
72
|
+
* - Scalar args are broadcast to every worker
|
|
73
|
+
* - Returns a function with the same arg types; return type becomes `ParallelResult<R>`
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const sum = multicore((chunk: number[]) => chunk.reduce((a, b) => a + b, 0));
|
|
78
|
+
* const results = await sum(largeArray); // number[]
|
|
79
|
+
* for await (const r of sum(largeArray)) { ... } // streaming
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare const multicore: <T extends (...args: any[]) => any>(fn: T, options?: MulticoreOptions<ReturnType<T>>) => ((...args: [...Parameters<T>, CallOptions?]) => ParallelResult<ReturnType<T>>);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Parallel `Array.prototype.map` — distributes work across CPU cores.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const doubled = await map(hugeArray, (x) => x * 2);
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare const map: <T, U>(array: T[], fn: (item: T) => U, options?: CallOptions) => ParallelResult<U[]>;
|
|
93
|
+
/**
|
|
94
|
+
* Parallel `Array.prototype.filter` — distributes predicate across CPU cores.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const evens = await filter(hugeArray, (x) => x % 2 === 0);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
declare const filter: <T>(array: T[], fn: (item: T) => boolean, options?: CallOptions) => ParallelResult<T[]>;
|
|
102
|
+
/**
|
|
103
|
+
* Parallel `Array.prototype.reduce` — each worker reduces its chunk,
|
|
104
|
+
* then main thread reduces the partial results.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const total = await reduce(hugeArray, (a, b) => a + b, 0);
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare const reduce: <T>(array: T[], fn: (a: T, b: T) => T, initial: T, options?: CallOptions) => Promise<T>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Batch dot product of vector pairs.
|
|
115
|
+
*
|
|
116
|
+
* For each i, computes `dotProduct(vectorsA[i], vectorsB[i])`.
|
|
117
|
+
* Uses JIT-optimized unrolled kernels with auto-selected factor (4/8/16).
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const similarities = await dotProduct(embeddingsA, embeddingsB);
|
|
122
|
+
* // similarities is Float32Array with one value per pair
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
declare const dotProduct: <T extends NumericArray>(vectorsA: Vectors<T>, vectorsB: Vectors<T>, options?: OpOptions) => Float32Array;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Matrix multiplication. C = A × B
|
|
129
|
+
*
|
|
130
|
+
* Row-parallel: each "worker" computes a slice of C's rows.
|
|
131
|
+
* Inner dot product uses the unrolled kernel (4/8/16) for the k-dimension.
|
|
132
|
+
*
|
|
133
|
+
* Complexity: O(M × N × K) with M = A.rows, K = A.cols = B.rows, N = B.cols.
|
|
134
|
+
*/
|
|
135
|
+
declare const matmul: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Element-wise matrix addition. C[i][j] = A[i][j] + B[i][j]
|
|
139
|
+
*
|
|
140
|
+
* Row-parallel with unrolled inner loop (factor 4/8/16).
|
|
141
|
+
* Output TypedArray constructor matches input type.
|
|
142
|
+
*/
|
|
143
|
+
declare const matadd: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Element-wise matrix subtraction. C[i][j] = A[i][j] - B[i][j]
|
|
147
|
+
*
|
|
148
|
+
* Row-parallel with unrolled inner loop (factor 4/8/16).
|
|
149
|
+
* Output TypedArray constructor matches input type.
|
|
150
|
+
*/
|
|
151
|
+
declare const matsub: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Element-wise matrix division. C[i][j] = A[i][j] / B[i][j]
|
|
155
|
+
*
|
|
156
|
+
* Row-parallel with unrolled inner loop (factor 4/8/16).
|
|
157
|
+
* Output TypedArray constructor matches input type.
|
|
158
|
+
*/
|
|
159
|
+
declare const matdiv: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
|
|
160
|
+
|
|
161
|
+
export { dotProduct, filter, getCoreCount as getPoolSize, map, matadd, matdiv, matmul, matsub, multicore, reduce };
|
|
162
|
+
export type { CallOptions, Matrix, MulticoreOptions, NumericArray, OpOptions, ParallelFn, ParallelResult, PoolConfig, ReducedParallelFn, ReducedParallelResult, TypedArray, Vectors, WorkerPool };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/** Union of all typed array constructors supported for parallel ops */
|
|
2
|
+
type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array;
|
|
3
|
+
/** Any array-like container of numbers */
|
|
4
|
+
type NumericArray = number[] | TypedArray;
|
|
5
|
+
/** A matrix is an array of row vectors (each row is a NumericArray) */
|
|
6
|
+
type Matrix<T extends NumericArray = Float32Array> = T[];
|
|
7
|
+
/** Batch of vectors (same shape as Matrix — alias for clarity) */
|
|
8
|
+
type Vectors<T extends NumericArray = Float32Array> = T[];
|
|
9
|
+
interface PoolConfig {
|
|
10
|
+
/** Maximum number of workers in the pool (default: core count) */
|
|
11
|
+
maxWorkers?: number;
|
|
12
|
+
/** Kill idle workers after this many ms (default: 30 000) */
|
|
13
|
+
idleTimeoutMs?: number;
|
|
14
|
+
}
|
|
15
|
+
/** Abstract interface implemented by pool-browser and pool-node */
|
|
16
|
+
interface WorkerPool {
|
|
17
|
+
readonly size: number;
|
|
18
|
+
execute(args: unknown[], transfer?: ArrayBuffer[], signal?: AbortSignal): Promise<unknown>;
|
|
19
|
+
warmup(count?: number): void;
|
|
20
|
+
terminate(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
interface MulticoreOptions<R = unknown> {
|
|
23
|
+
/** Override core count (default: all available cores) */
|
|
24
|
+
cores?: number;
|
|
25
|
+
/** Combine partial results into a single value on the main thread */
|
|
26
|
+
reduce?: (a: R, b: R) => R;
|
|
27
|
+
/** Pre-spawn worker pool at definition time (default: lazy) */
|
|
28
|
+
eager?: boolean;
|
|
29
|
+
/** Minimum array length to use workers; below this, run on main thread (default: 1024) */
|
|
30
|
+
threshold?: number;
|
|
31
|
+
}
|
|
32
|
+
/** Per-call overrides passed as trailing argument */
|
|
33
|
+
interface CallOptions {
|
|
34
|
+
/** Override core count for this call */
|
|
35
|
+
cores?: number;
|
|
36
|
+
/** AbortSignal to cancel in-flight workers */
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
/** Force or disable Transferable detection (default: auto) */
|
|
39
|
+
transfer?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Dual interface: use as `PromiseLike` (await → R[]) or as `AsyncIterable`
|
|
43
|
+
* (for-await-of → yields each R as workers complete).
|
|
44
|
+
*/
|
|
45
|
+
interface ParallelResult<R> extends AsyncIterable<R>, PromiseLike<R[]> {
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Same as ParallelResult but when `reduce` is set, await resolves to a single R.
|
|
49
|
+
*/
|
|
50
|
+
interface ReducedParallelResult<R> extends AsyncIterable<R>, PromiseLike<R> {
|
|
51
|
+
}
|
|
52
|
+
/** The callable returned by `multicore(fn)` — same arg types, parallel return */
|
|
53
|
+
type ParallelFn<T extends (...args: unknown[]) => unknown> = (...args: [...Parameters<T>, CallOptions?]) => ParallelResult<ReturnType<T>>;
|
|
54
|
+
type ReducedParallelFn<T extends (...args: unknown[]) => unknown> = (...args: [...Parameters<T>, CallOptions?]) => ReducedParallelResult<ReturnType<T>>;
|
|
55
|
+
interface OpOptions extends CallOptions {
|
|
56
|
+
/** Force a specific unroll factor (default: auto-select by dimension) */
|
|
57
|
+
unroll?: 4 | 8 | 16;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns the number of available CPU cores.
|
|
62
|
+
* Browser: `navigator.hardwareConcurrency`
|
|
63
|
+
* Node: `os.cpus().length`
|
|
64
|
+
* Fallback: 4
|
|
65
|
+
*/
|
|
66
|
+
declare const getCoreCount: () => number;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Higher-order function that wraps a pure function for parallel execution.
|
|
70
|
+
*
|
|
71
|
+
* - Array/TypedArray args are auto-split across CPU cores
|
|
72
|
+
* - Scalar args are broadcast to every worker
|
|
73
|
+
* - Returns a function with the same arg types; return type becomes `ParallelResult<R>`
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const sum = multicore((chunk: number[]) => chunk.reduce((a, b) => a + b, 0));
|
|
78
|
+
* const results = await sum(largeArray); // number[]
|
|
79
|
+
* for await (const r of sum(largeArray)) { ... } // streaming
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare const multicore: <T extends (...args: any[]) => any>(fn: T, options?: MulticoreOptions<ReturnType<T>>) => ((...args: [...Parameters<T>, CallOptions?]) => ParallelResult<ReturnType<T>>);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Parallel `Array.prototype.map` — distributes work across CPU cores.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const doubled = await map(hugeArray, (x) => x * 2);
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare const map: <T, U>(array: T[], fn: (item: T) => U, options?: CallOptions) => ParallelResult<U[]>;
|
|
93
|
+
/**
|
|
94
|
+
* Parallel `Array.prototype.filter` — distributes predicate across CPU cores.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const evens = await filter(hugeArray, (x) => x % 2 === 0);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
declare const filter: <T>(array: T[], fn: (item: T) => boolean, options?: CallOptions) => ParallelResult<T[]>;
|
|
102
|
+
/**
|
|
103
|
+
* Parallel `Array.prototype.reduce` — each worker reduces its chunk,
|
|
104
|
+
* then main thread reduces the partial results.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const total = await reduce(hugeArray, (a, b) => a + b, 0);
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare const reduce: <T>(array: T[], fn: (a: T, b: T) => T, initial: T, options?: CallOptions) => Promise<T>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Batch dot product of vector pairs.
|
|
115
|
+
*
|
|
116
|
+
* For each i, computes `dotProduct(vectorsA[i], vectorsB[i])`.
|
|
117
|
+
* Uses JIT-optimized unrolled kernels with auto-selected factor (4/8/16).
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const similarities = await dotProduct(embeddingsA, embeddingsB);
|
|
122
|
+
* // similarities is Float32Array with one value per pair
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
declare const dotProduct: <T extends NumericArray>(vectorsA: Vectors<T>, vectorsB: Vectors<T>, options?: OpOptions) => Float32Array;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Matrix multiplication. C = A × B
|
|
129
|
+
*
|
|
130
|
+
* Row-parallel: each "worker" computes a slice of C's rows.
|
|
131
|
+
* Inner dot product uses the unrolled kernel (4/8/16) for the k-dimension.
|
|
132
|
+
*
|
|
133
|
+
* Complexity: O(M × N × K) with M = A.rows, K = A.cols = B.rows, N = B.cols.
|
|
134
|
+
*/
|
|
135
|
+
declare const matmul: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Element-wise matrix addition. C[i][j] = A[i][j] + B[i][j]
|
|
139
|
+
*
|
|
140
|
+
* Row-parallel with unrolled inner loop (factor 4/8/16).
|
|
141
|
+
* Output TypedArray constructor matches input type.
|
|
142
|
+
*/
|
|
143
|
+
declare const matadd: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Element-wise matrix subtraction. C[i][j] = A[i][j] - B[i][j]
|
|
147
|
+
*
|
|
148
|
+
* Row-parallel with unrolled inner loop (factor 4/8/16).
|
|
149
|
+
* Output TypedArray constructor matches input type.
|
|
150
|
+
*/
|
|
151
|
+
declare const matsub: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Element-wise matrix division. C[i][j] = A[i][j] / B[i][j]
|
|
155
|
+
*
|
|
156
|
+
* Row-parallel with unrolled inner loop (factor 4/8/16).
|
|
157
|
+
* Output TypedArray constructor matches input type.
|
|
158
|
+
*/
|
|
159
|
+
declare const matdiv: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
|
|
160
|
+
|
|
161
|
+
export { dotProduct, filter, getCoreCount as getPoolSize, map, matadd, matdiv, matmul, matsub, multicore, reduce };
|
|
162
|
+
export type { CallOptions, Matrix, MulticoreOptions, NumericArray, OpOptions, ParallelFn, ParallelResult, PoolConfig, ReducedParallelFn, ReducedParallelResult, TypedArray, Vectors, WorkerPool };
|