Boost logo

Boost Users :

Subject: [Boost-users] mpi reduce on a vector
From: Prevrhal, Sven (Sven.Prevrhal_at_[hidden])
Date: 2010-06-12 14:55:12


I have a std::vector in each process and would like to sum them up by element in a master process such that
v_master[0] = v_worker1[0] + v_worker2[0] +...
v_master[1] = v_worker1[1] + v_worker2[1] +...
...

boost::mpi::reduce(world, v_worker, v_master, std::plus<int>(), masterID) doesn't cut it because std::add only works for individual vector elements but not for a whole vector.

So I could define my own functor

struct vplus
        : public std::binary_function<std::vector<int>, std::vector<int>, std::vector<int>>
{ // functor for operator+
        std::vector<int> operator()(const std::vector<int>& _Left, const std::vector<int>& _Right) const
        { // apply operator+ to operands
                std::vector<int> v(_Left.size());
                std::transform(_Left.begin(), _Left.end(), _Right.begin(), v.begin(), std::plus<int>());
                return (v);
        }
};

and apply it like so in the distributor
          boost::mpi::reduce(world, a, b, vplus(), 0);

This works, but here is my question: the functor makes a new vector v that gets returned and (do I say this right?) passed on to boost::mpi::reduce and goes out of scope. This making of a new vector strikes me as superfluous and wasteful (think big vectors) - should I avoid it, and if so, how?

Cheers,Sven
working on CUDA+Thrust+MPI for medical image processing

The information contained in this message may be confidential and legally protected under applicable law. The message is intended solely for the addressee(s). If you are not the intended recipient, you are hereby notified that any use, forwarding, dissemination, or reproduction of this message is strictly prohibited and may be unlawful. If you are not the intended recipient, please contact the sender by return e-mail and destroy all copies of the original message.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net