3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
parallel_for.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
19
26#ifndef DUMUX_PARALLEL_PARALLEL_FOR_HH
27#define DUMUX_PARALLEL_PARALLEL_FOR_HH
28
30
31#if HAVE_CPP_PARALLEL_ALGORITHMS
32#include <algorithm>
33#include <execution>
34#include <dune/common/rangeutilities.hh>
35#endif
36
37#if HAVE_TBB
38#include <tbb/parallel_for.h>
39#endif
40
41#if HAVE_KOKKOS
42#include <Kokkos_Core.hpp>
43#endif
44
45// contents of the detail namespace might change
46// any time without prior notice (do not use directly)
47#ifndef DOXYGEN // hide from doxygen
48namespace Dumux::Detail {
49
50// This should be specialized for different ExecutionBackends
51template<class FunctorType, class ExecutionBackend>
52class ParallelFor;
53
54
55// Serial backend implementation
56template<class FunctorType>
57class ParallelFor<FunctorType, Multithreading::ExecutionBackends::Serial>
58{
59public:
60 ParallelFor(const std::size_t count, const FunctorType& functor)
61 : functor_(functor), count_(count) {}
62
63 void execute() const
64 {
65 for (std::size_t i = 0; i < count_; ++i)
66 functor_(i);
67 }
68
69private:
70 FunctorType functor_;
71 std::size_t count_;
72};
73
74#if HAVE_CPP_PARALLEL_ALGORITHMS
75// C++ parallel algorithms backend implementation
76template<class FunctorType>
77class ParallelFor<FunctorType, Multithreading::ExecutionBackends::Cpp>
78{
79public:
80 ParallelFor(const std::size_t count, const FunctorType& functor)
81 : functor_(functor), range_(count) {}
82
83 void execute() const
84 {
85 std::for_each(std::execution::par_unseq, range_.begin(), range_.end(), functor_);
86 }
87
88private:
89 FunctorType functor_;
90 Dune::IntegralRange<std::size_t> range_;
91};
92#endif
93
94
95#if HAVE_TBB
96// TBB backend implementation
97template<class FunctorType>
98class ParallelFor<FunctorType, Multithreading::ExecutionBackends::TBB>
99{
100public:
101 ParallelFor(const std::size_t count, const FunctorType& functor)
102 : functor_(functor), count_(count) {}
103
104 void execute() const
105 {
106 tbb::parallel_for(std::size_t{0}, count_, [&](const std::size_t i){ functor_(i); });
107 }
108
109private:
110 FunctorType functor_;
111 std::size_t count_;
112};
113#endif // HAVE_TBB
114
115#if HAVE_KOKKOS
116// Kokkos backend implementation
117template<class FunctorType>
118class ParallelFor<FunctorType, Multithreading::ExecutionBackends::Kokkos>
119{
120public:
121 ParallelFor(const std::size_t count, const FunctorType& functor)
122 : functor_(functor), count_(count) {}
123
124 void execute() const
125 {
126 Kokkos::parallel_for(count_, [&](const std::size_t i){ functor_(i); });
127 }
128
129private:
130 FunctorType functor_;
131 std::size_t count_;
132};
133#endif // HAVE_KOKKOS
134
135
136#if HAVE_OPENMP
137// OpenMP backend implementation
138template<class FunctorType>
139class ParallelFor<FunctorType, Multithreading::ExecutionBackends::OpenMP>
140{
141public:
142 ParallelFor(const std::size_t count, const FunctorType& functor)
143 : functor_(functor), count_(count) {}
144
145 void execute() const
146 {
147 #pragma omp parallel for
148 for (std::size_t i = 0; i < count_; ++i)
149 functor_(i);
150 }
151
152private:
153 FunctorType functor_;
154 std::size_t count_;
155};
156#endif // HAVE_OPENMP
157
158
159} // end namespace Detail
160#endif // DOXYGEN
161
162
163namespace Dumux {
164
172template<class FunctorType>
173inline void parallelFor(const std::size_t count, const FunctorType& functor)
174{
176 Detail::ParallelFor<FunctorType, ExecutionBackend> action(count, functor);
177 action.execute();
178}
179
180} // end namespace Dumux
181
182#endif
Multithreading in Dumux.
Definition: adapt.hh:29
void parallelFor(const std::size_t count, const FunctorType &functor)
Definition: parallel_for.hh:173
Distance implementation details.
Definition: fclocalassembler.hh:42
ExecutionBackends::DUMUX_MULTITHREADING_BACKEND ExecutionBackend
Definition: multithreading.hh:47