TTK
Loading...
Searching...
No Matches
FTMAtomicVector.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#ifdef TTK_ENABLE_OPENMP
11#include <omp.h>
12#endif // TTK_ENABLE_OPENMP
13#include <iterator>
14#include <vector>
15
16#ifndef TTK_ENABLE_KAMIKAZE
17#include <iostream>
18#include <typeinfo>
19#endif
20
21namespace ttk {
22 template <typename type>
23 class FTMAtomicVector : public std::vector<type> {
24 private:
25 std::size_t nextId;
26 // for initialization
27 const type defaultValue;
28
29 public:
30 FTMAtomicVector(const std::size_t initSize = 1, const type &dv = type{})
31 : std::vector<type>(), nextId(0), defaultValue{dv} {
32#ifndef TTK_ENABLE_KAMIKAZE
33 if(!initSize) {
34 std::cout << "Caution, Atomic vector need a non-0 init size !"
35 << std::endl;
36 std::vector<type>::resize(1, defaultValue);
37 } else
38#endif
39 {
40 std::vector<type>::resize(initSize, defaultValue);
41 }
42 }
43
44 // copy constructor
46 : std::vector<type>(other), nextId(other.nextId) {
47#ifndef TTK_ENABLE_KAMIKAZE
48 if(!std::vector<type>::size()) {
49 reserve(1);
50 }
51#endif
52 }
53
54 FTMAtomicVector(FTMAtomicVector &&other) noexcept = default;
55
56 virtual ~FTMAtomicVector() = default;
57
58 // ---
59 // STL
60 // ---
61
62 // If we do not want to use default constructor for elements
63 // pre-allocated
64 void reserve(const std::size_t &newSize) {
65 if(newSize > std::vector<type>::size()) {
66#ifndef TTK_ENABLE_KAMIKAZE
67#ifdef TTK_ENABLE_OPENMP
68 if(omp_in_parallel()) {
69 // WARNING: In parallel we do not want to make reserve as it can lead
70 // to data race, we should not enter here
71#pragma omp critical(AtomicUFReserve)
72 { std::vector<type>::resize(newSize, defaultValue); }
73
74 } else
75#endif
76#endif
77 {
78 std::vector<type>::resize(newSize, defaultValue);
79 }
80 }
81 }
82
83 void reset(const std::size_t &nId = 0) {
84#ifdef TTK_ENABLE_OPENMP
85#pragma omp atomic write
86#endif
87 nextId = nId;
88 }
89
90 void clear() {
91 reset();
92
93 // Remove old content
94 std::size_t const oldSize = std::vector<type>::size();
95 std::vector<type>::clear();
96 reserve(oldSize);
97 }
98
99 std::size_t getNext() {
100 std::size_t resId;
101#ifdef TTK_ENABLE_OPENMP
102#pragma omp atomic capture
103#endif
104 resId = nextId++;
105
106 if(nextId == std::vector<type>::size()) {
107 reserve(std::vector<type>::size() * 2);
108 }
109
110 return resId;
111 }
112
113 std::size_t size() const {
114 return nextId;
115 }
116
117 bool empty() const {
118 return nextId == 0;
119 }
120
121 void push_back(const type &elmt) {
122 const auto &curPos = getNext();
123 (*this)[curPos] = elmt;
124 }
125
126 // --------
127 // OPERATOR
128 // --------
129
131 if(&other != this) {
132 std::vector<type>::operator=(other);
133 nextId = other.nextId;
134 }
135 return *this;
136 }
137
138 // ---------
139 // ITERATORS
140 // ---------
141 // allow foreach on the vector
142
143 using iterator = typename std::vector<type>::iterator;
144 using const_iterator = typename std::vector<type>::const_iterator;
145
147 return this->begin() + nextId;
148 }
149
151 return this->cbegin() + nextId;
152 }
153
154 using riterator = typename std::vector<type>::reverse_iterator;
155 using const_riterator = typename std::vector<type>::const_reverse_iterator;
156
158 return this->rend() - (nextId - 1);
159 }
160
162 return this->crend() - (nextId - 1);
163 }
164 };
165} // namespace ttk
TTK processing package that manage a parallel vecrion of vector.
typename std::vector< type >::const_reverse_iterator const_riterator
virtual ~FTMAtomicVector()=default
FTMAtomicVector(const FTMAtomicVector &other)
typename std::vector< type >::iterator iterator
FTMAtomicVector(FTMAtomicVector &&other) noexcept=default
const_iterator cend() const
void reset(const std::size_t &nId=0)
std::size_t size() const
FTMAtomicVector(const std::size_t initSize=1, const type &dv=type{})
FTMAtomicVector< type > & operator=(const FTMAtomicVector< type > &other)
typename std::vector< type >::const_iterator const_iterator
void reserve(const std::size_t &newSize)
void push_back(const type &elmt)
typename std::vector< type >::reverse_iterator riterator
const_riterator crbegin() const
The Topology ToolKit.
T begin(std::pair< T, T > &p)
Definition ripserpy.cpp:468