Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Barrier.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_SUBSTRATE_BARRIER_H
21 #define GALOIS_SUBSTRATE_BARRIER_H
22 
23 #include <functional>
24 #include <memory>
25 
26 #include "galois/config.h"
27 #include "galois/gIO.h"
29 
30 namespace galois {
31 namespace substrate {
32 
33 class Barrier {
34 public:
35  virtual ~Barrier();
36 
37  // not safe if any thread is in wait
38  virtual void reinit(unsigned val) = 0;
39 
40  // Wait at this barrier
41  virtual void wait() = 0;
42 
43  // wait at this barrier
44  void operator()(void) { wait(); }
45 
46  // barrier type.
47  virtual const char* name() const = 0;
48 };
49 
53 Barrier& getBarrier(unsigned activeThreads);
54 
59 std::unique_ptr<Barrier> createPthreadBarrier(unsigned);
60 std::unique_ptr<Barrier> createMCSBarrier(unsigned);
61 std::unique_ptr<Barrier> createTopoBarrier(unsigned);
62 std::unique_ptr<Barrier> createCountingBarrier(unsigned);
63 std::unique_ptr<Barrier> createDisseminationBarrier(unsigned);
64 
72 std::unique_ptr<Barrier> createSimpleBarrier(unsigned int);
73 
74 namespace internal {
75 
76 template <typename _UNUSED = void>
77 struct BarrierInstance {
78  unsigned m_num_threads;
79  std::unique_ptr<Barrier> m_barrier;
80 
81  BarrierInstance(void) {
82  m_num_threads = getThreadPool().getMaxThreads();
83  m_barrier = createTopoBarrier(m_num_threads);
84  }
85 
86  Barrier& get(unsigned numT) {
87  GALOIS_ASSERT(numT > 0,
88  "substrate::getBarrier() number of threads must be > 0");
89 
90  numT = std::min(numT, getThreadPool().getMaxUsableThreads());
91  numT = std::max(numT, 1u);
92 
93  if (numT != m_num_threads) {
94  m_num_threads = numT;
95  m_barrier->reinit(numT);
96  }
97 
98  return *m_barrier;
99  }
100 };
101 
102 void setBarrierInstance(BarrierInstance<>* bi);
103 
104 } // end namespace internal
105 
106 } // end namespace substrate
107 } // end namespace galois
108 
109 #endif
ThreadPool & getThreadPool(void)
return a reference to system thread pool
Definition: ThreadPool.cpp:259
unsigned getMaxThreads() const
return the number of threads supported by the thread pool on the current machine
Definition: ThreadPool.h:178
std::unique_ptr< Barrier > createTopoBarrier(unsigned)
Definition: Barrier_Topo.cpp:126
std::unique_ptr< Barrier > createCountingBarrier(unsigned)
Definition: Barrier_Counting.cpp:67
Definition: Barrier.h:33
virtual const char * name() const =0
Barrier & getBarrier(unsigned activeThreads)
Return a reference to system barrier.
Definition: libgalois/src/Barrier.cpp:38
std::unique_ptr< Barrier > createSimpleBarrier(unsigned int)
Creates a new simple barrier.
Definition: Barrier_Simple.cpp:85
virtual void reinit(unsigned val)=0
#define GALOIS_ASSERT(cond,...)
Like assert but unconditionally executed.
Definition: gIO.h:102
const Ty max(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:40
unsigned int activeThreads
Definition: Threads.cpp:26
std::unique_ptr< Barrier > createPthreadBarrier(unsigned)
Create specific types of barriers.
Definition: Barrier_Pthread.cpp:85
const Ty min(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:70
std::unique_ptr< Barrier > createMCSBarrier(unsigned)
Definition: Barrier_MCS.cpp:106
virtual ~Barrier()
Definition: libgalois/src/Barrier.cpp:23
std::unique_ptr< Barrier > createDisseminationBarrier(unsigned)
Definition: Barrier_Dissemination.cpp:100
void operator()(void)
Definition: Barrier.h:44