@danielsimonjr/mathts-parallel 0.6.3 → 0.6.5

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,119 @@
1
+ /**
2
+ * Chunking Strategies for Parallel Operations
3
+ *
4
+ * Provides utilities for dividing large arrays into chunks
5
+ * for parallel processing across multiple workers.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ /**
10
+ * Result of a chunking operation
11
+ */
12
+ export interface ChunkResult<T> {
13
+ /** The chunked data */
14
+ chunks: T[];
15
+ /** Metadata about each chunk */
16
+ chunkInfo: ChunkInfo[];
17
+ /** Total number of elements */
18
+ totalElements: number;
19
+ /** Number of workers/chunks */
20
+ numChunks: number;
21
+ }
22
+ /**
23
+ * Information about a single chunk
24
+ */
25
+ export interface ChunkInfo {
26
+ /** Start index in the original array */
27
+ startIndex: number;
28
+ /** End index (exclusive) in the original array */
29
+ endIndex: number;
30
+ /** Length of this chunk */
31
+ length: number;
32
+ /** Chunk index */
33
+ chunkIndex: number;
34
+ }
35
+ /**
36
+ * Options for chunking operations
37
+ */
38
+ export interface ChunkOptions {
39
+ /** Minimum elements per chunk (default: 1000) */
40
+ minChunkSize?: number;
41
+ /** Maximum number of chunks (default: number of CPUs) */
42
+ maxChunks?: number;
43
+ /** Target chunk size (default: auto-calculated) */
44
+ targetChunkSize?: number;
45
+ /** Whether to balance chunk sizes evenly (default: true) */
46
+ balanced?: boolean;
47
+ }
48
+ /**
49
+ * Calculate optimal number of chunks for a given array size
50
+ *
51
+ * @param totalElements - Total number of elements
52
+ * @param options - Chunking options
53
+ * @returns Optimal number of chunks
54
+ */
55
+ export declare function calculateOptimalChunks(totalElements: number, options?: ChunkOptions): number;
56
+ /**
57
+ * Chunk a Float64Array into multiple smaller arrays
58
+ *
59
+ * @param data - The array to chunk
60
+ * @param options - Chunking options
61
+ * @returns Chunked data with metadata
62
+ */
63
+ export declare function chunkFloat64Array(data: Float64Array, options?: ChunkOptions): ChunkResult<Float64Array>;
64
+ /**
65
+ * Chunk a generic array into multiple smaller arrays
66
+ *
67
+ * @param data - The array to chunk
68
+ * @param options - Chunking options
69
+ * @returns Chunked data with metadata
70
+ */
71
+ export declare function chunkArray<T>(data: T[], options?: ChunkOptions): ChunkResult<T[]>;
72
+ /**
73
+ * Merge chunked results back into a single array
74
+ *
75
+ * @param chunks - Array of chunk results
76
+ * @returns Merged array
77
+ */
78
+ export declare function mergeFloat64Chunks(chunks: Float64Array[]): Float64Array;
79
+ /**
80
+ * Merge generic array chunks
81
+ *
82
+ * @param chunks - Array of chunk results
83
+ * @returns Merged array
84
+ */
85
+ export declare function mergeArrayChunks<T>(chunks: T[][]): T[];
86
+ /**
87
+ * Determine if an array should be parallelized based on size
88
+ *
89
+ * @param elementCount - Number of elements
90
+ * @param threshold - Minimum elements for parallelization (default: 10000)
91
+ * @returns Whether parallelization is recommended
92
+ */
93
+ export declare function shouldParallelize(elementCount: number, threshold?: number): boolean;
94
+ /**
95
+ * Calculate the memory size of a Float64Array in bytes
96
+ *
97
+ * @param data - The array
98
+ * @returns Size in bytes
99
+ */
100
+ export declare function memorySizeBytes(data: Float64Array): number;
101
+ /**
102
+ * Create a range partitioner for parallel iteration
103
+ *
104
+ * @param start - Start of range (inclusive)
105
+ * @param end - End of range (exclusive)
106
+ * @param options - Chunking options
107
+ * @returns Array of [start, end] pairs for each chunk
108
+ */
109
+ export declare function partitionRange(start: number, end: number, options?: ChunkOptions): Array<[number, number]>;
110
+ /**
111
+ * Create a 2D partition for matrix operations
112
+ *
113
+ * @param rows - Number of rows
114
+ * @param cols - Number of columns
115
+ * @param options - Chunking options
116
+ * @returns Array of [rowStart, rowEnd, colStart, colEnd] tuples
117
+ */
118
+ export declare function partition2D(rows: number, cols: number, options?: ChunkOptions): Array<[number, number, number, number]>;
119
+ //# sourceMappingURL=chunk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunk.d.ts","sourceRoot":"","sources":["../../src/strategies/ch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;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,CAKtD;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,MAAM,EAAE,SAAS,GAAE,MAAc,GAAG,OAAO,CAE1F;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,YAAY,GAAG,MAAM,CAE1D;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,YAAiB,GACzB,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAsBzB;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,YAAiB,GACzB,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAKzC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Parallel Strategies
3
+ *
4
+ * Strategies for chunking, threshold-based dispatch, and workload distribution.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ export { calculateOptimalChunks, chunkFloat64Array, chunkArray, mergeFloat64Chunks, mergeArrayChunks, shouldParallelize as shouldChunkParallelize, partitionRange, partition2D, type ChunkOptions, type ChunkResult, type ChunkInfo, } from './chunk.js';
9
+ export { ThresholdDispatcher, thresholdDispatcher, shouldParallelize, dispatch, calculateChunks, DEFAULT_THRESHOLDS, type ThresholdConfig, type OperationCategory, type ExecutionMode, type DispatchResult, } from './threshold.js';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/strategies/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EACL,sBAAsB,EACtB,iBAAiB,EACjB,UAAU,EACV,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,IAAI,sBAAsB,EAC3C,cAAc,EACd,WAAW,EACX,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,SAAS,GACf,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,QAAQ,EACR,eAAe,EACf,kBAAkB,EAClB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EAClB,KAAK,cAAc,GACpB,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Threshold-based Dispatch Strategy
3
+ *
4
+ * Automatically decides whether to use parallel or sequential execution
5
+ * based on data size, operation type, and system capabilities.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ import { ComputePool } from '../ComputePool.js';
10
+ /**
11
+ * Operation categories for threshold selection
12
+ */
13
+ export type OperationCategory = 'matmul' | 'elementwise' | 'reduce' | 'map' | 'sort' | 'decomposition' | 'general';
14
+ /**
15
+ * Threshold configuration for different operation types
16
+ */
17
+ export interface ThresholdConfig {
18
+ /** Minimum elements for parallel matmul */
19
+ matmul: number;
20
+ /** Minimum elements for parallel elementwise ops */
21
+ elementwise: number;
22
+ /** Minimum elements for parallel reductions */
23
+ reduce: number;
24
+ /** Minimum elements for parallel map */
25
+ map: number;
26
+ /** Minimum elements for parallel sort */
27
+ sort: number;
28
+ /** Minimum elements for parallel decomposition */
29
+ decomposition: number;
30
+ /** Default threshold for other operations */
31
+ general: number;
32
+ }
33
+ /**
34
+ * Default category-level thresholds.
35
+ *
36
+ * NOTE: `ComputePool.shouldParallelize` (op-level, {@link DEFAULT_THRESHOLD_BY_OP})
37
+ * is the authority the typed functions dispatch through. This category map is a
38
+ * coarser helper; where a category overlaps a tuned op (matmul) its value is
39
+ * derived from the canonical const so the two cannot diverge. The remaining
40
+ * categories (no single ComputePool op equivalent — many are 'never') keep
41
+ * heuristic defaults for compute-vs-memory-bound work.
42
+ */
43
+ export declare const DEFAULT_THRESHOLDS: ThresholdConfig;
44
+ /**
45
+ * Execution mode returned by threshold dispatch
46
+ */
47
+ export type ExecutionMode = 'parallel' | 'sequential';
48
+ /**
49
+ * Result of threshold dispatch decision
50
+ */
51
+ export interface DispatchResult {
52
+ /** Recommended execution mode */
53
+ mode: ExecutionMode;
54
+ /** Reason for the decision */
55
+ reason: string;
56
+ /** Threshold used for comparison */
57
+ threshold: number;
58
+ /** Actual element count */
59
+ elementCount: number;
60
+ }
61
+ /**
62
+ * Threshold-based Dispatch Manager
63
+ *
64
+ * Provides intelligent parallel vs sequential dispatch based on
65
+ * operation type, data size, and system state.
66
+ */
67
+ export declare class ThresholdDispatcher {
68
+ private thresholds;
69
+ private pool;
70
+ constructor(thresholds?: Partial<ThresholdConfig>, pool?: ComputePool);
71
+ /**
72
+ * Get threshold for a specific operation category
73
+ */
74
+ getThreshold(category: OperationCategory): number;
75
+ /**
76
+ * Update thresholds
77
+ */
78
+ setThresholds(thresholds: Partial<ThresholdConfig>): void;
79
+ /**
80
+ * Get current threshold configuration
81
+ */
82
+ getThresholds(): ThresholdConfig;
83
+ /**
84
+ * Determine execution mode based on operation and data size
85
+ */
86
+ dispatch(elementCount: number, category?: OperationCategory): DispatchResult;
87
+ /**
88
+ * Simple boolean check for parallel execution
89
+ */
90
+ shouldParallelize(elementCount: number, category?: OperationCategory): boolean;
91
+ /**
92
+ * Calculate optimal chunk count based on operation and data size
93
+ */
94
+ calculateChunks(elementCount: number, category?: OperationCategory): number;
95
+ }
96
+ /**
97
+ * Global threshold dispatcher instance
98
+ */
99
+ export declare const thresholdDispatcher: ThresholdDispatcher;
100
+ /**
101
+ * Convenience function to check if operation should be parallelized
102
+ */
103
+ export declare function shouldParallelize(elementCount: number, category?: OperationCategory): boolean;
104
+ /**
105
+ * Convenience function to get dispatch decision
106
+ */
107
+ export declare function dispatch(elementCount: number, category?: OperationCategory): DispatchResult;
108
+ /**
109
+ * Convenience function to calculate optimal chunk count
110
+ */
111
+ export declare function calculateChunks(elementCount: number, category?: OperationCategory): number;
112
+ //# sourceMappingURL=threshold.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"threshold.d.ts","sourceRoot":"","sources":["../../src/strategies/threshold.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAe,WAAW,EAA2B,MAAM,mBAAmB,CAAC;AAatF;;GAEG;AACH,MAAM,MAAM,iBAAiB,GACzB,QAAQ,GACR,aAAa,GACb,QAAQ,GACR,KAAK,GACL,MAAM,GACN,eAAe,GACf,SAAS,CAAC;AAEd;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,2CAA2C;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,oDAAoD;IACpD,WAAW,EAAE,MAAM,CAAC;IACpB,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;IACf,wCAAwC;IACxC,GAAG,EAAE,MAAM,CAAC;IACZ,yCAAyC;IACzC,IAAI,EAAE,MAAM,CAAC;IACb,kDAAkD;IAClD,aAAa,EAAE,MAAM,CAAC;IACtB,6CAA6C;IAC7C,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,EAAE,eAQhC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,UAAU,GAAG,YAAY,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,iCAAiC;IACjC,IAAI,EAAE,aAAa,CAAC;IACpB,8BAA8B;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,oCAAoC;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,2BAA2B;IAC3B,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;;;;GAKG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,UAAU,CAAkB;IACpC,OAAO,CAAC,IAAI,CAAc;IAE1B,YAAY,UAAU,GAAE,OAAO,CAAC,eAAe,CAAM,EAAE,IAAI,CAAC,EAAE,WAAW,EAGxE;IAED;;OAEG;IACH,YAAY,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM,CAEhD;IAED;;OAEG;IACH,aAAa,CAAC,UAAU,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG,IAAI,CAExD;IAED;;OAEG;IACH,aAAa,IAAI,eAAe,CAE/B;IAED;;OAEG;IACH,QAAQ,CAAC,YAAY,EAAE,MAAM,EAAE,QAAQ,GAAE,iBAA6B,GAAG,cAAc,CA0CtF;IAED;;OAEG;IACH,iBAAiB,CAAC,YAAY,EAAE,MAAM,EAAE,QAAQ,GAAE,iBAA6B,GAAG,OAAO,CAExF;IAED;;OAEG;IACH,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,QAAQ,GAAE,iBAA6B,GAAG,MAAM,CAiCrF;CACF;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,qBAA4B,CAAC;AAE7D;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,YAAY,EAAE,MAAM,EACpB,QAAQ,GAAE,iBAA6B,GACtC,OAAO,CAET;AAED;;GAEG;AACH,wBAAgB,QAAQ,CACtB,YAAY,EAAE,MAAM,EACpB,QAAQ,GAAE,iBAA6B,GACtC,cAAc,CAEhB;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,YAAY,EAAE,MAAM,EACpB,QAAQ,GAAE,iBAA6B,GACtC,MAAM,CAER"}
package/package.json CHANGED
@@ -1,58 +1,58 @@
1
- {
2
- "name": "@danielsimonjr/mathts-parallel",
3
- "version": "0.6.3",
4
- "description": "WebWorker parallelization for MathTS computations",
5
- "author": "Daniel Simon Jr.",
6
- "license": "MIT",
7
- "type": "module",
8
- "main": "./dist/index.js",
9
- "module": "./dist/index.js",
10
- "types": "./dist/index.d.ts",
11
- "exports": {
12
- ".": {
13
- "import": "./dist/index.js",
14
- "types": "./dist/index.d.ts"
15
- }
16
- },
17
- "files": [
18
- "dist",
19
- "README.md"
20
- ],
21
- "scripts": {
22
- "build": "tsup src/index.ts src/matrix.worker.ts --format esm --dts --clean",
23
- "dev": "tsup src/index.ts src/matrix.worker.ts --format esm --dts --watch",
24
- "test": "vitest run",
25
- "test:watch": "vitest",
26
- "test:coverage": "vitest run --coverage",
27
- "typecheck": "tsc --noEmit",
28
- "lint": "eslint src --ext .ts",
29
- "lint:fix": "eslint src --ext .ts --fix",
30
- "clean": "rm -rf dist",
31
- "build:prod": "tsup src/index.ts src/matrix.worker.ts --format esm --dts --clean --minify --treeshake"
32
- },
33
- "dependencies": {
34
- "@danielsimonjr/mathts-workerpool": "^0.2.1",
35
- "@danielsimonjr/mathts-core": "^0.13.0"
36
- },
37
- "devDependencies": {
38
- "@types/node": "^25.5.2",
39
- "tsup": "^8.0.0",
40
- "typescript": "^5.3.0",
41
- "vitest": "^4.1.5"
42
- },
43
- "publishConfig": {
44
- "access": "public"
45
- },
46
- "repository": {
47
- "type": "git",
48
- "url": "https://github.com/danielsimonjr/mathts",
49
- "directory": "parallel"
50
- },
51
- "keywords": [
52
- "math",
53
- "typescript",
54
- "parallel",
55
- "webworker",
56
- "worker-pool"
57
- ]
58
- }
1
+ {
2
+ "name": "@danielsimonjr/mathts-parallel",
3
+ "version": "0.6.5",
4
+ "description": "WebWorker parallelization for MathTS computations",
5
+ "author": "Daniel Simon Jr.",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "types": "./dist/index.d.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "README.md"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsup src/index.ts src/matrix.worker.ts --format esm --clean && tsc -p tsconfig.dts.json",
23
+ "dev": "tsup src/index.ts src/matrix.worker.ts --format esm --dts --watch",
24
+ "test": "bun test --isolate --timeout 30000",
25
+ "test:watch": "vitest",
26
+ "test:coverage": "bun test --isolate --timeout 30000 --coverage",
27
+ "typecheck": "tsc --noEmit",
28
+ "lint": "oxlint src",
29
+ "lint:fix": "oxlint src --fix",
30
+ "clean": "rm -rf dist",
31
+ "build:prod": "tsup src/index.ts src/matrix.worker.ts --format esm --dts --clean --minify --treeshake"
32
+ },
33
+ "dependencies": {
34
+ "@danielsimonjr/mathts-workerpool": "^0.2.2",
35
+ "@danielsimonjr/mathts-core": "^0.15.0"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^26.4.0",
39
+ "tsup": "^8.0.0",
40
+ "typescript": "^7.0.2",
41
+ "vitest": "^5.0.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "git+https://github.com/danielsimonjr/MathTS.git",
49
+ "directory": "parallel"
50
+ },
51
+ "keywords": [
52
+ "math",
53
+ "typescript",
54
+ "parallel",
55
+ "webworker",
56
+ "worker-pool"
57
+ ]
58
+ }