-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Combining wasm workers and pthreads leads to excessive worker creation #20279
Comments
i experience the same issue: user@DESKTOP-68JUJRU:~/opencv/emsdk$ python ./opencv/platforms/js/build_js.py \
--cmake_option="-DCMAKE_TOOLCHAIN_FILE=/home/user/opencv/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" \
--cmake_option="-DCMAKE_CROSSCOMPILING_EMULATOR=/home/user/opencv/emsdk/node/16.20.0_64bit/bin/node" \
build_wasm \
--clean_build_dir \
--build_wasm \
--simd \
--threads \
--build_flags="-s EXPORT_ES6=1 " \
--disable_single_file so that does execute this finally: /usr/bin/cmake -E cmake_link_script CMakeFiles/opencv_js.dir/link.txt --verbose=1
/home/user/opencv/emsdk/upstream/emscripten/em++ -s WASM=1 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -msimd128 -s EXPORT_ES6=1 -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -pthread -Qunused-arguments -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG -Wl,--gc-sections --memory-init-file 0 -s TOTAL_MEMORY=128MB -s WASM_MEM_MAX=4GB -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s EXPORT_NAME="'cv'" -s DEMANGLE_SUPPORT=1 -s FORCE_FILESYSTEM=1 --use-preload-plugins --bind --post-js /home/user/opencv/emsdk/opencv/modules/js/src/helpers.js -Wno-missing-prototypes @CMakeFiles/opencv_js.dir/objects1.rsp -o ../../bin/opencv_js.js @CMakeFiles/opencv_js.dir/linklibs.rsp the idea with ENVIRONMENT_IS_WASM_WORKER didn't work as its undefined, any hint how to fix this? |
@simonbuehler looks like you are just using pthreads and not wasm workers, is that right? If so I don't think this is the same issue. |
@sbc100 oops you are right, my bad - the symptoms are the same though. |
i think i found the reason: when adding a additional worker between the vue component and the opencv workers so the UI stays responsive like this: created() {
this.worker = new Worker(
new URL( "/src/js/mertensWorker.js", import.meta.url),
{ type: "module" }
);
this.worker.addEventListener("message", this.handleWorkerMessage);
},
unmounted() {
if (this.worker) {
this.worker.terminate();
}
}, and in there : console.log("WebWorker initialized");
// Importieren von OpenCV.js
import cv from "./opencv";
console.log(cv.getBuildInformation());
//using cv ... initially 4 workers are created but on invocation of the first cv method the additional ones get created, strange. |
Sure, sounds like a seprate issue. If you build opencv with |
Thanks, this is a great observation. Posted PR #21290 , I think that should fix this problem. |
I am using both pthreads and wasm workers, and I see more workers being created than necessary.
The reason seems to be the following:
PThread.init
checks forENVIRONMENT_IS_PTHREAD
first, in all other casesPthread.initMainThread
is called. To me it looks like there should be an additional check forENVIRONMENT_IS_WASM_WORKER
in the generated js:The current init function leads to
initMainThread
being called for each wasm worker, which in turn will callPThread.init
to allocate the number of workers specified byPTHREAD_POOL_SIZE
multiple times..It seems to work correctly with the suggested change, however, I am not familiar enough with the internals of Emscripten to make a judgement.
Version of emscripten/emsdk: 3.1.46
Command line in full:
emcc ./src/main.c ...list if source files
-I ./include/ ^
-g3 ^
--source-map-base ./ ^
-gsource-map ^
-s ALLOW_MEMORY_GROWTH=1 ^
-s ENVIRONMENT=web,worker ^
--shell-file ./index_template.html ^
-s SUPPORT_ERRNO=0 ^
-s MODULARIZE=1 ^
-sMALLOC=emmalloc ^
-sWASM_WORKERS ^
-sOFFSCREEN_FRAMEBUFFER ^
-pthread ^
-s PTHREAD_POOL_SIZE=2 ^
-s "EXPORT_NAME='wasmMod'" ^
-s EXPORTED_FUNCTIONS="['_malloc','_free','_main']" ^
-s EXPORTED_RUNTIME_METHODS="['cwrap','UTF16ToString','UTF8ToString','stringToUTF8','allocateUTF8','AsciiToString']" ^
-o index.html
The text was updated successfully, but these errors were encountered: