Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UnionFind.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_UNIONFIND_H
21 #define GALOIS_UNIONFIND_H
22 
23 #include <atomic>
24 
25 #include "galois/config.h"
26 
27 namespace galois {
32 template <typename T>
34  T* findImpl() const {
35  if (isRep())
36  return m_component.load(std::memory_order_relaxed);
37 
38  T* rep = m_component;
39  while (rep->m_component != rep) {
40  T* next = rep->m_component.load(std::memory_order_relaxed);
41  rep = next;
42  }
43  return rep;
44  }
45 
46 protected:
47  std::atomic<T*> m_component;
48 
49  UnionFindNode(T* s) : m_component(s) {}
50 
51 public:
53 
54  bool isRep() const {
55  return m_component.load(std::memory_order_relaxed) == this;
56  }
57 
58  T* get() const { return m_component.load(std::memory_order_relaxed); }
59 
60  const T* find() const { return findImpl(); }
61 
62  T* find() { return findImpl(); }
63 
66  void compress() {
67  if (isRep())
68  return;
69 
70  // my current component
71  T* rep = m_component;
72 
73  // loop until rep == itself; i.e. get root
74  while (rep->m_component.load(std::memory_order_relaxed) != rep) {
75  // get next parent
76  T* next = rep->m_component.load(std::memory_order_relaxed);
77  rep = next;
78  }
79 
80  // at this point rep is the parent: save as my parent
81  m_component.store(rep, std::memory_order_relaxed);
82  }
83 
85  // Basic outline of race in synchronous path compression is that two path
86  // compressions along two different paths to the root can create a cycle
87  // in the union-find tree. Prevent that from happening by compressing
88  // incrementally.
89  if (isRep())
90  return m_component.load(std::memory_order_relaxed);
91 
92  T* rep = m_component;
93  T* prev = 0;
94  while (rep->m_component.load(std::memory_order_relaxed) != rep) {
95  T* next = rep->m_component.load(std::memory_order_relaxed);
96 
97  if (prev && prev->m_component.load(std::memory_order_relaxed) == rep) {
98  prev->m_component.store(next, std::memory_order_relaxed);
99  }
100  prev = rep;
101  rep = next;
102  }
103 
104  return rep;
105  }
106 
108  T* merge(T* b) {
109  T* a = m_component.load(std::memory_order_relaxed);
110  while (true) {
111  a = a->findAndCompress();
112  b = b->findAndCompress();
113  if (a == b)
114  return 0;
115  // Avoid cycles by directing edges consistently
116  if (a < b)
117  std::swap(a, b);
118  if (a->m_component.compare_exchange_strong(a, b)) {
119  return b;
120  }
121  }
122  }
123 };
124 } // namespace galois
125 #endif
Intrusive union-find implementation.
Definition: UnionFind.h:33
T * findAndCompress()
Definition: UnionFind.h:84
UnionFindNode(T *s)
Definition: UnionFind.h:49
UnionFindNode< T > SuperTy
Definition: UnionFind.h:52
T * merge(T *b)
Lock-free merge. Returns if merge was done.
Definition: UnionFind.h:108
const T * find() const
Definition: UnionFind.h:60
T * find()
Definition: UnionFind.h:62
void compress()
Compress ONLY node to point directly to the root of the tree; nodes on path are not altered...
Definition: UnionFind.h:66
std::atomic< T * > m_component
Definition: UnionFind.h:47
bool isRep() const
Definition: UnionFind.h:54