[mpich-discuss] Error with more processors---Problem description

yeliu at abo.fi yeliu at abo.fi
Sun Dec 14 12:51:56 CST 2008


Hello,
   I don't know if this is enough. I attach part of main method,  
master method and slave method.I think the comments can express the  
program behavior.
and I run with mpiexec -n 4 ./programname
Thank you very much!



----------------------------------------
  #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <mpi.h>

    int p,me;/*p is number of processors, me is the id of processor.*/
    MPI_Status status;
    static int height=800; /*total number of rows*/


  enum{
      tag_work=1,
      tag_done=2,
      tag_newregion=3,
      tag_close=4
    };

  int main(int argc,char*argv[]){
   MPI_Init(&argc,&argv);
     MPI_Comm_size(MPI_COMM_WORLD,&p);
     MPI_Comm_rank(MPI_COMM_WORLD,&me);

if(me==0){
      do_master();
    }

    else{
        do_slave();
    }
    MPI_Finalize();
       exit(0);
   }

void do_master(){

int row=0;
int k;
int total=height;
ins s;
int donerow;

   /*master send initial rows to each processor,*/
     for(k=1;k<p;k++){
       MPI_Send(&row,1,MPI_INT,k,tag_work,MPI_COMM_WORLD);
       ++row;
     }
     /*do this until no row to send*/
     do{
       /*receive a done msg from any slave*/
       MPI_Recv      
(&donerow,1,MPI_INT,MPI_ANY_SOURCE,tag_done,MPI_COMM_WORLD,&status);
       s=status.MPI_SOURCE;
       total--; /*whenever the master receive a message' tag_done',  
the total num of rows decrease by 1 */
       /*if there is still row to send,send to the slave,else set  
row=-1,send to the slave*/
       if(row<height){
         MPI_Send(&row,1,MPI_INT,s,tag_work,MPI_COMM_WORLD);
         row++;
       }
        else{
         row=-1;
          MPI_Send(&row,1,MPI_INT,s,tag_work,MPI_COMM_WORLD);
        }
     }while(total>0);
}


void do_slave(){
int recrow; /*buffer for receiving message*/
   /*if there is row to receive,do this*/
do{

       MPI_Recv(&recrow,1,MPI_INT,0,tag_work,MPI_COMM_WORLD,&status);
       int y=recrow;
       /*if received row=-1, exit the loop*/
         if(y==-1){
         break;
        }
     /*when slave receive a row ,send a tag_done message to master*/
        MPI_Send(&recrow,1,MPI_INT,0,tag_done,MPI_COMM_WORLD);

     }while(y!=-1);

}





More information about the mpich-discuss mailing list