First Commit
This commit is contained in:
51
shared.js
Normal file
51
shared.js
Normal file
@@ -0,0 +1,51 @@
|
||||
export async function setupWebAssembly(wasmBuffer) {
|
||||
|
||||
const memory = new WebAssembly.Memory({
|
||||
initial: 2747, // ~172 MB
|
||||
maximum: 4096 // ~256 MB
|
||||
});
|
||||
|
||||
|
||||
const { instance } = await WebAssembly.instantiate(wasmBuffer, {
|
||||
env: {
|
||||
memory: memory,
|
||||
emscripten_notify_memory_growth: (index) => {
|
||||
// This function is called when the WASM module wants to grow memory.
|
||||
// You can log it or handle it as needed.
|
||||
console.log(`Memory grew to ${index / 65536} pages`);
|
||||
},
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
const ARRAY_SIZE = 15000576;
|
||||
const a = instance.exports.alloc_float_array(ARRAY_SIZE);
|
||||
const b = instance.exports.alloc_float_array(ARRAY_SIZE);
|
||||
const c = instance.exports.alloc_float_array(ARRAY_SIZE);
|
||||
if (!a || !b || !c) throw new Error("Failed to allocate memory");
|
||||
|
||||
// Benchmark initialization
|
||||
const initStart = performance.now();
|
||||
instance.exports.init_arrays(a, b);
|
||||
const initTime = performance.now() - initStart;
|
||||
|
||||
// Benchmark calculation
|
||||
const calcStart = performance.now();
|
||||
instance.exports.calculate(a, b, c);
|
||||
const calcTime = performance.now() - calcStart;
|
||||
|
||||
console.log(`Array initialization time: ${initTime.toFixed(3)} ms`);
|
||||
console.log(`Calculation time: ${calcTime.toFixed(3)} ms`);
|
||||
console.log(`Total time: ${(initTime + calcTime).toFixed(3)} ms`);
|
||||
|
||||
// Sample output
|
||||
const mem = new Float32Array(instance.exports.memory.buffer);
|
||||
for (let i = 0; i < 10; i++) {
|
||||
console.log(`c[${i}] = ${mem[c/4 + i]}`);
|
||||
}
|
||||
|
||||
// Clean up
|
||||
instance.exports.dealloc_float_array(a);
|
||||
instance.exports.dealloc_float_array(b);
|
||||
instance.exports.dealloc_float_array(c);
|
||||
}
|
||||
Reference in New Issue
Block a user