DIY  3.0
data-parallel out-of-core C++ library
 All Classes Namespaces Functions Typedefs Groups Pages
communicator.hpp
1 namespace diy
2 {
3 namespace mpi
4 {
5 
9  {
10  public:
11  inline
12  communicator(MPI_Comm comm = MPI_COMM_WORLD, bool owner = false);
13 
14  inline
15  ~communicator();
16 
17  communicator(const communicator& other):
18  comm_(other.comm_),
19  rank_(other.rank_),
20  size_(other.size_),
21  owner_(false) {}
22 
23  communicator(communicator&& other):
24  comm_(other.comm_),
25  rank_(other.rank_),
26  size_(other.size_),
27  owner_(other.owner_) { other.owner_ = false; }
28 
30  operator=(const communicator& other) { comm_ = other.comm_; rank_ = other.rank_; size_ = other.size_; owner_ = false; return *this; }
32  operator=(communicator&& other) { comm_ = other.comm_; rank_ = other.rank_; size_ = other.size_; owner_ = other.owner_; other.owner_ = false; return *this; }
33 
34  int rank() const { return rank_; }
35  int size() const { return size_; }
36 
38  template<class T>
39  void send(int dest, int tag, const T& x) const { detail::send<T>()(comm_, dest, tag, x); }
40 
43  template<class T>
44  status recv(int source, int tag, T& x) const { return detail::recv<T>()(comm_, source, tag, x); }
45 
47  template<class T>
48  request isend(int dest, int tag, const T& x) const { return detail::isend<T>()(comm_, dest, tag, x); }
49 
52  template<class T>
53  request irecv(int source, int tag, T& x) const { return detail::irecv<T>()(comm_, source, tag, x); }
54 
56  inline
57  status probe(int source, int tag) const;
58 
60  inline
62  iprobe(int source, int tag) const;
63 
65  inline
66  void barrier() const;
67 
68  operator MPI_Comm() const { return comm_; }
69 
72  inline
73  communicator
74  split(int color, int key = 0) const;
75 
76  private:
77  MPI_Comm comm_;
78  int rank_;
79  int size_;
80  bool owner_;
81  };
82 }
83 }
84 
85 diy::mpi::communicator::
86 communicator(MPI_Comm comm, bool owner):
87  comm_(comm), rank_(0), size_(1), owner_(owner)
88 {
89 #ifndef DIY_NO_MPI
90  if (comm != MPI_COMM_NULL)
91  {
92  MPI_Comm_rank(comm_, &rank_);
93  MPI_Comm_size(comm_, &size_);
94  }
95 #endif
96 }
97 
98 diy::mpi::communicator::
99 ~communicator()
100 {
101 #ifndef DIY_NO_MPI
102  if (owner_)
103  MPI_Comm_free(&comm_);
104 #endif
105 }
106 
109 probe(int source, int tag) const
110 {
111  (void) source;
112  (void) tag;
113 
114 #ifndef DIY_NO_MPI
115  status s;
116  MPI_Probe(source, tag, comm_, &s.s);
117  return s;
118 #else
119  DIY_UNSUPPORTED_MPI_CALL(MPI_Probe);
120 #endif
121 }
122 
125 iprobe(int source, int tag) const
126 {
127  (void) source;
128  (void) tag;
129 #ifndef DIY_NO_MPI
130  status s;
131  int flag;
132  MPI_Iprobe(source, tag, comm_, &flag, &s.s);
133  if (flag)
134  return s;
135 #endif
136  return optional<status>();
137 }
138 
139 void
141 barrier() const
142 {
143 #ifndef DIY_NO_MPI
144  MPI_Barrier(comm_);
145 #endif
146 }
147 
150 split(int color, int key) const
151 {
152 #ifndef DIY_NO_MPI
153  MPI_Comm newcomm;
154  MPI_Comm_split(comm_, color, key, &newcomm);
155  return communicator(newcomm, true);
156 #else
157  return communicator();
158 #endif
159 }
communicator split(int color, int key=0) const
split When keys are the same, the ties are broken by the rank in the original comm.
Definition: communicator.hpp:150
request isend(int dest, int tag, const T &x) const
Non-blocking version of send().
Definition: communicator.hpp:48
status probe(int source, int tag) const
probe
Definition: communicator.hpp:109
Simple wrapper around MPI_Comm.
Definition: communicator.hpp:8
request irecv(int source, int tag, T &x) const
Non-blocking version of recv(). If T is an std::vector<...>, its size must be big enough to accommoda...
Definition: communicator.hpp:53
void barrier() const
barrier
Definition: communicator.hpp:141
Definition: status.hpp:5
status recv(int source, int tag, T &x) const
Receive x from dest using tag (blocking). If T is an std::vector<...>, recv will resize it to fit exa...
Definition: communicator.hpp:44
optional< status > iprobe(int source, int tag) const
iprobe
Definition: communicator.hpp:125
Definition: optional.hpp:6
Definition: request.hpp:5
void send(int dest, int tag, const T &x) const
Send x to processor dest using tag (blocking).
Definition: communicator.hpp:39