gpt4 book ai didi

c++ - MPI:Waitany 对 ibcast 调用没有反应

转载 作者:行者123 更新时间:2023-11-30 16:23:05 26 4
gpt4 key购买 nike

以下代码是主/工作模式的实现。

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

最佳答案

您的程序卡在等待第 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;
}

关于c++ - MPI:Waitany 对 ibcast 调用没有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54128453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com