-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster2d_cpu.hpp
164 lines (153 loc) · 4.13 KB
/
cluster2d_cpu.hpp
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
#pragma once
#include <algorithm>
#include <atomic>
#include <cassert>
#include <iostream>
#include <utility>
#include <vector>
#include "umatrix_cpu.hpp"
class UF2dCPU
{
public:
auto root(const std::size_t ind,
const std::memory_order ord = std::memory_order_seq_cst) const
{
auto pos = ind;
while (true)
{
const auto pos_new = parent[pos].load(ord);
if (pos_new == pos)
{
break;
}
else
{
pos = pos_new;
}
}
return pos;
}
private:
MatrixCPU<bool> c;
MatrixCPU<std::atomic<std::size_t>> parent;
void rowwise_merge(const std::size_t i, const std::size_t j)
{
if (j != 0 && c(i, j) == c(i, j - 1))
{
const auto p = parent(i, j - 1).load(std::memory_order_relaxed);
parent(i, j).store(p, std::memory_order_relaxed);
}
}
void colwise_merge(const std::size_t i, const std::size_t j)
{
if (i != 0 && c(i, j) == c(i - 1, j))
{
const auto p = parent(i - 1, j).load(std::memory_order_relaxed);
parent(i, j).store(p, std::memory_order_relaxed);
}
}
void pathcomp(const std::size_t ind)
{
parent[ind].store(root(ind, std::memory_order_relaxed),
std::memory_order_relaxed);
}
void merge(std::size_t ind1, std::size_t ind2)
{
// Needs seq_cst ordering?
// -> maybe NO
while (true)
{
ind1 = root(ind1, std::memory_order_relaxed);
ind2 = root(ind2, std::memory_order_relaxed);
if (ind1 == ind2)
{
return;
}
else if (ind1 > ind2)
{
std::swap(ind1, ind2);
}
auto expected = parent[ind2].load(std::memory_order_relaxed);
const auto desired = std::min({expected, ind1});
parent[ind2].compare_exchange_weak(expected, desired,
std::memory_order_relaxed);
}
}
public:
UF2dCPU(const MatrixCPU<bool>& map) : c(map), parent(map.rows(), map.cols())
{
assert(c.size() != 0);
// row-major index
std::size_t id = 0;
for (auto&& pij : parent)
{
pij = id++;
}
}
void run(void)
{
#pragma omp parallel
#pragma omp for collapse(2)
for (std::size_t i = 0; i < c.rows(); ++i)
{
for (std::size_t j = 0; j < c.cols(); ++j)
{
rowwise_merge(i, j);
}
}
#pragma omp for collapse(2)
for (std::size_t i = 0; i < c.rows(); ++i)
{
for (std::size_t j = 0; j < c.cols(); ++j)
{
colwise_merge(i, j);
}
}
#pragma omp for
for (std::size_t ii = 0; ii < c.size(); ++ii)
{
pathcomp(ii);
}
#pragma omp for collapse(2) nowait
for (std::size_t i = 0; i < c.rows(); ++i)
{
for (std::size_t j = 1; j < c.cols(); ++j)
{
if (c(i, j) == c(i, j - 1))
{
merge(c.index(i, j), c.index(i, j - 1));
}
}
}
#pragma omp for collapse(2)
for (std::size_t i = 1; i < c.rows(); ++i)
{
for (std::size_t j = 0; j < c.cols(); ++j)
{
if (c(i, j) == c(i - 1, j))
{
merge(c.index(i, j), c.index(i - 1, j));
}
}
}
#pragma omp for
for (std::size_t ii = 0; ii < c.size(); ++ii)
{
pathcomp(ii);
}
}
void debug(void) const
{
c.print();
std::cout << std::endl;
for (std::size_t i = 0; i < parent.rows(); ++i)
{
for (std::size_t j = 0; j < parent.cols(); ++j)
{
std::cout << root(parent.index(i, j))
<< (j + 1 == parent.cols() ? "\n" : " ");
}
}
std::cout << std::endl;
}
};