-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic_v5_threads.c
132 lines (112 loc) · 3.14 KB
/
magic_v5_threads.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#define NUM_THREADS 8
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
int count = 0;
pthread_mutex_t mutex;
char check(char v[]) {
char i, j, a, b, c, d;
char s[16];
s[0] = v[0];
s[1] = v[1];
s[2] = v[2];
s[3] = 34 - v[0] - v[1] - v[2];
s[4] = v[3];
s[5] = v[4];
s[6] = v[5];
s[7] = 34 - v[3] - v[4] - v[5];
s[10] = v[6];
s[14] = 34 - v[2] - v[5] - v[6];
s[15] = 34 - v[0] - v[4] - v[6];
s[11] = 34 - s[3] - s[7] - s[15];
a = 34 - s[10] - s[11];
b = 34 - s[14] - s[15];
c = 34 - s[3] - s[6];
d = 34 - s[0] - s[4];
s[8] = (a - c + d) / 2;
s[9] = (a + c - d) / 2;
s[12] = (c + d - a) / 2;
s[13] = b + (a - c - d) / 2;
for (i = 8; i < 14; i++) {
if (s[i] < 1 || s[i] > 16) {
return 0;
}
}
for (i = 3; i < 16; i++) {
for (j = 0; j < i; j++) {
if (s[i] == s[j]) {
return 0;
}
}
}
return 1;
}
void find(char square[], char entry) {
char i, j, begin, end;
if (entry == 7) {
if (check(square)) {
pthread_mutex_lock(&mutex);
count++;
pthread_mutex_unlock(&mutex);
}
return;
}
if (entry == 2) {
begin = max(18 - square[0] - square[1], 1);
end = min(33 - square[0] - square[1], 16);
} else if (entry == 5) {
begin = max(18 - square[3] - square[4], 1);
end = min(33 - square[3] - square[4], 16);
} else if (entry == 6) {
begin = max(18 - square[0] - square[4], max(18 - square[2] - square[5], 1));
end = min(33 - square[0] - square[4], min(33 - square[2] - square[5], 16));
} else {
begin = 1;
end = 16;
}
for (i = begin; i <= end; i++) {
for (j = 0; j < entry; j++) {
if (square[j] == i) {
goto A;
}
}
square[entry] = i;
find(square, entry + 1);
A: i=i;
}
}
void *perform_work(void *arguments){
int index = *((int *)arguments);
char* start = (char*) malloc(7*sizeof(char));
start[0] = index;
find(start, 1);
free(start);
}
int main(void) {
struct timespec start, finish;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &start);
pthread_t threads[NUM_THREADS];
int thread_args[NUM_THREADS];
int i;
int result_code;
pthread_mutex_init (&mutex, NULL);
// create all threads one by one
for (i = 0; i < NUM_THREADS; i++) {
thread_args[i] = i + 1;
pthread_create(&threads[i], NULL, perform_work, &thread_args[i]);
}
// wait for each thread to complete
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
printf("There are %i magic squares.\n", 2 * count);
printf("Runtime %.2f seconds\n",elapsed);
return 0;
}