FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
synch_vec.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
9#include <kernel/util/dist.hpp>
10#include <kernel/util/time_stamp.hpp>
11#include <kernel/util/statistics.hpp>
12#include <kernel/lafem/dense_vector.hpp>
13
14namespace FEAT
15{
16 namespace Global
17 {
27 template <typename VT_, typename VMT_>
29 {
30 public:
32 typedef typename VT_::DataType DataType;
35
36 protected:
39
40#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
42 VT_* _target;
46 const std::vector<VMT_>* _mirrors;
50 std::vector<BufferType> _send_bufs, _recv_bufs;
52 std::vector<Memory::TypedView<DataType>> _send_views, _recv_views;
53
54#endif // FEAT_HAVE_MPI || DOXYGEN
55
56 public:
59#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
60 _finished(true),
61 _target(nullptr),
62 _comm(nullptr),
63 _mirrors(nullptr),
64 _send_reqs(),
65 _recv_reqs(),
66 _send_bufs(),
67 _recv_bufs(),
69 _recv_views()
70#else
71 _finished(true)
72#endif // FEAT_HAVE_MPI || DOXYGEN
73 {
74 }
75
91#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
92 SynchVectorTicket(VT_ & target, const Dist::Comm& comm, const std::vector<int>& ranks, const std::vector<VMT_> & mirrors) :
93 _finished(false),
94 _target(&target),
95 _comm(&comm),
96 _mirrors(&mirrors)
97 {
98 TimeStamp ts_start;
99 const std::size_t n = ranks.size();
100
101 XASSERTM(_mirrors->size() == n, "invalid vector mirror count");
102
103 // post receives
104 _recv_reqs.reserve(n);
105 _recv_bufs.resize(n);
106 _recv_views.resize(n);
107 for(std::size_t i(0); i < n; ++i)
108 {
109 // skip hollow mirrors
110 if(_mirrors->at(i).hollow())
111 continue;
112
113 // create buffer vector in main memory
114 _recv_bufs.at(i) = BufferType(_mirrors->at(i).buffer_size(*_target));
115
116 // post receive
117 _recv_views.at(i) = _recv_bufs.at(i).elements_view_w();
118 _recv_reqs.push_back(_comm->irecv(_recv_views.at(i).get_w(), _recv_bufs.at(i).size(), ranks.at(i)));
119 }
120
121 // post sends
123 _send_bufs.resize(n);
124 _send_views.resize(n);
125 for(std::size_t i(0); i < n; ++i)
126 {
127 // skip hollow mirrors
128 if(_mirrors->at(i).hollow())
129 continue;
130
131 // create buffer in device memory
132 _send_bufs.at(i) = BufferType(_mirrors->at(i).buffer_size(*_target));
133
134 // gather from mirror
135 _mirrors->at(i).gather(_send_bufs.at(i), *_target);
136
137 // post send
138 _send_views.at(i) = _send_bufs.at(i).elements_view_r();
139 _send_reqs.push_back(_comm->isend(_send_views.at(i).get_r(), _send_bufs.at(i).size(), ranks.at(i)));
140 }
141
142 Statistics::add_time_mpi_execute_blas2(ts_start.elapsed_now());
143 }
144#else // non-MPI version
145 SynchVectorTicket(VT_ &, const Dist::Comm&, const std::vector<int>& ranks, const std::vector<VMT_> &) :
146 _finished(false)
147 {
148 XASSERT(ranks.empty());
149 }
150#endif // FEAT_HAVE_MPI
152 SynchVectorTicket(const SynchVectorTicket &) = delete;
155
158#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
159 _finished(other._finished),
160 _target(other._target),
161 _comm(other._comm),
162 _mirrors(other._mirrors),
163 _send_reqs(std::forward<Dist::RequestVector>(other._send_reqs)),
164 _recv_reqs(std::forward<Dist::RequestVector>(other._recv_reqs)),
165 _send_bufs(std::forward<std::vector<BufferType>>(other._send_bufs)),
166 _recv_bufs(std::forward<std::vector<BufferType>>(other._recv_bufs)),
167 _send_views(std::forward<std::vector<Memory::TypedView<DataType>>>(other._send_views)),
168 _recv_views(std::forward<std::vector<Memory::TypedView<DataType>>>(other._recv_views))
169 {
170 other._finished = true;
171 other._comm = nullptr;
172 other._target = nullptr;
173 other._mirrors = nullptr;
174 }
175#else
176 _finished(other._finished)
177 {
178 other._finished = true;
179 }
180#endif // FEAT_HAVE_MPI
181
184 {
185 if(this == &other)
186 return *this;
187
188#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
189 _finished = other._finished;
190 _target = other._target;
191 _comm = other._comm;
192 _mirrors = other._mirrors;
193 _send_reqs = std::forward<Dist::RequestVector>(other._send_reqs);
194 _recv_reqs = std::forward<Dist::RequestVector>(other._recv_reqs);
195 _send_bufs = std::forward<std::vector<BufferType>>(other._send_bufs);
196 _recv_bufs = std::forward<std::vector<BufferType>>(other._recv_bufs);
197 _send_views = std::forward<std::vector<Memory::TypedView<DataType>>>(other._send_views);
198 _recv_views = std::forward<std::vector<Memory::TypedView<DataType>>>(other._recv_views);
199
200 other._finished = true;
201 other._comm = nullptr;
202 other._target = nullptr;
203 other._mirrors = nullptr;
204#else
205 _finished = other._finished;
206 other._finished = true;
207#endif // FEAT_HAVE_MPI
208
209 return *this;
210 }
211
218 void wait()
219 {
220 XASSERTM(!_finished, "ticket was already completed by a wait call");
221
222#ifdef FEAT_HAVE_MPI
223 TimeStamp ts_start;
224
225 // process all pending receives
226 for(std::size_t idx(0u); _recv_reqs.wait_any(idx); )
227 {
228 // scatter the receive buffer
229 _recv_views.at(idx).release();
230 _mirrors->at(idx).scatter_axpy(*_target, _recv_bufs.at(idx));
231 }
232
233 // wait for all sends to finish
235
236 Statistics::add_time_mpi_wait_blas2(ts_start.elapsed_now());
237#endif // FEAT_HAVE_MPI
238
239 _finished = true;
240 }
241
244 {
245 XASSERTM(_finished, "trying to destroy an unfinished SynchVectorTicket");
246 }
247 }; // class SynchVectorTicket
248 } // namespace Global
249} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Communicator class.
Definition: dist.hpp:1657
Request irecv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag=0) const
Nonblocking Receive.
Definition: dist.cpp:719
Request isend(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Nonblocking Send.
Definition: dist.cpp:707
Communication Request vector class.
Definition: dist.hpp:948
void push_back(Request &&request)
Inserts a new request at the end of the request vector.
Definition: dist.hpp:1194
void reserve(std::size_t size_)
Reserves sufficient space for a specified number of requests.
Definition: dist.hpp:1121
bool wait_any(std::size_t &idx, Status &status)
Blocks until one of the active requests has been fulfilled.
Definition: dist.cpp:332
void wait_all()
Blocks until all active requests are fulfilled.
Definition: dist.cpp:327
Ticket class for asynchronous global operations on vectors.
Definition: synch_vec.hpp:29
SynchVectorTicket()
default constructor
Definition: synch_vec.hpp:58
const std::vector< VMT_ > * _mirrors
the vector mirrors
Definition: synch_vec.hpp:46
SynchVectorTicket(VT_ &target, const Dist::Comm &comm, const std::vector< int > &ranks, const std::vector< VMT_ > &mirrors)
Constructor.
Definition: synch_vec.hpp:92
VT_ * _target
the vector to be synchronized
Definition: synch_vec.hpp:42
SynchVectorTicket & operator=(SynchVectorTicket &&other)
move-assign operator
Definition: synch_vec.hpp:183
VT_::DataType DataType
our data type
Definition: synch_vec.hpp:32
SynchVectorTicket(SynchVectorTicket &&other)
move constructor
Definition: synch_vec.hpp:157
const Dist::Comm * _comm
our communicator
Definition: synch_vec.hpp:44
LAFEM::DenseVector< typename VT_::DataType, typename VT_::IndexType > BufferType
the buffer vector type
Definition: synch_vec.hpp:34
std::vector< Memory::TypedView< DataType > > _send_views
send and receive buffer views
Definition: synch_vec.hpp:52
Dist::RequestVector _send_reqs
send and receive request vectors
Definition: synch_vec.hpp:48
SynchVectorTicket & operator=(const SynchVectorTicket &)=delete
Unwanted copy assignment operator: Do not implement!
std::vector< BufferType > _send_bufs
send and receive buffers
Definition: synch_vec.hpp:50
bool _finished
signals, whether wait was already called
Definition: synch_vec.hpp:38
Dense data vector class template.
Time stamp class.
Definition: time_stamp.hpp:54
double elapsed_now() const
Calculates the time elapsed between the time stamp and now.
Definition: time_stamp.hpp:121
FEAT namespace.
Definition: adjactor.hpp:12