# MPI_Finalize() only some processors

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?

• I'm assuming by collective communications in your you mean over MPI_COMM_WORLD. This is going to stall since your idle processes aren't taking part. You'd need to use something like MPI_Comm_split to create a new communicator over just your worker processes before you go into the if block. P.S. for this kind of pure programming question, stack overflow might be a better fit and get more eyeballs. – origimbo Jul 28 '17 at 13:20
• @origimbo, thanks, this comment helps a lot! – user123 Jul 28 '17 at 13:58
• @origimbo you might convert your comment into an answer - as you certainly solved the principal issue. – Anton Menshov Mar 23 '18 at 4:49