Changed CPU synchronization from averaging to
communicating changes
This commit is contained in:
@@ -100,9 +100,14 @@ std::shared_ptr<Timer> SpeciesBuilder::timer() {
|
||||
return timer_;
|
||||
}
|
||||
|
||||
std::shared_ptr<Timer> SpeciesBuilder::lastSyncTimer() {
|
||||
return last_sync_timer_;
|
||||
}
|
||||
|
||||
void SpeciesBuilder::initTimer() {
|
||||
DEBUG_LEVEL_2("Init timer.");
|
||||
timer_ = std::shared_ptr<Timer>(new Timer());
|
||||
last_sync_timer_ = std::shared_ptr<Timer>(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<DeletionLogger>(*deletion_logger));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,68 +7,123 @@
|
||||
#include "simulation/species.h"
|
||||
#include "simulation/mpi_communication.h"
|
||||
|
||||
void CpuSynchronizer::sumToAll(blitz::Array<double, 4>& array) {
|
||||
void CpuSynchronizer::sumToAll(blitz::Array<double, 4>& array,
|
||||
blitz::Array<double, 4>& 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<double, 4> 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<long int, 4>& array) {
|
||||
void CpuSynchronizer::sumToAll(blitz::Array<long int, 4>& array,
|
||||
blitz::Array<long int, 4>& last_sync_array) {
|
||||
int size = array.size();
|
||||
MPI_Allreduce(MPI_IN_PLACE, array.data(), size, MPI_LONG, MPI_SUM,
|
||||
MPI_COMM_WORLD);
|
||||
blitz::Array<long int, 4> 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<double, 3>& array) {
|
||||
void CpuSynchronizer::sumToAll(blitz::Array<double, 3>& array,
|
||||
blitz::Array<double, 3>& 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<double, 3> 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());
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,8 @@ TrackedSpecie::TrackedSpecie(
|
||||
std::shared_ptr<Reflector> reflector,
|
||||
std::shared_ptr<DynamicDensityField> density_field,
|
||||
std::shared_ptr<VelocityDistribution> velocity_distribution,
|
||||
std::shared_ptr<DriftVelocity> drift_velocity)
|
||||
std::shared_ptr<DriftVelocity> drift_velocity,
|
||||
std::shared_ptr<DynamicDensityField> 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<DeletionLogger>(nullptr)),
|
||||
current_collector_(std::shared_ptr<CurrentCollector>(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<DeletionLogger>(nullptr)),
|
||||
last_sync_current_collector_(std::shared_ptr<CurrentCollector>(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<std::shared_ptr<interaction::Interaction>>
|
||||
TrackedSpecie::interactions() {
|
||||
return interactions_;
|
||||
@@ -100,11 +117,20 @@ void TrackedSpecie::deletionLogger(std::shared_ptr<DeletionLogger> new_logger) {
|
||||
deletion_logger_ = new_logger;
|
||||
}
|
||||
|
||||
void TrackedSpecie::lastSyncDeletionLogger(std::shared_ptr<DeletionLogger> new_logger) {
|
||||
last_sync_deletion_logger_ = new_logger;
|
||||
}
|
||||
|
||||
void TrackedSpecie::currentCollector(
|
||||
std::shared_ptr<CurrentCollector> new_collector) {
|
||||
current_collector_ = new_collector;
|
||||
}
|
||||
|
||||
void TrackedSpecie::lastSyncCurrentCollector(
|
||||
std::shared_ptr<CurrentCollector> new_collector) {
|
||||
last_sync_current_collector_ = new_collector;
|
||||
}
|
||||
|
||||
BackgroundSpecie::BackgroundSpecie(
|
||||
ImplementedSpecies type,
|
||||
std::string name,
|
||||
|
||||
Reference in New Issue
Block a user