[Bf-blender-cvs] [25fc970f445] functions: move tbb wrappers to blenlib

Jacques Lucke noreply at git.blender.org
Thu Jan 2 16:38:20 CET 2020


Commit: 25fc970f445f7e4407a9f1568f74a6348157e3ff
Author: Jacques Lucke
Date:   Thu Jan 2 15:20:44 2020 +0100
Branches: functions
https://developer.blender.org/rB25fc970f445f7e4407a9f1568f74a6348157e3ff

move tbb wrappers to blenlib

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

A	source/blender/blenlib/BLI_parallel.h
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/simulations/bparticles/simulate.cpp

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

diff --git a/source/blender/blenlib/BLI_parallel.h b/source/blender/blenlib/BLI_parallel.h
new file mode 100644
index 00000000000..490af51b00b
--- /dev/null
+++ b/source/blender/blenlib/BLI_parallel.h
@@ -0,0 +1,78 @@
+#ifndef __BLI_PARALLEL_H__
+#define __BLI_PARALLEL_H__
+
+#ifdef WITH_TBB
+#  define TBB_SUPPRESS_DEPRECATED_MESSAGES 1
+#  include "tbb/parallel_for.h"
+#  include "tbb/parallel_invoke.h"
+#endif
+
+#include "BLI_index_range.h"
+
+namespace BLI {
+
+/**
+ * Call func for every index in the IndexRange. func has to receive a single uint parameter.
+ */
+template<typename FuncT> void parallel_for(IndexRange range, const FuncT &func)
+{
+  if (range.size() == 0) {
+    return;
+  }
+#ifdef WITH_TBB
+  tbb::parallel_for(range.first(), range.one_after_last(), func);
+#else
+  for (uint i : range) {
+    func(i);
+  }
+#endif
+}
+
+/**
+ * Call func for subranges of range. The size of the individual subranges is controlled by a
+ * grain_size. func has to receive an IndexRange as parameter.
+ */
+template<typename FuncT>
+void blocked_parallel_for(IndexRange range, uint grain_size, const FuncT &func)
+{
+  if (range.size() == 0) {
+    return;
+  }
+#ifdef WITH_TBB
+  tbb::parallel_for(
+      tbb::blocked_range<uint>(range.first(), range.one_after_last(), grain_size),
+      [&](const tbb::blocked_range<uint> &sub_range) { func(IndexRange(sub_range)); });
+#else
+  func(range);
+#endif
+}
+
+/**
+ * Invoke multiple functions in parallel.
+ */
+template<typename FuncT1, typename FuncT2>
+void parallel_invoke(const FuncT1 &func1, const FuncT2 &func2)
+{
+#ifdef WITH_TBB
+  tbb::parallel_invoke(func1, func2);
+#else
+  func1();
+  func2();
+#endif
+}
+
+template<typename FuncT1, typename FuncT2, typename FuncT3>
+void parallel_invoke(const FuncT1 &func1, const FuncT2 &func2, const FuncT3 &func3)
+{
+#ifdef WITH_TBB
+  tbb::parallel_invoke(func1, func2, func3);
+#else
+  func1();
+  func2();
+  func3();
+#endif
+}
+
+}  // namespace BLI
+
+#endif /* __BLI_PARALLEL_H__ */
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 4ec31a827af..30dc6e31f3f 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -274,6 +274,7 @@ set(SRC
   BLI_utility_mixins.h
   BLI_static_class_ids.h
   BLI_index_mask.h
+  BLI_parallel.h
 )
 
 set(LIB
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 2b7fe272a1b..60146f4d095 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -3,17 +3,12 @@
 #include "BLI_timeit.h"
 #include "BLI_array_cxx.h"
 #include "BLI_vector_adaptor.h"
+#include "BLI_parallel.h"
 
 #include "FN_cpp_type.h"
 
 #include "simulate.hpp"
 
-#ifdef WITH_TBB
-#  define TBB_SUPPRESS_DEPRECATED_MESSAGES 1
-#  include "tbb/parallel_for.h"
-#  include "tbb/parallel_invoke.h"
-#endif
-
 namespace BParticles {
 
 using BLI::LargeScopedArray;
@@ -21,46 +16,6 @@ using BLI::LargeScopedVector;
 using BLI::VectorAdaptor;
 using FN::CPPType;
 
-template<typename FuncT> void parallel_for(IndexRange range, const FuncT &func)
-{
-  if (range.size() == 0) {
-    return;
-  }
-#ifdef WITH_TBB
-  tbb::parallel_for(range.first(), range.one_after_last(), func);
-#else
-  for (uint i : range) {
-    func(i);
-  }
-#endif
-}
-
-template<typename FuncT>
-void blocked_parallel_for(IndexRange range, uint grain_size, const FuncT &func)
-{
-  if (range.size() == 0) {
-    return;
-  }
-#ifdef WITH_TBB
-  tbb::parallel_for(
-      tbb::blocked_range<uint>(range.first(), range.one_after_last(), grain_size),
-      [&](const tbb::blocked_range<uint> &sub_range) { func(IndexRange(sub_range)); });
-#else
-  func(range);
-#endif
-}
-
-template<typename FuncT1, typename FuncT2>
-void parallel_invoke(const FuncT1 &func1, const FuncT2 &func2)
-{
-#ifdef WITH_TBB
-  tbb::parallel_invoke(func1, func2);
-#else
-  func1();
-  func2();
-#endif
-}
-
 BLI_NOINLINE static void find_next_event_per_particle(
     BlockStepData &step_data,
     IndexMask mask,
@@ -393,7 +348,7 @@ BLI_NOINLINE static void simulate_particles_for_time_span(SimulationState &simul
                                                           FloatInterval time_span,
                                                           MutableAttributesRef particle_attributes)
 {
-  blocked_parallel_for(IndexRange(particle_attributes.size()), 1000, [&](IndexRange range) {
+  BLI::blocked_parallel_for(IndexRange(particle_attributes.size()), 1000, [&](IndexRange range) {
     Array<float> remaining_durations(range.size(), time_span.size());
     simulate_particle_chunk(simulation_state,
                             particle_allocator,
@@ -413,7 +368,7 @@ BLI_NOINLINE static void simulate_particles_from_birth_to_end_of_step(
 {
   ArrayRef<float> all_birth_times = particle_attributes.get<float>("Birth Time");
 
-  blocked_parallel_for(IndexRange(particle_attributes.size()), 1000, [&](IndexRange range) {
+  BLI::blocked_parallel_for(IndexRange(particle_attributes.size()), 1000, [&](IndexRange range) {
     ArrayRef<float> birth_times = all_birth_times.slice(range);
 
     Array<float> remaining_durations(range.size());
@@ -445,7 +400,7 @@ BLI_NOINLINE static void simulate_existing_particles(
         particles_vector.append(particles);
       });
 
-  parallel_for(name_vector.index_iterator(), [&](uint index) {
+  BLI::parallel_for(name_vector.index_iterator(), [&](uint index) {
     ParticleSystemInfo *system_info = systems_to_simulate.lookup_ptr(name_vector[index]);
     ParticleSet *particles = particles_vector[index];
     if (system_info == nullptr) {
@@ -465,7 +420,7 @@ BLI_NOINLINE static void create_particles_from_emitters(SimulationState &simulat
                                                         ArrayRef<Emitter *> emitters,
                                                         FloatInterval time_span)
 {
-  parallel_for(emitters.index_iterator(), [&](uint emitter_index) {
+  BLI::parallel_for(emitters.index_iterator(), [&](uint emitter_index) {
     Emitter &emitter = *emitters[emitter_index];
     EmitterInterface interface(simulation_state, particle_allocator, time_span);
     emitter.emit(interface);
@@ -485,7 +440,7 @@ void simulate_particles(SimulationState &simulation_state,
   MultiMap<std::string, ParticleSet *> newly_created_particles;
   {
     ParticleAllocator particle_allocator(particles_state);
-    parallel_invoke(
+    BLI::parallel_invoke(
         [&]() {
           simulate_existing_particles(simulation_state, particle_allocator, systems_to_simulate);
         },
@@ -529,7 +484,7 @@ void simulate_particles(SimulationState &simulation_state,
         particle_sets_vector.append(new_particle_sets);
       });
 
-  parallel_for(main_sets.index_iterator(), [&](uint index) {
+  BLI::parallel_for(main_sets.index_iterator(), [&](uint index) {
     ParticleSet &main_set = *main_sets[index];
     ArrayRef<ParticleSet *> particle_sets = particle_sets_vector[index];



More information about the Bf-blender-cvs mailing list