From 95461433c2da59bbbc9b6fae6fb41fb2a65a0f8d Mon Sep 17 00:00:00 2001 From: Christian Sendlinger Date: Tue, 5 Mar 2024 17:22:05 +0100 Subject: [PATCH] Changed CPU synchronization from averaging to communicating changes --- include/generators/species_generator.h | 3 + include/simulation/cpu_synchronizer.h | 28 +++-- include/simulation/species.h | 18 +++- src/generators/species_generator.cpp | 23 +++- src/simulation/cpu_synchronizer.cpp | 141 ++++++++++++++++++------- src/simulation/species.cpp | 30 +++++- 6 files changed, 191 insertions(+), 52 deletions(-) diff --git a/include/generators/species_generator.h b/include/generators/species_generator.h index a6e09ac..f2c1e4e 100755 --- a/include/generators/species_generator.h +++ b/include/generators/species_generator.h @@ -23,6 +23,7 @@ struct TrackedSpeciesBuilderInfo { std::shared_ptr density_field; std::shared_ptr velocity_distribution; std::shared_ptr drift_velocity; + std::shared_ptr last_sync_density_field; double mass_kg; double charge_C; @@ -75,11 +76,13 @@ class SpeciesBuilder { std::map> backgroundSpecies(); std::shared_ptr timer(); + std::shared_ptr lastSyncTimer(); private: std::map background_species_info; std::map tracked_species_info; std::shared_ptr timer_; + std::shared_ptr last_sync_timer_; std::map> tracked_species; diff --git a/include/simulation/cpu_synchronizer.h b/include/simulation/cpu_synchronizer.h index b9275ba..9e39d37 100755 --- a/include/simulation/cpu_synchronizer.h +++ b/include/simulation/cpu_synchronizer.h @@ -23,16 +23,26 @@ class CpuSynchronizer { std::map>& tracked_species); - void synchronize(DynamicDensityField* field); - void synchronize(CurrentCollector* collector); - void synchronize(DeletionLogger* logger); - void synchronize(Timer* timer); + void synchronize(DynamicDensityField* field, + DynamicDensityField* last_sync_field); + void synchronize(CurrentCollector* collector, + CurrentCollector* last_sync_collector); + void synchronize(DeletionLogger* logger, + DeletionLogger* last_sync_logger); + void synchronize(Timer* timer, Timer* last_sync_timer); - void sumToAll(blitz::Array& array); - void sumToAll(blitz::Array& array); - void sumToAll(blitz::Array& array); - void sumToAll(double& value); - void sumToAll(long int& value); + + /* + sums delta = * - last_sync_* to last_sync_* over all CPUs + */ + void sumToAll(blitz::Array& array, + blitz::Array& last_sync_array); + void sumToAll(blitz::Array& array, + blitz::Array& last_sync_array); + void sumToAll(blitz::Array& array, + blitz::Array& last_sync_array); + void sumToAll(double& value, double& last_sync_value); + void sumToAll(long int& value, long int& last_sync_value); }; #endif /* CPU_SYNCHRONIZER_H */ diff --git a/include/simulation/species.h b/include/simulation/species.h index 7a4fc0e..6898bba 100755 --- a/include/simulation/species.h +++ b/include/simulation/species.h @@ -61,13 +61,15 @@ class TrackedSpecie : public Specie { std::shared_ptr reflector, std::shared_ptr density_field, std::shared_ptr velocity_distribution, - std::shared_ptr drift_velocity); + std::shared_ptr drift_velocity, + std::shared_ptr last_sync_density_field); DensityField* densityField(); VelocityDistribution* velocityDistibution(); DriftVelocity* driftVelocity(); DynamicDensityField* dynamicDensityField(); + DynamicDensityField* lastSyncDensityField(); Mover* mover(); Terminator* terminator(); Reflector* reflector(); @@ -75,9 +77,16 @@ class TrackedSpecie : public Specie { DeletionLogger* deletionLogger(); void deletionLogger(std::shared_ptr new_logger); + DeletionLogger* lastSyncDeletionLogger(); + void lastSyncDeletionLogger(std::shared_ptr new_logger); + CurrentCollector* currentCollector(); void currentCollector(std::shared_ptr new_collector); + CurrentCollector* lastSyncCurrentCollector(); + void lastSyncCurrentCollector( + std::shared_ptr new_collector); + std::vector> interactions(); void addInteraction(std::shared_ptr interaction); @@ -93,6 +102,13 @@ class TrackedSpecie : public Specie { std::shared_ptr deletion_logger_; std::shared_ptr current_collector_; std::vector> interactions_; + + // fields to store the date of the last synchronization + // (after a synchronization the last_sync field and the non-last_sync field + // contain the same data) + std::shared_ptr last_sync_density_field_; + std::shared_ptr last_sync_deletion_logger_; + std::shared_ptr last_sync_current_collector_; }; class BackgroundSpecie : public Specie { diff --git a/src/generators/species_generator.cpp b/src/generators/species_generator.cpp index ff4a4e7..7847f06 100755 --- a/src/generators/species_generator.cpp +++ b/src/generators/species_generator.cpp @@ -100,9 +100,14 @@ std::shared_ptr SpeciesBuilder::timer() { return timer_; } +std::shared_ptr SpeciesBuilder::lastSyncTimer() { + return last_sync_timer_; +} + void SpeciesBuilder::initTimer() { DEBUG_LEVEL_2("Init timer."); timer_ = std::shared_ptr(new Timer()); + last_sync_timer_ = std::shared_ptr(new Timer()); } void SpeciesBuilder::initBackgroundSpecies(Parameter& parameter) { @@ -251,6 +256,12 @@ void SpeciesBuilder::fillTrackedDensity(Parameter& parameter) { json_path_append(b.second.path_in_json, {"DensityField", "DynamicDensityField"}), timer_); + + b.second.last_sync_density_field = buildDynamicDensityField( + parameter, + json_path_append(b.second.path_in_json, + {"DensityField", "DynamicDensityField"}), + last_sync_timer_); } } @@ -270,7 +281,7 @@ void SpeciesBuilder::buildSpecies() { b.second.type, b.second.name, b.second.mass_kg, b.second.charge_C, b.second.mover, b.second.terminator, b.second.reflector, b.second.density_field, b.second.velocity_distribution, - b.second.drift_velocity)); + b.second.drift_velocity, b.second.last_sync_density_field)); } } @@ -282,7 +293,15 @@ void SpeciesBuilder::fillCurrentCollector(Parameter& parameter) { json_path_append(tracked_species_info.at(b.first).path_in_json, {"CurrentCollector"}), timer_); + // has to be recreated, if it would be copied the timer_ pointer would + // point to the same object -> could not get updated during synchronization + auto last_sync_current_collector = buildCurrentCollector( + parameter, + json_path_append(tracked_species_info.at(b.first).path_in_json, + {"CurrentCollector"}), + last_sync_timer_); b.second->currentCollector(current_collector); + b.second->lastSyncCurrentCollector(last_sync_current_collector); } } @@ -294,6 +313,8 @@ void SpeciesBuilder::fillLoggers(Parameter& parameter) { json_path_append(tracked_species_info.at(b.first).path_in_json, {"Loggers"})); b.second->deletionLogger(deletion_logger); + b.second->lastSyncDeletionLogger( + std::make_shared(*deletion_logger)); } } diff --git a/src/simulation/cpu_synchronizer.cpp b/src/simulation/cpu_synchronizer.cpp index 7ea1259..a00bdc5 100755 --- a/src/simulation/cpu_synchronizer.cpp +++ b/src/simulation/cpu_synchronizer.cpp @@ -7,68 +7,123 @@ #include "simulation/species.h" #include "simulation/mpi_communication.h" -void CpuSynchronizer::sumToAll(blitz::Array& array) { +void CpuSynchronizer::sumToAll(blitz::Array& array, + blitz::Array& last_sync_array) { int size = array.size(); - MPI_Allreduce(MPI_IN_PLACE, array.data(), size, MPI_DOUBLE, MPI_SUM, - MPI_COMM_WORLD); - array /= MpiCommunication::mpi_size; + blitz::Array delta(array.extent()); + delta = array - last_sync_array; + MPI_Allreduce(MPI_IN_PLACE, delta.data(), size, MPI_DOUBLE, + MPI_SUM, MPI_COMM_WORLD); + last_sync_array += delta; } -void CpuSynchronizer::sumToAll(blitz::Array& array) { +void CpuSynchronizer::sumToAll(blitz::Array& array, + blitz::Array& last_sync_array) { int size = array.size(); - MPI_Allreduce(MPI_IN_PLACE, array.data(), size, MPI_LONG, MPI_SUM, - MPI_COMM_WORLD); + blitz::Array delta(array.extent()); + delta = array - last_sync_array; + MPI_Allreduce(MPI_IN_PLACE, delta.data(), size, MPI_LONG, + MPI_SUM, MPI_COMM_WORLD); + last_sync_array += delta; } -void CpuSynchronizer::sumToAll(blitz::Array& array) { +void CpuSynchronizer::sumToAll(blitz::Array& array, + blitz::Array& last_sync_array) { int size = array.size(); - MPI_Allreduce(MPI_IN_PLACE, array.data(), size, MPI_DOUBLE, MPI_SUM, - MPI_COMM_WORLD); - array /= MpiCommunication::mpi_size; + blitz::Array delta(array.extent()); + delta = array - last_sync_array; + MPI_Allreduce(MPI_IN_PLACE, delta.data(), size, MPI_DOUBLE, + MPI_SUM, MPI_COMM_WORLD); + last_sync_array += delta; } -void CpuSynchronizer::sumToAll(double& value) { +void CpuSynchronizer::sumToAll(double& value, double& last_sync_value) { int size = 1; - MPI_Allreduce(MPI_IN_PLACE, &value, size, MPI_DOUBLE, MPI_SUM, + double delta = value - last_sync_value; + MPI_Allreduce(MPI_IN_PLACE, &delta, size, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); - value /= MpiCommunication::mpi_size; + last_sync_value += delta; } -void CpuSynchronizer::sumToAll(long int& value) { +void CpuSynchronizer::sumToAll(long int& value, long int& last_sync_value) { int size = 1; - MPI_Allreduce(MPI_IN_PLACE, &value, size, MPI_LONG, MPI_SUM, MPI_COMM_WORLD); + long int delta = value - last_sync_value; + MPI_Allreduce(MPI_IN_PLACE, &delta, size, MPI_LONG, MPI_SUM, + MPI_COMM_WORLD); + last_sync_value += delta; } -void CpuSynchronizer::synchronize(Timer* timer) { +void CpuSynchronizer::synchronize(Timer* timer, Timer* last_sync_timer) { + if(timer == last_sync_timer) { + ERROR("Synchronization of timers cannot work, point to same object"); + } double time_s = timer->time(); - sumToAll(time_s); - timer->setTime(time_s); + double last_sync_time_s = last_sync_timer->time(); + sumToAll(time_s, last_sync_time_s); + timer->setTime(last_sync_time_s); + last_sync_timer->setTime(last_sync_time_s); } -void CpuSynchronizer::synchronize(DynamicDensityField* field) { - if (field) { - sumToAll(field->time_in_bin_s_); - sumToAll(field->deleted_particles_); +void CpuSynchronizer::synchronize(DynamicDensityField* field, + DynamicDensityField* last_sync_field) { + if (field && last_sync_field) { + if(field == last_sync_field) { + ERROR("The way last_sync_field is created does not work as intended."); + } + sumToAll(field->time_in_bin_s_, last_sync_field->time_in_bin_s_); + field->time_in_bin_s_ = last_sync_field->time_in_bin_s_; + + sumToAll(field->deleted_particles_, last_sync_field->deleted_particles_); + field->deleted_particles_ = last_sync_field->deleted_particles_; } } -void CpuSynchronizer::synchronize(CurrentCollector* collector) { - if (collector) { - sumToAll(collector->z_negative_crossings); - sumToAll(collector->z_positive_crossings); - sumToAll(collector->phi_negative_crossings); - sumToAll(collector->phi_positive_crossings); - sumToAll(collector->r_negative_crossings); - sumToAll(collector->r_positive_crossings); +void CpuSynchronizer::synchronize(CurrentCollector* collector, + CurrentCollector* last_sync_collector) { + if (collector && last_sync_collector) { + sumToAll(collector->z_negative_crossings, + last_sync_collector->z_negative_crossings); + collector->z_negative_crossings = + last_sync_collector->z_negative_crossings; + + sumToAll(collector->z_positive_crossings, + last_sync_collector->z_positive_crossings); + collector->z_positive_crossings = + last_sync_collector->z_positive_crossings; + + sumToAll(collector->phi_negative_crossings, + last_sync_collector->phi_negative_crossings); + collector->phi_negative_crossings = + last_sync_collector->phi_negative_crossings; + + sumToAll(collector->phi_positive_crossings, + last_sync_collector->phi_positive_crossings); + collector->phi_positive_crossings = + last_sync_collector->phi_positive_crossings; + + sumToAll(collector->r_negative_crossings, + last_sync_collector->r_negative_crossings); + collector->r_negative_crossings = + last_sync_collector->r_negative_crossings; + + sumToAll(collector->r_positive_crossings, + last_sync_collector->r_positive_crossings); + collector->r_positive_crossings = + last_sync_collector->r_positive_crossings; } } -void CpuSynchronizer::synchronize(DeletionLogger* logger) { - if (logger) { +void CpuSynchronizer::synchronize(DeletionLogger* logger, + DeletionLogger* last_sync_logger) { + if (logger && last_sync_logger) { for (auto& pair : logger->num_deletions) { long int num_deletion = pair.second; - sumToAll(num_deletion); - pair.second = num_deletion; + long int last_sync_num_deletion = last_sync_logger->num_deletions[pair.first]; + sumToAll(num_deletion, last_sync_num_deletion); + // last_sync_num_deletion now contains the total number of deletions + // across all cpus -> update the values in the objects + pair.second = last_sync_num_deletion; + last_sync_logger->num_deletions[pair.first] = last_sync_num_deletion; } } } @@ -78,11 +133,19 @@ void CpuSynchronizer::synchronize( tracked_species) { MPI_Barrier(MPI_COMM_WORLD); auto it = tracked_species.begin(); - synchronize(it->second->dynamicDensityField()->timer_.get()); + DEBUG_LEVEL_3("Timer Synchronization."); + synchronize(it->second->dynamicDensityField()->timer_.get(), + it->second->lastSyncDensityField()->timer_.get()); for (auto& pair : tracked_species) { - synchronize(pair.second->dynamicDensityField()); - synchronize(pair.second->currentCollector()); - synchronize(pair.second->deletionLogger()); + // DEBUG_LEVEL_3("dynden Synchronization."); + synchronize(pair.second->dynamicDensityField(), + pair.second->lastSyncDensityField()); + // DEBUG_LEVEL_3("cc Synchronization."); + synchronize(pair.second->currentCollector(), + pair.second->lastSyncCurrentCollector()); + // DEBUG_LEVEL_3("dellogger Synchronization."); + synchronize(pair.second->deletionLogger(), + pair.second->lastSyncDeletionLogger()); } } \ No newline at end of file diff --git a/src/simulation/species.cpp b/src/simulation/species.cpp index 1e01505..33e42a6 100755 --- a/src/simulation/species.cpp +++ b/src/simulation/species.cpp @@ -41,7 +41,8 @@ TrackedSpecie::TrackedSpecie( std::shared_ptr reflector, std::shared_ptr density_field, std::shared_ptr velocity_distribution, - std::shared_ptr drift_velocity) + std::shared_ptr drift_velocity, + std::shared_ptr last_sync_density_field) : Specie(type, name, mass_kg, charge_C), mover_(mover), terminator_(terminator), @@ -50,7 +51,10 @@ TrackedSpecie::TrackedSpecie( deletion_logger_(std::shared_ptr(nullptr)), current_collector_(std::shared_ptr(nullptr)), velocity_distribution_(velocity_distribution), - drift_velocity_(drift_velocity) {} + drift_velocity_(drift_velocity), + last_sync_density_field_(last_sync_density_field), + last_sync_deletion_logger_(std::shared_ptr(nullptr)), + last_sync_current_collector_(std::shared_ptr(nullptr)) {} DensityField* TrackedSpecie::densityField() { return density_field_.get(); @@ -67,6 +71,10 @@ DynamicDensityField* TrackedSpecie::dynamicDensityField() { return density_field_.get(); } +DynamicDensityField* TrackedSpecie::lastSyncDensityField() { + return last_sync_density_field_.get(); +} + Mover* TrackedSpecie::mover() { return mover_.get(); } @@ -78,14 +86,23 @@ Terminator* TrackedSpecie::terminator() { Reflector* TrackedSpecie::reflector() { return reflector_.get(); } + DeletionLogger* TrackedSpecie::deletionLogger() { return deletion_logger_.get(); } +DeletionLogger* TrackedSpecie::lastSyncDeletionLogger() { + return last_sync_deletion_logger_.get(); +} + CurrentCollector* TrackedSpecie::currentCollector() { return current_collector_.get(); } +CurrentCollector* TrackedSpecie::lastSyncCurrentCollector() { + return last_sync_current_collector_.get(); +} + std::vector> TrackedSpecie::interactions() { return interactions_; @@ -100,11 +117,20 @@ void TrackedSpecie::deletionLogger(std::shared_ptr new_logger) { deletion_logger_ = new_logger; } +void TrackedSpecie::lastSyncDeletionLogger(std::shared_ptr new_logger) { + last_sync_deletion_logger_ = new_logger; +} + void TrackedSpecie::currentCollector( std::shared_ptr new_collector) { current_collector_ = new_collector; } +void TrackedSpecie::lastSyncCurrentCollector( + std::shared_ptr new_collector) { + last_sync_current_collector_ = new_collector; +} + BackgroundSpecie::BackgroundSpecie( ImplementedSpecies type, std::string name,