emscripten
emscripten copied to clipboard
std::execution::par is ignored with no errors
Using the hints from some other issues i can build a small benchmark that uses std::execution::par and it builds and runs with no errors. Howewher it seems that the parallel policy is ignored when running in wasm and it falls back to sequential policy.
I'm building with
-std=c++20 -O3 -fexperimental-library -s WASM=1 -pthread -sPTHREAD_POOL_SIZE=10
The benchmark code is:
#include <iostream>
#include <vector>
#include <chrono>
#include <numeric>
#include <thread>
void bench(){
std::vector<int> v(10000);
std::iota (std::begin(v), std::end(v), 10000); // Fill with 0, 1, ..., 99.
auto start = std::chrono::high_resolution_clock::now();
std::for_each(std::execution::seq, std::begin(v), std::end(v), [&](int &i)
{
for (int sos = 0; sos < 500; sos++) {
i=i*i*i*i*i*i*i*i*i*i*i;
i= sqrt(i);
}
});
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration = end - start;
printf("Execution time sequential: %.3f ms\n", duration.count());
}
void benchPar(){
std::vector<int> v(10000);
std::iota (std::begin(v), std::end(v), 10000); // Fill with 0, 1, ..., 99.
auto start = std::chrono::high_resolution_clock::now();
std::for_each(std::execution::par, std::begin(v), std::end(v), [&](int &i)
{
for (int sos = 0; sos < 500; sos++) {
i=i*i*i*i*i*i*i*i*i*i*i;
i= sqrt(i);
}
});
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration = end - start;
printf("Execution time par: %.3f ms\n", duration.count());
}
int main()
{
unsigned int NUM_THREADS = std::thread::hardware_concurrency();
std::cout << "detected threads nr:" << NUM_THREADS << std::endl;
bench();
benchPar();
return 0;
}
On my local machine thats the result
detected threads nr:10
Execution time sequential: 69.065 ms
Execution time par: 8.383 ms
On wasm:
detected threads nr:10
Execution time sequential: 68.405 ms
Execution time par: 68.590 ms`
Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.61 (67fa4c16496b157a7fc3377afd69ee0445e8a6e3)
clang version 19.0.0git (https:/github.com/llvm/llvm-project 7cfffe74eeb68fbb3fb9706ac7071f8caeeb6520)
Target: wasm32-unknown-emscripten
Thread model: posix
Thanks, those execution policies really makes the difference if you are building an high performance application.