簡體   English   中英

MPI_ALL聚集以將本地最大值發送到所有進程,進程的錯誤終止

[英]MPI_ALLgather to send local maxima to all processes, bad termination of processes

嗨,我希望找到所有進程的局部最大值,然后將所有局部最大值傳遞給所有進程以制作單個陣列,然后使用MPI環形拓撲比較局部最大值,最后輸出全局最大值。

我可以更有效地執行MPI_ALLreduce,並且已經做到了,但是我想測試環形拓撲的效率並產生與allreduce相同的結果。 我正在遵循使用MPI_Allgather的教程,它返回了一些錯誤。 代碼如下:

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

    int rank, size;

    MPI_Init (&argc, &argv);      // initializes MPI
    //MPI_Comm comm;
    double max_store[4];
    double *rbuf;
    //MPI_Comm_size( comm, &rank);
    MPI_Comm_rank (MPI_COMM_WORLD, &rank); // get current MPI-process ID. O, 1, ...
    MPI_Comm_size (MPI_COMM_WORLD, &size); // get the total number of processes


    /* define how many integrals */
    const int n = 10;       

    double b[n] = {5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0,5.0};                    
    double a[n] = {-5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0,-5.0};  

    double result, mean;
    int m;

    const unsigned int N = 5;
    double max = -1;



    cout.precision(6);
    cout.setf(ios::fixed | ios::showpoint); 


    srand(time(NULL) * rank);  // each MPI process gets a unique seed

    m = 4;                // initial number of intervals

    // convert command-line input to N = number of points
    //N = atoi( argv[1] );


    for (unsigned int  i=0; i <=N; i++)
    {
        result = int_mcnd(f, a, b, n, m);
        mean = result/(pow(10,10));

        m = m*4; 
        if( mean > max) 
        {
         max = mean;
        }


    }
    //if ( rank < 4 && rank >= 0 ) 
        //{
         //max_store[rank] = max;
        //}





    printf("Process ID %i, local_max = %f\n",rank, max);


    // All processes get the global max, stored in place of the local max
    MPI_Allreduce( MPI_IN_PLACE, &max, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );

    printf("Process ID %d, global_max = %f\n",rank, max);

    rbuf = (double *)malloc(rank*4*sizeof(double)); 
    MPI_Allgather( max_store, 4, MPI_DOUBLE, rbuf, 4, MPI_DOUBLE, MPI_COMM_WORLD);
    //print the array containing max from each processor
    //int k;    
    //for( int k = 0; k < 4; k++ )
    //{
     //printf( "%1.5e\n", max_store[k]);
    //}



   double send_junk = max_store[0];
   double rec_junk;
    //double global_max;
    MPI_Status status;

  if(rank==0) 
  {
    MPI_Send(&send_junk, 4, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD); //  send data to process 1
  }
  if(rank==1) 
  {
    MPI_Recv(&rec_junk, 4, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status); // receive data from process 0
  }
  //check between process 0 and process 1 maxima
  if(rec_junk>=max_store[1])
  {
   rec_junk = max_store[0];
  }
  else
  {
   rec_junk = max_store[1];
  }
  send_junk = rec_junk;

  MPI_Send(&send_junk, 4, MPI_DOUBLE, 2, 0, MPI_COMM_WORLD); //  send data to process 2

  if(rank==2)
  {
   MPI_Recv(&rec_junk, 4, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status); // receive data from process 1
  }
  //check between process 1 and process 2 maxima
  if(rec_junk>=max_store[2])
  {
   rec_junk = rec_junk;
  }
  else
  {
   rec_junk = max_store[2];
  }
  send_junk = rec_junk;

  MPI_Send(&send_junk, 4, MPI_DOUBLE, 3, 0, MPI_COMM_WORLD); //  send data to process 3

  if(rank==3)
  {
   MPI_Recv(&rec_junk, 4, MPI_DOUBLE, 2, 0, MPI_COMM_WORLD, &status); // receive data from process 2
  }
  //check between process 2 and process 3 maxima 
  if(rec_junk>=max_store[3])
  {
   rec_junk = rec_junk;
  }
  else
  {
   rec_junk = max_store[3];
  }


  printf("global ring max = %f", rec_junk); 


  MPI_Finalize(); // programs should always perform a "graceful" shutdown

  return 0;
}

我很想知道如何在單個數組中發送最大值,並且所有進程都可以訪問它,以便我可以比較環形拓撲中的值。 非常感謝。

您沒有正確分配接收緩沖區。 它必須足夠大以存儲每個級別的4個條目。 您目前有:

rbuf = (double *)malloc(rank*4*sizeof(double));

什么時候應該

rbuf = (double *)malloc(size*4*sizeof(double));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM