33
33
// load the PDI header
34
34
#include <pdi.h>
35
35
36
- /// size of the local data as [HEIGHT, WIDTH] including ghosts & boundary constants
36
+ // size of the local data as [HEIGHT, WIDTH] including the number of ghost layers
37
+ // for communications or boundary conditions
37
38
int dsize [2 ];
38
39
39
- /// 2D size of the process grid as [HEIGHT, WIDTH]
40
+ // 2D size of the process grid as [HEIGHT, WIDTH]
40
41
int psize [2 ];
41
42
42
- /// 2D rank of the local process in the process grid as [YY, XX]
43
+ // 2D rank of the local process in the process grid as [YY, XX]
43
44
int pcoord [2 ];
44
45
45
- /// the alpha coefficient used in the computation
46
+ // the alpha coefficient used in the computation
46
47
double alpha ;
47
48
48
49
double L = 1.0 ;
50
+ // definition of the source
51
+ // the source corresponds to a disk of an uniform value
52
+ // source1: center=(0.4,0.4), radius=0.2 and value=100
49
53
double source1 [4 ]= {0.4 , 0.4 , 0.2 , 100 };
54
+ // source2: center=(0.8,0.7), radius=0.1 and value=200
50
55
double source2 [4 ]= {0.7 , 0.8 , 0.1 , 200 };
56
+ // the order of the coordinates of the center (XX,YY) is inverted in the vector
51
57
52
58
FILE * pFile2 = NULL ;
53
59
@@ -77,7 +83,9 @@ void close_file(void)
77
83
fclose (pFile2 );
78
84
}
79
85
80
- /** Initialize the data all to 0 except for the left border (XX==0) initialized to 1 million
86
+ /** Initialize all the data to 0, with the exception of each cells
87
+ * whose center (cpos_x,cpos_y) is inside of the disks
88
+ * defined by source1 or source2
81
89
* \param[out] dat the local data to initialize
82
90
*/
83
91
void init (double dat [dsize [0 ]][dsize [1 ]])
@@ -87,14 +95,19 @@ void init(double dat[dsize[0]][dsize[1]])
87
95
double dx = L / ((dsize [1 ]- 2 ) * psize [1 ]) ;
88
96
89
97
double cpos_x ,cpos_y ;
98
+ double square_dist1 , square_dist2 ;
90
99
for (int yy = 0 ; yy < dsize [0 ];++ yy ) {
91
100
cpos_y = (yy + pcoord [0 ]* (dsize [0 ]- 2 ))* dy - 0.5 * dy ;
92
101
for (int xx = 0 ; xx < dsize [1 ];++ xx ) {
93
102
cpos_x = (xx + pcoord [1 ]* (dsize [1 ]- 2 ))* dx - 0.5 * dx ;
94
- if ((cpos_y - source1 [0 ])* (cpos_y - source1 [0 ]) + (cpos_x - source1 [1 ])* (cpos_x - source1 [1 ]) <= source1 [2 ]* source1 [2 ]) {
103
+ square_dist1 = ( cpos_y - source1 [0 ] ) * ( cpos_y - source1 [0 ] )
104
+ + ( cpos_x - source1 [1 ] ) * ( cpos_x - source1 [1 ] );
105
+ if (square_dist1 <= source1 [2 ] * source1 [2 ]) {
95
106
dat [yy ][xx ] = source1 [3 ];
96
107
}
97
- if ((cpos_y - source2 [0 ])* (cpos_y - source2 [0 ]) + (cpos_x - source2 [1 ])* (cpos_x - source2 [1 ]) <= source2 [2 ]* source2 [2 ]) {
108
+ square_dist2 = ( cpos_y - source2 [0 ] ) * ( cpos_y - source2 [0 ] )
109
+ + ( cpos_x - source2 [1 ] ) * ( cpos_x - source2 [1 ] );
110
+ if (square_dist2 <= source2 [2 ] * source2 [2 ]) {
98
111
dat [yy ][xx ] = source2 [3 ];
99
112
}
100
113
}
@@ -119,7 +132,7 @@ void iter(double cur[dsize[0]][dsize[1]], double next[dsize[0]][dsize[1]])
119
132
}
120
133
}
121
134
122
- /** Exchanges ghost values with neighbours
135
+ /** Exchange ghost values with neighbours
123
136
* \param[in] cart_comm the MPI communicator with all processes organized in a 2D Cartesian grid
124
137
* \param[in] cur the local data at the current time-step whose ghosts need exchanging
125
138
*/
@@ -146,8 +159,8 @@ void exchange(MPI_Comm cart_comm, double cur[dsize[0]][dsize[1]])
146
159
147
160
// send up
148
161
MPI_Cart_shift (cart_comm , 0 , -1 , & rank_source , & rank_dest );
149
- MPI_Sendrecv (& cur [1 ][1 ], 1 , row , rank_dest , 100 , // send column after ghost
150
- & cur [dsize [0 ]- 1 ][1 ], 1 , row , rank_source , 100 , // receive last column (ghost)
162
+ MPI_Sendrecv (& cur [1 ][1 ], 1 , row , rank_dest , 100 , // send row after ghost
163
+ & cur [dsize [0 ]- 1 ][1 ], 1 , row , rank_source , 100 , // receive last row (ghost)
151
164
cart_comm , & status );
152
165
153
166
// send to the right
@@ -158,17 +171,14 @@ void exchange(MPI_Comm cart_comm, double cur[dsize[0]][dsize[1]])
158
171
159
172
// send to the left
160
173
MPI_Cart_shift (cart_comm , 1 , -1 , & rank_source , & rank_dest );
161
- MPI_Sendrecv (& cur [1 ][1 ], 1 , column , rank_dest , 100 , // send column after ghost
174
+ MPI_Sendrecv (& cur [1 ][1 ], 1 , column , rank_dest , 100 , // send column after ghost
162
175
& cur [1 ][dsize [1 ]- 1 ], 1 , column , rank_source , 100 , // receive last column (ghost)
163
176
cart_comm , & status );
164
177
}
165
178
166
179
int main ( int argc , char * argv [] )
167
180
{
168
181
MPI_Init (& argc , & argv );
169
- srand ( time ( NULL ) );
170
-
171
- int switch_iter_value [10 ] = {20 , 35 , 50 , 55 , 60 , 35 , 25 , 20 , 15 , 60 };
172
182
173
183
// load the configuration tree
174
184
PC_tree_t conf = PC_parse_path ("ex12.yml" );
@@ -202,7 +212,7 @@ int main( int argc, char* argv[] )
202
212
assert (global_size [1 ]%psize [1 ]== 0 );
203
213
assert (psize [1 ]* psize [0 ] == psize_1d );
204
214
205
- // compute the local data-size with space for ghosts and boundary constants
215
+ // compute the local data-size (the number of ghost layers is 2 for each coordinate)
206
216
dsize [0 ] = global_size [0 ]/psize [0 ] + 2 ;
207
217
dsize [1 ] = global_size [1 ]/psize [1 ] + 2 ;
208
218
0 commit comments