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?