Commit 54c46406 authored by Johannes Leins's avatar Johannes Leins
Browse files

removed benchmark class and its (faulty?) singleton

parent 41e6c3f7
Pipeline #26547 passed with stage
in 27 seconds
......@@ -8,7 +8,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(
hileg
src/main.cpp
src/Benchmark.cpp
src/Cell.cpp
src/Climate.cpp
src/ClimateScenario.cpp
......
/*
* Benchmark.cpp
*
* Created on: 25.01.2021
* Author: Johannes Leins
*/
#include "Benchmark.h"
long Benchmark::NEXT_ID = 1;
Benchmark::Benchmark(bool active) {
this->active = active;
}
Benchmark::~Benchmark() {
}
bool Benchmark::flick() {
active = !active;
return active;
}
long Benchmark::mark(long ID) {
if(!active) {
return -1;
}
milliseconds tMilli = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
ID = ID < 0 ? Benchmark::NEXT_ID++ : ID;
if(marks.find(ID) == marks.end()) {
marks[ID] = pair<milliseconds,milliseconds>(tMilli,tMilli-tMilli);
} else {
marks[ID].first = tMilli;
}
return ID;
}
void Benchmark::stop(long ID) {
if(!active || marks.find(ID) == marks.end()) {
return;
}
milliseconds tMilli = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
pair<milliseconds,milliseconds> bMark = marks[ID];
bMark.second += (tMilli - bMark.first);
bMark.first = tMilli;
marks[ID] = bMark;
}
void Benchmark::reset() {
marks.clear();
}
void Benchmark::print(long ID) {
if(!active) {
return;
}
if(ID < 0) {
for(auto bMark : marks) {
print(bMark.first);
}
}
if(marks.find(ID) != marks.end()) {
cout << "BM mark[" << ID << "]->" << marks[ID].second << endl;
}
}
/*
* Benchmark.h
*
* Created on: 25.01.2021
* Author: Johannes Leins
*/
#ifndef SRC_BENCHMARK_H_
#define SRC_BENCHMARK_H_
#include <chrono>
#include <map>
#include <iostream>
#include <time.h>
#include <date.h>
#include <sys/stat.h>
#include <stdio.h>
using namespace std;
using namespace date;
using namespace std::chrono;
class Benchmark {
private:
Benchmark(bool active = false);
Benchmark(Benchmark const& other) = delete;
Benchmark(Benchmark&& other) = delete;
virtual ~Benchmark();
bool active;
map<long, pair<milliseconds,milliseconds>> marks;
protected:
static long NEXT_ID;
public:
static Benchmark& getInstance()
{
static Benchmark instance;
return instance;
}
bool flick();
long mark(long ID = -1);
void stop(long ID);
void reset();
void print(long ID = -1);
};
#endif /* SRC_BENCHMARK_H_ */
......@@ -26,7 +26,6 @@
#include <vector>
#include <string>
#include "Point.h"
#include "Benchmark.h"
enum GlobalModel {
CCCma, CNRM, ICHEC, MIROC, MOHC, MPI
......
......@@ -155,26 +155,20 @@ Measure* Environment::getMeasure(Point coord) {
Measure* measure = new Measure(Constants::ID_DISTURBANCE_NONE, timing);
bool uniformUnfixed = uniformDisturbances && fixedDisturbanceCoords.find(coord) == fixedDisturbanceCoords.end();
Benchmark::getInstance().mark(1000110);
if(activeCellsByCoord.find(coord) != activeCellsByCoord.end()) {
Benchmark::getInstance().mark(1000111);
if(uniformUnfixed) {
measure = activeCellsByCoord[coord]->getMeasure(uniformDisturbanceID);
}else{
measure = activeCellsByCoord[coord]->getMeasure();
}
Benchmark::getInstance().stop(1000111);
}
else if(cellsByCoord.find(coord) != cellsByCoord.end()) {
Benchmark::getInstance().mark(1000112);
if(uniformUnfixed) {
measure = cellsByCoord[coord]->getMeasure(uniformDisturbanceID);
}else{
measure = cellsByCoord[coord]->getMeasure();
}
Benchmark::getInstance().stop(1000112);
}
Benchmark::getInstance().stop(1000110);
return measure;
}
......@@ -185,13 +179,11 @@ bool Environment::hasDisturbanceScheduled(Point coord) {
float Environment::getValue(ClimateVariable climVar, Point coord) {
Benchmark::getInstance().mark(1014350);
Cell* cell = activeCellsByCoord[coord];
if(cell == NULL) {
cell = cellsByCoord[coord];
}
float value = climate->getValue(climVar, timestep, indexTime, cell);
Benchmark::getInstance().stop(1014350);
return value;
}
......@@ -204,13 +196,11 @@ void Environment::setDisturbanceAt(Point coord, int disturbanceID) {
if(uniformDisturbances) {
fixedDisturbanceCoords.insert(coord);
}
Benchmark::getInstance().mark(1011001);
if(activeCellsByCoord.find(coord) != activeCellsByCoord.end()) {
activeCellsByCoord[coord]->setActiveMeasure(disturbanceID);
} else {
cellsByCoord[coord]->setActiveMeasure(disturbanceID);
}
Benchmark::getInstance().stop(1011001);
//// auto itMeasuresAtCoord = measures.find(coord);
// if(measures.find(coord) != measures.end()) {
//// auto itMeasures = itMeasuresAtCoord->second.find(disturbanceID);
......@@ -405,12 +395,10 @@ void Environment::addActiveNeighbor(int key, Point activeNeighbor) {
if(activeNeighborhoods.find(key) != activeNeighborhoods.end()) {
activeNeighborhood = activeNeighborhoods[key];
}
Benchmark::getInstance().mark(1000105);
if(activeNeighborhood.find(activeNeighbor) == activeNeighborhood.end()) {
activeNeighborhood[activeNeighbor] = neighborhoods[key].at(activeNeighbor);
activeCellsByCoord[activeNeighbor] = cellsByCoord.at(activeNeighbor);
}
Benchmark::getInstance().stop(1000105);
activeNeighborhoods[key] = activeNeighborhood;
}
......@@ -423,26 +411,18 @@ void Environment::removeActiveNeighbor(int key, Point activeNeighbor) {
void Environment::addActiveNeighbors(int key, unordered_set<Point> activeNeighbors) {
// unordered_map<Point, Population*> activeNeighborhood;
Benchmark::getInstance().mark(1013210);
// if(activeNeighborhoods.find(key) != activeNeighborhoods.end()) {
// Benchmark::getInstance().mark(1013211);
// activeNeighborhood = activeNeighborhoods[key];
// Benchmark::getInstance().stop(1013211);
// }
Benchmark::getInstance().stop(1013210);
Benchmark::getInstance().mark(1013220);
for(auto activeNeighbor : activeNeighbors) {
if(activeNeighborhoods[key].find(activeNeighbor) == activeNeighborhoods[key].end()) {
Benchmark::getInstance().mark(1013221);
activeNeighborhoods[key][activeNeighbor] = neighborhoods[key].at(activeNeighbor);
activeCellsByCoord[activeNeighbor] = cellsByCoord.at(activeNeighbor);
Benchmark::getInstance().stop(1013221);
}
}
Benchmark::getInstance().stop(1013220);
Benchmark::getInstance().mark(1013230);
// activeNeighborhoods[key] = activeNeighborhood;
Benchmark::getInstance().stop(1013230);
}
unordered_map<Point, Population*> Environment::getActiveNeighborhood(int key) {
......
......@@ -29,7 +29,6 @@
#include <map>
#include <unordered_map>
#include "Disturbance.h"
#include "Benchmark.h"
// forward declaration to prevent circular inclusion
class Population;
......
......@@ -130,44 +130,32 @@ vector<LifeStage*> Population::getStages() {
unordered_set<Point> Population::updateFlows(Environment* environment) {
Benchmark::getInstance().mark(1013101);
for(auto flow : immigrationFlows) {
flow->update(environment);
}
Benchmark::getInstance().stop(1013101);
unordered_set<Point> activatedNeighbors;
if(vitality == DEAD) {
return activatedNeighbors;
}
Benchmark::getInstance().mark(1013102);
for(auto flow : flowsByType[REPRODUCTION]) {
flow->update(environment);
}
Benchmark::getInstance().stop(1013102);
vector<Flow*> flowsMigr = flowsByType[MIGRATION];
Benchmark::getInstance().mark(1013103);
shuffle(flowsMigr.begin(), flowsMigr.end(), *(environment->getRandGenLocal(atts->getCoord())));
Benchmark::getInstance().stop(1013103);
Benchmark::getInstance().mark(1013104);
for(auto flow : flowsMigr) {
if(flow->update(environment)) {
activatedNeighbors.insert(flow->getTo()->getAtts()->getCoord());
}
}
Benchmark::getInstance().stop(1013104);
Benchmark::getInstance().mark(1013105);
for(auto flow : flowsByType[MORTALITY]) {
flow->update(environment);
}
Benchmark::getInstance().stop(1013105);
Benchmark::getInstance().mark(1013106);
for(auto flow : flowsByType[TRANSFER]) {
flow->update(environment);
}
Benchmark::getInstance().stop(1013106);
return activatedNeighbors;
}
......
......@@ -23,7 +23,6 @@
#include <iostream>
#include <stdio.h>
#include "Population.h"
#include "Benchmark.h"
#include "Environment.h"
#include "ClimateScenario.h"
#include "LifeCycle.h"
......@@ -1263,9 +1262,6 @@ bool handleArgs(int argc, char** argv) {
else if (arg == "--dailyPrint") { // print daily simulation info
dailyPrint = true;
}
else if (arg == "--benchmark") { // run in prognosis mode applying all disturbance scenarios in unfixed cells of scheme
Benchmark::getInstance().flick();
}
else {
cout << "ERROR: invalid command line parameter '" << arg << "' defined. Printing help page" << endl;;
printHelp();
......@@ -1490,7 +1486,6 @@ bool setup() {
int batchRun() {
// get current timestamp from system clock
runID = runID < 0 ? static_cast<long int>(time(NULL)) : runID;
auto& benchmark = Benchmark::getInstance();
// BEGIN create output folders
const char* folderNameOutput = "output";
......@@ -1633,21 +1628,17 @@ int batchRun() {
int nAliveTotal = 0;
int nAlivePrevTotal = 0;
// TODO load next time frame if not the full time slice is looped
Benchmark::getInstance().mark(1010000);
for (auto disturbanceScheme : disturbanceSchemes) { // loop over each disturbance scheme
Benchmark::getInstance().mark(1011000);
currDisturbanceSchemeID = disturbanceScheme.first;
for(auto lastSchemeCoord : lastSchemeCoords) {
environment->setDisturbanceAt(lastSchemeCoord, baseDisturbanceScheme[lastSchemeCoord]);
}
Benchmark::getInstance().stop(1011000);
lastSchemeCoords.clear();
if(nAlivePrev[currDisturbanceSchemeID] == 0 && dispersalType != BASE_IMMIGRATION && dispersalType != BOTH) { // all dead and no base immigration
// benchmark.stop(10);
continue;
}
nAlivePrev[currDisturbanceSchemeID] = 0;
Benchmark::getInstance().mark(1012000);
if(disturbanceSchemes.size() > 1) {
for(auto pairCoordDist : disturbanceScheme.second) {
Point currSchemeCoord = pairCoordDist.first;
......@@ -1658,68 +1649,42 @@ int batchRun() {
environment->setDisturbanceAt(currSchemeCoord, newDisturbanceID);
}
}
Benchmark::getInstance().stop(1012000);
unordered_map<Point, Population*> activeNeighborhood = environment->getActiveNeighborhood(currDisturbanceSchemeID);
// unordered_set<Point> newActiveNeighborhood;
Benchmark::getInstance().mark(1013000);
for(auto activeNeighbor : activeNeighborhood) {
Population* population = activeNeighbor.second;
if(dispersalType != DispersalType::IMMOBILE // dispersal and/or base immigration
|| population->isAlive()) { // or population not extinct
Benchmark::getInstance().mark(1013100);
unordered_set<Point> activatedNeighbors = population->updateFlows(environment);
Benchmark::getInstance().stop(1013100);
Benchmark::getInstance().mark(1013200);
environment->addActiveNeighbors(currDisturbanceSchemeID, activatedNeighbors);
Benchmark::getInstance().stop(1013200);
Benchmark::getInstance().mark(1013300);
if(population->isAlive()) {
nAlivePrev[currDisturbanceSchemeID]++;
nAlivePrevTotal++;
}
Benchmark::getInstance().stop(1013300);
}
}
Benchmark::getInstance().stop(1013000);
unsigned int iCoord = 0;
// loop through all defined coordinates
Benchmark::getInstance().mark(1014000);
Benchmark::getInstance().mark(1014100);
activeNeighborhood = environment->getActiveNeighborhood(currDisturbanceSchemeID); // get updated list of neighbors
Benchmark::getInstance().stop(1014100);
for(auto activeNeighbor : activeNeighborhood) {
currentCoord = activeNeighbor.first;
Population* population = activeNeighbor.second;
Benchmark::getInstance().mark(1014200);
currDisturbanceID = environment->getDisturbanceAt(currentCoord);
Benchmark::getInstance().stop(1014200);
iCoord++;
Benchmark::getInstance().mark(1014300);
if(dispersalType != DispersalType::IMMOBILE // dispersal and/or base immigration
|| population->isAlive()) { // and population extinct
Benchmark::getInstance().mark(1014310);
population->updateStages(environment);
Benchmark::getInstance().stop(1014310);
Benchmark::getInstance().mark(1014320);
population->updateStats(environment);
Benchmark::getInstance().stop(1014320);
Benchmark::getInstance().mark(1014330);
outputRun(nTimesteps, population);
Benchmark::getInstance().stop(1014330);
Benchmark::getInstance().mark(1014340);
if(population->isAlive()) {
nAliveTotal++;
}else{
environment->removeActiveNeighbor(currDisturbanceSchemeID, currentCoord);
}
Benchmark::getInstance().stop(1014340);
}
Benchmark::getInstance().stop(1014300);
} // end coord loop
Benchmark::getInstance().stop(1014000);
} // end disturbance loop
Benchmark::getInstance().stop(1010000);
if(dailyPrint) {
milliseconds dailyEnd = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
......@@ -1752,8 +1717,6 @@ int batchRun() {
tBegin = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
benchmark.print();
benchmark.reset();
if(yearly) {
outputStats(fileStatsYearly);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment