-
Notifications
You must be signed in to change notification settings - Fork 2
/
beecolony.cuh
561 lines (446 loc) · 16.2 KB
/
beecolony.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
#ifndef BEE_H
#define BEE_H
// ----------------------------------------------------------------
// Sudoku -- Puzzle Solver on GPU using CUDA
// ----------------------------------------------------------------
#pragma once
#include <string.h>
#include <iostream>
#include <fstream>
#include <curand.h>
#include <curand_kernel.h>
#include "util/error_utils.cuh"
#include "data.cuh"
#define CHAINS 15
#define CYCLES (1<<20) // 1000000
#define INIT_EFFORT 0.4
#define EFFORT_MIN 0.001
#define INIT_TOLERANCE 1
#define DELTA_TOLERANCE 0.2
using namespace std;
/**
* @file
* beecolony.cuh
*
* @brief ArtificialBeeColony algorithm implementation.
*/
/**
* @brief Converts a 1D array to 2D array.
*
*/
void ONEDtoTWOD (Square h_sudoku[][9], Square * h_unsolved, int n) {
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
h_sudoku[x][y].value = h_unsolved[x + n * y].value;
h_sudoku[x][y].isLocked = h_unsolved[x + n * y].isLocked;
}
}
}
/**
* @brief Converts row major order to column major.
*
*/
__host__ __device__ void CSRtoCSC (Square * sudoku, int n) {
Square Swap[81];
int limit = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
Swap[i + n * j].value = sudoku[limit].value;
Swap[i + n * j].isLocked = sudoku[limit].isLocked;
limit++;
}
}
for (int k = 0; k < 81; k++) {
sudoku[k].value = Swap[k].value;
sudoku[k].isLocked = Swap[k].isLocked;
}
}
/**
* @brief Random number generator across cores for cuda.
*/
__global__ void cuRandomNumberGenerator (curandState *state) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
/* we have to initialize the state */
curand_init(1337, /* the seed controls the sequence of random values that are produced */
index, /* the sequence number is only important with multiple cores */
0, /* the offset is how much extra we advance in the sequence for each call, can be 0 */
&state[index]);
}
/**
* @brief Determine number of unique elements in a row or col.
*/
__host__ __device__ int foodQuality (int food, Square sudoku[][9], bool rc) {
int index, quality;
int nums[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
quality = 0;
for(int i = 0; i < 9; i++) {
if(rc == 1) {
index = sudoku[i][food].value - 1;
}
else {
index = sudoku[food][i].value - 1;
}
if(index == -1) { return -1; }
if(nums[index] != 0) {
quality += 1;
nums[index] = 0;
}
}
return quality;
}
/**
* @brief Used to determines the quality of the solution by calculating
* uniqueness of an elements.
*/
__host__ __device__ int hiveQuality (Square sudoku[][9]) {
int q_hive = 0;
for(int i = 0; i < 9; i++) {
q_hive += foodQuality(i, sudoku, 0) + foodQuality(i, sudoku, 1);
}
return 162 - q_hive;
}
__global__ void toCSC (Square * sudoku, int n) {
CSRtoCSC (sudoku, n);
}
__global__ void BeeColony (Square * d_unsolved, Square * d_solved,
int n, curandState * state, int c_hive,
int * qualityHives, float effort, // Square * d_sub) {
Square * d_solved1, Square * d_solved2,
Square * d_solved3, Square * d_solved4,
Square * d_solved5, Square * d_solved6,
Square * d_solved7, Square * d_solved8,
Square * d_solved9, Square * d_solved10,
Square * d_solved11, Square * d_solved12,
Square * d_solved13, Square * d_solved14,
Square * d_solved15) {
int index = threadIdx.y + blockIdx.x * blockDim.x;
int block = blockIdx.y + blockIdx.x * blockDim.x;
int index_x = threadIdx.x;
int index_y = threadIdx.y;
// TODO: Copy d_unsolved to shared memory;
__shared__ Square sudoku[9][9];
sudoku[index_x][index_y].value = d_unsolved[index_x + 9*index_y].value;
sudoku[index_x][index_y].isLocked = d_unsolved[index_x + 9*index_y].isLocked;
if (index != 0) {
return;
}
int sub_blockx, sub_blocky;
int unmask1_x, unmask1_y, unmask2_x, unmask2_y;
int q_hive;
for(int i = 0; i < CYCLES; i++) {
/* SCOUT BEE */
sub_blockx = 3 * (int) (3.0 * curand_uniform(&state[block]));
sub_blocky = 3 * (int) (3.0 * curand_uniform(&state[block]));
/* WORKER BEE */
do {
unmask1_x = (int) 3.0 * curand_uniform(&state[block]);
unmask1_y = (int) 3.0 * curand_uniform(&state[block]);
} while(sudoku[sub_blockx + unmask1_x][sub_blocky + unmask1_y].isLocked == -1);
do {
unmask2_x = (int) 3.0 * curand_uniform(&state[block]);
unmask2_y = (int) 3.0 * curand_uniform(&state[block]);
} while(sudoku[sub_blockx + unmask2_x][sub_blocky + unmask2_y].isLocked == -1);
int swap;
swap = sudoku[sub_blockx+unmask1_x][sub_blocky+unmask1_y].value;
sudoku[sub_blockx+unmask1_x][sub_blocky+unmask1_y].value = sudoku[sub_blockx+unmask2_x][sub_blocky+unmask2_y].value;
sudoku[sub_blockx+unmask2_x][sub_blocky+unmask2_y].value = swap;
/* ONLOOKER BEE */
q_hive = hiveQuality(sudoku);
if(q_hive < c_hive) {
c_hive = q_hive;
} else {
if(exp((float) (c_hive - q_hive)/effort) > curand_uniform(&state[block])) {
c_hive = q_hive;
} else {
swap = sudoku[sub_blockx + unmask1_x][sub_blocky + unmask1_y].value;
sudoku[sub_blockx + unmask1_x][sub_blocky + unmask1_y].value = \
sudoku[sub_blockx + unmask2_x][sub_blocky + unmask2_y].value;
sudoku[sub_blockx + unmask2_x][sub_blocky + unmask2_y].value = swap;
}
}
if (q_hive == 0) { break; }
}
for(int m = 0; m < 9; m++) {
for(int y = 0; y < 9; y++) {
// d_sub[(m + y) + (block * (n * n))].value = sudoku[m][y].value;
// d_sub[(m + y) + (block * (n * n))].isLocked = sudoku[m][y].isLocked;
switch (block) {
case 0:
d_solved1[m + 9 * y] = sudoku[m][y];
case 1:
d_solved2[m + 9 * y] = sudoku[m][y];
case 2:
d_solved3[m + 9 * y] = sudoku[m][y];
case 3:
d_solved4[m + 9 * y] = sudoku[m][y];
case 4:
d_solved5[m + 9 * y] = sudoku[m][y];
case 5:
d_solved6[m + 9 * y] = sudoku[m][y];
case 6:
d_solved7[m + 9 * y] = sudoku[m][y];
case 7:
d_solved8[m + 9 * y] = sudoku[m][y];
case 8:
d_solved9[m + 9 * y] = sudoku[m][y];
case 9:
d_solved10[m + 9 * y] = sudoku[m][y];
case 10:
d_solved11[m + 9 * y] = sudoku[m][y];
case 11:
d_solved12[m + 9 * y] = sudoku[m][y];
case 12:
d_solved13[m + 9 * y] = sudoku[m][y];
case 13:
d_solved14[m + 9 * y] = sudoku[m][y];
case 14:
d_solved15[m + 9 * y] = sudoku[m][y];
default:
break;
}
}
}
qualityHives[block] = c_hive;
}
/**
* @brief Used to restart the randomness if stuck in local minimum.
*
*/
void restart (Square * h_unsolved, Square * d_unsolved, int memsize) {
ERROR_CHECK( cudaMemcpy(h_unsolved, d_unsolved, memsize,
cudaMemcpyDeviceToHost));
int ar[3]={0,3,6};
int swap;
int rand1 = random()%3;
int rand2 = random()%3;
int r1_x,r1_y,r2_x,r2_y;
int block_x,block_y;
for (int suf = 0; suf < random()%10; suf++) {
block_x = ar[rand1];
block_y = ar[rand2];
do {
r1_x=random()%3;
r1_y=random()%3;
} while (h_unsolved[(block_x + r1_x) + 9 * (block_y + r1_y)].isLocked == -1);
do {
r2_x=random()%3;
r2_y=random()%3;
} while (h_unsolved[(block_x + r2_x) + 9 * (block_y + r2_y)].isLocked == -1);
swap = h_unsolved[(block_x + r1_x) + 9 * (block_y + r1_y)].value;
h_unsolved[(block_x + r1_x) + 9 * (block_y + r1_y)].value = \
h_unsolved[(block_x + r2_x) + 9 * (block_y + r2_y)].value;
h_unsolved[(block_x + r2_x) + 9 * (block_y + r2_y)].value = swap;
}
ERROR_CHECK( cudaMemcpy(d_unsolved, h_unsolved, memsize,
cudaMemcpyHostToDevice));
}
/**
* @brief Initializes the ABC algorithm, and also satisfies 3x3 clause.
*
*/
void init_ArtificalBeeColony (Square * h_unsolved, int n) {
int nums_filed[9];
int nums_sol[9];
int x, y, p, q, index;
for(int block_i = 0; block_i < 3; block_i++) {
for(int block_j = 0; block_j < 3; block_j++) {
for(int k = 0; k < 9; k++) { nums_filed[k] = k + 1; }
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
x = block_i * 3 + i;
y = block_j * 3 + j;
if(h_unsolved[x + 9 * y].isLocked != 0){
p = h_unsolved[x + 9 * y].value;
nums_filed[p - 1] = 0;
}
}
}
q = -1;
for(int k = 0; k < 9; k++) {
if(nums_filed[k] != 0) {
q += 1;
nums_sol[q] = nums_filed[k];
}
}
index = 0;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
x = block_i * 3 + i;
y = block_j * 3 + j;
if(h_unsolved[x + 9 * y].isLocked == 0) {
h_unsolved[x + 9 * y].value = nums_sol[index];
index += 1;
}
}
}
}
}
}
/**
* @brief Main ABC algorithm controller.
*
*/
void ArtificialBeeColony (Square * h_unsolved, Square * d_unsolved,
Square * d_solved, int n) {
// TODO: Employed Bee; neighborhood search population evaluation.
// TODO: Onlooker Bee; Calculate probability values, search again & evaluate.
// TODO: Scout Bee; determined abandoned solutions, replace them with new &
// better solutions.
/* Memory Allocations */
float effort = INIT_EFFORT;
int memsize = sizeof(Square) * n * n;
int * qualityHives;
int * h_qualityHives;
ERROR_CHECK( cudaHostAlloc((void**) &h_qualityHives, sizeof(int)*CYCLES,
cudaHostAllocDefault));
Square * d_solved1;
Square * d_solved2;
Square * d_solved3;
Square * d_solved4;
Square * d_solved5;
Square * d_solved6;
Square * d_solved7;
Square * d_solved8;
Square * d_solved9;
Square * d_solved10;
Square * d_solved11;
Square * d_solved12;
Square * d_solved13;
Square * d_solved14;
Square * d_solved15;
ERROR_CHECK( cudaMalloc((void**) &d_solved1, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved2, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved3, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved4, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved5, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved6, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved7, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved8, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved9, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved10, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved11, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved12, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved13, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved14, memsize));
ERROR_CHECK( cudaMalloc((void**) &d_solved15, memsize));
// Square * d_sub;
// ERROR_CHECK( cudaMalloc((void**) &d_sub, memsize * CHAINS));
ERROR_CHECK( cudaMalloc((void**) &qualityHives, sizeof(int)*CYCLES));
int threadsPerBlock = n;
dim3 grid (1, CHAINS, 1);
dim3 block (threadsPerBlock, threadsPerBlock, 1);
curandState *d_state;
ERROR_CHECK( cudaMalloc((void**) &d_state, block.x * block.y *
grid.x * grid.y));
cuRandomNumberGenerator<<<grid.x * grid.y, block.x * block.y>>>(d_state);
ERROR_CHECK( cudaPeekAtLastError() );
ERROR_CHECK( cudaDeviceSynchronize() );
int tolerance = INIT_TOLERANCE;
int minimum, min_index;
Square h_sudoku[9][9];
CSRtoCSC(h_unsolved, n);
init_ArtificalBeeColony(h_unsolved, n);
// const char * inprogress = "/********** Bee Colony (CSC) **********/";
// output(inprogress, "-bee", n, false, h_unsolved);
ONEDtoTWOD(h_sudoku, h_unsolved, n);
int c_hive = hiveQuality(h_sudoku);
int p_hive = c_hive;
ERROR_CHECK( cudaMemcpy(d_unsolved, h_unsolved, memsize,
cudaMemcpyHostToDevice) );
do {
minimum = 200;
min_index = 200;
BeeColony<<< grid, block >>>(d_unsolved, d_solved, n, d_state, c_hive,
qualityHives, effort, // d_sub);
d_solved1, d_solved2, d_solved3,
d_solved4, d_solved5, d_solved6,
d_solved7, d_solved8, d_solved9,
d_solved10, d_solved11, d_solved12,
d_solved13, d_solved14, d_solved15);
ERROR_CHECK( cudaPeekAtLastError() );
ERROR_CHECK( cudaDeviceSynchronize() );
ERROR_CHECK( cudaMemcpy(h_qualityHives, qualityHives, sizeof(int) * CHAINS,
cudaMemcpyDeviceToHost));
for(int quality = 0; quality < CHAINS; quality++) {
if (h_qualityHives[quality] < minimum) {
minimum = h_qualityHives[quality];
min_index = quality;
}
}
c_hive = minimum;
/* // TODO: Condense all the solution numbers to one giant array with ptrs.
ERROR_CHECK( cudaMemcpy(d_unsolved, d_sub+(min_index*n*n), memsize,
cudaMemcpyDeviceToDevice));
toCSC<<<1,1>>>(d_unsolved, n);
ERROR_CHECK( cudaPeekAtLastError() );
ERROR_CHECK( cudaDeviceSynchronize() );
*/
switch (min_index) {
case 0:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved1, memsize,
cudaMemcpyDeviceToDevice));
case 1:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved2, memsize,
cudaMemcpyDeviceToDevice));
case 2:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved3, memsize,
cudaMemcpyDeviceToDevice));
case 3:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved4, memsize,
cudaMemcpyDeviceToDevice));
case 4:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved5, memsize,
cudaMemcpyDeviceToDevice));
case 5:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved6, memsize,
cudaMemcpyDeviceToDevice));
case 6:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved7, memsize,
cudaMemcpyDeviceToDevice));
case 7:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved8, memsize,
cudaMemcpyDeviceToDevice));
case 8:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved9, memsize,
cudaMemcpyDeviceToDevice));
case 9:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved10, memsize,
cudaMemcpyDeviceToDevice));
case 10:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved11, memsize,
cudaMemcpyDeviceToDevice));
case 11:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved12, memsize,
cudaMemcpyDeviceToDevice));
case 12:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved13, memsize,
cudaMemcpyDeviceToDevice));
case 13:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved14, memsize,
cudaMemcpyDeviceToDevice));
case 14:
ERROR_CHECK( cudaMemcpy(d_unsolved, d_solved15, memsize,
cudaMemcpyDeviceToDevice));
default:
break;
}
if (c_hive == 0) { break; }
if (c_hive == p_hive) { tolerance--; }
else { tolerance = INIT_TOLERANCE; }
if (tolerance < 0) {
// printf("WARNING: Reached tolerance level. \n");
// printf("WARNING: Restarting randomness of ABC. \n");
restart(h_unsolved, d_unsolved, memsize);
ONEDtoTWOD(h_sudoku, h_unsolved, n);
c_hive = hiveQuality(h_sudoku);
tolerance = INIT_TOLERANCE;
effort = effort + DELTA_TOLERANCE;
}
p_hive = c_hive;
if (c_hive == 0) { break; }
effort = effort * 0.8;
} while (effort > EFFORT_MIN);
ERROR_CHECK( cudaMemcpy(d_solved, d_unsolved, memsize, cudaMemcpyDeviceToDevice));
return;
}
#endif