/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
Discretization Framework for functional Renormalization Group flows
tbb.hh
Go to the documentation of this file.
1#pragma once
2
4
5// kept: several headers rely on this pulling in tbb::parallel_for as well.
6#include <tbb/tbb.h>
7
8#include <cstddef>
9#include <cstdlib>
10#include <functional>
11
12namespace DiFfRG
13{
14 namespace internal
15 {
28 inline constexpr std::size_t reduction_serial_cutoff = 4096;
29
37 inline constexpr std::size_t reduction_grainsize = 256;
38
40 template <int dim>
41 void unflatten_index(std::size_t flat, const device::array<size_t, dim> &grid_size,
43 {
44 for (int d = dim - 1; d >= 0; --d) {
45 idx[d] = flat % grid_size[d];
46 flat /= grid_size[d];
47 }
48 }
49
51 template <int dim> void advance_index(const device::array<size_t, dim> &grid_size, device::array<size_t, dim> &idx)
52 {
53 for (int d = dim - 1; d >= 0; --d) {
54 if (++idx[d] < grid_size[d]) return;
55 idx[d] = 0;
56 }
57 }
58
60 template <int dim, typename NT, typename FUN>
61 NT serial_sum(const device::array<size_t, dim> &grid_size, const FUN &functor, const std::size_t begin,
62 const std::size_t end)
63 {
65 unflatten_index<dim>(begin, grid_size, idx);
66 NT value(0);
67 for (std::size_t flat = begin; flat < end; ++flat) {
68 value += functor(idx);
69 advance_index<dim>(grid_size, idx);
70 }
71 return value;
72 }
73 } // namespace internal
74
87 template <int dim, typename NT, typename FUN>
88 NT TBBReduction(const device::array<size_t, dim> &grid_size, const FUN &functor)
89 {
90 std::size_t total = 1;
91 for (int d = 0; d < dim; ++d)
92 total *= grid_size[d];
93 if (total == 0) return NT(0);
94
96 return internal::serial_sum<dim, NT, FUN>(grid_size, functor, 0, total);
97
98 return tbb::parallel_deterministic_reduce(
99 tbb::blocked_range<std::size_t>(0, total, internal::reduction_grainsize), NT(0),
100 [&](const tbb::blocked_range<std::size_t> &r, NT value) -> NT {
101 return value + internal::serial_sum<dim, NT, FUN>(grid_size, functor, r.begin(), r.end());
102 },
103 std::plus<NT>(), tbb::simple_partitioner());
104 }
105} // namespace DiFfRG
std::array< T, N > array
Definition kokkos.hh:155
void advance_index(const device::array< size_t, dim > &grid_size, device::array< size_t, dim > &idx)
Advance a row-major multi-index by one (odometer increment).
Definition tbb.hh:51
void unflatten_index(std::size_t flat, const device::array< size_t, dim > &grid_size, device::array< size_t, dim > &idx)
Turn a row-major flat index into a multi-index.
Definition tbb.hh:41
constexpr std::size_t reduction_serial_cutoff
Number of grid points below which a reduction is carried out serially.
Definition tbb.hh:28
NT serial_sum(const device::array< size_t, dim > &grid_size, const FUN &functor, const std::size_t begin, const std::size_t end)
Strictly row-major serial sum of functor over the flat index range [begin, end).
Definition tbb.hh:61
constexpr std::size_t reduction_grainsize
Leaf size of the deterministic reduction tree.
Definition tbb.hh:37
Definition complex_math.hh:10
NT TBBReduction(const device::array< size_t, dim > &grid_size, const FUN &functor)
Bitwise reproducible reduction of functor over a dim-dimensional index grid.
Definition tbb.hh:88