-
Notifications
You must be signed in to change notification settings - Fork 2
/
sudoku.cu
144 lines (111 loc) · 4.02 KB
/
sudoku.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
// ----------------------------------------------------------------
// Sudoku -- Puzzle Solver on GPU using CUDA
// ----------------------------------------------------------------
/**
* @file
* sudoku.cu
*
* @brief main sudoku file to init and execute
*/
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// includes, project
#include <cuda.h>
#include <cuda_runtime.h>
// includes, utilities
#include "util/error_utils.cuh"
#include "util/io_utils.cuh"
#include "data.cuh"
// includes, kernels
#include "beecolony.cuh"
#include "humanlogic.cuh"
#include "breadthfirstsearch.cuh"
void print (int n, Square * h_solved, const char * alg, float elapsedTime) {
/* Convert to proper format for display */
CSRtoCSC(h_solved, n);
/* Helper string per algorithm */
if (!strcmp(alg, "-bee")) {
const char * finished = "/********** Bee Colony (C) **********/";
output(finished, alg, n, false, h_solved);
} else if (!strcmp(alg, "-log")) {
const char * finished = "/********** Human-Logic(C) **********/";
output(finished, alg, n, false, h_solved);
} else if (!strcmp(alg, "-bfs")) {
const char * finished = "/************ BFS-DFS (C) ***********/";
output(finished, alg, n, false, h_solved);
}
/* Print algorithm statistics */
const char* statistics = "/******* Statistics (Begin) ********/";
printf("%s\n", statistics);
printf("Elapsed Time: %f (ms)\n", elapsedTime);
const char* statistics_end = "/******** Statistics (End) *********/";
printf("%s\n", statistics_end);
}
void release (Square * h_unsolved, Square * d_unsolved, Square * d_solved) {
/* Free Memory Allocations */
free(h_unsolved);
ERROR_CHECK( cudaFree(d_unsolved) );
ERROR_CHECK( cudaFree(d_solved) );
}
void KernelManager(int n, Square * h_unsolved, bool o_graphics) {
/* CUDA event setup */
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
/* Memory Allocations */
int memsize = sizeof(Square) * n * n;
Square * d_unsolved;
ERROR_CHECK( cudaMalloc((void**) &d_unsolved, memsize) );
/* IMPORTANT: PLEASE ADD THIS IN YOUR KERNEL MANAGER FUNCTION */
/*ERROR_CHECK( cudaMemcpy(d_unsolved, h_unsolved, memsize,
cudaMemcpyHostToDevice) );*/
/* IMPORTANT: END! */
Square * d_solved;
ERROR_CHECK( cudaMalloc((void**) &d_solved, memsize) );
/* ARTIFICIAL BEE COLONY ALGORITHM */
float elapsedTime;
cudaEventRecord(start, 0);
ArtificialBeeColony (h_unsolved, d_unsolved, d_solved, n);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
Square * h_solved = (Square *) malloc(memsize);
ERROR_CHECK( cudaMemcpy(h_solved, d_solved, memsize,
cudaMemcpyDeviceToHost) );
const char * bee = "-bee";
print(n, h_solved, bee, elapsedTime);
/* HUMAN LOGIC BASED ALGORITHM */
cudaEventRecord(start, 0);
HumanLogic (h_unsolved, d_unsolved, d_solved, n);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
ERROR_CHECK( cudaMemcpy(h_solved, d_solved, memsize,
cudaMemcpyDeviceToHost) );
const char * logic = "-log";
print(n, h_solved, logic, elapsedTime);
/* TREE BASED ALGORITHM */
cudaEventRecord(start, 0);
BreadthFirstSearch (h_unsolved, d_unsolved, d_solved, n);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
ERROR_CHECK( cudaMemcpy(h_solved, d_solved, memsize,
cudaMemcpyDeviceToHost) );
const char * tree = "-bfs";
print(n, h_solved, tree, elapsedTime);
/* Destroy CUDA event */
cudaEventDestroy(start);
cudaEventDestroy(stop);
/* Free Memory */
release(h_unsolved, d_unsolved, d_solved);
}
int main(int argc, char** argv) {
/* Gets arguments from command line and puzzle from a file */
CommandLineArgs * build = new CommandLineArgs;
input(argc, argv, build);
KernelManager((*build).size, (*build).Puzzle, (*build).graphics);
}