如何向其他节点发送信号,说明主节点无法进行更多工作

时间:2019-04-09 04:54:24

标签: c++ mpi

我想寻求一些帮助,以完成mpi实现中的工作量分配。

想法是节点0将具有文件名列表(“ list_file”)。当其他节点空闲时,它们将向节点0发送文件请求,而节点0将发回文件名。

一旦没有更多文件可发送,节点0完成其工作。但是,如何向其他节点的读取器线程发送信号,通知节点0没有更多文件,它们应该停止等待节点0发送新文件。

// pid 0 will collect all input text file names and distribute them to other nodes
if (pid == 0)
{
    vector<char*> list_file;                    // a vector to hold the input text file names to be processed

    GetInputFile(argv, list_file);              // argv[1] is a text file that contains the input text files to be processed, all the text file names will be added to list_file
    num_file_remaining = list_file.size();      // number of file remained in the queue to be processed

    MPI_Status requeststats;            // for MPI_recv

    // listen to request for file from other nodes as long as there is file left in list_file
    while (num_file_remaining != 0)
    {
        MPI_recv(NULL, 0, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &requeststats);       // listen to request for file
        MPI_send(list_file.back(), 5 * MAX_WORD_LENGTH, MPI_CHAR, requeststats.MPI_SOURCE, requeststats.MPI_SOURCE, MPI_COMM_WORLD);            // send the file to respective node
        list_file.pop_back();       // remove the file that was just sent
        num_file_remaining -= 1;    // reduce the number of file remained in the queue
    }
}


// other nodes will request work from pid 0
if (pid != 0)
{
    char* file_name;

    while (num_file_remaining != 0)
    {
        MPI_send(NULL, 0, MPI_INT, 0, 0, MPI_COMM_WORLD);           // send the request for a file to node 0
        MPI_recv(file_name, 5 * MAX_WORD_LENGTH, MPI_CHAR, 0, pid, MPI_COMM_WORLD, MPI_STATUS_IGNORE);      // receive the file from node 0
        cout << "pid: " << pid << " - " << file_name << endl;       // process the file
        // HOW TO EXIT THE LOOP WHEN NO MORE FILE TO RECEIVE FROM NODE 0
    }
}

1 个答案:

答案 0 :(得分:0)

有一个有效的MPI_Irecv通话:

// set this tag to whatever you want and don’t use it for anything else
constexpr int AM_I_FINISHED_TAG = 1376;
MPI_Request am_I_finished = MPI_Irecv(nullptr, 0, MPI_INT, 0, AM_I_FINISHED_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

每当在子节点上完成文件处理后,请使用MPI_Test检查该请求是否完成。当主节点不再有子节点的工作时,请使用AM_I_FINISHED_TAG将数据发送到所有子节点。