[Mpi-22] MPI_Scatter structure's field

Christos Lamprakis cl5882 at [hidden]
Wed Jul 7 02:51:56 CDT 2010



Hello to everybody,

In the script below I dont know how to Scatter the structure's field genes.

I would like to scatter them in a chunk of 2.

So for N old_chrome and M genes I will have:

at node 0 old_chrome[0-N].genes[0-1]
at node 1 old_chrome[0-N].genes[0-2]
...
at node x old_chrome[0-N].genes[(M-2)to(M-1)]
Any help How to Scatter?
Many thanks

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
#include <time.h>
#include <limits.h>
#include "mylib.h"

#define NUM_OF_GENERATIONS 10000
#define NUM_OF_GENES 3
#define NUM_OF_CHROMES 5
#define NUM_OF_FORCES 3

truct chromosome{
  double fitness;
  double genes[NUM_OF_GENES];
  double up_limit[NUM_OF_GENES];
  double low_limit[NUM_OF_GENES];
};
struct chromosome  old_chrome[NUM_OF_CHROMES],  
new_chrome[NUM_OF_CHROMES], temp_chrome[NUM_OF_CHROMES];

  double forces[NUM_OF_FORCES];
  double matrix[NUM_OF_FORCES][NUM_OF_GENES];
  double matrix_local[NUM_OF_FORCES][NUM_OF_GENES];
  double forces_local[NUM_OF_FORCES];
  int CROSS_CHROME[NUM_OF_CHROMES];

int main(int argc, char* argv[])

{

   int size,rank,i,j,n;

int blockCounts[4] = {1,NUM_OF_GENES,NUM_OF_GENES,NUM_OF_GENES};
MPI_Datatype types[4];
MPI_Aint displace[4];
MPI_Datatype myStructType;
int base;

  srand48(time(NULL));

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

MPI_Address(old_chrome,displace);
MPI_Address(old_chrome[0].genes,displace+1 );
MPI_Address(old_chrome[0].up_limit, displace+2);
MPI_Address(old_chrome[0].low_limit, displace+3);

types[0] = MPI_DOUBLE;
types[1] = MPI_DOUBLE;
types[2] = MPI_DOUBLE;
types[3] = MPI_DOUBLE;

base = displace[0];
for(i=0; i<4; i++)
  {
   displace[i] -= base;
  }

MPI_Type_struct(4, blockCounts, displace, types, &myStructType);
MPI_Type_commit(&myStructType);

if(rank == 0)
  {
    for(i=0;i<4;i++)
      {
      for(j=0;j<2;j++)
      {
        old_chrome[i].genes[j]=drand48();
      }
      }
  }

  for(i=0;i<NUM_OF_CHROMES;i++)
     {
       printf("%d old_chrome[%d].genes[0]= %lf\n",rank,i,  
old_chrome[i].genes[0]);
     }

  MPI_Barrier(MPI_COMM_WORLD);

  for(i=0;i<NUM_OF_CHROMES;i++)
    {

       
MPI_Scatter(&old_chrome[0].genes[0],2,myStructType,&temp_chrome[0].genes[0],2,myStructType,0,MPI_COMM_WORLD);

    }
MPI_Barrier(MPI_COMM_WORLD);

  for(i=0;i<NUM_OF_CHROMES;i++)
    {  for(j=0;j<2;j++)
        {
         printf("\n%d temp_chrome[%d].genes[%d] = %lf\n",rank,i,j,  
temp_chrome[i].genes[j]);
        }
    }
  MPI_Finalize();
}



More information about the Mpi-22 mailing list