Skip to content

Commit

Permalink
Add diagnostics output
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewcarbone committed Nov 22, 2023
1 parent 8bd71b2 commit 78fa80c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 73 deletions.
12 changes: 3 additions & 9 deletions inc/main_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ namespace main_utils
*/
void update_parameters_(utils::SimulationParameters* p);

/**
* @brief Prints information about the current processor that is running
* the job on the provided rank
*
* @param mpi_rank
* @param mpi_world_size
*/
// void print_processor_information(const int mpi_rank, const int mpi_world_size);

void save_and_log_config(const utils::SimulationParameters p);

/**
* @brief [brief description]
* @details [long description]
*
* @param p [description]
*/
void initialize_grids_and_config(const utils::SimulationParameters p);
void initialize_grids_and_directories(const utils::SimulationParameters p);


/**
Expand Down
13 changes: 3 additions & 10 deletions inc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,11 @@ struct SimulationStatistics
*
* @param inp [description]
*/
void log_json(const json inp);

/**
* @brief [brief description]
* @details [long description]
*
* @param p [description]
*/
void log_parameters(const SimulationParameters p);
void print_json(const json jrep);

json simulation_parameters_to_json(const utils::SimulationParameters p);

json parameters_to_json(const SimulationParameters p);
void json_to_file(const json jrep, const std::string& filename);

/**
* @brief [brief description]
Expand Down
6 changes: 4 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ int main(int argc, char *argv[])
// PARSER ----------------------------------------------------------------

update_parameters_(&p);
initialize_grids_and_config(p);
save_and_log_config(p);
initialize_grids_and_directories(p);
auto_determine_dynamics_(&p);
execute_process_pool(p);

execute_process_pool(p); // <----- Main simulation

MPI_Finalize();
}
51 changes: 34 additions & 17 deletions src/main_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ double get_sim_time(utils::SimulationParameters p, const std::string dynamics)
return utils::get_time_delta(t_start) / simulation_clock;
}

void master(const size_t min_index_inclusive, const size_t max_index_exclusive)
json master(const size_t min_index_inclusive, const size_t max_index_exclusive)
{

const std::string dt_start = utils::get_datetime();

int mpi_world_size;
MPI_Comm_size(MPI_COMM_WORLD, &mpi_world_size);

Expand Down Expand Up @@ -141,11 +143,8 @@ void master(const size_t min_index_inclusive, const size_t max_index_exclusive)

// We're done on the master process, send termination signals to all of
// the workers

std::cout << "!!! DONE !!!" << std::endl;

std::vector<int> jobs_finished_per_task;

for (size_t cc=0; cc<mpi_world_size - 1; cc++)
{

Expand All @@ -162,12 +161,16 @@ void master(const size_t min_index_inclusive, const size_t max_index_exclusive)
jobs_finished_per_task.push_back(tmp_n_jobs);
}

std::cout << "Diagnostic (jobs finished per task): ";
for (const auto& v : jobs_finished_per_task)
{
std::cout << v << " ";
}
std::cout << std::endl;
const std::string dt_end = utils::get_datetime();
const double duration = utils::get_time_delta(t_start);

json diagnostics;
diagnostics["elapsed"] = duration;
diagnostics["dt_start"] = dt_start;
diagnostics["dt_end"] = dt_end;
diagnostics["jobs_finished_per_task"] = jobs_finished_per_task;

return diagnostics;

}

Expand Down Expand Up @@ -251,26 +254,39 @@ void update_parameters_(utils::SimulationParameters* p)

// handle the manual seeding
if (p->seed > 0){p->use_manual_seed = true;}

MPI_Barrier(MPI_COMM_WORLD);
}

void initialize_grids_and_config(const utils::SimulationParameters p)
void save_and_log_config(const utils::SimulationParameters p)
{
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);

if (mpi_rank == MASTER)
{
json jrep = utils::simulation_parameters_to_json(p);
utils::print_json(jrep);
utils::json_to_file(jrep, "config.json");
}

fflush(stdout);
MPI_Barrier(MPI_COMM_WORLD);
}

void initialize_grids_and_directories(const utils::SimulationParameters p)
{

int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);

if (mpi_rank == MASTER)
{
utils::log_parameters(p);
utils::make_directories();
utils::make_energy_grid_logspace(p.log10_N_timesteps, p.grid_size);
utils::make_pi_grids(p.log10_N_timesteps, p.dw, p.grid_size);
json j = utils::parameters_to_json(p);
std::ofstream o("config.json");
o << std::setw(4) << j << std::endl;
}

fflush(stdout);
MPI_Barrier(MPI_COMM_WORLD);
}

Expand Down Expand Up @@ -382,7 +398,8 @@ void execute_process_pool(const utils::SimulationParameters params)
{
// TODO add some logic for checkpoint-restart here
std::cout << "Running " << mpi_world_size << " procs. " << mpi_world_size - 1 << " compute, 1 controller " << std::endl;
master(0, params.n_tracers);
const json diagnostics = master(0, params.n_tracers);
utils::json_to_file(diagnostics, "out.json");
}
else
{
Expand Down
53 changes: 18 additions & 35 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,44 +117,14 @@ bool _key_exists(const json inp, const std::string key)
return false;
}

void log_json(const json inp)
void print_json(const json jrep)
{
printf("------------------- config.json loaded -------------------\n");
for (auto it=inp.begin(); it!=inp.end(); ++it)
{
std::cout << it.key() << " : " << it.value() << std::endl;
}
}

void log_parameters(const utils::SimulationParameters p)
{

printf("--------------- simulation parameters set ----------------\n");
printf("log10_N_timesteps \t\t\t= %i\n", p.log10_N_timesteps);
printf("N_timesteps \t\t\t= %lli\n", p.N_timesteps);
printf("N_spins \t\t\t= %i\n", p.N_spins);
printf("beta \t\t\t= %.05f\n", p.beta);
printf("beta_critical \t\t\t= %.05f\n", p.beta_critical);
printf("landscape \t\t\t= %s\n", p.landscape.c_str());
printf("dynamics \t\t\t= %s\n", p.dynamics.c_str());
printf("memory \t\t\t= %lli\n", p.memory);
printf("energetic threshold \t\t\t= %.03e\n", p.energetic_threshold);
printf("entropic attractor \t\t\t= %.03e\n", p.entropic_attractor);
printf("valid_entropic_attractor \t\t\t= %i\n", p.valid_entropic_attractor);
printf("grid_size \t\t\t= %i\n", p.grid_size);
printf("dw \t\t\t= %.05f\n", p.dw);
printf("n_tracers \t\t\t= %i\n", p.n_tracers);
if (p.use_manual_seed)
{
printf("manual seed \t\t\t= %i\n", p.seed);
}
printf("calculate_IS \t\t\t= %i\n", p.calculate_inherent_structure_observables);
printf("PRECISON \t\t\t= %i\n", PRECISON);
printf("----------------------------------------------------------\n");
std::cout << "hdspin - simulation parameters" << std::endl;
std::cout << std::setw(4) << jrep << std::endl;
}


json parameters_to_json(const utils::SimulationParameters p)
json simulation_parameters_to_json(const utils::SimulationParameters p)
{
json j = {
{"log10_N_timesteps", p.log10_N_timesteps},
Expand All @@ -173,13 +143,26 @@ json parameters_to_json(const utils::SimulationParameters p)
{"n_tracers", p.n_tracers},
{"use_manual_seed", p.use_manual_seed},
{"seed", p.seed},
{"calculate_IS", p.calculate_inherent_structure_observables},
{"calculate_inherent_structure_observables", p.calculate_inherent_structure_observables},
{"PRECISON", PRECISON}
};

return j;
}

void json_to_file(const json jrep, const std::string& filename)
{
std::ofstream outputFile(filename);
if (outputFile.is_open())
{
outputFile << std::setw(4) << jrep;
outputFile.close();
} else {
std::cerr << "Unable to open file: " << filename << std::endl;
}
}


utils::FileNames get_filenames(const unsigned int ii)
{
std::string ii_str = std::to_string(ii);
Expand Down

0 comments on commit 78fa80c

Please # to comment.