source: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_barrier.hpp @ 1521

Last change on this file since 1521 was 1521, checked in by yushan, 3 years ago

save dev. TO DO : test with xios

File size: 1.6 KB
Line 
1#ifndef EP_BARRIER_HPP_INCLUDED
2#define EP_BARRIER_HPP_INCLUDED
3
4
5namespace ep_lib
6{
7
8  class ep_barrier
9  {
10    private:
11      int nbThreads;          //<The number of threads for this barrier
12      int currentNbThread;    //<The current number of threads waiting
13      bool sense;             //<Direct barrier feedback protection
14      omp_lock_t mutex;       //<To have an atomic int
15
16      ep_barrier(ep_barrier&){}
17      ep_barrier& operator=(ep_barrier&){return *this;}
18
19    public:
20      /** Constructor with the number of threads */
21      explicit ep_barrier(const int inNbThreads)
22          : nbThreads(inNbThreads), currentNbThread(0), sense(false) {
23          omp_init_lock( &mutex );
24      }
25
26      /** Destructor, release the omp lock */
27      ~ep_barrier(){
28          omp_destroy_lock( &mutex );
29      }
30
31      /** Perform a barrier */
32      void wait(){
33          const bool mySense = sense;
34          omp_set_lock( &mutex );
35          const int nbThreadsArrived = (++currentNbThread);
36          omp_unset_lock( &mutex );
37
38          if(nbThreadsArrived == nbThreads) {
39              currentNbThread = 0;
40              sense = !sense;
41              #pragma omp flush
42          }
43          else {
44              volatile const bool* const ptSense = &sense;
45              while( (*ptSense) == mySense){
46              }
47          }
48      }
49
50
51      /** Change the number of threads */
52      void setNbThreads(const int inNbThread){
53          omp_set_lock( &mutex );
54          nbThreads = inNbThread;
55          omp_unset_lock( &mutex );
56      }
57  };
58
59
60}
61
62
63
64#endif // EP_BARRIER_HPP_INCLUDED
65
Note: See TracBrowser for help on using the repository browser.