-
Notifications
You must be signed in to change notification settings - Fork 0
Claude/typescript wasm refactor 019dsze n rq exsgy5o kfu3m vu #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d51c7d5
2c000a6
c33db91
63a4b82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| { | ||
| "extends": "assemblyscript/std/assembly.json", | ||
| "targets": { | ||
| "release": { | ||
| "outFile": "lib/wasm/index.wasm", | ||
| "textFile": "lib/wasm/index.wat", | ||
| "sourceMap": true, | ||
| "optimizeLevel": 3, | ||
| "shrinkLevel": 2, | ||
| "converge": true, | ||
| "noAssert": true, | ||
| "runtime": "stub", | ||
| "importMemory": true, | ||
| "initialMemory": 256, | ||
| "maximumMemory": 16384, | ||
| "memoryBase": 0, | ||
| "exportRuntime": true | ||
| }, | ||
| "debug": { | ||
| "outFile": "lib/wasm/index.debug.wasm", | ||
| "textFile": "lib/wasm/index.debug.wat", | ||
| "sourceMap": true, | ||
| "debug": true, | ||
| "runtime": "stub", | ||
| "importMemory": true, | ||
| "initialMemory": 256, | ||
| "maximumMemory": 16384 | ||
| } | ||
| }, | ||
| "options": { | ||
| "bindings": "esm", | ||
| "exportStart": "_start" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,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`) | ||||||||
|
||||||||
| console.log(`Time: ${(transposeEnd - transposeStart).toFixed(2)}ms`) | |
| console.log(`Time: ${(transposeEnd - transposeStart).toFixed(2)}ms`) | |
| console.log(`First 4 elements: [${transposed.slice(0, 4).join(', ')}]`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable sum.