Files
karl/src/simulation/cpu_synchronizer.cpp
T
2024-03-11 16:59:25 +01:00

151 lines
5.6 KiB
C++
Executable File

#include "simulation/cpu_synchronizer.h"
#include <mpi.h>
#include "simulation/current_collector.h"
#include "simulation/density_field.h"
#include "simulation/logger.h"
#include "simulation/species.h"
#include "simulation/mpi_communication.h"
void CpuSynchronizer::sumToAll(blitz::Array<double, 4>& array,
blitz::Array<double, 4>& last_sync_array) {
int size = array.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,
blitz::Array<long int, 4>& last_sync_array) {
int size = array.size();
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,
blitz::Array<double, 3>& last_sync_array) {
int size = array.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, double& last_sync_value) {
int size = 1;
double delta = value - last_sync_value;
MPI_Allreduce(MPI_IN_PLACE, &delta, size, MPI_DOUBLE, MPI_SUM,
MPI_COMM_WORLD);
last_sync_value += delta;
}
void CpuSynchronizer::sumToAll(long int& value, long int& last_sync_value) {
int size = 1;
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, Timer* last_sync_timer) {
if(timer == last_sync_timer) {
ERROR("Synchronization of timers cannot work, point to same object");
}
double time_s = timer->time();
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,
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,
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,
DeletionLogger* last_sync_logger) {
if (logger && last_sync_logger) {
for (auto& pair : logger->num_deletions) {
long int num_deletion = pair.second;
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;
}
}
}
void CpuSynchronizer::synchronize(
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>&
tracked_species) {
MPI_Barrier(MPI_COMM_WORLD);
auto it = tracked_species.begin();
DEBUG_LEVEL_3("Timer Synchronization.");
synchronize(it->second->dynamicDensityField()->timer_.get(),
it->second->lastSyncDensityField()->timer_.get());
for (auto& pair : tracked_species) {
// 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());
}
}