/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/common/tbb.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/common/tbb.hh Source File
DiFfRG
tbb.hh
Go to the documentation of this file.
1#pragma once
2
4
5#include <tbb/tbb.h>
6
7#include <cstdlib>
8
9namespace DiFfRG
10{
11 namespace internal
12 {
13 template <int startDim, int stopDim, int dim, typename NT, typename FUN>
14 NT TBBReductionHelper(const device::array<size_t, dim> &grid_size, const FUN &functor,
16 {
17 static_assert(stopDim > startDim, "stopDim must be greater than startDim");
18
19 if constexpr (stopDim - startDim == 3) {
20 return tbb::parallel_reduce(
21 tbb::blocked_range3d<size_t, size_t, size_t>(0, grid_size[startDim], 0, grid_size[startDim + 1], 0,
22 grid_size[startDim + 2]),
23 NT(0),
24 [&](const tbb::blocked_range3d<size_t, size_t, size_t> &r, NT value) -> NT {
25 auto l_idx = idx;
26 for (size_t i = r.pages().begin(); i != r.pages().end(); ++i) {
27 l_idx[startDim] = i;
28 for (size_t j = r.rows().begin(); j != r.rows().end(); ++j) {
29 l_idx[startDim + 1] = j;
30 for (size_t k = r.cols().begin(); k != r.cols().end(); ++k) {
31 l_idx[startDim + 2] = k;
32 value += functor(l_idx);
33 }
34 }
35 }
36 return value;
37 },
38 std::plus<NT>());
39 } else if constexpr (stopDim - startDim == 2) {
40 return tbb::parallel_reduce(
41 tbb::blocked_range2d<size_t, size_t>(0, grid_size[startDim], 0, grid_size[startDim + 1]), NT(0),
42 [&](const tbb::blocked_range2d<size_t, size_t> &r, NT value) -> NT {
43 auto l_idx = idx;
44 for (size_t i = r.rows().begin(); i != r.rows().end(); ++i) {
45 l_idx[startDim] = i;
46 for (size_t j = r.cols().begin(); j != r.cols().end(); ++j) {
47 l_idx[startDim + 1] = j;
48 value += functor(l_idx);
49 }
50 }
51 return value;
52 },
53 std::plus<NT>());
54 } else if constexpr (stopDim - startDim == 1) {
55 return tbb::parallel_reduce(
56 tbb::blocked_range<size_t>(0, grid_size[startDim]), NT(0),
57 [&](const tbb::blocked_range<size_t> &r, NT value) -> NT {
58 auto l_idx = idx;
59 for (size_t i = r.begin(); i != r.end(); ++i) {
60 l_idx[startDim] = i;
61 value += functor(l_idx);
62 }
63 return value;
64 },
65 std::plus<NT>());
66 } else {
67 return tbb::parallel_reduce(
68 tbb::blocked_range3d<size_t, size_t, size_t>(0, grid_size[startDim], 0, grid_size[startDim + 1], 0,
69 grid_size[startDim + 2]),
70 NT(0),
71 [&](const tbb::blocked_range3d<size_t, size_t, size_t> &r, NT value) -> NT {
72 auto l_idx = idx;
73 for (size_t i = r.pages().begin(); i != r.pages().end(); ++i) {
74 l_idx[startDim] = i;
75 for (size_t j = r.rows().begin(); j != r.rows().end(); ++j) {
76 l_idx[startDim + 1] = j;
77 for (size_t k = r.cols().begin(); k != r.cols().end(); ++k) {
78 l_idx[startDim + 2] = k;
79 value += TBBReductionHelper<startDim + 3, stopDim, dim, NT, FUN>(grid_size, functor, l_idx);
80 }
81 }
82 }
83 return value;
84 },
85 std::plus<NT>());
86 }
87 }
88 } // namespace internal
89
90 template <int dim, typename NT, typename FUN>
91 NT TBBReduction(const device::array<size_t, dim> &grid_size, const FUN &functor)
92 {
94 return internal::TBBReductionHelper<0, dim, dim, NT, FUN>(grid_size, functor, idx);
95 }
96} // namespace DiFfRG
std::array< T, N > array
Definition kokkos.hh:133
NT TBBReductionHelper(const device::array< size_t, dim > &grid_size, const FUN &functor, const device::array< size_t, dim > &idx)
Definition tbb.hh:14
Definition complex_math.hh:10
NT TBBReduction(const device::array< size_t, dim > &grid_size, const FUN &functor)
Definition tbb.hh:91