← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
checked_parallel.h
Go to the documentation of this file.
1#ifndef CK_CHECKED_PARALLEL_H
2#define CK_CHECKED_PARALLEL_H
3
4#include "ck_threadpool.h"
5#include "ckernel_engine.h"
6
7#include <limits.h>
8
9typedef void (*ck_checked_range_fn)(size_t begin, size_t end, void *args);
10
16
17static inline void ck_checked_pool_range(int begin, int end, void *data)
18{
19 struct ck_checked_range *range = data;
20 range->fn(range->offset + (size_t)begin, range->offset + (size_t)end, range->args);
21}
22
23/* Each range owns disjoint output and scratch. No reduction crosses ranges. */
24static inline void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
25{
26 if (count <= (size_t)grain) {
27 fn(0, count, args);
28 return;
29 }
30 ck_threadpool_t *pool = ck_threadpool_global();
31 if (!pool || ck_threadpool_in_dispatch(pool)) {
32 fn(0, count, args);
33 return;
34 }
35 int threads = ck_threadpool_n_threads(pool);
36 const int requested = ck_get_num_threads();
37 if (threads > requested) threads = requested;
38 const size_t ranges = count / (size_t)grain + (count % (size_t)grain != 0);
39 if (ranges < (size_t)threads) threads = (int)ranges;
40 struct ck_checked_range range = {fn, args, 0};
41 const int limit = INT_MAX - grain * (CK_THREADPOOL_MAX_THREADS + 1);
42 while (range.offset < count) {
43 const size_t remaining = count - range.offset;
44 const int extent = remaining > (size_t)limit ? limit : (int)remaining;
45 ck_threadpool_parallel_for_n(pool, threads, 0, extent, grain, ck_checked_pool_range, &range);
46 range.offset += (size_t)extent;
47 }
48}
49
50#endif
void(* ck_checked_range_fn)(size_t begin, size_t end, void *args)
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
static void ck_checked_pool_range(int begin, int end, void *data)
Persistent pthread thread pool for CK-Engine inference.
void ck_threadpool_parallel_for_n(ck_threadpool_t *pool, int active_threads, int begin, int end, int grain_size, ck_range_fn_t fn, void *args)
#define CK_THREADPOOL_MAX_THREADS
ck_threadpool_t * ck_threadpool_global(void)
int ck_threadpool_in_dispatch(const ck_threadpool_t *pool)
int ck_threadpool_n_threads(const ck_threadpool_t *pool)
int ck_get_num_threads(void)
static int extent(size_t rows, size_t cols, size_t stride, size_t size)
ck_checked_range_fn fn
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t end
Definition utf8.c:215