indexType

#include<stdio.h>
#include<mpi.h>
#define NELEMENTS 6    

int main( int argc, char *argv[]){
   int numTasks, rank, source = 0, dest, tag = 1;
   int blockLengths[2], displacements[2];

   float a[16] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,    
                9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0};

   float b[NELEMENTS];

  MPI_Status status;
  MPI_Datatype indexType;

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

  blockLengths[0] = 4;
  displacements[0] = 5;

  blockLengths[1] = 2;
  displacements[1] = 12;

  MPI_Type_indexed(2, blockLengths, displacements, MPI_FLOAT, &indexType);
  MPI_Type_commit(&indexType);


   if(rank == 0){
     int i;
     int count = 1;
     for(i = 0; i < numTasks; i++){
       int dest = i;
      MPI_Send(a, count, indexType, dest, tag, MPI_COMM_WORLD);
    }
  }

  MPI_Recv(b, NELEMENTS, MPI_FLOAT, source, tag, MPI_COMM_WORLD, &status);
  printf( "rank = %d b = %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f\n",
      rank, b[0], b[1], b[2], b[3], b[4], b[5]);

  MPI_Type_free(&indexType);
  MPI_Finalize();
}
相關文章
相關標籤/搜索