【问题标题】:MPI status gets wrong senderMPI 状态收到错误的发件人
【发布时间】:2018-06-18 20:18:44
【问题描述】:

我正在使用 MPI 解决负载平衡问题:主进程将任务发送给从属进程,并在它们计算并发回工作时收集结果。 由于我想尽可能提高性能,我使用非阻塞通信:Master 发送多个任务,然后等到一个进程发回其响应,以便 Master 可以向它发送额外的工作等等。

我使用 MPI_Waitany(),因为我事先不知道哪个从进程首先响应,然后我从状态中获取发送者,然后我可以将新作业发送给它。

我的问题是有时我收到的发件人是错误的(不在 MPI_COMM_WORLD 中的排名)并且程序崩溃;其他时候效果很好。

这是代码。谢谢!

//master
if (rank == 0) {

    int N_chunks = 10;
    MPI_Request request[N_chunks];
    MPI_Status status[N_chunks];
    int N_computed = 0;
    int dest,index_completed;

    //initialize array of my data structure
    vec send[N_chunks];
    vec recv[N_chunks];


    //send one job to each process in communicator
    for(int i=1;i<size;i++){

      MPI_Send( &send[N_computed], 1, mpi_vec_type, i, tag, MPI_COMM_WORLD);
      MPI_Irecv(&recv[N_computed], 1, mpi_vec_type, i, tag, MPI_COMM_WORLD,  
                &request[N_computed]);
      N_computed++;
    }

    // loop
    while (N_computed < N_chunks){

      //get processed messages
      MPI_Waitany(N_computed,request,&index_completed,status);

      //get sender ID dest
      dest = status[index_completed].MPI_SOURCE;

      //send a new job to that process
      MPI_Send( &send[N_computed], 1, mpi_vec_type, dest, tag, MPI_COMM_WORLD);
      MPI_Irecv(&recv[N_computed], 1, mpi_vec_type, dest, tag, MPI_COMM_WORLD,
                &request[N_computed]);

      N_computed++;
  }

  MPI_Waitall(N_computed,request,status);

  //close all process
  printf("End master\n");
}

【问题讨论】:

    标签: c parallel-processing mpi


    【解决方案1】:

    你没有正确使用MPI_Waitany()。

    应该是

    MPI_Status status;
    MPI_Waitany(N_computed,request,&index_completed,&status);
    dest = status.MPI_SOURCE;
    

    注意:

    • 你需要一个额外的循环去MPI_Wait()最后一个size - 1请求
    • 您可以修改算法并使用MPI_Request request[size-1];,从而节省一些内存

    【讨论】:

    • 哇——我从来没有想过代码会继续使用新的请求对象,所以我完全误解了这个问题。
    【解决方案2】:

    我忘了在最初的帖子中添加一行,我在其中等待所有待处理的请求:顺便说一句,如果我每次执行 Waitany 时都初始化一个新状态,则主进程崩溃;我需要跟踪哪些进程仍在等待等待正确的次数...... 顺便谢谢

    编辑:即使我发现它不是很聪明,它现在也可以工作;是否可以在开始时初始化一个 MPI_Status 数组,而不是每次等待之前都这样做?

    //master
    if (rank == 0) {
    
        int N_chunks = 10;
        MPI_Request request[size-1];
        int N_computed = 0;
        int dest;
        int index_completed;
    
        //initialize array of vec
        vec send[N_chunks];
        vec recv[N_chunks];
    
    
    
        //initial case
        for(int i=1;i<size;i++){
    
          MPI_Send(&send[N_computed], 1, mpi_vec_type, i, tag, MPI_COMM_WORLD);
          MPI_Irecv(&recv[N_computed], 1, mpi_vec_type, i, tag, MPI_COMM_WORLD, &request[N_computed]);
          N_computed++;
        }
    
    
    
        // loop
        while (N_computed < N_chunks){
    
          MPI_Status status;
          //get processed messages
          MPI_Waitany(N_computed,request,&index_completed,&status);
    
          //get sender ID dest
          dest = status.MPI_SOURCE;
    
          MPI_Send(&send[N_computed], 1, mpi_vec_type, dest, tag, MPI_COMM_WORLD);
          MPI_Irecv(&recv[N_computed], 1, mpi_vec_type, dest, tag, MPI_COMM_WORLD, &request[N_computed]);
    
          N_computed++;
        }
    
        //wait other process to send back their load
        for(int i=0;i<size-1;i++){
    
          MPI_Status status;
          MPI_Waitany(N_computed, request, &index_completed,&status);
    
        }
    
    
        //end
        printf("Ms finish\n");
    }
    

    【讨论】:

    • 请编辑您的问题并添加错误信息。
    • 另外,试试MPI_Waitall(..., MPI_STATUSES_IGNORE)
    • 为什么你认为用MPI_Status 的数组浪费内存会更聪明?数组与否,没有初始化。如果要在最外面的块中声明变量,请继续。现在,request 数组出现缓冲区溢出。您必须调整代码以匹配新尺寸。
    • 顺便问一下,你为什么建议使用 MPI_Request request[size-1] ?我们不应该对每个 Irecv 有一个请求吗?这一点还不清楚
    • 您的算法只需要每个对等方一个请求,所以size - 1 请求就足够了。
    猜你喜欢
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2014-07-11
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    相关资源
    最近更新 更多