-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
116 lines (101 loc) · 2.99 KB
/
main.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
//gcc-7 -fopenmp -o main main.c
//./main
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define N 20 //matrix size
#define NUM_THREADS 4 // count of threads
#define LOOP_COUNT 10 // Repeat (b) at least 10 times and observe the changes in the matrix.
void printMatrix (int matrix[N][N]);
void printMatrix2 (int matrix[N+2][N+2]);
int checkNeighbours(int matrix[N+2][N+2],int i,int j);
int main() {
//Set thread size
omp_set_num_threads(NUM_THREADS);
int input[N][N];
int x[N+2][N+2];//Temp matrix for operations
int result[N+2][N+2];
//Assign random values to each matrix element (0-dead, 1-alive)
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
input[i][j]= rand() % 2;
}
}
//Show input matrix
printMatrix(input);
//Init result matrix
for (int i=0; i<N+2; i++) {
for (int j=0; j<=N+2; j++) {
result[i][j] = 0;
}
}
//Pad borders with zero and copy input matrix to temp matrix
for (int i=0; i<=N+1; i++) {
for (int j=0; j<=N+1; j++) {
if(i==0 || j==0 || i==N+1 || j==N+1)
x[i][j] = 0;
else
x[i][j]= input[i-1][j-1];
}
}
int n=N;
for(int i=0; i<LOOP_COUNT; i++)
{
printf("Step %d\n \n",i+1);
#pragma omp parallel for shared (n)
for (int j=1; j < n; j++)
{
for (int i=1; i < n; i++)
{
int temp= checkNeighbours(x,i,j);
result[i][j]=temp;
}
}
//Copy output matrix for next iteration
for (int i=0; i<N+2; i++) {
for (int j=0; j<=N+2; j++) {
x[i][j] = result[i][j];
}
}
printMatrix2(result);
}
}
//. Change the state of each element using its current state and the states of its 8 neighbours:
//- if there are 5 or more 1s, change its state to alive
//- otherwise change its state to dead
int checkNeighbours(int matrix[N+2][N+2],int i,int j)
{
int aliveCount = 0, deadCount = 0;
matrix[i-1][j-1] == 0 ? deadCount++ : aliveCount++;
matrix[i-1][j] == 0 ? deadCount++ : aliveCount++;
matrix[i-1][j+1] == 0 ? deadCount++ : aliveCount++;
matrix[i][j-1] == 0 ? deadCount++ : aliveCount++;
matrix[i][j+1] == 0 ? deadCount++ : aliveCount++;
matrix[i+1][j-1] == 0 ? deadCount++ : aliveCount++;
matrix[i+1][j] == 0 ? deadCount++ : aliveCount++;
matrix[i+1][j+1] == 0 ? deadCount++ : aliveCount++;
if(aliveCount>= 5)
return 1;
else
return 0;
}
void printMatrix (int matrix[N][N])
{
int i,j;
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%d \t", matrix[i][j]);
printf ("\n");
}
printf ("\n");
}
void printMatrix2 (int matrix[N+2][N+2])
{
int i,j;
for (i=1; i<N+1; i++) {
for (j=1; j<N+1; j++)
printf("%d \t", matrix[i][j]);
printf ("\n");
}
printf ("\n");
}