From 1dea95fb9241ca7e1e9b578aca2b2d0aced9efdf Mon Sep 17 00:00:00 2001 From: kmazouzi Date: Thu, 9 Apr 2015 11:43:15 +0200 Subject: [PATCH] add upper border to 100 --- lab3/mpi_heat2D.c | 204 ++++++++++++++++++++++++++--------------------------- 1 file changed, 101 insertions(+), 103 deletions(-) diff --git a/lab3/mpi_heat2D.c b/lab3/mpi_heat2D.c index 57d10d0..cb64a3f 100644 --- a/lab3/mpi_heat2D.c +++ b/lab3/mpi_heat2D.c @@ -73,10 +73,10 @@ int main(int argc, char *argv[]) float EPSILON=_EPSILON; - /* INITIALIZE MPI */ + /* INITIALIZE MPI */ MPI_Init(&argc, &argv); - - /* GET THE PROCESSOR ID AND NUMBER OF PROCESSORS */ + + /* GET THE PROCESSOR ID AND NUMBER OF PROCESSORS */ MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); @@ -90,7 +90,7 @@ int main(int argc, char *argv[]) fprintf(stderr,"\t\texample N = 100, EPSILON = 0.1\n"); return -1; } - + N = M = atoi(argv[1]); EPSILON = atof(argv[2]); @@ -102,64 +102,71 @@ int main(int argc, char *argv[]) } - //Wait for rank 0 , all process start here - MPI_Barrier(MPI_COMM_WORLD); - - //Exchange N - MPI_Bcast(&N , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD); - //Exchange EPSILON - MPI_Bcast(&EPSILON , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD); - - //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\n"); - return -1; - } - - if(rank==0) { - - printf ( "\n" ); - printf ( "HEATED_PLATE\n" ); - printf ( " Parallel MPI version using %d processors \n",size ); - printf ( " A program to solve for the steady state temperature distribution\n" ); - printf ( " over a rectangular plate.\n" ); - printf ( "\n" ); - printf ( " Spatial grid of %d by %d points.\n", N, N ); - printf ( " Each processor will use grid of %d +2 by %d points.\n", M-2, N ); - printf ( " The iteration will end until tolerance <= %f\n\n",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"); - - - /* - * iterate until the new solution unew differs from the old solution u + //Wait for rank 0 , all process start here + MPI_Barrier(MPI_COMM_WORLD); + + //Exchange N + MPI_Bcast(&N , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD); + //Exchange EPSILON + MPI_Bcast(&EPSILON , 1, MPI_FLOAT, 0 , MPI_COMM_WORLD); + + //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\n"); + return -1; + } + + if(rank==0) { + + printf ( "\n" ); + printf ( "HEATED_PLATE\n" ); + printf ( " Parallel MPI version using %d processors \n",size ); + printf ( " A program to solve for the steady state temperature distribution\n" ); + printf ( " over a rectangular plate.\n" ); + printf ( "\n" ); + printf ( " Spatial grid of %d by %d points.\n", N, N ); + printf ( " Each processor will use grid of %d by %d points.\n", M, N ); + printf ( " The iteration will end until tolerance <= %f\n\n",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 * by no more than EPSILON. - * */ + **/ + + while(globaldiff> EPSILON) { - float diff=1.0; - int iter=0; + diff= update(rank,size,M,N, u, unew); - while(diff> EPSILON) { + /** + * COMPUTE GLOBAL CONVERGENCE + * */ + + MPI_Allreduce(&diff, &globaldiff , 1, MPI_FLOAT, MPI_MAX, MPI_COMM_WORLD); - diff= update(rank,size,M,N, u, unew); if(rank==0) if(iter%ITER_PRINT==0) - printf("Processor #%d, iteration %d, epsilon = %f\n ", rank,iter,diff); + printf("Processor #%d, iteration %d, epsilon = %f\n ", rank,iter,globaldiff); iter++; } @@ -177,27 +184,26 @@ int main(int argc, char *argv[]) float update(int rank, int size, int nx,int ny, float *u, float *unew){ int ix, iy; float diff=0.0; - float globaldiff; MPI_Status status; - /* - * 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); - } + /* + * 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); + } - else if (rank == 0 && rank< size-1) - 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> 0 && 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); + 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); @@ -218,19 +224,6 @@ float update(int rank, int size, int nx,int ny, float *u, float *unew){ } - /** - * COMPUTE GLOBAL CONVERGENCE - * - * */ - - MPI_Allreduce(&diff, &globaldiff , 1, MPI_FLOAT, MPI_MAX, MPI_COMM_WORLD); - - - /** - * COPY OLD DATA - * */ - - for (ix = 1; ix < nx-1; ix++) { for (iy = 1; iy < ny-1; iy++) { u[ix*ny+iy] = unew[ix*ny+iy]; @@ -238,7 +231,7 @@ float update(int rank, int size, int nx,int ny, float *u, float *unew){ } - return globaldiff; + return diff; } /***************************************************************************** @@ -260,32 +253,37 @@ void inidat(int rank, int size,int nx, int ny, float *u, float *unew) u[ix*ny+iy]=0.0; } - //boundary left - for (ix = 1; ix < nx-1; ix++){ + //boundary left + for (ix = 0; ix < nx; ix++){ u[ix*ny]=100.0; } //boundary right - for (ix = 1; ix < nx-1; ix++){ + for (ix = 0; ix < nx; ix++){ u[ix*ny+ (ny-1)]=100.0; } //boundary down - for (iy = 0; iy < ny; iy++){ - - if(rank==size-1) { - u[(nx-1)*(ny)+iy]=100.0; - }else - { - u[(nx-1)*(ny)+iy]=0.0; - } + 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; + } } //boundary top - for (iy = 0; iy < ny; iy++){ - u[iy]=0.0; + for (iy = 1; iy < ny-1; iy++){ + if(rank==0) + u[iy]=100.0; + else + u[iy]=0.0; } @@ -300,13 +298,13 @@ void print2file(int rank, int nx, int ny, float *u,const char *fname) { int ix, iy; FILE *fp; - + char str[255]; - + sprintf(str, "%d_%s", rank,fname); fp = fopen(str, "w"); - + for (ix = 0 ; ix < nx; ix++) { for (iy =0; iy < ny; iy++) { @@ -335,7 +333,7 @@ void prtdat(int rank, int size,int nx, int ny, float *u,const char *fname) * * */ - print2file(rank,nx,ny,u,fname); + print2file(rank,nx,ny,u,fname); } -- 1.7.10.4