vectorType.c

#include<stdio.h>
#include<mpi.h>
#define SIZE 4

int main( int argc, char *argv[]){
   int numTasks, rank, source = 0, dest, tag = 1;
   float a[SIZE][SIZE] = {
  {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[SIZE];

  MPI_Status status;
  MPI_Datatype columnType;

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

   //MPI_Type_vector(count, blockLength, stride, oldType, &newType);
  MPI_Type_vector(SIZE, 1, SIZE, MPI_FLOAT, &columnType);
  MPI_Type_commit(&columnType);

   if(numTasks ==    SIZE){
     if(rank == 0){
       int i, j;
       for(j = 0; j < numTasks; j++){
               int count = 1;
         int dest = j;
         for(i = 0; i < numTasks; i++){
          MPI_Send(&a[i][j], count, columnType,    
            dest, tag, MPI_COMM_WORLD);
        }
      }
    }

    MPI_Recv(b, SIZE, MPI_FLOAT, source, tag, MPI_COMM_WORLD, &status);
    printf( "rank = %d b = %3.1f %3.1f %3.1f %3.1f\n",
      rank, b[0], b[1], b[2], b[3]);
  } else{
    printf( "Must Specify %d processors. Terminating.\n", SIZE);    
  }

  MPI_Type_free(&columnType);
  MPI_Finalize();
}

本文出自 “狼剩阿毛之笑傲江湖” 博客,谢绝转载!

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