I am having problems with ending my program using MS-MPI.
All return values seems fine but I have to ctrl + c in cmd to end it (it doesn't look like it's still computing so the exit condition looks fine).
I want to run a program using N processes. When one of them finds a solution, it should set flag as false, send it for all others and then in next iteration they shall all stop and the program ends.
The program actually does some more advanced calculations and I'm working on simplified version for clarity. I just wanted to make sure that communication works.
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
//sets as 0 -> (N-1) depending on number of processes running
int c = world_rank;
bool flag = true;
while (flag) {
std::cout << "process: " << world_rank << " value: " << c << std::endl;
c += world_size;
//dummy condition just to test stop
if (c == 13) {
flag = false;
}
MPI_Barrier(MPI_COMM_WORLD);
//I have also tried using MPI_Bcast without that if
if(!flag) MPI_Bcast(&flag, 1, MPI_C_BOOL, world_rank, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
} //end of while
MPI_Finalize();
return 0;
}
How I think it works: it starts with every process defining its c and flag, then on each (while) pass it increments its c by a fixed number. Then when it gets to stop condition it sets flag as false and sends it to all remaining processes. What I get when I run it with 4 processes:
process: 0 value: 0
process: 2 value: 2
process: 1 value: 1
process: 3 value: 3
process: 1 value: 5
process: 3 value: 7
process: 0 value: 4
process: 2 value: 6
process: 3 value: 11
process: 1 value: 9
process: 2 value: 10
process: 0 value: 8
process: 3 value: 15
process: 2 value: 14
process: 0 value: 12
(I am fine with those few extra values)
But after that I have to manually terminate it with ctrl + c. When running on 1 process it gets smoothly from 1 to 12 and exits.