aryafallahi / mithra

MITHRA is a full-wave numerical solver for free-electron lasers.
17 stars 7 forks source link

[Bug] MPI reduce in place #60

Closed arnaualba closed 3 years ago

arnaualba commented 3 years ago

We have these code lines that I had written where the recvbuf and sendbuf are the same variable (https://github.com/aryafallahi/mithra/blob/75f5e9f589def2404fe8b3fa350d0d6705910762/src/solver.cpp#L409):

    MPI_Allreduce(&zMin, &zMin, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
    MPI_Allreduce(&bz, &bz, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
    unsigned int Nq = chargeVectorn_.size();
    MPI_Allreduce(&Nq, &Nq, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);

But apparently this is not good practice and causes some errors. This can be fixed with (https://stackoverflow.com/questions/16507865/can-mpi-sendbuf-and-recvbuf-be-the-same-thing):

    MPI_Allreduce(MPI_IN_PLACE, &zMin, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
    MPI_Allreduce(MPI_IN_PLACE, &bz, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
    unsigned int Nq = chargeVectorn_.size();
    MPI_Allreduce(MPI_IN_PLACE, &Nq, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);