Thread: MPI question

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    MPI question

    I am using derived datatypes to broadcast array of structures as below. But what is happens is that I send only the half of what I would like. rank1 should have values (0-9) until i=9 like rank0. But it has (0-5) until i =4. Any idea?

    Code:
    #include <stdio.h>
    #include <mpi.h>
    #include <time.h>
    #include <stdlib.h>
    
    
    struct chromosome{
     double fitness;
     double glob_fit;
    };
    struct chromosome  old_chrome[100];
    
    
    
    
    int main(int argc, char *argv[])
    {int i,id,p,j,z;
    double arrayx[100];
     MPI_Datatype datatype;
     MPI_Datatype type1[2] = {MPI_DOUBLE,MPI_DOUBLE};
     int blocklen1[2]= {1,1};
     MPI_Aint disp1[2];
     MPI_Aint base;
    
    
    
    
    
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &id);
    MPI_Comm_size(MPI_COMM_WORLD, &p);
     
     MPI_Address(old_chrome,disp1);
     MPI_Address(old_chrome,disp1+1);
     base = disp1[0];
    
        for(i=0;i<2;i++){disp1[i] -= base;}
    
     MPI_Type_struct(2,blocklen1,disp1,type1,&datatype);
    
     MPI_Type_commit(&datatype);
    
    
    
    
    
     if(id==0){
      for(i=0;i<10;i++)
        {old_chrome[i].fitness=i;}
     }
    
     for(i=0;i<10;i++)
       {   printf("rank=%d  i=%d  value= %lf\n",id,i,old_chrome[i].fitness);}
     
     printf("\n");
    
     MPI_Bcast(&old_chrome,10,datatype,0,MPI_COMM_WORLD);
    
     for(i=0;i<10;i++)
        {
          printf("xixi rank %d i= %d  value %lf\n",id,i,old_chrome[i].fitness);}
        
    
    
    MPI_Finalize();
     return 0;
    }
    rank=0 i=0 value= 0.000000
    rank=0 i=1 value= 1.000000
    rank=0 i=2 value= 2.000000
    rank=0 i=3 value= 3.000000
    rank=0 i=4 value= 4.000000
    rank=0 i=5 value= 5.000000
    rank=0 i=6 value= 6.000000
    rank=0 i=7 value= 7.000000
    rank=0 i=8 value= 8.000000
    rank=0 i=9 value= 9.000000

    rank=1 i=0 value= 0.000000
    rank=1 i=1 value= 0.000000
    rank=1 i=2 value= 0.000000
    xixi rank 0 i= 0 value 0.000000
    rank=1 i=3 value= 0.000000
    xixi rank 0 i= 1 value 1.000000
    rank=1 i=4 value= 0.000000
    rank=1 i=5 value= 0.000000
    rank=1 i=6 value= 0.000000
    rank=1 i=7 value= 0.000000
    xixi rank 0 i= 2 value 2.000000
    rank=1 i=8 value= 0.000000
    rank=1 i=9 value= 0.000000
    xixi rank 0 i= 3 value 3.000000

    xixi rank 0 i= 4 value 4.000000
    xixi rank 0 i= 5 value 5.000000
    xixi rank 0 i= 6 value 6.000000
    xixi rank 0 i= 7 value 7.000000
    xixi rank 0 i= 8 value 8.000000
    xixi rank 0 i= 9 value 9.000000
    xixi rank 1 i= 0 value 0.000000
    xixi rank 1 i= 1 value 1.000000
    xixi rank 1 i= 2 value 2.000000
    xixi rank 1 i= 3 value 3.000000
    xixi rank 1 i= 4 value 4.000000
    xixi rank 1 i= 5 value 0.000000
    xixi rank 1 i= 6 value 0.000000
    xixi rank 1 i= 7 value 0.000000
    xixi rank 1 i= 8 value 0.000000
    xixi rank 1 i= 9 value 0.000000

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> MPI_Address(old_chrome, disp1);
    >> MPI_Address(old_chrome, disp1 + 1);
    The displacement array tells MPI where each element of your custom type begins. As you have it, both elements begin at 0.

    I believe it should be:
    MPI_Address(&old_chrome[0].fitness, disp1);
    MPI_Address(&old_chrome[0].glob_fit, disp1 + 1);

    >> MPI_Bcast(&old_chrome, 10, datatype, 0, MPI_COMM_WORLD);
    "old_chrome" is a pointer.
    "&old_chrome" is a pointer to a pointer.
    Remove the "&".

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MPI question
    By Chris_1980 in forum C Programming
    Replies: 3
    Last Post: 06-16-2010, 08:32 AM
  2. MPI communication question
    By waterborne in forum Tech Board
    Replies: 3
    Last Post: 06-05-2010, 04:12 PM
  3. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM