Skip to content

Commit 8ae9ea0

Browse files
mohanchenFisherd99
authored andcommitted
Update timer, only print out those timers that consume more than 1% of total time (deepmodeling#5984)
* change namespace and timer of check_atomic_stru * update stress timer and some formats * update timer * update timer * update wrong timer * update timer in symmetry * update pot_local * update timer * only print out timer that is larger than 1 precent of total time * update formats in rt-TDDFT * fix unit tests
1 parent 5badfcf commit 8ae9ea0

38 files changed

+629
-577
lines changed

source/driver_run.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void Driver::driver_run()
4646
PARAM.inp.fixed_axes);
4747

4848
ucell.setup_cell(PARAM.globalv.global_in_stru, GlobalV::ofs_running);
49-
Check_Atomic_Stru::check_atomic_stru(ucell, PARAM.inp.min_dist_coef);
49+
unitcell::check_atomic_stru(ucell, PARAM.inp.min_dist_coef);
5050

5151
//! 2: initialize the ESolver (depends on a set-up ucell after `setup_cell`)
5252
ModuleESolver::ESolver* p_esolver = ModuleESolver::init_esolver(PARAM.inp, ucell);

source/module_base/mathzone_add1.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void Mathzone_Add1::Cubic_Spline_Interpolation
138138
double * const dy
139139
)
140140
{
141-
ModuleBase::timer::tick("Mathzone_Add1","Cubic_Spline_Interpolation");
141+
ModuleBase::timer::tick("Mathzone","cubic_spline");
142142

143143
#ifdef _OPENMP
144144
#pragma omp parallel for schedule(static)
@@ -175,7 +175,7 @@ void Mathzone_Add1::Cubic_Spline_Interpolation
175175
//ddy[m] = ddy_tmp;
176176
}
177177

178-
ModuleBase::timer::tick("Mathzone_Add1","Cubic_Spline_Interpolation");
178+
ModuleBase::timer::tick("Mathzone","cubic_spline");
179179
}
180180

181181
/// Interpolation for Numerical Orbitals

source/module_base/module_mixing/broyden_mixing.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,20 @@ template void Broyden_Mixing::tem_cal_coef(
104104
template <class FPTYPE>
105105
void Broyden_Mixing::tem_cal_coef(const Mixing_Data& mdata, std::function<double(FPTYPE*, FPTYPE*)> inner_product)
106106
{
107-
ModuleBase::TITLE("Charge_Mixing", "Simplified_Broyden_mixing");
108-
ModuleBase::timer::tick("Charge", "Broyden_mixing");
109-
if (address != &mdata && address != nullptr)
110-
ModuleBase::WARNING_QUIT(
111-
"Broyden_mixing",
112-
"One Broyden_Mixing object can only bind one Mixing_Data object to calculate coefficients");
113-
const int length = mdata.length;
107+
ModuleBase::TITLE("Broyden_Mixing", "Simplified_Broyden_mixing");
108+
ModuleBase::timer::tick("Broyden_Mixing", "tem_cal_coef");
109+
110+
if (address != &mdata && address != nullptr)
111+
{
112+
ModuleBase::WARNING_QUIT(
113+
"Broyden_mixing",
114+
"One Broyden_Mixing object can only bind one Mixing_Data object to calculate coefficients");
115+
}
116+
117+
const int length = mdata.length;
114118
FPTYPE* FP_dF = static_cast<FPTYPE*>(dF);
115119
FPTYPE* FP_F = static_cast<FPTYPE*>(F);
120+
116121
if (ndim_cal_dF > 0)
117122
{
118123
ModuleBase::matrix beta_tmp(ndim_cal_dF, ndim_cal_dF);
@@ -140,7 +145,7 @@ void Broyden_Mixing::tem_cal_coef(const Mixing_Data& mdata, std::function<double
140145
double* work = new double[ndim_cal_dF]; // workspace
141146
int* iwork = new int[ndim_cal_dF]; // ipiv
142147
char uu = 'U';
143-
int info;
148+
int info = 0;
144149
int m = 1;
145150
// gamma means the coeficients for mixing
146151
// but now gamma store <dFi|Fm>, namely c
@@ -150,10 +155,25 @@ void Broyden_Mixing::tem_cal_coef(const Mixing_Data& mdata, std::function<double
150155
FPTYPE* dFi = FP_dF + i * length;
151156
gamma[i] = inner_product(dFi, FP_F);
152157
}
153-
// solve aG = c
154-
dsysv_(&uu, &ndim_cal_dF, &m, beta_tmp.c, &ndim_cal_dF, iwork, gamma.data(), &ndim_cal_dF, work, &ndim_cal_dF, &info);
155-
if (info != 0)
156-
ModuleBase::WARNING_QUIT("Charge_Mixing", "Error when DSYSV.");
158+
159+
// solve aG = c
160+
dsysv_(&uu,
161+
&ndim_cal_dF,
162+
&m,
163+
beta_tmp.c,
164+
&ndim_cal_dF,
165+
iwork,
166+
gamma.data(),
167+
&ndim_cal_dF,
168+
work,
169+
&ndim_cal_dF,
170+
&info);
171+
172+
if (info != 0)
173+
{
174+
ModuleBase::WARNING_QUIT("Charge_Mixing", "Error when DSYSV.");
175+
}
176+
157177
// after solving, gamma store the coeficients for mixing
158178
coef[mdata.start] = 1 + gamma[dFindex_move(0)];
159179
for (int i = 1; i < ndim_cal_dF; ++i)
@@ -178,6 +198,6 @@ void Broyden_Mixing::tem_cal_coef(const Mixing_Data& mdata, std::function<double
178198
{
179199
dFnext[i] = FP_F[i];
180200
}
181-
ModuleBase::timer::tick("Charge", "Broyden_mixing");
201+
ModuleBase::timer::tick("Broyden_Mixing", "tem_cal_coef");
182202
};
183203
} // namespace Base_Mixing

source/module_base/timer.cpp

Lines changed: 43 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
//==========================================================
2-
// AUTHOR : fangwei , mohan
3-
// DATE : 2008-11-06,
4-
// UPDATE : Peize Lin at 2019-11-21
5-
//==========================================================
61
#include "timer.h"
72

83
#include <cmath>
@@ -53,19 +48,10 @@ double timer::cpu_time()
5348
// only first call can let t0 = 0,clock begin
5449
// when enter this function second time , t0 > 0
5550
//----------------------------------------------------------
56-
// static clock_t t0 = clock();
57-
// const clock_t t1 = clock() - t0;
58-
// return (t1<0) ? 0 : (double)t1/CLOCKS_PER_SEC;
59-
60-
// static time_t t0 = time(NULL);
61-
// const time_t t1 = time(NULL);
62-
// double res = difftime(t1, t0);
63-
// return (res<0) ? 0 : res;
6451
static auto t1 = std::chrono::system_clock::now();
6552
const auto t2 = std::chrono::system_clock::now();
6653
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1);
6754
return double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
68-
// mohan add, abandon the cross point time 2^32 ~ -2^32 .
6955
}
7056

7157
void timer::tick(const std::string &class_name,const std::string &name)
@@ -118,13 +104,6 @@ void timer::tick(const std::string &class_name,const std::string &name)
118104
timer_one.cpu_second += MPI_Wtime() - timer_one.cpu_start;
119105
}
120106
#else
121-
// if(class_name=="electrons"&&name=="c_bands")
122-
// {
123-
// cout<<"call times"<<timer_one.calls<<endl;
124-
// cout<<"electrons c_bands cost time:"<<endl;
125-
// cout<<cpu_time()<<"-"<<timer_one.cpu_start<<endl;
126-
// }
127-
128107
timer_one.cpu_second += (cpu_time() - timer_one.cpu_start);
129108
#endif
130109
timer_one.start_flag = true;
@@ -201,7 +180,9 @@ void timer::write_to_json(std::string file_name)
201180
order_a ++;
202181
// if calss_name == "", it means total time, so we skip it
203182
if(timer_pool_A.first == "")
183+
{
204184
continue;
185+
}
205186
int order_b = 0;
206187
const std::string class_name = timer_pool_A.first;
207188
ofs << indent << indent << "{\n";
@@ -214,20 +195,32 @@ void timer::write_to_json(std::string file_name)
214195
const Timer_One timer_one = timer_pool_B.second;
215196
ofs << indent << indent << indent << indent << "{\n";
216197
ofs << indent << indent << indent << indent << "\"name\": \"" << name << "\",\n";
217-
ofs << indent << indent << indent << indent << "\"cpu_second\": " << std::setprecision(15) << timer_one.cpu_second << ",\n";
198+
ofs << indent << indent << indent << indent << "\"cpu_second\": "
199+
<< std::setprecision(15) << timer_one.cpu_second << ",\n";
218200
ofs << indent << indent << indent << indent << "\"calls\": " << timer_one.calls << ",\n";
219-
ofs << indent << indent << indent << indent << "\"cpu_second_per_call\": " << double_to_string(timer_one.cpu_second/timer_one.calls) << ",\n";
220-
ofs << indent << indent << indent << indent << "\"cpu_second_per_total\": " << double_to_string(timer_one.cpu_second/timer_pool[""]["total"].cpu_second) << "\n";
201+
ofs << indent << indent << indent << indent << "\"cpu_second_per_call\": "
202+
<< double_to_string(timer_one.cpu_second/timer_one.calls) << ",\n";
203+
ofs << indent << indent << indent << indent << "\"cpu_second_per_total\": "
204+
<< double_to_string(timer_one.cpu_second/timer_pool[""]["total"].cpu_second) << "\n";
205+
221206
if (order_b == timer_pool_A.second.size())
207+
{
222208
ofs << indent << indent << indent << indent << "}\n";
209+
}
223210
else
211+
{
224212
ofs << indent << indent << indent << indent << "},\n";
213+
}
225214
}
226215
ofs << indent << indent << indent << "]\n";
227216
if (order_a == timer_pool.size())
217+
{
228218
ofs << indent << indent << "}\n";
219+
}
229220
else
221+
{
230222
ofs << indent << indent << "},\n";
223+
}
231224
}
232225
ofs << indent << "]\n";
233226
ofs << "}\n";
@@ -248,9 +241,12 @@ void timer::print_all(std::ofstream &ofs)
248241
const std::string name = timer_pool_B.first;
249242
const Timer_One timer_one = timer_pool_B.second;
250243
if(timer_pool_order.size() < timer_one.order+1)
244+
{
251245
timer_pool_order.resize(timer_one.order+1);
246+
}
252247
//timer_pool_order[timer_one.order] = {{class_name, name}, timer_one}; //qianrui change it to make it compatible with old compiler version
253-
timer_pool_order[timer_one.order] = std::pair<std::pair<std::string,std::string>, Timer_One> {std::pair<std::string,std::string >{class_name,name}, timer_one};
248+
timer_pool_order[timer_one.order] = std::pair<std::pair<std::string,std::string>, Timer_One> {
249+
std::pair<std::string,std::string >{class_name,name}, timer_one};
254250
}
255251
}
256252
std::vector<std::string> class_names;
@@ -266,25 +262,42 @@ void timer::print_all(std::ofstream &ofs)
266262
const Timer_One &timer_one = timer_pool_order_A.second;
267263

268264
if(timer_one.cpu_second < 0)
265+
{
269266
continue;
267+
}
268+
269+
// only print out timers that are larger than 1%
270+
// mohan add 2025-03-09
271+
const double percentage_thr = 1.0;
272+
const double percentage = timer_one.cpu_second / timer_pool_order[0].second.cpu_second * 100;
273+
if(percentage<percentage_thr)
274+
{
275+
continue;
276+
}
277+
270278
class_names.push_back(class_name);
271279
names.push_back(name);
272280
times.push_back(timer_one.cpu_second);
273281
calls.push_back(timer_one.calls);
274282
avgs.push_back(timer_one.cpu_second/timer_one.calls);
275283

284+
276285
// if the total time is too small, we do not calculate the percentage
277-
if (timer_pool_order[0].second.cpu_second < 1e-9) {
278-
pers.push_back(0);
279-
} else {
280-
pers.push_back(timer_one.cpu_second / timer_pool_order[0].second.cpu_second * 100);
286+
if (timer_pool_order[0].second.cpu_second < 1e-9)
287+
{
288+
pers.push_back(0);
289+
}
290+
else
291+
{
292+
pers.push_back(percentage);
281293
}
282294
}
283295
assert(class_names.size() == names.size());
284296
assert(class_names.size() == times.size());
285297
assert(class_names.size() == calls.size());
286298
assert(class_names.size() == avgs.size());
287299
assert(class_names.size() == pers.size());
300+
288301
std::vector<std::string> titles = {"CLASS_NAME", "NAME", "TIME/s", "CALLS", "AVG/s", "PER/%"};
289302
std::vector<std::string> formats = {"%-10s", "%-10s", "%6.2f", "%8d", "%6.2f", "%6.2f"};
290303
FmtTable time_statistics(titles, pers.size(), formats, {FmtTable::Align::LEFT, FmtTable::Align::CENTER});
@@ -293,107 +306,6 @@ void timer::print_all(std::ofstream &ofs)
293306
std::cout<<table<<std::endl;
294307
ofs<<table<<std::endl;
295308
write_to_json("time.json");
296-
}
297-
}
298309

299-
/*
300-
void timer::print_all(std::ofstream &ofs)
301-
{
302-
// std::cout<<"\n timer::print_all()"<<std::endl;
303-
const double small = 0.1; // cpu = 10^6
304-
// if want to print > 1s , set small = 10^6
305-
306-
std::cout << std::setprecision(2);
307-
308-
// prepare
309-
bool *print_flag = new bool[n_clock];
310-
for(int i=0; i<n_clock; i++)
311-
{
312-
print_flag[i] = false;
313-
}
314-
315-
int type = 1; // 2:calls 1:total_time
316-
bool non_reorder = 1;
317-
318-
std::cout<<"\n |CLASS_NAME---------|NAME---------------|TIME(Sec)-----|CALLS----|AVG------|PER%-------" << std::endl;
319-
ofs <<"\n\n\n\n |CLASS_NAME---------|NAME---------------|TIME(Sec)-----|CALLS----|AVG------|PER%-------" << std::endl;
320-
ofs << std::setprecision(3);
321-
for (int i=0; i<n_clock; i++)
322-
{
323-
int k = 0;
324-
double tmp = -1.0;
325-
326-
if(non_reorder)
327-
{
328-
k = i;
329-
}
330-
else
331-
{
332-
// search in all clocks
333-
for(int j=0; j<n_clock; j++)
334-
{
335-
if(print_flag[j])
336-
{
337-
continue;
338-
}
339-
if(type==1)
340-
{
341-
if(tmp < cpu_second[j])
342-
{
343-
k = j;
344-
tmp = cpu_second[j];
345-
}
346-
}
347-
else if(type==2)
348-
{
349-
if(tmp < calls[j])
350-
{
351-
k = j;
352-
tmp = calls[j];
353-
}
354-
}
355-
}
356-
}
357-
print_flag[k]=true;
358-
359-
if ((cpu_second[k] >= 0 && cpu_second[k] < small) ||
360-
(cpu_second[k] <= 0 && cpu_second[k] > -small))
361-
{
362-
continue;
363-
}
364-
365-
if( level[k] > 'X' ) continue;
366-
367-
368-
const long double spend_time = cpu_second[k];
369-
const double average_spend_time = spend_time/calls[k];
370-
371-
372-
ofs << " "
373-
<< std::setw(2) << level[k]
374-
<< std::setw(20) << class_name[k]
375-
<< std::setw(20) << name[k]
376-
<< std::setw(15) << spend_time
377-
<< std::setw(10) << calls[k]
378-
<< std::setw(10) << std::setprecision(2) << average_spend_time
379-
<< std::setw(10) << spend_time / cpu_second[0] * 100 << "%" << std::endl;
380-
381-
382-
std::cout << std::resetiosflags(ios::scientific);
383-
384-
std::cout << " "
385-
<< std::setw(2) << level[k]
386-
<< std::setw(20) << class_name[k]
387-
<< std::setw(20) << name[k]
388-
<< std::setw(15) << spend_time
389-
<< std::setw(10) << calls[k]
390-
<< std::setw(10) << std::setprecision(2) << average_spend_time
391-
<< std::setw(10) << spend_time / cpu_second[0] * 100 << "%" << std::endl;
392-
393-
}
394-
std::cout<<" ----------------------------------------------------------------------------------------"<<std::endl;
395-
ofs <<" ----------------------------------------------------------------------------------------"<<std::endl;
396-
delete[] print_flag;
397-
return;
398310
}
399-
*/
311+
}

source/module_cell/check_atomic_stru.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
#include "module_base/element_covalent_radius.h"
44
#include "module_base/timer.h"
55

6-
void Check_Atomic_Stru::check_atomic_stru(UnitCell& ucell, const double& factor)
6+
namespace unitcell
77
{
8+
9+
void check_atomic_stru(UnitCell& ucell, const double& factor)
10+
{
11+
ModuleBase::timer::tick("unitcell", "check_atomic_stru");
812
// First we calculate all bond length in the structure,
913
// and compare with the covalent_bond_length,
1014
// if there has bond length is shorter than covalent_bond_length * factor,
@@ -17,7 +21,6 @@ void Check_Atomic_Stru::check_atomic_stru(UnitCell& ucell, const double& factor)
1721

1822
if (GlobalV::MY_RANK == 0)
1923
{
20-
ModuleBase::timer::tick("Check_Atomic_Stru", "Check_Atomic_Stru");
2124

2225
const int ntype = ucell.ntype;
2326
const double lat0 = ucell.lat0;
@@ -138,7 +141,6 @@ void Check_Atomic_Stru::check_atomic_stru(UnitCell& ucell, const double& factor)
138141
} // it2
139142
} // iat
140143
}
141-
ModuleBase::timer::tick("Check_Atomic_Stru", "Check_Atomic_Stru");
142144
}
143145
if (!all_pass || !no_warning)
144146
{
@@ -165,4 +167,8 @@ void Check_Atomic_Stru::check_atomic_stru(UnitCell& ucell, const double& factor)
165167
ModuleBase::WARNING_QUIT("Input", "The structure is unreasonable!");
166168
}
167169
}
170+
171+
ModuleBase::timer::tick("unitcell", "check_atomic_stru");
172+
}
173+
168174
}

0 commit comments

Comments
 (0)