First Commit
This commit is contained in:
74
shared_pthread.js
Normal file
74
shared_pthread.js
Normal file
@@ -0,0 +1,74 @@
|
||||
export async function setupWebAssembly( wasmBuffer, workerData, arraySize, threadCount ) {
|
||||
|
||||
const { start, end, sabA, sabB, sabC } = workerData;
|
||||
|
||||
var sizePerThread = arraySize / threadCount;
|
||||
|
||||
// const a = new Float32Array(sabA);
|
||||
//const b = new Float32Array(sabB);
|
||||
//const c = new Float32Array(sabC);
|
||||
|
||||
|
||||
const memory = new WebAssembly.Memory({
|
||||
initial: 2747, // ~172 MB
|
||||
maximum: 4096, // ~256 MB
|
||||
shared: true
|
||||
});
|
||||
|
||||
|
||||
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(sizePerThread);
|
||||
const b = instance.exports.alloc_float_array(sizePerThread);
|
||||
const c = instance.exports.alloc_float_array(sizePerThread);
|
||||
|
||||
//if (!a || !b || !c) throw new Error("Failed to allocate memory");
|
||||
|
||||
/*
|
||||
// Benchmark initialization
|
||||
const initStart = performance.now();
|
||||
|
||||
instance.exports.init_arrays_range(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