16 #ifndef dealii__parallel_h 17 #define dealii__parallel_h 20 #include <deal.II/base/config.h> 21 #include <deal.II/base/exceptions.h> 22 #include <deal.II/base/template_constraints.h> 23 #include <deal.II/base/synchronous_iterator.h> 25 #include <deal.II/base/std_cxx11/tuple.h> 26 #include <deal.II/base/std_cxx11/bind.h> 27 #include <deal.II/base/std_cxx11/function.h> 31 #ifdef DEAL_II_WITH_THREADS 32 # include <tbb/parallel_for.h> 33 # include <tbb/parallel_reduce.h> 34 # include <tbb/partitioner.h> 35 # include <tbb/blocked_range.h> 41 DEAL_II_NAMESPACE_OPEN
51 template <
typename Number>
54 static const bool value =
true;
57 #ifdef __INTEL_COMPILER 65 static const bool value =
false;
86 template <
typename Range>
88 operator () (
const Range &range)
const 90 for (
typename Range::const_iterator p=range.begin();
91 p != range.end(); ++p)
92 apply (f, p.iterators);
104 template <
typename I1,
typename I2>
108 const std_cxx11::tuple<I1,I2> &p)
110 *std_cxx11::get<1>(p) = f (*std_cxx11::get<0>(p));
116 template <
typename I1,
typename I2,
typename I3>
120 const std_cxx11::tuple<I1,I2,I3> &p)
122 *std_cxx11::get<2>(p) = f (*std_cxx11::get<0>(p),
123 *std_cxx11::get<1>(p));
129 template <
typename I1,
typename I2,
130 typename I3,
typename I4>
134 const std_cxx11::tuple<I1,I2,I3,I4> &p)
136 *std_cxx11::get<3>(p) = f (*std_cxx11::get<0>(p),
137 *std_cxx11::get<1>(p),
138 *std_cxx11::get<2>(p));
149 template <
typename F>
179 template <
typename InputIterator,
180 typename OutputIterator,
183 const InputIterator &end_in,
185 Predicate &predicate,
186 const unsigned int grainsize)
188 #ifndef DEAL_II_WITH_THREADS 193 for (OutputIterator in = begin_in; in != end_in;)
194 *out++ = predicate (*in++);
196 typedef std_cxx11::tuple<InputIterator,OutputIterator> Iterators;
198 Iterators x_begin (begin_in, out);
199 Iterators x_end (end_in, OutputIterator());
200 tbb::parallel_for (tbb::blocked_range<SyncIterators>(x_begin,
203 internal::make_body (predicate),
204 tbb::auto_partitioner());
233 template <
typename InputIterator1,
234 typename InputIterator2,
235 typename OutputIterator,
238 const InputIterator1 &end_in1,
241 Predicate &predicate,
242 const unsigned int grainsize)
244 #ifndef DEAL_II_WITH_THREADS 249 for (OutputIterator in1 = begin_in1; in1 != end_in1;)
250 *out++ = predicate (*in1++, *in2++);
253 std_cxx11::tuple<InputIterator1,InputIterator2,OutputIterator>
256 Iterators x_begin (begin_in1, in2, out);
257 Iterators x_end (end_in1, InputIterator2(), OutputIterator());
258 tbb::parallel_for (tbb::blocked_range<SyncIterators>(x_begin,
261 internal::make_body (predicate),
262 tbb::auto_partitioner());
291 template <
typename InputIterator1,
292 typename InputIterator2,
293 typename InputIterator3,
294 typename OutputIterator,
297 const InputIterator1 &end_in1,
301 Predicate &predicate,
302 const unsigned int grainsize)
304 #ifndef DEAL_II_WITH_THREADS 309 for (OutputIterator in1 = begin_in1; in1 != end_in1;)
310 *out++ = predicate (*in1++, *in2++, *in3++);
313 std_cxx11::tuple<InputIterator1,InputIterator2,InputIterator3,OutputIterator>
316 Iterators x_begin (begin_in1, in2, in3, out);
317 Iterators x_end (end_in1, InputIterator2(),
318 InputIterator3(), OutputIterator());
319 tbb::parallel_for (tbb::blocked_range<SyncIterators>(x_begin,
322 internal::make_body (predicate),
323 tbb::auto_partitioner());
330 #ifdef DEAL_II_WITH_THREADS 335 template <
typename RangeType,
typename Function>
339 f (range.begin(), range.end());
416 template <
typename RangeType,
typename Function>
418 const typename identity<RangeType>::type &end,
420 const unsigned int grainsize)
422 #ifndef DEAL_II_WITH_THREADS 427 # ifndef DEAL_II_BIND_NO_CONST_OP_PARENTHESES 436 tbb::parallel_for (tbb::blocked_range<RangeType>
437 (begin, end, grainsize),
438 std_cxx11::bind (&internal::apply_to_subranges<RangeType,Function>,
441 tbb::auto_partitioner());
490 void apply_parallel (
const std::size_t begin,
491 const std::size_t end,
492 const std::size_t minimum_parallel_grain_size)
const;
500 virtual void apply_to_subrange (
const std::size_t,
501 const std::size_t)
const = 0;
508 #ifdef DEAL_II_WITH_THREADS 515 template <
typename ResultType,
533 template <
typename Reductor>
535 const Reductor &reductor,
536 const ResultType neutral_element = ResultType())
538 result (neutral_element),
540 neutral_element (neutral_element),
550 result (r.neutral_element),
552 neutral_element (r.neutral_element),
553 reductor (r.reductor)
562 result = reductor(result, r.
result);
568 template <
typename RangeType>
569 void operator () (
const tbb::blocked_range<RangeType> &range)
571 result = reductor(result,
572 f (range.begin(), range.end()));
592 const std_cxx11::function<ResultType (ResultType, ResultType)>
reductor;
657 template <
typename ResultType,
typename RangeType,
typename Function>
659 const RangeType &begin,
660 const typename identity<RangeType>::type &end,
661 const unsigned int grainsize)
663 #ifndef DEAL_II_WITH_THREADS 668 # ifndef DEAL_II_BIND_NO_CONST_OP_PARENTHESES 669 return f (begin, end);
674 return ff (begin, end);
678 reductor (f, std::plus<ResultType>(), 0);
679 tbb::parallel_reduce (tbb::blocked_range<RangeType>(begin, end, grainsize),
681 tbb::auto_partitioner());
707 extern unsigned int minimum_parallel_grain_size;
717 extern unsigned int minimum_parallel_grain_size;
728 #ifdef DEAL_II_WITH_THREADS 743 void operator() (
const tbb::blocked_range<std::size_t> &range)
const 745 worker_.apply_to_subrange (range.begin(), range.end());
763 const std::size_t end,
764 const std::size_t minimum_parallel_grain_size)
const 766 #ifndef DEAL_II_WITH_THREADS 769 (void) minimum_parallel_grain_size;
771 apply_to_subrange (begin, end);
774 tbb::parallel_for (tbb::blocked_range<std::size_t>
775 (begin, end, minimum_parallel_grain_size),
777 tbb::auto_partitioner());
783 DEAL_II_NAMESPACE_CLOSE
void join(const ReductionOnSubranges &r)
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_cxx11::tuple< I1, I2 > &p)
virtual ~ParallelForInteger()
ReductionOnSubranges(const ReductionOnSubranges &r, tbb::split)
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 transform(const InputIterator &begin_in, const InputIterator &end_in, OutputIterator out, Predicate &predicate, const unsigned int grainsize)
void apply_parallel(const std::size_t begin, const std::size_t end, const std::size_t minimum_parallel_grain_size) const
const std_cxx11::function< ResultType(ResultType, ResultType)> reductor
static void apply(const F &f, const std_cxx11::tuple< I1, I2, I3 > &p)
static void apply(const F &f, const std_cxx11::tuple< I1, I2, I3, I4 > &p)