ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
parallel_for.h
Go to the documentation of this file.
1#pragma once
2// ember::parallel_for(n, fn): runs fn(i) for every i in [0, n) once, in
3// parallel. Native builds use OpenMP (schedule(static), exactly the loops
4// this replaces); WebAssembly builds with pthreads (EMBER_WASM_THREADS in
5// deploy/wasm/build.sh) use a small persistent thread pool, since Emscripten
6// has no OpenMP; anything else runs serially. Each index is computed the same
7// way whichever thread runs it, so results do not depend on the schedule.
8#include <cstdint>
9#include <functional>
10
11namespace ember {
12
13#if defined(__EMSCRIPTEN_PTHREADS__)
14namespace detail {
15// Splits [0, n) into contiguous chunks over the pool and the calling thread.
16void pool_run(int64_t n, const std::function<void(int64_t, int64_t)>& body);
17} // namespace detail
18
19template <class F>
20void parallel_for(int64_t n, F&& fn) {
21 if (n <= 1) {
22 for (int64_t i = 0; i < n; ++i) fn(i);
23 return;
24 }
25 detail::pool_run(n, [&fn](int64_t lo, int64_t hi) {
26 for (int64_t i = lo; i < hi; ++i) fn(i);
27 });
28}
29#else
30template <class F>
32#pragma omp parallel for schedule(static)
33 for (int64_t i = 0; i < n; ++i) fn(i);
34}
35#endif
36
37} // namespace ember
int n
size_t j
void parallel_for(int64_t n, F &&fn)