Skip to content

Commit

Permalink
solved bug for PerformanceRegistrar
Browse files Browse the repository at this point in the history
  • Loading branch information
djanloo committed Jul 13, 2024
1 parent eb21369 commit 8aa6553
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 97 deletions.
41 changes: 30 additions & 11 deletions quilt/core/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "include/base.hpp"

#define PERFMGR_OUTPUT_DIGITS 1

#define DEFAULT_LOG_FILE "quilt_log.log"

using std::cout;
using std::endl;
Expand All @@ -21,6 +21,7 @@ using std::vector;

namespace settings {
LogLevel verbosity = INFO;
string global_logfile = "";

void set_verbosity(int value) {
// std::cout << "Set verbosity to " << value << std::endl;
Expand All @@ -35,9 +36,6 @@ namespace settings {
verbosity = static_cast<LogLevel>(value);
}

LogLevel get_verbosity() {
return verbosity;
}
}
//************************* THREAD SAFE FILE ***************************//

Expand Down Expand Up @@ -114,17 +112,30 @@ void Logger::log(LogLevel level, const string& message)
}

Logger& get_global_logger(){
static Logger logger("quilt_log.log");
return logger;
if (!settings::global_logfile.empty()){
static Logger logger(settings::global_logfile);
return logger;
}else{
static Logger logger(DEFAULT_LOG_FILE);
return logger;
}
}

/************************************ PERFORMANCE MANAGER ***********************************/

PerformanceManager::PerformanceManager(string label)
: label(label)
{
// Register itself in the performance registry on creation
PerformanceRegistrar::get_instance().add_manager(this);

stringstream ss;
ss << "Created PerformanceManager at " << this;
get_global_logger().log(DEBUG, ss.str());
}

PerformanceManager::~PerformanceManager(){
stringstream ss;
ss << "Destroyed PerformanceManager at " << this;
get_global_logger().log(DEBUG, ss.str());
}

void PerformanceManager::set_tasks(vector<string> task_names){
Expand Down Expand Up @@ -194,7 +205,6 @@ string PerformanceManager::format_duration (std::chrono::nanoseconds duration) {
return ss.str();
}


PerformanceRegistrar::PerformanceRegistrar(){
stringstream ss;
ss << "Created PerformanceRegistrar at index: "<<this;
Expand All @@ -213,7 +223,8 @@ PerformanceRegistrar& PerformanceRegistrar::get_instance() {
}

// Method to add a PerformanceManager instance to the registrar
void PerformanceRegistrar::add_manager(PerformanceManager* pm) {
void PerformanceRegistrar::add_manager(std::shared_ptr<PerformanceManager> pm) {
cleanup();
instances.push_back(pm);
stringstream ss;
ss << "PerformanceRegistrar: registered manager " << instances.size() <<":"<< pm->label;
Expand All @@ -226,10 +237,18 @@ void PerformanceRegistrar::print_records() {
ss << "Ouput for PerformanceRegistrar "<< "(" << instances.size() << " managers )";
get_global_logger().log(INFO, ss.str());
for (auto pm : instances) {
pm->print_record();
// Locks the weak pointer and prints
auto locked_ptr = pm.lock();
if (locked_ptr) locked_ptr->print_record();
}
}

void PerformanceRegistrar::cleanup(){
get_global_logger().log(DEBUG, "Cleaning up PerformanceRegistrar");
auto expiration_criteria = [](const std::weak_ptr<PerformanceManager>& pm) { return pm.expired(); };
instances.erase(std::remove_if(instances.begin(), instances.end(), expiration_criteria), instances.end());
}

//************************* UTILS FOR DYNAMICAL SYSTEMS **********************//

HierarchicalID::HierarchicalID()
Expand Down
98 changes: 48 additions & 50 deletions quilt/core/include/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstddef>
#include <cmath>
#include <stdexcept>
#include <memory>
#include <vector>
#include <map>
#include <random>
Expand Down Expand Up @@ -35,9 +36,8 @@ enum LogLevel { DEBUG, INFO, WARNING, ERROR, CRITICAL };

namespace settings {
extern LogLevel verbosity;

extern string global_logfile;
void set_verbosity(int value);
LogLevel get_verbosity();
}

class negative_time_exception : public std::exception {
Expand Down Expand Up @@ -80,13 +80,6 @@ class ThreadSafeFile {

//*********************************** LOGGER *****************************************//

/**
Logger logger("logfile.txt");
logger.log(INFO, "Program started.");
logger.log(DEBUG, "Debugging information.");
logger.log(ERROR, "An error occurred.");
*/
class Logger {
public:
Logger(const string& filename);
Expand Down Expand Up @@ -123,6 +116,52 @@ class Logger {
*/
Logger& get_global_logger();

//********************************************************************************//
//******************************* PERFORMANCE MANAGER ****************************//
//********************************************************************************//

class PerformanceManager{
private:
map<string, std::chrono::nanoseconds> task_duration;
map<string, std::chrono::time_point<std::chrono::high_resolution_clock>> task_start_time;
map<string, int> task_count;
map<string, int> task_scale; //!< This prevents overhead for many calls (e.g. evolution of neurons)

public:
string label;
PerformanceManager(string label);
~PerformanceManager();
void set_tasks(vector<string> task_names);
void set_scales(map<string, int> scales);
void set_label(string label);
void start_recording(string task);
void end_recording(string task);
void print_record();
string format_duration(std::chrono::nanoseconds duration);
};

class PerformanceRegistrar {
private:
std::vector<std::weak_ptr<PerformanceManager>> instances;

PerformanceRegistrar(); // Private constructor to prevent instantiation
~PerformanceRegistrar();
public:
// Static method to get the singleton instance
static PerformanceRegistrar& get_instance();

// Method to add a PerformanceManager instance to the registrar
void add_manager(std::shared_ptr<PerformanceManager> pm);

// Method to print records for all registered instances
void print_records();

void cleanup();

// Delete copy constructor and assignment operator to prevent cloning
PerformanceRegistrar(const PerformanceRegistrar&) = delete;
void operator=(const PerformanceRegistrar&) = delete;
};

//****************************** RANDOM NUMBER GENERATION *************************//

Expand Down Expand Up @@ -207,47 +246,6 @@ class RNGDispatcher{
map<std::thread::id, RNG*> pids;
};


class PerformanceManager{
private:
map<string, std::chrono::nanoseconds> task_duration;
map<string, std::chrono::time_point<std::chrono::high_resolution_clock>> task_start_time;
map<string, int> task_count;
map<string, int> task_scale; //!< This prevents overhead for many calls (e.g. evolution of neurons)

public:
string label;
PerformanceManager(string label);
void set_tasks(vector<string> task_names);
void set_scales(map<string, int> scales);
void set_label(string label);
void start_recording(string task);
void end_recording(string task);
void print_record();
string format_duration(std::chrono::nanoseconds duration);
};
class PerformanceRegistrar {
private:
std::vector<PerformanceManager*> instances;

PerformanceRegistrar(); // Private constructor to prevent instantiation
~PerformanceRegistrar();
public:
// Static method to get the singleton instance
static PerformanceRegistrar& get_instance();

// Method to add a PerformanceManager instance to the registrar
void add_manager(PerformanceManager* pm);

// Method to print records for all registered instances
void print_records();

// Delete copy constructor and assignment operator to prevent cloning
PerformanceRegistrar(const PerformanceRegistrar&) = delete;
void operator=(const PerformanceRegistrar&) = delete;
};


//******************************** UTILS FOR DYNAMICAL SYSTEMS *****************************//

/**
Expand Down
2 changes: 1 addition & 1 deletion quilt/core/include/multiscale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class MultiscaleNetwork{
unsigned int n_oscillators; //!< Number of oscillators in the multiscale network. Must not change after init.
unsigned int time_ratio; //!< Timescale separation. Must be defined by two evolution contextes.

PerformanceManager perf_mgr;
std::shared_ptr<PerformanceManager> perf_mgr;
private:
bool timescales_initialized;
EvolutionContext * evo_short;
Expand Down
4 changes: 2 additions & 2 deletions quilt/core/include/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Population{
void print_info();
void set_evolution_context(EvolutionContext * evo);

PerformanceManager perf_mgr;
std::shared_ptr<PerformanceManager> perf_mgr;
private:
BS::thread_pool thread_pool; //!< Thread pool for evolution and spike handling
vector<unsigned int> batch_starts, batch_ends;
Expand Down Expand Up @@ -172,7 +172,7 @@ class SpikingNetwork{
void evolve();
void run(EvolutionContext * evo, double time, int verbosity);

PerformanceManager perf_mgr;
std::shared_ptr<PerformanceManager> perf_mgr;
private:
EvolutionContext * evo;
bool evocontext_initialized;
Expand Down
2 changes: 1 addition & 1 deletion quilt/core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ void test_inhom_poisson(){

void test_multiscale_base(){
Logger &logger = get_global_logger();
logger.set_level(INFO);
// logger.set_level(INFO);

// Create a spiking network of just one population
SpikingNetwork spike_net = SpikingNetwork();
Expand Down
26 changes: 14 additions & 12 deletions quilt/core/multiscale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@ double Transducer::get_past(unsigned int /*axis*/, double time)
MultiscaleNetwork::MultiscaleNetwork(SpikingNetwork * spikenet, OscillatorNetwork * oscnet)
: spikenet(spikenet),
oscnet(oscnet),
timescales_initialized(false),
perf_mgr("multiscale network")
timescales_initialized(false)
{
n_populations = spikenet->populations.size();
n_oscillators = oscnet->oscillators.size();
stringstream ss;
ss << "MultiscaleNetwork has " << n_populations << " populations and " << n_oscillators << " oscillators";
n_populations = spikenet->populations.size();
n_oscillators = oscnet->oscillators.size();
stringstream ss;
ss << "MultiscaleNetwork has " << n_populations << " populations and " << n_oscillators << " oscillators";

perf_mgr = std::make_shared<PerformanceManager>("multiscale network");
perf_mgr->set_tasks({"evolve_spikenet", "evolve_oscnet"});
PerformanceRegistrar::get_instance().add_manager(perf_mgr);

perf_mgr.set_tasks({"evolve_spikenet", "evolve_oscnet"});
get_global_logger().log(INFO, ss.str());
get_global_logger().log(INFO, ss.str());
}

void MultiscaleNetwork::set_evolution_contextes(EvolutionContext * evo_short, EvolutionContext * evo_long){
Expand Down Expand Up @@ -297,16 +299,16 @@ void MultiscaleNetwork::run(double time, int verbosity){
// Evolve the short timescale until it catches up with
// the long timescale
// cout << "Doing one big step" << endl;
perf_mgr.start_recording("evolve_spikenet");
perf_mgr->start_recording("evolve_spikenet");
while (evo_short->now < evo_long->now){
// cout << "Doing one small step"<<endl;
spikenet->evolve();
}
perf_mgr.end_recording("evolve_spikenet");
perf_mgr->end_recording("evolve_spikenet");

perf_mgr.start_recording("evolve_oscnet");
perf_mgr->start_recording("evolve_oscnet");
oscnet->evolve();
perf_mgr.end_recording("evolve_oscnet");
perf_mgr->end_recording("evolve_oscnet");
++bar;
}

Expand Down
Loading

0 comments on commit 8aa6553

Please sign in to comment.