optimized queue and backup queue a bit

This commit is contained in:
2024-05-15 09:57:31 +02:00
parent fcbff96f17
commit d6ee7e8be8
2 changed files with 42 additions and 43 deletions
+6 -5
View File
@@ -34,11 +34,8 @@ class Simulation {
void init(Parameter& parameter); void init(Parameter& parameter);
void start(); void start();
void finalize(); void finalize();
void processEvents(int events, std::vector<particle::Particle>& backup_queue, void processEvents(int events, const int time_resolution_events);
const int time_resolution_events); void processUntilTermination(particle::Particle& particle);
void processUntilTermination(particle::Particle& particle,
std::vector<particle::Particle>& particle_queue,
std::vector<particle::Particle>& backup_queue);
private: private:
std::vector<particle::Particle> determineInjectedParticles( std::vector<particle::Particle> determineInjectedParticles(
@@ -71,6 +68,10 @@ class Simulation {
std::shared_ptr<SimulationOutput> output_; std::shared_ptr<SimulationOutput> output_;
std::shared_ptr<CpuSynchronizer> cpu_synchronizer_; std::shared_ptr<CpuSynchronizer> cpu_synchronizer_;
std::vector<particle::Particle> queues_[2];
int active_queue_;
int non_active_queue_;
}; };
#endif /* SIMULATION_H */ #endif /* SIMULATION_H */
+36 -38
View File
@@ -69,6 +69,9 @@ void Simulation::init(Parameter& parameter) {
DEBUG_LEVEL_0("Init cpu synchronizer."); DEBUG_LEVEL_0("Init cpu synchronizer.");
cpu_synchronizer_ = std::make_shared<CpuSynchronizer>(); cpu_synchronizer_ = std::make_shared<CpuSynchronizer>();
DEBUG_LEVEL_3("Init done."); DEBUG_LEVEL_3("Init done.");
active_queue_ = 0;
non_active_queue_ = 1;
} }
int eventsThisCpu(int events_all_cpus) { int eventsThisCpu(int events_all_cpus) {
@@ -88,6 +91,11 @@ int eventsOfCycle(int stride, int leftover_events) {
} }
} }
void switch_active(int& active, int& non_active) {
non_active = active;
active = (active + 1)%2;
}
void Simulation::finalize() { void Simulation::finalize() {
SIM_INFO("Simulation ended."); SIM_INFO("Simulation ended.");
} }
@@ -106,22 +114,22 @@ void Simulation::start() {
SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events)); SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events));
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride)); SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
const int events_this_cpu = eventsThisCpu(total_simulated_events); const int events_this_cpu = eventsThisCpu(total_simulated_events);
// maybe better as class memeber...
const int time_resolution_this_cpu = eventsThisCpu(time_resolution_events); const int time_resolution_this_cpu = eventsThisCpu(time_resolution_events);
SIM_INFO("Events per cpu (approx.): " + std::to_string(events_this_cpu)); SIM_INFO("Events per cpu (approx.): " + std::to_string(events_this_cpu));
int leftover_events = events_this_cpu; int leftover_events = events_this_cpu;
int leftover_events_time_resolution = time_resolution_this_cpu; int leftover_events_time_resolution = time_resolution_this_cpu;
const double delta_t = time_resolution_events*injector_->timePassed(); const double delta_t = time_resolution_events*injector_->timePassed();
int num_cpu_syncs = 0; int num_cpu_syncs = 0;
bool reset = true; bool reset = true;
std::vector<particle::Particle> backup_queue;
backup_queue.reserve(100); // probably should be a more sensible value
// bring some order into the reset checks
while (leftover_events > 0) {
if(reset)
timer_->increaseTimestep(delta_t);
queues_[0].reserve(events_this_cpu);
queues_[1].reserve(events_this_cpu);
// bring some order into the reset checks
timer_->increaseTimestep(delta_t);
while (leftover_events > 0) {
std::stringstream out; std::stringstream out;
out << "Timeinterval: " << timer_->time() << "s."; out << "Timeinterval: " << timer_->time() << "s.";
out << "\n size of particle: " << sizeof(particle::Particle); out << "\n size of particle: " << sizeof(particle::Particle);
@@ -130,8 +138,9 @@ void Simulation::start() {
int events_of_cylcle = eventsOfCycle(event_stride, leftover_events_time_resolution); int events_of_cylcle = eventsOfCycle(event_stride, leftover_events_time_resolution);
SIM_INFO("Leftover events on first cpu: " + SIM_INFO("Leftover events on first cpu: " +
std::to_string(leftover_events)); std::to_string(leftover_events));
processEvents(events_of_cylcle, backup_queue, time_resolution_events); processEvents(events_of_cylcle, time_resolution_events);
DEBUG_LEVEL_3("Size of backup queue: " + std::to_string(backup_queue.size())); DEBUG_LEVEL_3("Size of backup queue: " +
std::to_string(queues_[non_active_queue_].size()));
DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events)); DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events));
leftover_events -= events_of_cylcle; leftover_events -= events_of_cylcle;
@@ -158,7 +167,8 @@ void Simulation::start() {
SimulationTimer::eventStopPoint(std::string("Output")); SimulationTimer::eventStopPoint(std::string("Output"));
if(reset) { if(reset) {
SIM_INFO("Reset density, current fields and deletions."); SIM_INFO("Do resets and increase the timestep for timeresolution.");
timer_->increaseTimestep(delta_t);
for(auto& pair : tracked_species_) { for(auto& pair : tracked_species_) {
pair.second->dynamicDensityField()->resetDynamicDensityField(); pair.second->dynamicDensityField()->resetDynamicDensityField();
pair.second->lastSyncDensityField()->resetDynamicDensityField(); pair.second->lastSyncDensityField()->resetDynamicDensityField();
@@ -169,10 +179,8 @@ void Simulation::start() {
pair.second->deletionLogger()->resetDeletionLogger(); pair.second->deletionLogger()->resetDeletionLogger();
pair.second->lastSyncDeletionLogger()->resetDeletionLogger(); pair.second->lastSyncDeletionLogger()->resetDeletionLogger();
} }
}
if ((leftover_events > 0) && reset) {
leftover_events_time_resolution = time_resolution_this_cpu; leftover_events_time_resolution = time_resolution_this_cpu;
switch_active(active_queue_, non_active_queue_);
} }
} }
} }
@@ -181,24 +189,13 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
return random->rollUniform(0, queue_length); return random->rollUniform(0, queue_length);
} }
void Simulation::processEvents(int events, void Simulation::processEvents(int events, const int time_resolution_events) {
std::vector<particle::Particle>& backup_queue,
const int time_resolution_events) {
DEBUG_LEVEL_0("Processing events."); DEBUG_LEVEL_0("Processing events.");
std::vector<particle::Particle> particle_queue;
particle_queue.reserve(100);
DEBUG_LEVEL_3("Move backup to processing queue.");
// highly inefficient in case that #cpu_syncs/time_res >> 1, i.e. backup queue is large
particle_queue.insert(particle_queue.end(), backup_queue.begin(),
backup_queue.end());
// clean backup_queue after moving the elements to the particle queue
backup_queue.erase(backup_queue.begin(), backup_queue.end());
DEBUG_LEVEL_3("Size processing queue: " + std::to_string(particle_queue.size()));
int current_event = 0; int current_event = 0;
while ((current_event < events) || (!particle_queue.empty())) { while ((current_event < events) || (!queues_[active_queue_].empty())) {
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event)); DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
if (particle_queue.empty()) { if (queues_[active_queue_].empty()) {
current_event += 1; current_event += 1;
DEBUG_LEVEL_2("Inject particles to queue."); DEBUG_LEVEL_2("Inject particles to queue.");
SimulationTimer::eventStartPoint(std::string("ParticleInjection")); SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
@@ -217,26 +214,27 @@ void Simulation::processEvents(int events,
SimulationTimer::eventStopPoint(std::string("ParticleInjection")); SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
DEBUG_LEVEL_2("Injected particles to queue."); DEBUG_LEVEL_2("Injected particles to queue.");
particle_queue.insert(particle_queue.end(), new_particles.begin(), queues_[active_queue_].insert(queues_[active_queue_].end(), new_particles.begin(),
new_particles.end()); new_particles.end());
} }
DEBUG_LEVEL_2("Particles in queue: " + std::to_string(particle_queue.size())); DEBUG_LEVEL_2("Particles in queue: " +
std::to_string(queues_[active_queue_].size()));
DEBUG_LEVEL_2("Particles in backup queue: " +
std::to_string(queues_[non_active_queue_].size()));
const int particle_number = const int particle_number =
particleNumberFromQueue(particle_queue.size(), random_.get()); particleNumberFromQueue(queues_[active_queue_].size(), random_.get());
// extract particle here from queue, so that an extension of the queue does // extract particle here from queue, so that an extension of the queue does
// not mess up the reference // not mess up the reference
particle::Particle particle = particle_queue.at(particle_number); particle::Particle particle = queues_[active_queue_].at(particle_number);
particle_queue.erase(particle_queue.begin() + particle_number); queues_[active_queue_].erase(queues_[active_queue_].begin() + particle_number);
processUntilTermination(particle, particle_queue, backup_queue); processUntilTermination(particle);
} }
} }
void Simulation::processUntilTermination( void Simulation::processUntilTermination(
particle::Particle& particle, particle::Particle& particle) {
std::vector<particle::Particle>& particle_queue,
std::vector<particle::Particle>& backup_queue) {
DEBUG_LEVEL_3("Process particle until termination"); DEBUG_LEVEL_3("Process particle until termination");
while (particle.specie()->terminator()->isAlive(&particle)) { while (particle.specie()->terminator()->isAlive(&particle)) {
auto old_position = particle.position(); auto old_position = particle.position();
@@ -265,7 +263,7 @@ void Simulation::processUntilTermination(
&particle)) { &particle)) {
DEBUG_LEVEL_3("Move particle " + particle.specie()->name() DEBUG_LEVEL_3("Move particle " + particle.specie()->name()
+" to backup queue."); +" to backup queue.");
backup_queue.push_back(particle); queues_[non_active_queue_].push_back(particle);
break; break;
} else if (particle.specie()->terminator()->leftSimulationDomain( } else if (particle.specie()->terminator()->leftSimulationDomain(
&particle)) { &particle)) {
@@ -304,7 +302,7 @@ void Simulation::processUntilTermination(
particle.increaseInteractionCounter(); particle.increaseInteractionCounter();
for (auto& new_particle : output.newParticles()) { for (auto& new_particle : output.newParticles()) {
particle_queue.push_back(new_particle); queues_[active_queue_].push_back(new_particle);
} }
if (output.deleteCurrentParticle()) { if (output.deleteCurrentParticle()) {