-
Notifications
You must be signed in to change notification settings - Fork 0
/
culap.cuh
391 lines (275 loc) · 11.2 KB
/
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
#pragma once
#include <omp.h>
#include "f_culap.cuh"
#include "d_structs.h"
#include "d_vars.h"
class CuLAP
{
int N;
long N2;
long M; // total number of zero cost edges on a single host.
int SP;
int devid;
int initial_assignment_count;
int *stepcounts;
double *steptimes;
int prevstep;
bool flag;
bool dynamic;
double *d_obj_val_dev;
Matrix d_costs_dev;
Vertices d_vertices_dev;
CompactEdges d_edges_csr_dev;
VertexData d_row_data_dev, d_col_data_dev;
public:
CuLAP(int _size, int _spcount, int _devid, bool _is_dynamic);
virtual ~CuLAP();
int solve(double *d_cost_matrix, int *d_row_assignments, double *d_row_duals, double *d_col_duals, double *d_obj_val);
void getAssignments(int *_row_assignments);
void getStepTimes(double *_steptimes);
void getStepCounts(int *_stepcounts);
private:
void initializeDevice(void);
void finalizeDevice(void);
int hungarianStep0(bool count_time);
int hungarianStep1(bool count_time);
int hungarianStep2(bool count_time);
int hungarianStep3(bool count_time);
int hungarianStep4(bool count_time);
int hungarianStep5(bool count_time);
int hungarianStep6(bool count_time);
};
CuLAP::CuLAP(int _size, int _spcount, int _devid, bool _is_dynamic)
{
N = _size;
N2 = N * N;
SP = _spcount;
devid = _devid;
dynamic = _is_dynamic;
M = 0;
prevstep = 0;
flag = false;
initial_assignment_count = 0;
stepcounts = new int[7];
steptimes = new double[9];
d_obj_val_dev = 0;
}
CuLAP::~CuLAP()
{
delete[] stepcounts;
delete[] steptimes;
}
// Helper function for initializing global variables and arrays on a single host.
void CuLAP::initializeDevice(void)
{
cudaSetDevice(devid);
// cudaSafeCall(cudaMalloc((void**) (&d_vertices_dev.row_assignments), SP * N * sizeof(int)), "error in cudaMalloc CuLAP::initializeDevice::d_row_assignment");
cudaSafeCall(cudaMalloc((void **)(&d_vertices_dev.col_assignments), SP * N * sizeof(int)), "error in cudaMalloc CuLAP::initializeDevice::d_col_assignment");
cudaSafeCall(cudaMalloc((void **)(&d_vertices_dev.row_covers), SP * N * sizeof(int)), "error in cudaMalloc CuLAP::initializeDevice::d_row_covers");
cudaSafeCall(cudaMalloc((void **)(&d_vertices_dev.col_covers), SP * N * sizeof(int)), "error in cudaMalloc CuLAP::initializeDevice::d_col_covers");
// cudaSafeCall(cudaMalloc((void**) (&d_vertices_dev.row_duals), SP * N * sizeof(double)), "error in cudaMalloc CuLAP::initializeDevice::d_row_duals");
// cudaSafeCall(cudaMalloc((void**) (&d_vertices_dev.col_duals), SP * N * sizeof(double)), "error in cudaMalloc CuLAP::initializeDevice::d_col_duals");
cudaSafeCall(cudaMalloc((void **)(&d_vertices_dev.col_slacks), SP * N * sizeof(double)), "error in cudaMalloc CuLAP::initializeDevice::d_col_slacks");
//
cudaSafeCall(cudaMemset(d_vertices_dev.row_assignments, -1, SP * N * sizeof(int)), "Error in cudaMemset d_row_assignment");
cudaSafeCall(cudaMemset(d_vertices_dev.col_assignments, -1, SP * N * sizeof(int)), "Error in cudaMemset CuLAP::initializeDevice::d_col_assignment");
cudaSafeCall(cudaMemset(d_vertices_dev.row_covers, 0, SP * N * sizeof(int)), "Error in cudaMemset CuLAP::initializeDevice::d_row_covers");
cudaSafeCall(cudaMemset(d_vertices_dev.col_covers, 0, SP * N * sizeof(int)), "Error in cudaMemset CuLAP::initializeDevice::d_col_covers");
cudaSafeCall(cudaMemset(d_vertices_dev.row_duals, 0, SP * N * sizeof(double)), "Error in cudaMemset CuLAP::initializeDevice::d_row_duals");
cudaSafeCall(cudaMemset(d_vertices_dev.col_duals, 0, SP * N * sizeof(double)), "Error in cudaMemset CuLAP::initializeDevice::d_col_duals");
// cudaSafeCall(cudaMemset(d_vertices_dev.col_slacks, INF, SP * N * sizeof(double)), "Error in cudaMemset CuLAP::initializeDevice::d_col_duals");
cudaSafeCall(cudaMalloc((void **)(&d_row_data_dev.is_visited), SP * N * sizeof(int)), "Error in cudaMalloc CuLAP::initializeDevice::d_row_data.is_visited");
cudaSafeCall(cudaMalloc((void **)(&d_col_data_dev.is_visited), SP * N * sizeof(int)), "Error in cudaMalloc CuLAP::initializeDevice::d_col_data.is_visited");
cudaSafeCall(cudaMalloc((void **)(&d_row_data_dev.parents), SP * N * sizeof(int)), "Error in cudaMalloc CuLAP::initializeDevice::d_row_data.parents");
cudaSafeCall(cudaMalloc((void **)(&d_row_data_dev.children), SP * N * sizeof(int)), "Error in cudaMalloc CuLAP::initializeDevice::d_row_data.children");
cudaSafeCall(cudaMalloc((void **)(&d_col_data_dev.parents), SP * N * sizeof(int)), "Error in cudaMalloc CuLAP::initializeDevice::d_col_data.parents");
cudaSafeCall(cudaMalloc((void **)(&d_col_data_dev.children), SP * N * sizeof(int)), "Error in cudaMalloc CuLAP::initializeDevice::d_col_data.children");
// cudaSafeCall(cudaMalloc((void**) (&d_costs_dev.elements), SP * N * N * sizeof(double)), "error in cudaMalloc CuLAP::initializeDevice::d_costs");
}
// Helper function for finalizing global variables and arrays on a single host.
void CuLAP::finalizeDevice(void)
{
cudaSetDevice(devid);
// cudaSafeCall(cudaFree(d_vertices_dev.row_assignments), "Error in cudaFree CuLAP::finalizeDevice::d_row_assignment");
cudaSafeCall(cudaFree(d_vertices_dev.col_assignments), "Error in cudaFree CuLAP::finalizeDevice::d_col_assignment");
cudaSafeCall(cudaFree(d_vertices_dev.row_covers), "Error in cudaFree CuLAP::finalizeDevice::d_row_covers");
cudaSafeCall(cudaFree(d_vertices_dev.col_covers), "Error in cudaFree CuLAP::finalizeDevice::d_col_covers");
// cudaSafeCall(cudaFree(d_vertices_dev.row_duals), "Error in cudaFree CuLAP::finalizeDevice::d_row_duals");
// cudaSafeCall(cudaFree(d_vertices_dev.col_duals), "Error in cudaFree CuLAP::finalizeDevice::d_col_duals");
cudaSafeCall(cudaFree(d_vertices_dev.col_slacks), "Error in cudaFree CuLAP::finalizeDevice::d_col_slacks");
cudaSafeCall(cudaFree(d_row_data_dev.is_visited), "Error in cudaFree CuLAP::finalizeDevice::d_row_data.is_visited");
cudaSafeCall(cudaFree(d_col_data_dev.is_visited), "Error in cudaFree CuLAP::finalizeDevice::d_col_data.is_visited");
cudaSafeCall(cudaFree(d_row_data_dev.parents), "Error in cudaFree CuLAP::finalizeDevice::d_row_data.parents");
cudaSafeCall(cudaFree(d_row_data_dev.children), "Error in cudaFree CuLAP::finalizeDevice::d_row_data.children");
cudaSafeCall(cudaFree(d_col_data_dev.parents), "Error in cudaFree CuLAP::finalizeDevice::d_col_data.parents");
cudaSafeCall(cudaFree(d_col_data_dev.children), "Error in cudaFree CuLAP::finalizeDevice::d_col_data.children");
cudaSafeCall(cudaDeviceSynchronize(), " ");
// cudaSafeCall(cudaFree(d_costs_dev.elements), "error in cudaFree CuLAP::finalizeDevice::d_costs");
}
// Executes Hungarian algorithm on the input cost matrix. Returns minimum cost.
int CuLAP::solve(double *d_cost_matrix, int *d_row_assignments, double *d_row_duals, double *d_col_duals, double *d_obj_val)
{
d_costs_dev.elements = d_cost_matrix;
d_vertices_dev.row_assignments = d_row_assignments;
d_vertices_dev.row_duals = d_row_duals;
d_vertices_dev.col_duals = d_col_duals;
d_obj_val_dev = d_obj_val;
initializeDevice();
int step = 0;
int total_count = 0;
bool done = false;
prevstep = -1;
std::fill(stepcounts, stepcounts + 7, 0);
std::fill(steptimes, steptimes + 9, 0);
while (!done)
{
total_count++;
switch (step)
{
case 0:
stepcounts[0]++;
step = hungarianStep0(true);
break;
case 1:
stepcounts[1]++;
step = hungarianStep1(true);
break;
case 2:
stepcounts[2]++;
step = hungarianStep2(true);
break;
case 3:
stepcounts[3]++;
step = hungarianStep3(true);
break;
case 4:
stepcounts[4]++;
step = hungarianStep4(true);
break;
case 5:
stepcounts[5]++;
step = hungarianStep5(true);
break;
case 6:
stepcounts[6]++;
step = hungarianStep6(true);
break;
case 100:
done = true;
break;
}
}
finalizeDevice();
return 0;
}
// Function for calculating initial zeros by subtracting row and column minima from each element.
int CuLAP::hungarianStep0(bool count_time)
{
double start = omp_get_wtime();
if (dynamic)
dynamicUpdate(d_costs_dev, d_vertices_dev, SP, N, devid);
else
initialReduction(d_costs_dev, d_vertices_dev, SP, N, devid);
int next = (dynamic) ? 2 : 1;
double end = omp_get_wtime();
if (count_time)
steptimes[0] += (end - start);
prevstep = 0;
return next;
}
// Function for calculating initial zeros by subtracting row and column minima from each element.
int CuLAP::hungarianStep1(bool count_time)
{
double start = omp_get_wtime();
computeInitialAssignments(d_costs_dev, d_vertices_dev, SP, N, devid);
double mid = omp_get_wtime();
int next = 2;
while (true)
{
initial_assignment_count = 0;
if ((next = hungarianStep2(false)) == 6)
break;
if ((next = hungarianStep3(false)) == 5)
break;
hungarianStep4(false);
}
double end = omp_get_wtime();
if (count_time)
{
steptimes[1] += (mid - start);
steptimes[2] += (end - mid);
}
prevstep = 1;
return next;
}
// Function for checking optimality and constructing predicates and covers.
int CuLAP::hungarianStep2(bool count_time)
{
double start = omp_get_wtime();
int cover_count = computeRowCovers(d_vertices_dev, d_row_data_dev, d_col_data_dev, SP, N, devid);
if (initial_assignment_count == 0)
initial_assignment_count = cover_count;
int next = (cover_count == SP * N) ? 6 : 3;
double end = omp_get_wtime();
if (count_time)
steptimes[3] += (end - start);
prevstep = 2;
return next;
}
// Function for building alternating tree rooted at unassigned rows.
int CuLAP::hungarianStep3(bool count_time)
{
double start = omp_get_wtime();
///////////////////////////////////////////////////////////////
double mid = omp_get_wtime();
///////////////////////////////////////////////////////////////
int next;
bool h_flag = false;
executeZeroCover(d_costs_dev, d_vertices_dev, d_row_data_dev, d_col_data_dev, &h_flag, SP, N, devid); // execute zero cover algorithm.
next = h_flag ? 4 : 5;
///////////////////////////////////////////////////////////////
double end = omp_get_wtime();
if (count_time)
{
steptimes[4] += (mid - start);
steptimes[5] += (end - mid);
}
prevstep = 3;
return next;
}
// Function for augmenting the solution along multiple node-disjoint alternating trees.
int CuLAP::hungarianStep4(bool count_time)
{
double start = omp_get_wtime();
///////////////////////////////////////////////////////////////
reversePass(d_row_data_dev, d_col_data_dev, SP, N, devid); // execute reverse pass of the maximum matching algorithm.
augmentationPass(d_vertices_dev, d_row_data_dev, d_col_data_dev, SP, N, devid); // execute augmentation pass of the maximum matching algorithm.
///////////////////////////////////////////////////////////////
double end = omp_get_wtime();
if (count_time)
steptimes[6] += (end - start);
prevstep = 4;
return 2;
}
// Function for updating dual solution to introduce new zero-cost arcs.
int CuLAP::hungarianStep5(bool count_time)
{
double start = omp_get_wtime();
dualUpdate(d_vertices_dev, d_row_data_dev, d_col_data_dev, SP, N, devid);
double end = omp_get_wtime();
if (count_time)
steptimes[7] += (end - start);
prevstep = 5;
return 3;
}
int CuLAP::hungarianStep6(bool count_time)
{
double start = omp_get_wtime();
// calcObjVal(d_obj_val_dev, d_vertices_dev, SP, N, devid);
calcObjVal2(d_obj_val_dev, d_costs_dev.elements, d_vertices_dev, SP, N, devid);
double end = omp_get_wtime();
if (count_time)
steptimes[8] += (end - start);
prevstep = 6;
return 100;
}