[Bf-blender-cvs] [8753f92cb2b] functions: utility to iterate over string map in parallel

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


Commit: 8753f92cb2b2c21ade0e8ec1214b102db8726255
Author: Jacques Lucke
Date:   Thu Jan 2 16:05:41 2020 +0100
Branches: functions
https://developer.blender.org/rB8753f92cb2b2c21ade0e8ec1214b102db8726255

utility to iterate over string map in parallel

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

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

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

diff --git a/source/blender/blenlib/BLI_parallel.h b/source/blender/blenlib/BLI_parallel.h
index 5df7104767f..da82c2fdc7b 100644
--- a/source/blender/blenlib/BLI_parallel.h
+++ b/source/blender/blenlib/BLI_parallel.h
@@ -9,6 +9,7 @@
 
 #include "BLI_index_range.h"
 #include "BLI_multi_map.h"
+#include "BLI_string_map.h"
 
 namespace BLI {
 
@@ -93,6 +94,25 @@ void parallel_multi_map_items(const MultiMap<KeyT, ValueT, N> &multi_map, const
   });
 }
 
+template<typename ValueT, typename FuncT>
+void parallel_string_map_items(const StringMap<ValueT> &string_map, const FuncT &func)
+{
+  ScopedVector<StringRefNull> key_vector;
+  ScopedVector<const ValueT *> value_vector;
+
+  string_map.foreach_key_value_pair([&](StringRefNull key, const ValueT &value) {
+    key_vector.append(key);
+    value_vector.append(&value);
+  });
+
+  parallel_for(key_vector.index_range(), [&](uint index) {
+    StringRefNull key = key_vector[index];
+    const ValueT &value = *value_vector[index];
+
+    func(key, value);
+  });
+}
+
 }  // namespace BLI
 
 #endif /* __BLI_PARALLEL_H__ */
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 2872031ec26..2d0d1f8758b 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -392,27 +392,20 @@ BLI_NOINLINE static void simulate_existing_particles(
 {
   FloatInterval simulation_time_span = simulation_state.time().current_update_time();
 
-  Vector<std::string> name_vector;
-  Vector<ParticleSet *> particles_vector;
-  simulation_state.particles().particle_containers().foreach_key_value_pair(
-      [&](StringRef name, ParticleSet *particles) {
-        name_vector.append(name);
-        particles_vector.append(particles);
+  BLI::parallel_string_map_items(
+      simulation_state.particles().particle_containers(),
+      [&](StringRef system_name, ParticleSet *particle_set) {
+        ParticleSystemInfo *system_info = systems_to_simulate.lookup_ptr(system_name);
+        if (system_info == nullptr) {
+          return;
+        }
+
+        simulate_particles_for_time_span(simulation_state,
+                                         particle_allocator,
+                                         *system_info,
+                                         simulation_time_span,
+                                         particle_set->attributes());
       });
-
-  BLI::parallel_for(name_vector.index_range(), [&](uint index) {
-    ParticleSystemInfo *system_info = systems_to_simulate.lookup_ptr(name_vector[index]);
-    ParticleSet *particles = particles_vector[index];
-    if (system_info == nullptr) {
-      return;
-    }
-
-    simulate_particles_for_time_span(simulation_state,
-                                     particle_allocator,
-                                     *system_info,
-                                     simulation_time_span,
-                                     particles->attributes());
-  });
 }
 
 BLI_NOINLINE static void create_particles_from_emitters(SimulationState &simulation_state,
@@ -456,21 +449,23 @@ void simulate_particles(SimulationState &simulation_state,
   while (newly_created_particles.key_amount() > 0) {
     ParticleAllocator particle_allocator(particles_state);
 
-    newly_created_particles.foreach_item(
-        [&](StringRef name, ArrayRef<ParticleSet *> new_particle_sets) {
+    BLI::parallel_multi_map_items(
+        newly_created_particles, [&](StringRef name, ArrayRef<ParticleSet *> new_particle_sets) {
           ParticleSystemInfo *system_info = systems_to_simulate.lookup_ptr(name);
           if (system_info == nullptr) {
             return;
           }
 
-          for (ParticleSet *new_particles : new_particle_sets) {
+          BLI::parallel_for(new_particle_sets.index_range(), [&](uint index) {
+            ParticleSet &particle_set = *new_particle_sets[index];
             simulate_particles_from_birth_to_end_of_step(simulation_state,
                                                          particle_allocator,
                                                          *system_info,
                                                          simulation_time_span.end(),
-                                                         new_particles->attributes());
-          }
+                                                         particle_set.attributes());
+          });
         });
+
     newly_created_particles = particle_allocator.allocated_particles();
     all_newly_created_particles.add_multiple(newly_created_particles);
   }



More information about the Bf-blender-cvs mailing list