以下代码是Master / Worker模式的实现。
Ibcast用于终止所有工作人员,因为我们在程序开始时不知道问题的严重程度。
MPI_Waitany()用于等待Irecv获取新数据,并使用Ibcast终止程序。
问题在于,接收到来自主机的所有MPI_Send()后,MPI_Waitany正在阻塞。因此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
答案 0 :(得分:0)
您的程序被卡在等待第11个发送或第11个ibcast。如果您最终等待第11个发送或第一个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;
}