Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WorkListHelpers.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_WORKLISTHELPERS_H
21 #define GALOIS_WORKLIST_WORKLISTHELPERS_H
22 
23 #include <boost/iterator/iterator_facade.hpp>
24 
25 #include "galois/config.h"
28 
29 namespace galois {
30 namespace worklists {
31 
32 template <typename T>
34  T* next;
35 
36 public:
37  ConExtListNode() : next(0) {}
38  T*& getNext() { return next; }
39  T* const& getNext() const { return next; }
40 };
41 
42 template <typename T>
44  : public boost::iterator_facade<ConExtIterator<T>, T,
45  boost::forward_traversal_tag> {
47  T* at;
48 
49  template <typename OtherTy>
50  bool equal(const ConExtIterator<OtherTy>& o) const {
51  return at == o.at;
52  }
53 
54  T& dereference() const { return *at; }
55  void increment() { at = at->getNext(); }
56 
57 public:
58  ConExtIterator() : at(0) {}
59 
60  template <typename OtherTy>
61  ConExtIterator(const ConExtIterator<OtherTy>& o) : at(o.at) {}
62 
63  explicit ConExtIterator(T* x) : at(x) {}
64 };
65 
66 template <typename T, bool concurrent>
68  // fixme: deal with concurrent
70 
71 public:
73 
74  bool empty() const { return !head.getValue(); }
75 
76  void push(T* C) {
77  T* oldhead(0);
78  do {
79  oldhead = head.getValue();
80  C->getNext() = oldhead;
81  } while (!head.CAS(oldhead, C));
82  }
83 
84  T* pop() {
85  // lock free Fast path (empty)
86  if (empty())
87  return 0;
88 
89  // Disable CAS
90  head.lock();
91  T* C = head.getValue();
92  if (!C) {
93  head.unlock();
94  return 0;
95  }
96  head.unlock_and_set(C->getNext());
97  C->getNext() = 0;
98  return C;
99  }
100 
102  typedef T value_type;
103  typedef T& reference;
106 
107  iterator begin() { return iterator(head.getValue()); }
108  iterator end() { return iterator(); }
109 
110  const_iterator begin() const { return const_iterator(head.getValue()); }
111  const_iterator end() const { return const_iterator(); }
112 };
113 
114 template <typename T, bool concurrent>
116  // Fixme: deal with concurrent
118  T* tail;
119 
120 public:
122 
123  ConExtLinkedQueue() : tail(0) {}
124 
125  bool empty() const { return !tail; }
126 
127  void push(T* C) {
128  head.lock();
129  // std::cerr << "in(" << C << ") ";
130  C->getNext() = 0;
131  if (tail) {
132  tail->getNext() = C;
133  tail = C;
134  head.unlock();
135  } else {
136  assert(!head.getValue());
137  tail = C;
138  head.unlock_and_set(C);
139  }
140  }
141 
142  T* pop() {
143  // lock free Fast path empty case
144  if (empty())
145  return 0;
146 
147  head.lock();
148  T* C = head.getValue();
149  if (!C) {
150  head.unlock();
151  return 0;
152  }
153  if (tail == C) {
154  tail = 0;
155  assert(!C->getNext());
156  head.unlock_and_clear();
157  } else {
158  head.unlock_and_set(C->getNext());
159  C->getNext() = 0;
160  }
161  return C;
162  }
163 
165  typedef T value_type;
166  typedef T& reference;
169 
170  iterator begin() { return iterator(head.getValue()); }
171  iterator end() { return iterator(); }
172 
173  const_iterator begin() const { return const_iterator(head.getValue()); }
174  const_iterator end() const { return const_iterator(); }
175 };
176 
177 template <typename T>
178 struct DummyIndexer : public std::unary_function<const T&, unsigned> {
179  unsigned operator()(const T&) { return 0; }
180 };
181 
182 } // namespace worklists
183 } // end namespace galois
184 
185 #endif
Definition: WorkListHelpers.h:33
Definition: WorkListHelpers.h:67
Definition: WorkListHelpers.h:43
T * pop()
Definition: WorkListHelpers.h:142
Definition: WorkListHelpers.h:115
PtrLock is a spinlock and a pointer.
Definition: PtrLock.h:42
T & reference
Definition: WorkListHelpers.h:103
friend class boost::iterator_core_access
Definition: WorkListHelpers.h:46
iterator end()
Definition: WorkListHelpers.h:171
iterator end()
Definition: WorkListHelpers.h:108
Definition: WorkListHelpers.h:178
bool empty() const
Definition: WorkListHelpers.h:125
T value_type
iterators not safe with concurrent modifications
Definition: WorkListHelpers.h:165
T * pop()
Definition: WorkListHelpers.h:84
const_iterator end() const
Definition: WorkListHelpers.h:174
ConExtIterator()
Definition: WorkListHelpers.h:58
const_iterator begin() const
Definition: WorkListHelpers.h:173
ConExtIterator(const ConExtIterator< OtherTy > &o)
Definition: WorkListHelpers.h:61
iterator begin()
Definition: WorkListHelpers.h:107
const_iterator begin() const
Definition: WorkListHelpers.h:110
ConExtListNode()
Definition: WorkListHelpers.h:37
ConExtIterator< const T > const_iterator
Definition: WorkListHelpers.h:105
ConExtIterator< T > iterator
Definition: WorkListHelpers.h:104
ConExtListNode< T > ListNode
Definition: WorkListHelpers.h:72
ConExtListNode< T > ListNode
Definition: WorkListHelpers.h:121
unsigned operator()(const T &)
Definition: WorkListHelpers.h:179
void push(T *C)
Definition: WorkListHelpers.h:76
T & reference
Definition: WorkListHelpers.h:166
const_iterator end() const
Definition: WorkListHelpers.h:111
T *const & getNext() const
Definition: WorkListHelpers.h:39
void push(T *C)
Definition: WorkListHelpers.h:127
ConExtIterator< T > iterator
Definition: WorkListHelpers.h:167
iterator begin()
Definition: WorkListHelpers.h:170
ConExtLinkedQueue()
Definition: WorkListHelpers.h:123
T value_type
iterators not safe with concurrent modifications
Definition: WorkListHelpers.h:102
ConExtIterator(T *x)
Definition: WorkListHelpers.h:63
T *& getNext()
Definition: WorkListHelpers.h:38
bool empty() const
Definition: WorkListHelpers.h:74
ConExtIterator< const T > const_iterator
Definition: WorkListHelpers.h:168