Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
parallelalgorithm.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_PARALLEL_PARALLELALGORITHM_HH
8#define DUNE_FUFEM_PARALLEL_PARALLELALGORITHM_HH
9
10#include <cstddef>
11#include <vector>
12#include <thread>
13
15
17
18
19
20namespace Dune {
21namespace Fufem {
22
23
38template<class Range>
39static auto rangeSegment(Range& range, std::size_t i, std::size_t n) {
40 std::size_t length = range.size() / n;
41 std::size_t remainder = range.size() % n;
42 auto begin = length*i + std::min(remainder, i);
43 auto end = length*(i+1) + std::min(remainder, i+1);
44 return Dune::IteratorRange(range.begin()+begin, range.begin()+end);
45}
46
47
48
49
50namespace Impl {
51
52class ParallelAlgorithmThreadHelper
53{
54public:
55 ParallelAlgorithmThreadHelper(std::size_t threadCount, std::size_t threadIndex, Dune::Fufem::Barrier& barrier):
56 threadCount_(threadCount),
57 threadIndex_(threadIndex),
58 barrier_(barrier)
59 {}
60
61 template<class Range, class CallBack>
62 void forEach(Range&& range, CallBack&& callBack) const
63 {
64 for (auto&& entry : rangeSegment(range, threadIndex_, threadCount_))
65 callBack(entry);
66 barrier_.arrive_and_wait();
67 }
68
69 template<class ColorPartition, class CallBack>
70 void coloredForEach(const ColorPartition& partition, CallBack&& callBack) const
71 {
72 for (auto&& singleColorRange : partition)
73 forEach(singleColorRange, callBack);
74 }
75
76 std::size_t threadIndex() const
77 {
78 return threadIndex_;
79 }
80
81 std::size_t threadCount() const
82 {
83 return threadCount_;
84 }
85
86 Dune::Fufem::Barrier& barrier() const
87 {
88 return barrier_;;
89 }
90
91private:
92 std::size_t threadCount_;
93 std::size_t threadIndex_;
94 Dune::Fufem::Barrier& barrier_;
95};
96
97} // namespace Impl
98
99
100
146template<class F>
147void parallelAlgorithm(std::size_t threadCount, F&& threadImp)
148{
149 Dune::Fufem::Barrier barrier(threadCount);
150
152 for (std::size_t i=0; i<threadCount; ++i)
153 threads.emplace_back(threadImp, Impl::ParallelAlgorithmThreadHelper(threadCount, i, barrier));
154 for (auto& thread : threads)
155 thread.join();
156}
157
158
159
160} // namespace Fufem
161} // namespace Dune
162
163
164#endif // DUNE_FUFEM_PARALLEL_PARALLELALGORITHM_HH
165
iterator end()
iterator begin()
static constexpr IntegralRange< std::decay_t< T > > range(T &&from, U &&to) noexcept
constexpr void forEach(Range &&range, F &&f)
void parallelAlgorithm(std::size_t threadCount, F &&threadImp)
Utility for implementing parallel algorithms based on colored entity partitions.
Definition parallelalgorithm.hh:147
static auto rangeSegment(Range &range, std::size_t i, std::size_t n)
Split range into uniform segments.
Definition parallelalgorithm.hh:39
A barrier primitive for multi-threaded code.
Definition barrier.hh:30
T emplace_back(T... args)
T min(T... args)
T partition(T... args)