MPI_Bcast未分配结果

时间:2018-12-02 13:05:37

标签: c mpi

我正尝试在循环中制作MPI_Bcast,以广播与流程对应的数组块的每个元素。问题在stencil()函数上,MPI_bcast在函数内部不起作用,最后程序中的每个过程都有结果,但没有其他过程的结果。 我知道这也许不是最好的解决方案,但我只需要这项工作,而且我不知道为什么它不起作用

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

  #define N 4
  #define ITERS 1
  #define ARRAY_SIZE (N+2) * (N+2)
  // N and ITERS might be input arguments

  double **A;

  long usecs (void)
  {
    struct timeval t;

    gettimeofday(&t,NULL);
    return t.tv_sec*1000000+t.tv_usec;
  }

  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 sendToAll(double **A){
    int i,j;

    for(i =0; i < N+2 ; i++){
      for(j =0; j < N+2 ; j++){
        MPI_Bcast( &A[i][j], 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
      }
    }
  }

  void sendResultToAll(double **A, int start, int end,int sender){
    int i = start;
    int j;

    for(i =start; i <= end; i++){
      for(j =0; j < N+2 ; j++){
        //if(sender == 0)
          //printf("rank = %d,i= %d j=%d\n",sender,i,j );
        MPI_Bcast( &A[i][j], 1, MPI_DOUBLE, sender, MPI_COMM_WORLD);
      }
    }
  }
  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 start, int end,int sender){
    int i = start;
    int j;
    for(i; i <= end; 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]);
          MPI_Bcast( &A[i][j], 1, MPI_DOUBLE, sender, MPI_COMM_WORLD);
      }
    }
  }


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

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

    //long t_start,t_end;
    //double tmp,time;
    //srand ( time(NULL) );


    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));
    }
    if(rank == 0){
      initialize(A);
    }
    sendToAll(A);

    int start = n_per_proc * rank +1;
    int end = n_per_proc * (rank+1);

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

    /*printf("rank = %d start = %d\n",rank, start );
    printf("rank = %d end = %d\n",rank, end );
    printf("%s\n");*/
    int idx;
    for(idx = 0; idx < ITERS; idx++){
      stencil(A, start,end,rank);
      //sendResultToAll(A,start,end,rank);
      MPI_Barrier(MPI_COMM_WORLD);
    }
    showArray(A);
    //time = ((double)(t_end-t_start))/1000000;
    //printf("Computation time = %f\n", time);

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

  }

0 个答案:

没有答案