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
7157void 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+ }
0 commit comments