9#include <kernel/util/dist.hpp>
11#include <kernel/lafem/dense_vector.hpp>
12#include <kernel/global/synch_vec.hpp>
13#include <kernel/global/synch_scal.hpp>
24 enum class SyncLocation : std::uint8_t
31 inline std::ostream& operator<<(std::ostream& os, SyncLocation loc)
35 case FEAT::Global::SyncLocation::host:
39 case FEAT::Global::SyncLocation::device:
41 return os <<
"device";
43 case FEAT::Global::SyncLocation::backend:
45 return os <<
"backend";
76 template<
typename LocalVector_,
typename Mirror_>
81 typedef typename LocalVector_::DataType
DataType;
106 mutable std::vector<Memory::TypedView<DataType>>
_send_views, _recv_views;
110 SyncLocation _sync_location = SyncLocation::host;
113 template <
typename LocalVector2_,
typename Mirror2_>
117 template <
typename DataType2_,
typename IndexType2_>
144 _recv_buffers(std::move(other._recv_buffers)),
146 _recv_reqs(std::move(other._recv_reqs)),
148 _recv_views(std::move(other._recv_views)),
162 _ranks = std::move(other._ranks);
163 _mirrors = std::move(other._mirrors);
165 _recv_buffers = std::move(other._recv_buffers);
167 _recv_reqs = std::move(other._recv_reqs);
169 _recv_views = std::move(other._recv_views);
170 _freqs = std::move(other._freqs);
237 template<
typename LVT2_,
typename MT2_>
240 if((
void*)
this == (
void*)&other)
243 this->_comm = other._comm;
244 this->_ranks = other._ranks;
246 const std::size_t size = other._mirrors.size();
248 this->_mirrors.resize(size);
249 this->_send_buffers.clear();
250 this->_send_buffers.reserve(size);
251 this->_recv_buffers.clear();
252 this->_recv_buffers.reserve(size);
253 for(std::size_t i(0); i < size; ++i)
255 this->_mirrors.at(i).convert(other._mirrors.at(i));
256 this->_send_buffers.emplace_back(
_mirrors.at(i).create_buffer(LocalVector_()));
257 this->_recv_buffers.emplace_back(
_mirrors.at(i).create_buffer(LocalVector_()));
259 this->_send_reqs.
clear();
260 this->_recv_reqs.
clear();
261 this->_send_reqs.
reserve(size);
262 this->_recv_reqs.
reserve(size);
263 this->_send_views.resize(size);
264 this->_recv_views.resize(size);
266 this->_freqs.convert(other._freqs);
288 template<
typename LVT2_>
291 if((
void*)
this == (
void*)&other)
294 const std::size_t size = other._mirrors.size();
295 this->_ranks.clear();
296 this->_mirrors.clear();
297 this->_send_buffers.clear();
298 this->_recv_buffers.clear();
299 this->_mirrors.resize(size);
300 this->_send_buffers.reserve(size);
301 this->_recv_buffers.reserve(size);
302 this->_freqs.clear();
304 this->_comm = other._comm;
305 this->_ranks = other._ranks;
308 for(std::size_t i(0); i <
_mirrors.size(); ++i)
310 _mirrors.at(i).clone(other._mirrors.at(i), mode);
314 compile(std::forward<LocalVector_>(vector));
329 for(
const auto& i : _recv_buffers)
334 temp +=
_ranks.size() *
sizeof(int);
348 void push(
int rank, Mirror_&& mirror)
350 XASSERT(this->_comm !=
nullptr);
357 _mirrors.push_back(std::move(mirror));
372 const std::size_t size =
_mirrors.size();
374 _freqs = std::move(vector);
379 _recv_buffers.clear();
380 _recv_buffers.reserve(size);
383 for(std::size_t i(0); i < size; ++i)
389 this->_send_reqs.
clear();
390 this->_recv_reqs.
clear();
391 this->_send_reqs.
reserve(size);
392 this->_recv_reqs.
reserve(size);
393 this->_send_views.resize(size);
394 this->_recv_views.resize(size);
397 for(std::size_t i(0); i <
_mirrors.size(); ++i)
445 XASSERT(this->_comm !=
nullptr);
446 if(this->_comm->
size() <= 1)
450 const int my_rank = this->_comm->
rank();
456 std::vector<int> mask(std::size_t(loc_dofs), 1);
459 for(std::size_t i(0); i <
_mirrors.size(); ++i)
461 if(this->_ranks.at(i) < my_rank)
462 this->_mirrors.at(i).mask_scatter(this->_freqs, mask, 0);
466 Index owned_dofs(0u);
467 for(
const auto& k : mask)
468 owned_dofs +=
Index(k);
471 Index global_dofs(0u);
488 XASSERT(this->_comm !=
nullptr);
489 if(this->_comm->
size() <= 1)
493 const int my_rank = this->_comm->
rank();
499 std::vector<int> mask(std::size_t(loc_dofs), 1);
502 for(std::size_t i(0); i <
_mirrors.size(); ++i)
504 if(this->_ranks.at(i) < my_rank)
505 this->_mirrors.at(i).mask_scatter_raw(this->_freqs, mask, 0);
509 Index owned_dofs(0u);
510 for(
const auto& k : mask)
511 owned_dofs +=
Index(k);
514 Index global_dofs(0u);
534 vector.component_product(vector,
_freqs);
655 return sum(x.dot(y));
826 _sync_location = sync_location;
829 SyncLocation get_sync_location()
const
831 return _sync_location;
850 const std::size_t n = this->_mirrors.size();
852 for(std::size_t i(0); i < n; ++i)
855 if(this->_mirrors.at(i).hollow())
859 _recv_views.at(i) = _recv_buffers.at(i).elements_view_w(Memory::to_location(sync_backend));
866 for(std::size_t i(0); i < n; ++i)
879 for(std::size_t i(0); i < n; ++i)
895 for(std::size_t idx(0u); _recv_reqs.
wait_any(idx); )
898 _recv_views.at(idx).release();
899 _mirrors.at(idx).scatter_axpy(vector, _recv_buffers.at(idx));
916 switch (this->_sync_location)
918 case SyncLocation::host:
920 case SyncLocation::device:
922 case SyncLocation::backend:
#define XASSERT(expr)
Assertion macro definition.
static PreferredBackend get_preferred_backend()
get current preferred backend
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
int size() const
Returns the size of this communicator.
Request irecv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag=0) const
Nonblocking Receive.
Request isend(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Nonblocking Send.
int rank() const
Returns the rank of this process in this communicator.
Communication Request vector class.
void push_back(Request &&request)
Inserts a new request at the end of the request vector.
void reserve(std::size_t size_)
Reserves sufficient space for a specified number of requests.
void clear()
Clears the request vector.
bool wait_any(std::size_t &idx, Status &status)
Blocks until one of the active requests has been fulfilled.
void wait_all()
Blocks until all active requests are fulfilled.
Global gate implementation.
void compile(LocalVector_ &&vector)
Compiles the gate to finish its setup.
virtual ~Gate()
virtual destructor
void sync_0(LocalVector_ &vector) const
Synchronizes a type-0 vector, resulting in a type-1 vector.
void convert(const Gate< LVT2_, MT2_ > &other)
Conversion function for same vector container type but with different MDI-Type.
const Dist::Comm * get_comm() const
Returns a const pointer to the underlying communicator.
std::vector< BufferVectorType > _send_buffers
persistent buffer vectors
const LocalVector_ & get_freqs() const
Returns a const reference to the frequencies vector.
void push(int rank, Mirror_ &&mirror)
Adds a mirror for a neighbor process.
void from_1_to_0(LocalVector_ &vector) const
Converts a type-1 vector into a type-0 vector.
std::vector< Memory::TypedView< DataType > > _send_views
send and receive buffer views
LocalVector_::IndexType IndexType
the index type
LAFEM::DenseVector< DataType, IndexType > BufferVectorType
the internal buffer vector type
ScalarTicketType sum_async(DataType x, bool sqrt=false) const
Computes a reduced sum over all processes.
DataType max(DataType x) const
Computes the maximum of a scalar variable over all processes.
Gate()
standard constructor
Index get_num_local_dofs_raw() const
Returns the number of raw local DOFs.
ScalarTicketType min_async(DataType x) const
Computes the minimum of a scalar variable over all processes.
Index get_num_global_dofs() const
Returns the number of native global DOFs.
Index get_num_global_dofs_raw() const
Returns the number of raw global DOFs.
std::vector< Mirror_ > _mirrors
vector mirrors
Index get_num_local_dofs() const
Returns the number of native local DOFs.
DataType norm2(DataType x) const
Computes a reduced 2-norm over all processes.
LocalVector_ LocalVectorType
the local vector type
ScalarTicketType norm2_async(DataType x) const
Computes a reduced 2-norm over all processes.
void set_comm(const Dist::Comm *comm_)
Sets the communicator for this gate.
const Dist::Comm * _comm
our communicator
Gate & operator=(Gate &&other)
move-assign operator
const std::vector< Mirror_ > & get_mirrors() const
Returns a const reference to the neighbor mirrors vector.
void convert(const Gate< LVT2_, Mirror_ > &other, LocalVector_ &&vector, LAFEM::CloneMode mode=LAFEM::CloneMode::Shallow)
Conversion function for different vector container type.
ScalarTicketType dot_async(const LocalVector_ &x, const LocalVector_ &y, bool sqrt=false) const
Computes a synchronized dot-product of two type-1 vectors.
ScalarTicketType max_async(DataType x) const
Computes the maximum of a scalar variable over all processes.
Mirror_ MirrorType
the mirror type
void sync_1(LocalVector_ &vector) const
Synchronizes a type-1 vector, resulting in a type-1 vector.
Dist::RequestVector _send_reqs
persistent MPI Requests
void _sync_vec(LocalVector_ &vector) const
Sychronizes a vector by adding all mirrors elements.
VectorTicketType sync_1_async(LocalVector_ &vector) const
Synchronizes a type-1 vector, resulting in a type-1 vector.
LocalVector_ _freqs
frequency vector
const std::vector< int > get_ranks() const
Returns a const reference to the neighbor ranks vector.
void set_sync_location(SyncLocation sync_location)
Set the sync location to be used.
DataType sum(DataType x) const
Computes a reduced sum over all processes.
DataType min(DataType x) const
Computes the minimum of a scalar variable over all processes.
VectorTicketType sync_0_async(LocalVector_ &vector) const
Synchronizes a type-0 vector, resulting in a type-1 vector.
Gate(Gate &&other)
move constructor
std::vector< int > _ranks
communication ranks
DataType dot(const LocalVector_ &x, const LocalVector_ &y) const
Computes a synchronized dot-product of two type-1 vectors.
LocalVector_::DataType DataType
the data type
std::size_t bytes() const
Returns the total amount of bytes allocated.
Gate(const Dist::Comm &comm)
Constructor.
Ticket class for asynchronous global operations on scalars.
Ticket class for asynchronous global operations on vectors.
Dense data vector class template.
static void synchronize_devices()
Synchronizes with all available devices (CUDA, HIP, etc...)
const Operation op_min(MPI_MIN)
Operation wrapper for MPI_MIN.
const Operation op_max(MPI_MAX)
Operation wrapper for MPI_MAX.
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
PreferredBackend
The backend that shall be used in all compute heavy calculations.
std::uint64_t Index
Index data type.