Search code examples
cmpicommunicator

MPI Check if communicator is MPI_COMM_WORLD


I need to check if a MPI communicator is MPI_COMM_WORLD comm. This means that all processors are within this communicator.

I tried this

int isCommWolrd(MPI_Comm comm) {
    int size_comm = 0;
    int size_comm_world = 0;
    MPI_Comm_size(comm, &size_comm);
    MPI_Comm_size(MPI_COMM_WORLD, &size_comm_world);
    return (size_comm == size_comm_world);
}

Is it sufficient to check only the sizes of the communicator. Can there be a false positive of negative?


Solution

  • Use MPI_Comm_compare() and check the result is MPI_IDENT

    int MPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int *result)
    

    MPI_IDENT results if and only if comm1 and comm2 are handles for the same object (identical groups and same contexts). MPI_CONGRUENT results if the underlying groups are identical in constituents and rank order; these communicators differ only by context. MPI_SIMILAR results of the group members of both communicators are the same but the rank order differs. MPI_UNEQUAL results otherwise.

    Your method can lead so false positive. For example, if you MPI_Comm_dup(MPI_COMM_WORLD, &comm), then the resulting comm has the same size than MPI_COMM_WORLD, but it a different communicator.