我用“-np 2”调用 mpirun。我将等级 0 的进程称为“主”,将等级 1 的进程称为“从”。
目标:
- master 偶尔会向 slave 发送消息,例如
mpi::send(1, UPDATE, data);
. 其他消息类型包括 DIE、COMPUTE ...等。这些消息类型是具有唯一值的常量整数。 - slave 运行一个无限循环,“监听”来自 master 的任何消息。当它收到一条消息时,它会向主设备发送一个确认。
执行:
从运行:
...
int updateData, computeData;
mpi::request updateRequest = world.irecv(0,UPDATE, updateData);
mpi::request computeRequest = world.irecv(0,COMPUTE, computeData);
do {
cerr << "slave ready to take a command" << endl;
if(updateRequest.test()) {
cerr << "slave ireceived UPDATE" << endl;
world.send(0, UPDATE_ACK, 0);
cerr << "slave sent UPDATE_ACK" << endl;
/* do something useful
...
...
*/
updateRequest = world.irecv(0, UPDATE, updateData);
} else if (computeRequest.test()) {
...
} else {
boost::this_thread::sleep( boost::posix_time::seconds(1) );
}
}
当主人运行时:
...
world.send(1, UPDATE, 10);
cerr << "master sent UPDATE" << endl;
int dummy;
world.recv(1, UPDATE_ACK, dummy);
cerr << "master received UPDATE_ACK" << endl;
...
主代码的更多上下文:
...
// update1
world.send(1, UPDATE, params);
cerr << "master sent UPDATE" << endl;
int dummy;
world.recv(1, UPDATE_ACK, dummy);
cerr << "master received UPDATE_ACK" << endl;
// update2
world.send(1, UPDATE2, params2);
cerr << "master sent UPDATE2" << endl;
world.recv(1, UPDATE2_ACK, dummy);
cerr << "master received UPDATE2_ACK" << endl;
// update3
world.send(1, UPDATE3, params3);
cerr << "master sent UPDATE3" << endl;
world.recv(1, UPDATE3_ACK, dummy);
cerr << "master received UPDATE3_ACK" << endl;
...
// training iterations
do {
mpi::request computeRecvReq1, computeRecvReq2;
std::map<int, int> result1, result2;
// for each line in a text file, the master asks the slave(s)
// to compute two things and aggregates the results
for(unsigned sentId = 0; sentId != data.size(); sentId++) {
// these two functions won't return until at least one slave is "idle"
CollectSlavesWork1(computeRecvReq1, result1);
CollectSlavesWork2(computeRecvReq2, result2);
// async ask the slave to compute and async get the results
world.isend(1, COMPUTE, sentId);
computeRecvReq1 = world.irecv(1, RESULT1, result1);
computeRecvReq2 = world.irecv(1, RESULT2, result2);
}
// based on the slave(s) work, the master updates params1
// and send them again to the slave(s)
world.send(1, UPDATE, params);
cerr << "master sent UPDATE" << endl;
world.recv(1, UPDATE_ACK, dummy); // PROBLEM HAPPENS HERE
cerr << "master received UPDATE_ACK" << endl;
} while(!ModelIsConverged())
...
输出:
...
奴隶准备接受命令
主发送更新
奴隶收到更新
从机发送 UPDATE_ACK
主控收到 UPDATE_ACK
奴隶准备接受命令
...
奴隶准备接受命令
主发送更新
奴隶收到更新
从机发送 UPDATE_ACK
奴隶准备接受命令
...
问题: 主人第一次发送更新消息时,一切似乎都很好。但是,在第二次中,主服务器没有收到 UPDATE_ACK。