如何向其他节点发出信号,表明主节点没有更多工作可用

How to signal to other node that no more work is available from the master node

本文关键字:节点 工作 其他 信号      更新时间:2023-10-16

我想请求一些帮助来完成 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
    }
}

进行有效的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 将数据发送到所有子节点。