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
11
namespace
ember
{
12
13
#if defined(__EMSCRIPTEN_PTHREADS__)
14
namespace
detail
{
15
// Splits [0, n) into contiguous chunks over the pool and the calling thread.
16
void
pool_run
(
int64_t
n
,
const
std::function<
void
(
int64_t
,
int64_t
)>&
body
);
17
}
// namespace detail
18
19
template
<
class
F>
20
void
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
30
template
<
class
F>
31
void
parallel_for
(
int64_t
n
,
F
&&
fn
) {
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
n
int n
Definition
graph_ir_cuda.cu:326
body
E body
Definition
graph_ir_recurrence.cpp:145
j
size_t j
Definition
graph_ir_validate.cpp:222
detail
Definition
model_macros.h:20
ember
Definition
chat_gguf.h:11
ember::parallel_for
void parallel_for(int64_t n, F &&fn)
Definition
parallel_for.h:31
parallel_for.h
Generated by
1.9.8