FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
gate.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>
11#include <kernel/lafem/dense_vector.hpp>
12#include <kernel/global/synch_vec.hpp>
13#include <kernel/global/synch_scal.hpp>
14
15#include <vector>
16
17namespace FEAT
18{
22 namespace Global
23 {
24 enum class SyncLocation : std::uint8_t
25 {
26 host = 0, // always sync on host memory
27 device, // always sync on cuda device
28 backend // use the preferred backend
29 };
30
31 inline std::ostream& operator<<(std::ostream& os, SyncLocation loc)
32 {
33 switch (loc)
34 {
35 case FEAT::Global::SyncLocation::host:
36 {
37 return os << "host";
38 }
39 case FEAT::Global::SyncLocation::device:
40 {
41 return os << "device";
42 }
43 case FEAT::Global::SyncLocation::backend:
44 {
45 return os << "backend";
46 }
47 }
48 return os;
49 }
50
76 template<typename LocalVector_, typename Mirror_>
77 class Gate
78 {
79 public:
81 typedef typename LocalVector_::DataType DataType;
83 typedef typename LocalVector_::IndexType IndexType;
85 typedef LocalVector_ LocalVectorType;
87 typedef Mirror_ MirrorType;
90
93
94 public:
98 std::vector<int> _ranks;
100 std::vector<Mirror_> _mirrors;
102 mutable std::vector<BufferVectorType> _send_buffers, _recv_buffers;
104 mutable Dist::RequestVector _send_reqs, _recv_reqs;
106 mutable std::vector<Memory::TypedView<DataType>> _send_views, _recv_views;
108 LocalVector_ _freqs;
109
110 SyncLocation _sync_location = SyncLocation::host;
111
113 template <typename LocalVector2_, typename Mirror2_>
115
117 template <typename DataType2_, typename IndexType2_>
119
120 public:
122 explicit Gate() :
123 _comm(nullptr)
124 {
125 }
126
133 explicit Gate(const Dist::Comm& comm) :
134 _comm(&comm)
135 {
136 }
137
139 Gate(Gate&& other) :
140 _comm(other._comm),
141 _ranks(std::move(other._ranks)),
142 _mirrors(std::move(other._mirrors)),
143 _send_buffers(std::move(other._send_buffers)),
144 _recv_buffers(std::move(other._recv_buffers)),
145 _send_reqs(std::move(other._send_reqs)),
146 _recv_reqs(std::move(other._recv_reqs)),
147 _send_views(std::move(other._send_views)),
148 _recv_views(std::move(other._recv_views)),
149 _freqs(std::move(other._freqs))
150 {
151 }
152
155 {
156 if(this == &other)
157 {
158 return *this;
159 }
160
161 _comm = other._comm;
162 _ranks = std::move(other._ranks);
163 _mirrors = std::move(other._mirrors);
164 _send_buffers = std::move(other._send_buffers);
165 _recv_buffers = std::move(other._recv_buffers);
166 _send_reqs = std::move(other._send_reqs);
167 _recv_reqs = std::move(other._recv_reqs);
168 _send_views = std::move(other._send_views);
169 _recv_views = std::move(other._recv_views);
170 _freqs = std::move(other._freqs);
171
172 return *this;
173 }
174
176 virtual ~Gate()
177 {
178 }
179
185 const Dist::Comm* get_comm() const
186 {
187 return this->_comm;
188 }
189
196 void set_comm(const Dist::Comm* comm_)
197 {
198 this->_comm = comm_;
199 }
200
206 const std::vector<int> get_ranks() const
207 {
208 return this->_ranks;
209 }
210
216 const std::vector<Mirror_>& get_mirrors() const
217 {
218 return this->_mirrors;
219 }
220
226 const LocalVector_& get_freqs() const
227 {
228 return this->_freqs;
229 }
230
237 template<typename LVT2_, typename MT2_>
238 void convert(const Gate<LVT2_, MT2_>& other)
239 {
240 if((void*)this == (void*)&other)
241 return;
242
243 this->_comm = other._comm;
244 this->_ranks = other._ranks;
245
246 const std::size_t size = other._mirrors.size();
247
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)
254 {
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_()));
258 }
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);
265
266 this->_freqs.convert(other._freqs);
267 }
268
288 template<typename LVT2_>
289 void convert(const Gate<LVT2_, Mirror_>& other, LocalVector_&& vector, LAFEM::CloneMode mode = LAFEM::CloneMode::Shallow)
290 {
291 if((void*)this == (void*)&other)
292 return;
293
294 const std::size_t size = other._mirrors.size();
295 this->_ranks.clear();
296 this->_mirrors.clear();
297 this->_send_buffers.clear(); // will be rebuild by compile function
298 this->_recv_buffers.clear(); // will be rebuild by compile function
299 this->_mirrors.resize(size);
300 this->_send_buffers.reserve(size);
301 this->_recv_buffers.reserve(size);
302 this->_freqs.clear(); // will be rebuild by compile function
303
304 this->_comm = other._comm;
305 this->_ranks = other._ranks;
306
307 // shallow-clone mirrors
308 for(std::size_t i(0); i < _mirrors.size(); ++i)
309 {
310 _mirrors.at(i).clone(other._mirrors.at(i), mode);
311 }
312
313 // compile this gate
314 compile(std::forward<LocalVector_>(vector));
315 }
316
318 std::size_t bytes() const
319 {
320 size_t temp(0);
321 for(const auto& i : _mirrors)
322 {
323 temp += i.bytes();
324 }
325 for(const auto& i : _send_buffers)
326 {
327 temp += i.bytes();
328 }
329 for(const auto& i : _recv_buffers)
330 {
331 temp += i.bytes();
332 }
333 temp += _freqs.bytes();
334 temp += _ranks.size() * sizeof(int);
335
336 return temp;
337 }
338
348 void push(int rank, Mirror_&& mirror)
349 {
350 XASSERT(this->_comm != nullptr);
351 XASSERT(rank < this->_comm->size());
352
353 // push rank and tags
354 _ranks.push_back(rank);
355
356 // push mirror
357 _mirrors.push_back(std::move(mirror));
358 }
359
370 void compile(LocalVector_&& vector)
371 {
372 const std::size_t size = _mirrors.size();
373 // initialize frequency vector
374 _freqs = std::move(vector);
375 _freqs.format(DataType(1));
376
377 _send_buffers.clear();
378 _send_buffers.reserve(size);
379 _recv_buffers.clear();
380 _recv_buffers.reserve(size);
381
382 // loop over all mirrors and create the buffers
383 for(std::size_t i(0); i < size; ++i)
384 {
385 _send_buffers.emplace_back(_mirrors.at(i).create_buffer(_freqs));
386 _recv_buffers.emplace_back(_mirrors.at(i).create_buffer(_freqs));
387 }
388
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);
395
396 // loop over all mirrors
397 for(std::size_t i(0); i < _mirrors.size(); ++i)
398 {
399 // skip empty mirrors
400 if(_mirrors.at(i).hollow())
401 continue;
402
403 // sum up number of ranks per frequency entry, listed in different mirrors
404 auto& temp = _send_buffers.at(i);
405 temp.format(DataType(1));
406
407 // gather-axpy into frequency vector
408 _mirrors.at(i).scatter_axpy(_freqs, temp);
409 }
410
411 // invert frequencies
412 _freqs.component_invert(_freqs);
413 }
414
421 {
422 return _freqs.size();
423 }
424
431 {
432 return _freqs.size_raw();
433 }
434
444 {
445 XASSERT(this->_comm != nullptr);
446 if(this->_comm->size() <= 1)
447 return this->get_num_local_dofs();
448
449 // get my rank
450 const int my_rank = this->_comm->rank();
451
452 // get local number of DOFs
453 const Index loc_dofs = this->get_num_local_dofs();
454
455 // create a local mask vector and format it to 1
456 std::vector<int> mask(std::size_t(loc_dofs), 1);
457
458 // set all DOFs, which are shared with a lower rank neighbor, to 0
459 for(std::size_t i(0); i < _mirrors.size(); ++i)
460 {
461 if(this->_ranks.at(i) < my_rank)
462 this->_mirrors.at(i).mask_scatter(this->_freqs, mask, 0);
463 }
464
465 // count the number of DOFs that are still 1 and thus not owned by a lower rank neighbor
466 Index owned_dofs(0u);
467 for(const auto& k : mask)
468 owned_dofs += Index(k);
469
470 // now sum up the number of owned DOFs over all processes
471 Index global_dofs(0u);
472 this->_comm->allreduce(&owned_dofs, &global_dofs, std::size_t(1), Dist::op_sum);
473
474 // done!
475 return global_dofs;
476 }
477
487 {
488 XASSERT(this->_comm != nullptr);
489 if(this->_comm->size() <= 1)
490 return this->get_num_local_dofs_raw();
491
492 // get my rank
493 const int my_rank = this->_comm->rank();
494
495 // get local number of DOFs
496 const Index loc_dofs = this->get_num_local_dofs_raw();
497
498 // create a local mask vector and format it to 1
499 std::vector<int> mask(std::size_t(loc_dofs), 1);
500
501 // set all DOFs, which are shared with a lower rank neighbor, to 0
502 for(std::size_t i(0); i < _mirrors.size(); ++i)
503 {
504 if(this->_ranks.at(i) < my_rank)
505 this->_mirrors.at(i).mask_scatter_raw(this->_freqs, mask, 0);
506 }
507
508 // count the number of DOFs that are still 1 and thus not owned by a lower rank neighbor
509 Index owned_dofs(0u);
510 for(const auto& k : mask)
511 owned_dofs += Index(k);
512
513 // now sum up the number of owned DOFs over all processes
514 Index global_dofs(0u);
515 this->_comm->allreduce(&owned_dofs, &global_dofs, std::size_t(1), Dist::op_sum);
516
517 // done!
518 return global_dofs;
519 }
520
530 void from_1_to_0(LocalVector_& vector) const
531 {
532 if(!_ranks.empty())
533 {
534 vector.component_product(vector, _freqs);
535 }
536 }
537
551 // void sync_0(LocalVector_& vector, PreferredBackend sync_backend = Backend::get_preferred_backend()) const
552 void sync_0(LocalVector_& vector) const
553 {
554 BackendScope scope(_get_sync_backend());
555 _sync_vec(vector);
556 }
557
573 VectorTicketType sync_0_async(LocalVector_& vector) const
574 {
576 }
577
598 // void sync_1(LocalVector_& vector, PreferredBackend sync_backend = Backend::get_preferred_backend()) const
599 void sync_1(LocalVector_& vector) const
600 {
601 if(!_ranks.empty())
602 {
603 BackendScope scope(_get_sync_backend());
604 from_1_to_0(vector);
605 _sync_vec(vector);
606 }
607 }
608
627 VectorTicketType sync_1_async(LocalVector_& vector) const
628 {
629 from_1_to_0(vector);
631 }
632
645 DataType dot(const LocalVector_& x, const LocalVector_& y) const
646 {
647 // This is if there is only one process
648 if(_comm == nullptr || _comm->size() == 1)
649 {
650 return x.dot(y);
651 }
652 // Even if there are no neighbors, we still need to sum up globally
653 else if(_ranks.empty())
654 {
655 return sum(x.dot(y));
656 }
657 // If there are neighbors, we have to use the frequencies and sum up globally
658 else
659 {
660 return sum(_freqs.triple_dot(x, y));
661 }
662 }
663
675 ScalarTicketType dot_async(const LocalVector_& x, const LocalVector_& y, bool sqrt = false) const
676 {
677 return sum_async(_freqs.triple_dot(x, y), sqrt);
678 }
679
693 {
694 return sum_async(x).wait();
695 }
696
708 ScalarTicketType sum_async(DataType x, bool sqrt = false) const
709 {
711 }
712
726 {
727 return min_async(x).wait();
728 }
729
742 {
744 }
745
759 {
760 return max_async(x).wait();
761 }
762
775 {
777 }
778
795 {
796 return norm2_async(x).wait();
797 }
798
814 {
816 }
817
824 void set_sync_location(SyncLocation sync_location)
825 {
826 _sync_location = sync_location;
827 }
828
829 SyncLocation get_sync_location() const
830 {
831 return _sync_location;
832 }
833
834 protected:
840 void _sync_vec(LocalVector_& vector) const
841 {
842 if(_ranks.empty())
843 {
844 return;
845 }
846
848 const bool flip_sync_order = sync_backend == PreferredBackend::cuda;
849
850 const std::size_t n = this->_mirrors.size();
851 // post receives
852 for(std::size_t i(0); i < n; ++i)
853 {
854 // skip hollow mirrors
855 if(this->_mirrors.at(i).hollow())
856 continue;
857
858 // post receive
859 _recv_views.at(i) = _recv_buffers.at(i).elements_view_w(Memory::to_location(sync_backend));
860 _recv_reqs.push_back(_comm->irecv(_recv_views.at(i).get_w(), _recv_buffers.at(i).size(), _ranks.at(i)));
861 }
862
863 if(flip_sync_order)
864 {
865 // post sends
866 for(std::size_t i(0); i < n; ++i)
867 {
868 // skip hollow mirrors
869 if(_mirrors.at(i).hollow())
870 continue;
871
872 // gather from mirror
873 _mirrors.at(i).gather(_send_buffers.at(i), vector);
874 }
875
877 }
878 // post sends
879 for(std::size_t i(0); i < n; ++i)
880 {
881 // skip hollow mirrors
882 if(_mirrors.at(i).hollow())
883 continue;
884
885 // gather from mirror
886 if(!flip_sync_order)
887 _mirrors.at(i).gather(_send_buffers.at(i), vector);
888
889 // post send
890 _send_views.at(i) = _send_buffers.at(i).elements_view_r(Memory::to_location(sync_backend));
891 _send_reqs.push_back(_comm->isend(_send_views.at(i).get_r(), _send_buffers.at(i).size(), _ranks.at(i)));
892 }
893
894 // process all pending receives
895 for(std::size_t idx(0u); _recv_reqs.wait_any(idx); )
896 {
897 // scatter the receive buffer
898 _recv_views.at(idx).release();
899 _mirrors.at(idx).scatter_axpy(vector, _recv_buffers.at(idx));
900 }
901
903 // release send views
904 for(auto& v : _send_views)
905 {
906 v.release();
907 }
909 _recv_reqs.clear();
910 }
911
912 protected:
913
914 PreferredBackend _get_sync_backend() const
915 {
916 switch (this->_sync_location)
917 {
918 case SyncLocation::host:
920 case SyncLocation::device:
922 case SyncLocation::backend:
924 }
926 }
927
928 }; // class Gate<...>
929 } // namespace Global
930} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
FEAT Kernel base header.
static PreferredBackend get_preferred_backend()
get current preferred backend
Definition: backend.cpp:32
Communicator class.
Definition: dist.hpp:1657
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
Definition: dist.cpp:658
int size() const
Returns the size of this communicator.
Definition: dist.hpp:1814
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
int rank() const
Returns the rank of this process in this communicator.
Definition: dist.hpp:1802
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
void clear()
Clears the request vector.
Definition: dist.hpp:1323
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
Global gate implementation.
Definition: gate.hpp:78
void compile(LocalVector_ &&vector)
Compiles the gate to finish its setup.
Definition: gate.hpp:370
virtual ~Gate()
virtual destructor
Definition: gate.hpp:176
void sync_0(LocalVector_ &vector) const
Synchronizes a type-0 vector, resulting in a type-1 vector.
Definition: gate.hpp:552
void convert(const Gate< LVT2_, MT2_ > &other)
Conversion function for same vector container type but with different MDI-Type.
Definition: gate.hpp:238
const Dist::Comm * get_comm() const
Returns a const pointer to the underlying communicator.
Definition: gate.hpp:185
std::vector< BufferVectorType > _send_buffers
persistent buffer vectors
Definition: gate.hpp:102
const LocalVector_ & get_freqs() const
Returns a const reference to the frequencies vector.
Definition: gate.hpp:226
void push(int rank, Mirror_ &&mirror)
Adds a mirror for a neighbor process.
Definition: gate.hpp:348
void from_1_to_0(LocalVector_ &vector) const
Converts a type-1 vector into a type-0 vector.
Definition: gate.hpp:530
std::vector< Memory::TypedView< DataType > > _send_views
send and receive buffer views
Definition: gate.hpp:106
LocalVector_::IndexType IndexType
the index type
Definition: gate.hpp:83
LAFEM::DenseVector< DataType, IndexType > BufferVectorType
the internal buffer vector type
Definition: gate.hpp:89
ScalarTicketType sum_async(DataType x, bool sqrt=false) const
Computes a reduced sum over all processes.
Definition: gate.hpp:708
DataType max(DataType x) const
Computes the maximum of a scalar variable over all processes.
Definition: gate.hpp:758
Gate()
standard constructor
Definition: gate.hpp:122
Index get_num_local_dofs_raw() const
Returns the number of raw local DOFs.
Definition: gate.hpp:430
ScalarTicketType min_async(DataType x) const
Computes the minimum of a scalar variable over all processes.
Definition: gate.hpp:741
Index get_num_global_dofs() const
Returns the number of native global DOFs.
Definition: gate.hpp:443
Index get_num_global_dofs_raw() const
Returns the number of raw global DOFs.
Definition: gate.hpp:486
std::vector< Mirror_ > _mirrors
vector mirrors
Definition: gate.hpp:100
Index get_num_local_dofs() const
Returns the number of native local DOFs.
Definition: gate.hpp:420
DataType norm2(DataType x) const
Computes a reduced 2-norm over all processes.
Definition: gate.hpp:794
LocalVector_ LocalVectorType
the local vector type
Definition: gate.hpp:85
ScalarTicketType norm2_async(DataType x) const
Computes a reduced 2-norm over all processes.
Definition: gate.hpp:813
void set_comm(const Dist::Comm *comm_)
Sets the communicator for this gate.
Definition: gate.hpp:196
const Dist::Comm * _comm
our communicator
Definition: gate.hpp:96
Gate & operator=(Gate &&other)
move-assign operator
Definition: gate.hpp:154
const std::vector< Mirror_ > & get_mirrors() const
Returns a const reference to the neighbor mirrors vector.
Definition: gate.hpp:216
void convert(const Gate< LVT2_, Mirror_ > &other, LocalVector_ &&vector, LAFEM::CloneMode mode=LAFEM::CloneMode::Shallow)
Conversion function for different vector container type.
Definition: gate.hpp:289
ScalarTicketType dot_async(const LocalVector_ &x, const LocalVector_ &y, bool sqrt=false) const
Computes a synchronized dot-product of two type-1 vectors.
Definition: gate.hpp:675
ScalarTicketType max_async(DataType x) const
Computes the maximum of a scalar variable over all processes.
Definition: gate.hpp:774
Mirror_ MirrorType
the mirror type
Definition: gate.hpp:87
void sync_1(LocalVector_ &vector) const
Synchronizes a type-1 vector, resulting in a type-1 vector.
Definition: gate.hpp:599
Dist::RequestVector _send_reqs
persistent MPI Requests
Definition: gate.hpp:104
void _sync_vec(LocalVector_ &vector) const
Sychronizes a vector by adding all mirrors elements.
Definition: gate.hpp:840
VectorTicketType sync_1_async(LocalVector_ &vector) const
Synchronizes a type-1 vector, resulting in a type-1 vector.
Definition: gate.hpp:627
LocalVector_ _freqs
frequency vector
Definition: gate.hpp:108
const std::vector< int > get_ranks() const
Returns a const reference to the neighbor ranks vector.
Definition: gate.hpp:206
void set_sync_location(SyncLocation sync_location)
Set the sync location to be used.
Definition: gate.hpp:824
DataType sum(DataType x) const
Computes a reduced sum over all processes.
Definition: gate.hpp:692
DataType min(DataType x) const
Computes the minimum of a scalar variable over all processes.
Definition: gate.hpp:725
VectorTicketType sync_0_async(LocalVector_ &vector) const
Synchronizes a type-0 vector, resulting in a type-1 vector.
Definition: gate.hpp:573
Gate(Gate &&other)
move constructor
Definition: gate.hpp:139
std::vector< int > _ranks
communication ranks
Definition: gate.hpp:98
DataType dot(const LocalVector_ &x, const LocalVector_ &y) const
Computes a synchronized dot-product of two type-1 vectors.
Definition: gate.hpp:645
LocalVector_::DataType DataType
the data type
Definition: gate.hpp:81
std::size_t bytes() const
Returns the total amount of bytes allocated.
Definition: gate.hpp:318
Gate(const Dist::Comm &comm)
Constructor.
Definition: gate.hpp:133
Ticket class for asynchronous global operations on scalars.
Definition: synch_scal.hpp:31
Ticket class for asynchronous global operations on vectors.
Definition: synch_vec.hpp:29
Dense data vector class template.
static void synchronize_devices()
Synchronizes with all available devices (CUDA, HIP, etc...)
Definition: runtime.cpp:522
const Operation op_min(MPI_MIN)
Operation wrapper for MPI_MIN.
Definition: dist.hpp:575
const Operation op_max(MPI_MAX)
Operation wrapper for MPI_MAX.
Definition: dist.hpp:573
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
Definition: dist.hpp:571
FEAT namespace.
Definition: adjactor.hpp:12
PreferredBackend
The backend that shall be used in all compute heavy calculations.
Definition: backend.hpp:18
std::uint64_t Index
Index data type.