0

I am learning MPI, and just encountered this problem. The answer suggested

#include <mpi.h>
#include <stdio.h>

int main(int argc, char** argv) {
        int rank;
        int buf;
        const int root=0;

        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);

        if(rank == root) {
           buf = 777;
        }

        printf("[%d]: Before Bcast, buf is %d\n", rank, buf);

        /* everyone calls bcast, data is taken from root and ends up in everyone's buf */
        MPI_Bcast(&buf, 1, MPI_INT, root, MPI_COMM_WORLD);

        printf("[%d]: After Bcast, buf is %d\n", rank, buf);

        MPI_Finalize();
        return 0;
}

However when I compile it (assume it is saved in main.cpp) with mpic++ -O3 -Wall -Wextra -std=c++11 -march=native -fopenmp -Wpedantic -DOMPI_SKIP_MPICXX main.cpp -o main and launch with mpirun -np 3 main it got stuck. However when I launch with mpirun -np 2 main it works fine and can finish. I found that deadlock would happen for > 2 processors.

I wonder what might be the issue. I am using Open MPI 4.1.5 and I have Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz, which I believe have 12 processors (by checking cat /proc/cpuinfo).

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • FYI: `printf("[%d]: Before Bcast, buf is %d\n", rank, buf);` is undefined behaviour, since `buf` isn't set. The compiler is allowed to do whatever it wants at that point. – ChrisMM Mar 07 '23 at 21:29
  • @ChrisMM thanks! Didn't know that before. But still removing `printf` doesn't help; it still hangs for launching with >2 processors. – dragon_and_dracula Mar 07 '23 at 21:34
  • 1
    Didn't mean that was the problem, was just an aside :) – ChrisMM Mar 07 '23 at 21:53

0 Answers0