-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_culap.cuh
858 lines (639 loc) · 32.7 KB
/
f_culap.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
#pragma once
#include <cuda.h>
#include <thrust/scan.h>
#include <thrust/reduce.h>
#include <thrust/device_ptr.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "d_structs.h"
#include "f_cutils.cuh"
// 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.
__device__ void __traverse(Matrix d_costs, Vertices d_vertices, bool *d_flag, int *d_row_parents, int *d_col_parents, int *d_row_visited, int *d_col_visited, int *d_start_ptr, int *d_end_ptr, int spid, int colid, int SP, int N)
{
int *ptr1 = d_start_ptr;
while (ptr1 != d_end_ptr)
{
int rowid = *ptr1;
int ROWID = spid * N + rowid;
int COLID = spid * N + colid;
double slack = d_costs.elements[spid * N * N + rowid * N + colid] - d_vertices.row_duals[ROWID] - d_vertices.col_duals[COLID];
int nxt_rowid = d_vertices.col_assignments[COLID];
int NXT_ROWID = spid * N + nxt_rowid;
if (rowid != nxt_rowid && d_vertices.col_covers[COLID] == 0)
{
if (slack < d_vertices.col_slacks[COLID])
{
d_vertices.col_slacks[COLID] = slack;
d_col_parents[COLID] = ROWID;
}
if (d_vertices.col_slacks[COLID] < EPSILON && d_vertices.col_slacks[COLID] > -EPSILON)
{
if (nxt_rowid != -1)
{
d_row_parents[NXT_ROWID] = COLID; // update parent info
d_vertices.row_covers[NXT_ROWID] = 0;
d_vertices.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;
ptr1++;
}
}
// Device function for traversing an alternating path from unassigned row to unassigned column.
__device__ void __reverse_traversal(int *d_row_visited, int *d_row_children, int *d_col_children, int *d_row_parents, int *d_col_parents, int COLID)
{
int cur_colid = 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.
__device__ void __augment(int *d_row_assignments, int *d_col_assignments, int *d_row_children, int *d_col_children, int ROWID, int N)
{
int cur_colid = -1;
int cur_rowid = ROWID;
while (cur_rowid != -1)
{
cur_colid = d_row_children[cur_rowid];
d_row_assignments[cur_rowid] = cur_colid % N; // true colid
d_col_assignments[cur_colid] = cur_rowid % N; // true rowid
cur_rowid = d_col_children[cur_colid];
}
}
// Kernel for reducing the rows by subtracting row minimum from each row element.
__global__ void kernel_rowReduction(double *d_costs, double *d_row_duals, double *d_col_duals, int SP, int N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
double min = INF;
if (spid < SP && rowid < N)
{
for (int colid = 0; colid < N; colid++)
{
double 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.
__global__ void kernel_columnReduction(double *d_costs, double *d_row_duals, double *d_col_duals, int SP, int N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int colid = blockIdx.x * blockDim.x + threadIdx.x;
double min = INF;
if (spid < SP && colid < N)
{
for (int rowid = 0; rowid < N; rowid++)
{
double cost = d_costs[spid * N * N + rowid * N + colid];
double row_dual = d_row_duals[spid * N + rowid];
double slack = cost - row_dual;
if (slack < min)
{
min = slack;
}
}
d_col_duals[spid * N + colid] = min;
}
}
// This kernel is used to update the row duals and validate the optimality of solution.
__global__ void kernel_dynamicUpdate(int *d_row_assignments, int *d_col_assignments, double *d_row_duals, double *d_col_duals, double *d_costs, int SP, int N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
int ROWID = spid * N + rowid;
double min = INF;
if (spid < SP && rowid < N)
{
double row_dual = d_row_duals[ROWID];
for (int colid = 0; colid < N; colid++)
{
double cost = d_costs[spid * N * N + rowid * N + colid];
double col_dual = d_col_duals[spid * N + colid];
double slack = cost - row_dual - col_dual;
if (slack < min)
{
min = slack;
}
}
// Update row duals.
row_dual = (d_row_duals[ROWID] += min);
////////////////////////////////////////////////////////////////////////////////////////
// Validate optimality
int colid = d_row_assignments[ROWID];
if (colid != -1 && colid < N)
{
int COLID = spid * N + colid;
double cost = d_costs[spid * N * N + rowid * N + colid];
double col_dual = d_col_duals[COLID];
double slack = cost - row_dual - col_dual;
if (slack < -EPSILON || slack > EPSILON)
d_row_assignments[ROWID] = -1;
else
d_col_assignments[COLID] = rowid;
}
////////////////////////////////////////////////////////////////////////////////////////
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Kernel for calculating initial assignments.
__global__ void kernel_computeInitialAssignments(double *d_costs, double *d_row_duals, double *d_col_duals, int *d_row_assignments, int *d_col_assignments, int *d_row_lock, int *d_col_lock, int SP, int N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int colid = blockIdx.x * blockDim.x + threadIdx.x;
int COLID = spid * N + colid;
if (spid < SP && colid < N)
{
double col_dual = d_col_duals[COLID];
for (int rowid = 0; rowid < N; rowid++)
{
int ROWID = spid * N + rowid;
if (d_col_lock[COLID] == 1)
break;
double cost = d_costs[spid * N * N + rowid * N + colid];
double row_dual = d_row_duals[ROWID];
double slack = cost - row_dual - col_dual;
if (slack > -EPSILON && slack < EPSILON)
{
if (atomicCAS(&d_row_lock[ROWID], 0, 1) == 0)
{
d_row_assignments[ROWID] = colid;
d_col_assignments[COLID] = rowid;
d_col_lock[COLID] = 1;
}
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Kernel for populating the cover arrays and initializing alternating tree.
__global__ void kernel_computeRowCovers(int *d_row_assignments, int *d_row_covers, int *d_row_visited, int SP, int N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
int ROWID = spid * N + rowid;
// Copy the predicate matrix back to global memory
if (spid < SP && rowid < N)
{
if (d_row_assignments[ROWID] != -1)
{
d_row_covers[ROWID] = 1;
}
else
{
d_row_visited[ROWID] = ACTIVE;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Kernel for populating the predicate matrix for edges in row major format.
__global__ void kernel_rowPredicateConstructionCSR(Predicates d_row_predicates_csr, int *d_row_visited, int SP, int N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
int ROWID = spid * N + rowid;
if (spid < SP && rowid < N)
{
int row_visited = d_row_visited[ROWID];
bool predicate = (row_visited == ACTIVE);
long addr = predicate ? 1 : 0;
d_row_predicates_csr.predicates[ROWID] = predicate; // Copy the predicate matrix back to global memory
d_row_predicates_csr.addresses[ROWID] = addr;
}
}
// Kernel for scattering the edges based on the scatter addresses.
__global__ void kernel_rowScatterCSR(CompactEdges d_row_vertices_csr, Predicates d_row_predicates_csr, long M, int SP, int N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int rowid = blockIdx.x * blockDim.x + threadIdx.x;
int ROWID = spid * N + rowid;
// Copy the matrix into shared memory
if (spid < SP && rowid < N)
{
bool predicate = d_row_predicates_csr.predicates[ROWID];
long compid = d_row_predicates_csr.addresses[ROWID];
if (predicate)
{
d_row_vertices_csr.neighbors[compid] = rowid;
}
if (rowid == 0)
{
d_row_vertices_csr.ptrs[spid] = compid;
d_row_vertices_csr.ptrs[SP] = M; // extra pointer for the total number of edges. necessary for calculating number of edges in each row.
}
}
}
// Kernel for finding the minimum zero cover.
__global__ void kernel_coverAndExpand(bool *d_flag, CompactEdges d_row_vertices_csr, Matrix d_costs, Vertices d_vertices, VertexData d_row_data, VertexData d_col_data, int SP, int N)
{
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)
{
int in_size = d_row_vertices_csr.ptrs[spid + 1] - d_row_vertices_csr.ptrs[spid];
int nbr_start = d_row_vertices_csr.ptrs[spid];
int *st_ptr = &d_row_vertices_csr.neighbors[nbr_start];
int *end_ptr = &d_row_vertices_csr.neighbors[nbr_start + in_size];
__traverse(d_costs, d_vertices, d_flag, d_row_data.parents, d_col_data.parents, d_row_data.is_visited, d_col_data.is_visited, st_ptr, end_ptr, spid, colid, SP, N);
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Kernel for constructing the predicates for reverse pass or augmentation candidates.
__global__ void kernel_augmentPredicateConstruction(Predicates d_predicates, int *d_visited, int size)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
// Copy the matrix into shared memory.
if (id < size)
{
int visited = d_visited[id];
bool predicate = (visited == REVERSE || visited == AUGMENT);
long addr = predicate ? 1 : 0;
d_predicates.predicates[id] = predicate;
d_predicates.addresses[id] = addr;
}
}
// Kernel for scattering the vertices based on the scatter addresses.
__global__ void kernel_augmentScatter(Array d_vertex_ids, Predicates d_predicates, int size)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size)
{
bool predicate = d_predicates.predicates[id];
long compid = (predicate) ? d_predicates.addresses[id] : -1; // compaction id.
if (predicate)
d_vertex_ids.elements[compid] = id;
}
}
// Kernel for executing the reverse pass of the maximum matching algorithm.
__global__ void kernel_reverseTraversal(Array d_col_vertices, VertexData d_row_data, VertexData d_col_data)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
int size = d_col_vertices.size;
if (id < size)
{
int COLID = d_col_vertices.elements[id];
__reverse_traversal(d_row_data.is_visited, d_row_data.children, d_col_data.children, d_row_data.parents, d_col_data.parents, COLID);
}
}
// Kernel for executing the augmentation pass of the maximum matching algorithm.
__global__ void kernel_augmentation(int *d_row_assignments, int *d_col_assignments, Array d_row_vertices, VertexData d_row_data, VertexData d_col_data, int N)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
int size = d_row_vertices.size;
if (id < size)
{
int ROWID = d_row_vertices.elements[id];
__augment(d_row_assignments, d_col_assignments, d_row_data.children, d_col_data.children, ROWID, N);
}
}
// Kernel for updating the dual values in Step 5.
__global__ void kernel_dualUpdate_1(double *d_sp_min, double *d_col_slacks, int *d_col_covers, int SP, int N)
{
int spid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP)
{
double min = INF;
for (int colid = 0; colid < N; colid++)
{
int COLID = spid * N + colid;
double slack = d_col_slacks[COLID];
int col_cover = d_col_covers[COLID];
if (col_cover == 0)
if (slack < min)
min = slack;
}
d_sp_min[spid] = min;
}
}
// Kernel for updating the dual values in Step 5.
__global__ void kernel_dualUpdate_2(double *d_sp_min, double *d_row_duals, double *d_col_duals, double *d_col_slacks, int *d_row_covers, int *d_col_covers, int *d_row_visited, int *d_col_parents, int SP, int N)
{
int spid = blockIdx.y * blockDim.y + threadIdx.y;
int id = blockIdx.x * blockDim.x + threadIdx.x;
int ID = spid * N + id;
if (spid < SP && id < N)
{
if (d_sp_min[spid] < INF3)
{
// double theta = 0.5 * d_sp_min[spid];
double theta = d_sp_min[spid];
int row_cover = d_row_covers[ID];
int col_cover = d_col_covers[ID];
if (row_cover == 0) // Row vertex is reachable from source.
d_row_duals[ID] += theta;
// else
// // Row vertex is unreachable from source.
// d_row_duals[ID] -= theta;
if (col_cover == 1) // Col vertex is reachable from source.
d_col_duals[ID] -= theta;
else
{
// Col vertex is unreachable from source.
// d_col_duals[ID] += theta;
d_col_slacks[ID] -= d_sp_min[spid];
if (d_col_slacks[ID] > -EPSILON && d_col_slacks[ID] < EPSILON)
{
int PAR_ROWID = d_col_parents[ID];
if (PAR_ROWID != -1)
d_row_visited[PAR_ROWID] = ACTIVE;
}
}
}
}
}
// Kernel for calculating optimal objective function value using dual variables.
__global__ void kernel_calcObjVal(double *d_obj_val, double *d_row_duals, double *d_col_duals, int SP, int N)
{
int spid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP)
{
d_obj_val[spid] = 0;
for (int i = 0; i < N; i++)
d_obj_val[spid] += (d_row_duals[spid * N + i] + d_col_duals[spid * N + i]);
}
}
// Kernel for calculating optimal objective function value using dual variables.
__global__ void kernel_calcObjVal2(double *d_obj_val, double *d_costs, int *d_row_assignments, int SP, int N)
{
int spid = blockIdx.x * blockDim.x + threadIdx.x;
if (spid < SP)
{
d_obj_val[spid] = 0;
for (int i = 0; i < N; i++)
{
int j = d_row_assignments[spid * N + i];
d_obj_val[spid] += d_costs[spid * N * N + i * N + j];
}
}
}
// This function is used to perform initial reduction.
void initialReduction(Matrix &d_costs, Vertices &d_vertices_dev, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
dim3 blocks_per_grid;
dim3 threads_per_block;
int total_blocks = 0;
calculateRectangularDims(blocks_per_grid, threads_per_block, total_blocks, N, SP);
kernel_rowReduction<<<blocks_per_grid, threads_per_block>>>(d_costs.elements, d_vertices_dev.row_duals, d_vertices_dev.col_duals, SP, N); // Kernel execution.
cudaSafeCall(cudaGetLastError(), "Error in kernel_rowReduction execution f_culap::initialReduction");
kernel_columnReduction<<<blocks_per_grid, threads_per_block>>>(d_costs.elements, d_vertices_dev.row_duals, d_vertices_dev.col_duals, SP, N); // Kernel execution.
cudaSafeCall(cudaGetLastError(), "Error in kernel_colReduction execution f_culap::initialReduction");
}
// This function is used to validate the optimality of the previous solution after cost update.
void dynamicUpdate(Matrix &d_costs, Vertices &d_vertices_dev, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
dim3 blocks_per_grid;
dim3 threads_per_block;
int total_blocks = 0;
calculateRectangularDims(blocks_per_grid, threads_per_block, total_blocks, N, SP);
kernel_dynamicUpdate<<<blocks_per_grid, threads_per_block>>>(d_vertices_dev.row_assignments, d_vertices_dev.col_assignments, d_vertices_dev.row_duals, d_vertices_dev.col_duals, d_costs.elements, SP, N); // Kernel execution.
cudaSafeCall(cudaGetLastError(), "Error in kernel_dynamicUpdate execution f_culap::dynamicUpdate");
}
////////////////////////////////////////////////////////////////////////////////////////
// Function for calculating initial assignments on individual cards and stitcing them together on host.
void computeInitialAssignments(Matrix &d_costs, Vertices &d_vertices_dev, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
dim3 blocks_per_grid;
dim3 threads_per_block;
int total_blocks = 0;
calculateLinearDims(blocks_per_grid, threads_per_block, total_blocks, N * SP);
cudaSafeCall(cudaMemset(d_vertices_dev.row_assignments, -1, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeInitialAssignments::d_row_assignment");
cudaSafeCall(cudaMemset(d_vertices_dev.col_assignments, -1, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeInitialAssignments::d_col_assignment");
int *d_row_lock, *d_col_lock;
cudaSafeCall(cudaMalloc((void **)&d_row_lock, SP * N * sizeof(int)), "Error in cudaMalloc f_culap::computeInitialAssignments::d_row_lock");
cudaSafeCall(cudaMalloc((void **)&d_col_lock, SP * N * sizeof(int)), "Error in cudaMalloc f_culap::computeInitialAssignments::d_col_lock");
cudaSafeCall(cudaMemset(d_row_lock, 0, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeInitialAssignments::d_row_lock");
cudaSafeCall(cudaMemset(d_col_lock, 0, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeInitialAssignments::d_col_lock");
calculateRectangularDims(blocks_per_grid, threads_per_block, total_blocks, N, SP);
kernel_computeInitialAssignments<<<blocks_per_grid, threads_per_block>>>(d_costs.elements, d_vertices_dev.row_duals, d_vertices_dev.col_duals, d_vertices_dev.row_assignments, d_vertices_dev.col_assignments, d_row_lock, d_col_lock, SP, N); // Kernel execution.
cudaSafeCall(cudaGetLastError(), "Error in kernel_computeInitialAssignments execution f_culap::computeInitialAssignments");
cudaSafeCall(cudaFree(d_row_lock), "Error in cudaFree f_culap::computeInitialAssignments::d_row_lock");
cudaSafeCall(cudaFree(d_col_lock), "Error in cudaFree f_culap::computeInitialAssignments::d_col_lock");
}
////////////////////////////////////////////////////////////////////////////////////////
// Function for finding row cover on individual devices.
int computeRowCovers(Vertices &d_vertices_dev, VertexData &d_row_data_dev, VertexData &d_col_data_dev, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
dim3 blocks_per_grid;
dim3 threads_per_block;
int total_blocks = 0;
cudaSafeCall(cudaMemset(d_vertices_dev.row_covers, 0, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeRowCovers::d_row_covers");
cudaSafeCall(cudaMemset(d_vertices_dev.col_covers, 0, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeRowCovers::d_col_covers");
calculateLinearDims(blocks_per_grid, threads_per_block, total_blocks, SP * N);
kernel_memSet<<<blocks_per_grid, threads_per_block>>>(d_vertices_dev.col_slacks, INF, SP * N);
cudaSafeCall(cudaMemset(d_row_data_dev.is_visited, DORMANT, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeRowCovers::d_row_data.is_visited");
cudaSafeCall(cudaMemset(d_col_data_dev.is_visited, DORMANT, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeRowCovers::d_col_data.is_visited"); // initialize "visited" array for columns. later used in BFS (Step 4).
cudaSafeCall(cudaMemset(d_row_data_dev.parents, -1, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeRowCovers::d_row_data.parents");
cudaSafeCall(cudaMemset(d_row_data_dev.children, -1, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeRowCovers::d_row_data.children");
cudaSafeCall(cudaMemset(d_col_data_dev.parents, -1, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeRowCovers::d_col_data.parents");
cudaSafeCall(cudaMemset(d_col_data_dev.children, -1, SP * N * sizeof(int)), "Error in cudaMemset f_culap::computeRowCovers::d_col_data.children");
calculateRectangularDims(blocks_per_grid, threads_per_block, total_blocks, N, SP);
kernel_computeRowCovers<<<blocks_per_grid, threads_per_block>>>(d_vertices_dev.row_assignments, d_vertices_dev.row_covers, d_row_data_dev.is_visited, SP, N); // Kernel execution.
cudaSafeCall(cudaGetLastError(), "Error in kernel_computeRowCovers execution f_culap::computeRowCovers");
thrust::device_ptr<int> ptr(d_vertices_dev.row_covers);
int cover_count = thrust::reduce(ptr, ptr + SP * N);
return cover_count;
}
// Function for compacting the edges in row major format.
void compactRowVertices(CompactEdges &d_rows_csr_dev, VertexData &d_row_data_dev, long &M, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
dim3 blocks_per_grid;
dim3 threads_per_block;
int total_blocks = 0;
Predicates d_row_predicates_csr;
d_row_predicates_csr.size = SP * N;
cudaSafeCall(cudaMalloc((void **)(&d_row_predicates_csr.predicates), d_row_predicates_csr.size * sizeof(bool)), "Error in cudaMalloc f_culap::compactRowVertices::d_row_predicates_csr.predicates");
cudaSafeCall(cudaMalloc((void **)(&d_row_predicates_csr.addresses), d_row_predicates_csr.size * sizeof(long)), "Error in cudaMalloc f_culap::compactRowVertices::d_row_predicates_csr.addresses");
cudaSafeCall(cudaMemset(d_row_predicates_csr.predicates, false, d_row_predicates_csr.size * sizeof(bool)), "Error in cudaMemset f_culap::compactRowVertices::d_row_predicates_csr.predicates");
cudaSafeCall(cudaMemset(d_row_predicates_csr.addresses, 0, d_row_predicates_csr.size * sizeof(long)), "Error in cudaMemset f_culap::compactRowVertices::d_row_predicates_csr.addresses");
cudaSafeCall(cudaMalloc((void **)(&d_rows_csr_dev.ptrs), (SP + 1) * sizeof(long)), "Error in cudaMalloc f_culap::compactRowVertices::d_rows_csr_dev.ptrs");
cudaSafeCall(cudaMemset(d_rows_csr_dev.ptrs, -1, (SP + 1) * sizeof(long)), "Error in cudaMemset f_culap::compactRowVertices::d_rows_csr_dev.ptrs");
calculateRectangularDims(blocks_per_grid, threads_per_block, total_blocks, N, SP);
kernel_rowPredicateConstructionCSR<<<blocks_per_grid, threads_per_block>>>(d_row_predicates_csr, d_row_data_dev.is_visited, SP, N); // construct predicate matrix for edges.
cudaSafeCall(cudaGetLastError(), "Error in kernel_edgePredicateConstructionCSR execution f_culap::compactRowVertices");
thrust::device_ptr<long> ptr(d_row_predicates_csr.addresses);
M = thrust::reduce(ptr, ptr + d_row_predicates_csr.size); // calculate total number of edges.
thrust::exclusive_scan(ptr, ptr + d_row_predicates_csr.size, ptr); // exclusive scan for calculating the scatter addresses.
if (M > 0)
{
cudaSafeCall(cudaMalloc((void **)(&d_rows_csr_dev.neighbors), M * sizeof(int)), "Error in cudaMalloc f_culap::compactRowVertices::d_rows_csr_dev.neighbors");
kernel_rowScatterCSR<<<blocks_per_grid, threads_per_block>>>(d_rows_csr_dev, d_row_predicates_csr, M, SP, N);
cudaSafeCall(cudaGetLastError(), "Error in kernel_edgeScatterCSR execution f_culap::compactRowVertices");
}
cudaSafeCall(cudaFree(d_row_predicates_csr.predicates), "Error in cudaFree f_culap::compactRowVertices::d_row_predicates_csr.predicates");
cudaSafeCall(cudaFree(d_row_predicates_csr.addresses), "Error in cudaFree f_culap::compactRowVertices::d_row_predicates_csr.addresses");
}
// Function for covering the zeros in uncovered rows and expanding the frontier.
void coverZeroAndExpand(Matrix &d_costs_dev, CompactEdges &d_rows_csr_dev, Vertices &d_vertices_dev, VertexData &d_row_data_dev, VertexData &d_col_data_dev, bool *h_flag, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
int total_blocks = 0;
dim3 blocks_per_grid;
dim3 threads_per_block;
calculateRectangularDims(blocks_per_grid, threads_per_block, total_blocks, N, SP);
bool *d_flag;
cudaSafeCall(cudaMalloc((void **)&d_flag, sizeof(bool)), "Error in cudaMalloc d_flag");
cudaSafeCall(cudaMemcpy(d_flag, h_flag, sizeof(bool), cudaMemcpyHostToDevice), "Error in cudaMemcpy h_flag");
kernel_coverAndExpand<<<blocks_per_grid, threads_per_block>>>(d_flag, d_rows_csr_dev, d_costs_dev, d_vertices_dev, d_row_data_dev, d_col_data_dev, SP, N);
cudaSafeCall(cudaMemcpy(h_flag, d_flag, sizeof(bool), cudaMemcpyDeviceToHost), "Error in cudaMemcpy d_next");
cudaSafeCall(cudaFree(d_flag), "Error in cudaFree d_next");
}
////////////////////////////////////////////////////////////////////////////////////////
// Function for executing recursive zero cover. Returns the next step (Step 4 or Step 5) depending on the presence of uncovered zeros.
void executeZeroCover(Matrix &d_costs_dev, Vertices &d_vertices_dev, VertexData &d_row_data_dev, VertexData &d_col_data_dev, bool *h_flag, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
CompactEdges d_rows_csr_dev;
while (true)
{
long M = 0;
compactRowVertices(d_rows_csr_dev, d_row_data_dev, M, SP, N, devid); // compact the current vertex frontier.
if (M > 0)
{
coverZeroAndExpand(d_costs_dev, d_rows_csr_dev, d_vertices_dev, d_row_data_dev, d_col_data_dev, h_flag, SP, N, devid);
cudaSafeCall(cudaFree(d_rows_csr_dev.neighbors), "Error in cudaFree f_culap::executeZeroCover::d_edges_csr.neighbors");
cudaSafeCall(cudaFree(d_rows_csr_dev.ptrs), "Error in cudaFree f_culap::executeZeroCover::d_edges_csr_dev.ptrs");
}
else
{
cudaSafeCall(cudaFree(d_rows_csr_dev.ptrs), "Error in cudaFree f_culap::executeZeroCover::d_edges_csr_dev.ptrs");
break;
}
}
}
// Function for executing reverse pass of the maximum matching.
void reversePass(VertexData &d_row_data_dev, VertexData &d_col_data_dev, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
int total_blocks = 0;
dim3 blocks_per_grid;
dim3 threads_per_block;
calculateLinearDims(blocks_per_grid, threads_per_block, total_blocks, SP * N);
Array d_col_ids_csr;
Predicates d_col_predicates; // predicates for compacting the colids eligible for the reverse pass.
d_col_predicates.size = SP * N;
cudaSafeCall(cudaMalloc((void **)(&d_col_predicates.predicates), d_col_predicates.size * sizeof(bool)), "Error in cudaMalloc f_culap::reversePass::d_col_predicates.predicates");
cudaSafeCall(cudaMalloc((void **)(&d_col_predicates.addresses), d_col_predicates.size * sizeof(long)), "Error in cudaMalloc f_culap::reversePass::d_col_predicates.addresses");
cudaSafeCall(cudaMemset(d_col_predicates.predicates, false, d_col_predicates.size * sizeof(bool)), "Error in cudaMemset f_culap::reversePass::d_col_predicates.predicates");
cudaSafeCall(cudaMemset(d_col_predicates.addresses, 0, d_col_predicates.size * sizeof(long)), "Error in cudaMemset f_culap::reversePass::d_col_predicates.addresses");
// compact the reverse pass row vertices.
kernel_augmentPredicateConstruction<<<blocks_per_grid, threads_per_block>>>(d_col_predicates, d_col_data_dev.is_visited, d_col_predicates.size);
cudaSafeCall(cudaGetLastError(), "Error in kernel_augmentPredicateConstruction f_culap::reversePass");
thrust::device_ptr<long> ptr(d_col_predicates.addresses);
d_col_ids_csr.size = thrust::reduce(ptr, ptr + d_col_predicates.size); // calculate total number of vertices.
thrust::exclusive_scan(ptr, ptr + d_col_predicates.size, ptr); // exclusive scan for calculating the scatter addresses.
if (d_col_ids_csr.size > 0)
{
int total_blocks_1 = 0;
dim3 blocks_per_grid_1;
dim3 threads_per_block_1;
calculateLinearDims(blocks_per_grid_1, threads_per_block_1, total_blocks_1, d_col_ids_csr.size);
cudaSafeCall(cudaMalloc((void **)(&d_col_ids_csr.elements), d_col_ids_csr.size * sizeof(int)), "Error in cudaMalloc f_culap::reversePass::d_col_ids_csr.elements");
kernel_augmentScatter<<<blocks_per_grid, threads_per_block>>>(d_col_ids_csr, d_col_predicates, d_col_predicates.size);
cudaSafeCall(cudaGetLastError(), "Error in kernel_augmentScatter f_culap::reversePass");
kernel_reverseTraversal<<<blocks_per_grid_1, threads_per_block_1>>>(d_col_ids_csr, d_row_data_dev, d_col_data_dev);
cudaSafeCall(cudaGetLastError(), "Error in kernel_reverseTraversal f_culap::reversePass");
cudaSafeCall(cudaFree(d_col_ids_csr.elements), "Error in cudaFree f_culap::reversePass::d_col_ids_csr.elements");
}
cudaSafeCall(cudaFree(d_col_predicates.predicates), "Error in cudaFree f_culap::reversePass::d_col_predicates.predicates");
cudaSafeCall(cudaFree(d_col_predicates.addresses), "Error in cudaFree f_culap::reversePass::d_col_predicates.addresses");
}
// Function for executing augmentation pass of the maximum matching.
void augmentationPass(Vertices &d_vertices_dev, VertexData &d_row_data_dev, VertexData &d_col_data_dev, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
int total_blocks = 0;
dim3 blocks_per_grid;
dim3 threads_per_block;
calculateLinearDims(blocks_per_grid, threads_per_block, total_blocks, SP * N);
Array d_row_ids_csr;
Predicates d_row_predicates; // predicates for compacting the colids eligible for the augmentation pass.
d_row_predicates.size = SP * N;
cudaSafeCall(cudaMalloc((void **)(&d_row_predicates.predicates), d_row_predicates.size * sizeof(bool)), "Error in cudaMalloc f_culap::augmentationPass::d_row_predicates.predicates");
cudaSafeCall(cudaMalloc((void **)(&d_row_predicates.addresses), d_row_predicates.size * sizeof(long)), "Error in cudaMalloc f_culap::augmentationPass::d_row_predicates.addresses");
cudaSafeCall(cudaMemset(d_row_predicates.predicates, false, d_row_predicates.size * sizeof(bool)), "Error in cudaMemset f_culap::augmentationPass::d_row_predicates.predicates");
cudaSafeCall(cudaMemset(d_row_predicates.addresses, 0, d_row_predicates.size * sizeof(long)), "Error in cudaMemset f_culap::augmentationPass::d_row_predicates.addresses");
// compact the reverse pass row vertices.
kernel_augmentPredicateConstruction<<<blocks_per_grid, threads_per_block>>>(d_row_predicates, d_row_data_dev.is_visited, d_row_predicates.size);
cudaSafeCall(cudaGetLastError(), "Error in kernel_augmentPredicateConstruction f_culap::augmentationPass");
thrust::device_ptr<long> ptr(d_row_predicates.addresses);
d_row_ids_csr.size = thrust::reduce(ptr, ptr + d_row_predicates.size); // calculate total number of vertices.
thrust::exclusive_scan(ptr, ptr + d_row_predicates.size, ptr); // exclusive scan for calculating the scatter addresses.
if (d_row_ids_csr.size > 0)
{
int total_blocks_1 = 0;
dim3 blocks_per_grid_1;
dim3 threads_per_block_1;
calculateLinearDims(blocks_per_grid_1, threads_per_block_1, total_blocks_1, d_row_ids_csr.size);
cudaSafeCall(cudaMalloc((void **)(&d_row_ids_csr.elements), d_row_ids_csr.size * sizeof(int)), "Error in cudaMalloc f_culap::augmentationPass::d_row_ids_csr.elements");
kernel_augmentScatter<<<blocks_per_grid, threads_per_block>>>(d_row_ids_csr, d_row_predicates, d_row_predicates.size);
cudaSafeCall(cudaGetLastError(), "Error in kernel_augmentScatter f_culap::augmentationPass");
kernel_augmentation<<<blocks_per_grid_1, threads_per_block_1>>>(d_vertices_dev.row_assignments, d_vertices_dev.col_assignments, d_row_ids_csr, d_row_data_dev, d_col_data_dev, N);
cudaSafeCall(cudaGetLastError(), "Error in kernel_augmentation f_culap::augmentationPass");
cudaSafeCall(cudaFree(d_row_ids_csr.elements), "Error in cudaFree f_culap::augmentationPass::d_row_ids_csr.elements");
}
cudaSafeCall(cudaFree(d_row_predicates.predicates), "Error in cudaFree f_culap::augmentationPass::d_row_predicates.predicates");
cudaSafeCall(cudaFree(d_row_predicates.addresses), "Error in cudaFree f_culap::augmentationPass::d_row_predicates.addresses");
}
void dualUpdate(Vertices &d_vertices_dev, VertexData &d_row_data_dev, VertexData &d_col_data_dev, int SP, int N, unsigned int devid)
{
dim3 blocks_per_grid;
dim3 threads_per_block;
int total_blocks;
double *d_sp_min;
cudaSafeCall(cudaMalloc((void **)(&d_sp_min), SP * sizeof(double)), "Error in cudaMalloc f_culap::dualUpdate::d_sp_min");
calculateLinearDims(blocks_per_grid, threads_per_block, total_blocks, SP);
kernel_dualUpdate_1<<<blocks_per_grid, threads_per_block>>>(d_sp_min, d_vertices_dev.col_slacks, d_vertices_dev.col_covers, SP, N);
cudaSafeCall(cudaGetLastError(), "Error in kernel_augmentation f_culap::kernel_dualUpdate_1");
calculateRectangularDims(blocks_per_grid, threads_per_block, total_blocks, N, SP);
kernel_dualUpdate_2<<<blocks_per_grid, threads_per_block>>>(d_sp_min, d_vertices_dev.row_duals, d_vertices_dev.col_duals, d_vertices_dev.col_slacks, d_vertices_dev.row_covers, d_vertices_dev.col_covers, d_row_data_dev.is_visited, d_col_data_dev.parents, SP, N);
cudaSafeCall(cudaGetLastError(), "Error in kernel_augmentation f_culap::kernel_dualUpdate_2");
cudaSafeCall(cudaFree(d_sp_min), "Error in cudaFree f_culap::dualUpdate::d_sp_min");
}
////////////////////////////////////////////////////////////////////////////////////////
// Function for calculating optimal objective function value using dual variables.
void calcObjVal(double *d_obj_val, Vertices &d_vertices_dev, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
dim3 blocks_per_grid;
dim3 threads_per_block;
int total_blocks = 0;
calculateLinearDims(blocks_per_grid, threads_per_block, total_blocks, SP);
kernel_calcObjVal<<<blocks_per_grid, threads_per_block>>>(d_obj_val, d_vertices_dev.row_duals, d_vertices_dev.col_duals, SP, N);
cudaSafeCall(cudaGetLastError(), "Error in kernel_calcObjVal execution f_culap::calcObjVal");
}
// Function for calculating optimal objective function value using dual variables.
void calcObjVal2(double *d_obj_val, double *d_costs, Vertices &d_vertices_dev, int SP, int N, unsigned int devid)
{
cudaSetDevice(devid);
dim3 blocks_per_grid;
dim3 threads_per_block;
int total_blocks = 0;
calculateLinearDims(blocks_per_grid, threads_per_block, total_blocks, SP);
kernel_calcObjVal2<<<blocks_per_grid, threads_per_block>>>(d_obj_val, d_costs, d_vertices_dev.row_assignments, SP, N);
cudaSafeCall(cudaGetLastError(), "Error in kernel_calcObjVal execution f_culap::calcObjVal");
}