[Beowulf] problem of mpich-1.2.7p1

Gus Correa gus at ldeo.columbia.edu
Wed Feb 3 22:01:17 EST 2010


Hi Christian

Is the code  trying to
multiply two matrices using block decomposition?

In MPICH2 you need to establish passwordless ssh (not rsh!)
connection across your machines.
You also need to start the mpd daemon ring (although there is
now also the Hydra mechanism, but I am not familiar to it).
Finally you launch your program with mpirun (now called also mpiexec),
and you can pass your machine file in the command line (in MPICH1
you could do the same too).
"man mpirun" will help you.

In OpenMPI you also need to establish passwordless ssh connection
across the machines.
However, there is no daemon ring to start, making the process
easier.
Likewise, you can give a hostfile/machine file on the command line,
or you can list the hosts one by one in the command line.
In my opinion OpenMPI is easier to use and install
(and more flexible, and has more features).

I hope this helps,
Gus Correa

christian suhendra wrote:
> please take a look my source code..
> 
> mr.gus i wanna ask something about the mpich2
> if i swith my mpich1 to mpich2 where do i have supposed to put the list 
> of machine of each node..
> if in mpich1 the list in machines.LINUX..
> in mpich2 where is it??
> 
> about amdahl law..i still confused about that..^_^
> maybe i have to read many times so i can understand..
> 
> thank you mr.gus..
> i still need your advice
> 
> 
> 
> 
> regards
> christian

christian suhendra wrote:
 > hello mr.gus here is my source code..
 > when i compile that it works..
 >
 > #include <mpi.h>
 > #include <stdio.h>
 > #include <math.h>
 > #include <stdlib.h>
 >
 > #define N 100 /* 512*/
 >
 > void Print_Matrix(int x, int y, int myid,int S, int *M);
 > int readmat(char *fname, int *mat, int n);
 > int writemat(char *fname, int *mat, int n);
 >
 > int A[N][N],B[N][N],C[N][N];
 > int main(int argc, char **argv){
 >     int myid, S, M, nproc,source,dest;
 >     int i,j,k,m,l,repeat,temp,S2;
 >     int  namelen;
 >     char processor_name[MPI_MAX_PROCESSOR_NAME];
 >     MPI_Status status;
 >     double t1, t2,t3,t4;
 >
 >     int *T,*TA,*TB,*t,*TC;
 >     MPI_Comm GRID_COMM;
 >     int ndims,dims[2];
 >     int periods[2],coords[2],grid_rank;
 >
 >     MPI_Init(&argc, &argv);    /* initialization MPI */
 >     MPI_Comm_rank(MPI_COMM_WORLD,&myid);
 >     MPI_Comm_size(MPI_COMM_WORLD,&nproc);        /* #procesor */
 >     MPI_Get_processor_name(processor_name,&namelen);
 >
 >     printf("Process %d of %d on %s\n",myid, nproc, processor_name);
 >
 >     if(myid==0)    {
 >         /* read data from files: "A_file", "B_file"*/
 >         if (readmat("A_file.txt", (int *) A,N) < 0)
 >             exit( 1 + printf("file problem\n") );
 >         if (readmat("B_file.txt", (int *) B, N) < 0)
 >             exit( 1 + printf("file problem\n") );
 >         /*catat waktu*/
 >         t1=MPI_Wtime();
 >     }
 >     /* topologi*/
 >     M=(int)sqrt(nproc);
 >     S=N/M;  /*dimensi blok*/
 >     S2=S*S; /*dimensi blok*/
 >     dims[0]=dims[1]=M; /*dimensi topologi*/
 >     periods[0]=periods[1]=1;
 >     MPI_Cart_create(MPI_COMM_WORLD,2,dims,periods,0,&GRID_COMM);
 >
 >     MPI_Comm_rank(GRID_COMM,&grid_rank);
 >     MPI_Cart_coords(GRID_COMM,grid_rank,2,coords);
 >     myid=grid_rank;
 >     source=coords[0];
 >     dest=coords[1];
 >
 >     /*place for matrix input and output*/
 >     TA=(int *)malloc(sizeof(MPI_INT)*S2);
 >     TB=(int *)malloc(sizeof(MPI_INT)*S2);
 >     TC=(int *)malloc(sizeof(MPI_INT)*S2);
 >
 >     for(i=0; i<S2; i++)
 >     TC[i]=0;
 >
 >     /*start cannon*/
 >     if(myid==0)
 >         {
 >         T=(int *)malloc(sizeof(MPI_INT)*S2);
 >         t3=MPI_Wtime(); /*timing*/
 >         for(k=0; k<nproc; k++){
 >             MPI_Cart_coords(GRID_COMM,k,2,coords);
 >             if(k==0){
 >                 t=TA;
 >                 for(i=k; i<k+S; i++){
 >                     temp=(k*S)%N;
 >                     for(j=temp; j<temp+S; j++){
 >                         *t=A[i][j];
 >                         t++;
 >                     }
 >                 }
 >             }
 >             else{
 >                 t=T;
 >                 for(i=coords[0]*S; i<(coords[0]+1)*S; i++)
 >                     for(j=coords[1]*S; j<(coords[1]+1)*S; j++){
 >                         *t=A[i][j];
 >                         t++;
 >                     }
 >                 MPI_Send(T,S2,MPI_INT,k,0,GRID_COMM);
 >             }
 >         }
 >
 >     for(k=0; k<nproc; k++){
 >         MPI_Cart_coords(GRID_COMM,k,2,coords);
 >         if(k==0){
 >             t=TB;
 >             for(i=k; i<k+S; i++){
 >                 temp=(k*S)%N;
 >                 for(j=temp; j<temp+S; j++){
 >                     *t=B[i][j];
 >                     t++;
 >                     }
 >                 }
 >             }
 >         else{
 >             t=T;
 >             for(i=coords[0]*S; i<(coords[0]+1)*S; i++)
 >                 for(j=coords[1]*S; j<(coords[1]+1)*S; j++){
 >                     *t=B[i][j];
 >                     t++;
 >                     }
 >             MPI_Send(T,S2,MPI_INT,k,1,GRID_COMM);
 >             }
 >         }
 >
 >     coords[0]=source;
 >     coords[1]=dest;
 >     t4= MPI_Wtime();
 >
 >     /*matriks multiplication*/
 >     for(repeat=0; repeat<N/S; repeat++){
 >         for(i=0; i<S; i++){
 >             for(j=0; j<S; j++){
 >                 for(k=0; k<S; k++){
 >                     TC[i*S+j]+=TA[i*S+k]*TB[j+k*S];
 >                     }
 >                 }
 >             }
 >         /*swivel block & fill the value*/
 >         MPI_Cart_shift(GRID_COMM,1,-1,&source,&dest);
 >
 > MPI_Sendrecv_replace(TA,S2,MPI_INT,dest,0,source,0,GRID_COMM,&status);
 >
 >         MPI_Cart_shift(GRID_COMM,0,-1,&source,&dest);
 >
 > MPI_Sendrecv_replace(TB,S2,MPI_INT,dest,0,source,0,GRID_COMM,&status);
 >         }
 >
 >     for(i=0; i<S; i++)
 >         for(j=0; j<S; j++){
 >             C[i][j]=TC[i*S+j];
 >             }
 >
 >     for(i=1; i<nproc; i++){
 >         l=0;
 >         m=0;
 >
 > 
MPI_Recv(T,S2,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,GRID_COMM,&status);
 >         MPI_Cart_coords(GRID_COMM,status.MPI_TAG,2,coords);
 >
 >         for(j=coords[0]*S; j<(coords[0]+1)*S; j++){
 >             for(k=coords[1]*S; k<(coords[1]+1)*S; k++){
 >                 C[j][k]=T[l*S+m];
 >                 m++;
 >             }
 >             l++;
 >             m=0;
 >         }
 >     }
 >
 >     t2= MPI_Wtime();
 >     printf("Total Time: %lf msecs \n",(t2 - t1) / 0.001);
 >     //printf("Transmit Time: %lf msecs \n",(t4 - t3) / 0.001);
 >     writemat("C_file_par", (int *) C, N);
 >
 >     free(T);
 >     free(TA);
 >     free(TB);
 >     free(TC);
 >     }
 > else
 >     {
 >     MPI_Recv(TA,S2,MPI_INT,0,0,GRID_COMM,&status);
 >     MPI_Recv(TB,S2,MPI_INT,0,1,GRID_COMM,&status);
 >
 >     MPI_Cart_shift(GRID_COMM,1,-coords[0],&source,&dest);
 > 
MPI_Sendrecv_replace(TA,S2,MPI_INT,dest,0,source,0,GRID_COMM,&status);
 >
 >     MPI_Cart_shift(GRID_COMM,0,-coords[1],&source,&dest);
 > 
MPI_Sendrecv_replace(TB,S2,MPI_INT,dest,0,source,0,GRID_COMM,&status);
 >     for(repeat=0; repeat<N/S; repeat++){
 >         for(i=0; i<S; i++){
 >             for(j=0; j<S; j++){
 >                 for(k=0; k<S; k++){
 >                     TC[i*S+j]+=TA[i*S+k]*TB[j+k*S];
 >                     }
 >                 }
 >             }
 >
 >         MPI_Cart_shift(GRID_COMM,1,-1,&source,&dest);
 >
 > MPI_Sendrecv_replace(TA,S2,MPI_INT,dest,0,source,0,GRID_COMM,&status);
 >
 >         MPI_Cart_shift(GRID_COMM,0,-1,&source,&dest);
 >
 > MPI_Sendrecv_replace(TB,S2,MPI_INT,dest,0,source,0,GRID_COMM,&status);
 >         }
 >
 >     MPI_Send(TC,S2,MPI_INT,0,myid,GRID_COMM);
 >     free(TA);
 >     free(TB);
 >     free(TC);
 >     }
 > MPI_Finalize();
 > return(0);
 > }
 > /*function of read the input and put to the output/
 > #define _mat(i,j) (mat[(i)*n + (j)])
 > int readmat(char *fname, int *mat, int n){
 > FILE *fp;
 > int  i, j;
 > if ((fp = fopen(fname, "r")) == NULL)
 >     return (-1);
 > for (i = 0; i < n; i++)
 >     for (j = 0; j < n; j++)
 >         if (fscanf(fp, "%d", &_mat(i,j)) == EOF){
 >             fclose(fp);
 >             return (-1);
 >             };
 > fclose(fp);
 > return (0);
 > }
 > int writemat(char *fname, int *mat, int n){
 > FILE *fp;
 > int  i, j;
 >
 > if ((fp = fopen(fname, "w")) == NULL)
 >     return (-1);
 > for (i = 0; i < n; fprintf(fp, "\n"), i++)
 >     for (j = 0; j < n; j++)
 >         fprintf(fp, "%d ", _mat(i, j));
 > fclose(fp);
 > return (0);
 > }
 > void Print_Matrix(int x, int y, int myid, int S, int *M){
 > int i,j;
 > printf("myid:%d\n",myid);
 > for(i=0; i<x; i++)
 >     {
 >     for(j=0; j<y; j++)
 >         printf(" %d ",M[i*S+j]);
 >     printf("\n");
 >     }
 > }
 >


_______________________________________________
Beowulf mailing list, Beowulf at beowulf.org sponsored by Penguin Computing
To change your subscription (digest mode or unsubscribe) visit http://www.beowulf.org/mailman/listinfo/beowulf



More information about the Beowulf mailing list