In MPI, I am doing a reduce operation(minimum) on a value. This works fine, but how do I grab the processor number that the minimum came from and solicit that processor for more information(or send the additional data with the reduce operation)?
Share
If you don’t mind paring each value locally with an integer index (filled in this case with the value of the local rank), you can use the MPI_MINLOC or MPI_MAXLOC builtin operations for reduce; or it’s fairly easy to write your own MPI reduction operator to include things like multiple indices, etcc
Updated to add:
With the builtin operators MINLOC or MAXLOC, instead of passing in a single value to find the minimum of, you pass in that plus an integer index. That index can have any value you want it to, but it “follows” the other value along. MPI has built in “pair” data types – MPI_DOUBLE_INT for a double + an int, or MPI_2INT for two ints, that you can use.
So say you want to find the minimum of an integer array, and on which MPI task it was located. As normal, you find your local minimum on each task, and do the reduce; but this time you also pair it with an integer, in this case your rank:
And running you get:
If the value you’re reducing isn’t an integer, (say, a double), you create a structure containing the reduction value and the integer index, and use the appropriate MPI pair data type. (eg, MPI_DOUBLE_INT).
Updated further: Ok, just for fun, doing it with our own reduction operation and our own type to implement two indices:
Running gives