-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
154 lines (137 loc) · 3.93 KB
/
main.c
File metadata and controls
154 lines (137 loc) · 3.93 KB
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
/**
* @file main.c
* @brief Main for n-puzzle solver program
* @author Mitchell Clay
* @date 4/9/2020
**/
#include <CL/cl.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "state.h"
#include "node.h"
#include "clsupport.h"
#include "stack.h"
#include "solve.h"
int main(int argc, char **argv) {
// initialization
// puzzle_size = square root of n
// dimensions = number of dimensions in puzzle
// verbose = option to print status messages (bool)
// seed = random number generator seed
// scramble_size = initial random moves to change inital state
// (to-do: justification for this calculation)
signed puzzle_size = 3;
signed dimensions = 2;
bool verbose = false;
bool debug = false;
bool use_opencl = false;
unsigned seed = clock();
unsigned scramble_size = 0;
// get command line switches
int c;
while ((c = getopt(argc, argv, "odvn:s:c:m:")) != -1)
switch (c) {
case 'o':
use_opencl = true;
break;
case 'd':
debug = true;
break;
case 'v':
verbose = true;
break;
case 'n':
puzzle_size = atoi(optarg);
break;
case 's':
scramble_size = atoi(optarg);
break;
case 'c':
seed = atoi(optarg);
break;
case 'm':
dimensions = atoi(optarg);
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
// calculate n from puzzle size to make parameter passing easier
signed n = puzzle_size * puzzle_size;
// allocate row-major array for initial state and final state
unsigned arr[n];
unsigned arr_final[n];
// set scramble_size default if switch wasn't used
if (scramble_size == 0) {
scramble_size = 10 * puzzle_size;
}
// if verbose is set, print out program options/defaults
if (verbose) {
printf("Program Options/Defaults: \n");
printf("Debug: ");
if (debug) {
printf("true\n");
}
else {
printf("false\n");
}
printf("Puzzle size: %d\n", puzzle_size);
printf("Dimensions: %d\n", dimensions);
printf("Scramble size: %d\n", scramble_size);
printf("Seed: %d\n", seed);
}
// if verbose is set, print out OpenCL device info
if (verbose) {
printf("OpenCL device(s):\n");
print_cl_devices();
}
// seed random number generator
srand(seed);
if (verbose) {
printf("Starting program\n");
}
if (verbose) {
printf("Generating initial state\n");
}
unsigned blank_position = GenerateInitialState(arr, arr_final, puzzle_size, scramble_size, verbose, debug);
if (verbose) {
printf("Initial state: \n");
PrintState(arr, puzzle_size);
}
if (verbose) {
printf("Attemping to solve puzzle\n\n");
}
// Solve with GPU if using OpenCL option
if (use_opencl) {
if (verbose) {
printf("GPU Solution:\n");
}
CLSolve(arr, arr_final, puzzle_size, blank_position, verbose, debug);
}
// Solve with CPU if not using OpenCL
else {
if (verbose) {
printf("CPU Solution:\n");
}
Solve(arr, arr_final, puzzle_size, blank_position, verbose, debug);
}
if (verbose) {
printf("Done.\n");
}
return 0;
}