Skip to content

Commit e7e7629

Browse files
committed
1. fix bug of timer openmp thread-safe
1 parent bfb38c0 commit e7e7629

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

source/module_base/timer.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
#include<vector>
99

1010
#ifdef __MPI
11-
#include "mpi.h"
11+
#include <mpi.h>
12+
#endif
13+
14+
#ifdef _OPENMP
15+
#include <omp.h>
1216
#endif
1317

1418
namespace ModuleBase
@@ -67,7 +71,11 @@ void timer::tick(const std::string &class_name,const std::string &name)
6771
if (disabled)
6872
return;
6973

70-
Timer_One &timer_one = timer_pool[class_name][name];
74+
#ifdef _OPENMP
75+
if(!omp_get_thread_num())
76+
#endif
77+
{
78+
Timer_One &timer_one = timer_pool[class_name][name];
7179

7280
//----------------------------------------------------------
7381
// CALL MEMBER FUNCTION :
@@ -79,32 +87,33 @@ void timer::tick(const std::string &class_name,const std::string &name)
7987
// if start_flag == false, means it's the end of this counting,
8088
// so we add the time during this two 'time point' to the clock time storage.
8189
//----------------------------------------------------------
82-
if(timer_one.start_flag)
83-
{
90+
if(timer_one.start_flag)
91+
{
8492
#ifdef __MPI
85-
timer_one.cpu_start = MPI_Wtime();
93+
timer_one.cpu_start = MPI_Wtime();
8694
#else
87-
timer_one.cpu_start = cpu_time();
95+
timer_one.cpu_start = cpu_time();
8896
#endif
89-
++timer_one.calls;
90-
timer_one.start_flag = false;
91-
}
92-
else
93-
{
97+
++timer_one.calls;
98+
timer_one.start_flag = false;
99+
}
100+
else
101+
{
94102
#ifdef __MPI
95-
timer_one.cpu_second += MPI_Wtime() - timer_one.cpu_start;
103+
timer_one.cpu_second += MPI_Wtime() - timer_one.cpu_start;
96104
#else
97-
// if(class_name=="electrons"&&name=="c_bands")
98-
// {
99-
// cout<<"call times"<<timer_one.calls<<endl;
100-
// cout<<"electrons c_bands cost time:"<<endl;
101-
// cout<<cpu_time()<<"-"<<timer_one.cpu_start<<endl;
102-
// }
103-
104-
timer_one.cpu_second += (cpu_time() - timer_one.cpu_start);
105+
// if(class_name=="electrons"&&name=="c_bands")
106+
// {
107+
// cout<<"call times"<<timer_one.calls<<endl;
108+
// cout<<"electrons c_bands cost time:"<<endl;
109+
// cout<<cpu_time()<<"-"<<timer_one.cpu_start<<endl;
110+
// }
111+
112+
timer_one.cpu_second += (cpu_time() - timer_one.cpu_start);
105113
#endif
106-
timer_one.start_flag = true;
107-
}
114+
timer_one.start_flag = true;
115+
}
116+
} // end if(!omp_get_thread_num())
108117
}
109118

110119
long double timer::print_until_now(void)

0 commit comments

Comments
 (0)