Thread: MPI_Allreduce question

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

    MPI_Allreduce question

    In the code below. I use MPI_Allreduce in order to sum the old_chrome[x].fitness.

    Code:
    #include <stdio.h>
    #include <mpi.h>
    #include <time.h>
    #include <stdlib.h>
    
    
    struct chromosome{
     int fitness;
     int glob_fit;
    };
    struct chromosome  old_chrome[10];
    
    
    
    
    int main(int argc, char *argv[])
    {int i,id,p,j;
    
      MPI_Init(&argc,&argv);
      MPI_Comm_rank(MPI_COMM_WORLD,&id);
      MPI_Comm_size(MPI_COMM_WORLD,&p);
      
      for(i=0;i<10;i++)
        {old_chrome[i].fitness=i;}
    
     for(i=0;i<10;i++)
       {   printf("rank=%d  i=%d  value= %d\n",id,i,old_chrome[i].fitness);}
     
     printf("\n");
    
     MPI_Allreduce(&old_chrome[0].fitness,&old_chrome[0].glob_fit,10,MPI_INT,MPI_SUM,MPI_COMM_WORLD);
    
     for(i=0;i<10;i++)
        {
          printf("rank %d i= %d  value %d\n",id,i,old_chrome[i].glob_fit);}
        
    
    
    MPI_Finalize();
     return 0;
    }
    What happens is that for each old_chrome[i] i am taking the output only for the 5 first [0-4]. I guess that the number of elements in send buffer that I use (10) is half of the number needed (when I use 20 I am taking output from all) because I have two members in the structure (so need double memory) . Is there any way to have only the one member sended?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Can you post the code for these functions?

    Also you should either move your functions above main and get rid of the declarations or move your declarations above the main procedure. As they are now, they're only valid inside main.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Can you post the code for these functions?
    MPI is a standard library for distributed/parallel processing.
    http://en.wikipedia.org/wiki/Message_Passing_Interface

    >> Is there any way to have only the one member sent
    The easiest thing to do is to get your data into array form:
    Code:
    int fitness[10];
    int glob_fit[10];
    gg

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    THanks for the reply codeplug.
    Is there any other way to do this without changing these lines of code?

    Many thanks on behalf

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> without changing these lines of code?
    I believe MPI only supports the transmission contiguous data - or data in array form.

    What you have now is an array of "struct chromosome". So your only other choices that I can see are:

    1) send the entire "old_chrome" array, and use a custom reduction operator that only works on the "fitness" element. http://www.mpi-forum.org/docs/mpi22-...07.htm#Node107

    2) call MPI_Allreduce in a loop, reducing a single 'int fitness' at a time.

    Both are inefficient, putting more data on the network than what's needed.

    gg

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    thank you. This is what I was thinking. No way to avoid loading network
    many thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM