FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
alg_dof_parti.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
8#include <kernel/global/gate.hpp>
9#include <kernel/global/vector.hpp>
10#include <kernel/lafem/dense_vector.hpp>
11#include <kernel/lafem/vector_mirror.hpp>
12#include <kernel/lafem/tuple_mirror.hpp>
13#include <kernel/util/omp_util.hpp>
14
15#include <vector>
16
17namespace FEAT
18{
19 namespace Global
20 {
22 namespace Intern
23 {
24 template<typename IdxVector_>
25 class ADPAux;
26 } // namespace Intern
28
190 template<typename LocalVector_, typename Mirror_>
192 {
193 public:
195 typedef LocalVector_ LocalVectorType;
197 typedef Mirror_ MirrorType;
198
200 typedef typename LocalVectorType::DataType DataType;
202 typedef typename LocalVectorType::IndexType IndexType;
203
208
210 typedef typename LocalVectorType::template ContainerTypeByDI<IndexType, IndexType> IndexVectorType;
211
213 typedef Intern::ADPAux<IndexVectorType> ADPAuxType;
214
217
224
225 //protected:
247 std::vector<int> _owner_ranks;
249 std::vector<int> _donee_ranks;
251 std::vector<OwnerMirrorType> _owner_mirrors;
253 std::vector<DoneeMirrorType> _donee_mirrors;
256
257 // Note: the following vectors hold int objects, because these are used as recvcount in an
258 // MPI_Allgatherv call, which only accepts int objects as counts; we have an XASSERT in
259 // place to ensure that we do not overflow the int datatype with more than a billion DOFs
260
262 std::vector<int> _all_global_dof_offset;
264 std::vector<int> _all_global_dof_counts;
265
266 public:
269 _comm(nullptr),
277 {
278 }
279
281 AlgDofParti(const AlgDofParti&) = delete;
284
286 virtual ~AlgDofParti() = default;
287
289 void clear()
290 {
291 _comm = nullptr;
299 _global_dof_idx.clear();
300 _owned_mirror.clear();
301 _owner_mirrors.clear();
302 _donee_mirrors.clear();
305 }
306
308 const Dist::Comm* get_comm() const
309 {
310 return _comm;
311 }
312
314 std::size_t bytes() const
315 {
316 std::size_t r = 0u;
317 r += _global_dof_idx.bytes();
318 r += _owned_mirror.bytes();
319 for(const auto& x : _owner_mirrors)
320 r += x.second.bytes();
321 for(const auto& x : _donee_mirrors)
322 r += x.second.bytes();
323 r += _all_global_dof_offset.size() * 4u;
324 r += _all_global_dof_counts.size() * 4u;
325 return r;
326 }
327
333 {
334 return this->_block_information;
335 }
336
339 {
340 return this->_owned_dof_count;
341 }
342
345 {
346 return this->_local_dof_count;
347 }
348
351 {
352 return this->_global_dof_count;
353 }
354
357 {
358 return this->_global_dof_offset;
359 }
360
364 const IndexVectorType& get_global_dof_indices() const
365 {
366 return this->_global_dof_idx;
367 }
368
372 const OwnedMirrorType& get_owned_mirror() const
373 {
374 return this->_owned_mirror;
375 }
376
379 {
380 return Index(this->_owner_mirrors.size());
381 }
382
385 {
386 return Index(this->_donee_mirrors.size());
387 }
388
399 {
400 return this->_owner_ranks.at(i);
401 }
402
413 {
414 return this->_donee_ranks.at(i);
415 }
416
430 {
431 return this->_owner_mirrors.at(i);
432 }
433
447 {
448 return this->_donee_mirrors.at(i);
449 }
450
460 const std::vector<int>& get_all_global_dof_offsets() const
461 {
462 XASSERTM(!this->_all_global_dof_offset.empty(), "You did not ask to assemble the global dof offsets");
463 return this->_all_global_dof_offset;
464 }
465
475 const std::vector<int>& get_all_global_dof_counts() const
476 {
477 XASSERTM(!this->_all_global_dof_counts.empty(), "You did not ask to assemble the global dof counts");
478 return this->_all_global_dof_counts;
479 }
480
493 void assemble_allgather(bool yes_i_really_want_to_do_this = false)
494 {
495 XASSERTM(yes_i_really_want_to_do_this, "You probably don't want to do this!");
496
497 // ensure that we don't have more than a billion DOFs
498 XASSERTM(this->get_num_global_dofs() < 1'000'000'000,
499 "allgather assembly not possible for more than a billion DOFs!");
500
501 std::size_t num_ranks = std::size_t(this->_comm->size());
502
503 int my_nod = int(this->get_num_owned_dofs());
504 int my_off = int(this->get_global_dof_offset());
505
506 // resize vectors
507 this->_all_global_dof_offset.resize(num_ranks, 0);
508 this->_all_global_dof_counts.resize(num_ranks, 0);
509
510 // allgather offsets + counts
511 this->_comm->allgather(&my_off, 1, this->_all_global_dof_offset.data(), 1);
512 this->_comm->allgather(&my_nod, 1, this->_all_global_dof_counts.data(), 1);
513 }
514
526 void assemble_by_gate(const GateType& gate, Index extra_local = 0u, Index extra_shared = 0u)
527 {
528 // set our communicator
529 this->_comm = gate.get_comm();
530 XASSERTM(this->_comm != nullptr, "need a communicator here!");
531
532 // get the communicator
533 const Dist::Comm& comm = *this->_comm;
534
535 // get my rank and neighbor ranks
536 const int my_rank = comm.rank();
537 const std::vector<int>& neighbor_ranks = gate._ranks;
538
539 // get a template vector
540 const LocalVectorType& vec_tmpl = gate.get_freqs();
541
542 // get number of total local DOFs on our patch
543 this->_local_dof_count = vec_tmpl.size_raw();
544 this->_extra_local_dof_count = extra_local;
545 this->_extra_shared_dof_count = extra_shared;
546 //this->_local_dof_count += extra_local + extra_shared;
547
548 // As a very first step, we have to decide which process will become the owner of each DOF.
549 // In this implementation, each DOF is owned by by the process with the lowest rank, which
550 // simplifies a lot of things.
551
552 // So, first create a vector, which stores the rank of the owner process of each of our
553 // local DOFs and format the vector to our rank, i.e. at startup assume that we own all
554 // local DOFs.
555 IndexVectorType dof_owners;
556 ADPAuxType::alloc_idx_vector(dof_owners, vec_tmpl, IndexType(my_rank));
557
558 // Now loop over all our neighbor processes
559 for(std::size_t i(0); i < neighbor_ranks.size(); ++i)
560 {
561 // Check whether the neighbors rank is less than our rank, as otherwise that particular
562 // neighbor can not own any of our local DOFs.
563 if(neighbor_ranks.at(i) < my_rank)
564 {
565 // this neighbor has a lower rank than our process, so update the dof owners vector and
566 // update the ownership of these dofs by setting this neighbor as their owner -- unless
567 // another neighbor with an even lower rank is already a candidate for ownership
568 ADPAuxType::update_owners(IndexType(neighbor_ranks.at(i)), dof_owners, gate.get_mirrors().at(i));
569 }
570 }
571
572 // Okay, at this point we (and all other processes) know the owners of each of our local DOFs.
573 // Now, we have to generate the mirror of all local DOF indices of the DOFs that we own:
574 this->_owned_dof_count = ADPAuxType::build_owned_mirror(IndexType(my_rank), this->_owned_mirror, dof_owners);
575
576 // Save current owned DOF count as extra DOF offset
577 this->_extra_dof_offset = this->_owned_dof_count;
578
579 // Now add the extra local DOFs for each process
580 this->_owned_dof_count += _extra_local_dof_count;
581
582 // The shared DOFs are always owned by the last process
583 const bool is_last_process = (this->_comm->rank()+1 == this->_comm->size());
584 if(is_last_process)
585 this->_owned_dof_count += _extra_shared_dof_count;
586
588 XASSERTM(this->_owned_dof_count > Index(0), "this process has no DOFs!");
589
590 // Next, we have to determine the global offset of our first owned DOF, which is easily
591 // obtained by an exclusive-scan over each processes owned DOF count:
592 comm.exscan(&this->_owned_dof_count, &this->_global_dof_offset, 1, Dist::op_sum);
593
594 // Furthermore, we also perform an allreduce to obtain the total number of global DOFs,
595 // which is generally helpful and can be used for sanity checks.
596 comm.allreduce(&this->_owned_dof_count, &this->_global_dof_count, 1, Dist::op_sum);
597
598 // Allocate an index vector that for each local DOF contains the "owned DOF index" if we
599 // own this particular DOF or ~Index(0) if this particular DOF is owned by another process:
600 IndexVectorType own_dof_idx;
601 ADPAuxType::alloc_idx_vector(own_dof_idx, vec_tmpl, ~IndexType(0));
602 ADPAuxType::build_owned_dofs(IndexType(my_rank), own_dof_idx, dof_owners, 0u);
603
604 // Now we also have to create the mirrors for each of our neighbor processes:
605 for(std::size_t i(0); i < neighbor_ranks.size(); ++i)
606 {
607 // get the neighbor rank and its mirror
608 const int neighbor_rank = neighbor_ranks.at(i);
609 const MirrorType& halo_mirror = gate.get_mirrors().at(i);
610
611 // There are two cases now:
612 // 1) The neighbor process has a lower rank, so it is a (potential) owner
613 // of at least one of our local DOFs.
614 // 2) The neighbor process has a greater rank, so we are an owner of at least
615 // one of the neighbor process's local DOFs.
616 if(neighbor_rank < my_rank)
617 {
618 // This is a potential "owner-neighbor", which may own one or several of our local DOFs.
619 // We call a helper function, which will create a vector of all *local* DOF indices,
620 // which are owned by that neighbor process. Note that it is perfectly legit if the
621 // vector is empty, as this simply means that all our local DOFs, which are shared with
622 // that particular neighbor, are owned by some *other* neighbor process(es) and not by
623 // the particular neighbor that we are currently considering.
624 OwnerMirrorType owner_mirror;
625 if(ADPAuxType::build_owner_mirror(IndexType(neighbor_rank), owner_mirror, halo_mirror, dof_owners) > Index(0))
626 {
627 // That neighbor owns at least one of our local DOFs, so create a mirror from the
628 // index-vector and push it into our list of "owner-neighbors".
629 // Note that the indices in the mirror are *local* DOF indices.
630 _owner_ranks.push_back(neighbor_rank);
631 _owner_mirrors.emplace_back(std::move(owner_mirror));
632 }
633 }
634 else
635 {
636 // This is a potential "donee-neighbor", which shares one or several of our local DOFs,
637 // which we might own. We call a helper function, which will create a vector of all our
638 // *owned* DOF indices, which are shared with that particular neighbor process.
639 // Note that it is perfectly legit if the vector is empty, as this simply means that
640 // all our local DOFs, which are shared with that particular neighbor, are owned by
641 // some *other* neighbor process(es) and not by this process (i.e. us).
642 Index num_donee_dofs = ADPAuxType::count_donee_dofs(halo_mirror, own_dof_idx);
643 if(num_donee_dofs > Index(0))
644 {
645 // allocate and build the actual donee mirror
646 DoneeMirrorType donee_mirror(this->_owned_dof_count, num_donee_dofs);
647 ADPAuxType::build_donee_mirror(donee_mirror, halo_mirror, own_dof_idx, 0u);
648
649 // We own at least of one of the neighbor's local DOFs, so create a mirror from the
650 // index-vector and push it into our list of "donee-neighbors".
651 // Note that the indices in the mirror are *owned* DOF indices.
652 _donee_ranks.push_back(neighbor_rank);
653 _donee_mirrors.emplace_back(std::move(donee_mirror));
654 }
655 }
656 }
657
658 // Finally, we have to determine the global DOF index for each of our local DOFs, so that
659 // we can perform a "local-to-global" DOF index lookup. This information is required for
660 // the assembly of matrices later on, so we have to store this in a member-variable vector.
661
662 // Let's create the vector of the required length, initialize all its indices to ~0, which
663 // can be used for a sanity check later on to ensure that we know the global indices of all
664 // our local DOFs:
665 ADPAuxType::alloc_idx_vector(this->_global_dof_idx, vec_tmpl, ~IndexType(0));
666
667 // Next, let's loop over all DOFs that we own and assign their corresponding global DOF
668 // indices, starting with our global DOF offset, which we have already determined before:
669 ADPAuxType::init_global_dof_idx(this->_global_dof_idx, this->_owned_mirror, this->_global_dof_offset);
670
671 // Finally, we also need to query the global DOF indices of all our local DOFs, which are
672 // owned by some neighbor process (i.e. we are the donee for these DOFs).
673 // Of course, we also have to send the global DOF indices of all DOFs that we own
674 // to all of our "donee-neighbors".
675
676 // Allocate index buffers and post receives for all of our owner-neighbors:
677 const Index num_neigh_owner = Index(_owner_mirrors.size());
678 Dist::RequestVector recv_reqs(num_neigh_owner);
679 std::vector<std::vector<IndexType>> recv_bufs(num_neigh_owner);
680 for(Index i(0); i < num_neigh_owner; ++i)
681 {
682 const std::size_t num_idx = _owner_mirrors.at(i).buffer_size(vec_tmpl);
683 recv_bufs.at(i).resize(num_idx, ~IndexType(0));
684 recv_reqs[i] = comm.irecv(recv_bufs.at(i).data(), num_idx, _owner_ranks.at(i));
685 }
686
687 // Allocate index buffers, fill them with the global DOF indices of our owned
688 // DOFs and send them to the donee-neighbors:
689 const Index num_neigh_donee = Index(_donee_mirrors.size());
690 Dist::RequestVector send_reqs(num_neigh_donee);
691 std::vector<std::vector<IndexType>> send_bufs(num_neigh_donee);
692 for(Index i(0); i < num_neigh_donee; ++i)
693 {
694 // Allocate buffer of required size and translate the indices of our mirror,
695 // which are "owned DOF indices", to global DOF indices, where:
696 // global_dof_index := global_dof_offset + owned_dof_index
697 const std::size_t num_idx = _donee_mirrors.at(i).num_indices();
698 send_bufs.at(i).resize(num_idx, ~IndexType(0));
699 _get_donee_dofs(send_bufs.at(i), _donee_mirrors.at(i), this->_global_dof_offset);
700 send_reqs[i] = comm.isend(send_bufs.at(i).data(), num_idx, _donee_ranks.at(i));
701 }
702
703 // Now process all receive requests from our owner-neighbors:
704 for(std::size_t i(0u); recv_reqs.wait_any(i); )
705 {
706 // Scatter the global DOF indices, which our friendly owner-neighbor has
707 // provided us with, into our global DOF index vector:
708 ADPAuxType::set_owner_dofs(this->_global_dof_idx, recv_bufs.at(i), _owner_mirrors.at(i), 0u);
709 }
710
711 // wait for all sends to finish
712 send_reqs.wait_all();
713
714 // get local counts
715 std::vector<Index> local_num;
716 ADPAuxType::get_local_dof_count(local_num, this->_global_dof_idx, this->_owned_mirror);
717
718 // compute global counts
719 std::vector<Index> global_num(local_num.size());
720 comm.allreduce(local_num.data(), global_num.data(), local_num.size(), Dist::op_sum);
721
722 // compute global offsets
723 std::vector<Index> global_first(local_num.size());
724 comm.exscan(local_num.data(), global_first.data(), local_num.size(), Dist::op_sum);
725
726 // build deques
727 std::deque<Index> loc_off, loc_num, glob_first, glob_num;
728 for(Index i(0), off(0); i < Index(local_num.size()); ++i)
729 {
730 loc_off.push_back(off);
731 loc_num.push_back(local_num[i]);
732 glob_num.push_back(global_num[i]);
733 glob_first.push_back(global_first[i]);
734 off += local_num[i];
735 }
736
737 // finally, build the block information
738 this->_block_information = ADPAuxType::build_block_info(this->_global_dof_idx, this->_global_dof_offset,
739 glob_first, glob_num, loc_off, loc_num);
740 }
741
742 template<typename DT2_>
743 void upload_vector(DT2_* owned_dofs, const LocalVectorType& local_vector,
744 const DataType* extra_local = nullptr, const DataType* extra_shared = nullptr) const
745 {
746 // gather owned DOFs
747 ADPAuxType::gather(owned_dofs, local_vector, this->_owned_mirror);
748
749 // gather extra local DOFs
750 if(this->_extra_local_dof_count > Index(0))
751 {
752 XASSERT(extra_local != nullptr);
753 DT2_* dst = &owned_dofs[this->_extra_dof_offset];
754 for(Index i = 0u; i < this->_extra_local_dof_count; ++i)
755 dst[i] = DT2_(extra_local[i]);
756 }
757
758 // gather extra shared DOFs on last process
759 if(this->_extra_shared_dof_count > Index(0))
760 {
761 XASSERT(extra_shared != nullptr);
762 if(this->_comm->rank() + 1 == this->_comm->size())
763 {
764 DT2_* dst = &owned_dofs[this->_extra_dof_offset + this->_extra_local_dof_count];
765 for(Index i = 0u; i < this->_extra_shared_dof_count; ++i)
766 dst[i] = DT2_(extra_shared[i]);
767 }
768 }
769 }
770
771 template<typename DT2_>
772 void download_vector(const DT2_* owned_dofs, LocalVectorType& local_vector,
773 DataType* extra_local = nullptr, DataType* extra_shared = nullptr) const
774 {
775 // ensure we have a communicator
776 XASSERT(this->_comm != nullptr);
777
778 // get the number of owner- and donee-neighbors
779 const std::size_t num_neigh_owner = this->_owner_mirrors.size();
780 const std::size_t num_neigh_donee = this->_donee_mirrors.size();
781
782 // The basic idea is simple:
783 // 1) Send the shared DOFs to all of our donee-neighbors
784 // 2) Receive the shared DOFs from all of our owner-neighbors
785 // 3) Scatter our own owned DOFs into our local vector
786 // 4) Scatter the dofs we received from our owner-neighbors
787
788 // Create receive buffers and post receives for all owner-neighbors
789 Dist::RequestVector recv_reqs(num_neigh_owner);
790 std::vector<BufferVectorType> recv_bufs(num_neigh_owner);
791 std::vector<Memory::TypedView<DataType>> recv_views(num_neigh_owner);
792 for(std::size_t i(0); i < num_neigh_owner; ++i)
793 {
794 // create a vector buffer
795 // note: owner mirrors relate to local DOF indices
796 recv_bufs.at(i) = this->_owner_mirrors.at(i).create_buffer(local_vector);
797 // get a write view
798 recv_views.at(i) = recv_bufs.at(i).elements_view_w();
799 // post receive from owner neighbor
800 recv_reqs[i] = this->_comm->irecv(recv_views.at(i).get_w(), recv_bufs.at(i).size(), this->_owner_ranks.at(i));
801 }
802
803 // Create send buffers, fill them with our owned DOFs and send this
804 // to our donee-neighbors
805 Dist::RequestVector send_reqs(num_neigh_donee);
806 std::vector<BufferVectorType> send_bufs(num_neigh_donee);
807 std::vector<Memory::TypedView<DataType>> send_views(num_neigh_donee);
808 for(Index i(0); i < num_neigh_donee; ++i)
809 {
810 // get mirror, create buffer and gather the shared DOFs
811 // note: donee mirrors relate to owned DOF indices
812 const DoneeMirrorType& mir = this->_donee_mirrors.at(i);
813 send_bufs.at(i) = BufferVectorType(mir.num_indices());
814 _gather(send_bufs.at(i), owned_dofs, mir);
815 // get a write view
816 send_views.at(i) = send_bufs.at(i).elements_view_r();
817 // post send to donee neighbor
818 send_reqs[i] = this->_comm->isend(send_views.at(i).get_r(), send_bufs.at(i).size(), this->_donee_ranks.at(i));
819 }
820
821 // format the output vector
822 local_vector.format();
823
824 // scatter our own owned DOFs into our output vector
825 ADPAuxType::scatter(owned_dofs, local_vector, this->_owned_mirror);
826
827 // process receives from our owner-neighbors
828 for(std::size_t idx(0u); recv_reqs.wait_any(idx); )
829 {
830 // scatter received DOFs into our output vector
831 ADPAuxType::scatter(recv_views.at(idx).get_w(), local_vector, this->_owner_mirrors.at(idx));
832 }
833
834 // at this point, all receives should have finished
835 XASSERT(recv_reqs.is_null());
836
837 // wait for all sends to finish
838 send_reqs.wait_all();
839
840 // copy extra local DOFs
841 if(this->_extra_local_dof_count > Index(0))
842 {
843 XASSERT(extra_local != nullptr);
844 const DT2_* src = &owned_dofs[this->_extra_dof_offset];
845 for(Index i = 0u; i < this->_extra_local_dof_count; ++i)
846 extra_local[i] = DataType(src[i]);
847 }
848
849 // scatter extra shared DOFs
850 if(this->_extra_shared_dof_count > Index(0))
851 {
852 XASSERT(extra_shared != nullptr);
853
854 // the last process owns the shared DOFs
855 if(this->_comm->rank() + 1 == this->_comm->size())
856 {
857 const DT2_* src = &owned_dofs[this->_extra_dof_offset + this->_extra_local_dof_count];
858 for(Index i = 0u; i < this->_extra_shared_dof_count; ++i)
859 extra_shared[i] = DataType(src[i]);
860 }
861
862 // broadcast shared DOFs
863 this->_comm->bcast(extra_shared, std::size_t(this->_extra_shared_dof_count), this->_comm->size() - 1);
864 }
865 }
866
867 protected:
868 static Index _get_donee_dofs(std::vector<IndexType>& send_buf,
869 const LAFEM::VectorMirror<DataType, IndexType>& donee_mir, Index offset)
870 {
871 const Index num_idx = donee_mir.num_indices();
872 const Memory::TypedView<IndexType> mir_idx = donee_mir.indices_view_r();
873 for(Index j(0); j < num_idx; ++j)
874 send_buf[j] = IndexType(offset) + mir_idx[j];
875
876 return num_idx;
877 }
878
879 template<typename DT2_>
880 static Index _gather(
881 LAFEM::DenseVector<DataType, IndexType>& buffer,
882 const DT2_* vector,
883 const LAFEM::VectorMirror<DataType, IndexType>& mirror)
884 {
885 const Index n = mirror.num_indices();
886 const Memory::TypedView<IndexType> idx = mirror.indices_view_r();
887 Memory::TypedView<DataType> buf = buffer.elements_view_w();
888 for(Index i = 0u; i < n; ++i)
889 buf[i] = DataType(vector[idx[i]]);
890 return n;
891 }
892 }; // class AlgDofParti<...>
893
899
901 namespace Intern
902 {
903 template<typename IT_>
904 class ADPAux<LAFEM::DenseVector<IT_, IT_>>
905 {
906 public:
907 template<typename DT_>
908 static Index get_local_dof_count(std::vector<Index>& v,
909 const LAFEM::DenseVector<IT_, IT_>& DOXY(global_dof_idx),
910 const LAFEM::VectorMirror<DT_, IT_>& owned_mirror)
911 {
912 v.push_back(owned_mirror.num_indices());
913 return v.back();
914 }
915
916 static String build_block_info(
917 const LAFEM::DenseVector<IT_, IT_>& DOXY(global_dof_idx), Index global_offset,
918 std::deque<Index>& global_first, std::deque<Index>& global_num,
919 std::deque<Index>& local_offset, std::deque<Index>& local_num)
920 {
921 String s = "<Scalar";
922 s += " gc=\"" + stringify(global_num.front()) + "\"";
923 s += " gf=\"" + stringify(global_first.front()) + "\"";
924 s += " go=\"" + stringify(global_offset + local_offset.front()) + "\"";
925 s += " lc=\"" + stringify(local_num.front()) + "\"";
926 s += " lo=\"" + stringify(local_offset.front()) + "\"";
927 s += "/>";
928 global_first.pop_front();
929 global_num.pop_front();
930 local_offset.pop_front();
931 local_num.pop_front();
932 return s;
933 }
934
935 template<typename DT_>
936 static void alloc_idx_vector(LAFEM::DenseVector<IT_, IT_>& idx_vec,
937 const LAFEM::DenseVector<DT_, IT_>& tmpl_vec, IT_ value)
938 {
939 idx_vec = LAFEM::DenseVector<IT_, IT_>(tmpl_vec.size(), value);
940 }
941
942 template<typename DT_>
943 static void update_owners(const IT_ neighbor_rank,
944 LAFEM::DenseVector<IT_, IT_>& dof_owners,
945 const LAFEM::VectorMirror<DT_, IT_>& mirror)
946 {
947 const Index num_indices = mirror.num_indices();
948 const Memory::TypedView<IT_> mir_idx = mirror.indices_view_r();
949 Memory::TypedView<IT_> dof_own = dof_owners.elements_view_rw();
950 for(Index j(0); j < num_indices; ++j)
951 {
952 IT_& dof_owner = dof_own[mir_idx[j]];
953 if(neighbor_rank < dof_owner)
954 dof_owner = neighbor_rank;
955 }
956 }
957
958 static Index build_owned_dofs(const IT_ my_rank,
959 LAFEM::DenseVector<IT_, IT_>& owned_dofs,
960 const LAFEM::DenseVector<IT_, IT_>& dof_owners, Index offset)
961 {
962 const Index n = dof_owners.size();
963 const Memory::TypedView<IT_> dof_own = dof_owners.elements_view_r();
964 Memory::TypedView<IT_> own_idx = owned_dofs.elements_view_w();
965
966 Index k = 0;
967 for(Index i = 0; i < n; ++i)
968 {
969 if(dof_own[i] == my_rank)
970 own_idx[i] = IT_(offset + k++);
971 }
972
973 return k;
974 }
975
976 template<typename DT_>
977 static Index build_owned_mirror(const IT_ owner_rank,
978 LAFEM::VectorMirror<DT_, IT_>& owner_mirror,
979 const LAFEM::DenseVector<IT_, IT_>& dof_owners)
980 {
981 // count the number of owned DOFs
982 const Index n = dof_owners.size();
983 const Memory::TypedView<IT_> dof_own = dof_owners.elements_view_r();
984 Index num_owned = 0u;
985 for(Index i = 0; i < n; ++i)
986 {
987 if(dof_own[i] == owner_rank)
988 ++num_owned;
989 }
990
991 // allocate mirror
992 owner_mirror = LAFEM::VectorMirror<DT_, IT_>(n, num_owned);
993 Memory::TypedView<IT_> mir_idx = owner_mirror.indices_view_w();
994
995 // store owned DOF indices
996 for(Index i = 0, k = 0; i < n; ++i)
997 {
998 if(dof_own[i] == owner_rank)
999 {
1000 mir_idx[k++] = IT_(i);
1001 }
1002 }
1003
1004 return num_owned;
1005 }
1006
1007 template<typename DT_>
1008 static Index build_owner_mirror(const IT_ neighbor_rank,
1009 LAFEM::VectorMirror<DT_, IT_>& owner_mirror,
1010 const LAFEM::VectorMirror<DT_, IT_>& halo_mirror,
1011 const LAFEM::DenseVector<IT_, IT_>& dof_owners)
1012 {
1013 // get halo mirror indices
1014 const Index n = halo_mirror.num_indices();
1015 const Memory::TypedView<IT_> halo_idx = halo_mirror.indices_view_r();
1016 const Memory::TypedView<IT_> dof_own = dof_owners.elements_view_r();
1017
1018 // count number of owner dofs
1019 Index num_owner = 0u;
1020 for(Index i = 0; i < n; ++i)
1021 {
1022 if(dof_own[halo_idx[i]] == neighbor_rank)
1023 ++num_owner;
1024 }
1025
1026 // allocate mirror
1027 owner_mirror = LAFEM::VectorMirror<DT_, IT_>(dof_owners.size(), num_owner);
1028
1029 if(num_owner <= Index(0))
1030 return Index(0);
1031
1032 // store owner DOF indices
1033 Memory::TypedView<IT_> own_idx = owner_mirror.indices_view_rw();
1034 for(IT_ i = 0, k = 0; i < n; ++i)
1035 {
1036 if(dof_own[halo_idx[i]] == neighbor_rank)
1037 {
1038 own_idx[k++] = halo_idx[i];
1039 }
1040 }
1041
1042 return num_owner;
1043 }
1044
1045 template<typename DT_>
1046 static Index count_donee_dofs(
1047 const LAFEM::VectorMirror<DT_, IT_>& halo_mirror,
1048 const LAFEM::DenseVector<IT_, IT_>& own_dof_idx)
1049 {
1050 // get halo mirror indices
1051 const Index n = halo_mirror.num_indices();
1052 const Memory::TypedView<IT_> halo_idx = halo_mirror.indices_view_r();
1053 const Memory::TypedView<IT_> own_idx = own_dof_idx.elements_view_r();
1054
1055 // count number of owner dofs
1056 Index num_donee = 0u;
1057 for(Index i = 0; i < n; ++i)
1058 {
1059 if(own_idx[halo_idx[i]] != ~IT_(0))
1060 ++num_donee;
1061 }
1062
1063 return num_donee;
1064 }
1065
1066 template<typename DT_>
1067 static Index build_donee_mirror(
1068 LAFEM::VectorMirror<DT_, IT_>& donee_mirror,
1069 const LAFEM::VectorMirror<DT_, IT_>& halo_mirror,
1070 const LAFEM::DenseVector<IT_, IT_>& own_dof_idx, Index offset)
1071 {
1072 // get halo mirror indices
1073 const Index n = halo_mirror.num_indices();
1074 const Memory::TypedView<IT_> halo_idx = halo_mirror.indices_view_r();
1075 const Memory::TypedView<IT_> own_idx = own_dof_idx.elements_view_r();
1076 Memory::TypedView<IT_> donee_idx = donee_mirror.indices_view_rw();
1077
1078 // store donee DOF indices
1079 Index k = 0u;
1080 for(Index i = 0; i < n; ++i)
1081 {
1082 if(own_idx[halo_idx[i]] != ~IT_(0))
1083 {
1084 donee_idx[offset + k++] = own_idx[halo_idx[i]];
1085 }
1086 }
1087
1088 return k;
1089 }
1090
1091 template<typename DT_>
1092 static Index init_global_dof_idx(
1093 LAFEM::DenseVector<IT_, IT_>& global_dof_idx,
1094 const LAFEM::VectorMirror<DT_, IT_>& owned_mirror, Index offset)
1095 {
1096 Memory::TypedView<IT_> g_dof_idx = global_dof_idx.elements_view_rw();
1097 const Memory::TypedView<IT_> own_idx = owned_mirror.indices_view_r();
1098 const Index n = owned_mirror.num_indices();
1099 for(Index i = 0; i < n; ++i)
1100 g_dof_idx[own_idx[i]] = IT_(offset + i);
1101 return n;
1102 }
1103
1104 template<typename DT_>
1105 static Index set_owner_dofs(
1106 LAFEM::DenseVector<IT_, IT_>& global_dof_idx,
1107 const std::vector<IT_>& recv_buf,
1108 const LAFEM::VectorMirror<DT_, IT_>& mirror, Index offset)
1109 {
1110 const Index num_indices = mirror.num_indices();
1111 const Memory::TypedView<IT_> mir_idx = mirror.indices_view_r();
1112 Memory::TypedView<IT_> g_dof_idx = global_dof_idx.elements_view_rw();
1113 for(Index j(0); j < num_indices; ++j)
1114 g_dof_idx[mir_idx[j]] = recv_buf[offset + j];
1115 return num_indices;
1116 }
1117
1118 template<typename DT_, typename DT2_>
1119 static Index gather(DT2_* buf,
1120 const LAFEM::DenseVector<DT_, IT_>& vector,
1121 const LAFEM::VectorMirror<DT_, IT_>& mirror)
1122 {
1123 XASSERT(mirror.size() == vector.size());
1124 const Index n = mirror.num_indices();
1125 const Memory::TypedView<IT_> idx = mirror.indices_view_r();
1126 const Memory::TypedView<DT_> val = vector.elements_view_r();
1127 for(Index i = 0u; i < n; ++i)
1128 buf[i] = DT2_(val[idx[i]]);
1129 return n;
1130 }
1131
1132 template<typename DT_, typename DT2_>
1133 static Index scatter(const DT2_* buf,
1134 LAFEM::DenseVector<DT_, IT_>& vector,
1135 const LAFEM::VectorMirror<DT_, IT_>& mirror)
1136 {
1137 XASSERT(mirror.size() == vector.size());
1138 const Index n = mirror.num_indices();
1139 const Memory::TypedView<IT_> idx = mirror.indices_view_r();
1140 Memory::TypedView<DT_> val = vector.elements_view_rw();
1141 for(Index i = 0u; i < n; ++i)
1142 val[idx[i]] = DT_(buf[i]);
1143 return n;
1144 }
1145 }; // class ADPAux<LAFEM::DenseVector<IT_, IT_>>
1146
1148
1149 template<typename IT_, int bs_>
1150 class ADPAux<LAFEM::DenseVectorBlocked<IT_, IT_, bs_>>
1151 {
1152 public:
1153 template<typename DT_>
1154 static Index get_local_dof_count(std::vector<Index>& v,
1155 const LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& DOXY(global_dof_idx),
1156 const LAFEM::VectorMirror<DT_, IT_>& owned_mirror)
1157 {
1158 v.push_back(owned_mirror.num_indices() * Index(bs_));
1159 return v.back();
1160 }
1161
1162 static String build_block_info(
1163 const LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& DOXY(global_dof_idx), Index global_offset,
1164 std::deque<Index>& global_first, std::deque<Index>& global_num,
1165 std::deque<Index>& local_offset, std::deque<Index>& local_num)
1166 {
1167 String s = "<Blocked";
1168 s += " bs=\"" + stringify(bs_) + "\"";
1169 s += " gc=\"" + stringify(global_num.front()) + "\"";
1170 s += " gf=\"" + stringify(global_first.front()) + "\"";
1171 s += " go=\"" + stringify(global_offset + local_offset.front()) + "\"";
1172 s += " lc=\"" + stringify(local_num.front()) + "\"";
1173 s += " lo=\"" + stringify(local_offset.front()) + "\"";
1174 s += "/>";
1175 global_first.pop_front();
1176 global_num.pop_front();
1177 local_offset.pop_front();
1178 local_num.pop_front();
1179 return s;
1180 }
1181
1182 template<typename DT_>
1183 static void alloc_idx_vector(LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& idx_vec,
1184 const LAFEM::DenseVectorBlocked<DT_, IT_, bs_>& tmpl_vec, IT_ value)
1185 {
1186 idx_vec = LAFEM::DenseVectorBlocked<IT_, IT_, bs_>(tmpl_vec.size(), value);
1187 }
1188
1189 template<typename DT_>
1190 static void update_owners(const IT_ neighbor_rank,
1191 LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& dof_owners,
1192 const LAFEM::VectorMirror<DT_, IT_>& mirror)
1193 {
1194 const Index num_indices = mirror.num_indices();
1195 const Memory::TypedView<IT_> mir_idx = mirror.indices_view_r();
1196 Memory::TypedView<Tiny::Vector<IT_, bs_>> dof_own = dof_owners.elements_view_rw();
1197 for(Index j(0); j < num_indices; ++j)
1198 {
1199 auto& dof_owner = dof_own[mir_idx[j]];
1200 if(neighbor_rank < dof_owner[0])
1201 {
1202 for(int k = 0; k < bs_; ++k)
1203 dof_owner[k] = neighbor_rank;
1204 }
1205 }
1206 }
1207
1208 static Index build_owned_dofs(const IT_ my_rank,
1209 LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& owned_dofs,
1210 const LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& dof_owners, Index offset)
1211 {
1212 const Index n = dof_owners.size();
1213 const Memory::TypedView<Tiny::Vector<IT_, bs_>> dof_own = dof_owners.elements_view_r();
1214 Memory::TypedView<Tiny::Vector<IT_, bs_>> own_idx = owned_dofs.elements_view_w();
1215
1216 Index k = 0;
1217 for(Index i = 0; i < n; ++i)
1218 {
1219 if(dof_own[i][0] == my_rank)
1220 {
1221 for(int j = 0; j < bs_; ++j)
1222 own_idx[i][j] = IT_(offset + k++);
1223 }
1224 }
1225
1226 return k;
1227 }
1228
1229 template<typename DT_>
1230 static Index build_owned_mirror(const IT_ owner_rank,
1231 LAFEM::VectorMirror<DT_, IT_>& owner_mirror,
1232 const LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& dof_owners)
1233 {
1234 // count the number of owned DOFs
1235 const Index n = dof_owners.size();
1236 const Memory::TypedView<Tiny::Vector<IT_, bs_>> dof_own = dof_owners.elements_view_r();
1237 Index num_owned = 0u;
1238 for(Index i = 0; i < n; ++i)
1239 {
1240 if(dof_own[i][0] == owner_rank)
1241 ++num_owned;
1242 }
1243
1244 // allocate mirror
1245 owner_mirror = LAFEM::VectorMirror<DT_, IT_>(n, num_owned);
1246 Memory::TypedView<IT_> mir_idx = owner_mirror.indices_view_w();
1247
1248 // store owned DOF indices
1249 for(Index i = 0, k = 0; i < n; ++i)
1250 {
1251 if(dof_own[i][0] == owner_rank)
1252 {
1253 mir_idx[k++] = IT_(i);
1254 }
1255 }
1256
1257 return num_owned * Index(bs_);
1258 }
1259
1260 template<typename DT_>
1261 static Index build_owner_mirror(const IT_ neighbor_rank,
1262 LAFEM::VectorMirror<DT_, IT_>& owner_mirror,
1263 const LAFEM::VectorMirror<DT_, IT_>& halo_mirror,
1264 const LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& dof_owners)
1265 {
1266 // get halo mirror indices
1267 const Index n = halo_mirror.num_indices();
1268 const Memory::TypedView<IT_> halo_idx = halo_mirror.indices_view_r();
1269 const Memory::TypedView<Tiny::Vector<IT_, bs_>> dof_own = dof_owners.elements_view_r();
1270
1271 // count number of owner dofs
1272 Index num_owner = 0u;
1273 for(Index i = 0; i < n; ++i)
1274 {
1275 if(dof_own[halo_idx[i]][0] == neighbor_rank)
1276 ++num_owner;
1277 }
1278
1279 // allocate mirror
1280 owner_mirror = LAFEM::VectorMirror<DT_, IT_>(dof_owners.size(), num_owner);
1281
1282 if(num_owner <= Index(0))
1283 return Index(0);
1284
1285 // store owner DOF indices
1286 Memory::TypedView<IT_> own_idx = owner_mirror.indices_view_w();
1287 for(IT_ i = 0, k = 0; i < n; ++i)
1288 {
1289 if(dof_own[halo_idx[i]][0] == neighbor_rank)
1290 {
1291 own_idx[k++] = halo_idx[i];
1292 }
1293 }
1294
1295 return num_owner * Index(bs_);
1296 }
1297
1298 template<typename DT_>
1299 static Index count_donee_dofs(
1300 const LAFEM::VectorMirror<DT_, IT_>& halo_mirror,
1301 const LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& own_dof_idx)
1302 {
1303 // get halo mirror indices
1304 const Index n = halo_mirror.num_indices();
1305 const Memory::TypedView<IT_> halo_idx = halo_mirror.indices_view_r();
1306 const Memory::TypedView<Tiny::Vector<IT_, bs_>> own_idx = own_dof_idx.elements_view_r();
1307
1308 // count number of owner dofs
1309 Index num_donee = 0u;
1310 for(Index i = 0; i < n; ++i)
1311 {
1312 if(own_idx[halo_idx[i]][0] != ~IT_(0))
1313 num_donee += Index(bs_);
1314 }
1315
1316 return num_donee;
1317 }
1318
1319 template<typename DT_>
1320 static Index build_donee_mirror(
1321 LAFEM::VectorMirror<DT_, IT_>& donee_mirror,
1322 const LAFEM::VectorMirror<DT_, IT_>& halo_mirror,
1323 const LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& own_dof_idx, Index offset)
1324 {
1325 // get halo mirror indices
1326 const Index n = halo_mirror.num_indices();
1327 const Memory::TypedView<IT_> halo_idx = halo_mirror.indices_view_r();
1328 const Memory::TypedView<Tiny::Vector<IT_, bs_>> own_idx = own_dof_idx.elements_view_r();
1329 Memory::TypedView<IT_> donee_idx = donee_mirror.indices_view_w();
1330
1331 // store donee DOF indices
1332 Index k = 0u;
1333 for(Index i = 0; i < n; ++i)
1334 {
1335 if(own_idx[halo_idx[i]][0] != ~IT_(0))
1336 {
1337 for(int j = 0; j < bs_; ++j)
1338 donee_idx[offset + k++] = own_idx[halo_idx[i]][j];
1339 }
1340 }
1341
1342 return k;
1343 }
1344
1345 template<typename DT_>
1346 static Index init_global_dof_idx(
1347 LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& global_dof_idx,
1348 const LAFEM::VectorMirror<DT_, IT_>& owned_mirror, Index offset)
1349 {
1350 Memory::TypedView<Tiny::Vector<IT_, bs_>> g_dof_idx = global_dof_idx.elements_view_w();
1351 const Memory::TypedView<IT_> own_idx = owned_mirror.indices_view_r();
1352 const Index n = owned_mirror.num_indices();
1353 for(Index i = 0; i < n; ++i)
1354 {
1355 for(int j = 0; j < bs_; ++j)
1356 g_dof_idx[own_idx[i]][j] = IT_(offset + i*Index(bs_) + Index(j));
1357 }
1358 return n * Index(bs_);
1359 }
1360
1361 template<typename DT_>
1362 static Index set_owner_dofs(
1363 LAFEM::DenseVectorBlocked<IT_, IT_, bs_>& glob_dof_idx,
1364 const std::vector<IT_>& recv_buf,
1365 const LAFEM::VectorMirror<DT_, IT_>& mirror, Index offset)
1366 {
1367 const Index num_indices = mirror.num_indices();
1368 const Memory::TypedView<IT_> mir_idx = mirror.indices_view_r();
1369 Memory::TypedView<Tiny::Vector<IT_, bs_>> g_dof_idx = glob_dof_idx.elements_view_w();
1370 for(Index i(0); i < num_indices; ++i)
1371 {
1372 for(int j = 0; j < bs_; ++j)
1373 g_dof_idx[mir_idx[i]][j] = recv_buf[offset + i*IT_(bs_) + IT_(j)];
1374 }
1375 return num_indices * Index(bs_);
1376 }
1377
1378 template<typename DT_, typename DT2_>
1379 static Index gather(DT2_* buf,
1380 const LAFEM::DenseVectorBlocked<DT_, IT_, bs_>& vector,
1381 const LAFEM::VectorMirror<DT_, IT_>& mirror)
1382 {
1383 XASSERT(mirror.size() == vector.size());
1384 const Index n = mirror.num_indices();
1385 const Memory::TypedView<IT_> idx = mirror.indices_view_r();
1386 const Memory::TypedView<Tiny::Vector<DT_, bs_>> val = vector.elements_view_r();
1387 for(Index i = 0u, k = 0u; i < n; ++i)
1388 {
1389 for(int j = 0; j < bs_; ++j, ++k)
1390 buf[k] = DT2_(val[idx[i]][j]);
1391 }
1392 return n * Index(bs_);
1393 }
1394
1395 template<typename DT_, typename DT2_>
1396 static Index scatter(const DT2_* buf,
1397 LAFEM::DenseVectorBlocked<DT_, IT_, bs_>& vector,
1398 const LAFEM::VectorMirror<DT_, IT_>& mirror)
1399 {
1400 XASSERT(mirror.size() == vector.size());
1401 const Index n = mirror.num_indices();
1402 const Memory::TypedView<IT_> idx = mirror.indices_view_r();
1403 Memory::TypedView<Tiny::Vector<DT_, bs_>> val = vector.elements_view_w();
1404 for(Index i = 0u, k = 0u; i < n; ++i)
1405 {
1406 for(int j = 0; j < bs_; ++j, ++k)
1407 val[idx[i]][j] = DT_(buf[k]);
1408 }
1409 return n * Index(bs_);
1410 }
1411 }; // class ADPAux<LAFEM::DenseVectorBlocked<IT_, IT_, bs_>>
1412
1414
1415 template<typename FirstIdxVec_, typename... RestIdxVec_>
1416 class ADPAux<LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>>
1417 {
1418 public:
1419 typedef typename FirstIdxVec_::IndexType IT_;
1420 typedef ADPAux<FirstIdxVec_> ADPAuxFirst;
1421 typedef ADPAux<LAFEM::TupleVector<RestIdxVec_...>> ADPAuxRest;
1422
1423 template<typename FirstMirror_, typename... RestMirror_>
1424 static Index get_local_dof_count_raw(std::vector<Index>& v,
1425 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& global_dof_idx,
1426 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& owned_mirror)
1427 {
1428 Index n1 = ADPAuxFirst::get_local_dof_count(v, global_dof_idx.first(), owned_mirror.first());
1429 Index n2 = ADPAuxRest::get_local_dof_count_raw(v, global_dof_idx.rest(), owned_mirror.rest());
1430 return n1 + n2;
1431 }
1432
1433 template<
1434 typename FirstMirror_, typename... RestMirror_>
1435 static Index get_local_dof_count(std::vector<Index>& v,
1436 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& global_dof_idx,
1437 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& owned_mirror)
1438 {
1439 v.push_back(get_local_dof_count_raw(v, global_dof_idx, owned_mirror));
1440 return v.back();
1441 }
1442
1443 static String build_block_info_raw(
1444 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& global_dof_idx, Index global_offset,
1445 std::deque<Index>& global_first, std::deque<Index>& global_num,
1446 std::deque<Index>& local_offset, std::deque<Index>& local_num)
1447 {
1448 String s = ADPAuxFirst::build_block_info(global_dof_idx.first(), global_offset, global_first, global_num, local_offset, local_num);
1449 s += "\n";
1450 s += ADPAuxRest::build_block_info_raw(global_dof_idx.rest(), global_offset, global_first, global_num, local_offset, local_num);
1451 return s;
1452 }
1453
1454 static String build_block_info(
1455 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& global_dof_idx, Index global_offset,
1456 std::deque<Index>& global_first, std::deque<Index>& global_num,
1457 std::deque<Index>& local_offset, std::deque<Index>& local_num)
1458 {
1459 // offsets need to be processed BEFORE the call to '_build_block_info_raw' because
1460 // the offsets of the tuple coincide with the offsets of the first tuple entry
1461 String slo = stringify(local_offset.front());
1462 String sgo = stringify(global_offset + local_offset.front());
1463 String sraw = build_block_info_raw(global_dof_idx, global_offset, global_first, global_num, local_offset, local_num);
1464 String s = "<Tuple";
1465 // counts and first need to be processed AFTER the call to '_build_block_info_raw' because
1466 // these correspond to the sums of the corresponding values of all the tuple entries
1467 s += " gc=\"" + stringify(global_num.front()) + "\"";
1468 s += " gf=\"" + stringify(global_first.front()) + "\"";
1469 s += " go=\"" + sgo + "\"";
1470 s += " lc=\"" + stringify(local_num.front()) + "\"";
1471 s += " lo=\"" + slo + "\">\n";
1472 s += sraw;
1473 s += "\n</Tuple>";
1474 global_first.pop_front();
1475 global_num.pop_front();
1476 local_offset.pop_front();
1477 local_num.pop_front();
1478 return s;
1479 }
1480
1481 template<typename FirstTmplVector_, typename... RestTmplVector_>
1482 static void alloc_idx_vector(LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& idx_vec,
1483 const LAFEM::TupleVector<FirstTmplVector_, RestTmplVector_...>& tmpl_vec, IT_ value)
1484 {
1485 ADPAuxFirst::alloc_idx_vector(idx_vec.first(), tmpl_vec.first(), value);
1486 ADPAuxRest::alloc_idx_vector(idx_vec.rest(), tmpl_vec.rest(), value);
1487 }
1488
1489 template<typename FirstMirror_, typename... RestMirror_>
1490 static void update_owners(const IT_ neighbor_rank,
1491 LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& dof_owners,
1492 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& mirror)
1493 {
1494 ADPAuxFirst::update_owners(neighbor_rank, dof_owners.first(), mirror.first());
1495 ADPAuxRest::update_owners(neighbor_rank, dof_owners.rest(), mirror.rest());
1496 }
1497
1498 static Index build_owned_dofs(const IT_ my_rank,
1499 LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& owned_dofs,
1500 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& dof_owners, Index offset)
1501 {
1502 Index n1 = ADPAuxFirst::build_owned_dofs(my_rank, owned_dofs.first(), dof_owners.first(), offset);
1503 Index n2 = ADPAuxRest::build_owned_dofs(my_rank, owned_dofs.rest(), dof_owners.rest(), offset + n1);
1504 return n1 + n2;
1505 }
1506
1507 template<typename FirstMirror_, typename... RestMirror_>
1508 static Index build_owned_mirror(const IT_ owner_rank,
1509 LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& owner_mirror,
1510 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& dof_owners)
1511 {
1512 Index n1 = ADPAuxFirst::build_owned_mirror(owner_rank, owner_mirror.first(), dof_owners.first());
1513 Index n2 = ADPAuxRest::build_owned_mirror(owner_rank, owner_mirror.rest(), dof_owners.rest());
1514 return n1 + n2;
1515 }
1516
1517 template<typename FirstMirror_, typename... RestMirror_>
1518 static Index build_owner_mirror(const IT_ neighbor_rank,
1519 LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& owner_mirror,
1520 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& halo_mirror,
1521 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& dof_owners)
1522 {
1523 Index n1 = ADPAuxFirst::build_owner_mirror(neighbor_rank, owner_mirror.first(), halo_mirror.first(), dof_owners.first());
1524 Index n2 = ADPAuxRest::build_owner_mirror(neighbor_rank, owner_mirror.rest(), halo_mirror.rest(), dof_owners.rest());
1525 return n1 + n2;
1526 }
1527
1528 template<typename FirstMirror_, typename... RestMirror_>
1529 static Index count_donee_dofs(
1530 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& halo_mirror,
1531 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& own_dof_idx)
1532 {
1533 Index n1 = ADPAuxFirst::count_donee_dofs(halo_mirror.first(), own_dof_idx.first());
1534 Index n2 = ADPAuxRest::count_donee_dofs(halo_mirror.rest(), own_dof_idx.rest());
1535 return n1 + n2;
1536 }
1537
1538 template<typename DT_, typename FirstMirror_, typename... RestMirror_>
1539 static Index build_donee_mirror(
1540 LAFEM::VectorMirror<DT_, IT_>& donee_mirror,
1541 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& halo_mirror,
1542 const LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& own_dof_idx, Index offset)
1543 {
1544 Index n1 = ADPAuxFirst::build_donee_mirror(donee_mirror, halo_mirror.first(), own_dof_idx.first(), offset);
1545 Index n2 = ADPAuxRest::build_donee_mirror(donee_mirror, halo_mirror.rest(), own_dof_idx.rest(), offset + n1);
1546 return n1 + n2;
1547 }
1548
1549 template<typename FirstMirror_, typename... RestMirror_>
1550 static Index init_global_dof_idx(
1551 LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& global_dof_idx,
1552 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& owned_mirror, Index offset)
1553 {
1554 Index n1 = ADPAuxFirst::init_global_dof_idx(global_dof_idx.first(), owned_mirror.first(), offset);
1555 Index n2 = ADPAuxRest::init_global_dof_idx(global_dof_idx.rest(), owned_mirror.rest(), offset + n1);
1556 return n1 + n2;
1557 }
1558
1559 template<typename FirstMirror_, typename... RestMirror_>
1560 static Index set_owner_dofs(
1561 LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>& glob_dof_idx,
1562 const std::vector<IT_>& recv_buf,
1563 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& mirror, Index offset)
1564 {
1565 Index n1 = ADPAuxFirst::set_owner_dofs(glob_dof_idx.first(), recv_buf, mirror.first(), offset);
1566 Index n2 = ADPAuxRest::set_owner_dofs(glob_dof_idx.rest(), recv_buf, mirror.rest(), offset + n1);
1567 return n1 + n2;
1568 }
1569
1570 template<typename DT2_,
1571 typename FirstVector_, typename... RestVector_,
1572 typename FirstMirror_, typename... RestMirror_>
1573 static Index gather(DT2_* buf,
1574 const LAFEM::TupleVector<FirstVector_, RestVector_...>& vector,
1575 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& mirror)
1576 {
1577 Index n1 = ADPAuxFirst::gather(buf, vector.first(), mirror.first());
1578 Index n2 = ADPAuxRest::gather(&buf[n1], vector.rest(), mirror.rest());
1579 return n1 + n2;
1580 }
1581
1582 template<typename DT2_,
1583 typename FirstVector_, typename... RestVector_,
1584 typename FirstMirror_, typename... RestMirror_>
1585 static Index scatter(const DT2_* buf,
1586 LAFEM::TupleVector<FirstVector_, RestVector_...>& vector,
1587 const LAFEM::TupleMirror<FirstMirror_, RestMirror_...>& mirror)
1588 {
1589 Index n1 = ADPAuxFirst::scatter(buf, vector.first(), mirror.first());
1590 Index n2 = ADPAuxRest::scatter(&buf[n1], vector.rest(), mirror.rest());
1591 return n1 + n2;
1592 }
1593 }; // class ADPAux<LAFEM::TupleVector<FirstIdxVec_, RestIdxVec_...>>
1594
1596
1597 template<typename FirstIdxVec_>
1598 class ADPAux<LAFEM::TupleVector<FirstIdxVec_>>
1599 {
1600 public:
1601 typedef typename FirstIdxVec_::IndexType IT_;
1602 typedef ADPAux<FirstIdxVec_> ADPAuxFirst;
1603
1604 template<typename FirstMirror_>
1605 static Index get_local_dof_count_raw(std::vector<Index>& v,
1606 const LAFEM::TupleVector<FirstIdxVec_>& global_dof_idx,
1607 const LAFEM::TupleMirror<FirstMirror_>& owned_mirror)
1608 {
1609 return ADPAuxFirst::get_local_dof_count(v, global_dof_idx.first(), owned_mirror.first());
1610 }
1611
1612 template<typename FirstMirror_>
1613 static Index get_local_dof_count(std::vector<Index>& v,
1614 const LAFEM::TupleVector<FirstIdxVec_>& global_dof_idx,
1615 const LAFEM::TupleMirror<FirstMirror_>& owned_mirror)
1616 {
1617 v.push_back(get_local_dof_count_raw(v, global_dof_idx, owned_mirror));
1618 return v.back();
1619 }
1620
1621 static String build_block_info_raw(
1622 const LAFEM::TupleVector<FirstIdxVec_>& global_dof_idx, Index global_offset,
1623 std::deque<Index>& global_first, std::deque<Index>& global_num,
1624 std::deque<Index>& local_offset, std::deque<Index>& local_num)
1625 {
1626 return ADPAuxFirst::build_block_info(global_dof_idx.first(), global_offset, global_first, global_num, local_offset, local_num);
1627 }
1628
1629 static String build_block_info(
1630 const LAFEM::TupleVector<FirstIdxVec_>& global_dof_idx, Index global_offset,
1631 std::deque<Index>& global_first, std::deque<Index>& global_num,
1632 std::deque<Index>& local_offset, std::deque<Index>& local_num)
1633 {
1634 // offsets need to be processed BEFORE the call to '_build_block_info_raw' because
1635 // the offsets of the tuple coincide with the offsets of the first tuple entry
1636 String slo = stringify(local_offset.front());
1637 String sgo = stringify(global_offset + local_offset.front());
1638 String sraw = build_block_info_raw(global_dof_idx, global_offset, global_first, global_num, local_offset, local_num);
1639 String s = "<Tuple";
1640 // counts and first need to be processed AFTER the call to '_build_block_info_raw' because
1641 // these correspond to the sums of the corresponding values of all the tuple entries
1642 s += " gc=\"" + stringify(global_num.front()) + "\"";
1643 s += " gf=\"" + stringify(global_first.front()) + "\"";
1644 s += " go=\"" + sgo + "\"";
1645 s += " lc=\"" + stringify(local_num.front()) + "\"";
1646 s += " lo=\"" + slo + "\">\n";
1647 s += sraw;
1648 s += "\n</Tuple>";
1649 global_first.pop_front();
1650 global_num.pop_front();
1651 local_offset.pop_front();
1652 local_num.pop_front();
1653 return s;
1654 }
1655
1656 template<typename FirstTmplVector_>
1657 static void alloc_idx_vector(LAFEM::TupleVector<FirstIdxVec_>& idx_vec,
1658 const LAFEM::TupleVector<FirstTmplVector_>& tmpl_vec, IT_ value)
1659 {
1660 ADPAuxFirst::alloc_idx_vector(idx_vec.first(), tmpl_vec.first(), value);
1661 }
1662
1663 template<typename FirstMirror_>
1664 static void update_owners(const IT_ neighbor_rank,
1665 LAFEM::TupleVector<FirstIdxVec_>& dof_owners,
1666 const LAFEM::TupleMirror<FirstMirror_>& mirror)
1667 {
1668 ADPAuxFirst::update_owners(neighbor_rank, dof_owners.first(), mirror.first());
1669 }
1670
1671 static Index build_owned_dofs(const IT_ my_rank,
1672 LAFEM::TupleVector<FirstIdxVec_>& owned_dofs,
1673 const LAFEM::TupleVector<FirstIdxVec_>& dof_owners, Index offset)
1674 {
1675 return ADPAuxFirst::build_owned_dofs(my_rank, owned_dofs.first(), dof_owners.first(), offset);
1676 }
1677
1678 template<typename FirstMirror_>
1679 static Index build_owned_mirror(const IT_ owner_rank,
1680 LAFEM::TupleMirror<FirstMirror_>& owner_mirror,
1681 const LAFEM::TupleVector<FirstIdxVec_>& dof_owners)
1682 {
1683 return ADPAuxFirst::build_owned_mirror(owner_rank, owner_mirror.first(), dof_owners.first());
1684 }
1685
1686 template<typename FirstMirror_>
1687 static Index build_owner_mirror(const IT_ neighbor_rank,
1688 LAFEM::TupleMirror<FirstMirror_>& owner_mirror,
1689 const LAFEM::TupleMirror<FirstMirror_>& halo_mirror,
1690 const LAFEM::TupleVector<FirstIdxVec_>& dof_owners)
1691 {
1692 return ADPAuxFirst::build_owner_mirror(neighbor_rank, owner_mirror.first(), halo_mirror.first(), dof_owners.first());
1693 }
1694
1695 template<typename FirstMirror_>
1696 static Index count_donee_dofs(
1697 const LAFEM::TupleMirror<FirstMirror_>& halo_mirror,
1698 const LAFEM::TupleVector<FirstIdxVec_>& own_dof_idx)
1699 {
1700 return ADPAuxFirst::count_donee_dofs(halo_mirror.first(), own_dof_idx.first());
1701 }
1702
1703 template<typename DT_, typename FirstMirror_>
1704 static Index build_donee_mirror(
1705 LAFEM::VectorMirror<DT_, IT_>& donee_mirror,
1706 const LAFEM::TupleMirror<FirstMirror_>& halo_mirror,
1707 const LAFEM::TupleVector<FirstIdxVec_>& own_dof_idx, Index offset)
1708 {
1709 return ADPAuxFirst::build_donee_mirror(donee_mirror, halo_mirror.first(), own_dof_idx.first(), offset);
1710 }
1711
1712 template<typename FirstMirror_>
1713 static Index init_global_dof_idx(
1714 LAFEM::TupleVector<FirstIdxVec_>& global_dof_idx,
1715 const LAFEM::TupleMirror<FirstMirror_>& owned_mirror, Index offset)
1716 {
1717 return ADPAuxFirst::init_global_dof_idx(global_dof_idx.first(), owned_mirror.first(), offset);
1718 }
1719
1720 template<typename FirstMirror_>
1721 static Index set_owner_dofs(
1722 LAFEM::TupleVector<FirstIdxVec_>& glob_dof_idx,
1723 const std::vector<IT_>& recv_buf,
1724 const LAFEM::TupleMirror<FirstMirror_>& mirror, Index offset)
1725 {
1726 return ADPAuxFirst::set_owner_dofs(glob_dof_idx.first(), recv_buf, mirror.first(), offset);
1727 }
1728
1729 template<typename DT2_, typename FirstVector_, typename FirstMirror_>
1730 static Index gather(DT2_* buf,
1731 const LAFEM::TupleVector<FirstVector_>& vector,
1732 const LAFEM::TupleMirror<FirstMirror_>& mirror)
1733 {
1734 return ADPAuxFirst::gather(buf, vector.first(), mirror.first());
1735 }
1736
1737 template<typename DT2_, typename FirstVector_, typename FirstMirror_>
1738 static Index scatter(const DT2_* buf,
1739 LAFEM::TupleVector<FirstVector_>& vector,
1740 const LAFEM::TupleMirror<FirstMirror_>& mirror)
1741 {
1742 return ADPAuxFirst::scatter(buf, vector.first(), mirror.first());
1743 }
1744 }; // class ADPAux<LAFEM::TupleVector<FirstIdxVec_>>
1745 } // namespace Intern
1747 } // namespace Global
1748} // 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
Communicator class.
Definition: dist.hpp:1657
void bcast(void *buffer, std::size_t count, const Datatype &datatype, int root) const
Blocking broadcast.
Definition: dist.cpp:544
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
void exscan(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking Exclusive Scan.
Definition: dist.cpp:686
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
void allgather(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype) const
Blocking gather-to-all.
Definition: dist.cpp:592
Communication Request vector class.
Definition: dist.hpp:948
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
Algebraic DOF Partitioning implementation.
LAFEM::DenseVector< DataType, IndexType > BufferVectorType
the type for buffers used by our mirrors
Index _extra_shared_dof_count
number of extra shared dofs; must be equal on all processes
Mirror_ MirrorType
the vector mirror type
String get_block_information() const
Returns the block information of the algebraic dof partitioning as an XML string.
Index _local_dof_count
number of local dofs of this process
void clear()
Resets the whole object.
const std::vector< int > & get_all_global_dof_offsets() const
Returns the all-global-dof-offsets vector.
MirrorType OwnerMirrorType
type of mirror for neighbor owner local dof indices
int get_donee_rank(Index i) const
Returns an donee neighbor rank.
Index get_num_local_dofs() const
Index _extra_local_dof_count
number of extra local dofs; may be different for each process
void assemble_by_gate(const GateType &gate, Index extra_local=0u, Index extra_shared=0u)
Assembles the AlgDofParti object from a given Global::Gate.
std::vector< DoneeMirrorType > _donee_mirrors
rank/mirror-pair of DOF-donee processes
Index _global_dof_offset
global dof offset of this process
Global::Vector< LocalVectorType, MirrorType > GlobalVectorType
the global vector type
LocalVector_ LocalVectorType
the local vector type
Index _global_dof_count
global dof count over all processes
std::vector< int > _owner_ranks
ranks of DOF-owner neighbor processes
Index get_global_dof_offset() const
AlgDofParti()
default constructor
std::vector< int > _all_global_dof_offset
global DOF offsets of all processes,
MirrorType _owned_mirror
mirror for this process's owned DOFs
const std::vector< int > & get_all_global_dof_counts() const
Returns the all-global-dof-counts vector.
void assemble_allgather(bool yes_i_really_want_to_do_this=false)
Assembles the required data for the AlgDofPartiSystem::apply() function.
std::vector< int > _all_global_dof_counts
global DOF counts of all processes,
MirrorType OwnedMirrorType
type of mirror for owned local dof indices
Intern::ADPAux< IndexVectorType > ADPAuxType
auxiliary helper class type
const OwnedMirrorType & get_owned_mirror() const
Returns the Mirror of the owned DOFs.
Index _extra_dof_offset
offset of first extra DOF
const OwnerMirrorType & get_owner_mirror(Index i) const
Returns an owner neighbor mirror.
virtual ~AlgDofParti()=default
virtual destructor
LocalVectorType::template ContainerTypeByDI< IndexType, IndexType > IndexVectorType
the index vector type, this one is required for internal computations
const IndexVectorType & get_global_dof_indices() const
Returns the global-dof-indices array.
std::vector< int > _donee_ranks
ranks of DOF-donee neighbor processes
LocalVectorType::DataType DataType
our data type
AlgDofParti(const AlgDofParti &)=delete
no copy, no problems
Index _owned_dof_count
number of owned dofs of this process
int get_owner_rank(Index i) const
Returns an owner neighbor rank.
String _block_information
a string containing the block information in XML format
std::size_t bytes() const
const Dist::Comm * get_comm() const
Index get_num_owner_neighbors() const
LAFEM::VectorMirror< DataType, IndexType > DoneeMirrorType
type of mirror for neighbor donee owned dof indices
Index get_num_donee_neighbors() const
const DoneeMirrorType & get_donee_mirror(Index i) const
Returns a donee neighbor mirror.
Index get_num_global_dofs() const
Index get_num_owned_dofs() const
Global::Gate< LocalVectorType, MirrorType > GateType
the global gate type
IndexVectorType _global_dof_idx
global dof indices for each local dof
const Dist::Comm * _comm
our communicator
LocalVectorType::IndexType IndexType
our index type
AlgDofParti & operator=(const AlgDofParti &)=delete
no copy, no problems
std::vector< OwnerMirrorType > _owner_mirrors
rank/mirror-pair of DOF-owner processes
Global gate implementation.
Definition: gate.hpp:78
const Dist::Comm * get_comm() const
Returns a const pointer to the underlying communicator.
Definition: gate.hpp:185
const LocalVector_ & get_freqs() const
Returns a const reference to the frequencies vector.
Definition: gate.hpp:226
const std::vector< Mirror_ > & get_mirrors() const
Returns a const reference to the neighbor mirrors vector.
Definition: gate.hpp:216
std::vector< int > _ranks
communication ranks
Definition: gate.hpp:98
Global vector wrapper class template.
Definition: vector.hpp:68
Dense data vector class template.
Handles vector prolongation, restriction and serialization.
String class implementation.
Definition: string.hpp:47
void pop_front()
Removes the first character from the string.
Definition: string.hpp:240
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
Definition: dist.hpp:571
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
std::uint64_t Index
Index data type.