I want to develop a distributed algorithm with MPI which have load balancing capability. But there is a situation that I'm not very clear about, which is, when the number of tasks is less than the number of processors available, I need to detect that and terminate those processors whose ranks (actually rank + 1) are greater than the total number of tasks. In my implementation, I just add a condition to determine that, if the condition is true, do something, otherwise, call MPI_Finalize() to shut down these processors. For your reference, here is the psuedocode:
MPI_Init(); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &proc_rank); if (proc_rank + 1 < task_counts): do something (including some collective communications); MPI_Finalize(); return 0;
This algorithm seems to be quiter obvious. However, it doesn't work. I added some fprintf code after MPI_Finalize(), but this code never got there for those processors whose ranks are out of range.
So what's the problem here?