Skip to content
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

Question : Emscripten integration #98

Closed
thmax opened this issue Apr 26, 2022 · 10 comments · Fixed by #103
Closed

Question : Emscripten integration #98

thmax opened this issue Apr 26, 2022 · 10 comments · Fixed by #103

Comments

@thmax
Copy link

thmax commented Apr 26, 2022

Hi !

I'm trying to integrate the library with emscripten and i'm a bit struggling.
I tried a lot of stuff but nothing worked.

Does anyone know a proper way to do it ?

Thanks a lot

@elalish
Copy link
Owner

elalish commented Apr 26, 2022

I certainly don't, but having an emscripten build would be awesome! Feel free to open a draft PR with what you've tried so we can follow along.

@pca006132
Copy link
Collaborator

Wondering if this is possible with nvcc. Probably easier after switching to C++20 parallel algorithms?

@thmax
Copy link
Author

thmax commented May 1, 2022

I think it's possible, this repo https://github.com/jstepien/boids seems to achieve it but i didn't try to compile it.
It will be easier indeed with c++ 20

@pca006132
Copy link
Collaborator

It seems to me that the make file is compiling the cpu simulation to llvm IR using clang, and then use emscripten to compile the LLVM IR to JavaScript. I don't think nvcc can emit LLVM IR (at least I don't see an option for this).

@elalish
Copy link
Owner

elalish commented May 2, 2022

You can build this library without nvcc, as long as you're not targeting the CUDA backend. Thrust is just a header-only C++ lib. That may be a path forward.

What I'd really love to see is emscripten taking C++20 parallel algorithms and converting them to GPU-accelerated WASM (WebGPU?). I would imagine someone somewhere is thinking about that, but I have no idea if/when it might ship.

@pca006132
Copy link
Collaborator

I've tried using the OpenMP backend and CUDA backend for some tests, it seems that CUDA is quite fast for some larger meshes comparing to OpenMP (which is expected). Wondering if GPU acceleration will be dropped if the goal is to switch to c++ 20 parallel algorithms later.

@elalish
Copy link
Owner

elalish commented May 2, 2022

Yes, GPU parallelization is definitely a goal of this library, and I should really be saying C++17, not C++20. std::execution::par targets GPUs, and eventually more of them than CUDA.

@pca006132
Copy link
Collaborator

OK I have successfully compiling and running the code with emscripten. You have to:

  1. Change all the .cu files to .cpp. (actually not sure if this is needed, but I need to do this to use clang++ to build the project instead of using nvcc)
  2. Comment out modules that uses libraries that you don't plan to deal with now, e.g. assimp. I think we should be able to use this, but I am too lazy to deal with it now.
  3. Set include path to thrust and glm explicitly in the CMakeLists.txt, otherwise emscripten won't be able to find them.
  4. Specify emscripten toolchain file: CMAKE_TOOLCHAIN_FILE=path/to/emscripten/emscripten/version/cmake/Modules/Platform/Emscripten.cmake (from https://github.com/adevaykin/minimal-cmake-emscripten-project)
  5. Build with MANIFOLD_USE_CPP=on.

You may want to build with -DCMAKE_CXX_FLAGS="-s ALLOW_MEMORY_GROWTH=1". I tried building without this argument and the perfTest failed with out of memory error. Not sure if this is the appropriate way to do this because em++ is complaining about linker setting ignored during compilation, but at the end it seems working.

@elalish
Copy link
Owner

elalish commented May 3, 2022

That's awesome! Fantastic work. It would be great to integrate that into our CMake scripts somehow, maybe even get it building/testing on our Github Actions CI?

@pca006132
Copy link
Collaborator

Yes this can be integrated into the CMake script and test it on GitHub Actions CI, but this requires some modifications:

  1. Change all .cu files to .cpp. I think we can still get nvcc to treat them as CUDA source files by set_source_files_properties(${CUDA_SOURCES} PROPERTIES LANGUAGE CUDA), haven't yet tried this though.
  2. We may need to put the dependencies as git submodules in the third_party directory, or ask users to specify the path to these dependencies manually via CMake variables when compiling with emscripten. I tried using find_package for packages like glm and thrust but this does not work, I am not familiar with CMake and C++ so not sure if I am doing this correctly.

Also, it will be nice if we can specify the gtest dependency as git a submodule rather than using CMake to download it. I can package this with NixOS for reproducible builds, but it forbids network connection for pure builds so CMake will not be able to download gtest from the web.

I just tried building assimp with emscripten and it seems that it builds fine out of the box for v5.2.3. Will try to link against it and do some tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants