Commit 1dea95fb9241ca7e1e9b578aca2b2d0aced9efdf

Authored by kmazouzi
1 parent 3604dfae01
Exists in master

add upper border to 100

Showing 1 changed file with 101 additions and 103 deletions Side-by-side Diff

lab3/mpi_heat2D.c View file @ 1dea95f
... ... @@ -73,10 +73,10 @@
73 73 float EPSILON=_EPSILON;
74 74  
75 75  
76   - /* INITIALIZE MPI */
  76 + /* INITIALIZE MPI */
77 77 MPI_Init(&argc, &argv);
78   -
79   - /* GET THE PROCESSOR ID AND NUMBER OF PROCESSORS */
  78 +
  79 + /* GET THE PROCESSOR ID AND NUMBER OF PROCESSORS */
80 80 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
81 81 MPI_Comm_size(MPI_COMM_WORLD, &size);
82 82  
... ... @@ -90,7 +90,7 @@
90 90 fprintf(stderr,"\t\texample N = 100, EPSILON = 0.1\n");
91 91 return -1;
92 92 }
93   -
  93 +
94 94 N = M = atoi(argv[1]);
95 95 EPSILON = atof(argv[2]);
96 96  
97 97  
98 98  
99 99  
100 100  
101 101  
102 102  
103 103  
104 104  
105 105  
106 106  
107 107  
108 108  
109 109  
110 110  
111 111  
... ... @@ -102,64 +102,71 @@
102 102  
103 103 }
104 104  
105   - //Wait for rank 0 , all process start here
106   - MPI_Barrier(MPI_COMM_WORLD);
  105 + //Wait for rank 0 , all process start here
  106 + MPI_Barrier(MPI_COMM_WORLD);
107 107  
108   - //Exchange N
109   - MPI_Bcast(&N , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD);
110   - //Exchange EPSILON
111   - MPI_Bcast(&EPSILON , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD);
  108 + //Exchange N
  109 + MPI_Bcast(&N , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD);
  110 + //Exchange EPSILON
  111 + MPI_Bcast(&EPSILON , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD);
112 112  
113   - //local size
114   - M = (N-2)/size + 2;
  113 + //local size
  114 + M = (N-2) / size + 2 ;
115 115  
116   - float *u = (float *)malloc(N * M * sizeof(float));
117   - float *unew = (float *)malloc(N * M * sizeof(float));
  116 + float *u = (float *)malloc(N * M * sizeof(float));
  117 + float *unew = (float *)malloc(N * M * sizeof(float));
118 118  
119   - if(u==0 || unew ==0)
120   - {
121   - perror("Can't allocated data\n");
122   - return -1;
123   - }
  119 + if(u==0 || unew ==0)
  120 + {
  121 + perror("Can't allocated data\n");
  122 + return -1;
  123 + }
124 124  
125   - if(rank==0) {
126   -
127   - printf ( "\n" );
128   - printf ( "HEATED_PLATE\n" );
129   - printf ( " Parallel MPI version using %d processors \n",size );
130   - printf ( " A program to solve for the steady state temperature distribution\n" );
131   - printf ( " over a rectangular plate.\n" );
132   - printf ( "\n" );
133   - printf ( " Spatial grid of %d by %d points.\n", N, N );
134   - printf ( " Each processor will use grid of %d +2 by %d points.\n", M-2, N );
135   - printf ( " The iteration will end until tolerance <= %f\n\n",EPSILON);
136   -
137   - }
138   -
139   - /* Initialize grid and create input file
140   - * each process initialize its part
141   - * */
  125 + if(rank==0) {
142 126  
143   - inidat(rank,size,M,N,u,unew);
  127 + printf ( "\n" );
  128 + printf ( "HEATED_PLATE\n" );
  129 + printf ( " Parallel MPI version using %d processors \n",size );
  130 + printf ( " A program to solve for the steady state temperature distribution\n" );
  131 + printf ( " over a rectangular plate.\n" );
  132 + printf ( "\n" );
  133 + printf ( " Spatial grid of %d by %d points.\n", N, N );
  134 + printf ( " Each processor will use grid of %d by %d points.\n", M, N );
  135 + printf ( " The iteration will end until tolerance <= %f\n\n",EPSILON);
144 136  
145   - prtdat(rank,size,M,N,u, "initial.dat");
  137 + }
146 138  
  139 + /* Initialize grid and create input file
  140 + * each process initialize its part
  141 + * */
147 142  
148   - /*
149   - * iterate until the new solution unew differs from the old solution u
  143 + inidat(rank,size,M,N,u,unew);
  144 +
  145 + prtdat(rank,size,M,N,u, "initial.dat");
  146 +
  147 +
  148 + float diff, globaldiff=1.0;
  149 + int iter=0;
  150 +
  151 + /*
  152 + * iterate (JACOBI ITERATION) until the new solution unew differs from the old solution u
150 153 * by no more than EPSILON.
151   - * */
  154 + **/
152 155  
153   - float diff=1.0;
154   - int iter=0;
  156 + while(globaldiff> EPSILON) {
155 157  
156   - while(diff> EPSILON) {
  158 + diff= update(rank,size,M,N, u, unew);
157 159  
158   - diff= update(rank,size,M,N, u, unew);
  160 + /**
  161 + * COMPUTE GLOBAL CONVERGENCE
  162 + * */
159 163  
  164 + MPI_Allreduce(&diff, &globaldiff , 1, MPI_FLOAT, MPI_MAX, MPI_COMM_WORLD);
  165 +
  166 +
160 167 if(rank==0)
161 168 if(iter%ITER_PRINT==0)
162   - printf("Processor #%d, iteration %d, epsilon = %f\n ", rank,iter,diff);
  169 + printf("Processor #%d, iteration %d, epsilon = %f\n ", rank,iter,globaldiff);
163 170 iter++;
164 171 }
165 172  
166 173  
167 174  
... ... @@ -177,27 +184,26 @@
177 184 float update(int rank, int size, int nx,int ny, float *u, float *unew){
178 185 int ix, iy;
179 186 float diff=0.0;
180   - float globaldiff;
181 187 MPI_Status status;
182 188  
183 189  
184   - /*
185   - * EXCHANGE GHOST CELL
186   - */
187   - if (rank > 0 && rank< size-1)
188   - {
189   - MPI_Sendrecv(&u[ny*(nx-2)], ny, MPI_FLOAT, rank+1, 0,
190   - &u[ny*0], ny, MPI_FLOAT, rank-1, 0, MPI_COMM_WORLD, &status);
191   - MPI_Sendrecv(&u[ny*1], ny, MPI_FLOAT, rank-1, 1,
192   - &u[ny*(nx-1)], ny, MPI_FLOAT, rank+1, 1, MPI_COMM_WORLD, &status);
193   - }
  190 + /*
  191 + * EXCHANGE GHOST CELL
  192 + */
  193 + if (rank > 0 && rank< size-1)
  194 + {
  195 + MPI_Sendrecv(&u[ny*(nx-2)], ny, MPI_FLOAT, rank+1, 0,
  196 + &u[ny*0], ny, MPI_FLOAT, rank-1, 0, MPI_COMM_WORLD, &status);
  197 + MPI_Sendrecv(&u[ny*1], ny, MPI_FLOAT, rank-1, 1,
  198 + &u[ny*(nx-1)], ny, MPI_FLOAT, rank+1, 1, MPI_COMM_WORLD, &status);
  199 + }
194 200  
195   - else if (rank == 0 && rank< size-1)
196   - MPI_Sendrecv(&u[ny*(nx-2)], ny, MPI_FLOAT, rank+1, 0,
197   - &u[ny*(nx-1)], ny, MPI_FLOAT, rank+1, 1, MPI_COMM_WORLD, &status);
198   - else if ( rank> 0 && rank == size-1)
199   - MPI_Sendrecv(&u[ny*1], ny, MPI_FLOAT, rank-1, 1,
200   - &u[ny*0], ny, MPI_FLOAT, rank-1, 0, MPI_COMM_WORLD, &status);
  201 + else if (rank == 0)
  202 + MPI_Sendrecv(&u[ny*(nx-2)], ny, MPI_FLOAT, rank+1, 0,
  203 + &u[ny*(nx-1)], ny, MPI_FLOAT, rank+1, 1, MPI_COMM_WORLD, &status);
  204 + else if (rank == size-1)
  205 + MPI_Sendrecv(&u[ny*1], ny, MPI_FLOAT, rank-1, 1,
  206 + &u[ny*0], ny, MPI_FLOAT, rank-1, 0, MPI_COMM_WORLD, &status);
201 207  
202 208  
203 209  
... ... @@ -218,19 +224,6 @@
218 224 }
219 225  
220 226  
221   - /**
222   - * COMPUTE GLOBAL CONVERGENCE
223   - *
224   - * */
225   -
226   - MPI_Allreduce(&diff, &globaldiff , 1, MPI_FLOAT, MPI_MAX, MPI_COMM_WORLD);
227   -
228   -
229   - /**
230   - * COPY OLD DATA
231   - * */
232   -
233   -
234 227 for (ix = 1; ix < nx-1; ix++) {
235 228 for (iy = 1; iy < ny-1; iy++) {
236 229 u[ix*ny+iy] = unew[ix*ny+iy];
... ... @@ -238,7 +231,7 @@
238 231 }
239 232  
240 233  
241   - return globaldiff;
  234 + return diff;
242 235 }
243 236  
244 237 /*****************************************************************************
245 238  
246 239  
247 240  
... ... @@ -260,32 +253,37 @@
260 253 u[ix*ny+iy]=0.0;
261 254 }
262 255  
263   - //boundary left
264   - for (ix = 1; ix < nx-1; ix++){
  256 + //boundary left
  257 + for (ix = 0; ix < nx; ix++){
265 258 u[ix*ny]=100.0;
266 259  
267 260 }
268 261  
269 262 //boundary right
270   - for (ix = 1; ix < nx-1; ix++){
  263 + for (ix = 0; ix < nx; ix++){
271 264 u[ix*ny+ (ny-1)]=100.0;
272 265  
273 266 }
274 267  
275 268 //boundary down
276   - for (iy = 0; iy < ny; iy++){
277   -
278   - if(rank==size-1) {
279   - u[(nx-1)*(ny)+iy]=100.0;
280   - }else
281   - {
282   - u[(nx-1)*(ny)+iy]=0.0;
283   - }
  269 + for (iy = 1; iy < ny-1; iy++){
  270 +
  271 + if(rank==size-1) {
  272 + u[(nx-1)*(ny)+iy]=100.0;
  273 + u[0]=100.0;
  274 +
  275 + }else
  276 + {
  277 + u[(nx-1)*(ny)+iy]=0.0;
  278 + }
284 279 }
285 280  
286 281 //boundary top
287   - for (iy = 0; iy < ny; iy++){
288   - u[iy]=0.0;
  282 + for (iy = 1; iy < ny-1; iy++){
  283 + if(rank==0)
  284 + u[iy]=100.0;
  285 + else
  286 + u[iy]=0.0;
289 287  
290 288 }
291 289  
292 290  
293 291  
... ... @@ -300,13 +298,13 @@
300 298 {
301 299 int ix, iy;
302 300 FILE *fp;
303   -
  301 +
304 302 char str[255];
305   -
  303 +
306 304 sprintf(str, "%d_%s", rank,fname);
307 305  
308 306 fp = fopen(str, "w");
309   -
  307 +
310 308 for (ix = 0 ; ix < nx; ix++) {
311 309 for (iy =0; iy < ny; iy++) {
312 310  
... ... @@ -335,7 +333,7 @@
335 333 * * */
336 334  
337 335  
338   - print2file(rank,nx,ny,u,fname);
  336 + print2file(rank,nx,ny,u,fname);
339 337  
340 338 }