c++ - MPI:Waitany 对 ibcast 呼叫没有反应
问题描述
以下代码是 Master/Worker 模式的实现。
Ibcast 用于终止所有工作人员,因为在程序开始时我们不知道问题的大小。
MPI_Waitany() 用于等待 Irecv 获取新数据并等待 Ibcast 终止程序。
问题是 MPI_Waitany 在收到来自主控的所有 MPI_Send() 后阻塞。所以 MPI_Ibcast() 不返回阻塞
if (myid == 0) { //Master
bool test = false;
MPI_Request mpi_request;
for (int i = 0; i < 10; ++i) {
//Sending I 1..10 to Worker
MPI_Send(&i, 1, MPI_INT, 1, 10, MPI_COMM_WORLD);
cout << "Sending: " << i << endl;
}
cout << "Bcast Worker finished" << endl;
//Only to show even if the Bcast ist fired after worker is finished.
sleep(10);
//Using Ibcast for because Bcast can not Send to Ibcast in worker.
MPI_Ibcast(&test, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD, &mpi_request);
MPI_Wait(&mpi_request, MPI_STATUS_IGNORE);
cout << "Root Fin" << endl;
} else { //Worker
while (true) {
bool test = true;
int i = 20;
MPI_Request mpi_request[2];
MPI_Ibcast(&test, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD, &mpi_request[0]);
MPI_Irecv(&i, 1, MPI_INT, 0, 10, MPI_COMM_WORLD, &mpi_request[1]);
int RequestID = 0;
//Wait passes 10 Times for Send
//Wait can not pass after work is done.
MPI_Waitany(2, mpi_request, &RequestID, MPI_STATUS_IGNORE);
cout << "The \'I\' i got is: " << i <<" The Boolean is:" << test << endl;
if (!test)
break;
}
}
//OUTPUT
//Sending: 0
//Sending: 1
//Sending: 2
//Sending: 3
//Sending: 4
//Sending: 5
//Sending: 6
//Sending: 7
//Sending: 8
//The 'I' i got is: 0 The Boolean is:1
//The 'I' i got is: 1 The Boolean is:1
//Sending: 9
//Bcast Worker finished
//The 'I' i got is: 2 The Boolean is:1
//Root Fin
//The 'I' i got is: 3 The Boolean is:1
//The 'I' i got is: 4 The Boolean is:1
//The 'I' i got is: 5 The Boolean is:1
//The 'I' i got is: 6 The Boolean is:1
//The 'I' i got is: 7 The Boolean is:1
//The 'I' i got is: 8 The Boolean is:1
//The 'I' i got is: 9 The Boolean is:1
//No more output Programm is not Terminating and stuck in MPI_Waitany();
以下是解锁工作的更简单示例。
if (myid == 0) {
bool test = false;
MPI_Request mpi_request;
MPI_Ibcast(&test, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD, &mpi_request);
MPI_Wait(&mpi_request, MPI_STATUS_IGNORE);
cout << "Root Fin" << endl;
} else {
bool test = true;
cout << "Value is:" << test << endl;
int i = 20;
MPI_Request mpi_request[2];
sleep(2);
MPI_Ibcast(&test, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD, &mpi_request[0]);
MPI_Irecv(&i, 1, MPI_INT, 0, 10, MPI_COMM_WORLD, &mpi_request[1]);
int RequestID = 0;
sleep(2);
MPI_Waitany(2, mpi_request, &RequestID, MPI_STATUS_IGNORE);
//Should Print 0 in Begining is 1
cout << "Value is:" << test << endl;
}
//OUTPUT
//Value is:1
//Root Fin
//Value is:0
解决方案
您的程序卡在等待第 11 次发送或第 11 次 ibcast。如果您最终等待第 11 次发送或第 1 次 ibcast,它将完成。
这是我的(未经测试的)答案。
#include <iostream>
#include <mpi.h>
#include <unistd.h>
using namespace std;
int myid, numprocs;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int main(int argc, char **argv) {
int namelen;
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Get_processor_name(processor_name, &namelen);
MPI_Barrier(MPI_COMM_WORLD);
if (myid == 0) { //Master
bool test = false;
MPI_Request mpi_request;
for (int i = 0; i < 10; ++i) {
//Sending I 1..10 to Worker
MPI_Send(&i, 1, MPI_INT, 1, 10, MPI_COMM_WORLD);
cout << "Sending: " << i << endl;
}
cout << "Bcast Worker finished" << endl;
//Only to show even if the Bcast ist fired after worker is finished.
sleep(10);
//Using Ibcast for because Bcast can not Send to Ibcast in worker.
MPI_Ibcast(&test, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD, &mpi_request);
MPI_Wait(&mpi_request, MPI_STATUS_IGNORE);
cout << "Root Fin" << endl;
} else { //Worker
bool test = true;
MPI_Request mpi_request[2];
MPI_Ibcast(&test, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD, &mpi_request[0]);
while (true) {
int i = 20;
MPI_Irecv(&i, 1, MPI_INT, 0, 10, MPI_COMM_WORLD, &mpi_request[1]);
int RequestID = 0;
//Wait passes 10 Times for Send
//Wait can not pass after work is done.
MPI_Waitany(2, mpi_request, &RequestID, MPI_STATUS_IGNORE);
cout << "The \'I\' i got is: " << i <<" The Boolean is:" << test << endl;
if (!test)
break;
}
}
MPI_Finalize();
return 0;
}
推荐阅读
- git - 是否可以将分支重置为特定状态并选择稍后在同一分支上完成的提交?
- xml - BaseX 错误:没有为 'doc:csv' 声明命名空间
- java - 在java准备好的mysql查询语句中替换字符串?
- javascript - 如何禁用跨度标签上的点击事件 - Angular 6
- c# - 使用 iTextSharp (SignDeferred) 的外部签名 PDF - 无效签名问题(更改/损坏的文档)
- javascript - 运行 Ionic 模块的问题
- git - 如何使 Windows 中的 Visual Studio Code 针对 WSL 区分大小写?
- highcharts - x 和 y 轴的 Highcharts x-range 图表日期时间类型
- android - 有没有办法以编程方式访问三星的剪贴板内容?
- odoo - 杂货店的 POS/库存