DIY  3.0
data-parallel out-of-core C++ library
 All Classes Namespaces Functions Typedefs Groups Pages
collectives.hpp
1 #ifndef DIY_COLLECTIVES_HPP
2 #define DIY_COLLECTIVES_HPP
3 
4 namespace diy
5 {
6 namespace detail
7 {
8  struct CollectiveOp
9  {
10  virtual void init() =0;
11  virtual void update(const CollectiveOp& other) =0;
12  virtual void global(const mpi::communicator& comm) =0;
13  virtual void copy_from(const CollectiveOp& other) =0;
14  virtual void result_out(void* dest) const =0;
15  virtual ~CollectiveOp() {}
16  };
17 
18  template<class T, class Op>
19  struct AllReduceOp: public CollectiveOp
20  {
21  AllReduceOp(const T& x, Op op):
22  in_(x), op_(op) {}
23 
24  void init() { out_ = in_; }
25  void update(const CollectiveOp& other) { out_ = op_(out_, static_cast<const AllReduceOp&>(other).in_); }
26  void global(const mpi::communicator& comm) { T res; mpi::all_reduce(comm, out_, res, op_); out_ = res; }
27  void copy_from(const CollectiveOp& other) { out_ = static_cast<const AllReduceOp&>(other).out_; }
28  void result_out(void* dest) const { *reinterpret_cast<T*>(dest) = out_; }
29 
30  private:
31  T in_, out_;
32  Op op_;
33  };
34 
35  template<class T>
36  struct Scratch: public CollectiveOp
37  {
38  Scratch(const T& x):
39  x_(x) {}
40 
41  void init() {}
42  void update(const CollectiveOp& other) {}
43  void global(const mpi::communicator& comm) {}
44  void copy_from(const CollectiveOp& other) {}
45  void result_out(void* dest) const { *reinterpret_cast<T*>(dest) = x_; }
46 
47  private:
48  T x_;
49  };
50 
51 }
52 }
53 
54 #endif
void all_reduce(const communicator &comm, const T &in, T &out, const Op &op)
all_reduce
Definition: collectives.hpp:300