]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
only use TBB in workstream if we are allowed to use more than one thread
authorheister <heister@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 2 Oct 2013 17:30:01 +0000 (17:30 +0000)
committerheister <heister@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 2 Oct 2013 17:30:01 +0000 (17:30 +0000)
git-svn-id: https://svn.dealii.org/trunk@31071 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/include/deal.II/base/work_stream.h

index bccc4b8896c97976102b87ea8eeac9f2d0f2285e..45b15fe197283abf407e325da01636e06f6ef37a 100644 (file)
@@ -832,48 +832,52 @@ namespace WorkStream
     if (!(begin != end))
       return;
 
+    // we want to use TBB if we have support and if it is not disabled at
+    // runtime:
 #ifdef DEAL_II_WITH_THREADS
-    // create the three stages of the
-    // pipeline
-    internal::IteratorRangeToItemStream<Iterator,ScratchData,CopyData>
-      iterator_range_to_item_stream (begin, end,
-          queue_length,
-          chunk_size,
-          sample_scratch_data,
-          sample_copy_data);
-
-
-    internal::Worker<Iterator, ScratchData, CopyData> worker_filter (worker);
-    internal::Copier<Iterator, ScratchData, CopyData> copier_filter (copier,true);
+    if (multithread_info.n_threads()==1)
+#endif
+      {
+        // need to copy the sample since it is marked const
+        ScratchData scratch_data = sample_scratch_data;
+        CopyData    copy_data    = sample_copy_data;
 
-    // now create a pipeline from
-    // these stages
-    tbb::pipeline assembly_line;
-    assembly_line.add_filter (iterator_range_to_item_stream);
-    assembly_line.add_filter (worker_filter);
-    assembly_line.add_filter (copier_filter);
+        for (Iterator i=begin; i!=end; ++i)
+          {
+            if (static_cast<const std_cxx1x::function<void (const Iterator &,
+                                                            ScratchData &,
+                                                            CopyData &)> >(worker))
+              worker (i, scratch_data, copy_data);
+            if (static_cast<const std_cxx1x::function<void (const CopyData &)> >
+                (copier))
+              copier (copy_data);
+          }
+      }
+#ifdef DEAL_II_WITH_THREADS
+    else // have TBB and use more than one thread
+      {
+        // create the three stages of the pipeline
+        internal::IteratorRangeToItemStream<Iterator,ScratchData,CopyData>
+        iterator_range_to_item_stream (begin, end,
+            queue_length,
+            chunk_size,
+            sample_scratch_data,
+            sample_copy_data);
 
-    // and run it
-    assembly_line.run (queue_length);
 
-    assembly_line.clear ();
+        internal::Worker<Iterator, ScratchData, CopyData> worker_filter (worker);
+        internal::Copier<Iterator, ScratchData, CopyData> copier_filter (copier,true);
 
-#else
+        // now create a pipeline from these stages
+        tbb::pipeline assembly_line;
+        assembly_line.add_filter (iterator_range_to_item_stream);
+        assembly_line.add_filter (worker_filter);
+        assembly_line.add_filter (copier_filter);
 
-    // need to copy the sample since it is
-    // marked const
-    ScratchData scratch_data = sample_scratch_data;
-    CopyData    copy_data    = sample_copy_data;
+        // and run it
+        assembly_line.run (queue_length);
 
-    for (Iterator i=begin; i!=end; ++i)
-      {
-        if (static_cast<const std_cxx1x::function<void (const Iterator &,
-                                                        ScratchData &,
-                                                        CopyData &)> >(worker))
-          worker (i, scratch_data, copy_data);
-        if (static_cast<const std_cxx1x::function<void (const CopyData &)> >
-            (copier))
-          copier (copy_data);
+        assembly_line.clear ();
       }
 #endif
   }
@@ -974,89 +978,97 @@ namespace WorkStream
     if (!(begin != end))
       return;
 
+    // we want to use TBB if we have support and if it is not disabled at
+    // runtime:
+ #ifdef DEAL_II_WITH_THREADS
+     if (multithread_info.n_threads()==1)
+ #endif
+       {
+         // need to copy the sample since it is
+         // marked const
+         ScratchData scratch_data = sample_scratch_data;
+         CopyData    copy_data    = sample_copy_data;
+
+         for (Iterator i=begin; i!=end; ++i)
+           {
+             if (static_cast<const std_cxx1x::function<void (const Iterator &,
+                                                             ScratchData &,
+                                                             CopyData &)> >(worker))
+               worker (i, scratch_data, copy_data);
+             if (static_cast<const std_cxx1x::function<void (const CopyData &)> >
+                 (copier))
+               copier (copy_data);
+           }
+       }
 #ifdef DEAL_II_WITH_THREADS
-    // color the graph
-    std::vector<std::vector<Iterator> > coloring = graph_coloring::make_graph_coloring(
-        begin,end,get_conflict_indices);
-    
-    // colors that do not have cells, i.e., less than chunk_size times 
-    // multithread_info.n_default_threads, are gathered and the copier is
-    // called serially.
-    const unsigned int serial_limit(chunk_size*multithread_info.n_default_threads);
-    std::vector<Iterator> serial_copying;
-
-    for (unsigned int color=0; color<coloring.size(); ++color)
-    {
-      if (coloring[color].size()<serial_limit)
-        serial_copying.insert(serial_copying.end(),coloring[color].begin(),coloring[color].end());
-      else
-      {
-        // create the three stages of the
-        // pipeline
-        internal::IteratorRangeToItemStream<Iterator,ScratchData,CopyData>
-          iterator_range_to_item_stream (coloring[color].begin(), coloring[color].end(),
-              queue_length,
-              chunk_size,
-              sample_scratch_data,
-              sample_copy_data);
-
-        internal::Worker<Iterator, ScratchData, CopyData> worker_filter (worker);
-        internal::Copier<Iterator, ScratchData, CopyData> copier_filter (copier,false);
-
-        // now create a pipeline from
-        // these stages
-        tbb::pipeline assembly_line;
-        assembly_line.add_filter (iterator_range_to_item_stream);
-        assembly_line.add_filter (worker_filter);
-        assembly_line.add_filter (copier_filter);
-
-        // and run it
-        assembly_line.run (queue_length);
-
-        assembly_line.clear ();
-      }
-    }
-
-    // use the serial copier for all the colors that do not have enough cells
-    if (serial_copying.size()!=0)
-    {
-      internal::IteratorRangeToItemStream<Iterator,ScratchData,CopyData>
-        iterator_range_to_item_stream (serial_copying.begin(), serial_copying.end(),
-            queue_length,
-            chunk_size,
-            sample_scratch_data,
-            sample_copy_data);
-
-      internal::Worker<Iterator, ScratchData, CopyData> worker_filter (worker);
-      internal::Copier<Iterator, ScratchData, CopyData> copier_filter (copier,true);
-
-      tbb::pipeline assembly_line;
-      assembly_line.add_filter (iterator_range_to_item_stream);
-      assembly_line.add_filter (worker_filter);
-      assembly_line.add_filter (copier_filter);
-
-      // and run it
-      assembly_line.run (queue_length);
-
-      assembly_line.clear ();
-    }
-#else
-
-    // need to copy the sample since it is
-    // marked const
-    ScratchData scratch_data = sample_scratch_data;
-    CopyData    copy_data    = sample_copy_data;
-
-    for (Iterator i=begin; i!=end; ++i)
-      {
-        if (static_cast<const std_cxx1x::function<void (const Iterator &,
-                                                        ScratchData &,
-                                                        CopyData &)> >(worker))
-          worker (i, scratch_data, copy_data);
-        if (static_cast<const std_cxx1x::function<void (const CopyData &)> >
-            (copier))
-          copier (copy_data);
-      }
+     else
+       {
+         // color the graph
+         std::vector<std::vector<Iterator> > coloring = graph_coloring::make_graph_coloring(
+             begin,end,get_conflict_indices);
+
+         // colors that do not have cells, i.e., less than chunk_size times
+         // multithread_info.n_default_threads, are gathered and the copier is
+         // called serially.
+         const unsigned int serial_limit(chunk_size*multithread_info.n_default_threads);
+         std::vector<Iterator> serial_copying;
+
+         for (unsigned int color=0; color<coloring.size(); ++color)
+           {
+             if (coloring[color].size()<serial_limit)
+               serial_copying.insert(serial_copying.end(),coloring[color].begin(),coloring[color].end());
+             else
+               {
+                 // create the three stages of the
+                 // pipeline
+                 internal::IteratorRangeToItemStream<Iterator,ScratchData,CopyData>
+                 iterator_range_to_item_stream (coloring[color].begin(), coloring[color].end(),
+                     queue_length,
+                     chunk_size,
+                     sample_scratch_data,
+                     sample_copy_data);
+
+                 internal::Worker<Iterator, ScratchData, CopyData> worker_filter (worker);
+                 internal::Copier<Iterator, ScratchData, CopyData> copier_filter (copier,false);
+
+                 // now create a pipeline from
+                 // these stages
+                 tbb::pipeline assembly_line;
+                 assembly_line.add_filter (iterator_range_to_item_stream);
+                 assembly_line.add_filter (worker_filter);
+                 assembly_line.add_filter (copier_filter);
+
+                 // and run it
+                 assembly_line.run (queue_length);
+
+                 assembly_line.clear ();
+               }
+           }
+
+         // use the serial copier for all the colors that do not have enough cells
+         if (serial_copying.size()!=0)
+           {
+             internal::IteratorRangeToItemStream<Iterator,ScratchData,CopyData>
+             iterator_range_to_item_stream (serial_copying.begin(), serial_copying.end(),
+                 queue_length,
+                 chunk_size,
+                 sample_scratch_data,
+                 sample_copy_data);
+
+             internal::Worker<Iterator, ScratchData, CopyData> worker_filter (worker);
+             internal::Copier<Iterator, ScratchData, CopyData> copier_filter (copier,false);
+
+             tbb::pipeline assembly_line;
+             assembly_line.add_filter (iterator_range_to_item_stream);
+             assembly_line.add_filter (worker_filter);
+             assembly_line.add_filter (copier_filter);
+
+             // and run it
+             assembly_line.run (queue_length);
+
+             assembly_line.clear ();
+           }
+       }
 #endif
   }
 

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.