forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypescript-wasm-example.ts
More file actions
224 lines (190 loc) · 6.52 KB
/
typescript-wasm-example.ts
File metadata and controls
224 lines (190 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* Example: Using TypeScript + WASM + Parallel Computing with mathjs
*
* This example demonstrates the new high-performance features:
* - WASM-accelerated matrix operations
* - Parallel/multicore computing
* - Automatic optimization selection
*/
import { MatrixWasmBridge } from '../src/wasm/MatrixWasmBridge.js'
import { ParallelMatrix } from '../src/parallel/ParallelMatrix.js'
async function main() {
console.log('=== TypeScript + WASM + Parallel Computing Example ===\n')
// Initialize WASM module
console.log('Initializing WASM...')
await MatrixWasmBridge.init()
// Check capabilities
const caps = MatrixWasmBridge.getCapabilities()
console.log('Capabilities:')
console.log(' WASM Available:', caps.wasmAvailable)
console.log(' Parallel Available:', caps.parallelAvailable)
console.log(' SIMD Available:', caps.simdAvailable)
console.log()
// Example 1: Matrix Multiplication Benchmark
console.log('=== Example 1: Matrix Multiplication Benchmark ===')
await matrixMultiplicationBenchmark()
console.log()
// Example 2: LU Decomposition
console.log('=== Example 2: LU Decomposition ===')
await luDecompositionExample()
console.log()
// Example 3: Parallel Matrix Operations
console.log('=== Example 3: Parallel Matrix Operations ===')
await parallelMatrixExample()
console.log()
// Example 4: Configuration Options
console.log('=== Example 4: Custom Configuration ===')
await customConfigurationExample()
console.log()
// Cleanup
console.log('Cleaning up...')
await MatrixWasmBridge.cleanup()
console.log('Done!')
}
/**
* Example 1: Matrix Multiplication Performance Comparison
*/
async function matrixMultiplicationBenchmark() {
const size = 500
console.log(`Matrix size: ${size}x${size}\n`)
// Generate random matrices
const a = new Float64Array(size * size)
const b = new Float64Array(size * size)
for (let i = 0; i < size * size; i++) {
a[i] = Math.random()
b[i] = Math.random()
}
// Benchmark with WASM (automatic selection)
console.log('Computing with automatic optimization...')
const start = performance.now()
const result = await MatrixWasmBridge.multiply(a, size, size, b, size, size)
const end = performance.now()
console.log(`Time: ${(end - start).toFixed(2)}ms`)
console.log(`Result dimensions: ${size}x${size}`)
console.log(`First 4 elements: [${result.slice(0, 4).join(', ')}]`)
}
/**
* Example 2: LU Decomposition
*/
async function luDecompositionExample() {
// Create a test matrix
const n = 4
const matrix = new Float64Array([
4, 3, 2, 1,
3, 4, 3, 2,
2, 3, 4, 3,
1, 2, 3, 4
])
console.log('Input matrix (4x4):')
printMatrix(matrix, n, n)
console.log()
// Perform LU decomposition
const { lu, perm, singular } = await MatrixWasmBridge.luDecomposition(matrix, n)
if (singular) {
console.log('Matrix is singular!')
} else {
console.log('LU Decomposition successful')
console.log('Permutation vector:', Array.from(perm))
console.log('\nL and U (combined):')
printMatrix(lu, n, n)
}
}
/**
* Example 3: Parallel Matrix Operations
*/
async function parallelMatrixExample() {
// Configure parallel execution
ParallelMatrix.configure({
minSizeForParallel: 100,
maxWorkers: 4,
useSharedMemory: true
})
const size = 1000
console.log(`Large matrix multiplication: ${size}x${size}`)
console.log('Using parallel/multicore execution\n')
// Generate large matrices
const a = new Float64Array(size * size)
const b = new Float64Array(size * size)
for (let i = 0; i < size * size; i++) {
a[i] = Math.random()
b[i] = Math.random()
}
// Multiply using parallel workers
console.log('Computing with parallel workers...')
const start = performance.now()
const result = await ParallelMatrix.multiply(a, size, size, b, size, size)
const end = performance.now()
console.log(`Time: ${(end - start).toFixed(2)}ms`)
console.log(`Workers used: 4 (or auto-detected)`)
console.log(`First 4 elements: [${result.slice(0, 4).join(', ')}]`)
// Test matrix addition
console.log('\nParallel matrix addition...')
const addStart = performance.now()
const sum = await ParallelMatrix.add(a, b, size * size)
const addEnd = performance.now()
console.log(`Time: ${(addEnd - addStart).toFixed(2)}ms`)
// Test matrix transpose
console.log('\nParallel matrix transpose...')
const transposeStart = performance.now()
const transposed = await ParallelMatrix.transpose(a, size, size)
const transposeEnd = performance.now()
console.log(`Time: ${(transposeEnd - transposeStart).toFixed(2)}ms`)
}
/**
* Example 4: Custom Configuration
*/
async function customConfigurationExample() {
// Configure to use only JavaScript (no WASM)
console.log('Configuration 1: JavaScript only')
MatrixWasmBridge.configure({
useWasm: false,
useParallel: false
})
const size = 100
const a = new Float64Array(size * size).map(() => Math.random())
const b = new Float64Array(size * size).map(() => Math.random())
const start1 = performance.now()
await MatrixWasmBridge.multiply(a, size, size, b, size, size)
const end1 = performance.now()
console.log(`Time (JavaScript): ${(end1 - start1).toFixed(2)}ms\n`)
// Configure to use WASM only
console.log('Configuration 2: WASM only')
MatrixWasmBridge.configure({
useWasm: true,
useParallel: false,
minSizeForWasm: 0 // Always use WASM
})
const start2 = performance.now()
await MatrixWasmBridge.multiply(a, size, size, b, size, size)
const end2 = performance.now()
console.log(`Time (WASM): ${(end2 - start2).toFixed(2)}ms\n`)
// Configure for optimal performance
console.log('Configuration 3: Optimal (WASM + Parallel)')
MatrixWasmBridge.configure({
useWasm: true,
useParallel: true,
minSizeForWasm: 100,
minSizeForParallel: 1000
})
const largeSize = 500
const c = new Float64Array(largeSize * largeSize).map(() => Math.random())
const d = new Float64Array(largeSize * largeSize).map(() => Math.random())
const start3 = performance.now()
await MatrixWasmBridge.multiply(c, largeSize, largeSize, d, largeSize, largeSize)
const end3 = performance.now()
console.log(`Time (Optimal, ${largeSize}x${largeSize}): ${(end3 - start3).toFixed(2)}ms`)
}
/**
* Utility: Print matrix
*/
function printMatrix(data: Float64Array, rows: number, cols: number) {
for (let i = 0; i < rows; i++) {
const row = []
for (let j = 0; j < cols; j++) {
row.push(data[i * cols + j].toFixed(2))
}
console.log(' [' + row.join(', ') + ']')
}
}
// Run the examples
main().catch(console.error)