Skip to content

Commit 80090f9

Browse files
cleanup
1 parent 3a5ef20 commit 80090f9

File tree

2 files changed

+43
-53
lines changed

2 files changed

+43
-53
lines changed

assignment2/gameOfLife.c

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,52 @@
11
#include "gameOfLife.h"
22

3-
int main(int argc,char** argv){
3+
int main(int argc,char** argv) {
44

55
int rows, columns, generations = atoi(argv[2]);
66
char** array = read_file(argv[1], &rows, &columns);
77
FILE* output = fopen(argv[3],"w");
8-
omp_set_num_threads(atoi(argv[4]));
8+
numOfThreads = atoi(argv[4]);
99

10-
game_of_life_serial(array, columns, rows, generations);
11-
12-
FILE* outputSerial = fopen("outpuSerial.txt","w");
10+
game_of_life_parallel(array, columns, rows, generations);
1311

1412
for(int i = 0; i < rows; i++){
1513
for(int j = 0; j < columns; j++){
16-
fprintf(outputSerial, "|%c", array[i][j]);
14+
fprintf(output, "|%c", array[i][j]);
1715
}
18-
fprintf(outputSerial, "|\n");
16+
fprintf(output, "|\n");
1917
}
2018

21-
// game_of_life_parallel(array, columns, rows, generations);
22-
23-
// for(int i = 0; i < rows; i++){
24-
// for(int j = 0; j < columns; j++){
25-
// fprintf(output, "|%c", array[i][j]);
26-
// }
27-
// fprintf(output, "|\n");
28-
// }
29-
// printf("test");
30-
3119
}
3220

33-
void game_of_life_parallel(char **array, int columns, int rows, int generations){
34-
char **newArray = make_array(columns, rows);
21+
void game_of_life_parallel(char **array, int columns, int rows, int generations) {
3522

23+
char** newArray = make_array(columns, rows);
3624
int numNeighbors,currentGen = 0, i = 0 , j = 0;
37-
#pragma omp for
38-
for(int currentGen = 0; currentGen < generations; currentGen++){
39-
for(int i = 0; i < rows; i++){
40-
for(int j = 0; j < columns; j++){
41-
numNeighbors = count_living_neighbors(i, j, array, rows, columns);
42-
if( numNeighbors == 3 ) newArray[i][j] = alive;
43-
else if( numNeighbors == 2 && array[i][j] == alive) newArray[i][j] = alive;
44-
else newArray[i][j] = dead;
45-
}
46-
}
47-
48-
#pragma omp critical
49-
{
50-
copy_array(array,newArray,columns);
51-
}
52-
}
53-
}
5425

55-
void game_of_life_serial(char** array, int columns, int rows, int generations){
56-
57-
char** newArray = make_array(columns, rows);
26+
omp_set_dynamic(0);
27+
omp_set_num_threads(numOfThreads);
28+
29+
for(currentGen = 0; currentGen < generations; currentGen++){
5830

59-
int numNeighbors;
60-
for(int currentGen = 0; currentGen < generations; currentGen++){
61-
for(int i = 0; i < rows; i++){
62-
for(int j = 0; j < columns; j++){
31+
#pragma omp parallel for private(i, j, numNeighbors)
32+
for(i = 0; i < rows; i++){
33+
for(j = 0; j < columns; j++){
6334
numNeighbors = count_living_neighbors(i, j, array, rows, columns);
6435
if( numNeighbors == 3 ) newArray[i][j] = alive;
6536
else if( numNeighbors == 2 && array[i][j] == alive) newArray[i][j] = alive;
6637
else newArray[i][j] = dead;
6738
}
6839
}
69-
copy_array(array,newArray,columns);
40+
41+
copy_array_parallel(array, newArray, rows, columns);
7042
}
7143

7244
}
7345

7446
int count_living_neighbors(int row, int col, char** array, int numOfRows, int numOfCols) {
47+
7548
int count = 0;
49+
7650
if(col + 1 < numOfCols) count += (array[row][col+1] == alive);
7751
if(col - 1 >= 0) count += (array[row][col-1] == alive);
7852
if(row - 1 >= 0) {
@@ -85,27 +59,39 @@ int count_living_neighbors(int row, int col, char** array, int numOfRows, int nu
8559
if(col + 1 < numOfCols) count+= (array[row+1][col+1] == alive);
8660
if(col - 1 >= 0) count += (array[row+1][col-1] == alive);
8761
}
62+
8863
return count;
64+
8965
}
9066

91-
char** make_array(int columns, int rows){
67+
char** make_array(int columns, int rows) {
68+
9269
char** newArray = (char**)malloc(rows * sizeof(char*));
9370
check_memory_ptr(newArray);
71+
9472
for(int i = 0; i < rows; i++){
9573
newArray[i] = (char*) malloc(columns * sizeof(char));
9674
check_memory_ptr(newArray[i]);
9775
}
76+
9877
return newArray;
78+
9979
}
10080

101-
void copy_array(char** dst, char** src, int size){
102-
for(int i = 0; i < size; i++){
103-
strcpy(dst[i],src[i]);
81+
void copy_array_parallel(char** dst, char** src, int rows, int columns) {
82+
83+
int i = 0, j = 0;
84+
85+
#pragma omp parallel for private (i, j)
86+
for(i = 0; i < rows; i++){
87+
for(j = 0; j < columns; j++)
88+
dst[i][j] = src[i][j];
10489
}
90+
10591
}
10692

10793

108-
char** read_file(const char* fileName, int* _rows, int* _columns){
94+
char** read_file(const char* fileName, int* _rows, int* _columns) {
10995

11096
FILE* file = fopen(fileName,"r");
11197
check_file_ptr(file,fileName);
@@ -142,18 +128,23 @@ char** read_file(const char* fileName, int* _rows, int* _columns){
142128
*_rows = rows;
143129
*_columns = columns;
144130
return array;
131+
145132
}
146133

147-
void check_file_ptr(FILE* filePtr, const char* fileName){
134+
void check_file_ptr(FILE* filePtr, const char* fileName) {
135+
148136
if(filePtr == NULL) {
149137
printf("Could not open file %s",fileName);
150138
exit(EXIT_FAILURE);
151139
}
140+
152141
}
153142

154-
void check_memory_ptr(void* ptr){
143+
void check_memory_ptr(void* ptr) {
144+
155145
if(ptr == NULL) {
156146
printf("Could not allocate memory");
157147
exit(EXIT_FAILURE);
158148
}
149+
159150
}

assignment2/gameOfLife.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ int numOfThreads;
1111

1212
char** read_file(const char* filename, int* rows, int* columns);
1313
void game_of_life_parallel(char** array, int columns, int rows, int generations);
14-
void game_of_life_serial(char** array, int columns, int rows, int generations);
1514
int count_living_neighbors(int row, int col, char** array, int numOfRows, int numOfCols);
1615
char** make_array(int columns, int rows);
17-
void copy_array_parallel(char** dst, char** src, int size);
16+
void copy_array_parallel(char** dst, char** src, int rows, int columns);
1817
void check_file_ptr(FILE* filePtr, const char* fileName);
1918
void check_memory_ptr(void* ptr);

0 commit comments

Comments
 (0)