emscripten-core / emscripten-core/emscripten

std::execution::par is ignored with no errors

Open
#22,194 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

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
#include
#include
#include
#include

void bench(){
std::vector 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 duration = end - start;

printf("Execution time sequential: %.3f ms\n", duration.count());
}
void benchPar(){

std::vector 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 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.