This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

MPI::all_reduce with a std::vector

0 votes

Hi! This might be a silly question, but what the "correct" way of using MPI::all_reduce, when I want to reduce a std::vector? Apart from resorting to boost.MPI.

Let's say I want to sum all values of a vector, I can do this in old MPI calls, but I can't see how I can do this with Fenics calls.

Can you help me?

Thanks!

    std::vector<int> w(10), e(10);

    for (int i = 1; i <= 10; i++)
    {
        w[i - 1] = (i * MPI::rank(MPI_COMM_WORLD));
    }

    MPI_Allreduce(w.data(), e.data(), 10, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
    //???? ----> MPI::all_reduce(MPI_COMM_WORLD, w, MPI_SUM); <----

    for (int i = 1; i <= 10; i++)
        std::cout << "RANK " << MPI::rank(MPI_COMM_WORLD) << " REDUCE " << i - 1 << " = " << e[i - 1] << " LOCAL = " << w[i - 1] << std::endl;
asked Jul 31, 2015 by senseiwa FEniCS User (2,620 points)
edited Jul 31, 2015 by senseiwa

1 Answer

+1 vote
 
Best answer

Hi

To the best of my knowledge dolfin's all_reduce has not been defined for std::vectors, only scalars. See source code MPI.h for complete list of routines available. Notice that the gathers, scatters and broadcasts have beed defined for vectors, but not all_reduce, which only means that no-one in development has had any use for it yet. So you have to resort to the MPI_Allreduce call, which should be ok I guess?

answered Aug 3, 2015 by mikael-mortensen FEniCS Expert (29,340 points)
selected Aug 3, 2015 by senseiwa

Thanks for the answer, I will use MPI_Allreduce as I'm doing now, then.

Thank you!

...