]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Encapsulate the calls to tbb::parallel_for
authorBruno Turcksin <bruno.turcksin@gmail.com>
Tue, 12 Mar 2019 13:32:28 +0000 (09:32 -0400)
committerBruno Turcksin <bruno.turcksin@gmail.com>
Thu, 14 Mar 2019 21:57:38 +0000 (17:57 -0400)
include/deal.II/base/parallel.h
include/deal.II/base/work_stream.h
include/deal.II/lac/vector_operations_internal.h

index 9f82c117502586795caec8254edef2ebfa18d88f..d9db08f5acd5841f46100fb83c13b7cff08688e0 100644 (file)
@@ -131,6 +131,7 @@ namespace parallel
     };
 
 
+
     /**
      * Take a function object and create a Body object from it. We do this in
      * this helper function since alternatively we would have to specify the
@@ -143,6 +144,41 @@ namespace parallel
     {
       return Body<F>(f);
     }
+
+
+
+    /**
+     * Encapsulate tbb::parallel_for.
+     */
+    template <typename Iterator, typename Functor>
+    void
+    parallel_for(Iterator           x_begin,
+                 Iterator           x_end,
+                 const Functor &    functor,
+                 const unsigned int grainsize)
+    {
+      tbb::parallel_for(tbb::blocked_range<Iterator>(x_begin, x_end, grainsize),
+                        functor,
+                        tbb::auto_partitioner());
+    }
+
+
+
+    /**
+     * Encapsulate tbb::parallel_for when an affinite_partitioner is provided.
+     */
+    template <typename Iterator, typename Functor>
+    void
+    parallel_for(Iterator                                          x_begin,
+                 Iterator                                          x_end,
+                 const Functor &                                   functor,
+                 const unsigned int                                grainsize,
+                 const std::shared_ptr<tbb::affinity_partitioner> &partitioner)
+    {
+      tbb::parallel_for(tbb::blocked_range<Iterator>(x_begin, x_end, grainsize),
+                        functor,
+                        *partitioner);
+    }
   } // namespace internal
 
   /**
@@ -188,11 +224,10 @@ namespace parallel
     using SyncIterators = SynchronousIterators<Iterators>;
     Iterators x_begin(begin_in, out);
     Iterators x_end(end_in, OutputIterator());
-    tbb::parallel_for(tbb::blocked_range<SyncIterators>(x_begin,
-                                                        x_end,
-                                                        grainsize),
-                      internal::make_body(predicate),
-                      tbb::auto_partitioner());
+    internal::parallel_for(SyncIterators(x_begin),
+                           SyncIterators(x_end),
+                           internal::make_body(predicate),
+                           grainsize);
 #endif
   }
 
@@ -246,11 +281,10 @@ namespace parallel
     using SyncIterators = SynchronousIterators<Iterators>;
     Iterators x_begin(begin_in1, in2, out);
     Iterators x_end(end_in1, InputIterator2(), OutputIterator());
-    tbb::parallel_for(tbb::blocked_range<SyncIterators>(x_begin,
-                                                        x_end,
-                                                        grainsize),
-                      internal::make_body(predicate),
-                      tbb::auto_partitioner());
+    internal::parallel_for(SyncIterators(x_begin),
+                           SyncIterators(x_end),
+                           internal::make_body(predicate),
+                           grainsize);
 #endif
   }
 
@@ -309,11 +343,10 @@ namespace parallel
                     InputIterator2(),
                     InputIterator3(),
                     OutputIterator());
-    tbb::parallel_for(tbb::blocked_range<SyncIterators>(x_begin,
-                                                        x_end,
-                                                        grainsize),
-                      internal::make_body(predicate),
-                      tbb::auto_partitioner());
+    internal::parallel_for(SyncIterators(x_begin),
+                           SyncIterators(x_end),
+                           internal::make_body(predicate),
+                           grainsize);
 #endif
   }
 
@@ -428,12 +461,13 @@ namespace parallel
     ff(begin, end);
 #  endif
 #else
-    tbb::parallel_for(
-      tbb::blocked_range<RangeType>(begin, end, grainsize),
+    internal::parallel_for(
+      begin,
+      end,
       std::bind(&internal::apply_to_subranges<RangeType, Function>,
                 std::placeholders::_1,
                 std::cref(f)),
-      tbb::auto_partitioner());
+      grainsize);
 #endif
   }
 
@@ -826,10 +860,7 @@ namespace parallel
     apply_to_subrange(begin, end);
 #else
     internal::ParallelForWrapper worker(*this);
-    tbb::parallel_for(
-      tbb::blocked_range<std::size_t>(begin, end, minimum_parallel_grain_size),
-      worker,
-      tbb::auto_partitioner());
+    internal::parallel_for(begin, end, worker, minimum_parallel_grain_size);
 #endif
   }
 
index b09c2d1fdfb4491db7c900b34aa4b21c21c8302e..555c35c98da7562d65960f9f3dd25287429dcb8d 100644 (file)
@@ -1154,14 +1154,13 @@ namespace WorkStream
                                                 sample_scratch_data,
                                                 sample_copy_data);
 
-              tbb::parallel_for(
-                tbb::blocked_range<RangeType>(colored_iterators[color].begin(),
-                                              colored_iterators[color].end(),
-                                              /*grain_size=*/chunk_size),
+              parallel::internal::parallel_for(
+                colored_iterators[color].begin(),
+                colored_iterators[color].end(),
                 std::bind(&WorkerAndCopier::operator(),
                           std::ref(worker_and_copier),
                           std::placeholders::_1),
-                tbb::auto_partitioner());
+                chunk_size);
             }
       }
 #  endif
index 5d98e3f23a6bec22be62e84d9b448ccc8023d2e8..bd13406faac5d7385d58f643ec785b24b6582ad6 100644 (file)
@@ -188,10 +188,8 @@ namespace internal
             partitioner->acquire_one_partitioner();
 
           TBBForFunctor<Functor> generic_functor(functor, start, end);
-          tbb::parallel_for(
-            tbb::blocked_range<size_type>(0, generic_functor.n_chunks, 1),
-            generic_functor,
-            *tbb_partitioner);
+          parallel::internal::parallel_for(
+            0U, generic_functor.n_chunks, generic_functor, 1, tbb_partitioner);
           partitioner->release_one_partitioner(tbb_partitioner);
         }
       else if (vec_size > 0)
@@ -1389,10 +1387,8 @@ namespace internal
           TBBReduceFunctor<Operation, ResultType> generic_functor(op,
                                                                   start,
                                                                   end);
-          tbb::parallel_for(
-            tbb::blocked_range<size_type>(0, generic_functor.n_chunks, 1),
-            generic_functor,
-            *tbb_partitioner);
+          parallel::internal::parallel_for(
+            0U, generic_functor.n_chunks, generic_functor, 1, tbb_partitioner);
           partitioner->release_one_partitioner(tbb_partitioner);
           result = generic_functor.do_sum();
         }

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.