-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmst.cu
261 lines (215 loc) · 7.85 KB
/
mst.cu
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "mst.hpp"
#include <cuda/std/limits>
#include <cuda/atomic>
#include <cooperative_groups.h>
namespace cg = cooperative_groups;
namespace custd = cuda::std;
#define WARPSIZE 32
#define BLOCKSIZE 512
static inline constexpr uint64_t MINV_MAX =
custd::numeric_limits<uint64_t>::max();
namespace kernel {
struct alignas(16) WorkList {
uint32_t u;
uint32_t v;
uint32_t w;
uint32_t e;
};
namespace DS {
static inline __device__ uint32_t
root(const uint32_t node, const uint32_t *const __restrict__ parent) {
auto root = node;
auto next = parent[root];
while (root != next) {
root = next;
next = parent[root];
}
return root;
}
static inline __device__ void join(const uint32_t u, const uint32_t v,
uint32_t *const __restrict__ parent) {
uint32_t M;
uint32_t m = u;
uint32_t R = v;
do {
M = max(m, R);
m = min(m, R);
R = M;
} while (!cuda::atomic_ref<uint32_t, cuda::thread_scope::thread_scope_device>{
parent[M]}
.compare_exchange_strong(R, m));
}
} // namespace DS
static __global__ void initializeVertices(const uint32_t nodes,
uint32_t *const __restrict__ parent,
uint64_t *const __restrict__ minv) {
const auto node = threadIdx.x + blockIdx.x * BLOCKSIZE;
if (node < nodes) {
parent[node] = node;
minv[node] = MINV_MAX;
}
}
static __global__ void initializeWL(const uint32_t nodes,
uint32_t *const __restrict__ wlSize,
WorkList *const __restrict__ wl,
const uint32_t *const __restrict__ N,
const uint32_t *const __restrict__ F,
const uint32_t *const __restrict__ W) {
auto node = threadIdx.x + blockIdx.x * BLOCKSIZE;
uint32_t adjBegin = 0;
uint32_t adjEnd = 0;
uint32_t degree = 0;
if (node < nodes) {
adjBegin = N[node];
adjEnd = N[node + 1];
degree = adjEnd - adjBegin;
if (degree < 4) {
for (uint32_t i = adjBegin; i < adjEnd; i++) {
const auto neighbor = F[i];
// Only one direction
// less work is being done
if (neighbor > node) {
const auto weight = W[i];
const auto k =
cuda::atomic_ref<uint32_t, cuda::thread_scope_device>{*wlSize}
.fetch_add(1);
wl[k] = WorkList{node, neighbor, weight, i};
}
}
}
}
auto warp = cg::tiled_partition<WARPSIZE>(cg::this_thread_block());
auto lane = warp.thread_rank();
auto ballot = warp.ballot(degree >= 4);
while (ballot != 0) {
// Select lane with lowest id and mark as processed
// aka remove active bit from ballot
int who = __ffs(ballot) - 1;
ballot &= ballot - 1;
// Pass around the node, adjBegin and adjEnd
node = warp.shfl(node, who);
adjBegin = warp.shfl(adjBegin, who);
adjEnd = warp.shfl(adjEnd, who);
for (uint32_t i = adjBegin + lane; i < adjEnd; i += warp.num_threads()) {
const auto neighbor = F[i];
if (neighbor > node) {
const auto weight = W[i];
const auto k =
cuda::atomic_ref<uint32_t, cuda::thread_scope_device>{*wlSize}
.fetch_add(1);
wl[k] = WorkList{node, neighbor, weight, i};
}
}
}
}
static __global__ void
gatherLightestNode(const uint32_t activeWLSize,
uint32_t *const __restrict__ secondaryWLSize,
const WorkList *const __restrict__ activeWL,
WorkList *const __restrict__ secondaryWL,
const uint32_t *const __restrict__ parent,
uint64_t *const __restrict__ minv) {
const int idx = threadIdx.x + blockIdx.x * BLOCKSIZE;
if (idx < activeWLSize) {
auto item = activeWL[idx];
const auto u = DS::root(item.u, parent);
const auto v = DS::root(item.v, parent);
if (u != v) {
item.u = u;
item.v = v;
const auto k =
cuda::atomic_ref<uint32_t, cuda::thread_scope_device>{
*secondaryWLSize}
.fetch_add(1);
secondaryWL[k] = item;
const auto value =
static_cast<uint64_t>(item.w) << 32 | static_cast<uint64_t>(item.e);
if (minv[u] > value)
cuda::atomic_ref<uint64_t, cuda::thread_scope_device>{minv[u]}
.fetch_min(value);
if (minv[v] > value)
cuda::atomic_ref<uint64_t, cuda::thread_scope_device>{minv[v]}
.fetch_min(value);
}
}
}
static __global__ void insideMST(const uint32_t wlSize,
const WorkList *const __restrict__ wl,
uint32_t *const __restrict__ parent,
uint64_t *const __restrict__ minv,
bool *const __restrict__ inMST) {
const auto idx = threadIdx.x + blockIdx.x * BLOCKSIZE;
if (idx < wlSize) {
const auto item = wl[idx];
const auto value =
static_cast<uint64_t>(item.w) << 32 | static_cast<uint64_t>(item.e);
if (value == minv[item.u] || value == minv[item.v]) {
DS::join(item.u, item.v, parent);
inMST[item.e] = true;
}
}
}
static __global__ void resetMINV(const uint32_t wlSize,
WorkList *const __restrict__ wl,
uint64_t *const __restrict__ minv) {
const auto idx = threadIdx.x + blockIdx.x * BLOCKSIZE;
if (idx < wlSize) {
const auto item = wl[idx];
minv[item.u] = MINV_MAX;
minv[item.v] = MINV_MAX;
}
}
} // namespace kernel
bool *MST(const CSRGraph &g) {
bool *inMST;
cudaMallocManaged(&inMST, sizeof(bool) * g.E);
uint32_t *parentD;
uint64_t *minvD;
uint32_t *activeWlSizeD;
kernel::WorkList *activeWLD;
kernel::WorkList *secondaryWLD;
cudaStream_t stream0;
cudaStream_t stream1;
cudaStreamCreateWithFlags(&stream0, cudaStreamNonBlocking);
cudaStreamCreateWithFlags(&stream1, cudaStreamNonBlocking);
cudaMallocAsync(&parentD, sizeof(uint32_t) * g.V, stream0);
cudaMallocAsync(&minvD, sizeof(uint64_t) * g.V, stream0);
const uint32_t initBlocks = (g.V + BLOCKSIZE - 1) / BLOCKSIZE;
kernel::initializeVertices<<<initBlocks, BLOCKSIZE, 0, stream0>>>(
g.V, parentD, minvD);
cudaMallocAsync(&activeWlSizeD, sizeof(uint32_t), stream1);
cudaMemsetAsync(activeWlSizeD, 0, sizeof(uint32_t), stream1);
cudaMallocAsync(&activeWLD, sizeof(kernel::WorkList) * (g.E / 2), stream1);
cudaMallocAsync(&secondaryWLD, sizeof(kernel::WorkList) * (g.E / 2), stream1);
kernel::initializeWL<<<initBlocks, BLOCKSIZE, 0, stream1>>>(
g.V, activeWlSizeD, activeWLD, g.N, g.F, g.W);
uint32_t wlSize;
cudaMemcpyAsync(&wlSize, activeWlSizeD, sizeof(uint32_t), cudaMemcpyDefault,
stream1);
cudaStreamSynchronize(stream1);
while (wlSize > 0) {
cudaMemsetAsync(activeWlSizeD, 0, sizeof(uint32_t), stream0);
const uint32_t wlBlocks = (wlSize + BLOCKSIZE - 1) / BLOCKSIZE;
kernel::gatherLightestNode<<<wlBlocks, BLOCKSIZE, 0, stream0>>>(
wlSize, activeWlSizeD, activeWLD, secondaryWLD, parentD, minvD);
cudaMemcpyAsync(&wlSize, activeWlSizeD, sizeof(uint32_t), cudaMemcpyDefault,
stream0);
cudaStreamSynchronize(stream0);
std::swap(activeWLD, secondaryWLD);
if (wlSize > 0) {
kernel::insideMST<<<wlBlocks, BLOCKSIZE, 0, stream0>>>(
wlSize, activeWLD, parentD, minvD, inMST);
kernel::resetMINV<<<wlBlocks, BLOCKSIZE, 0, stream0>>>(wlSize, activeWLD,
minvD);
}
}
cudaFreeAsync(secondaryWLD, stream0);
cudaFreeAsync(activeWLD, stream0);
cudaFreeAsync(activeWlSizeD, stream0);
cudaFreeAsync(minvD, stream0);
cudaFreeAsync(parentD, stream0);
cudaStreamSynchronize(stream0);
cudaStreamDestroy(stream1);
cudaStreamDestroy(stream0);
return inMST;
}