Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid nan in the output of time percentage #4857

Merged
merged 4 commits into from
Aug 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions source/module_base/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//==========================================================
#include "timer.h"

#include <math.h>
#include <cmath>

#ifdef __MPI
#include <mpi.h>
Expand All @@ -32,20 +32,21 @@ std::map<std::string,std::map<std::string,timer::Timer_One>> timer::timer_pool;
void timer::finish(std::ofstream &ofs,const bool print_flag)
{
timer::tick("","total");
if(print_flag)
if(print_flag) {
print_all( ofs );
}
}

//----------------------------------------------------------
//
//----------------------------------------------------------
void timer::start(void)
void timer::start()
{
// first init ,then we can use tick
timer::tick("","total");
}

double timer::cpu_time(void)
double timer::cpu_time()
{
//----------------------------------------------------------
// EXPLAIN : here static is important !!
Expand All @@ -72,8 +73,9 @@ void timer::tick(const std::string &class_name,const std::string &name)
//----------------------------------------------------------
// EXPLAIN : if timer is disabled , return
//----------------------------------------------------------
if (disabled)
if (disabled) {
return;
}

#ifdef _OPENMP
if(!omp_get_thread_num())
Expand Down Expand Up @@ -130,7 +132,7 @@ void timer::tick(const std::string &class_name,const std::string &name)
} // end if(!omp_get_thread_num())
}

long double timer::print_until_now(void)
long double timer::print_until_now()
{
// stop the clock
timer::tick("","total");
Expand All @@ -146,12 +148,14 @@ void timer::write_to_json(std::string file_name)
// if mpi is not initialized, we do not run this function
int is_initialized = 0;
MPI_Initialized(&is_initialized);
if (!is_initialized)
if (!is_initialized) {
return;
}
int my_rank = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (my_rank != 0)
if (my_rank != 0) {
return;
}
#endif

// check if a double is inf, if so, return "null", else return a string of the input double
Expand Down Expand Up @@ -268,7 +272,13 @@ void timer::print_all(std::ofstream &ofs)
times.push_back(timer_one.cpu_second);
calls.push_back(timer_one.calls);
avgs.push_back(timer_one.cpu_second/timer_one.calls);
pers.push_back(timer_one.cpu_second / timer_pool_order[0].second.cpu_second * 100);

// if the total time is too small, we do not calculate the percentage
if (timer_pool_order[0].second.cpu_second < 1e-9) {
pers.push_back(0);
} else {
pers.push_back(timer_one.cpu_second / timer_pool_order[0].second.cpu_second * 100);
}
}
assert(class_names.size() == names.size());
assert(class_names.size() == times.size());
Expand Down
Loading