forked from bcsefercik/3D-7PointStencil-MPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3d_serial.cpp
190 lines (153 loc) · 5.36 KB
/
3d_serial.cpp
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sys/time.h>
#define MASTER 0
#define OUTER 1.0
#define INNER -1.0
void usage(char **argv);
void initValues(double* array, int width, int height, int depth, double inner_temp, double outer_temp);
double getElement(double* array, int x, int y, int z, int width, int height, int depth);
double getElement(double* array, int offset, int x, int y, int z, int width, int height, int depth);
void setElement(double* array, double value, int x, int y, int z, int width, int height, int depth);
void computation(double *result, double *temp, int offset, int width, int height, int depth);
void print_3d(double *array, int width, int height, int depth);
double average_array(double *array, int width, int height, int depth);
double getTime();
int main(int argc, char **argv) {
int grid_width, grid_height, grid_depth, num_iterations;
int ghost_width, ghost_height, ghost_depth, ghost_size;
double *ghost_temp, *ghost_result;
double start_time, end_time, total_time;
if (argc < 5)
usage(argv);
if((grid_width = atoi(argv[1])) <= 0 ||
(grid_height = atoi(argv[2])) <= 0 ||
(grid_depth = atoi(argv[3])) <= 0 ||
(num_iterations = atoi(argv[4])) <= 0)
usage(argv);
ghost_width = grid_width + 2;
ghost_height = grid_height +2;
ghost_depth = grid_depth + 2;
ghost_size = ghost_width * ghost_height * ghost_depth;
ghost_temp = (double *) malloc(ghost_size * sizeof(double));
ghost_result = (double *) malloc(ghost_size * sizeof(double));
if(!ghost_temp || !ghost_result){
fprintf(stderr, "error: unable to allocate memory\n");
exit(1);
}
initValues(ghost_result, ghost_width, ghost_height, ghost_depth, INNER, OUTER);
int num = 0;
start_time = getTime();
while(num<num_iterations){
num++;
computation(ghost_result, ghost_temp, 0, ghost_width, ghost_height, ghost_depth);
}
end_time = getTime();
total_time = end_time - start_time;
double average = average_array(ghost_result,ghost_width,ghost_height,ghost_depth);
//print_3d(ghost_result,ghost_width,ghost_height,ghost_depth);
printf("Average : %.2f \n", average);
printf("Time : %f \n", total_time);
free(ghost_temp);
free(ghost_result);
return 0;
}
void usage(char **argv)
{
fprintf(stderr, "Usage: %s <width> <height> <depth> <num_iterations>\n", argv[0]);
exit(1);
}
void initValues(double* array, int width, int height, int depth, double inner_temp, double outer_temp){
for(int i=1; i<(width-1); i++){
for(int j=1; j<(height-1); j++){
for(int k=1; k<(depth-1); k++){
setElement(array, inner_temp, i, j, k, width, height, depth);
}
}
}
for(int j=0; j<height; j++){
for(int k=0; k<depth; k++){
setElement(array, outer_temp, 0, j, k, width, height, depth);
setElement(array, outer_temp, width-1, j, k, width, height, depth);
}
}
for(int i=0; i<width; i++){
for(int k=0; k<depth; k++){
setElement(array, outer_temp, i, 0, k, width, height, depth);
setElement(array, outer_temp, i, height-1, k, width, height, depth);
}
}
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
setElement(array, outer_temp, i, j, 0, width, height, depth);
setElement(array, outer_temp, i, j, depth-1, width, height, depth);
}
}
}
double getElement(double* array, int x, int y, int z, int width, int height, int depth){
int index = x*height*depth + y*depth + z;
return (array)[index];
}
double getElement(double* array, int offset, int x, int y, int z, int width, int height, int depth){
int index = x*height*depth + y*depth + z + offset;
return (array)[index];
}
void setElement(double* array, double value, int x, int y, int z, int width, int height, int depth){
int index = x*height*depth + y*depth + z;
array[index] = value;
}
void computation(double *result, double *temp, int offset, int width, int height, int depth){
int index;
for(int i = 1; i<(width-1); i++){
for(int j = 1; j<(height-1); j++){
for(int k = 1; k<(depth-1); k++){
index = offset + i*height*depth + j*depth + k;
double a = result[index];
for(int b = 0; b<130; b++){
a = (a+getElement(result, offset, i, j, k, width, height, depth)+
getElement(result, offset, i-1, j, k, width, height, depth)+
getElement(result, offset, i+1, j, k, width, height, depth)+
getElement(result, offset, i, j-1, k, width, height, depth)+
getElement(result, offset, i, j+1, k, width, height, depth)+
getElement(result, offset, i, j, k-1, width, height, depth)+
getElement(result, offset, i, j, k+1, width, height, depth))/8.0;
}
result[index] = a;
}
}
}
}
void print_3d(double *array, int width, int height, int depth){
for(int k=0; k<depth; k++){
for(int j=0; j<height; j++){
for(int i=0; i<width; i++){
printf("%3.3f ", getElement(array, i, j, k, width, height, depth));
}
printf("\n");
}
printf("\n\n");
}
}
double average_array(double *array, int width, int height, int depth){
double average = 0;
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
for(int k=0; k<depth; k++){
average += getElement(array, i, j, k, width, height, depth);
}
}
}
return average/(width*height*depth);
}
double getTime(){
const double kMicro = 1.0e-6;
struct timeval TV;
const int RC = gettimeofday(&TV, NULL);
if(RC == -1)
{
printf("ERROR: Bad call to gettimeofday\n");
return(-1);
}
return( ((double)TV.tv_sec) + kMicro * ((double)TV.tv_usec) );
}