@@ -44,13 +44,32 @@ int pcoord[2];
4444/// the alpha coefficient used in the computation
4545double alpha ;
4646
47+ double L = 1.0 ;
48+ double source1 [4 ]= {0.4 , 0.4 , 0.2 , 100 };
49+ double source2 [4 ]= {0.7 , 0.8 , 0.1 , 200 };
50+
4751/** Initialize the data all to 0 except for the left border (XX==0) initialized to 1 million
4852 * \param[out] dat the local data to initialize
4953 */
5054void init (double dat [dsize [0 ]][dsize [1 ]])
5155{
5256 for (int yy = 0 ; yy < dsize [0 ]; ++ yy ) for (int xx = 0 ; xx < dsize [1 ]; ++ xx ) dat [yy ][xx ] = 0 ;
53- if ( pcoord [1 ] == 0 ) for (int yy = 0 ; yy < dsize [0 ]; ++ yy ) dat [yy ][0 ] = 1000000 ;
57+ double dy = L / ((dsize [0 ]- 2 ) * psize [0 ]) ;
58+ double dx = L / ((dsize [1 ]- 2 ) * psize [1 ]) ;
59+
60+ double cpos_x ,cpos_y ;
61+ for (int yy = 0 ; yy < dsize [0 ];++ yy ) {
62+ cpos_y = (yy + pcoord [0 ]* (dsize [0 ]- 2 ))* dy - 0.5 * dy ;
63+ for (int xx = 0 ; xx < dsize [1 ];++ xx ) {
64+ cpos_x = (xx + pcoord [1 ]* (dsize [1 ]- 2 ))* dx - 0.5 * dx ;
65+ if ((cpos_y - source1 [0 ])* (cpos_y - source1 [0 ]) + (cpos_x - source1 [1 ])* (cpos_x - source1 [1 ]) <= source1 [2 ]* source1 [2 ]) {
66+ dat [yy ][xx ] = source1 [3 ];
67+ }
68+ if ((cpos_y - source2 [0 ])* (cpos_y - source2 [0 ]) + (cpos_x - source2 [1 ])* (cpos_x - source2 [1 ]) <= source2 [2 ]* source2 [2 ]) {
69+ dat [yy ][xx ] = source2 [3 ];
70+ }
71+ }
72+ }
5473}
5574
5675/** Compute the values at the next time-step based on the values at the current time-step
@@ -60,21 +79,15 @@ void init(double dat[dsize[0]][dsize[1]])
6079void iter (double cur [dsize [0 ]][dsize [1 ]], double next [dsize [0 ]][dsize [1 ]])
6180{
6281 int xx , yy ;
63- for (xx = 0 ; xx < dsize [1 ]; ++ xx ) next [0 ][xx ] = cur [0 ][xx ];
6482 for (yy = 1 ; yy < dsize [0 ]- 1 ; ++ yy ) {
65- next [yy ][0 ] = cur [yy ][0 ];
6683 for (xx = 1 ; xx < dsize [1 ]- 1 ; ++ xx ) {
67- next [yy ][xx ] =
68- (1. - 4. * alpha ) * cur [yy ][xx ]
69- + alpha * ( cur [yy ][xx - 1 ]
70- + cur [yy ][xx + 1 ]
71- + cur [yy - 1 ][xx ]
72- + cur [yy + 1 ][xx ]
73- );
84+ next [yy ][xx ] = (1. - 4. * alpha ) * cur [yy ][xx ]
85+ + alpha * ( cur [yy ][xx - 1 ]
86+ + cur [yy ][xx + 1 ]
87+ + cur [yy - 1 ][xx ]
88+ + cur [yy + 1 ][xx ]);
7489 }
75- next [yy ][dsize [1 ]- 1 ] = cur [yy ][dsize [1 ]- 1 ];
7690 }
77- for (xx = 0 ; xx < dsize [1 ]; ++ xx ) next [dsize [0 ]- 1 ][xx ] = cur [dsize [0 ]- 1 ][xx ];
7891}
7992
8093/** Exchanges ghost values with neighbours
@@ -87,7 +100,7 @@ void exchange(MPI_Comm cart_comm, double cur[dsize[0]][dsize[1]])
87100 int rank_source , rank_dest ;
88101 static MPI_Datatype column , row ;
89102 static int initialized = 0 ;
90-
103+
91104 if ( !initialized ) {
92105 MPI_Type_vector (dsize [0 ]- 2 , 1 , dsize [1 ], MPI_DOUBLE , & column );
93106 MPI_Type_commit (& column );
@@ -104,8 +117,8 @@ void exchange(MPI_Comm cart_comm, double cur[dsize[0]][dsize[1]])
104117
105118 // send up
106119 MPI_Cart_shift (cart_comm , 0 , -1 , & rank_source , & rank_dest );
107- MPI_Sendrecv (& cur [1 ][1 ], 1 , row , rank_dest , 100 , // send column after ghost
108- & cur [dsize [0 ]- 1 ][1 ], 1 , row , rank_source , 100 , // receive last column (ghost)
120+ MPI_Sendrecv (& cur [1 ][1 ], 1 , row , rank_dest , 100 , // send row after ghost
121+ & cur [dsize [0 ]- 1 ][1 ], 1 , row , rank_source , 100 , // receive last row (ghost)
109122 cart_comm , & status );
110123
111124 // send to the right
@@ -116,7 +129,7 @@ void exchange(MPI_Comm cart_comm, double cur[dsize[0]][dsize[1]])
116129
117130 // send to the left
118131 MPI_Cart_shift (cart_comm , 1 , -1 , & rank_source , & rank_dest );
119- MPI_Sendrecv (& cur [1 ][1 ], 1 , column , rank_dest , 100 , // send column after ghost
132+ MPI_Sendrecv (& cur [1 ][1 ], 1 , column , rank_dest , 100 , // send column after ghost
120133 & cur [1 ][dsize [1 ]- 1 ], 1 , column , rank_source , 100 , // receive last column (ghost)
121134 cart_comm , & status );
122135}
@@ -162,7 +175,7 @@ int main( int argc, char* argv[] )
162175 dsize [1 ] = global_size [1 ]/psize [1 ] + 2 ;
163176
164177 // create a 2D Cartesian MPI communicator & get our coordinate (rank) in it
165- int cart_period [2 ] = { 0 , 0 };
178+ int cart_period [2 ] = { 1 , 1 };
166179 MPI_Comm cart_comm ; MPI_Cart_create (main_comm , 2 , psize , cart_period , 1 , & cart_comm );
167180 MPI_Cart_coords (cart_comm , pcoord_1d , 2 , pcoord );
168181
@@ -178,11 +191,9 @@ int main( int argc, char* argv[] )
178191 int ii = 0 ;
179192
180193 // share useful configuration bits with PDI
181- PDI_expose ("ii" , & ii , PDI_OUT );
182194 PDI_expose ("pcoord" , pcoord , PDI_OUT );
183195 PDI_expose ("dsize" , dsize , PDI_OUT );
184196 PDI_expose ("psize" , psize , PDI_OUT );
185- PDI_expose ("main_field" , cur , PDI_OUT );
186197
187198 // the main loop
188199 for (; ii < 10 ; ++ ii ) {
@@ -197,14 +208,14 @@ int main( int argc, char* argv[] )
197208
198209 // exchange data with the neighbours
199210 exchange (cart_comm , next );
200-
211+
201212 // swap the current and next values
202213 double (* tmp )[dsize [1 ]] = cur ; cur = next ; next = tmp ;
203214 }
204215 // finally share the main field as well as the loop counter after the loop
205216 PDI_multi_expose ("finalization" ,
206- "main_field" , cur , PDI_OUT ,
207217 "ii" , & ii , PDI_OUT ,
218+ "main_field" , cur , PDI_OUT ,
208219 NULL );
209220
210221 // finalize PDI
0 commit comments