-
Notifications
You must be signed in to change notification settings - Fork 174
/
lap_kernels.cuh
560 lines (482 loc) · 19.2 KB
/
lap_kernels.cuh
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
* Copyright 2020 KETAN DATE & RAKESH NAGI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* CUDA Implementation of O(n^3) alternating tree Hungarian Algorithm
* Authors: Ketan Date and Rakesh Nagi
*
* Article reference:
* Date, Ketan, and Rakesh Nagi. "GPU-accelerated Hungarian algorithms
* for the Linear Assignment Problem." Parallel Computing 57 (2016): 52-72.
*
*/
#pragma once
#include "d_structs.h"
#include <raft/cudart_utils.h>
#include <raft/handle.hpp>
#include <thrust/execution_policy.h>
#include <thrust/for_each.h>
#include <cstddef>
namespace raft {
namespace lap {
namespace detail {
const int DORMANT{0};
const int ACTIVE{1};
const int VISITED{2};
const int REVERSE{3};
const int AUGMENT{4};
const int MODIFIED{5};
template <typename weight_t>
bool __device__ near_zero(weight_t w, weight_t epsilon)
{
return ((w > -epsilon) && (w < epsilon));
}
template <>
bool __device__ near_zero<int32_t>(int32_t w, int32_t epsilon)
{
return (w == 0);
}
template <>
bool __device__ near_zero<int64_t>(int64_t w, int64_t epsilon)
{
return (w == 0);
}
// Device function for traversing the neighbors from start pointer to end pointer and updating the
// covers. The function sets d_next to 4 if there are uncovered zeros, indicating the requirement of
// Step 4 execution.
template <typename vertex_t, typename weight_t>
__device__ void cover_and_expand_row(weight_t const* d_elements,
weight_t const* d_row_duals,
weight_t const* d_col_duals,
weight_t* d_col_slacks,
int* d_row_covers,
int* d_col_covers,
vertex_t const* d_col_assignments,
bool* d_flag,
vertex_t* d_row_parents,
vertex_t* d_col_parents,
int* d_row_visited,
int* d_col_visited,
vertex_t rowid,
int spid,
int colid,
vertex_t N,
weight_t epsilon)
{
int ROWID = spid * N + rowid;
int COLID = spid * N + colid;
weight_t slack =
d_elements[spid * N * N + rowid * N + colid] - d_row_duals[ROWID] - d_col_duals[COLID];
int nxt_rowid = d_col_assignments[COLID];
int NXT_ROWID = spid * N + nxt_rowid;
if (rowid != nxt_rowid && d_col_covers[COLID] == 0) {
if (slack < d_col_slacks[COLID]) {
d_col_slacks[COLID] = slack;
d_col_parents[COLID] = ROWID;
}
if (near_zero(d_col_slacks[COLID], epsilon)) {
if (nxt_rowid != -1) {
d_row_parents[NXT_ROWID] = COLID; // update parent info
d_row_covers[NXT_ROWID] = 0;
d_col_covers[COLID] = 1;
if (d_row_visited[NXT_ROWID] != VISITED) d_row_visited[NXT_ROWID] = ACTIVE;
} else {
d_col_visited[COLID] = REVERSE;
*d_flag = true;
}
}
}
d_row_visited[ROWID] = VISITED;
}
// Device function for traversing an alternating path from unassigned row to unassigned column.
template <typename vertex_t>
__device__ void __reverse_traversal(int* d_row_visited,
vertex_t* d_row_children,
vertex_t* d_col_children,
vertex_t const* d_row_parents,
vertex_t const* d_col_parents,
int cur_colid)
{
int cur_rowid = -1;
while (cur_colid != -1) {
d_col_children[cur_colid] = cur_rowid;
cur_rowid = d_col_parents[cur_colid];
d_row_children[cur_rowid] = cur_colid;
cur_colid = d_row_parents[cur_rowid];
}
d_row_visited[cur_rowid] = AUGMENT;
}
// Device function for augmenting the alternating path from unassigned column to unassigned row.
template <typename vertex_t>
__device__ void __augment(vertex_t* d_row_assignments,
vertex_t* d_col_assignments,
vertex_t const* d_row_children,
vertex_t const* d_col_children,
vertex_t cur_rowid,
vertex_t N)
{
int cur_colid = -1;
while (cur_rowid != -1) {
cur_colid = d_row_children[cur_rowid];
d_row_assignments[cur_rowid] = cur_colid % N;
d_col_assignments[cur_colid] = cur_rowid % N;
cur_rowid = d_col_children[cur_colid];
}
}
// Kernel for reducing the rows by subtracting row minimum from each row element.
// FIXME: Once cuda 10.2 is the standard should replace passing infinity
// here with using cuda::std::numeric_limits<weight_t>::max()
template <typename vertex_t, typename weight_t>
__global__ void kernel_rowReduction(
weight_t const* d_costs, weight_t* d_row_duals, int SP, vertex_t N, weight_t infinity)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
weight_t min = infinity;
if (spid < SP && rowid < N) {
for (int colid = 0; colid < N; colid++) {
weight_t slack = d_costs[spid * N * N + rowid * N + colid];
if (slack < min) { min = slack; }
}
d_row_duals[spid * N + rowid] = min;
}
}
// Kernel for reducing the column by subtracting column minimum from each column element.
// FIXME: Once cuda 10.2 is the standard should replace passing infinity
// here with using cuda::std::numeric_limits<weight_t>::max()
template <typename vertex_t, typename weight_t>
__global__ void kernel_columnReduction(weight_t const* d_costs,
weight_t const* d_row_duals,
weight_t* d_col_duals,
int SP,
vertex_t N,
weight_t infinity)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int colid = blockIdx.x * blockDim.x + threadIdx.x;
weight_t min = infinity;
if (spid < SP && colid < N) {
for (int rowid = 0; rowid < N; rowid++) {
weight_t cost = d_costs[spid * N * N + rowid * N + colid];
weight_t row_dual = d_row_duals[spid * N + rowid];
weight_t slack = cost - row_dual;
if (slack < min) { min = slack; }
}
d_col_duals[spid * N + colid] = min;
}
}
// Kernel for calculating initial assignments.
template <typename vertex_t, typename weight_t>
__global__ void kernel_computeInitialAssignments(weight_t const* d_costs,
weight_t const* d_row_duals,
weight_t const* d_col_duals,
vertex_t* d_row_assignments,
vertex_t* d_col_assignments,
int* d_row_lock,
int* d_col_lock,
int SP,
vertex_t N,
weight_t epsilon)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int colid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP && colid < N) {
int overall_colid = spid * N + colid;
weight_t col_dual = d_col_duals[overall_colid];
for (vertex_t rowid = 0; rowid < N; rowid++) {
int overall_rowid = spid * N + rowid;
if (d_col_lock[overall_colid] == 1) break;
weight_t cost = d_costs[spid * N * N + rowid * N + colid];
weight_t row_dual = d_row_duals[overall_rowid];
weight_t slack = cost - row_dual - col_dual;
if (near_zero(slack, epsilon)) {
if (atomicCAS(&d_row_lock[overall_rowid], 0, 1) == 0) {
d_row_assignments[overall_rowid] = colid;
d_col_assignments[overall_colid] = rowid;
d_col_lock[overall_colid] = 1;
}
}
}
}
}
// Kernel for populating the cover arrays and initializing alternating tree.
template <typename vertex_t>
__global__ void kernel_computeRowCovers(
vertex_t* d_row_assignments, int* d_row_covers, int* d_row_visited, int SP, vertex_t N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP && rowid < N) {
int index = spid * N + rowid;
if (d_row_assignments[index] != -1) {
d_row_covers[index] = 1;
} else {
d_row_visited[index] = ACTIVE;
}
}
}
// Kernel for populating the predicate matrix for edges in row major format.
template <typename vertex_t>
__global__ void kernel_rowPredicateConstructionCSR(
bool* d_predicates, vertex_t* d_addresses, int* d_row_visited, int SP, vertex_t N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP && rowid < N) {
int index = spid * N + rowid;
if (d_row_visited[index] == ACTIVE) {
d_predicates[index] = true;
d_addresses[index] = 1;
} else {
d_predicates[index] = false;
d_addresses[index] = 0;
}
}
}
// Kernel for scattering the edges based on the scatter addresses.
template <typename vertex_t>
__global__ void kernel_rowScatterCSR(bool const* d_predicates,
vertex_t const* d_addresses,
vertex_t* d_neighbors,
vertex_t* d_ptrs,
vertex_t M,
int SP,
vertex_t N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP && rowid < N) {
int index = spid * N + rowid;
bool predicate = d_predicates[index];
vertex_t compid = d_addresses[index];
if (predicate) { d_neighbors[compid] = rowid; }
if (rowid == 0) {
d_ptrs[spid] = compid;
d_ptrs[SP] = M;
}
}
}
// Kernel for finding the minimum zero cover.
template <typename vertex_t, typename weight_t>
__global__ void kernel_coverAndExpand(bool* d_flag,
vertex_t const* d_ptrs,
vertex_t const* d_neighbors,
weight_t const* d_elements,
Vertices<vertex_t, weight_t> d_vertices,
VertexData<vertex_t> d_row_data,
VertexData<vertex_t> d_col_data,
int SP,
vertex_t N,
weight_t epsilon)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int colid = blockIdx.x * blockDim.x + threadIdx.x;
// Load values into local memory
if (spid < SP && colid < N) {
thrust::for_each(
thrust::seq,
d_neighbors + d_ptrs[spid],
d_neighbors + d_ptrs[spid + 1],
[d_elements, d_vertices, d_flag, d_row_data, d_col_data, spid, colid, N, epsilon] __device__(
vertex_t rowid) {
cover_and_expand_row(d_elements,
d_vertices.row_duals,
d_vertices.col_duals,
d_vertices.col_slacks,
d_vertices.row_covers,
d_vertices.col_covers,
d_vertices.col_assignments,
d_flag,
d_row_data.parents,
d_col_data.parents,
d_row_data.is_visited,
d_col_data.is_visited,
rowid,
spid,
colid,
N,
epsilon);
});
}
}
// Kernel for constructing the predicates for reverse pass or augmentation candidates.
template <typename vertex_t>
__global__ void kernel_augmentPredicateConstruction(bool* d_predicates,
vertex_t* d_addresses,
int* d_visited,
int size)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int visited = d_visited[id];
if ((visited == REVERSE) || (visited == AUGMENT)) {
d_predicates[id] = true;
d_addresses[id] = 1;
} else {
d_predicates[id] = false;
d_addresses[id] = 0;
}
}
}
// Kernel for scattering the vertices based on the scatter addresses.
template <typename vertex_t>
__global__ void kernel_augmentScatter(vertex_t* d_elements,
bool const* d_predicates,
vertex_t const* d_addresses,
std::size_t size)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
if (d_predicates[id]) { d_elements[d_addresses[id]] = id; }
}
}
// Kernel for executing the reverse pass of the maximum matching algorithm.
template <typename vertex_t>
__global__ void kernel_reverseTraversal(vertex_t* d_elements,
VertexData<vertex_t> d_row_data,
VertexData<vertex_t> d_col_data,
int size)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
__reverse_traversal(d_row_data.is_visited,
d_row_data.children,
d_col_data.children,
d_row_data.parents,
d_col_data.parents,
d_elements[id]);
}
}
// Kernel for executing the augmentation pass of the maximum matching algorithm.
template <typename vertex_t>
__global__ void kernel_augmentation(vertex_t* d_row_assignments,
vertex_t* d_col_assignments,
vertex_t const* d_row_elements,
VertexData<vertex_t> d_row_data,
VertexData<vertex_t> d_col_data,
vertex_t N,
vertex_t size)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
__augment(d_row_assignments,
d_col_assignments,
d_row_data.children,
d_col_data.children,
d_row_elements[id],
N);
}
}
// Kernel for updating the dual values in Step 5.
// FIXME: Once cuda 10.2 is the standard should replace passing infinity
// here with using cuda::std::numeric_limits<weight_t>::max()
template <typename vertex_t, typename weight_t>
__global__ void kernel_dualUpdate_1(weight_t* d_sp_min,
weight_t const* d_col_slacks,
int const* d_col_covers,
int SP,
vertex_t N,
weight_t infinity)
{
int spid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP) {
weight_t min = infinity;
for (int colid = 0; colid < N; colid++) {
int index = spid * N + colid;
weight_t slack = d_col_slacks[index];
int col_cover = d_col_covers[index];
if (col_cover == 0)
if (slack < min) min = slack;
}
d_sp_min[spid] = min;
}
}
// Kernel for updating the dual values in Step 5.
// FIXME: Once cuda 10.2 is the standard should replace passing infinity
// here with using cuda::std::numeric_limits<weight_t>::max()
template <typename vertex_t, typename weight_t>
__global__ void kernel_dualUpdate_2(weight_t const* d_sp_min,
weight_t* d_row_duals,
weight_t* d_col_duals,
weight_t* d_col_slacks,
int const* d_row_covers,
int const* d_col_covers,
int* d_row_visited,
vertex_t* d_col_parents,
int SP,
vertex_t N,
weight_t infinity,
weight_t epsilon)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP && id < N) {
int index = spid * N + id;
if (d_sp_min[spid] < infinity) {
weight_t theta = d_sp_min[spid];
int row_cover = d_row_covers[index];
int col_cover = d_col_covers[index];
if (row_cover == 0) // Row vertex is reachable from source.
d_row_duals[index] += theta;
if (col_cover == 1) // Col vertex is reachable from source.
d_col_duals[index] -= theta;
else {
// Col vertex is unreachable from source.
d_col_slacks[index] -= d_sp_min[spid];
if (near_zero(d_col_slacks[index], epsilon)) {
int par_rowid = d_col_parents[index];
if (par_rowid != -1) d_row_visited[par_rowid] = ACTIVE;
}
}
}
}
}
// Kernel for calculating optimal objective function value using dual variables.
template <typename vertex_t, typename weight_t>
__global__ void kernel_calcObjValDual(weight_t* d_obj_val_dual,
weight_t const* d_row_duals,
weight_t const* d_col_duals,
int SP,
vertex_t N)
{
int spid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP) {
float val = 0;
for (int i = 0; i < N; i++)
val += (d_row_duals[spid * N + i] + d_col_duals[spid * N + i]);
d_obj_val_dual[spid] = val;
}
}
// Kernel for calculating optimal objective function value using dual variables.
template <typename vertex_t, typename weight_t>
__global__ void kernel_calcObjValPrimal(weight_t* d_obj_val_primal,
weight_t const* d_costs,
vertex_t const* d_row_assignments,
int SP,
vertex_t N)
{
int spid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP) {
weight_t val = 0;
for (int i = 0; i < N; i++) {
vertex_t j = d_row_assignments[spid * N + i];
val += d_costs[spid * N * N + i * N + j];
}
d_obj_val_primal[spid] = val;
}
}
} // namespace detail
} // namespace lap
} // namespace raft