-
Notifications
You must be signed in to change notification settings - Fork 11
/
snode.h
224 lines (190 loc) · 6.11 KB
/
snode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "bate.h"
#include <mutex>
#include <mutex>
#if __has_include(<tbb/spin_mutex.h>)
#include <tbb/spin_mutex.h>
#else // for students don't have tbb
namespace tbb { using spin_mutex = mutex; };
#endif
#define N (512*512)
template <int Bshift, class Node>
struct DenseBlock {
static constexpr int numDepth = 0;
static constexpr bool isPlace = false;
static constexpr bool bitShift = Bshift;
static constexpr int B = 1 << Bshift;
static constexpr int Bmask = B - 1;
Node m_data[B][B];
Node *fetch(int x, int y) {
return &m_data[x & Bmask][y & Bmask];
}
Node *touch(int x, int y) {
return &m_data[x & Bmask][y & Bmask];
}
template <class Func>
void foreach(Func const &func) {
for (int x = 0; x < B; x++) {
for (int y = 0; y < B; y++) {
func(x, y, &m_data[x][y]);
}
}
}
};
template <int Bshift, class Node>
struct PointerBlock {
static constexpr int numDepth = Node::numDepth + 1;
static constexpr bool isPlace = false;
static constexpr bool bitShift = Bshift;
static constexpr int B = 1 << Bshift;
static constexpr int Bmask = B - 1;
std::unique_ptr<Node> m_data[B][B];
tbb::spin_mutex m_mtx[B][B];
Node *fetch(int x, int y) {
return m_data[x & Bmask][y & Bmask].get();
}
Node *touch(int x, int y) {
std::lock_guard _(m_mtx[x & Bmask][y & Bmask]);
auto &block = m_data[x & Bmask][y & Bmask];
if (!block)
block = std::make_unique<Node>();
return block.get();
}
template <class Func>
void foreach(Func func) {
#pragma omp parallel for collapse(2) firstprivate(func)
for (int x = 0; x < B; x++) {
for (int y = 0; y < B; y++) {
auto ptr = m_data[x][y].get();
if (ptr)
func(x, y, ptr);
}
}
}
};
template <class Node>
struct HashBlock {
static constexpr int numDepth = Node::numDepth + 1;
static constexpr bool isPlace = false;
static constexpr bool bitShift = 0;
struct MyHash {
std::size_t operator()(std::tuple<int, int> const &key) const {
auto const &[x, y] = key;
return (x * 2718281828) ^ (y * 3141592653);
}
};
std::unordered_map<std::tuple<int, int>, Node, MyHash> m_data;
tbb::spin_mutex m_mtx;
Node *fetch(int x, int y) {
std::lock_guard _(m_mtx);
auto it = m_data.find(std::make_tuple(x, y));
if (it == m_data.end())
return nullptr;
return &it->second;
}
Node *touch(int x, int y) {
std::lock_guard _(m_mtx);
auto it = m_data.find(std::make_tuple(x, y));
if (it == m_data.end()) {
return &m_data.try_emplace({x, y}).first->second;
}
return &it->second;
}
template <class Func>
void foreach(Func func) {
std::vector<std::tuple<int, int, Node *>> vec;
for (auto &[key, block]: m_data) {
auto const &[x, y] = key;
vec.emplace_back(x, y, &block);
}
#pragma omp parallel for firstprivate(func)
for (int i = 0; i < vec.size(); i++) {
auto const &[x, y, block] = vec[i];
func(x, y, block);
}
}
};
template <class T>
struct PlaceData {
static constexpr bool isPlace = true;
T m_value;
T read() {
return m_value;
}
void write(T value) {
m_value = value;
}
template <class Func>
void visit(Func const &func) {
func(m_value);
}
};
template <class T, class Layout>
struct RootGrid {
Layout m_root;
template <class Node>
static T _read(Node &node, int x, int y) {
if constexpr (node.isPlace) {
return node.read();
} else {
auto *child = node.fetch(x >> node.bitShift, y >> node.bitShift);
if (!child)
return T{};
return _read(*child, x, y);
}
}
T read(int x, int y) {
return _read(m_root, x, y);
}
struct WriteAccessor {
Layout &m_root;
mutable std::array<std::map<std::tuple<int, int>, void *>, Layout::numDepth> cached;
template <int currDepth, class Node>
void _write(Node &node, int x, int y, T value) const {
if constexpr (node.isPlace) {
return node.write(value);
} else {
auto child = [&] {
if constexpr (currDepth < Layout::numDepth) {
auto &cache = std::get<currDepth>(cached);
auto it = cache.find({x >> node.bitShift, y >> node.bitShift});
if (it != cache.end()) {
return decltype(node.touch(0, 0))(it->second);
}
}
auto *child = node.touch(x >> node.bitShift, y >> node.bitShift);
if constexpr (currDepth < Layout::numDepth) {
auto &cache = std::get<currDepth>(cached);
cache.try_emplace({x >> node.bitShift, y >> node.bitShift}, child);
}
return child;
}();
return _write<currDepth + 1>(*child, x, y, value);
}
}
void write(int x, int y, T value) const {
return _write<0>(m_root, x, y, value);
}
};
WriteAccessor writeAccess() {
return {m_root};
}
template <class Node, class Func>
static void _foreach(Node &node, int x, int y, Func const &func) {
if constexpr (node.isPlace) {
return node.visit([&] (T &val) {
func(x, y, val);
});
} else {
int xb = x << node.bitShift;
int yb = y << node.bitShift;
return node.foreach([&] (int x, int y, auto *child) {
_foreach(*child, x, y, func);
});
}
}
template <class Func>
void foreach(Func const &func) {
_foreach(m_root, 0, 0, func);
}
};
//using ExampleGrid = RootGrid<float, HashBlock<PointerBlock<11, DenseBlock<8, PlaceData<float>>>>>;