-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw1.c
196 lines (164 loc) · 5.68 KB
/
hw1.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
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
/******************************************************************************
* Homework1 in CS 432/632/732 *
* *
* Program illustrate the use of dynamic memory allocation to create *
* contiguous 2D-matrices and use traditional array indexing. *
* It also illustrate the use of gettime to measure wall clock time. *
* *
* To Compile: gcc -O hw1.c (to print matrices add -D DEBUG_PRINT) *
* To run: ./a.out <size> *
* *
* Author: Eesaan Atluri *
* Email: [email protected] *
\******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
/* Function to calculate the runtime */
double gettime(void) {
struct timeval tval;
gettimeofday(&tval, NULL);
return( (double)tval.tv_sec + (double)tval.tv_usec/1000000.0 );
}
/* Function to dynamically allocate memory for matrices(2D Arrays) using row major storage */
double **allocarray(int P, int Q) {
int i;
double *p, **a;
p = (double *)malloc(P*Q*sizeof(double));
a = (double **)malloc(P*sizeof(double*));
if (p == NULL || a == NULL)
printf("Error allocating memory\n");
/* for row major storage */
for (i = 0; i < P; i++)
a[i] = &p[i*Q];
return a;
}
/*Function definition to Initialize a matrix with random 0's and 1's
and pad them with ghost cells on all 4 sides with 0's. */
double **initarray(double **a, int mrows, int ncols) {
int i,j;
for (i=0; i<mrows; i++)
for (j=0; j<ncols; j++){
// Initialize the cells with random 0's and 1's
a[i][j] = rand() % 2;
a[0][j] = 0;
a[i][0] = 0;
a[mrows-1][j] = 0;
a[i][ncols-1] = 0;
}
return a;
}
/* Function to print the 2D array (used in debugging when -D flag enabled)*/
void printarray(double **a, int mrows, int ncols) {
int i,j;
for (i=0; i<mrows; i++) {
for (j=0; j<ncols; j++)
printf("%d ", (int)a[i][j]);
printf("\n");
}
}
/* Function to count the no. of live Neighbors */
int checkneighbors(double **a, int x, int y)
{
int i,j;
int count = 0;
for(i=-1; i<2; i++){
for(j=-1; j<2; j++){
count = count + a[x+i][y+j];
}
}
count = count - a[x][y]; //remove the cell itself from count.
return count;
}
/* Function to create next generation of cells */
double **createNextGen(double **a, double **b, int mrows, int ncols)
{
int i,j;
for (i=1; i<mrows-1; i++) {
for (j=1; j<ncols-1; j++){
int current = a[i][j];
int neighbors=checkneighbors(a,i,j);
// implement rules of the game to create next gen matrix 'b'
if(current==0 && neighbors ==3){
b[i][j]=1;
}
else if(current==1 && (neighbors < 2 || neighbors>3)){
b[i][j]=0;
}
else{
b[i][j]=current;
}
}
}
return 0;
}
/* Function to check for equality of matrices */
int ismatrixequal(double **a, double **b, int N)
{
int i, j,flag=1;
for (i=0; i<N; i++){
for (j=0; j<N; j++){
if(a[i][j]!=b[i][j]){
/* Start by assuming matrices are equal with flag set to 1.
If the elements are not equal then set flag to 0 and break */
flag=0;
break;
}
}
}
return flag;
}
int main(int argc, char **argv)
{
int N, i;
double **a=NULL, **b=NULL;
double starttime, endtime;
time_t t;
/* Prints help message if no. of args not equal to 3 */
if (argc != 3) {
printf("Usage: %s <SIZE> <MAX_ITER>\n", argv[0]);
exit(-1);
}
/* Args for the program - Size and Max generations. */
N = atoi(argv[1]);
int MAX_ITER = atoi(argv[2]);
/* Allocate memory for matrices a,b of order N+2 to accommodate ghost cells */
a = allocarray(N+2, N+2);
b = allocarray(N+2, N+2);
/* Initialize the matrix 'a' with 0's and 1's at random */
srand((unsigned) time(&t));
a = initarray(a, N+2, N+2);
starttime = gettime(); //Start time to measure the working block performance
/* Create new generations until 'MAX_ITER' iterations reached or until there's no change in generations
Ending criteria for the game is implemented below. */
for(i=0; i<MAX_ITER; i++){
createNextGen(a, b, N+2, N+2);
//printf("This is Iteration %d\n", i);
#ifdef DEBUG_PRINT
printf("This is Iteration %d\n", i);
printarray(a, N+2, N+2);
printf("\n");
printarray(b, N+2, N+2);
printf("\n");
#endif
/* Check if the matrices are equal to end the Game. */
int test=ismatrixequal(a,b,N);
//printf("%d\n", test);
if(test)
{
printf("Exiting at iteration %d because there is no change in generations.\n", i);
break; // stop the game basically.
}
else
{
//swapmatrices(a, b); by swapping their corresponding pointers
double **t = a;
a = b;
b = t;
}
}
endtime = gettime(); //End time to measure the working block performance.
printf("Time taken = %lf seconds\n", endtime-starttime);
return 0;
}