TTK
Loading...
Searching...
No Matches
FTRAtomicUF.h
Go to the documentation of this file.
1
11
12#pragma once
13
14// c++ includes
15#include <memory>
16#include <vector>
17
18namespace ttk {
19 namespace ftr {
20
21 class Propagation;
22
23 class AtomicUF {
24 private:
25 unsigned rank_;
26 AtomicUF *parent_;
27 Propagation *prop_;
28
29 public:
30 inline explicit AtomicUF(Propagation *const p) : rank_(0), prop_{p} {
31 parent_ = this;
32 }
33
34 // heavy recursif
35 inline AtomicUF *find() {
36 if(parent_ == this)
37 return this;
38 else {
39 decltype(parent_) tmp = parent_->find();
40#ifdef TTK_ENABLE_OPENMP
41#pragma omp atomic write
42#endif
43 parent_ = tmp;
44
45 return parent_;
46 }
47 }
48
49 // Shared data get/set
50
52 return prop_;
53 }
54
55 inline void setPropagation(Propagation *const p) {
56#ifdef TTK_ENABLE_OPENMP
57#pragma omp atomic write
58#endif
59 prop_ = p;
60 }
61
62 // UF get / set
63
64 inline int getRank() const {
65 return rank_;
66 }
67
68 inline void setRank(const int &rank) {
69#ifdef TTK_ENABLE_OPENMP
70#pragma omp atomic write
71#endif
72 rank_ = rank;
73 }
74
75 inline void setParent(AtomicUF *parent) {
76#ifdef TTK_ENABLE_OPENMP
77#pragma omp atomic write
78#endif
79 parent_ = parent;
80 }
81
82 static inline AtomicUF *makeUnion(AtomicUF *uf0, AtomicUF *uf1) {
83 uf0 = uf0->find();
84 uf1 = uf1->find();
85
86 if(uf0 == uf1) {
87 return uf0;
88 } else if(uf0->getRank() > uf1->getRank()) {
89 uf1->setParent(uf0);
90 return uf0;
91 } else if(uf0->getRank() < uf1->getRank()) {
92 uf0->setParent(uf1);
93 return uf1;
94 } else {
95 uf1->setParent(uf0);
96 uf0->setRank(uf0->getRank() + 1);
97 return uf0;
98 }
99 }
100
101 static inline AtomicUF *makeUnion(std::vector<AtomicUF *> &sets) {
102 AtomicUF *n = nullptr;
103
104 if(!sets.size())
105 return nullptr;
106
107 if(sets.size() == 1)
108 return sets[0];
109
110 for(int i = 0; i < (int)sets.size() - 1; i++) {
111 n = makeUnion(sets[i], sets[i + 1]);
112 }
113
114 return n;
115 }
116
117 inline bool operator<(const AtomicUF &other) const {
118 return rank_ < other.rank_;
119 }
120
121 inline bool operator>(const AtomicUF &other) const {
122 return rank_ > other.rank_;
123 }
124 };
125
126 } // namespace ftr
127} // namespace ttk
Union find compliant with parallel find and maintaining the current local propagation inspired by ttk...
Definition FTRAtomicUF.h:23
bool operator>(const AtomicUF &other) const
void setParent(AtomicUF *parent)
Definition FTRAtomicUF.h:75
AtomicUF * find()
Definition FTRAtomicUF.h:35
bool operator<(const AtomicUF &other) const
int getRank() const
Definition FTRAtomicUF.h:64
void setRank(const int &rank)
Definition FTRAtomicUF.h:68
Propagation * getPropagation()
Definition FTRAtomicUF.h:51
void setPropagation(Propagation *const p)
Definition FTRAtomicUF.h:55
AtomicUF(Propagation *const p)
Definition FTRAtomicUF.h:30
static AtomicUF * makeUnion(std::vector< AtomicUF * > &sets)
static AtomicUF * makeUnion(AtomicUF *uf0, AtomicUF *uf1)
Definition FTRAtomicUF.h:82
TTK fTRGraph propagation management with Fibonacci heaps.
The Topology ToolKit.