MPI:如何中断MPI任务但不杀死MPI进程?

时间:2017-10-24 23:16:00

标签: c++ parallel-processing mpi

所以,让我们说我已经通过以下步骤创建了一个主从计划:

1 - 主人向所有奴隶发送任务

2 - 更快的奴隶完成任务并将结果发送回主人

3 - master从最快的奴隶那里收到结果并向每个人发送新任务

4 - 更快的从站已准备好接收任务,但较慢的从站必须中断或取消它们正在执行的旧的,缓慢的任务,以便同时启动新任务时间作为更快的奴隶

我知道如何执行所有步骤,除了第4步,我必须中断较慢的奴隶正在做的事情才能继续下一个任务。

以下是缺少该部分的不完整代码的示例:

#include <mpi.h>
#include <iostream>
#include <string>
#include <unistd.h>

using namespace std;

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

  int rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);


  if (rank == 0) {
    int value = 17;
    string valuec = "Hello";

    for(int i = 1; i < world_size; i++){


        int result = MPI_Send(valuec.c_str(), valuec.length(), MPI_CHAR, i, 0, MPI_COMM_WORLD);
            if (result == MPI_SUCCESS)
          std::cout << "Rank 0 OK!" << std::endl;
    }

    int slavesDone = 0;
    MPI_Status status;
    int flag = 0;

    while(1){
        flag=0;
        MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status);
        if(flag==1){
            slavesDone++;
            cout << "Slaves done: " << slavesDone << endl;
        }

        if(slavesDone >= world_size/2){/* here the master moves on
                                          before all slaves are done
                                          */
            cout << "Master breaking" << endl; 
            break;
        }

    }

    /* interruption Here:
                    How do I make the master tell to the slow slaves
                    interrupt or cancel what they were doing in order
                    to receive new tasks
                    */

    // New tasks should go here here

  } else if (rank != 0) {
    int receivedMessages = 0;
    while(1){ 
                MPI_Status status;
            int flag = 0;

        MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status);

    if(flag==1){
        receivedMessages++;
            int value;
            char buffer[256];
            int result = MPI_Recv(&buffer, 256, MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            cout << rank << " received " << buffer << endl;

            sleep(rank); /* this hypothetical task will be slower
                            in some slaves, faster in others. In the
                            final version of code of course this
                            will not be a sleep command, and the time
                            it takes will not be proportional to the
                            process rank.
                            */
        MPI_Send(buffer, sizeof(buffer), MPI_CHAR, 0, 0, MPI_COMM_WORLD);

        cout << rank << " breaking" << endl;

        break; 

    }
 }
}
  MPI_Finalize();
  return 0;
} 

0 个答案:

没有答案