Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BulkSynchronous.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_WORKLIST_BULKSYNCHRONOUS_H
21 #define GALOIS_WORKLIST_BULKSYNCHRONOUS_H
22 
23 #include <atomic>
24 
25 #include "galois/config.h"
27 #include "galois/worklists/Chunk.h"
29 
30 namespace galois {
31 namespace worklists {
32 
38 template <class Container = PerSocketChunkFIFO<>, class T = int,
39  bool Concurrent = true>
40 class BulkSynchronous : private boost::noncopyable {
41 public:
42  template <bool _concurrent>
44 
45  template <typename _T>
46  using retype =
48 
49  template <typename _container>
51 
52 private:
53  typedef typename Container::template rethread<Concurrent> CTy;
54 
55  struct TLD {
56  unsigned round;
57  TLD() : round(0) {}
58  };
59 
60  CTy wls[2];
62  substrate::Barrier& barrier;
64  std::atomic<bool> isEmpty;
65 
66 public:
67  typedef T value_type;
68 
70  : barrier(runtime::getBarrier(runtime::activeThreads)), some(false),
71  isEmpty(false) {}
72 
73  void push(const value_type& val) {
74  wls[(tlds.getLocal()->round + 1) & 1].push(val);
75  }
76 
77  template <typename ItTy>
78  void push(ItTy b, ItTy e) {
79  while (b != e)
80  push(*b++);
81  }
82 
83  template <typename RangeTy>
84  void push_initial(const RangeTy& range) {
85  auto rp = range.local_pair();
86  push(rp.first, rp.second);
87  tlds.getLocal()->round = 1;
88  some.get() = true;
89  }
90 
92  TLD& tld = *tlds.getLocal();
94 
95  while (true) {
96  if (isEmpty)
97  return r; // empty
98 
99  r = wls[tld.round].pop();
100  if (r)
101  return r;
102 
103  barrier.wait();
104  if (substrate::ThreadPool::getTID() == 0) {
105  if (!some.get())
106  isEmpty = true;
107  some.get() = false;
108  }
109  tld.round = (tld.round + 1) & 1;
110  barrier.wait();
111 
112  r = wls[tld.round].pop();
113  if (r) {
114  some.get() = true;
115  return r;
116  }
117  }
118  }
119 };
120 GALOIS_WLCOMPILECHECK(BulkSynchronous)
121 
122 } // end namespace worklists
123 } // end namespace galois
124 
125 #endif
Definition: CacheLineStorage.h:32
void push_initial(const RangeTy &range)
Definition: BulkSynchronous.h:84
substrate::Barrier & getBarrier(unsigned activeThreads)
Have a pre-instantiated barrier available for use.
Definition: Substrate.cpp:24
galois::optional< value_type > pop()
Definition: BulkSynchronous.h:91
T * getLocal()
Definition: PerThreadStorage.h:128
Definition: Barrier.h:33
T & get()
Definition: CacheLineStorage.h:46
BulkSynchronous()
Definition: BulkSynchronous.h:69
Galois version of boost::optional.
Definition: optional.h:34
static unsigned getTID()
Definition: ThreadPool.h:204
unsigned int activeThreads
Definition: Threads.cpp:26
Bulk-synchronous scheduling.
Definition: BulkSynchronous.h:40
#define GALOIS_WLCOMPILECHECK(name)
Definition: WLCompileCheck.h:26
void push(ItTy b, ItTy e)
Definition: BulkSynchronous.h:78
T value_type
Definition: BulkSynchronous.h:67
void push(const value_type &val)
Definition: BulkSynchronous.h:73