mardi 3 mars 2015

Custom reduce operation in boost.mpi

I'm having troubles getting a custom reduce operation compiling with the boost.mpi library. I'm trying to do an elementwise add of two std::vector. The operation works fine when I use to add two vectors together but when I try with mpi::reduce I get the error "no known conversion from 'int' to 'const std::vector' for 1st argument".



#include <boost/mpi.hpp>

namespace mpi = boost::mpi;

template <typename T>
struct elementwise_add {
std::vector<T> operator()(const std::vector<T>& a, const std::vector<T>& b) const {
std::vector<T> result(a.size());
std::transform(a.begin(), a.end(), b.begin(), result.begin(), std::plus<T>());
return result;
}
};

int main() {
mpi::environment env;
mpi::communicator world;
int N = 10;

std::vector<int> a(N,1), b(N,1);
auto op = elementwise_add<int>();
auto c = op(a, b); // works fine

if (world.rank() == 0) {
std::vector<int> sum(N,0);
mpi::reduce(world, a, sum, op, 0); // errors
} else {
mpi::reduce(world, a, op, 0);
}
}


Compiled with: mpic++ -O3 -std=c++11 -I/usr/local/include/ -I. -lboost_mpi-mt -lboost_serialization-mt test.cc -o test


Aucun commentaire:

Enregistrer un commentaire