-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.cpp
More file actions
358 lines (313 loc) · 8.24 KB
/
parallel.cpp
File metadata and controls
358 lines (313 loc) · 8.24 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
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
#include <omp.h>
#include "my_timer.h"
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstddef>
typedef struct grid {
//two boards needed for testing boards and correct answers
//originally was a class called SudokuGrid in the serial version
short **guesses;
short **fixed;
} SUDOKUGRID;
//linkedList
struct listElements {
SUDOKUGRID grid;
short i, j;
struct listElements *next;
};
typedef struct listElements list;
//makes grid size scalable
int l;
int SIZE;
FILE *inputGrid;
SUDOKUGRID solution;
//linked list stuff
list *head;
list *tail;
SUDOKUGRID readStringToBoard(char *filename) {
int i,j;
SUDOKUGRID grid;
int element_int;
inputGrid = fopen(filename, "rt");
fscanf(inputGrid, "%d", &element_int);
l = element_int;
SIZE = l*l;
grid.guesses = (short**)malloc(SIZE*sizeof(short*));
for (i=0;i<SIZE;i++)
grid.guesses[i] = (short*) malloc (SIZE * sizeof (short));
grid.fixed = (short**) malloc(SIZE * sizeof(short*));
for (i=0;i<SIZE;i++)
grid.fixed[i] = (short*) malloc (SIZE * sizeof (short));
// set all to 0 as we did in the SudokuGrid Class to initialize
for (i=0; i<SIZE; i++) {
for (j=0; j<SIZE; j++) {
grid.fixed[i][j] = 0;
}
}
for(i = 0; i < SIZE; i++) {
for(j = 0; j < SIZE; j++){
fscanf(inputGrid, "%d", &element_int);
grid.guesses[i][j] = element_int;
//set to 1 if filled already
if (grid.guesses[i][j] != 0)
grid.fixed[i][j] = 1;
}
}
fclose(inputGrid);
return grid;
}
void print(SUDOKUGRID *grid) {
int col,row;
printf("\n-------------------\n");
for (col = 0; col < SIZE; col++) {
printf("|");
for (row = 0; row < SIZE; row=row+3) {
printf("%1d %1d %1d|",grid->guesses[col][row],grid->guesses[col][row+1],grid->guesses[col][row+2]);
}
if((col+1)%3==0){
printf("\n-------------------\n");
} else {
printf("\n");
}
}
}
short checkAll(SUDOKUGRID grid, short x_line, short x_col) {
short line, column;
short value = grid.guesses[x_line][x_col];
// Previously checkColumn
for (line = 0; line < SIZE; line++) {
if (grid.guesses[line][x_col] == 0)
continue;
if ((x_line != line) &&
(grid.guesses[line][x_col] == value))
return 0;
}
// previously checkRow
for (column = 0; column < SIZE; column++) {
if (grid.guesses[x_line][column] == 0)
continue;
if (x_col != column && grid.guesses[x_line][column] == value)
return 0;
}
// previously checkBox TODO
short x_group = (x_line / l) * l;
short y_group = (x_col / l) * l;
for (line = x_group; line < x_group+l; line++) {
for (column = y_group; column < y_group+l; column++) {
if (grid.guesses[line][column] == 0)
continue;
if ((x_line != line) &&
(x_col != column) &&
(grid.guesses[line][column] == value)) {
return 0;
}
}
}
return 1;
}
void decreasePosition(SUDOKUGRID* grid, short* iPointer, short* jPointer){
do {
if (*jPointer == 0 && *iPointer > 0) {
*jPointer = SIZE - 1;
(*iPointer)--;
} else
(*jPointer)--;
} while (*jPointer >= 0 && (*grid).fixed[*iPointer][*jPointer] == 1);
}
void increasePosition(SUDOKUGRID* grid, short* iPointer, short* jPointer){
do{
if(*jPointer < SIZE-1)
(*jPointer)++;
else {
*jPointer = 0;
(*iPointer)++;
}
} while (*iPointer < SIZE && (*grid).fixed[*iPointer][*jPointer]);
}
//linked list stuff
void freeListElement(list *node) {
int i;
for (i = 0; i < SIZE; i++) {
free(node->grid.guesses[i]);
free(node->grid.fixed[i]);
}
free(node->grid.guesses);
free(node->grid.fixed);
free(node);
}
//linked list stuff
list* createList(SUDOKUGRID grid, short i, short j){
list * curr = (list *)malloc(sizeof(list));
int m;
short x, y;
/* allocate memory for new copy */
curr->grid.guesses = (short**)malloc(SIZE*sizeof(short*));
for (m=0;m<SIZE;m++)
curr->grid.guesses[m] = (short*) malloc (SIZE * sizeof (short));
curr->grid.fixed = (short**) malloc(SIZE * sizeof(short*));
for (m=0;m<SIZE;m++)
curr->grid.fixed[m] = (short*) malloc (SIZE * sizeof (short));
for(x = 0; x < SIZE; x++){
for(y = 0; y < SIZE; y++){
curr->grid.guesses[x][y] = grid.guesses[x][y];
curr->grid.fixed[x][y] = grid.fixed[x][y];
}
}
curr->i = i;
curr->j = j;
curr->next = NULL;
return curr;
}
//linked list stuff
void attachItem(list* newItem){
if(head == NULL){
head = newItem;
tail = newItem;
} else {
tail->next = newItem;
tail = newItem;
}
}
//linked list stuff
list* removeItem(){
list* result = NULL;
if(head != NULL){
result = head;
head = result->next;
if(head == NULL){
tail = NULL;
}
}
return result;
}
void initializeBruteForce(SUDOKUGRID* grid){
short i = 0;
short j = 0;
if ((*grid).fixed[i][j] == 1)
increasePosition(grid, &i, &j);
short num=0;
list* current = NULL;
while(1) {
((*grid).guesses[i][j])++;
//check to make sure it is valid
if (grid->guesses[i][j] <= SIZE && checkAll(*grid, i, j) == 1) {
list* newPath = createList(*grid, i, j);
attachItem(newPath);
num++;
} else if(grid->guesses[i][j] > SIZE) {
if(current != NULL){
freeListElement(current);
}
if(num >= SIZE){
break;
}
list* current = removeItem();
if(current == NULL){
break;
}
grid = &(current->grid);
i = current->i;
j = current->j;
if(i == SIZE-1 && j == SIZE-1){
//solved
attachItem(current);
break;
}
num--;
increasePosition(grid, &i, &j);
}
}
if(current != NULL){
freeListElement(current);
}
}
short bruteForceParallel(SUDOKUGRID grid) {
head = NULL;
tail = NULL;
initializeBruteForce(&grid);
short found = 0;
short i, j;
list* current;
int level;
#pragma omp parallel shared(found) private(i,j, current, level)
{
#pragma omp critical (deadlocks)
current = removeItem();
while(current != NULL && found == 0){
SUDOKUGRID currentGrid = current->grid;
i = current->i;
j = current->j;
increasePosition(¤tGrid, &i, &j);
level = 1;
while (level > 0 && i < SIZE && found == 0) {
if (currentGrid.guesses[i][j] < SIZE) {
// increase cell value and run checks
currentGrid.guesses[i][j]++;
if (checkAll(currentGrid, i, j) == 1) {
increasePosition(¤tGrid, &i, &j);
level++;
}
} else {
currentGrid.guesses[i][j] = 0;
decreasePosition(¤tGrid, &i, &j);
level--;
}
}
if(i == SIZE){
found = 1;
solution = currentGrid;
continue;
}
free(current);
#pragma omp critical (deadlocks)
current = removeItem();
}
}
return found;
}
int main(int argc, char* argv[]) {
//parallel testpuzzle.txt threadNumber
myTimer_t t0;
int thread_count = atoi(argv[2]);
int max_threads = omp_get_max_threads();
printf("omp_get_max_threads = %d\n", omp_get_max_threads());
printf(" %d threads going\n", thread_count);
if (thread_count > 1)
{
//Static assignment to get consistent usage across trials
int max_threads = omp_get_max_threads();
printf("%d threads going", atoi(argv[2]));
omp_set_num_threads(thread_count);
SUDOKUGRID m = readStringToBoard(argv[1]);
printf("testBoard:");
print(&m);
//TODO START TIMER
t0 = getTimeStamp();
//sending the input grid to the bruteForceParallel method which would return 1 if a solution is found
short hasSolution = bruteForceParallel(m);
if(hasSolution == 0){
printf("invalidBoard\n");
return 1;
}
}
else
{
//Set thread count
printf("SERIAL WE DO NOT WANT TO BE HERE");
}
//TODO END TIMER
myTimer_t t1 = getTimeStamp();
printf("Solved:");
std::cout << getElapsedTime(t0,t1) << std::endl;
print(&solution);
//MEMORY Cleanup From Linked List algorithm code
list* node = head;
while (node != NULL) {
list* next = node->next;
freeListElement(node);
node = next;
}
return 0;
}