本文介绍了升压MPI挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下一个简单的异步消息传递程序。我在两台PC上用16个进程运行。这些过程都连接在一个torrus为4×4矩阵。因此,在主函数的开始,你会看到谁是一个过程的邻居。我所要做的是实现一个检查点算法。所以,每一个进程发送消息给所有的邻居当i = 5或10(假定我是时候)。当一个进程收到消息时,它启动为相同的处理的新irecv。但是,如果我是10不启动irecv,因为没有消息会后收到。在节目的最后,所有的进程等待未被接收的消息。

I have a simple asynchronous messaging program as below. I run it on two PCs with 16 processes. These processes are connected over a torrus as 4x4 matrix. So, at the beginning of main function you will see who are neighbours of a process. What I am trying to do is implementing a checkpointing algorithm. So every process sends a message to its all neighbors when i = 5 or 10 (assume that i is time). And when a process receives a message, it starts a new irecv for the same process. But if i is 10 it does not start an irecv, because no messages will be received after that. At the end of program, all processes wait for unreceived messages.

/* Demo_01_Main.cpp */

#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <string>
#include <iostream>
#include <fstream>
#include <map>

using namespace std;

class Packet{
    friend class boost::serialization::access;
private:
    int receiver;
    int sender;
    int data;

public:
    Packet(){
        receiver = 0;
        sender = 0;
        data = 0;
    }

    Packet(int receiver, int sender, int data){
        this->receiver = receiver;
        this->sender = sender;
        this->data = data;
    }

    ~Packet(){}

    int getData() {
        return data;
    }

    void setData(int data) {
        this->data = data;
    }

    int getReceiver() {
        return receiver;
    }

    void setReceiver(int receiver) {
        this->receiver = receiver;
    }

    int getSender() {
        return sender;
    }

    void setSender(int sender) {
        this->sender = sender;
    }

    template<class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        ar & receiver;
        ar & sender;
        ar & data;
    }

    string toString(){
        stringstream ss;
        ss << "Packet = [Data: " << data << ", Receiver: " << receiver << ", Sender: " << sender << "]";
        return ss.str();
    }
};

int rank;

void log(string str){
    ofstream outfile;
    stringstream logFileName;
    logFileName << "log_" << rank << ".txt";
    outfile.open(logFileName.str().c_str(), std::ios_base::app);
    outfile << str;
    outfile.close();
}

int main(int argc, char* argv[]){
    map<int, boost::mpi::request> mpiReceiveRequest;
    map<int, boost::mpi::request> mpiSendRequest;
    map<int, Packet *> receivedData;
    vector<int> neighbors;

    boost::mpi::environment env(argc, argv);
    boost::mpi::communicator world;

    rank = world.rank();

    if(rank == 0){
        neighbors.push_back(1);
        neighbors.push_back(3);
        neighbors.push_back(4);
        neighbors.push_back(5);
        neighbors.push_back(7);
        neighbors.push_back(12);
        neighbors.push_back(13);
        neighbors.push_back(15);
    }
    else if(rank == 1){
        neighbors.push_back(0);
        neighbors.push_back(2);
        neighbors.push_back(4);
        neighbors.push_back(5);
        neighbors.push_back(6);
        neighbors.push_back(12);
        neighbors.push_back(13);
        neighbors.push_back(14);
    }
    else if(rank == 2){
        neighbors.push_back(1);
        neighbors.push_back(3);
        neighbors.push_back(5);
        neighbors.push_back(6);
        neighbors.push_back(7);
        neighbors.push_back(13);
        neighbors.push_back(14);
        neighbors.push_back(15);
    }
    else if(rank == 3){
        neighbors.push_back(0);
        neighbors.push_back(2);
        neighbors.push_back(4);
        neighbors.push_back(6);
        neighbors.push_back(7);
        neighbors.push_back(12);
        neighbors.push_back(14);
        neighbors.push_back(15);
    }
    else if(rank == 4){
        neighbors.push_back(0);
        neighbors.push_back(1);
        neighbors.push_back(3);
        neighbors.push_back(5);
        neighbors.push_back(7);
        neighbors.push_back(8);
        neighbors.push_back(9);
        neighbors.push_back(11);
    }
    else if(rank == 5){
        neighbors.push_back(0);
        neighbors.push_back(1);
        neighbors.push_back(2);
        neighbors.push_back(4);
        neighbors.push_back(6);
        neighbors.push_back(8);
        neighbors.push_back(9);
        neighbors.push_back(10);
    }
    else if(rank == 6){
        neighbors.push_back(1);
        neighbors.push_back(2);
        neighbors.push_back(3);
        neighbors.push_back(5);
        neighbors.push_back(7);
        neighbors.push_back(9);
        neighbors.push_back(10);
        neighbors.push_back(11);
    }
    else if(rank == 7){
        neighbors.push_back(0);
        neighbors.push_back(2);
        neighbors.push_back(3);
        neighbors.push_back(4);
        neighbors.push_back(6);
        neighbors.push_back(8);
        neighbors.push_back(10);
        neighbors.push_back(11);
    }
    else if(rank == 8){
        neighbors.push_back(4);
        neighbors.push_back(5);
        neighbors.push_back(7);
        neighbors.push_back(9);
        neighbors.push_back(11);
        neighbors.push_back(12);
        neighbors.push_back(13);
        neighbors.push_back(15);
    }
    else if(rank == 9){
        neighbors.push_back(4);
        neighbors.push_back(5);
        neighbors.push_back(6);
        neighbors.push_back(8);
        neighbors.push_back(10);
        neighbors.push_back(12);
        neighbors.push_back(13);
        neighbors.push_back(14);
    }
    else if(rank == 10){
        neighbors.push_back(5);
        neighbors.push_back(6);
        neighbors.push_back(7);
        neighbors.push_back(9);
        neighbors.push_back(11);
        neighbors.push_back(13);
        neighbors.push_back(14);
        neighbors.push_back(15);
    }
    else if(rank == 11){
        neighbors.push_back(4);
        neighbors.push_back(6);
        neighbors.push_back(7);
        neighbors.push_back(8);
        neighbors.push_back(10);
        neighbors.push_back(12);
        neighbors.push_back(14);
        neighbors.push_back(15);
    }
    else if(rank == 12){
        neighbors.push_back(0);
        neighbors.push_back(1);
        neighbors.push_back(3);
        neighbors.push_back(8);
        neighbors.push_back(9);
        neighbors.push_back(11);
        neighbors.push_back(13);
        neighbors.push_back(15);
    }
    else if(rank == 13){
        neighbors.push_back(0);
        neighbors.push_back(1);
        neighbors.push_back(2);
        neighbors.push_back(8);
        neighbors.push_back(9);
        neighbors.push_back(10);
        neighbors.push_back(12);
        neighbors.push_back(14);
    }
    else if(rank == 14){
        neighbors.push_back(1);
        neighbors.push_back(2);
        neighbors.push_back(3);
        neighbors.push_back(9);
        neighbors.push_back(10);
        neighbors.push_back(11);
        neighbors.push_back(13);
        neighbors.push_back(15);
    }
    else if(rank == 15){
        neighbors.push_back(0);
        neighbors.push_back(2);
        neighbors.push_back(3);
        neighbors.push_back(8);
        neighbors.push_back(10);
        neighbors.push_back(11);
        neighbors.push_back(12);
        neighbors.push_back(14);
    }

    for(int i=0; i<8; i++){
        Packet * packet = new Packet();
        receivedData[neighbors[i]] = packet;
        mpiReceiveRequest[neighbors[i]] = world.irecv(neighbors[i], 100, *packet);
    }


    for(int i=1; i<=10; i++){
        if(i%5 == 0){    // Checkpoint time
            for(int j=0; j<8; j++){
                Packet * p = new Packet(neighbors[j], rank, i);
                mpiSendRequest[neighbors[j]] = world.isend(neighbors[j], 100, *p);
                log("Sending: ");
                log(p->toString());
                log("\n");
            }
        }

        for(int j=0; j<8; j++){
            if(mpiReceiveRequest[neighbors[j]].test()){
                Packet * p = receivedData[neighbors[j]];
                log("Received: ");
                log(receivedData[neighbors[j]]->toString());
                log("\n");

                if(p->getData() != 10){
                    Packet * packet = new Packet();
                    receivedData[neighbors[j]] = packet;
                    mpiReceiveRequest[neighbors[j]] = world.irecv(neighbors[j], 100, *packet);
                }
            }
        }
    }

    for(int i=0; i<8; i++){
        stringstream ss;
        ss << "  Wait from: " << neighbors[i] << endl;
        log(ss.str());
        mpiReceiveRequest[neighbors[i]].wait();

        log("Received: ");
        log(receivedData[neighbors[i]]->toString());
        log("\n");
    }

    stringstream ss;
    ss << rank << " is done" << endl;
    log(ss.str());

    return 0;
}

的问题是,它在等待命令挂起。同时,一些收到的消息中包含的数据废话。例如,处理7的输出文件是象下面这样:

The problem is that it hangs on at the wait command. As well, some of the received messages contains nonsense data. For instance, the output file of process 7 is like below:

Received: Packet = [Data: 5, Receiver: 7, Sender: 10]
Received: Packet = [Data: 5, Receiver: 7, Sender: 11]
Received: Packet = [Data: 5, Receiver: 7, Sender: 0]
Received: Packet = [Data: 5, Receiver: 7, Sender: 4]
Sending: Packet = [Data: 5, Receiver: 0, Sender: 7]
Sending: Packet = [Data: 5, Receiver: 2, Sender: 7]
Sending: Packet = [Data: 5, Receiver: 3, Sender: 7]
Sending: Packet = [Data: 5, Receiver: 4, Sender: 7]
Sending: Packet = [Data: 5, Receiver: 6, Sender: 7]
Sending: Packet = [Data: 5, Receiver: 8, Sender: 7]
Sending: Packet = [Data: 5, Receiver: 10, Sender: 7]
Sending: Packet = [Data: 5, Receiver: 11, Sender: 7]
  Wait from: 0
Received: Packet = [Data: 537985024, Receiver: 0, Sender: 0]
  Wait from: 2

我想不出问题出在哪里。

I could not figure out where the problem is.

推荐答案

问题是在调用wait命令是前测试,并顺利通过了要求。其实,我意识到,这也是我的previous问题之一的原因:Waiting请求已已经完成

The problem was on calling wait command for the request which is tested before and passed. Actually, I realized that it is also the reason of one of my previous questions: Waiting requests which have been already completed

所以,我修改了code如下,现在它运行:

So, I modified my code as below, and now it runs:

/* ...
 else if(rank == 15){
    neighbors.push_back(0);
    neighbors.push_back(2);
    neighbors.push_back(3);
    neighbors.push_back(8);
    neighbors.push_back(10);
    neighbors.push_back(11);
    neighbors.push_back(12);
    neighbors.push_back(14);
}
...After assigning neighbors
*/

for(int i=0; i<8; i++){
    Packet * packet = new Packet();
    receivedData[neighbors[i]] = packet;
    mpiReceiveRequest[i] = world.irecv(neighbors[i], 100, *packet);
}

vector<int> completed;
for(int i=1; i<=10; i++){
    if(i%5 == 0){    // Checkpoint time
        for(int j=0; j<8; j++){
            Packet * p = new Packet(neighbors[j], rank, i);
            mpiSendRequest.push_back(world.isend(neighbors[j], 100, *p));
            log("Sending: ");
            log(p->toString());
            log("\n");
        }
    }

    for(int j=0; j<8; j++){
        vector<int>::iterator it = completed.begin();
        bool passed = false;

        while(it != completed.end()){
            if(*it == j){
                passed = true;
                break;
            }
            it++;
        }

        if(!passed){
            if(mpiReceiveRequest[j].test()){
                completed.push_back(j);

                Packet * p = receivedData[neighbors[j]];
                if(p->getData() != 10){
                    Packet * packet = new Packet();
                    receivedData[neighbors[j]] = packet;
                    mpiReceiveRequest[j] = world.irecv(neighbors[j], 100, *packet);
                    completed.pop_back();
                }
            }
        }
    }
}

vector<boost::mpi::request> reqs;
for(int i=0; i<8; i++){
    vector<int>::iterator it = completed.begin();
    bool passed = false;

    while(it != completed.end()){
        if(*it == i){
            passed = true;
            break;
        }
        it++;
    }

    if(!passed){
        mpiReceiveRequest[i].wait();
        reqs.push_back(mpiReceiveRequest[i]);
    }
}

stringstream ss;
ss << rank << " is done" << endl;
log(ss.str());

return 0;
} // End of main

在结束

的主要区别是存储在一个载体完成的请求,而不是等着他们。

The main difference is storing the completed requests in a vector and not waiting them at the end.

这篇关于升压MPI挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:22