structType.c

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

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

  typedef struct {
     float x, y, z;
     float velocity;
     int n;
     int type;
  } Particle;

  Particle p[NELEMENTS], particles[NELEMENTS];
  MPI_Datatype particleType, oldTypes[2];
   int blockCounts[2];
  MPI_Aint offsets[2], extent;

  MPI_Status status;

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

  offsets[0] = 0;
  oldTypes[0] = MPI_FLOAT;
  blockCounts[0] = 4;
    
  MPI_Type_extent(MPI_FLOAT, &extent);
  offsets[1] = 4 * extent;
  oldTypes[1] = MPI_INT;
  blockCounts[1] = 2;

  MPI_Type_struct(2, blockCounts, offsets, oldTypes, &particleType);
  MPI_Type_commit(&particleType);

   if(rank == 0){
     int i;
     for(i = 0; i < NELEMENTS; i++){
      particles[i].x = i * 1.0;
      particles[i].y = i * -1.0;
      particles[i].z = i * 1.0;
      particles[i].velocity = 0.25;
      particles[i].n = i;
      particles[i].type = i % 2;
    }
    
     for(i = 0; i < numTasks; i++){
      MPI_Send(particles, NELEMENTS, particleType,    
        i, tag, MPI_COMM_WORLD);
    }
  }

  MPI_Recv(p, NELEMENTS, particleType, source,    
                                 tag, MPI_COMM_WORLD, &status);
  printf( "rank = %d %3.2f %3.2f %3.2f %3.2f %d %d\n",
    rank, p[3].x, p[3].y, p[3].z,    
    p[3].velocity, p[3].n, p[3].type);    

  MPI_Type_free(&particleType);
  MPI_Finalize();
}

你可能感兴趣的:(职场,休闲,structType)