InsightSoftwareConsortium / ITK-Wasm

High performance spatial analysis in a web browser and across programming languages and hardware architectures
https://wasm.itk.org
Apache License 2.0
194 stars 49 forks source link

About Multithreading #1200

Open leecervan904 opened 1 month ago

leecervan904 commented 1 month ago

I want to know why WorkerPool is used instead of pthread for implementing multithreading? My application needs to share the same image data across multiple wasm threads, but I found that the pipeline implementation creates new data. Is my understanding incorrect? packages/core/typescript/itk-wasm/src/pipeline/internal/run-pipeline-emscripten.ts:

// ...
function setPipelineModuleInputArray (emscriptenModule: PipelineEmscriptenModule, dataArray: TypedArray | null, inputIndex: number, subIndex: number): number {
  let dataPtr = 0
  if (dataArray !== null) {
    dataPtr = emscriptenModule.ccall('itk_wasm_input_array_alloc', 'number', ['number', 'number', 'number', 'number'], [0, inputIndex, subIndex, dataArray.buffer.byteLength])
    emscriptenModule.HEAPU8.set(new Uint8Array(dataArray.buffer), dataPtr)
  }
  return dataPtr
}
// ...
thewtex commented 1 month ago

Hi @leecervan904 ,

The plan is also to support pthread threading also and fall back gracefully when SharedArrayBuffer, etc. is not available.