diff --git a/src/tucucore/montecarlopercentilecalculator.cpp b/src/tucucore/montecarlopercentilecalculator.cpp index af4ee3fd..6480677b 100644 --- a/src/tucucore/montecarlopercentilecalculator.cpp +++ b/src/tucucore/montecarlopercentilecalculator.cpp @@ -26,6 +26,23 @@ #ifdef TUCU_CONFIG_OPENMP #include #endif // TUCU_CONFIG_OPENMP + +#ifdef __EMSCRIPTEN__ +// WebAssembly build without pthreads: run each worker synchronously in the calling thread. +namespace { +struct TucuThread +{ + template, TucuThread> > > + explicit TucuThread(F&& _f) + { + _f(); + } + void join() {} +}; +} // namespace +#else +using TucuThread = std::thread; +#endif #include #include @@ -692,18 +709,18 @@ foundFalse: #else // TUCU_SINGLETHREADEDPERCENTILESORTOPTION // Sort and set percentile - for (unsigned int cycle = 0; cycle < recordedIntakes.size(); cycle++) { - for (unsigned int point = 0; point < static_cast(recordedIntakes[cycle].getNbPoints()); point++) { + for (unsigned int cycle = 0; cycle < _recordedIntakes.size(); cycle++) { + for (unsigned int point = 0; point < static_cast(_recordedIntakes[cycle].getNbPoints()); point++) { // Sort concentrations in increasing order at each time (at the cycle and at the point) std::sort( - concentrations[cycle][point].begin(), - concentrations[cycle][point].end(), + _concentrations[cycle][point].begin(), + _concentrations[cycle][point].end(), [&](const double v1, const double v2) { return v1 < v2; }); for (unsigned int percRankIdx = 0; percRankIdx < _percentileRanks.size(); percRankIdx++) { int pos = positions[percRankIdx]; - double conc = concentrations[cycle][point][pos]; + double conc = _concentrations[cycle][point][pos]; _percentiles.appendPercentile(percRankIdx, cycle, point, conc); } } @@ -1038,11 +1055,11 @@ ComputingStatus AposterioriMonteCarloPercentileCalculator::calculateEtasAndEpsil auto nbSamplePerThread = static_cast(ceil(static_cast(nbInitialSamples) / static_cast(nbThreads))); - std::vector workers; + std::vector workers; std::vector nbZeros(nbThreads, 0); for (unsigned thread = 0; thread < nbThreads; thread++) { - workers.push_back(std::thread([thread, + workers.push_back(TucuThread([thread, &abort, _aborter, nbSamplePerThread, @@ -1132,7 +1149,7 @@ ComputingStatus AposterioriMonteCarloPercentileCalculator::calculateEtasAndEpsil })); } - std::for_each(workers.begin(), workers.end(), [](std::thread& _t) { _t.join(); }); + std::for_each(workers.begin(), workers.end(), [](TucuThread& _t) { _t.join(); }); if (abort) { return ComputingStatus::Aborted;