TTK
Loading...
Searching...
No Matches
FTMAtomicUF.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <memory>
18#include <vector>
19
20#include "FTMDataTypes.h"
21#include "FTMStructures.h"
22
23namespace ttk {
24 namespace ftm {
25
26 class AtomicUF {
27 private:
28 unsigned rank_{};
29 AtomicUF *parent_{};
30 SharedData data_;
31
32 public:
33 inline explicit AtomicUF(SimplexId extrema = nullVertex)
34 : data_(extrema) {
35 }
36
37 // heavy recursif
38 inline AtomicUF *find() {
39 if(parent_ == nullptr)
40 return this;
41 else {
42 decltype(parent_) tmp = parent_->find();
43#ifdef TTK_ENABLE_OPENMP
44#pragma omp atomic write
45#endif
46 parent_ = tmp;
47
48 return parent_;
49 }
50 }
51
52 // Shared data get/set
53
54 inline SimplexId getExtrema() const {
55 return data_.extrema;
56 }
57
59#ifndef TTK_ENABLE_KAMIKAZE
60 if(data_.states.size() != 1) {
61 std::cout << "AtomicUF :: getFirstState : nb state 1 != "
62 << data_.states.size() << std::endl;
63 }
64#endif
65 return data_.states[0];
66 }
67
68 inline CurrentState *getState(const std::size_t id) {
69#ifndef TTK_ENABLE_KAMIKAZE
70 if(id >= data_.states.size()) {
71 std::cout << "AtomicUF :: getState : " << id
72 << " >= " << data_.states.size() << std::endl;
73 }
74#endif
75 return data_.states[id];
76 }
77
79 return data_.states;
80 }
81
82 inline size_t getNbStates() const {
83 return data_.states.size();
84 }
85
87 return data_.openedArcs;
88 }
89
90 inline void clearOpenedArcs() {
91 data_.openedArcs.reset();
92 }
93
94 inline void setExtrema(SimplexId v) {
95 data_.extrema = v;
96 }
97
98 inline void reserveData(const size_t &s) {
99 data_.reserve(s);
100 }
101
102 inline void addState(CurrentState *s) {
103 data_.addState(s);
104 }
105
106 inline void addArcToClose(idSuperArc a) {
107 data_.addArc(a);
108 }
109
111 CurrentState *s = data_.states[0];
112 const auto &nbState = data_.states.size();
113
114 for(valence i = 1; i < (valence)nbState; ++i) {
115 s->merge(*data_.states[i]);
116 }
117
118 data_.states.reset(1);
119 return s;
120 }
121
122 // UF get / set
123
124 inline int getRank() const {
125 return rank_;
126 }
127
128 inline void setRank(const int &rank) {
129#ifdef TTK_ENABLE_OPENMP
130#pragma omp atomic write
131#endif
132 rank_ = rank;
133 }
134
135 inline void setParent(AtomicUF *parent) {
136#ifdef TTK_ENABLE_OPENMP
137#pragma omp atomic write
138#endif
139 parent_ = parent;
140 }
141
142 static inline AtomicUF *makeUnion(AtomicUF *uf0, AtomicUF *uf1) {
143 uf0 = uf0->find();
144 uf1 = uf1->find();
145
146 if(uf0 == uf1) {
147 return uf0;
148 } else if(uf0->getRank() > uf1->getRank()) {
149 uf1->setParent(uf0);
150 uf0->data_.merge(uf1->data_);
151 return uf0;
152 } else if(uf0->getRank() < uf1->getRank()) {
153 uf0->setParent(uf1);
154 uf1->data_.merge(uf0->data_);
155 return uf1;
156 } else {
157 uf1->setParent(uf0);
158 uf0->setRank(uf0->getRank() + 1);
159 uf0->data_.merge(uf1->data_);
160 return uf0;
161 }
162 }
163
164 static inline AtomicUF *makeUnion(std::vector<AtomicUF *> &sets) {
165 AtomicUF *n = nullptr;
166
167 if(!sets.size())
168 return nullptr;
169
170 if(sets.size() == 1)
171 return sets[0];
172
173 for(int i = 0; i < (int)sets.size() - 1; i++)
174 n = makeUnion(sets[i], sets[i + 1]);
175
176 return n;
177 }
178
179 inline bool operator<(const AtomicUF &other) const {
180 return rank_ < other.rank_;
181 }
182
183 inline bool operator>(const AtomicUF &other) const {
184 return rank_ > other.rank_;
185 }
186 };
187
188 } // namespace ftm
189} // namespace ttk
TTK processing package that manage a parallel vecrion of vector.
void reset(const std::size_t &nId=0)
void setExtrema(SimplexId v)
Definition FTMAtomicUF.h:94
void reserveData(const size_t &s)
Definition FTMAtomicUF.h:98
void addArcToClose(idSuperArc a)
CurrentState * getState(const std::size_t id)
Definition FTMAtomicUF.h:68
int getRank() const
FTMAtomicVector< idSuperArc > & getOpenedArcs()
Definition FTMAtomicUF.h:86
FTMAtomicVector< CurrentState * > & getStates()
Definition FTMAtomicUF.h:78
SimplexId getExtrema() const
Definition FTMAtomicUF.h:54
CurrentState * getFirstState()
Definition FTMAtomicUF.h:58
bool operator>(const AtomicUF &other) const
AtomicUF(SimplexId extrema=nullVertex)
Definition FTMAtomicUF.h:33
CurrentState * mergeStates()
void setRank(const int &rank)
void addState(CurrentState *s)
size_t getNbStates() const
Definition FTMAtomicUF.h:82
static AtomicUF * makeUnion(std::vector< AtomicUF * > &sets)
static AtomicUF * makeUnion(AtomicUF *uf0, AtomicUF *uf1)
AtomicUF * find()
Definition FTMAtomicUF.h:38
void setParent(AtomicUF *parent)
bool operator<(const AtomicUF &other) const
long unsigned int idSuperArc
SuperArc index in vect_superArcs_.
SimplexId valence
for vertex up/down valence
The Topology ToolKit.
int SimplexId
Identifier type for simplices of any dimension.
Definition DataTypes.h:22
void merge(CurrentState &other)
FTMAtomicVector< CurrentState * > states
void addState(CurrentState *curState)
FTMAtomicVector< idSuperArc > openedArcs
void addArc(const idSuperArc arc)
void merge(SharedData &other)
void reserve(const size_t &s)