[Bf-blender-cvs] [7c10e364b23] master: BLI: wrap parallel_invoke from tbb

Jacques Lucke noreply at git.blender.org
Wed Feb 9 13:08:12 CET 2022


Commit: 7c10e364b2358f08fa49ce35fc98d4de1431e615
Author: Jacques Lucke
Date:   Wed Feb 9 13:08:04 2022 +0100
Branches: master
https://developer.blender.org/rB7c10e364b2358f08fa49ce35fc98d4de1431e615

BLI: wrap parallel_invoke from tbb

===================================================================

M	source/blender/blenlib/BLI_task.hh
M	source/blender/blenlib/tests/BLI_task_test.cc

===================================================================

diff --git a/source/blender/blenlib/BLI_task.hh b/source/blender/blenlib/BLI_task.hh
index 84d5cd39bb4..8f75aa19cfe 100644
--- a/source/blender/blenlib/BLI_task.hh
+++ b/source/blender/blenlib/BLI_task.hh
@@ -31,6 +31,7 @@
 #  include <tbb/blocked_range.h>
 #  include <tbb/parallel_for.h>
 #  include <tbb/parallel_for_each.h>
+#  include <tbb/parallel_invoke.h>
 #  include <tbb/parallel_reduce.h>
 #  include <tbb/task_arena.h>
 #  ifdef WIN32
@@ -103,6 +104,19 @@ Value parallel_reduce(IndexRange range,
 #endif
 }
 
+/**
+ * Execute all of the provided functions. The functions might be executed in parallel or in serial
+ * or some combination of both.
+ */
+template<typename... Functions> void parallel_invoke(Functions &&...functions)
+{
+#ifdef WITH_TBB
+  tbb::parallel_invoke(std::forward<Functions>(functions)...);
+#else
+  (functions(), ...);
+#endif
+}
+
 /** See #BLI_task_isolate for a description of what isolating a task means. */
 template<typename Function> void isolate_task(const Function &function)
 {
diff --git a/source/blender/blenlib/tests/BLI_task_test.cc b/source/blender/blenlib/tests/BLI_task_test.cc
index 3bb6f6f753c..1ed732c1f18 100644
--- a/source/blender/blenlib/tests/BLI_task_test.cc
+++ b/source/blender/blenlib/tests/BLI_task_test.cc
@@ -1,6 +1,7 @@
 /* Apache License, Version 2.0 */
 
 #include "testing/testing.h"
+#include <atomic>
 #include <cstring>
 
 #include "atomic_ops.h"
@@ -12,6 +13,7 @@
 #include "BLI_listbase.h"
 #include "BLI_mempool.h"
 #include "BLI_task.h"
+#include "BLI_task.hh"
 
 #define NUM_ITEMS 10000
 
@@ -280,3 +282,15 @@ TEST(task, ListBaseIter)
   MEM_freeN(items_buffer);
   BLI_threadapi_exit();
 }
+
+TEST(task, ParallelInvoke)
+{
+  std::atomic<int> counter = 0;
+  blender::threading::parallel_invoke([&]() { counter++; },
+                                      [&]() { counter++; },
+                                      [&]() { counter++; },
+                                      [&]() { counter++; },
+                                      [&]() { counter++; },
+                                      [&]() { counter++; });
+  EXPECT_EQ(counter, 6);
+}



More information about the Bf-blender-cvs mailing list