Skip to content

Commit

Permalink
fix: avoid nan in the output of time percentage (#4857)
Browse files Browse the repository at this point in the history
* fix: avoid nan in the output of time percentage

* [pre-commit.ci lite] apply automatic fixes

* add annotation

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
pxlxingliang and pre-commit-ci-lite[bot] committed Aug 3, 2024
1 parent 53aa387 commit 3228e5f
Showing 1 changed file with 19 additions and 9 deletions.
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

0 comments on commit 3228e5f

Please sign in to comment.