Blame view
lab3/mpi_heat2D.c
8.22 KB
3604dfae0 MPI version |
1 2 |
/**************************************************************************** * DESCRIPTION: |
a67a1dd2b rewrite error wit... |
3 |
* MPI HEAT2D Example - C Version |
3604dfae0 MPI version |
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 |
* This example is based on a simplified * two-dimensional heat equation domain decomposition. The initial * temperature is computed to be high in the middle of the domain and * zero at the boundaries. The boundaries are held at zero throughout * the simulation. During the time-stepping, an array containing two * domains is used; these domains alternate between old data and new data. * * The physical region, and the boundary conditions, are suggested by this diagram; u = 0 +------------------+ | | u = 100 | | u = 100 | | | | | | | | +------------------+ u = 100 Interrior point : u[Central] = (1/4) * ( u[North] + u[South] + u[East] + u[West] ) PARALLEL MPI VERSION : +-------------------+ | | P0 m=(n-2)/P +2 +-------------------+ | | P1 +-------------------+ n | | .. +-------------------+ | | Pq +-------------------+ <-------- n --------> <-------n-2 ------> ****************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <math.h> |
9cad13733 MAJ |
49 |
#include <mpi.h> |
3604dfae0 MPI version |
50 51 52 53 54 55 |
#define NN 50 #define MM 50 #define RING 100 #define ITER_PRINT 100 #define PRINT_DATA 1 |
a67a1dd2b rewrite error wit... |
56 |
#define _MAX_ITER 1000 |
3604dfae0 MPI version |
57 |
#define _EPSILON 0.01 |
a67a1dd2b rewrite error wit... |
58 |
|
3604dfae0 MPI version |
59 60 61 62 63 64 65 66 |
float update(int rank,int size, int nx,int ny, float *u, float *unew); void inidat(int rank, int size, int nx, int ny, float *u, float *unew); void prtdat(int rank, int size, int nx, int ny, float *u,const char *fnam); int main(int argc, char *argv[]) |
a67a1dd2b rewrite error wit... |
67 |
{ |
3604dfae0 MPI version |
68 |
int N=NN,M=MM; |
3604dfae0 MPI version |
69 |
float EPSILON=_EPSILON; |
a67a1dd2b rewrite error wit... |
70 71 72 73 |
int MAX_ITER= _MAX_ITER; int size,rank; |
1dea95fb9 add upper border ... |
74 |
/* INITIALIZE MPI */ |
3604dfae0 MPI version |
75 |
MPI_Init(&argc, &argv); |
1dea95fb9 add upper border ... |
76 77 |
/* GET THE PROCESSOR ID AND NUMBER OF PROCESSORS */ |
3604dfae0 MPI version |
78 79 80 81 82 |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); //Only Rank 0 read application parameters if(rank==0) { |
a67a1dd2b rewrite error wit... |
83 84 85 86 87 88 89 90 91 92 |
if(argc !=4) { fprintf(stderr,"usage %s N EPSILON MAX_ITER ", argv[0]); fprintf(stderr,"\t\twhere N is GRID size, EPSILON is Tolerance, MAX_ITER is max iteration "); fprintf(stderr,"\t\texample N = 100, EPSILON = 0.1, MAX_ITER=1000 "); return -1; } |
3604dfae0 MPI version |
93 94 |
N = M = atoi(argv[1]); EPSILON = atof(argv[2]); |
a67a1dd2b rewrite error wit... |
95 |
MAX_ITER = atoi (argv[3]); |
3604dfae0 MPI version |
96 97 98 99 100 101 102 |
if(N % size!=0) { fprintf(stderr,"Grid Size MUST be divisible by the number of processors !"); return -1; } } |
1dea95fb9 add upper border ... |
103 104 105 106 |
//Wait for rank 0 , all process start here MPI_Barrier(MPI_COMM_WORLD); //Exchange N |
a67a1dd2b rewrite error wit... |
107 |
MPI_Bcast(&N , 1, MPI_INT, 0 , MPI_COMM_WORLD); |
1dea95fb9 add upper border ... |
108 109 |
//Exchange EPSILON MPI_Bcast(&EPSILON , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD); |
a67a1dd2b rewrite error wit... |
110 111 112 |
//Exchange MAX_ITER MPI_Bcast(&MAX_ITER , 1, MPI_INT, 0 , MPI_COMM_WORLD); |
1dea95fb9 add upper border ... |
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 |
//local size M = (N-2) / size + 2 ; float *u = (float *)malloc(N * M * sizeof(float)); float *unew = (float *)malloc(N * M * sizeof(float)); if(u==0 || unew ==0) { perror("Can't allocated data "); return -1; } if(rank==0) { printf ( " " ); printf ( "HEATED_PLATE " ); printf ( " Parallel MPI version using %d processors ",size ); printf ( " A program to solve for the steady state temperature distribution " ); printf ( " over a rectangular plate. " ); printf ( " " ); printf ( " Spatial grid of %d by %d points. ", N, N ); printf ( " Each processor will use grid of %d by %d points. ", M, N ); printf ( " The iteration will end until tolerance <= %f ",EPSILON); } /* Initialize grid and create input file * each process initialize its part * */ inidat(rank,size,M,N,u,unew); prtdat(rank,size,M,N,u, "initial.dat"); float diff, globaldiff=1.0; int iter=0; /* * iterate (JACOBI ITERATION) until the new solution unew differs from the old solution u |
3604dfae0 MPI version |
165 |
* by no more than EPSILON. |
1dea95fb9 add upper border ... |
166 |
**/ |
a67a1dd2b rewrite error wit... |
167 |
while(globaldiff> EPSILON && iter < MAX_ITER) { |
3604dfae0 MPI version |
168 |
|
1dea95fb9 add upper border ... |
169 |
diff= update(rank,size,M,N, u, unew); |
3604dfae0 MPI version |
170 |
|
1dea95fb9 add upper border ... |
171 172 173 174 175 |
/** * COMPUTE GLOBAL CONVERGENCE * */ MPI_Allreduce(&diff, &globaldiff , 1, MPI_FLOAT, MPI_MAX, MPI_COMM_WORLD); |
3604dfae0 MPI version |
176 |
|
3604dfae0 MPI version |
177 178 179 |
if(rank==0) if(iter%ITER_PRINT==0) |
1dea95fb9 add upper border ... |
180 181 |
printf("Processor #%d, iteration %d, epsilon = %f ", rank,iter,globaldiff); |
3604dfae0 MPI version |
182 183 |
iter++; } |
a67a1dd2b rewrite error wit... |
184 |
|
3604dfae0 MPI version |
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
prtdat(rank,size,M,N, u, "final.dat"); free(u); free(unew); MPI_Finalize(); } /**************************************************************************** * subroutine update ****************************************************************************/ float update(int rank, int size, int nx,int ny, float *u, float *unew){ int ix, iy; float diff=0.0; |
3604dfae0 MPI version |
200 |
MPI_Status status; |
1dea95fb9 add upper border ... |
201 202 203 204 205 206 207 208 209 210 |
/* * EXCHANGE GHOST CELL */ if (rank > 0 && rank< size-1) { MPI_Sendrecv(&u[ny*(nx-2)], ny, MPI_FLOAT, rank+1, 0, &u[ny*0], ny, MPI_FLOAT, rank-1, 0, MPI_COMM_WORLD, &status); MPI_Sendrecv(&u[ny*1], ny, MPI_FLOAT, rank-1, 1, &u[ny*(nx-1)], ny, MPI_FLOAT, rank+1, 1, MPI_COMM_WORLD, &status); } |
3604dfae0 MPI version |
211 |
|
1dea95fb9 add upper border ... |
212 213 214 215 216 217 |
else if (rank == 0) MPI_Sendrecv(&u[ny*(nx-2)], ny, MPI_FLOAT, rank+1, 0, &u[ny*(nx-1)], ny, MPI_FLOAT, rank+1, 1, MPI_COMM_WORLD, &status); else if (rank == size-1) MPI_Sendrecv(&u[ny*1], ny, MPI_FLOAT, rank-1, 1, &u[ny*0], ny, MPI_FLOAT, rank-1, 0, MPI_COMM_WORLD, &status); |
3604dfae0 MPI version |
218 219 220 221 222 223 224 225 226 227 228 |
/** * PERFORM LOCAL COMPUTATION * */ for (ix = 1; ix < nx-1; ix++) { for (iy = 1; iy < ny-1; iy++) { unew[ix*ny+iy] = (u[(ix+1)*ny+iy] + u[(ix-1)*ny+iy] + u[ix*ny+iy+1] + u[ix*ny+iy-1] )/4.0 ; |
a67a1dd2b rewrite error wit... |
229 |
diff = fmax( diff, fabs(unew[ix*ny+iy] - u[ix*ny+iy])); |
3604dfae0 MPI version |
230 231 232 |
} } |
3604dfae0 MPI version |
233 234 235 236 237 |
for (ix = 1; ix < nx-1; ix++) { for (iy = 1; iy < ny-1; iy++) { u[ix*ny+iy] = unew[ix*ny+iy]; } } |
1dea95fb9 add upper border ... |
238 |
return diff; |
3604dfae0 MPI version |
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
} /***************************************************************************** * Initialize Data *****************************************************************************/ void inidat(int rank, int size,int nx, int ny, float *u, float *unew) { int ix, iy; /* *Set boundary data and interrior values * */ // interior points for (ix = 1; ix < nx-1; ix++) for (iy = 1; iy < ny-1; iy++) { u[ix*ny+iy]=0.0; } |
1dea95fb9 add upper border ... |
259 260 |
//boundary left for (ix = 0; ix < nx; ix++){ |
3604dfae0 MPI version |
261 262 263 264 265 |
u[ix*ny]=100.0; } //boundary right |
1dea95fb9 add upper border ... |
266 |
for (ix = 0; ix < nx; ix++){ |
3604dfae0 MPI version |
267 268 269 270 271 |
u[ix*ny+ (ny-1)]=100.0; } //boundary down |
1dea95fb9 add upper border ... |
272 273 274 275 276 277 278 279 280 281 |
for (iy = 1; iy < ny-1; iy++){ if(rank==size-1) { u[(nx-1)*(ny)+iy]=100.0; u[0]=100.0; }else { u[(nx-1)*(ny)+iy]=0.0; } |
3604dfae0 MPI version |
282 283 284 |
} //boundary top |
1dea95fb9 add upper border ... |
285 286 287 288 289 |
for (iy = 1; iy < ny-1; iy++){ if(rank==0) u[iy]=100.0; else u[iy]=0.0; |
3604dfae0 MPI version |
290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
} } /*************************************************************************** * Print Data to files **************************************************************************/ void print2file(int rank, int nx, int ny, float *u,const char *fname) { int ix, iy; FILE *fp; |
1dea95fb9 add upper border ... |
304 |
|
3604dfae0 MPI version |
305 |
char str[255]; |
1dea95fb9 add upper border ... |
306 |
|
3604dfae0 MPI version |
307 308 309 |
sprintf(str, "%d_%s", rank,fname); fp = fopen(str, "w"); |
1dea95fb9 add upper border ... |
310 |
|
3604dfae0 MPI version |
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
for (ix = 0 ; ix < nx; ix++) { for (iy =0; iy < ny; iy++) { fprintf(fp, "%6.2f ", u[ix*ny+iy]); } fputc ( ' ', fp); } fclose(fp); } void prtdat(int rank, int size,int nx, int ny, float *u,const char *fname) { if(ITER_PRINT==0)return; /** * USE TOKEN RING to write to unique file * 0 will rite first and then 1, 2, ... size-1 * * * */ |
1dea95fb9 add upper border ... |
338 |
print2file(rank,nx,ny,u,fname); |
3604dfae0 MPI version |
339 340 |
} |