Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
executor.h
Go to the documentation of this file.
1#ifndef BVH_V2_EXECUTOR_H
2#define BVH_V2_EXECUTOR_H
3
5
6#include <cstddef>
7#include <algorithm>
8#include <vector>
9
10namespace bvh::v2 {
11
12/// Helper object that provides iteration and reduction over one-dimensional ranges.
13template <typename Derived>
14struct Executor {
15 template <typename Loop>
16 inline void for_each(size_t begin, size_t end, const Loop& loop) {
17 return static_cast<Derived*>(this)->for_each(begin, end, loop);
18 }
19
20 template <typename T, typename Reduce, typename Join>
21 inline T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join& join) {
22 return static_cast<Derived*>(this)->reduce(begin, end, init, reduce, join);
23 }
24};
25
26/// Executor that executes serially.
27struct SequentialExecutor : Executor<SequentialExecutor> {
28 template <typename Loop>
29 void for_each(size_t begin, size_t end, const Loop& loop) {
30 loop(begin, end);
31 }
32
33 template <typename T, typename Reduce, typename Join>
34 T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join&) {
35 T result(init);
36 reduce(result, begin, end);
37 return result;
38 }
39};
40
41/// Executor that executes in parallel using the given thread pool.
42struct ParallelExecutor : Executor<ParallelExecutor> {
45
48 {}
49
50 template <typename Loop>
51 void for_each(size_t begin, size_t end, const Loop& loop) {
52 if (end - begin < parallel_threshold)
53 return loop(begin, end);
54
55 auto chunk_size = std::max(size_t{1}, (end - begin) / thread_pool.get_thread_count());
56 for (size_t i = begin; i < end; i += chunk_size) {
57 size_t next = std::min(end, i + chunk_size);
58 thread_pool.push([=] (size_t) { loop(i, next); });
59 }
61 }
62
63 template <typename T, typename Reduce, typename Join>
64 T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join& join) {
65 if (end - begin < parallel_threshold) {
66 T result(init);
67 reduce(result, begin, end);
68 return result;
69 }
70
71 auto chunk_size = std::max(size_t{1}, (end - begin) / thread_pool.get_thread_count());
72 std::vector<T> per_thread_result(thread_pool.get_thread_count(), init);
73 for (size_t i = begin; i < end; i += chunk_size) {
74 size_t next = std::min(end, i + chunk_size);
75 thread_pool.push([&, i, next] (size_t thread_id) {
76 auto& result = per_thread_result[thread_id];
77 reduce(result, i, next);
78 });
79 }
81 for (size_t i = 1; i < thread_pool.get_thread_count(); ++i)
82 join(per_thread_result[0], std::move(per_thread_result[i]));
83 return per_thread_result[0];
84 }
85};
86
87} // namespace bvh::v2
88
89#endif
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
void push(Task &&fun)
Definition thread_pool.h:48
size_t get_thread_count() const
Definition thread_pool.h:30
Definition bbox.h:9
Helper object that provides iteration and reduction over one-dimensional ranges.
Definition executor.h:14
T reduce(size_t begin, size_t end, const T &init, const Reduce &reduce, const Join &join)
Definition executor.h:21
void for_each(size_t begin, size_t end, const Loop &loop)
Definition executor.h:16
Executor that executes in parallel using the given thread pool.
Definition executor.h:42
T reduce(size_t begin, size_t end, const T &init, const Reduce &reduce, const Join &join)
Definition executor.h:64
ParallelExecutor(ThreadPool &thread_pool, size_t parallel_threshold=1024)
Definition executor.h:46
void for_each(size_t begin, size_t end, const Loop &loop)
Definition executor.h:51
ThreadPool & thread_pool
Definition executor.h:43
Executor that executes serially.
Definition executor.h:27
void for_each(size_t begin, size_t end, const Loop &loop)
Definition executor.h:29
T reduce(size_t begin, size_t end, const T &init, const Reduce &reduce, const Join &)
Definition executor.h:34