You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to enable/compile with openmp support ?
For this task, I was thinking more specifically about execution in the nodejs environment and not the browser.
Reacted by ted537, Johannes Mols, Filipi Nascimento Silva, Shashwat, Innovgame, Martin Robinson, Jan Vorisek, George Gkasdrogkas, Agriya Khetarpal, Vladyslav Huba and 1 more
I have tried to build with the following flags, but no luck/love :
source /usr/local/emsdk/emsdk_env.sh
export EMSCRIPTEN=$EMSDK/upstream/emscripten
emcmake cmake -DCMAKE_INSTALL_PREFIX:PATH=$EMSCRIPTEN/system -DCMAKE_DISABLE_FIND_PACKAGE_CUDA=TRUE ..
Hi @nihui, I am trying to use simpleomp in my project with emscripten. But problem is that the simpleomp.cpp is dependent on platform.h but actually platform.h.in exists there instead of the platform.h. Can you please guide me about how to add the source of simpleomp correctly to my project?
flag -pthread must be used to compile every single translation unit
when linking, specify e.g. -sPTHREAD_POOL_SIZE=navigator.hardwareConcurrency, otherwise if you use OpenMP in the main thread, it will deadlock or something
I compile simpleomp.cpp separately with emcc -DNCNN_SIMPLEOMP=1 -c simpleomp.cpp, which will generate an object simpleomp.o, which can then be linked in the final application.
I think the issue should remain open. Emscripten itself doesn't seem to impose any limitation itself, but:
Emscripten should provide an OpenMP runtime itself, without requiring linking a third-party
Is this something that other compilers/toolchain do? In general I would prefer to keep stuff in third-party rather than comlicating emscripten itself, whenever this possible anyway.
A more fully-featured runtime is highly desirable, some very basic features are missing
e.g. if clause that enables/disables parallelism dynamically
flag -fopenmp should imply -pthread
Is -fopenmp an existing clang flag or something you are adding somehow>
one way is to advance ncnn's implementation. however, as mentioned, the way to go would be to patch and use LLVM's existing runtime
Yes, anything you can upstream in LLVM would be great, and will then be in inherited by emscripten automatically.
I will make some personal effort in less than a year
I think the issue should remain open. Emscripten itself doesn't seem to impose any limitation itself, but:
Emscripten should provide an OpenMP runtime itself, without requiring linking a third-party
Is this something that other compilers/toolchain do? In general I would prefer to keep stuff in third-party rather than comlicating emscripten itself, whenever this possible anyway.
yeah. GCC does, Clang does, and MSVC does. not only they provide the runtime, but because OpenMP is an extension to C, C++ and Fortran, compilers also perform code transformation to realize the parallelism. Emscripten transforms the code correctly when -fopenmp is passed. this transformed code calls certain functions, this is where the runtime comes into play
A more fully-featured runtime is highly desirable, some very basic features are missing
e.g. if clause that enables/disables parallelism dynamically
flag -fopenmp should imply -pthread
Is -fopenmp an existing clang flag or something you are adding somehow>
yes. it is -fopenmp on GCC and Clang, and it is /openmp on MSVC. it is already present in Emscripten
yeah. GCC does, Clang does, and MSVC does. not only they provide the runtime, but because OpenMP is an extension to C, C++ and Fortran, compilers also perform code transformation to realize the parallelism. Emscripten transforms the code correctly when -fopenmp is passed. this transformed code calls certain functions, this is where the runtime comes into play
I see, in that case yes we should provide the same support that clang and gcc do.
Activity
flatmax commentedon Apr 14, 2021
It seems that you can builde libiomp out of tree, but will it work ?
https://openmp.llvm.org/README.txt
I have tried to build with the following flags, but no luck/love :
source /usr/local/emsdk/emsdk_env.sh
export EMSCRIPTEN=$EMSDK/upstream/emscripten
emcmake cmake -DCMAKE_INSTALL_PREFIX:PATH=$EMSCRIPTEN/system -DCMAKE_DISABLE_FIND_PACKAGE_CUDA=TRUE ..
nihui commentedon May 8, 2021
In ncnn project, we implement a minimal openmp runtime for webassembly target
https://github.com/Tencent/ncnn/blob/master/src/simpleomp.cpp
It only works for
ted537 commentedon Dec 1, 2021
@nihui Impressive! How does this integrate with the OpenMP pragma? Currently, I'm guessing that
__kmpc_fork_call()is the central componentnkbai commentedon Mar 26, 2022
@nihui are there any documents about how to use simpleomp in emscripten?
nihui commentedon Mar 26, 2022
add simpleomp source to your project and compile with -fopenmp
Smithangshu commentedon Jul 14, 2023
Hi @nihui, I am trying to use simpleomp in my project with emscripten. But problem is that the simpleomp.cpp is dependent on platform.h but actually platform.h.in exists there instead of the platform.h. Can you please guide me about how to add the source of simpleomp correctly to my project?
bottle2 commentedon Jan 17, 2025
hey @Smithangshu and others
I successfully used ncnn's
simpleomp.cpp!I had to copy some parts of code. see:
I also created this header to solve more linker errors:
in my particular case, my application is written in C and I just have a Makefile (I don't know CMake). hence
https://github.com/bottle2/raytracing/blob/master/Makefile
https://emscripten.org/docs/porting/pthreads.html
as seen in my Makefile, some caveats:
-pthreadmust be used to compile every single translation unit-sPTHREAD_POOL_SIZE=navigator.hardwareConcurrency, otherwise if you use OpenMP in the main thread, it will deadlock or somethingsimpleomp.cppseparately withemcc -DNCNN_SIMPLEOMP=1 -c simpleomp.cpp, which will generate an objectsimpleomp.o, which can then be linked in the final application.that's it. works like a charm
MuTsunTsai commentedon Nov 14, 2025
Based on @bottle2's answer, I created this repo SimpleOMP that includes prebuilt static library and an usage example.
sbc100 commentedon Nov 14, 2025
Wow, thats very cool. Thanks for working on that.
Is there anything we need to change on the emscripten side or can this issue be closed now?
bottle2 commentedon Nov 15, 2025
I think the issue should remain open. Emscripten itself doesn't seem to impose any limitation itself, but:
ifclause that enables/disables parallelism dynamically-fopenmpshould imply-pthreadone way is to advance
ncnn's implementation. however, as mentioned, the way to go would be to patch and use LLVM's existing runtimeI will make some personal effort in less than a year
sbc100 commentedon Nov 15, 2025
Is this something that other compilers/toolchain do? In general I would prefer to keep stuff in third-party rather than comlicating emscripten itself, whenever this possible anyway.
Is
-fopenmpan existing clang flag or something you are adding somehow>Yes, anything you can upstream in LLVM would be great, and will then be in inherited by emscripten automatically.
Thanks! I'll leave this open then
bottle2 commentedon Nov 15, 2025
yeah. GCC does, Clang does, and MSVC does. not only they provide the runtime, but because OpenMP is an extension to C, C++ and Fortran, compilers also perform code transformation to realize the parallelism. Emscripten transforms the code correctly when
-fopenmpis passed. this transformed code calls certain functions, this is where the runtime comes into playyes. it is
-fopenmpon GCC and Clang, and it is/openmpon MSVC. it is already present in Emscriptensbc100 commentedon Nov 15, 2025
I see, in that case yes we should provide the same support that clang and gcc do.
MuTsunTsai commentedon Nov 17, 2025
Also, my current solution doesn't work on all projects. In some projects I encountered this error
during wasm-opt, and I have no idea why nor how to solve it.
Update:
Ah I see now, it's
memcpythat is causing this. I either not use it or add the-mbulk-memoryoption.MuTsunTsai commentedon Nov 17, 2025
Also I just added support for the
if()clause to SimpleOMP, as that looks quite desirable to me. I might add more soon.JAicewizard commentedon Dec 10, 2025
I have openmp workin in emscripten. I think it only needs propper detection of when to link
sbc100 commentedon Dec 10, 2025
I'm not sure what this means but if you would like to send a PR that would be great.
Add openmp library from llvm (#27073)