1
$\begingroup$

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?

$\endgroup$
  • 3
    $\begingroup$ 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. $\endgroup$ – origimbo Jul 28 '17 at 13:20
  • $\begingroup$ @origimbo, thanks, this comment helps a lot! $\endgroup$ – user123 Jul 28 '17 at 13:58
  • $\begingroup$ @origimbo you might convert your comment into an answer - as you certainly solved the principal issue. $\endgroup$ – Anton Menshov Mar 23 '18 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.