@danielsimonjr/mathts-matrix 0.7.1 → 0.7.3
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/dist/backends/Backend.d.ts +146 -0
- package/dist/backends/Backend.d.ts.map +1 -0
- package/dist/backends/BackendManager.d.ts +189 -0
- package/dist/backends/BackendManager.d.ts.map +1 -0
- package/dist/backends/GPUBackend.d.ts +128 -0
- package/dist/backends/GPUBackend.d.ts.map +1 -0
- package/dist/backends/GPUMatrixBackend.d.ts +123 -0
- package/dist/backends/GPUMatrixBackend.d.ts.map +1 -0
- package/dist/backends/JSBackend.d.ts +93 -0
- package/dist/backends/JSBackend.d.ts.map +1 -0
- package/dist/backends/ParallelBackend.d.ts +131 -0
- package/dist/backends/ParallelBackend.d.ts.map +1 -0
- package/dist/backends/WASMBackend.d.ts +156 -0
- package/dist/backends/WASMBackend.d.ts.map +1 -0
- package/dist/backends/WasmLoader.d.ts +326 -0
- package/dist/backends/WasmLoader.d.ts.map +1 -0
- package/dist/backends/gpu/BatchExecutor.d.ts +190 -0
- package/dist/backends/gpu/BatchExecutor.d.ts.map +1 -0
- package/dist/backends/gpu/Sync.d.ts +167 -0
- package/dist/backends/gpu/Sync.d.ts.map +1 -0
- package/dist/backends/gpu/builtin-shaders.d.ts +178 -0
- package/dist/backends/gpu/builtin-shaders.d.ts.map +1 -0
- package/dist/backends/gpu/index.d.ts +13 -0
- package/dist/backends/gpu/index.d.ts.map +1 -0
- package/dist/backends/index.d.ts +18 -0
- package/dist/backends/index.d.ts.map +1 -0
- package/dist/backends/register-backends.d.ts +18 -0
- package/dist/backends/register-backends.d.ts.map +1 -0
- package/dist/backends/wasm/detect.d.ts +58 -0
- package/dist/backends/wasm/detect.d.ts.map +1 -0
- package/dist/backends/wasm/fft-wasm.d.ts +120 -0
- package/dist/backends/wasm/fft-wasm.d.ts.map +1 -0
- package/dist/backends/wasm/index.d.ts +9 -0
- package/dist/backends/wasm/index.d.ts.map +1 -0
- package/dist/backends/wasm/integrity.d.ts +18 -0
- package/dist/backends/wasm/integrity.d.ts.map +1 -0
- package/dist/backends/wasm/resolve.d.ts +12 -0
- package/dist/backends/wasm/resolve.d.ts.map +1 -0
- package/dist/{chunk-4VMDO6W2.js → chunk-PNKVD2UK.js} +2 -7
- package/dist/config.d.ts +145 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/index.d.ts +14 -3037
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -9
- package/dist/{integrity-LIWCCDIJ.js → integrity-IBNQNHXP.js} +1 -1
- package/dist/operations/cholesky.d.ts +24 -0
- package/dist/operations/cholesky.d.ts.map +1 -0
- package/dist/operations/common.d.ts +61 -0
- package/dist/operations/common.d.ts.map +1 -0
- package/dist/operations/condest.d.ts +27 -0
- package/dist/operations/condest.d.ts.map +1 -0
- package/dist/operations/eig-wasm.d.ts +35 -0
- package/dist/operations/eig-wasm.d.ts.map +1 -0
- package/dist/operations/eig.d.ts +75 -0
- package/dist/operations/eig.d.ts.map +1 -0
- package/dist/operations/expm.d.ts +46 -0
- package/dist/operations/expm.d.ts.map +1 -0
- package/dist/operations/index.d.ts +21 -0
- package/dist/operations/index.d.ts.map +1 -0
- package/dist/operations/logm.d.ts +59 -0
- package/dist/operations/logm.d.ts.map +1 -0
- package/dist/operations/lu.d.ts +51 -0
- package/dist/operations/lu.d.ts.map +1 -0
- package/dist/operations/pinv.d.ts +36 -0
- package/dist/operations/pinv.d.ts.map +1 -0
- package/dist/operations/qr-family.d.ts +52 -0
- package/dist/operations/qr-family.d.ts.map +1 -0
- package/dist/operations/qr-pivoted.d.ts +39 -0
- package/dist/operations/qr-pivoted.d.ts.map +1 -0
- package/dist/operations/qr.d.ts +35 -0
- package/dist/operations/qr.d.ts.map +1 -0
- package/dist/operations/schur.d.ts +58 -0
- package/dist/operations/schur.d.ts.map +1 -0
- package/dist/operations/sqrtm.d.ts +76 -0
- package/dist/operations/sqrtm.d.ts.map +1 -0
- package/dist/operations/svd-wasm.d.ts +23 -0
- package/dist/operations/svd-wasm.d.ts.map +1 -0
- package/dist/operations/svd.d.ts +70 -0
- package/dist/operations/svd.d.ts.map +1 -0
- package/dist/parallel-matrix.d.ts +223 -0
- package/dist/parallel-matrix.d.ts.map +1 -0
- package/dist/{resolve-DDCEWHWX.js → resolve-2SX6WJ6R.js} +1 -1
- package/dist/typed-operations.d.ts +162 -0
- package/dist/typed-operations.d.ts.map +1 -0
- package/dist/types/DenseMatrix.d.ts +213 -0
- package/dist/types/DenseMatrix.d.ts.map +1 -0
- package/dist/types/Matrix.d.ts +188 -0
- package/dist/types/Matrix.d.ts.map +1 -0
- package/dist/types/SparseMatrix.d.ts +214 -0
- package/dist/types/SparseMatrix.d.ts.map +1 -0
- package/dist/types/dense/arithmetic.d.ts +8 -0
- package/dist/types/dense/arithmetic.d.ts.map +1 -0
- package/dist/types/dense/reduction.d.ts +8 -0
- package/dist/types/dense/reduction.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/wasm/mathts-as.wasm +0 -0
- package/dist/wasm/wasm-manifest.json +1 -1
- package/package.json +70 -70
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GPU-CPU Synchronization Strategy
|
|
3
|
+
*
|
|
4
|
+
* Implements efficient patterns for synchronizing data between
|
|
5
|
+
* CPU and GPU memory. Minimizes transfer latency and enables
|
|
6
|
+
* overlapping of CPU and GPU work.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
import type { GPUContext } from '@danielsimonjr/mathts-gpu';
|
|
11
|
+
import type { BufferPool } from '@danielsimonjr/mathts-gpu';
|
|
12
|
+
/**
|
|
13
|
+
* Synchronization strategy type
|
|
14
|
+
*/
|
|
15
|
+
export type SyncStrategy = 'immediate' | 'lazy' | 'double-buffer' | 'streaming';
|
|
16
|
+
/**
|
|
17
|
+
* Transfer direction
|
|
18
|
+
*/
|
|
19
|
+
export type TransferDirection = 'cpu-to-gpu' | 'gpu-to-cpu';
|
|
20
|
+
/**
|
|
21
|
+
* Transfer request
|
|
22
|
+
*/
|
|
23
|
+
export interface TransferRequest {
|
|
24
|
+
/** Unique ID for this request */
|
|
25
|
+
id: number;
|
|
26
|
+
/** Source buffer */
|
|
27
|
+
source: GPUBuffer | Float32Array;
|
|
28
|
+
/** Destination buffer */
|
|
29
|
+
destination: GPUBuffer | Float32Array;
|
|
30
|
+
/** Transfer direction */
|
|
31
|
+
direction: TransferDirection;
|
|
32
|
+
/** Byte offset in source */
|
|
33
|
+
sourceOffset?: number;
|
|
34
|
+
/** Byte offset in destination */
|
|
35
|
+
destOffset?: number;
|
|
36
|
+
/** Number of bytes to transfer */
|
|
37
|
+
size: number;
|
|
38
|
+
/** Priority (higher = more urgent) */
|
|
39
|
+
priority?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Transfer result
|
|
43
|
+
*/
|
|
44
|
+
export interface TransferResult {
|
|
45
|
+
/** Request ID */
|
|
46
|
+
id: number;
|
|
47
|
+
/** Success status */
|
|
48
|
+
success: boolean;
|
|
49
|
+
/** Transfer time in milliseconds */
|
|
50
|
+
duration: number;
|
|
51
|
+
/** Bytes transferred */
|
|
52
|
+
bytesTransferred: number;
|
|
53
|
+
/** Error message if failed */
|
|
54
|
+
error?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Sync configuration
|
|
58
|
+
*/
|
|
59
|
+
export interface SyncConfig {
|
|
60
|
+
/** Default strategy */
|
|
61
|
+
strategy: SyncStrategy;
|
|
62
|
+
/** Chunk size for streaming (bytes) */
|
|
63
|
+
chunkSize?: number;
|
|
64
|
+
/** Maximum pending transfers */
|
|
65
|
+
maxPendingTransfers?: number;
|
|
66
|
+
/** Auto-coalesce nearby transfers */
|
|
67
|
+
coalesceTransfers?: boolean;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* GPU-CPU Synchronization Manager
|
|
71
|
+
*
|
|
72
|
+
* Manages data transfer between CPU and GPU with various strategies
|
|
73
|
+
* optimized for different use cases.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const sync = new SyncManager(context, bufferPool, {
|
|
78
|
+
* strategy: 'double-buffer',
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* // Upload data to GPU
|
|
82
|
+
* await sync.upload(cpuData, gpuBuffer);
|
|
83
|
+
*
|
|
84
|
+
* // Download results from GPU
|
|
85
|
+
* const result = await sync.download(gpuBuffer);
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare class SyncManager {
|
|
89
|
+
private context;
|
|
90
|
+
private config;
|
|
91
|
+
private pendingTransfers;
|
|
92
|
+
private nextRequestId;
|
|
93
|
+
private stagingBuffers;
|
|
94
|
+
private totalUploads;
|
|
95
|
+
private totalDownloads;
|
|
96
|
+
private totalBytesUploaded;
|
|
97
|
+
private totalBytesDownloaded;
|
|
98
|
+
constructor(context: GPUContext, _bufferPool: BufferPool, // Reserved for future pool integration
|
|
99
|
+
config?: Partial<SyncConfig>);
|
|
100
|
+
/**
|
|
101
|
+
* Upload data from CPU to GPU
|
|
102
|
+
*/
|
|
103
|
+
upload(cpuData: Float32Array | Float64Array | Uint32Array | Int32Array, gpuBuffer: GPUBuffer, options?: {
|
|
104
|
+
offset?: number;
|
|
105
|
+
size?: number;
|
|
106
|
+
}): Promise<TransferResult>;
|
|
107
|
+
/**
|
|
108
|
+
* Download data from GPU to CPU
|
|
109
|
+
*/
|
|
110
|
+
download(gpuBuffer: GPUBuffer, options?: {
|
|
111
|
+
offset?: number;
|
|
112
|
+
size?: number;
|
|
113
|
+
}): Promise<Float32Array>;
|
|
114
|
+
/**
|
|
115
|
+
* Download data using double-buffering for overlap
|
|
116
|
+
*/
|
|
117
|
+
downloadDoubleBuffered(gpuBuffer: GPUBuffer, size: number): Promise<Float32Array>;
|
|
118
|
+
/**
|
|
119
|
+
* Stream large data in chunks
|
|
120
|
+
*/
|
|
121
|
+
uploadStreaming(cpuData: Float32Array, gpuBuffer: GPUBuffer, onProgress?: (progress: number) => void): Promise<TransferResult>;
|
|
122
|
+
/**
|
|
123
|
+
* Download large data in chunks
|
|
124
|
+
*/
|
|
125
|
+
downloadStreaming(gpuBuffer: GPUBuffer, totalSize: number, onProgress?: (progress: number) => void): Promise<Float32Array>;
|
|
126
|
+
/**
|
|
127
|
+
* Batch multiple transfers
|
|
128
|
+
*/
|
|
129
|
+
batchTransfer(requests: Array<{
|
|
130
|
+
data: Float32Array;
|
|
131
|
+
buffer: GPUBuffer;
|
|
132
|
+
direction: TransferDirection;
|
|
133
|
+
}>): Promise<TransferResult[]>;
|
|
134
|
+
/**
|
|
135
|
+
* Create or reuse a staging buffer
|
|
136
|
+
*/
|
|
137
|
+
private getOrCreateStagingBuffer;
|
|
138
|
+
/**
|
|
139
|
+
* Round up to next power of 2
|
|
140
|
+
*/
|
|
141
|
+
private roundToPowerOf2;
|
|
142
|
+
/**
|
|
143
|
+
* Wait for all pending transfers to complete
|
|
144
|
+
*/
|
|
145
|
+
flush(): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Get synchronization statistics
|
|
148
|
+
*/
|
|
149
|
+
getStats(): {
|
|
150
|
+
totalUploads: number;
|
|
151
|
+
totalDownloads: number;
|
|
152
|
+
totalBytesUploaded: number;
|
|
153
|
+
totalBytesDownloaded: number;
|
|
154
|
+
pendingTransfers: number;
|
|
155
|
+
stagingBuffersCount: number;
|
|
156
|
+
strategy: SyncStrategy;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Destroy sync manager and release resources
|
|
160
|
+
*/
|
|
161
|
+
destroy(): void;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Create a sync manager with the recommended configuration
|
|
165
|
+
*/
|
|
166
|
+
export declare function createSyncManager(context: GPUContext, bufferPool: BufferPool, strategy?: SyncStrategy): SyncManager;
|
|
167
|
+
//# sourceMappingURL=Sync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Sync.d.ts","sourceRoot":"","sources":["../../../src/backends/gpu/Sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D;;GAEG;AACH,MAAM,MAAM,YAAY,GACpB,WAAW,GACX,MAAM,GACN,eAAe,GACf,WAAW,CAAC;AAEhB;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,YAAY,GAAG,YAAY,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,iCAAiC;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,oBAAoB;IACpB,MAAM,EAAE,SAAS,GAAG,YAAY,CAAC;IACjC,yBAAyB;IACzB,WAAW,EAAE,SAAS,GAAG,YAAY,CAAC;IACtC,yBAAyB;IACzB,SAAS,EAAE,iBAAiB,CAAC;IAC7B,4BAA4B;IAC5B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iCAAiC;IACjC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,sCAAsC;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,iBAAiB;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,qBAAqB;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,oCAAoC;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,wBAAwB;IACxB,gBAAgB,EAAE,MAAM,CAAC;IACzB,8BAA8B;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,uBAAuB;IACvB,QAAQ,EAA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ackend registers them onto a ShaderManager at init.
|
|
6
|
+
*/
|
|
7
|
+
import type { ShaderManager } from '@danielsimonjr/mathts-gpu';
|
|
8
|
+
export declare const BUILTIN_SHADERS: {
|
|
9
|
+
/** Matrix addition shader */
|
|
10
|
+
readonly matrixAdd: `
|
|
11
|
+
@group(0) @binding(0) var<storage, read> a: array<f32>;
|
|
12
|
+
@group(0) @binding(1) var<storage, read> b: array<f32>;
|
|
13
|
+
@group(0) @binding(2) var<storage, read_write> result: array<f32>;
|
|
14
|
+
@group(0) @binding(3) var<uniform> params: vec4<u32>; // rows, cols, _, _
|
|
15
|
+
|
|
16
|
+
@compute @workgroup_size(16, 16)
|
|
17
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
18
|
+
let rows = params.x;
|
|
19
|
+
let cols = params.y;
|
|
20
|
+
let row = gid.y;
|
|
21
|
+
let col = gid.x;
|
|
22
|
+
|
|
23
|
+
if (row >= rows || col >= cols) { return; }
|
|
24
|
+
|
|
25
|
+
let idx = row * cols + col;
|
|
26
|
+
result[idx] = a[idx] + b[idx];
|
|
27
|
+
}
|
|
28
|
+
`;
|
|
29
|
+
/** Matrix subtraction shader */
|
|
30
|
+
readonly matrixSub: `
|
|
31
|
+
@group(0) @binding(0) var<storage, read> a: array<f32>;
|
|
32
|
+
@group(0) @binding(1) var<storage, read> b: array<f32>;
|
|
33
|
+
@group(0) @binding(2) var<storage, read_write> result: array<f32>;
|
|
34
|
+
@group(0) @binding(3) var<uniform> params: vec4<u32>;
|
|
35
|
+
|
|
36
|
+
@compute @workgroup_size(16, 16)
|
|
37
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
38
|
+
let rows = params.x;
|
|
39
|
+
let cols = params.y;
|
|
40
|
+
let row = gid.y;
|
|
41
|
+
let col = gid.x;
|
|
42
|
+
|
|
43
|
+
if (row >= rows || col >= cols) { return; }
|
|
44
|
+
|
|
45
|
+
let idx = row * cols + col;
|
|
46
|
+
result[idx] = a[idx] - b[idx];
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
/** Element-wise multiplication shader */
|
|
50
|
+
readonly matrixMul: `
|
|
51
|
+
@group(0) @binding(0) var<storage, read> a: array<f32>;
|
|
52
|
+
@group(0) @binding(1) var<storage, read> b: array<f32>;
|
|
53
|
+
@group(0) @binding(2) var<storage, read_write> result: array<f32>;
|
|
54
|
+
@group(0) @binding(3) var<uniform> params: vec4<u32>;
|
|
55
|
+
|
|
56
|
+
@compute @workgroup_size(16, 16)
|
|
57
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
58
|
+
let rows = params.x;
|
|
59
|
+
let cols = params.y;
|
|
60
|
+
let row = gid.y;
|
|
61
|
+
let col = gid.x;
|
|
62
|
+
|
|
63
|
+
if (row >= rows || col >= cols) { return; }
|
|
64
|
+
|
|
65
|
+
let idx = row * cols + col;
|
|
66
|
+
result[idx] = a[idx] * b[idx];
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
69
|
+
/** Scalar multiplication shader */
|
|
70
|
+
readonly scalarMul: `
|
|
71
|
+
@group(0) @binding(0) var<storage, read> a: array<f32>;
|
|
72
|
+
@group(0) @binding(1) var<storage, read_write> result: array<f32>;
|
|
73
|
+
@group(0) @binding(2) var<uniform> params: vec4<f32>; // scalar, length, _, _
|
|
74
|
+
|
|
75
|
+
@compute @workgroup_size(256)
|
|
76
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
77
|
+
let scalar = params.x;
|
|
78
|
+
let length = u32(params.y);
|
|
79
|
+
let idx = gid.x;
|
|
80
|
+
|
|
81
|
+
if (idx >= length) { return; }
|
|
82
|
+
|
|
83
|
+
result[idx] = a[idx] * scalar;
|
|
84
|
+
}
|
|
85
|
+
`;
|
|
86
|
+
/** Matrix multiplication (naive) shader */
|
|
87
|
+
readonly matmul: `
|
|
88
|
+
@group(0) @binding(0) var<storage, read> a: array<f32>;
|
|
89
|
+
@group(0) @binding(1) var<storage, read> b: array<f32>;
|
|
90
|
+
@group(0) @binding(2) var<storage, read_write> result: array<f32>;
|
|
91
|
+
@group(0) @binding(3) var<uniform> params: vec4<u32>; // M, N, K, _
|
|
92
|
+
|
|
93
|
+
@compute @workgroup_size(16, 16)
|
|
94
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
95
|
+
let M = params.x;
|
|
96
|
+
let N = params.y;
|
|
97
|
+
let K = params.z;
|
|
98
|
+
let row = gid.y;
|
|
99
|
+
let col = gid.x;
|
|
100
|
+
|
|
101
|
+
if (row >= M || col >= N) { return; }
|
|
102
|
+
|
|
103
|
+
var sum: f32 = 0.0;
|
|
104
|
+
for (var k: u32 = 0u; k < K; k = k + 1u) {
|
|
105
|
+
sum = sum + a[row * K + k] * b[k * N + col];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
result[row * N + col] = sum;
|
|
109
|
+
}
|
|
110
|
+
`;
|
|
111
|
+
/** Matrix transpose shader */
|
|
112
|
+
readonly transpose: `
|
|
113
|
+
@group(0) @binding(0) var<storage, read> a: array<f32>;
|
|
114
|
+
@group(0) @binding(1) var<storage, read_write> result: array<f32>;
|
|
115
|
+
@group(0) @binding(2) var<uniform> params: vec4<u32>; // rows, cols, _, _
|
|
116
|
+
|
|
117
|
+
@compute @workgroup_size(16, 16)
|
|
118
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
119
|
+
let rows = params.x;
|
|
120
|
+
let cols = params.y;
|
|
121
|
+
let row = gid.y;
|
|
122
|
+
let col = gid.x;
|
|
123
|
+
|
|
124
|
+
if (row >= rows || col >= cols) { return; }
|
|
125
|
+
|
|
126
|
+
result[col * rows + row] = a[row * cols + col];
|
|
127
|
+
}
|
|
128
|
+
`;
|
|
129
|
+
/** Sum reduction shader (first pass) */
|
|
130
|
+
readonly sumReduce: `
|
|
131
|
+
@group(0) @binding(0) var<storage, read> input: array<f32>;
|
|
132
|
+
@group(0) @binding(1) var<storage, read_write> output: array<f32>;
|
|
133
|
+
@group(0) @binding(2) var<uniform> params: vec4<u32>; // inputLength, outputLength, _, _
|
|
134
|
+
|
|
135
|
+
// NOTE: 'shared' is a RESERVED KEYWORD in WGSL — naming this workgroup
|
|
136
|
+
// array 'shared' made this shader fail to compile, which (because
|
|
137
|
+
// GPUBackend.initialize() precompiles every registered shader) poisoned
|
|
138
|
+
// backend init and silently forced ALL GPU ops onto the CPU fallback.
|
|
139
|
+
var<workgroup> sdata: array<f32, 256>;
|
|
140
|
+
|
|
141
|
+
@compute @workgroup_size(256)
|
|
142
|
+
fn main(
|
|
143
|
+
@builtin(local_invocation_id) lid: vec3<u32>,
|
|
144
|
+
@builtin(workgroup_id) wid: vec3<u32>
|
|
145
|
+
) {
|
|
146
|
+
let inputLength = params.x;
|
|
147
|
+
let idx = wid.x * 512u + lid.x;
|
|
148
|
+
|
|
149
|
+
// Load two elements and sum
|
|
150
|
+
var sum: f32 = 0.0;
|
|
151
|
+
if (idx < inputLength) {
|
|
152
|
+
sum = input[idx];
|
|
153
|
+
}
|
|
154
|
+
if (idx + 256u < inputLength) {
|
|
155
|
+
sum = sum + input[idx + 256u];
|
|
156
|
+
}
|
|
157
|
+
sdata[lid.x] = sum;
|
|
158
|
+
|
|
159
|
+
workgroupBarrier();
|
|
160
|
+
|
|
161
|
+
// Reduce within workgroup
|
|
162
|
+
for (var s: u32 = 128u; s > 0u; s = s >> 1u) {
|
|
163
|
+
if (lid.x < s) {
|
|
164
|
+
sdata[lid.x] = sdata[lid.x] + sdata[lid.x + s];
|
|
165
|
+
}
|
|
166
|
+
workgroupBarrier();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Write result
|
|
170
|
+
if (lid.x == 0u) {
|
|
171
|
+
output[wid.x] = sdata[0];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
`;
|
|
175
|
+
};
|
|
176
|
+
/** Register every builtin matrix kernel onto a ShaderManager. */
|
|
177
|
+
export declare function registerBuiltinShaders(sm: ShaderManager): void;
|
|
178
|
+
//# sourceMappingURL=builtin-shaders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builtin-shaders.d.ts","sourceRoot":"","sources":["../../../src/backends/gpu/builtin-shaders.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE/D,eAAO,MAAM,eAAe;IAC1B,6BAA6B;aAC7B,SAAS,EAAE;;;;;;;;;;;;;;;;;;GAkBV;IAED,gCAAgC;aAChC,SAAS,EAAE;;;;;;;;;;;;;;;;;;GAkBV;IAED,yCAAyC;aACzC,SAAS,EAAE;;;;;;;;;;;;;;;;;;GAkBV;IAED,mCAAmC;aACnC,SAAS,EAAE;;;;;;;;;;;;;;;GAeV;IAED,2CAA2C;aAC3C,MAAM,EAAE;;;;;;;;;;;;;;;;;;;;;;;GAuBP;IAED,8BAA8B;aAC9B,SAAS,EAAE;;;;;;;;;;;;;;;;GAgBV;IAED,wCAAwC;aACxC,SAAS,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CV;CACO,CAAC;AAEX,iEAAiE;AACjE,wBAAgB,sBAAsB,CAAC,EAAE,EAAE,aAAa,GAAG,IAAI,CAI9D"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GPU Backend Exports
|
|
3
|
+
*
|
|
4
|
+
* WebGPU infrastructure for accelerated matrix operations. The generic
|
|
5
|
+
* foundation now lives in @danielsimonjr/mathts-gpu and is re-exported here
|
|
6
|
+
* so matrix's public surface is unchanged. Matrix-domain kernels and the
|
|
7
|
+
* (unextracted) batch/sync helpers stay local.
|
|
8
|
+
*/
|
|
9
|
+
export { hasWebGPU, isBrowser, getGPUAdapter, detectGPUCapabilities, isGPUSuitableForMatrixOps, getRecommendedWorkgroupSize, getMaxMatrixSize, GPUContext, getGlobalGPUContext, initializeGlobalGPU, destroyGlobalGPU, getGpuDevice, resetGpuDevice, BufferPool, ShaderManager, type GPUAdapterInfo, type GPUCapabilities, type GPUContextOptions, type GPUContextStatus, type DeviceLostEvent, type BufferPoolOptions, type ShaderSource, type PipelineConfig, } from '@danielsimonjr/mathts-gpu';
|
|
10
|
+
export { BUILTIN_SHADERS, registerBuiltinShaders } from './builtin-shaders.js';
|
|
11
|
+
export { BatchExecutor, type BatchOperation, type BatchOperationType, type BatchResult, type BatchOptions, } from './BatchExecutor.js';
|
|
12
|
+
export { SyncManager, createSyncManager, type SyncStrategy, type TransferDirection, type TransferRequest, type TransferResult, type SyncConfig, } from './Sync.js';
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/backends/gpu/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EACL,SAAS,EACT,SAAS,EACT,aAAa,EACb,qBAAqB,EACrB,yBAAyB,EACzB,2BAA2B,EAC3B,gBAAgB,EAChB,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,YAAY,EACZ,cAAc,EACd,UAAU,EACV,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,cAAc,GACpB,MAAM,2BAA2B,CAAC;AAGnC,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAG/E,OAAO,EACL,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACvB,KAAK,WAAW,EAChB,KAAK,YAAY,GAClB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,WAAW,EACX,iBAAiB,EACjB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,UAAU,GAChB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Matrix Backend Exports
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
export { BackendRegistry, backendRegistry, DEFAULT_BACKEND_HINTS } from './Backend.js';
|
|
6
|
+
export type { MatrixBackend, BackendType, BackendHints } from './Backend.js';
|
|
7
|
+
export { JSBackend, jsBackend } from './JSBackend.js';
|
|
8
|
+
export { ParallelBackend, parallelBackend, createParallelBackend, type ParallelBackendConfig, } from './ParallelBackend.js';
|
|
9
|
+
export { WASMBackend, wasmBackend, createWASMBackend, type WASMBackendConfig, } from './WASMBackend.js';
|
|
10
|
+
export { GPUMatrixBackend, gpuMatrixBackend, createGPUMatrixBackend, type GPUMatrixBackendConfig, } from './GPUMatrixBackend.js';
|
|
11
|
+
export { GPUBackend, getGlobalGPUBackend, initializeGlobalGPUBackend, destroyGlobalGPUBackend, type GPUBackendOptions, type GPUBackendStatus, } from './GPUBackend.js';
|
|
12
|
+
export { BackendManager, backendManager, createBackendManager, DEFAULT_EXTENDED_HINTS, type ExtendedBackendHints, type OperationType, } from './BackendManager.js';
|
|
13
|
+
export { detectWasmFeatures, isWasmAvailable, isSharedMemoryAvailable, isAtomicsAvailable, clearFeatureCache, getCachedFeatures, } from './wasm/index.js';
|
|
14
|
+
export type { WasmFeatures } from './wasm/index.js';
|
|
15
|
+
export { hasWebGPU, detectGPUCapabilities, getRecommendedWorkgroupSize, GPUContext, getGlobalGPUContext, destroyGlobalGPU, BufferPool, ShaderManager, BUILTIN_SHADERS, BatchExecutor, SyncManager, createSyncManager, } from './gpu/index.js';
|
|
16
|
+
export type { GPUCapabilities, GPUContextOptions, SyncStrategy, SyncConfig } from './gpu/index.js';
|
|
17
|
+
import './register-backends.js';
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sour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AAY,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAKnG,OAAO,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default backend registrations.
|
|
3
|
+
*
|
|
4
|
+
* This module is imported for side effects from both `./index.ts` and
|
|
5
|
+
* `./BackendManager.ts`. Putting the registrations here ensures they run
|
|
6
|
+
* regardless of which module the consumer imports first, and avoids a
|
|
7
|
+
* back-edge from BackendManager → index → BackendManager.
|
|
8
|
+
*
|
|
9
|
+
* Backends declared here are *registered* but not *initialized*: the WASM
|
|
10
|
+
* artifacts only load when `BackendManager.initialize()` (or a direct
|
|
11
|
+
* `.initialize()` call on the backend) runs. Importing this module is
|
|
12
|
+
* therefore cheap and safe in environments without WebAssembly (the
|
|
13
|
+
* `isAvailable()` check on each backend is the gate).
|
|
14
|
+
*
|
|
15
|
+
* @packageDocumentation
|
|
16
|
+
*/
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=register-backends.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register-backends.d.ts","sourceRoot":"","sources":["../../src/backends/register-backends.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM Feature Detection
|
|
3
|
+
*
|
|
4
|
+
* Detects available WebAssembly features in the current environment.
|
|
5
|
+
* Used to enable/disable SIMD, threads, and other optimizations.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Available WASM features
|
|
11
|
+
*/
|
|
12
|
+
export interface WasmFeatures {
|
|
13
|
+
/** Basic WebAssembly support */
|
|
14
|
+
webAssembly: boolean;
|
|
15
|
+
/** WASM SIMD (v128) support */
|
|
16
|
+
simd: boolean;
|
|
17
|
+
/** SharedArrayBuffer available */
|
|
18
|
+
sharedMemory: boolean;
|
|
19
|
+
/** Atomics API available */
|
|
20
|
+
atomics: boolean;
|
|
21
|
+
/** WASM threads (requires SharedArrayBuffer) */
|
|
22
|
+
threads: boolean;
|
|
23
|
+
/** Bulk memory operations */
|
|
24
|
+
bulkMemory: boolean;
|
|
25
|
+
/** Reference types */
|
|
26
|
+
referenceTypes: boolean;
|
|
27
|
+
/** WASM exception handling */
|
|
28
|
+
exceptions: boolean;
|
|
29
|
+
/** Tail call optimization */
|
|
30
|
+
tailCall: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Detect all WASM features
|
|
34
|
+
*
|
|
35
|
+
* @returns Promise resolving to detected features
|
|
36
|
+
*/
|
|
37
|
+
export declare function detectWasmFeatures(): Promise<WasmFeatures>;
|
|
38
|
+
/**
|
|
39
|
+
* Synchronously check if WASM is available (basic check only)
|
|
40
|
+
*/
|
|
41
|
+
export declare function isWasmAvailable(): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Synchronously check if SharedArrayBuffer is available
|
|
44
|
+
*/
|
|
45
|
+
export declare function isSharedMemoryAvailable(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Synchronously check if Atomics are available
|
|
48
|
+
*/
|
|
49
|
+
export declare function isAtomicsAvailable(): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Clear the feature detection cache (useful for testing)
|
|
52
|
+
*/
|
|
53
|
+
export declare function clearFeatureCache(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Get cached features if available
|
|
56
|
+
*/
|
|
57
|
+
export declare function getCachedFeatures(): WasmFeatures | null;
|
|
58
|
+
//# sourceMappingURL=detect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detect.d.ts","sourceRoot":"","sources":["../../../src/backends/wasm/detect.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,gCAAgC;IAChC,WAAW,EAAE,OAAO,CAAC;IACrB,+BAA+B;IAC/B,IAAI,EAAE,OAAO,CAAC;IACd,kCAAkC;IAClC,YAAY,EAAE,OAAO,CAAC;IACtB,4BAA4B;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,OAAO,EAAE,OAAO,CAAC;IACjB,6BAA6B;IAC7B,UAAU,EAAE,OAAO,CAAC;IACpB,sBAAsB;IACtB,cAAc,EAAE,OAAO,CAAC;IACxB,8BAA8B;IAC9B,UAAU,EAAE,OAAO,CAAC;IACpB,6BAA6B;IAC7B,QAAQ,EAAE,OAAO,CAAC;CACnB;AA8PD;;;;GAIG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,YAAY,CAAC,CAkDhE;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,OAAO,CAEzC;AAED;;GAEG;AACH,wBAAgB,uBAAuB,IAAI,OAAO,CAEjD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,OAAO,CAE5C;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,IAAI,CAExC;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,YAAY,GAAG,IAAI,CAEvD"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM-Accelerated FFT Operations
|
|
3
|
+
*
|
|
4
|
+
* Wraps the AssemblyScript WASM FFT exports (`fft` / `rfft` / `powerSpectrum`,
|
|
5
|
+
* `assembly/src/ops/fft.ts`, interleaved complex format) with a high-level
|
|
6
|
+
* TypeScript API. Falls back to a pure JavaScript Cooley-Tukey radix-2
|
|
7
|
+
* implementation when WASM is unavailable. (Phase 7b: the matrix loader now
|
|
8
|
+
* defaults to the AS binary; the AS `fft(data, n, inverse)` / `rfft(data, n,
|
|
9
|
+
* result)` / `powerSpectrum(data, n, result)` call shapes match the consumer
|
|
10
|
+
* exactly, so this module needed no marshalling changes.)
|
|
11
|
+
*
|
|
12
|
+
* The WASM kernels use interleaved complex arrays: [re0, im0, re1, im1, ...].
|
|
13
|
+
* This module converts between split (separate real/imag) and interleaved
|
|
14
|
+
* formats as needed.
|
|
15
|
+
*
|
|
16
|
+
* @packageDocumentation
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* FFT result with separate real and imaginary arrays
|
|
20
|
+
*/
|
|
21
|
+
export interface FFTResult {
|
|
22
|
+
real: Float64Array;
|
|
23
|
+
imag: Float64Array;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* FFT backend selection
|
|
27
|
+
*/
|
|
28
|
+
export type FFTBackend = 'wasm' | 'js' | 'auto';
|
|
29
|
+
/**
|
|
30
|
+
* Configuration for FFT operations
|
|
31
|
+
*/
|
|
32
|
+
export interface FFTConfig {
|
|
33
|
+
/** Backend to use (default: 'auto') */
|
|
34
|
+
backend?: FFTBackend;
|
|
35
|
+
/** Minimum elements to use WASM (default: 64) */
|
|
36
|
+
wasmThreshold?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Check if n is a power of 2
|
|
40
|
+
*/
|
|
41
|
+
export declare function isPowerOf2(n: number): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Next power of 2 >= n
|
|
44
|
+
*/
|
|
45
|
+
export declare function nextPowerOf2(n: number): number;
|
|
46
|
+
/**
|
|
47
|
+
* Pure JavaScript Cooley-Tukey radix-2 FFT
|
|
48
|
+
*
|
|
49
|
+
* Input length must be a power of 2.
|
|
50
|
+
*/
|
|
51
|
+
export declare function fftJS(real: Float64Array, imag: Float64Array, inverse?: boolean): FFTResult;
|
|
52
|
+
/**
|
|
53
|
+
* Check if the WASM FFT backend is available
|
|
54
|
+
*/
|
|
55
|
+
export declare function isWasmFFTAvailable(): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Compute the Fast Fourier Transform.
|
|
58
|
+
*
|
|
59
|
+
* Automatically selects WASM or JS backend based on availability and
|
|
60
|
+
* array size. Input length must be a power of 2.
|
|
61
|
+
*
|
|
62
|
+
* @param real - Real part of input signal
|
|
63
|
+
* @param imag - Imaginary part (zeros for real-only input)
|
|
64
|
+
* @param config - Backend configuration
|
|
65
|
+
* @returns FFT result with real and imaginary arrays
|
|
66
|
+
*/
|
|
67
|
+
export declare function fft(real: Float64Array, imag: Float64Array, config?: FFTConfig): FFTResult;
|
|
68
|
+
/**
|
|
69
|
+
* Compute the Inverse Fast Fourier Transform.
|
|
70
|
+
*
|
|
71
|
+
* @param real - Real part of frequency-domain signal
|
|
72
|
+
* @param imag - Imaginary part of frequency-domain signal
|
|
73
|
+
* @param config - Backend configuration
|
|
74
|
+
* @returns Time-domain signal with real and imaginary arrays
|
|
75
|
+
*/
|
|
76
|
+
export declare function ifft(real: Float64Array, imag: Float64Array, config?: FFTConfig): FFTResult;
|
|
77
|
+
/**
|
|
78
|
+
* Compute the real FFT (real input -> complex output).
|
|
79
|
+
*
|
|
80
|
+
* Convenience wrapper that creates a zero imaginary array.
|
|
81
|
+
*
|
|
82
|
+
* @param data - Real-valued input signal (length must be power of 2)
|
|
83
|
+
* @param config - Backend configuration
|
|
84
|
+
* @returns FFT result
|
|
85
|
+
*/
|
|
86
|
+
export declare function rfft(data: Float64Array, config?: FFTConfig): FFTResult;
|
|
87
|
+
/**
|
|
88
|
+
* Compute the power spectrum: |X[k]|^2 for each frequency bin.
|
|
89
|
+
*
|
|
90
|
+
* @param real - Real part of FFT output
|
|
91
|
+
* @param imag - Imaginary part of FFT output
|
|
92
|
+
* @returns Power spectrum array
|
|
93
|
+
*/
|
|
94
|
+
export declare function powerSpectrum(real: Float64Array, imag: Float64Array): Float64Array;
|
|
95
|
+
/**
|
|
96
|
+
* Compute the magnitude spectrum: |X[k]| for each frequency bin.
|
|
97
|
+
*
|
|
98
|
+
* @param real - Real part of FFT output
|
|
99
|
+
* @param imag - Imaginary part of FFT output
|
|
100
|
+
* @returns Magnitude spectrum array
|
|
101
|
+
*/
|
|
102
|
+
export declare function magnitudeSpectrum(real: Float64Array, imag: Float64Array): Float64Array;
|
|
103
|
+
/**
|
|
104
|
+
* Compute the phase spectrum: angle(X[k]) for each frequency bin.
|
|
105
|
+
*
|
|
106
|
+
* @param real - Real part of FFT output
|
|
107
|
+
* @param imag - Imaginary part of FFT output
|
|
108
|
+
* @returns Phase spectrum array (radians)
|
|
109
|
+
*/
|
|
110
|
+
export declare function phaseSpectrum(real: Float64Array, imag: Float64Array): Float64Array;
|
|
111
|
+
/**
|
|
112
|
+
* FFT-based convolution: conv(signal, kernel) = IFFT(FFT(signal) * FFT(kernel))
|
|
113
|
+
*
|
|
114
|
+
* @param signal - Input signal
|
|
115
|
+
* @param kernel - Convolution kernel
|
|
116
|
+
* @param config - Backend configuration
|
|
117
|
+
* @returns Convolution result (length = signal.length + kernel.length - 1)
|
|
118
|
+
*/
|
|
119
|
+
export declare function convolve(signal: Float64Array, kernel: Float64Array, config?: FFTConfig): Float64Array;
|
|
120
|
+
//# sourceMappingURL=fft-wasm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fft-wasm.d.ts","sourceRoot":"","sources":["../../../src/backends/wasm/fft-wasm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAQH;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,YAAY,CAAC;IACnB,IAAI,EAAE,YAAY,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,uCAAuC;IACvC,OAAO,CAAC,EAAE,UAAU,CAAC;IACrB,iDAAiD;IACjD,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAqDD;;GAEG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAE7C;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAG9C;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,OAAO,GAAE,OAAe,GAAG,SAAS,CA+EjG;AAMD;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,OAAO,CAG5C;AA2CD;;;;;;;;;;GAUG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,GAAE,SAAc,GAAG,SAAS,CAgB7F;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,GAAE,SAAc,GAAG,SAAS,CAgB9F;AAED;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,GAAE,SAAc,GAAG,SAAS,CAqC1E;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,GAAG,YAAY,CAkClF;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,GAAG,YAAY,CAOtF;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,GAAG,YAAY,CAOlF;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CACtB,MAAM,EAAE,YAAY,EACpB,MAAM,EAAE,YAAY,EACpB,MAAM,GAAE,SAAc,GACrB,YAAY,CAkCd"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM Utilities Index
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
export { detectWasmFeatures, isWasmAvailable, isSharedMemoryAvailable, isAtomicsAvailable, clearFeatureCache, getCachedFeatures, } from './detect.js';
|
|
6
|
+
export type { WasmFeatures } from './detect.js';
|
|
7
|
+
export { fft as wasmFFT, ifft as wasmIFFT, rfft as wasmRFFT, fftJS, convolve as wasmConvolve, powerSpectrum, magnitudeSpectrum, phaseSpectrum, isPowerOf2, nextPowerOf2, isWasmFFTAvailable, } from './fft-wasm.js';
|
|
8
|
+
export type { FFTResult, FFTBackend, FFTConfig } from './fft-wasm.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/backends/wasm/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,uBAAuB,EACvB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,EACL,GAAG,IAAI,OAAO,EACd,IAAI,IAAI,QAAQ,EAChB,IAAI,IAAI,QAAQ,EAChB,KAAK,EACL,QAAQ,IAAI,YAAY,EACxB,aAAa,EACb,iBAAiB,EACjB,aAAa,EACb,UAAU,EACV,YAAY,EACZ,kBAAkB,GACnB,MAAM,eAAe,CAAC;AAEvB,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM integrity verification - SHA-384 manifest check.
|
|
3
|
+
*
|
|
4
|
+
* The shared runtime logic (`sha384OfBuffer` / `loadWasmManifest` /
|
|
5
|
+
* `verifyWasmIntegrity`) is consolidated in
|
|
6
|
+
* `@danielsimonjr/mathts-core/internal` (`core/src/wasm-loader.ts`) so `matrix`
|
|
7
|
+
* and `functions` build on ONE copy — see docs/Architecture/duplicate-symbols.json.
|
|
8
|
+
* matrix can't import from functions (that would invert the dep edge and create a
|
|
9
|
+
* cycle), but both packages depend on core, so core/internal is a shared home
|
|
10
|
+
* reachable from both without a cycle. This module re-exports it (plus the
|
|
11
|
+
* `WasmManifest` type) to preserve the local `./integrity.js` import surface.
|
|
12
|
+
*
|
|
13
|
+
* SECURITY INVARIANT (CLAUDE.md #1): the SHA-384 hash-and-compare-before-
|
|
14
|
+
* instantiate is unchanged — it now lives in core, byte-for-byte. Guarded by
|
|
15
|
+
* matrix/tests/security/wasm-integrity.test.ts.
|
|
16
|
+
*/
|
|
17
|
+
export { sha384OfBuffer, loadWasmManifest, verifyWasmIntegrity, type WasmManifest, } from '@danielsimonjr/mathts-core/internal';
|
|
18
|
+
//# sourceMappingURL=integrity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"integrity.d.ts","sourceRoot":"","sources":["../../../src/backends/wasm/integrity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,mBAAmB,EACnB,KAAK,YAAY,GAClB,MAAM,qCAAqC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Packaged `.wasm` artifact resolution.
|
|
3
|
+
*
|
|
4
|
+
* `resolvePackagedWasm` and `defaultWasmLocation` are consolidated in
|
|
5
|
+
* `@danielsimonjr/mathts-core/internal` (`core/src/wasm-loader.ts`) — shared
|
|
6
|
+
* byte-for-byte with functions (see docs/Architecture/duplicate-symbols.json).
|
|
7
|
+
* matrix can't import from functions (cycle), but both depend on core, so
|
|
8
|
+
* core/internal is a shared home reachable from both without a cycle. Each caller
|
|
9
|
+
* injects its own `import.meta.url`, so resolution stays relative to this package.
|
|
10
|
+
*/
|
|
11
|
+
export { resolvePackagedWasm, defaultWasmLocation } from '@danielsimonjr/mathts-core/internal';
|
|
12
|
+
//# sourceMappingURL=resolve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../../../src/backends/wasm/resolve.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC"}
|