source: XIOS/dev/dev_trunk_omp/extern/src_ep_dev/ep_barrier.hpp @ 1646

Last change on this file since 1646 was 1646, checked in by yushan, 5 years ago

branch merged with trunk @1645. arch file (ep&mpi) added for ADA

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