ale
ale

Reputation: 11824

Getting only two processes to execute code in MPI - don't know which processes will get to code segment though

I have a MPI program in C. There is a part of the program where an arbitrary number processes could reach. I just want 2 processes to do something and I want the rest not to do anything. It is unknown which process ids will reach this part of the program so I CAN'T do this:

if(rank == 0 || rank == 1) { 
  // do something
}
// (else do nothing)

The rank-0 and rank-1 process may never reach here though. I do know that at least two processes will reach here though.

How can this be achieved in MPI?

Many thanks :).

Upvotes: 0

Views: 527

Answers (2)

Stan Graves
Stan Graves

Reputation: 6955

Setup Rank 0 (or Rank n-1) as a "control rank". This rank will hold the two magic tokens to control entry into the critical section.

Use an MPI_IRecv on MPI_ANY_SOURCE. Every rank that reached the critical code section will MPI_Send to rank 0 to ID themselves as having reached the critical section. Then rank 0 will MPI_Send and the rank at the critical section will MPI_Recv a token. The first two ranks to reach the critical code will be sent a "go ahead" token. Send any other ranks a "don't bother" token.

At the end of the run, there will be one unmatched MPI_IRecv. This MPI_Irecv can be cancelled prior to entering MPI_Finalize.

Upvotes: 2

Dhaivat Pandya
Dhaivat Pandya

Reputation: 6536

Start two processes seperately that you must code to reach there, and then use the rank determination.

EDIT: Make a shared integer across all proccesses that one process will increment when it reaches that patch of code and so will the next process, and mark the code in question with a if condition so it won't run unless this shared integer, is less than 2.

Upvotes: 2

Related Questions