Repository navigation
[EXPERIMENT] WebAssembly build of OpenMS with JavaScript bindings (single- and multi-threaded) - #10564
[EXPERIMENT] WebAssembly build of OpenMS with JavaScript bindings (single- and multi-threaded)#10564timosachsenberg wants to merge 24 commits into
Conversation
…dings OpenMS can now be compiled to WebAssembly with Emscripten, so web applications (and Node.js programs) can use the library without native binaries. - CMake preset wasm32-emscripten-release and cmake/emscripten.cmake: static libraries, native WebAssembly exceptions, WebAssembly SIMD, larger stack; GUI, TOPP tools, OpenMP, curl, Thermo RAW, Bruker .d and ONNX are off (the options that cannot work in WebAssembly are rejected), HiGHS is the LP solver. - Dependencies from vcpkg: overlay triplet wasm32-emscripten (native wasm exceptions for every port), platform qualifiers in vcpkg.json, and fixed ports for Arrow, Thrift and HiGHS in vcpkg-overlays/emscripten (used only by the WebAssembly preset, so native builds keep their ABI hashes). - Library: new option WITH_CURL (default ON) makes libcurl optional; ExternalProcess, RWrapper, JavaInfo and PythonInfo report that programs cannot be started instead of using Boost.Process; OSW Parquet writes run deferred without threads; POSIX uint for musl in FLASHHelperClasses.h; no undefined 32-bit shift in ProSEAlgorithm. - src/openms_wasm: Embind bindings of spectra/experiments, file formats, chemistry, signal processing, feature finding, identifications and the simple search engine, with C++ exceptions turned into JavaScript errors and peak data as typed arrays; an npm package with TypeScript declarations and the shared data embedded; Node.js tests that reproduce the results of the corresponding class and TOPP tests; a demo web application. - CI workflow .github/workflows/wasm.yml builds, tests and packages it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
…fix fix - openms.d.ts now names the shape of every plain JavaScript object and array the bindings return or take (peaks, precursors, residues, parameter values, feature and identification records, ...) instead of 'any': js_types.h declares them as typed emscripten::val, js_types.cpp registers their TypeScript definitions. - The meta value accessors are shared templates in binding_utils.h. - examples/node/summary.mjs: summary of a peak file with Node.js (NODEFS). - README: reading a user's file in a Web Worker (WORKERFS), `using` support. - The Node.js programs of the build (class tests) drop the '.js' suffix per target: CMAKE_EXECUTABLE_SUFFIX was reset by project() calls in subdirectories. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
Dependencies: - The wasm32-emscripten triplet chainloads its own toolchain (vcpkg-overlays/toolchains/wasm32-emscripten.cmake): vcpkg applies VCPKG_*_FLAGS only through its platform toolchains, so with Emscripten.cmake chainloaded directly every CMake port was built without -fwasm-exceptions. Their catch blocks were compiled out: Xerces-C's internal XMLErrs::Codes exception escaped into OpenMS (OnDiscMSExperiment). Library (static linking and wasm32): - IndexedMzMLDecoder, MzMLSpectrumDecoder: initialise Xerces before DOM parsing. The initialiser object of CONCEPT/Init.cpp is linked from a static library only if other code calls into that file (crash in IndexedMzMLDecoder_test). - MetaboliteSpectralMatching.h: the comparator objects are inline variables (they were defined in every translation unit including the header: duplicate symbols when linked statically). - DataValue stores integers as Int64 instead of SignedSize (32 bits on wasm32), so 64-bit meta values are not truncated. - StringUtils: libc++ formats long double as double; write it with the digits of a double. - OpenSwathWorkflow's buffered OSW writer and ProSE's background spectrum loading run without a thread under Emscripten (no pthreads). - StopWatch: CPU time from Emscripten's process clock (times() is a stub returning 0). - PythonInfo: keep the "found but failed to run" wording of the native error. Build: - libcurl is a plain list entry of the private dependencies, so the -rpath-link hints for them (Linux) still see it; ENABLE_UPDATE_CHECK is off without WITH_CURL. Bindings: JavaScript numbers beyond 32 bits become double parameters (not truncated ints) and 64-bit integer meta values. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
…rated value explicitly IDScoreGetterSetter::setScores_ looked a NaN group score up in the score-to-FDR map. NaN is outside the ordering of std::map<double, double>, so the lookup is undefined: libstdc++ returns the first/last element (the worst calibrated score, which FalseDiscoveryRate_test expects), libc++ (Emscripten) returns an element in between (q-value 1/3 instead of 2/3). Assign the worst calibrated value without a lookup. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
…formats long double like libOpenMS - openms_emscripten_node_program() (cmake/emscripten.cmake) makes an executable a Node.js program (NODERAWFS, no '.js' suffix); the OpenSwathAlgo class tests, which do not go through openms_add_executable_compiler_flags, call it too (ctest did not find them under their suffixed names). - ClassTestUtils.h mirrors the libc++ long double formatting of StringUtils, as ClassTestUtils_test requires. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
… status - File::getUniqueName(): getpid() is 42 in every WebAssembly module, so the temporary file names of Node.js programs run in parallel (ctest -j) could collide (UpdateCheck_test failed intermittently). Under Emscripten a random number drawn once per module instance replaces the pid. - README: which class tests fail on WebAssembly and why (threads, external programs and network, TOPP tool registry); StopWatch CPU times are elapsed times. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
…readed Emscripten ships LLVM's OpenMP runtime, but it runs on pthreads, which this build does not use. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
…threads - vcpkg triplet wasm32-emscripten-mt (dependencies with -pthread) and CMake preset wasm32-emscripten-mt-release, which sets the new OPENMS_WASM_THREADS option: -pthread for all code, OpenMP with Emscripten's libomp (FindOpenMP preset to plain -fopenmp), no nested OpenMP. Configuring with a triplet of the other threading mode fails with a hint. - The fallbacks for builds without threads (OSW writer thread, Parquet writes, ProSE spectrum loading) check __EMSCRIPTEN_PTHREADS__ instead of __EMSCRIPTEN__. - The JavaScript module of such a build is @openms/openms-wasm-mt. threads.js sets the number of threads (Module.numThreads; Node.js: OMP_NUM_THREADS or all cores; browsers: half the cores, at most 4, and 1 on pages that are not cross-origin isolated) and sizes the pool of Web Workers at run time (none in Node.js and on non-isolated pages: a fixed pool makes the module hang at startup there). - getThreadingInfo() in both modules; tests/threads.test.mjs compares results at 1 and 4 threads; README: threads. Validated so far in the single-threaded build only (JS tests: 26 pass, the thread tests skip); the threaded build is being validated under Node.js. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
The previous commit dropped it from the options the WebAssembly build switches off, so a new build directory tried to fetch the .NET-based Thermo RAW bridge. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
Emscripten's default allocator serializes all threads on one lock. Measured in Node.js on 4 cores (best of 3), a SimpleSearchEngine search (1684 spectra of a BSA run against an E. coli target-decoy database) took 1835 ms on 1 thread, 3265 ms on 2 and 6269 ms on 4 threads; with -sMALLOC=mimalloc it takes 1458, 1013 and 880 ms (single-threaded module: 1601 ms). The module grows by 0.3 MB. README: the measured gains. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
…aptured variables
LLVM's OpenMP runtime passes at most 32 variables to a parallel region on WebAssembly
(__kmp_invoke_microtask); the per-spectrum loop of ProSEAlgorithm::postProcessHits_
captured 53, so the threaded WebAssembly build exited ("Too many args to microtask: 53!")
in ProSEAlgorithm_test. The loop body is now a lambda, which the region captures as one
variable. A scan of the OpenMP regions of the library (LLVM IR of all 39 source files
with OpenMP pragmas, argument counts of __kmpc_fork_call) found no other region above
28. ProSEAlgorithm_test passes in the threaded and the single-threaded build.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
- .github/workflows/wasm.yml builds, tests and packs both variants (matrix: wasm32-emscripten-release and wasm32-emscripten-mt-release, each with its own vcpkg binary cache and ccache), and runs the browser tests in Playwright's Chromium. - tests/browser/run.mjs serves the package with and without cross-origin isolation (COOP/COEP) and runs the module on the page and in a Web Worker: it checks that the module starts, the threads getThreadingInfo() reports, feature finding and a database search (TOPP_SimpleSearchEngine_1), and that a Worker keeps the page responsive; --benchmark adds a larger search at 1, 2 and 4 threads. In Chromium 141 all scenarios pass for both packages. The threaded module runs single-threaded on pages that are not isolated (no hang at startup) and with the requested threads on isolated ones, on the page as in a Worker, with identical results; in a Worker the page was never blocked longer than 12 ms. Database search (1684 spectra, E. coli target-decoy), in a Worker: 1832 ms on 1 thread, 1342 ms on 2, 1233 ms on 4. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
- @openms/openms-wasm holds the single-threaded (st/) and the multi-threaded build (mt/); its entry point (loader/index.mjs) starts the threaded build where threads are available (Node.js; browsers: cross-origin isolated pages), else the single-threaded one. threadSupport() tells before loading whether threads are available and why not; getThreadingInfo() of the module tells which build runs, and with how many threads; asking for numThreads > 1 where there are none logs one warning with the reason. threads: true/false forces a build; ./st and ./mt import one directly. - assemble_package.cmake assembles the package from the packages of the two builds. - CI: a package job assembles it from the artifacts of both builds, runs all tests of the bindings through the loader in Node.js and the browser tests in Chromium, and uploads the npm package. - tests/loader.test.mjs; the browser tests run the assembled package through the loader (threaded build on isolated pages, single-threaded with one warning elsewhere). Locally: all 32 Node.js tests pass through the loader, and all 6 browser scenarios in Chromium 141. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
The threaded build is faster with mimalloc than the single-threaded one with dlmalloc, also on one thread (database search: 1458 ms vs 1601 ms). The full class test suite of the single-threaded build with mimalloc is running; this commit is reverted if it shows problems. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
Single-threaded build with mimalloc: the class tests give the same results as with dlmalloc (704 of 714 pass; the 10 failures are the known ones: threads, external programs, network, TOPP tool registry), the tests of the bindings pass, and so do the browser tests in Chromium for the build and for the assembled package. Node.js, best of 3: database search 1549 ms (dlmalloc: 1601), loading a 14 MB mzML file 184 ms (238), feature finding 227 ms (223). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
The rsync-cache action reads the vcpkg binary cache over HTTP with rclone (the rsync server only accepts writes); without rclone both WebAssembly jobs failed before building. Installed as tools/ci/deps-ubuntu.sh does for the native CI. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v
|
just an experiment at this point @okohlbacher but sizes are not too bad. |
…orted m/z range, MS level 0, node lookup, cache and output guards - examples/browser: the maximum of a peak array is computed with a loop; Math.max(...array) exceeds the call stack for profile spectra. The demo deletes its FileHandler and MzMLFile on every path. - bind_kernel: getSpectrumSummary() takes minMZ/maxMZ from the peaks (std::minmax_element), not from front()/back(): spectra need not be sorted by m/z. get2DPeakData() treats MS level 0 as all levels, like getClosestSpectrumInRT(). - src/openms_wasm/CMakeLists.txt: find_program() gets the directories of EMSDK_NODE and NODE_JS_EXECUTABLE as HINTS, not the binaries (HINTS are directories). - emscripten.cmake: a threaded build with a cached MT_ENABLE_OPENMP=OFF (build directory configured without threads first) is a configuration error instead of a silent fallback. - assemble_package.cmake: refuses to delete an OUTPUT that is not a previous package. - tests: spectrum summary of peaks not sorted by m/z; get2DPeakData with MS level 0. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Example: the demo web application (
|
# Conflicts: # CHANGELOG
timosachsenberg
left a comment
There was a problem hiding this comment.
Reviewed the Wasm build/package setup, automatic single-/multi-thread loader, shared binding conversion helpers and representative spectrum ownership/bounds paths at 53cb381. No new substantive finding in those paths. Existing Wasm, native-build and wheel workflows passed. This is a scoped source review of the large experimental change; I did not compile a fresh Emscripten build or exercise every exported API/browser combination locally.
…openms-library-1mutfn Conflict in vcpkg.json: develop (#10563) makes HiGHS the default LP solver and moves COIN-OR to the opt-in "lp-coin" feature. Taken from develop; the branch keeps only curl as "!emscripten" (HiGHS is now unconditional, so the emscripten-only HiGHS entry and the "!emscripten" COIN-OR entries go). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…sm-prose-webapp Brings in develop (with #10578 + #10602 as squash 2ef8693 and #10644 as 8528cf2) through the updated #10564 branch. This branch had carried a pre-squash version of #10578/#10602 (claude/prose-merged-in-library) and a cherry-pick of #10644. Resolution: every library path is develop's version plus only the library commits of this branch that develop does not have (ProSEAlgorithm:: pinEnzymeName() and modMatchTolerance() public, ML::PEPTDEEP_DEFAULT_BATCH_SIZE, the joined reader thread, peptdeep:batch_size 128 in WebAssembly, the PeptDeepRescoring NCE fix, ONNXPredictorBase without the memory pattern, the ONNX environment changes for WebAssembly threads). - ProSEAlgorithm.h, ProSEAlgorithm_test.cpp, src/tests/topp/CMakeLists.txt (conflicts) and ProSEAlgorithm.cpp (auto-merged, but with rescoreWithPercolator(), applyDeferredPsmFDR() and applyPsmFDR_() twice, since the squash moved them): develop's version with those commits on top. - src/tests/topp/ProSE_13_prepare.cmake: removed; develop's Test 13 copies its inputs with add_test(... -E copy) instead. - CHANGELOG: develop's text with this branch's entries. - #10644 (7abcb28): identical to develop's, no difference left. Co-Authored-By: Claude Opus 5.5 <[email protected]>
timosachsenberg
left a comment
There was a problem hiding this comment.
Reviewed the merge update since the previous review at 53cb381 through 19a6e1a. The additional commits bring in develop changes; the WebAssembly implementation itself is unchanged in this update. No additional substantive finding. WebAssembly, build, and wheel CI passed. I did not run a local browser build.
Conflicts and adaptations to develop: - ProSE was split into src/openms/source/ANALYSIS/ID/PROSE/: the three WebAssembly changes of ProSEAlgorithm.cpp move there (the parallel region of the PSM annotation as a lambda, for the 32-variable limit of LLVM's OpenMP on WebAssembly; 64-bit lookup counts on wasm32; a deferred read without threads, now also without a ChunkHelp, whose help() would wait for a reader that has not started). - libcurl is a required dependency of develop (HTTPInputSource for http/https/ftp inputs): it stays optional (WITH_CURL); without it, HTTPInputSource reports an error when a transfer is started. - Xerces-C is gone in develop (libxml2): linked libraries follow develop. - cmake/linker.cmake: the WebAssembly build keeps emcc's linker (OPENMS_LINKER=default); LP_SOLVER is no longer set (HiGHS only). - CHANGELOG: develop's (entries are generated from merged PRs). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0149aXmXLttDcVnqdHzuucnY
develop reads unimod.xml and the OBO files in parts on helper threads, loads the modification providers on helper threads (ModificationsDB) and probes the OpenMP runtime with a thread (SysInfo::ompThreadCountIsGlobal()), each joined right away. In WebAssembly, a thread started on the browser's main thread beyond the module's pool of Web Workers starts only when the main thread returns to its event loop, which it does not while it joins: the multi-threaded module hung in the Chromium test on the page (the Node.js tests, with a pool of all cores, passed). In WebAssembly these read as before (as a whole, one after the other); the probe answers for LLVM's libomp, which keeps a number of threads per thread. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0149aXmXLttDcVnqdHzuucnY
Brings in the boost-locale overlay for CMake 4.5 (#10904), without which vcpkg's boost-locale no longer configures on the Windows runners (CMake 4.5.0; build-wheels-windows failed in its configure). Conflict: CMakeLists.txt, develop removed WITH_HDF5 (#10937); this branch keeps only its option WITH_CURL there. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0149aXmXLttDcVnqdHzuucnY
…test since develop's mass trace fixes The test reproduces the native class test (the same input, the default MassTraceDetection and ElutionPeakDetection, 13C isotope scoring, then the default): develop changed its counts from 83 and 81 to 80 and 78 (ElutionPeakDetection no longer extrapolates its smoothing, #10899; the isotope assembly defaults of #10901), and the WebAssembly build gives the native result (80). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0149aXmXLttDcVnqdHzuucnY




Description
Builds the OpenMS library for WebAssembly (Emscripten) and adds JavaScript bindings. A web app or Node.js program can then use OpenMS without native binaries, a server or Python. In a browser, the user's data stays on their machine.
Build (presets
wasm32-emscripten-releaseandwasm32-emscripten-mt-release)wasm32-emscripten/wasm32-emscripten-mt. A chainloaded toolchain (vcpkg-overlays/toolchains/) passes the triplet flags (-fwasm-exceptions,-pthread) to CMake ports, because vcpkg applies them only through its own toolchains.vcpkg.jsonleaves out curl, boost-process, COIN-OR and arrow's s3 feature on Emscripten.cmake/emscripten.cmakeholds all WebAssembly settings:WITH_CURL(default ON). Without curl,NetworkGetRequestandMascotRemoteQueryreport an error when called. Native builds don't change.JavaScript package (
src/openms_wasm)Paramopenms.d.ts)share/OpenMSdata embedded in the module@openms/openms-wasm, with two builds:st/is single-threadedmt/runs OpenMP andstd::threadon WebAssembly threadscreateOpenMS()picks the build:numThreads > 1logs one console warning.threadSupport()andgetThreadingInfo()report which build runs and why.openms.wasm(6.7 MB gzipped), including the embedded share data.Library changes found by running the class tests on WebAssembly. They also apply to native builds and are proposed separately in #10558:
IndexedMzMLDecoderandMzMLSpectrumDecoder. Statically linked, the initializer did not run.MetaboliteSpectralMatching.h(ODR)DataValue: the size field isInt64, so 64-bit values no longer truncate.StringUtils,ClassTestUtils)std::map, which is undefined behavior.WebAssembly-only changes in the library
StopWatchusesclock_gettimeon WebAssembly.File: on WebAssembly, temp file names use a random instance id instead of the pid, which every instance shares.ExternalProcess,JavaInfo,PythonInfo,RWrapperandNetworkGetRequestreport clear errors where there is no subprocess or network.ProSEAlgorithm: one OpenMP region captured 53 variables. LLVM's OpenMP runtime on wasm32 allows at most 32, so the loop body is now a lambda. A scan of the compiled library found no other region above 28.CI (
.github/workflows/wasm.yml)packagejob assembles the npm package from both builds, tests it through the loader in Node.js and Chromium, and runsnpm pack.Test results (local)
Class tests built for WebAssembly and run in Node.js:
ExternalProcess,TOPPExternalToolBase,Network;Filelooks for programs next to its own executableToolHandlerstd::thread:GlobalExceptionHandler,LogConfigHandler,LogStream,UniqueIdGenerator,File,TOPPBaseJS tests: 32/32, through the loader of the assembled package
Chromium, on pages with and without cross-origin isolation, on the page and in a Web Worker: all scenarios pass for the single-threaded build, the multi-threaded build and the assembled package. Each checks the reported threads, feature finding results, search hits, and that a Worker never blocks the page for more than 500 ms.
Benchmark: SimpleSearchEngine, 1684 spectra against an E. coli target-decoy database
Not covered yet
The details are in
src/openms_wasm/README.md.Checklist
🤖 Generated with Claude Code
https://claude.ai/code/session_01FpXYKBCQpAEKMLsNvSpo8v