带有MPI的主从模型中的死锁
Deadlock in Master-Slave model with MPI
我试图用MPI实现主/从模型,但我遇到了一个小问题。
我想做的是,奴隶应该等待主人的命令,他们不应该工作,直到主人发出命令。Master应该同时向所有奴隶发送命令,等待所有奴隶完成命令,然后再次向所有奴隶同时发送命令。
例如,我有3个处理器(1个主处理器,2个从处理器),我向从处理器发送两次订单,我想打印:
Master initialization done.
Master sends order to slave 1
Master sends order to slave 2
Slave 1 got the order from master
Slave 2 got the order from master
Master got response from Slave 1
Master got response from Slave 2
_________________________________
Master sends order to slave 1
Master sends order to slave 2
Slave 1 got the order from master
Slave 2 got the order from master
Master got response from Slave 1
Master got response from Slave 2
All done.
以下是我迄今为止所做的工作。
int count = 0;
int number;
if (procnum == 0) {
// initialize master, slaves shouldn't be working until this ends
std::cout << "Master initialization done." << endl;
while (count < 2) {
for (int i = 1; i < numprocesses; i++) {
number = i * 2;
std::cout << "Master sends order to slave " << i << endl;
MPI_Send(&number, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Recv(&number, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "Master got response from Slave " << i << endl;
}
count++;
}
std::cout << "All done" << endl;
} else {
int received;
MPI_Recv(&received, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "Slave " << procnum << " got the order from master" << endl;
MPI_Send(&received, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
但我得到了这个:
Master initialization done.
Master sends order to slave 1
Slave 1 got the order from master
Master got response from Slave 1
Master sends order to slave 2
Slave 2 got the order from master
Master got response from Slave 2
Master sends order to slave 1
然后它就卡住了。我做错了什么?
for (int i = 1; i < size; i++) {
应该是
for (int i = 1; i <= size; i++) {
编辑:可以,因为size
是3(包括服务器)
关于序列:MPI_Send和MPI_Recv正在阻止调用,因此输出如预期(?)。
如果主控在第二轮被阻止,那是因为从控没有响应。while (count < 2)
循环应该同时包含master和slave。
相关文章:
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何在没有死锁和/或争用的情况下正确使用 std::mutex C++?
- 用C++中的std::condition_variable将线程置于死锁中会有风险吗
- 使用 std::async 时死锁,将来作为成员
- 如何调试读写器锁的死锁?
- 为什么在Visual Studio 2013上的std::this_thread::sleep_for上死锁
- localtime() 函数正在调用 ___lll_lock_wait_private(),这会使线程陷入死锁
- 如何重现 Boost 进程文档提示的死锁?
- 多线程Windows GUI应用程序中的死锁
- 为什么printf会导致与future.get的死锁,而cout则不会?
- C++中具有阻塞队列和障碍的死锁
- 死锁使用 std::mutex 来保护多个线程中的 cout
- 避免并发等待对象中的死锁
- 在VC++中从DLLMAIN内部调用D3D的CREATEDEVICE时,它会创建一个死锁(loaderlock?)。有没有办法克服这个问题?最终目标内
- 当用2个螺纹锁定时,将recursive_mutex死锁
- 程序在 C++11 中使用条件变量进入死锁
- 一个线程提升的死锁
- 单个生产者/多个消费者死锁
- 当被调用方法使用调用方已锁定的同一锁时,如何避免死锁
- 带有MPI的主从模型中的死锁