MPI将结果发送到其他流程

时间:2018-12-01 23:14:59

标签: c mpi

这个程序,分割一个二维数组,对分配给每个进程的那一行行进行一些操作,但是我不知道如何发送结果以将其混合并在完整的数组上获得完整的结果,我想过一些解决方案,但太复杂了,如果有人知道一个简单的解决方案,我就会很有用。

  #include "mpi.h"
  #include <stdio.h>
  #include <stdlib.h>
  #include <math.h>
  #include <sys/time.h>

  #define N 12
  #define ITERS 10
  #define ARRAY_SIZE (N+2) * (N+2)

  double **A;

  void initialize (double **A)
  {
    int i,j;

     for(i =0; i < N+2 ; i++){
       for(j =0; j < N+2 ; j++){
        if(i== 0 || j == 0 || i == (N+1) || j == (N +1) )
          A[i][j] = 0.0;
        else
          A[i][j] = rand() % 10 + 1;
       }
     }
  }
  void showArray(double **A){
    int i,j;
    printf("\n");
    for(i =0 ; i < N+2 ; i++){
      for(j =0; j < N+2 ; j++){
        printf("%f, ",A[i][j]);
      }
      printf("\n");
    }
  }



  void stencil(double **A, int inicio, int final){
    int i = inicio;
    int j;
    for(i; i <= final; i++){
      for(j =1; j <= N ; j++){
          A[i][j] = 0.3 *( A[i][j] + A[i-1][j] + A[i+1][j] + A[i][j-1] + A[i][j+1]);
      }
    }
  }


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

    int rank, size,tag=1;
    char msg='A';
    double row;
    MPI_Status status;
    double *result_row;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    int n_per_proc = N/size;

    printf("Process # %d started \n", rank);
    MPI_Barrier(MPI_COMM_WORLD);

    A = malloc((N+2) * sizeof(double *));
    int i;
    for (i=0; i<N+2; i++) {
      A[i] = malloc((N+2) * sizeof(double));
    }

    initialize(A);
    //showArray(A);
    int inicio = n_per_proc * rank +1;
    int final = n_per_proc * (rank+1);

    if(rank == size-1)
      final = N;

    /*printf("rank = %d inicio = %d\n",rank, inicio );
    printf("rank = %d final = %d\n",rank, final );
    printf("%s\n");*/
    stencil(A, inicio,final);
    showArray(A);

    printf("Finishing proc %d\n", rank);
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Finalize();

  }

0 个答案:

没有答案