16 #ifndef dealii_parallel_h 17 #define dealii_parallel_h 20 #include <deal.II/base/config.h> 22 #include <deal.II/base/exceptions.h> 23 #include <deal.II/base/synchronous_iterator.h> 24 #include <deal.II/base/template_constraints.h> 25 #include <deal.II/base/thread_management.h> 32 #ifdef DEAL_II_WITH_THREADS 33 # include <tbb/blocked_range.h> 34 # include <tbb/parallel_for.h> 35 # include <tbb/parallel_reduce.h> 36 # include <tbb/partitioner.h> 43 DEAL_II_NAMESPACE_OPEN
53 template <
typename Number>
56 static const bool value =
true;
59 #ifdef __INTEL_COMPILER 67 static const bool value =
false;
87 template <
typename Range>
89 operator()(
const Range &range)
const 91 for (
typename Range::const_iterator p = range.begin(); p != range.end();
105 template <
typename I1,
typename I2>
107 apply(
const F &
f,
const std::tuple<I1, I2> &p)
109 *std::get<1>(p) =
f(*std::get<0>(p));
115 template <
typename I1,
typename I2,
typename I3>
117 apply(
const F &
f,
const std::tuple<I1, I2, I3> &p)
119 *std::get<2>(p) =
f(*std::get<0>(p), *std::get<1>(p));
125 template <
typename I1,
typename I2,
typename I3,
typename I4>
127 apply(
const F &
f,
const std::tuple<I1, I2, I3, I4> &p)
129 *std::get<3>(p) =
f(*std::get<0>(p), *std::get<1>(p), *std::get<2>(p));
141 template <
typename F>
150 #ifdef DEAL_II_WITH_THREADS 154 template <
typename Iterator,
typename Functor>
158 const Functor & functor,
159 const unsigned int grainsize)
161 tbb::parallel_for(tbb::blocked_range<Iterator>(x_begin, x_end, grainsize),
163 tbb::auto_partitioner());
171 template <
typename Iterator,
typename Functor>
175 const Functor & functor,
176 const unsigned int grainsize,
177 const std::shared_ptr<tbb::affinity_partitioner> &partitioner)
179 tbb::parallel_for(tbb::blocked_range<Iterator>(x_begin, x_end, grainsize),
209 template <
typename InputIterator,
typename OutputIterator,
typename Predicate>
212 const InputIterator &end_in,
214 const Predicate & predicate,
215 const unsigned int grainsize)
217 #ifndef DEAL_II_WITH_THREADS 222 for (OutputIterator in = begin_in; in != end_in;)
223 *out++ = predicate(*in++);
225 using Iterators = std::tuple<InputIterator, OutputIterator>;
227 Iterators x_begin(begin_in, out);
228 Iterators x_end(end_in, OutputIterator());
230 SyncIterators(x_end),
261 template <
typename InputIterator1,
262 typename InputIterator2,
263 typename OutputIterator,
267 const InputIterator1 &end_in1,
270 const Predicate & predicate,
271 const unsigned int grainsize)
273 #ifndef DEAL_II_WITH_THREADS 278 for (OutputIterator in1 = begin_in1; in1 != end_in1;)
279 *out++ = predicate(*in1++, *in2++);
282 std::tuple<InputIterator1, InputIterator2, OutputIterator>;
284 Iterators x_begin(begin_in1, in2, out);
285 Iterators x_end(end_in1, InputIterator2(), OutputIterator());
287 SyncIterators(x_end),
318 template <
typename InputIterator1,
319 typename InputIterator2,
320 typename InputIterator3,
321 typename OutputIterator,
325 const InputIterator1 &end_in1,
329 const Predicate & predicate,
330 const unsigned int grainsize)
332 #ifndef DEAL_II_WITH_THREADS 337 for (OutputIterator in1 = begin_in1; in1 != end_in1;)
338 *out++ = predicate(*in1++, *in2++, *in3++);
340 using Iterators = std::
341 tuple<InputIterator1, InputIterator2, InputIterator3, OutputIterator>;
343 Iterators x_begin(begin_in1, in2, in3, out);
344 Iterators x_end(end_in1,
349 SyncIterators(x_end),
358 #ifdef DEAL_II_WITH_THREADS 363 template <
typename RangeType,
typename Function>
368 f(range.begin(), range.end());
445 template <
typename RangeType,
typename Function>
448 const typename identity<RangeType>::type &end,
450 const unsigned int grainsize)
452 #ifndef DEAL_II_WITH_THREADS 457 # ifndef DEAL_II_BIND_NO_CONST_OP_PARENTHESES 469 std::bind(&internal::apply_to_subranges<RangeType, Function>,
470 std::placeholders::_1,
523 const std::size_t end,
524 const std::size_t minimum_parallel_grain_size)
const;
540 #ifdef DEAL_II_WITH_THREADS 547 template <
typename ResultType,
typename Function>
564 template <
typename Reductor>
597 template <
typename RangeType>
621 const std::function<ResultType(ResultType, ResultType)>
reductor;
686 template <
typename ResultType,
typename RangeType,
typename Function>
689 const RangeType & begin,
690 const typename identity<RangeType>::type &end,
691 const unsigned int grainsize)
693 #ifndef DEAL_II_WITH_THREADS 698 # ifndef DEAL_II_BIND_NO_CONST_OP_PARENTHESES 699 return f(begin, end);
704 return ff(begin, end);
708 f, std::plus<ResultType>(), 0);
709 tbb::parallel_reduce(tbb::blocked_range<RangeType>(begin, end, grainsize),
711 tbb::auto_partitioner());
738 #ifdef DEAL_II_WITH_THREADS 751 std::shared_ptr<tbb::affinity_partitioner>
752 acquire_one_partitioner();
760 release_one_partitioner(std::shared_ptr<tbb::affinity_partitioner> &p);
767 std::shared_ptr<tbb::affinity_partitioner> my_partitioner;
787 namespace VectorImplementation
803 extern unsigned int minimum_parallel_grain_size;
807 namespace SparseMatrixImplementation
814 extern unsigned int minimum_parallel_grain_size;
824 #ifdef DEAL_II_WITH_THREADS 839 operator()(
const tbb::blocked_range<std::size_t> &range)
const 853 const std::size_t begin,
854 const std::size_t end,
855 const std::size_t minimum_parallel_grain_size)
const 857 #ifndef DEAL_II_WITH_THREADS 860 (void)minimum_parallel_grain_size;
871 DEAL_II_NAMESPACE_CLOSE
Body< F > make_body(const F &f)
void transform(const InputIterator &begin_in, const InputIterator &end_in, OutputIterator out, const Predicate &predicate, const unsigned int grainsize)
void parallel_for(Iterator x_begin, Iterator x_end, const Functor &functor, const unsigned int grainsize)
void join(const ReductionOnSubranges &r)
static void apply(const F &f, const std::tuple< I1, I2, I3 > &p)
void operator()(const tbb::blocked_range< RangeType > &range)
void apply_to_subranges(const RangeType &begin, const typename identity< RangeType >::type &end, const Function &f, const unsigned int grainsize)
ReductionOnSubranges(const Function &f, const Reductor &reductor, const ResultType neutral_element=ResultType())
static void apply(const F &f, const std::tuple< I1, I2, I3, I4 > &p)
void apply_to_subranges(const tbb::blocked_range< RangeType > &range, const Function &f)
ReductionOnSubranges(const ReductionOnSubranges &r, tbb::split)
const std::function< ResultType(ResultType, ResultType)> reductor
static void apply(const F &f, const std::tuple< I1, I2 > &p)
ResultType accumulate_from_subranges(const Function &f, const RangeType &begin, const typename identity< RangeType >::type &end, const unsigned int grainsize)
const ResultType neutral_element
void apply_parallel(const std::size_t begin, const std::size_t end, const std::size_t minimum_parallel_grain_size) const
virtual void apply_to_subrange(const std::size_t, const std::size_t) const =0
virtual ~ParallelForInteger()=default