I want to have the global minimum (floating point types) across all MPI ranks and proceed only on the rank which computed the local minimum.
Specifically, can we compare the global and local minimum for exact equality (==)? Or is it possible that there can occur floating point errors in the reduction itself?
#include <iostream>
#include <mpi.h>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank;
double minDistance = 100.0 + rank;
double globalMinDistance;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Reduce to find the global minimum
MPI_Reduce(&minDistance, &globalMinDistance, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD);
// Can i safely compare the doubles here with ==?
if (minDistance == globalMinDistance) {
std::cout << "Rank " << rank << " has the globally minimum distance: " << globalMinDistance << std::endl;
}
MPI_Finalize();
return 0;
}
There should be no roundoff in doing a minimum reduction. Your code looks safe to me. Have you tested it?
However, you are testing globalMinDistance
on each process, while the value is only set on rank zero. Use an Allreduce
.