andrjohns / StanEstimators

Estimate Parameters for Arbitrary R Functions using 'Stan'
Other
20 stars 1 forks source link

WASM support #7

Open andrjohns opened 8 months ago

andrjohns commented 8 months ago

Package fails to compile due to TBB architecture/machine detection issues, investigate appropriate TBB macros for WASM:

/** This header provides basic platform abstraction layer by hooking up appropriate
    architecture/OS/compiler specific headers from the /include/tbb/machine directory.
    If a plug-in header does not implement all the required APIs, it must specify
    the missing ones by setting one or more of the following macros:

    __TBB_USE_GENERIC_PART_WORD_CAS
    __TBB_USE_GENERIC_PART_WORD_FETCH_ADD
    __TBB_USE_GENERIC_PART_WORD_FETCH_STORE
    __TBB_USE_GENERIC_FETCH_ADD
    __TBB_USE_GENERIC_FETCH_STORE
    __TBB_USE_GENERIC_DWORD_FETCH_ADD
    __TBB_USE_GENERIC_DWORD_FETCH_STORE
    __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE
    __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE
    __TBB_USE_GENERIC_RELAXED_LOAD_STORE
    __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE
**/
andrjohns commented 8 months ago

Fixed in main

StaffanBetner commented 8 months ago

But the package isn't loadable in webR:

> install.packages('StanEstimators',
  repos = c('https://andrjohns.r-universe.dev', 'https://repo.r-wasm.org'))
Downloading webR package: Rcpp
Downloading webR package: RcppParallel
Downloading webR package: abind
Downloading webR package: backports
Downloading webR package: checkmate
Downloading webR package: rlang
Downloading webR package: fansi
Downloading webR package: cli
Downloading webR package: glue
Downloading webR package: lifecycle
Downloading webR package: magrittr
Downloading webR package: utf8
Downloading webR package: vctrs
Downloading webR package: pillar
Downloading webR package: pkgconfig
Downloading webR package: tibble
Downloading webR package: tensorA
Downloading webR package: generics
Downloading webR package: numDeriv
Downloading webR package: gtable
Downloading webR package: isoband
Downloading webR package: MASS
Downloading webR package: lattice
Downloading webR package: nlme
Downloading webR package: Matrix
Downloading webR package: mgcv
Downloading webR package: farver
Downloading webR package: labeling
Downloading webR package: colorspace
Downloading webR package: munsell
Downloading webR package: R6
Downloading webR package: RColorBrewer
Downloading webR package: viridisLite
Downloading webR package: scales
Downloading webR package: withr
Downloading webR package: ggplot2
Downloading webR package: digest
Downloading webR package: distributional
Downloading webR package: matrixStats
Downloading webR package: posterior
Downloading webR package: ps
Downloading webR package: processx
Downloading webR package: callr
Downloading webR package: codetools
Downloading webR package: globals
Downloading webR package: listenv
Downloading webR package: parallelly
Downloading webR package: future
Downloading webR package: RcppEigen
Downloading webR package: BH
Downloading webR package: rapidjsonr
Downloading webR package: StanEstimators
> library(StanEstimators)
Error: package or namespace load failed for ‘StanEstimators’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/usr/lib/R/library/StanEstimators/libs/StanEstimators.so':
  Could not load dynamic lib: /usr/lib/R/library/StanEstimators/libs/StanEstimators.so
Error: bad export type for '_ZTIN3tbb4taskE': undefined
andrjohns commented 8 months ago

Ah that's what I get for not testing. What's the easiest way for me to iterate with webR? Do you use the demo web REPL or run something else locally?

StaffanBetner commented 8 months ago

I use the demo web REPL.

andrjohns commented 8 months ago

Ah, it looks like this will be a tricky one to get working. The Math library needs to link to the TBB (not just include it's headers). To do this under WASM, the TBB itself needs to be built with emscripten so that the library is in a format that it can be linked to. However, when installing/building RcppParallel for webR, the TBB itself does not get built - so there is nothing for StanEstimators to link to during installation.

I'll open an issue on the r-universe or r-wasm repo, but I don't expect this to be workable anytime too soon

StaffanBetner commented 8 months ago

Related? https://github.com/RcppCore/RcppParallel/issues/88

andrjohns commented 8 months ago

Possibly. But compiling the TBB under emscripten is beyond the scope of what I can patch/fix in this package and likely to need a fairly non-trivial amount of work, so I'm not likely to tackle it any time soon unfortunately

StaffanBetner commented 8 months ago

No worries, it is more fun to have, rather than need to have. :)