FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
deslagged_domain_control.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#pragma once
6
8
9#include <kernel/geometry/cgal.hpp>
10#include <kernel/geometry/common_factories.hpp>
11#include <kernel/geometry/mesh_file_reader.hpp>
12#include <kernel/geometry/mesh_node.hpp>
13#include <kernel/geometry/parti_2lvl.hpp>
14#include <kernel/geometry/parti_iterative.hpp>
15#include <kernel/geometry/parti_parmetis.hpp>
16#include <kernel/geometry/parti_zoltan.hpp>
17#include <kernel/geometry/partition_set.hpp>
18#include <kernel/geometry/patch_mesh_factory.hpp>
19#include <kernel/geometry/patch_meshpart_factory.hpp>
20#include <kernel/geometry/patch_meshpart_splitter.hpp>
21#include <kernel/geometry/voxel_map.hpp>
22#include <kernel/runtime.hpp>
23#include <kernel/trafo/standard/mapping.hpp>
24#include <kernel/util/dist.hpp>
25#include <kernel/util/dist_file_io.hpp>
26#include <kernel/util/math.hpp>
27#include <kernel/util/property_map.hpp>
28#include <kernel/util/simple_arg_parser.hpp>
29#include <kernel/util/statistics.hpp>
30#include <kernel/util/stop_watch.hpp>
31
32#include <control/domain/parti_domain_control_base.hpp>
33
34#include <kernel/util/string.hpp>
35#include <kernel/util/tiny_algebra.hpp>
36#include <memory>
37#include <numeric>
38#include <sstream>
39
41{
52 template<typename DomainLevel_>
53 class DeslaggedDomainLevelWrapper : public DomainLevel_
54 {
55 public:
57 using BaseClass = DomainLevel_;
59 using typename BaseClass::MeshType;
61 using typename BaseClass::MeshNodeType;
62
64 std::shared_ptr<DomainLevel_> slag_level;
65
68
71
73 int _lvl_idx,
74 std::unique_ptr<MeshNodeType> node,
75 Adjacency::Coloring&& coloring,
76 Adjacency::Coloring&& layering) :
77 BaseClass(_lvl_idx, std::move(node)),
78 slag_level(),
79 element_coloring(std::move(coloring)),
80 element_layering(std::move(layering))
81 {
82 }
83
85 int _lvl_idx,
86 std::unique_ptr<MeshNodeType> node,
87 std::unique_ptr<MeshNodeType> slag_node,
88 Adjacency::Coloring&& coloring,
89 Adjacency::Coloring&& layering) :
90 BaseClass(_lvl_idx, std::move(node)),
91 slag_level(),
92 element_coloring(std::move(coloring)),
93 element_layering(std::move(layering))
94 {
95 if(slag_node)
96 {
97 slag_level = std::make_shared<DomainLevel_>(_lvl_idx, std::move(slag_node));
98 }
99 }
100 }; // class SlaggedDomainLevelWrapper<...>
101
108 template<typename MeshType_, typename WeightType_>
110 {
111 public:
112 using WeightType = WeightType_;
113 using MeshType = MeshType_;
114
115 virtual ~DeslaggingPredicateBase() = default;
116
129 virtual std::vector<WeightType> gather_element_weights(const MeshType& mesh) const = 0;
130
140 virtual std::vector<Index> gather_elements(const MeshType& mesh) const = 0;
141
153 virtual std::vector<Index> gather_elements(const MeshType& mesh, const Dist::Comm& comm) const = 0;
154 }; // class SlaggingPredicate
155
161 template<typename MeshType_, typename WeightType_>
162 class VoxelMapDeslaggingPredicate : public DeslaggingPredicateBase<MeshType_, WeightType_>
163 {
164 public:
166
167 using MeshType = typename BaseClass::MeshType;
168 using WeightType = typename BaseClass::WeightType;
169 using VertexType = typename MeshType::VertexType;
170 using CoordType = typename MeshType::CoordType;
171
172 static constexpr int dim = MeshType::shape_dim;
173
174 protected:
175 std::unique_ptr<Geometry::VoxelMap> _voxel_map;
176
177 public:
178 explicit VoxelMapDeslaggingPredicate(std::unique_ptr<Geometry::VoxelMap>&& voxel_map) :
179 _voxel_map(std::move(voxel_map))
180 {
181 }
182
195 const VertexType& min,
196 const VertexType& max,
197 const Tiny::Vector<Index, dim>& slices,
198 Index level)
199 {
200 _voxel_map = std::make_unique<Geometry::VoxelMap>();
201 if constexpr(dim == 2)
202 {
203 _voxel_map->set_bounding_box_2d(min[0], max[0], min[1], max[1]);
204 }
205 else
206 {
207 _voxel_map->set_bounding_box_3d(min[0], max[0], min[1], max[1], min[2], max[2]);
208 }
209
210 Real resolution = Math::Limits<Real>::max();
211 for(int i(0); i < dim; i++)
212 {
213 resolution = Math::min(resolution, (max[i] - min[i]) / Real(slices[i]));
214 }
215 resolution /= Real(2ULL << level);
216 _voxel_map->set_resolution(resolution);
217 }
218
228 VoxelMapDeslaggingPredicate(const VertexType& min, const VertexType& max, Real resolution)
229 {
230 _voxel_map = std::make_unique<Geometry::VoxelMap>();
231 if constexpr(dim == 2)
232 {
233 _voxel_map->set_bounding_box_2d(min[0], max[0], min[1], max[1]);
234 }
235 else
236 {
237 _voxel_map->set_bounding_box_3d(min[0], max[0], min[1], max[1], min[2], max[2]);
238 }
239 _voxel_map->set_resolution(resolution);
240 }
241
244 {
245 return *_voxel_map;
246 }
247
250 {
251 return *_voxel_map;
252 }
253
264 {
265 _voxel_map->set_out_of_bounds_value(value);
266 }
267
284 {
285 _voxel_map->compute_map(comm, masker, true);
286 }
287
305 template<typename Lambda_>
306 void compute_from_lambda(const Dist::Comm& comm, Lambda_&& lambda)
307 {
308 if constexpr(dim == 2)
309 {
310 _voxel_map->compute_map_from_lambda_2d(comm, std::forward<Lambda_>(lambda), true);
311 }
312 else
313 {
314 _voxel_map->compute_map_from_lambda_3d(comm, std::forward<Lambda_>(lambda), true);
315 }
316 }
317
336 void compute_from_chart(const Dist::Comm& comm, const Geometry::Atlas::ChartBase<MeshType>& chart, bool invert)
337 {
338 this->_voxel_map->compute_map_from_chart(comm, chart, invert, true);
339 }
340
362 void compute_from_off(const Dist::Comm& comm, const String& filename, bool invert)
363 {
364 std::stringstream sstr;
365 DistFileIO::read_common(sstr, filename, comm);
366 compute_from_off(comm, sstr, invert);
367 }
368
392 void compute_from_off(const Dist::Comm& comm, std::istream& is, bool invert)
393 {
394 // Error if this template member function gets instantiated for a non 3d mesh
395 static_assert(dim == 3);
396 _voxel_map->compute_map_from_off_3d(comm, is, invert, true);
397 }
398
412 {
413 Geometry::VoxelMap::ReadResult result = _voxel_map->read(comm, filename);
414 return result;
415 }
416
426 std::vector<int> gather_vertex_voxel_map(const MeshType& mesh) const
427 {
428 const auto& vtx = mesh.get_vertex_set();
429 const Index nv = vtx.get_num_vertices();
430
431 std::vector<int> mask(nv, 0);
432 for(Index i(0); i < nv; ++i)
433 {
434 mask[i] = _voxel_map->check_point_nearest(vtx[i]);
435 }
436
437 return mask;
438 }
439
441 std::vector<WeightType> gather_element_weights(const MeshType& mesh) const override
442 {
443 const Index num_elems = mesh.get_num_elements();
444
445 // allocate the weight vector
446 std::vector<WeightType> weights(num_elems, 0.0);
447
448 // get the vertex set and the vertices-at-element index set
449 const auto& vtx = mesh.get_vertex_set();
450 const auto& verts_at_elem = mesh.template get_index_set<dim, 0>();
451
452 // element bounding box coordinates
455
456 // loop over all elements
457 for(Index ielem(0); ielem < num_elems; ++ielem)
458 {
459 // compute bounding box of element
460 // note: actually, we should always have
461 // elbox_min = vtx[verts_at_elem(ielem, 0)]
462 // elbox_max = vtx[verts_at_elem(ielem, 1 << shape_dim - 1)]
463 // because of the element orientation of the base mesh, but we'll compute the bounding
464 // box for each element manually anyways, just to be sure in case that a derived class
465 // might give us a mesh object that is not directly refined from the base mesh using
466 // the standard 2-level refinement...
467 elbox_min = elbox_max = vtx[verts_at_elem(ielem, 0)];
468 for(int j(1); j < verts_at_elem.num_indices; ++j)
469 {
470 const auto& v = vtx[verts_at_elem(ielem, j)];
471 for(int k(0); k < dim; ++k)
472 {
473 Math::minimax(v[k], elbox_min[k], elbox_max[k]);
474 }
475 }
476
477 // gather element weight based on the bounding box
478 weights[ielem] = this->_voxel_map->sample_box(elbox_min, elbox_max);
479 }
480
481 // returns the element weights
482 return weights;
483 }
484
499 std::vector<Index> gather_elements(const MeshType& mesh) const override
500 {
501 const Index num_elems = mesh.get_num_elements();
502
503 // reserve the vector
504 std::vector<Index> masked_elems;
505 masked_elems.reserve(num_elems);
506
507 // get the vertex set and the vertices-at-element index set
508 const auto& vtx = mesh.get_vertex_set();
509 const auto& verts_at_elem = mesh.template get_index_set<dim, 0>();
510
511 // element bounding box coordinates
514
515 // loop over all elements
516 for(Index ielem(0); ielem < num_elems; ++ielem)
517 {
518 // compute bounding box of element
519 // note: actually, we should always have
520 // elbox_min = vtx[verts_at_elem(ielem, 0)]
521 // elbox_max = vtx[verts_at_elem(ielem, 1 << shape_dim - 1)]
522 // because of the element orientation of the base mesh, but we'll compute the bounding
523 // box for each element manually anyways, just to be sure in case that a derived class
524 // might give us a mesh object that is not directly refined from the base mesh using
525 // the standard 2-level refinement...
526 elbox_min = elbox_max = vtx[verts_at_elem(ielem, 0)];
527 for(int j(1); j < verts_at_elem.num_indices; ++j)
528 {
529 const auto& v = vtx[verts_at_elem(ielem, j)];
530 for(int k(0); k < dim; ++k)
531 {
532 Math::minimax(v[k], elbox_min[k], elbox_max[k]);
533 }
534 }
535
536 // check the bounding box against the voxel map
537 if(this->_voxel_map->check_box(elbox_min, elbox_max))
538 {
539 masked_elems.push_back(ielem);
540 }
541 }
542
543 // return the list of all elements that we found
544 return masked_elems;
545 }
546
548 std::vector<Index> gather_elements(const MeshType& mesh, const Dist::Comm& comm) const override
549 {
550 const Index num_elems = mesh.get_num_elements();
551 const auto num_procs = Index(comm.size());
552 const auto cur_rank = Index(comm.rank());
553 //const Index elems_per_rank = (num_elems / num_procs) + Index((num_elems % num_procs) > 0);
554
555 const Index base_elems = num_elems / num_procs;
556 const Index remainder = num_elems % num_procs;
557 // Assign remaining elements to first remainder ranks
558 const Index num_elements = base_elems + Index(cur_rank < remainder);
559 // Offset is base offset, plus number of remainder elements assigned up to this rank
560 const Index offset = (cur_rank * base_elems) + Math::min(cur_rank, remainder);
561
562 // reserve the vector
563 std::vector<Index> masked_elems;
564 masked_elems.reserve(num_elements);
565
566 // get the vertex set and the vertices-at-element index set
567 const auto& vtx = mesh.get_vertex_set();
568 const auto& verts_at_elem = mesh.template get_index_set<dim, 0>();
569
570 // element bounding box coordinates
573
574 // loop over all elements
575 for(Index ielem = 0; ielem < num_elements; ielem++)
576 {
577 const Index elem = offset + ielem;
578 // compute bounding box of element
579 // note: actually, we should always have
580 // elbox_min = vtx[verts_at_elem(ielem, 0)]
581 // elbox_max = vtx[verts_at_elem(ielem, 1 << shape_dim - 1)]
582 // because of the element orientation of the base mesh, but we'll compute the bounding
583 // box for each element manually anyways, just to be sure in case that a derived class
584 // might give us a mesh object that is not directly refined from the base mesh using
585 // the standard 2-level refinement...
586 elbox_min = elbox_max = vtx[verts_at_elem(elem, 0)];
587 for(int j(1); j < verts_at_elem.num_indices; ++j)
588 {
589 const auto& v = vtx[verts_at_elem(elem, j)];
590 for(int k(0); k < dim; ++k)
591 {
592 Math::minimax(v[k], elbox_min[k], elbox_max[k]);
593 }
594 }
595
596 // check the bounding box against the voxel map
597 if(this->_voxel_map->check_box(elbox_min, elbox_max))
598 {
599 masked_elems.push_back(elem);
600 }
601 }
602
603 // communicate sizes of buffers
604 std::vector<int> recv_sizes(num_procs);
605 recv_sizes.at(cur_rank) = int(masked_elems.size());
606
607 comm.allgather(recv_sizes.data(), 1, recv_sizes.data(), 1);
608
609 // and now, create big buffer
610 std::vector<Index> gathered_mask(std::size_t(std::accumulate(recv_sizes.begin(), recv_sizes.end(), 0)), 0);
611
612 // and also, we have to caclulate our displacements into our big buffer
613 std::vector<int> displacements;
614 displacements.reserve(recv_sizes.size());
615 std::exclusive_scan(recv_sizes.begin(), recv_sizes.end(), std::back_inserter(displacements), 0);
616
617 // and now gather
618 comm.allgatherv(
619 masked_elems.data(),
620 masked_elems.size(),
621 gathered_mask.data(),
622 recv_sizes.data(),
623 displacements.data());
624
625 // return the list of all elements that we found
626 return gathered_mask;
627 }
628 }; // class VoxelMapSlaggingPredicate
629
630#if defined(FEAT_HAVE_CGAL) || defined(DOXYGEN)
636 template<typename MeshType_, typename WeightType_>
637 class CGALDeslaggingPredicate : public DeslaggingPredicateBase<MeshType_, WeightType_>
638 {
639 public:
641
642 using MeshType = typename BaseClass::MeshType;
643 using WeightType = typename BaseClass::WeightType;
644 using VertexType = typename MeshType::VertexType;
645 using CoordType = typename MeshType::CoordType;
647
648 static constexpr int dim = MeshType::shape_dim;
649
650 static_assert(dim == 3, "CGAL deslagging is only available in 3D");
651
652 protected:
653 bool _cheap_weights = true;
654 std::unique_ptr<CGALWrapperType> _cgal_wrapper;
655
656 public:
658 explicit CGALDeslaggingPredicate(std::unique_ptr<CGALWrapperType>&& cgal_wrapper) :
659 _cgal_wrapper(std::move(cgal_wrapper))
660 {
661 }
662
665 _cgal_wrapper(std::make_unique<CGALWrapperType>(file, file_mode))
666 {
667 }
668
670 explicit CGALDeslaggingPredicate(const Dist::Comm& comm, const FEAT::String& file, Geometry::CGALFileMode file_mode)
671 {
672 std::stringstream file_contents;
673 DistFileIO::read_common(file_contents, file, comm);
674 _cgal_wrapper = std::make_unique<CGALWrapperType>(file_contents, file_mode);
675 }
676
679 {
680 XASSERT(_cgal_wrapper != nullptr);
681 return *_cgal_wrapper;
682 }
683
686 {
687 XASSERT(_cgal_wrapper != nullptr);
688 return *_cgal_wrapper;
689 }
690
697 void cheap_weight_calculation(bool enable)
698 {
699 _cheap_weights = enable;
700 }
701
703 std::vector<Index> gather_elements(const MeshType& mesh) const override
704 {
705 const Index num_elems = mesh.get_num_elements();
706
707 // get the vertex set and the vertices-at-element index set
708 const auto& vtx = mesh.get_vertex_set();
709 const auto& verts_at_elem = mesh.template get_index_set<dim, 0>();
710
711 // dispatch so we use the thread safe variant
712 return _offset_gather_mask(vtx, verts_at_elem, 0u, num_elems);
713 }
714
716 std::vector<Index> gather_elements(const MeshType& mesh, const Dist::Comm& comm) const override
717 {
718 const Index num_elems = mesh.get_num_elements();
719 const auto num_procs = Index(comm.size());
720 const auto cur_rank = Index(comm.rank());
721
722 const Index base_elems = num_elems / num_procs;
723 const Index remainder = num_elems % num_procs;
724 // Assign remaining elements to first remainder ranks
725 const Index num_elements = base_elems + Index(cur_rank < remainder);
726 // Offset is base offset, plus number of remainder elements assigned up to this rank
727 const Index offset = (cur_rank * base_elems) + Math::min(cur_rank, remainder);
728
729 // get the vertex set and the vertices-at-element index set
730 const auto& vtx = mesh.get_vertex_set();
731 const auto& verts_at_elem = mesh.template get_index_set<dim, 0>();
732
733 const auto masked_elems = _offset_gather_mask(vtx, verts_at_elem, offset, num_elements);
734
735 // communicate sizes of buffers
736 std::vector<int> recv_sizes(num_procs);
737 recv_sizes.at(cur_rank) = int(masked_elems.size());
738
739 comm.allgather(recv_sizes.data(), 1, recv_sizes.data(), 1);
740
741 // and now, create big buffer
742 std::vector<Index> gathered_mask(std::size_t(std::accumulate(recv_sizes.begin(), recv_sizes.end(), 0)), 0);
743
744 // and also, we have to caclulate our displacements into our big buffer
745 std::vector<int> displacements;
746 displacements.reserve(recv_sizes.size());
747 std::exclusive_scan(recv_sizes.begin(), recv_sizes.end(), std::back_inserter(displacements), 0);
748
749 // and now gather
750 comm.allgatherv(
751 masked_elems.data(),
752 masked_elems.size(),
753 gathered_mask.data(),
754 recv_sizes.data(),
755 displacements.data());
756
757 // return the list of all elements that we found
758 return gathered_mask;
759 }
760
762 std::vector<WeightType> gather_element_weights(const MeshType& mesh) const override
763 {
764 if(_cheap_weights)
765 {
766 return {};
767 }
768
769 const Index num_elems = mesh.get_num_elements();
770
771 // allocate the weight vector
772 std::vector<WeightType> weights(num_elems, 0.0);
773
774 Cubature::DynamicFactory cub_fac("refine*4:gauss-legendre:3");
776 cub_fac.create(rule);
777
778 // guarantee that cgal wrapper is initialized
779 this->_cgal_wrapper->point_not_outside(CoordType(0), CoordType(0), CoordType(0));
780
781 // create trafo
782 using TrafoType = Trafo::Standard::Mapping<MeshType>;
783 using TrafoEvaluator = typename TrafoType::template Evaluator<typename MeshType::ShapeType, CoordType>::Type;
784 static constexpr TrafoTags trafo_config = TrafoTags::dom_point | TrafoTags::img_point | TrafoTags::jac_det;
785
787 using TrafoEvalData = typename TrafoEvaluator::template ConfigTraits<trafo_config>::EvalDataType;
788
789 const TrafoType trafo(const_cast<MeshType&>(mesh)); //NOLINT
790
791 FEAT_PRAGMA_OMP(parallel)
792 {
793 TrafoEvaluator trafo_eval(trafo);
794 TrafoEvalData trafo_data;
795
796 // loop over all elements
797 FEAT_PRAGMA_OMP(for)
798 for(Index ielem = 0; ielem < num_elems; ++ielem)
799 {
800 trafo_eval.prepare(ielem);
801 CoordType loc_sum = CoordType(0);
802 for(int k = 0; k < rule.get_num_points(); ++k)
803 {
804 const auto ref_point = rule.get_point(k);
805 trafo_eval(trafo_data, ref_point);
806 const auto& img_point = trafo_data.img_point;
807 const CoordType weight = trafo_data.jac_det * rule.get_weight(k);
808 loc_sum +=
809 weight * CoordType(this->_cgal_wrapper->point_not_outside(img_point[0], img_point[1], img_point[2]));
810 }
811 loc_sum /= trafo_eval.volume();
812 ASSERTM(loc_sum >= CoordType(0) && loc_sum <= CoordType(1), "Local sum not between zero and one");
813
814 // gather element weight based on the bounding box
815 weights[ielem] = loc_sum;
816 }
817 }
818
819 // returns the element weights
820 return weights;
821 }
822 protected:
823 template<typename VtxSet_, typename IdxMap_>
824 std::vector<Index> _offset_gather_mask_omp(const VtxSet_& vtx, const IdxMap_& verts_at_elem, Index offset, Index num_elems) const
825 {
826 // reserve the vector
827 std::vector<std::uint8_t> masked_elems;
828 masked_elems.resize(num_elems);
829
830 FEAT_PRAGMA_OMP(parallel for)
831 for(std::size_t k = 0; k < num_elems; ++k)
832 {
833 masked_elems[k] = 0u;
834 }
835
836 std::size_t num_mask = 0u;
837 // loop over all elements
838 FEAT_PRAGMA_OMP(parallel for reduction(+ : num_mask))
839 for(Index ielem = 0; ielem < num_elems; ++ielem)
840 {
841 std::array<Tiny::Vector<CoordType, dim>, std::decay_t<decltype(verts_at_elem)>::num_indices> verts;
842 for(std::size_t k = 0; k < verts.size(); ++k)
843 {
844 verts[k] = vtx[verts_at_elem(ielem+offset, int(k))];
845 }
846
847 if(this->_cgal_wrapper->intersects_polygon(verts))
848 {
849 masked_elems.at(ielem) = 1;
850 ++num_mask;
851 }
852 }
853
854 std::vector<Index> masked_elems_gathered;
855 masked_elems_gathered.reserve(num_mask);
856
857 std::size_t ctr_num = 0;
858 for(Index ielem = 0; ielem < num_elems; ++ielem)
859 {
860 if(masked_elems[ielem] == 1)
861 {
862 ++ctr_num;
863 masked_elems_gathered.push_back(ielem+offset);
864 }
865 }
866
867 XASSERTM(ctr_num == num_mask, "Misscount in gather loop");
868
869 // return the list of all elements that we found
870 return masked_elems_gathered;
871
872 }
873
874 template<typename VtxSet_, typename IdxMap_>
875 std::vector<Index> _offset_gather_mask_single_thread(const VtxSet_& vtx, const IdxMap_& verts_at_elem, Index offset, Index num_elems) const
876 {
877 // reserve the vector
878 std::vector<Index> masked_elems;
879 masked_elems.reserve(num_elems);
880
881 // loop over all elements
882 for(Index ielem = 0; ielem < num_elems; ++ielem)
883 {
884 std::array<Tiny::Vector<CoordType, dim>, std::decay_t<decltype(verts_at_elem)>::num_indices> verts;
885 for(std::size_t k = 0; k < verts.size(); ++k)
886 {
887 verts[k] = vtx[verts_at_elem(ielem+offset, int(k))];
888 }
889
890 if(this->_cgal_wrapper->intersects_polygon(verts))
891 {
892 masked_elems.push_back(ielem+offset);
893 }
894 }
895
896 // return the list of all elements that we found
897 return masked_elems;
898
899 }
900
901 template<typename VtxSet_, typename IdxMap_>
902 std::vector<Index> _offset_gather_mask(const VtxSet_& vtx, const IdxMap_& verts_at_elem, Index offset, Index num_elems) const
903 {
904
905 #ifdef FEAT_HAVE_OMP
907 return _offset_gather_mask_omp(vtx, verts_at_elem, offset, num_elems);
908 #endif
909 return _offset_gather_mask_single_thread(vtx, verts_at_elem, offset, num_elems);
910 }
911 }; // class CGALDeslaggingPredicate
912#endif
913
922 template<typename DomainLevel_>
924 {
925 public:
929 using typename BaseClass::LevelType;
931 using typename BaseClass::LayerType;
933 using typename BaseClass::MeshType;
935 using typename BaseClass::AtlasType;
937 using typename BaseClass::MeshNodeType;
939 using typename BaseClass::MeshPartType;
941 using typename BaseClass::WeightType;
943 using typename BaseClass::Ancestor;
944
946 static constexpr int shape_dim = MeshType::shape_dim;
947
949 using CoordType = typename MeshType::CoordType;
950
952 using VertexType = typename MeshType::VertexType;
953
956
957 protected:
960
963
966
968 std::array<Index, shape_dim> _base_slices;
969
971 std::unique_ptr<DeslaggingPredicateType> _deslagging_predicate;
972
974 std::unique_ptr<MeshNodeType> _base_mesh_node;
975
977 StopWatch _watch_create_base_mesh, _watch_create_voxel_map, _watch_create_hierarchy;
978
981
983 bool _unstructured_mesh = false;
984
985 public:
995 explicit DeslaggedDomainControl(const Dist::Comm& comm_, bool support_multi_layered) :
996 BaseClass(comm_, support_multi_layered)
997 {
998 }
999
1002
1005
1008
1011
1013 virtual ~DeslaggedDomainControl() = default;
1014
1028 {
1029 this->_keep_deslagging_predicate = true;
1030 }
1031
1033 void set_deslagging_predicate(std::unique_ptr<DeslaggingPredicateType>&& pred)
1034 {
1035 _deslagging_predicate = std::move(pred);
1036 }
1037
1047 void set_deslag_level_range(int min, int max)
1048 {
1049 XASSERT(min <= max);
1050
1051 _deslag_lvl_min = min;
1052 _deslag_lvl_max = max;
1053 }
1054
1059 {
1060 return *_deslagging_predicate;
1061 }
1062
1067 {
1068 return *_deslagging_predicate;
1069 }
1070
1071 DeslaggingPredicateType* release_deslagging_predicate()
1072 {
1073 return _deslagging_predicate.release();
1074 }
1075
1076 VertexType get_bounding_box_min() const
1077 {
1078 return _bbox_min;
1079 }
1080
1081 VertexType get_bounding_box_max() const
1082 {
1083 return _bbox_max;
1084 }
1085
1109 create_base_mesh_2d(Index num_x, Index num_y, CoordType x_min, CoordType x_max, CoordType y_min, CoordType y_max)
1110 {
1111 XASSERTM(shape_dim == 2, "invalid dimension: this function can only be called for 2D domains!");
1112 XASSERTM(_base_mesh_node == nullptr, "base mesh already created");
1113
1114 XASSERT(num_x > Index(0));
1115 XASSERT(num_y > Index(0));
1116 XASSERT(x_min < x_max);
1117 XASSERT(y_min < y_max);
1118
1120
1121 _base_slices[0] = num_x;
1122 _base_slices[1] = num_y;
1123 _bbox_min[0] = x_min;
1124 _bbox_max[0] = x_max;
1125 _bbox_min[1] = y_min;
1126 _bbox_max[1] = y_max;
1127
1128 // create a structured mesh from factory
1131 &this->_atlas);
1132
1133 // get the node's vertex set
1134 auto& vtx = _base_mesh_node->get_mesh()->get_vertex_set();
1135 Index n = vtx.get_num_vertices();
1136 for(Index i(0); i < n; ++i)
1137 {
1138 vtx[i][0] = x_min + (x_max - x_min) * vtx[i][0];
1139 vtx[i][1] = y_min + (y_max - y_min) * vtx[i][1];
1140 }
1141
1143
1144 // return the base-mesh node
1145 return *_base_mesh_node.get();
1146 }
1147
1174 Index num_x,
1175 Index num_y,
1176 Index num_z,
1177 CoordType x_min,
1178 CoordType x_max,
1179 CoordType y_min,
1180 CoordType y_max,
1181 CoordType z_min,
1182 CoordType z_max)
1183 {
1184 XASSERTM(shape_dim == 3, "invalid dimension: this function can only be called for 3D domains!");
1185 XASSERTM(_base_mesh_node == nullptr, "base mesh already created");
1186
1187 XASSERT(num_x > Index(0));
1188 XASSERT(num_y > Index(0));
1189 XASSERT(num_z > Index(0));
1190 XASSERT(x_min < x_max);
1191 XASSERT(y_min < y_max);
1192 XASSERT(z_min < z_max);
1193
1195
1196 _base_slices[0] = num_x;
1197 _base_slices[1] = num_y;
1198 _base_slices[2] = num_z;
1199 _bbox_min[0] = x_min;
1200 _bbox_max[0] = x_max;
1201 _bbox_min[1] = y_min;
1202 _bbox_max[1] = y_max;
1203 _bbox_min[2] = z_min;
1204 _bbox_max[2] = z_max;
1205
1206 // create a structured mesh from factory
1209 &this->_atlas);
1210
1211 // get the node's vertex set
1212 auto& vtx = _base_mesh_node->get_mesh()->get_vertex_set();
1213 Index n = vtx.get_num_vertices();
1214 for(Index i(0); i < n; ++i)
1215 {
1216 vtx[i][0] = x_min + (x_max - x_min) * vtx[i][0];
1217 vtx[i][1] = y_min + (y_max - y_min) * vtx[i][1];
1218 vtx[i][2] = z_min + (z_max - z_min) * vtx[i][2];
1219 }
1220
1222
1223 // return the base-mesh node
1224 return *_base_mesh_node.get();
1225 }
1226
1227 MeshNodeType& set_base_mesh(std::unique_ptr<MeshNodeType>&& mesh_node)
1228 {
1229 XASSERTM(_base_mesh_node == nullptr, "base mesh already created!");
1230
1232 _base_mesh_node = std::move(mesh_node);
1233
1234 for(std::size_t k = 0; k < std::size_t(shape_dim); ++k)
1235 {
1237 }
1238
1239 for(int k = 0; k < shape_dim; ++k)
1240 {
1242 _bbox_max[k] = Math::Limits<CoordType>::lowest();
1243 }
1244
1245 auto& vtx = _base_mesh_node->get_mesh()->get_vertex_set();
1246 Index n = vtx.get_num_vertices();
1247 for(Index i(0); i < n; ++i)
1248 {
1249 for(int k = 0; k < shape_dim; ++k)
1250 {
1251 _bbox_min[k] = Math::min(_bbox_min[k], vtx[i][k]);
1252 _bbox_max[k] = Math::max(_bbox_max[k], vtx[i][k]);
1253 }
1254 }
1255
1257
1258 // update unstructured mesh flag
1259 this->_unstructured_mesh = true;
1260
1261 // return the base-mesh node
1262 return *_base_mesh_node.get();
1263 }
1264
1272 {
1273 XASSERTM(_base_mesh_node != nullptr, "invalid creation order: create/set base mesh first");
1274 XASSERTM(_deslagging_predicate != nullptr, "invalid creation order: set deslagging predicate first");
1275 XASSERTM(!this->_was_created, "hierarchy already created");
1276
1277 this->_watch_create_hierarchy.start();
1278
1279 // create the domain control
1280#ifdef FEAT_HAVE_MPI
1281 if(this->_comm.size() == 1)
1282 {
1283 // We've got just one process, so it's a simple choice:
1284 this->_create_single_process(std::move(this->_base_mesh_node));
1285 }
1286 else if(this->_support_multi_layered && (this->_desired_levels.size() > std::size_t(2)))
1287 {
1288 // The application supports multi-layered domain controls and
1289 // the user wants that, so create a multi-layered one:
1290 this->_create_multi_layered(std::move(this->_base_mesh_node));
1291 }
1292 else
1293 {
1294 // Create a single-layered domain control:
1295 this->_create_single_layered(std::move(this->_base_mesh_node));
1296 }
1297#else // not FEAT_HAVE_MPI
1298 {
1299 // In the non-MPI case, we always have only one process:
1300 this->_create_single_process(std::move(this->_base_mesh_node));
1301 }
1302#endif // FEAT_HAVE_MPI
1303
1304 // compile virtual levels
1305 this->compile_virtual_levels();
1306
1307 this->_watch_create_hierarchy.stop();
1308
1309 // collect statistics
1310 FEAT::Statistics::toe_partition = this->_watch_create_base_mesh.elapsed() +
1311 this->_watch_create_voxel_map.elapsed() +
1312 this->_watch_create_hierarchy.elapsed();
1313
1314 // Mark creation
1315 this->_was_created = true;
1316
1317 // delete the slagging predicate unless we are meant to keep it
1318 if(!_keep_deslagging_predicate)
1319 {
1320 _deslagging_predicate.reset();
1321 }
1322 }
1323
1326 {
1327 return this->_watch_create_base_mesh;
1328 }
1329
1332 {
1333 return this->_watch_create_voxel_map;
1334 }
1335
1338 {
1339 return this->_watch_create_hierarchy;
1340 }
1341
1354 std::vector<WeightType> gather_element_weights(Index level) const
1355 {
1356 XASSERT(level < this->size_physical());
1357 return _deslagging_predicate->gather_element_weights(this->at(level)->get_mesh());
1358 }
1359
1364 {
1365 String msg;
1366 for(auto it = this->_layer_levels.begin(); it != this->_layer_levels.end(); ++it)
1367 {
1368 if(it != this->_layer_levels.begin())
1369 {
1370 msg += " |";
1371 }
1372 for(auto jt = it->begin(); jt != it->end(); ++jt)
1373 {
1374 msg += " " + stringify((*jt)->get_level_index());
1375 msg += "[";
1376 msg += stringify((*jt)->get_mesh().get_num_elements()).pad_front(4);
1377 msg += "]";
1378 if((*jt)->slag_level)
1379 {
1380 ((msg += "<[") += stringify((*jt)->slag_level->get_mesh().get_num_elements()).pad_front(4)) += "]";
1381 }
1382 }
1383 }
1384 return msg;
1385 }
1386
1396 virtual void create(const std::deque<String>& filenames, const String& dirpath = "")
1397 {
1398 // dummy implementation
1399 ASSERTM(false, "Thou shall not arrive here!");
1400 (void)filenames;
1401 (void)dirpath;
1402 }
1403
1410 virtual void create(Geometry::MeshFileReader& /*unused*/)
1411 {
1412 // dummy implementation
1413 ASSERTM(false, "Thou shall not arrive here!");
1414 }
1415
1422 virtual void create(std::unique_ptr<MeshNodeType>& /*unused*/)
1423 {
1424 // dummy implementation
1425 ASSERTM(false, "Thou shall not arrive here!");
1426 }
1427
1428 protected:
1436 std::vector<Index> _gather_masked_elements(const MeshType& mesh)
1437 {
1438 return _deslagging_predicate->gather_elements(mesh);
1439 }
1440
1449 std::vector<Index> _gather_masked_elements(const MeshType& mesh, const Dist::Comm& sibling_comm) const
1450 {
1451 return _deslagging_predicate->gather_elements(mesh, sibling_comm);
1452 }
1453
1472 virtual std::unique_ptr<MeshNodeType> _deslag_mesh_node(int lvl, MeshNodeType& mesh_node)
1473 {
1474 XASSERTM(mesh_node.get_halo_map().empty(), "This function must not be used for partitioned mesh nodes!");
1475 if(_deslag_lvl_min <= lvl && lvl <= _deslag_lvl_max)
1476 {
1477 return mesh_node.extract_patch(_gather_masked_elements(*mesh_node.get_mesh(), this->_comm), true, false, false);
1478 }
1479 else
1480 {
1481 // Return MeshNode with full patch, i.e. no deslagging
1482 const Index num_elements = mesh_node.get_mesh()->get_num_elements();
1483 std::vector<Index> elements(num_elements);
1484 for(Index i(0); i < num_elements; i++)
1485 {
1486 elements[i] = i;
1487 }
1488
1489 return mesh_node.extract_patch(std::move(elements), true, false, false);
1490 }
1491 }
1492
1514 virtual std::unique_ptr<MeshNodeType>
1515 _deslag_mesh_node(int lvl, MeshNodeType& mesh_node, const Ancestor& ancestor, bool is_child)
1516 {
1517 const bool split_meshparts = true;
1518 const bool split_halos = false;
1519 const bool split_patches = false;
1520
1521 if(_deslag_lvl_min <= lvl && lvl <= _deslag_lvl_max)
1522 {
1523 const auto* prod_comm = is_child ? &ancestor.progeny_comm : nullptr;
1524 std::unique_ptr<MeshNodeType> new_node =
1525 prod_comm
1526 ? mesh_node.extract_patch(_gather_masked_elements(*mesh_node.get_mesh(), *prod_comm), split_meshparts, split_halos, split_patches)
1527 : mesh_node.extract_patch(_gather_masked_elements(*mesh_node.get_mesh()), split_meshparts, split_halos, split_patches);
1528 this->_deslag_patch_halos(*new_node, mesh_node, ancestor, is_child);
1529 return new_node;
1530 }
1531 else
1532 {
1533 // Return MeshNode with full patch, i.e. no deslagging
1534 const Index num_elements = mesh_node.get_mesh()->get_num_elements();
1535 std::vector<Index> elements(num_elements);
1536 for(Index i(0); i < num_elements; i++)
1537 {
1538 elements[i] = i;
1539 }
1540
1541 // OPTIMIZATION: Let extract_patch take a range of indices, rather than explicitly a vector.
1542 return mesh_node.extract_patch(std::move(elements), true, false, false);
1543 }
1544 }
1545
1564 MeshNodeType& patch_mesh_node,
1565 const MeshNodeType& base_mesh_node,
1566 const Ancestor& ancestor,
1567 bool is_child)
1568 {
1569 // get the map of the base-mesh halos
1570 const std::map<int, std::unique_ptr<MeshPartType>>& base_halo_map = base_mesh_node.get_halo_map();
1571
1572 // if the base mesh has no halos, then we can jump out of here
1573 if(base_halo_map.empty())
1574 {
1575 return;
1576 }
1577
1578 // get number of halos
1579 const std::size_t num_halos = base_halo_map.size();
1580
1581 // create a halo splitter
1582 Geometry::PatchHaloSplitter<MeshType> halo_splitter(*base_mesh_node.get_mesh(), *base_mesh_node.get_patch(-1));
1583
1584 // add each base-mesh halo to our halo splitter and store the resulting split data size
1585 std::vector<int> halo_ranks;
1586 std::vector<std::size_t> halo_send_sizes;
1587 for(auto it = base_halo_map.begin(); it != base_halo_map.end(); ++it)
1588 {
1589 // store halo rank
1590 halo_ranks.push_back(it->first);
1591
1592 // add halo and compute send buffer size
1593 halo_send_sizes.push_back(halo_splitter.add_halo(it->first, *it->second));
1594 }
1595
1596 // This vector will receive the split halo data from all our potential neighbor processes
1597 std::vector<std::size_t> halo_recv_sizes(num_halos);
1598 std::vector<std::vector<Index>> halo_send_data(num_halos);
1599 std::vector<std::vector<Index>> halo_recv_data(num_halos);
1600 Dist::RequestVector halo_recv_reqs(num_halos);
1601 Dist::RequestVector halo_send_reqs(num_halos);
1602
1603 // get the layer index of our comm layer; the ancestor layer unless we got the child ancestry during
1604 // multi-layered partitioning; this may be equal to -1 in either case if this process does not participate
1605 // in that corresponding layer
1606 const int layer_idx = is_child ? ancestor.layer_p : ancestor.layer;
1607
1608 // split and serialize halos and store sizes
1609 for(std::size_t i(0); i < num_halos; ++i)
1610 {
1611 // serialize the split halo into the send data buffer
1612 if(halo_send_sizes.at(i) > std::size_t(0))
1613 {
1614 halo_send_data.at(i) = halo_splitter.serialize_split_halo(halo_ranks[i], -1);
1615 }
1616 }
1617
1618 // exchange halo send data sizes over the corresponding layer communicator
1619 if(layer_idx >= 0)
1620 {
1621 // get the corresponding layer communicator
1622 const Dist::Comm& layer_comm = this->_layers.at(std::size_t(layer_idx))->comm();
1623
1624 // exchange halos sizes
1625 for(std::size_t i(0); i < num_halos; ++i)
1626 {
1627 // post receive requests
1628 halo_recv_reqs[i] = layer_comm.irecv(&halo_recv_sizes[i], std::size_t(1), halo_ranks[i]);
1629
1630 // post send requests
1631 halo_send_reqs[i] = layer_comm.isend(&halo_send_sizes[i], std::size_t(1), halo_ranks[i]);
1632 }
1633
1634 // wait for sends and receives to finish
1635 halo_recv_reqs.wait_all();
1636 halo_send_reqs.wait_all();
1637
1638 // exchange halo data
1639 for(std::size_t i(0); i < num_halos; ++i)
1640 {
1641 // do we receive any data from our neighbor?
1642 if(halo_recv_sizes[i] > Index(0))
1643 {
1644 // resize buffer and post receive
1645 halo_recv_data.at(i).resize(halo_recv_sizes[i]);
1646 halo_recv_reqs[i] = layer_comm.irecv(halo_recv_data.at(i).data(), halo_recv_sizes.at(i), halo_ranks[i]);
1647 }
1648
1649 // do we send any data to our neighbor?
1650 if(halo_send_sizes.at(i) > Index(0))
1651 {
1652 // post send of actual halo buffer
1653 halo_send_reqs[i] = layer_comm.isend(halo_send_data.at(i).data(), halo_send_sizes.at(i), halo_ranks[i]);
1654 }
1655 }
1656
1657 // wait for sends and receives to finish
1658 halo_recv_reqs.wait_all();
1659 halo_send_reqs.wait_all();
1660 }
1661
1662 // broadcast halo receive data over progeny comm in case of multi-layered partitioning
1663 if(is_child)
1664 {
1665 // broadcast sizes
1666 ancestor.progeny_comm.bcast(halo_recv_sizes.data(), num_halos, 0);
1667
1668 // allocate buffers
1669 if(ancestor.progeny_comm.rank() != 0)
1670 {
1671 for(std::size_t i(0); i < num_halos; ++i)
1672 {
1673 halo_recv_data.at(i).resize(halo_recv_sizes[i]);
1674 }
1675 }
1676
1677 // broadcast all halos
1678 for(std::size_t i(0); i < num_halos; ++i)
1679 {
1680 if(halo_recv_sizes[i] > std::size_t(0))
1681 {
1682 ancestor.progeny_comm.bcast(halo_recv_data[i].data(), halo_recv_sizes[i], 0);
1683 }
1684 }
1685 }
1686
1687 /*{
1688 String s;
1689 for(std::size_t i(0); i < num_halos; ++i)
1690 {
1691 s += stringify(halo_ranks[i]);
1692 s += " >";
1693 for(Index j(halo_recv_data[i]); j < halo_recv_data[i+1]; ++j)
1694 (s += " ") += stringify(halo_recv_data[j]);
1695 s += "\n";
1696 }
1697 this->_comm.allprint(s);
1698 }*/
1699
1700 // create a vector of split halo sizes
1701 std::vector<std::array<Index, shape_dim + 1>> halo_send_intsec_sizes(num_halos);
1702 std::vector<std::array<Index, shape_dim + 1>> halo_recv_intsec_sizes(num_halos);
1703
1704 // process all halos
1705 for(std::size_t i(0); i < num_halos; ++i)
1706 {
1707 // did we receive any data from our neighbor?
1708 if(halo_recv_sizes.at(i) == Index(0))
1709 {
1710 continue;
1711 }
1712
1713 // intersect with our other halo
1714 if(!halo_splitter.intersect_split_halo(halo_ranks[i], halo_recv_data.at(i), 0))
1715 {
1716 continue; // no intersection between halos
1717 }
1718
1719 // create the new halo
1720 std::unique_ptr<MeshPartType> split_halo = halo_splitter.make_unique();
1721
1722 // store the entity counts of the halo in our split sizes vector
1723 for(int j(0); j <= shape_dim; ++j)
1724 {
1725 halo_send_intsec_sizes[i][Index(j)] = split_halo->get_num_entities(j);
1726 }
1727
1728 // create new halo mesh-part
1729 patch_mesh_node.add_halo(halo_ranks[i], std::move(split_halo));
1730 }
1731
1732 // exchange halo intersected halo sizes over progeny comm; we do this just to ensure consistency
1733 if((ancestor.layer_p >= 0) || (!is_child && (ancestor.layer >= 0)))
1734 {
1735 // get the corresponding layer communicator
1736 const LayerType& layer = *this->_layers.at(std::size_t(is_child ? ancestor.layer_p : ancestor.layer));
1737
1738 // exchange intersected halo sizes
1739 for(std::size_t i(0); i < num_halos; ++i)
1740 {
1741 halo_recv_reqs[i] =
1742 layer.comm().irecv(halo_recv_intsec_sizes.at(i).data(), std::size_t(shape_dim + 1), halo_ranks[i]);
1743 halo_send_reqs[i] =
1744 layer.comm().isend(halo_send_intsec_sizes.at(i).data(), std::size_t(shape_dim + 1), halo_ranks[i]);
1745 }
1746
1747 // wait for sends and receives to finish
1748 halo_recv_reqs.wait_all();
1749 halo_send_reqs.wait_all();
1750
1751 // ensure that the halo sizes are consistent between neighboring processes
1752 for(std::size_t i(0); i < num_halos; ++i)
1753 {
1754 for(int j(0); j <= shape_dim; ++j)
1755 {
1756 if(halo_recv_intsec_sizes[i][Index(j)] != halo_send_intsec_sizes[i][Index(j)])
1757 {
1758 String msg = "Inconsistent deslagged halo size between process ";
1759 msg += stringify(this->_comm.rank());
1760 msg += " and neighbor with layer rank ";
1761 msg += stringify(halo_ranks[i]);
1762
1763 // abort execution
1764 XABORTM(msg.c_str());
1765 }
1766 }
1767 }
1768 }
1769 }
1770
1771 // virtual bool _parse_parti_type(const String& type) override
1772 // {
1773 // if(type.compare_no_case("extern") == 0)
1774 // return this->_allow_parti_extern = true;
1775 // // else if(type.compare_no_case("2level") == 0)
1776 // // return _allow_parti_2level = true;
1777 // else
1778 // return BaseClass::_parse_parti_type(type);
1779 // }
1780
1781 Adjacency::Coloring _compute_unstructured_coloring(const MeshNodeType& base_node) const
1782 {
1783 const auto& verts_at_elem = base_node.get_mesh()->template get_index_set<MeshType::shape_dim, 0>();
1784 Adjacency::Graph elems_at_vert(Adjacency::RenderType::transpose, verts_at_elem);
1785 Adjacency::Graph elems_at_elem(Adjacency::RenderType::injectify, verts_at_elem, elems_at_vert);
1786 return Adjacency::Coloring(elems_at_elem);
1787 }
1788
1789 Adjacency::Coloring _compute_unstructured_layering(const MeshNodeType& DOXY(base_node)) const
1790 {
1791 // ASSERTM(false, "Unstructured layering computation not implemented yet!");
1792#ifdef FEAT_DEBUG_MODE
1793 std::cout << "Unstructured Layering Computation not implemented yet!";
1794#endif
1795 return {};
1796 }
1797
1808 {
1809 // get element target set of slag patch
1810 const Geometry::MeshPart<MeshType>* slag_part = slag_node.get_patch(-1);
1811 XASSERT(slag_part != nullptr);
1812 const Geometry::TargetSet& slag_target = slag_part->template get_target_set<MeshType::shape_dim>();
1813
1814 // number of colors = 2^shape_dim
1815 const Index num_colors = Index(1) << MeshType::shape_dim;
1816 const Index num_elems = slag_target.get_num_entities();
1817
1818 // allocate coloring
1819 Adjacency::Coloring coloring(num_elems, num_colors);
1820 Index* colors = coloring.get_coloring();
1821
1822 // unrefined base mesh: structured numbering
1823 if constexpr(MeshType::shape_dim == 1)
1824 {
1825 for(Index i(0); i < num_elems; ++i)
1826 {
1827 colors[i] = slag_target[i] & 1ULL;
1828 }
1829 }
1830 else if constexpr(MeshType::shape_dim == 2)
1831 {
1832 for(Index i(0); i < num_elems; ++i)
1833 {
1834 Index iel = slag_target[i];
1835 Index ix = iel % this->_base_slices[0];
1836 Index iy = iel / this->_base_slices[0];
1837 colors[i] = (ix & 1ULL) | ((iy & 1ULL) << 1ULL);
1838 }
1839 }
1840 else if constexpr(MeshType::shape_dim == 3)
1841 {
1842 for(Index i(0); i < num_elems; ++i)
1843 {
1844 Index iel = slag_target[i];
1845 Index ix = iel % this->_base_slices[0];
1846 Index iy = (iel / this->_base_slices[0]) % this->_base_slices[1];
1847 Index iz = iel / (this->_base_slices[0] * this->_base_slices[1]);
1848 colors[i] = (ix & 1ULL) | ((iy & 1ULL) << 1ULL) | ((iz & 1ULL) << (1ULL << 1ULL));
1849 }
1850 }
1851
1852 return coloring;
1853 }
1854
1865 {
1866 // get element target set of slag patch
1867 const Geometry::MeshPart<MeshType>* slag_part = slag_node.get_patch(-1);
1868 XASSERT(slag_part != nullptr);
1869 const Geometry::TargetSet& slag_target = slag_part->template get_target_set<MeshType::shape_dim>();
1870
1871 // number of colors = 2^shape_dim
1872 const Index num_colors = Index(1) << MeshType::shape_dim;
1873 const Index num_elems = slag_target.get_num_entities();
1874
1875 // allocate coloring
1876 Adjacency::Coloring coloring(num_elems, num_colors);
1877 Index* colors = coloring.get_coloring();
1878
1879 // color of element i = i % num_colors
1880 for(Index i(0); i < num_elems; ++i)
1881 {
1882 colors[i] = slag_target[i] % num_colors;
1883 }
1884
1885 return coloring;
1886 }
1887
1904 const MeshNodeType& slag_node,
1905 const Adjacency::Coloring& parent_coloring,
1906 int child_rank) const
1907 {
1908 // get element target set of patch
1909 const Geometry::MeshPart<MeshType>* patch_part = slag_node.get_patch(child_rank);
1910 XASSERT(patch_part != nullptr);
1911 const Geometry::TargetSet& patch_target = patch_part->template get_target_set<MeshType::shape_dim>();
1912
1913 // number of colors = 2^shape_dim
1914 const Index num_colors = parent_coloring.get_num_colors();
1915 const Index num_elems = patch_target.get_num_entities();
1916
1917 // allocate coloring
1918 Adjacency::Coloring coloring(num_elems, num_colors);
1919 Index* colors = coloring.get_coloring();
1920
1921 // color of element i = i % num_colors
1922 for(Index i(0); i < num_elems; ++i)
1923 {
1924 colors[i] = parent_coloring[patch_target[i]];
1925 }
1926
1927 return coloring;
1928 }
1929
1940 {
1941 // get element target set of slag patch
1942 const Geometry::MeshPart<MeshType>* slag_part = slag_node.get_patch(-1);
1943 XASSERT(slag_part != nullptr);
1944 const Geometry::TargetSet& slag_target = slag_part->template get_target_set<MeshType::shape_dim>();
1945
1946 // maximum number of layers = number of slices in highest dimensions; this may be less than
1947 // base_slices[shape_dim], since entire layers might have been removed during the deslagging process
1948 const Index max_layers = this->_base_slices[shape_dim - 1];
1949 const Index num_elems = slag_target.get_num_entities();
1950
1951 // compute denominator for layer index computation
1952 Index denom = 1;
1953 for(int j(0); j + 1 < shape_dim; ++j)
1954 {
1955 denom *= this->_base_slices[Index(j)];
1956 }
1957
1958 // compute number of elements for each potential layer
1959 std::vector<Index> aux(max_layers, 0);
1960 for(Index i(0); i < num_elems; ++i)
1961 {
1962 ++aux[slag_target[i] / denom];
1963 }
1964
1965 // count number of actually used layers
1966 Index num_layers = 0;
1967 for(Index i(0); i < max_layers; ++i)
1968 {
1969 Index k = num_layers;
1970 if(aux[i] > 0)
1971 {
1972 ++num_layers;
1973 }
1974 aux[i] = k;
1975 }
1976
1977 // compute layering
1978 Adjacency::Coloring layering(num_elems, num_layers);
1979 Index* layers = layering.get_coloring();
1980 for(Index i(0); i < num_elems; ++i)
1981 {
1982 layers[i] = aux[slag_target[i] / denom];
1983 }
1984
1985 return layering;
1986 }
1987
1998 _compute_refined_mesh_layering(const MeshNodeType& slag_node, const Adjacency::Coloring& coarse_layering) const
1999 {
2001 {
2002 return {};
2003 }
2004 // get element target set of slag patch
2005 const Geometry::MeshPart<MeshType>* slag_part = slag_node.get_patch(-1);
2006 XASSERT(slag_part != nullptr);
2007 const Geometry::TargetSet& slag_target = slag_part->template get_target_set<MeshType::shape_dim>();
2008
2009 // maximum number of layers = twice the number of layers in coarse mesh; this may actually be less,
2010 // since entire layers might have been removed during the deslagging process
2011 const Index max_layers = 2 * coarse_layering.get_num_colors();
2012 const Index num_elems = slag_target.get_num_entities();
2013
2014 // compute number of elements for each potential layer
2015 std::vector<Index> aux(max_layers, 0);
2016 for(Index i(0); i < num_elems; ++i)
2017 {
2018 Index qux = slag_target[i] >> Index(shape_dim - 1);
2019 ++aux[(coarse_layering[qux >> 1ULL] << 1ULL) | (qux & 1ULL)]; // all hail to bit-shift magic!
2020 }
2021
2022 // count number of actually used layers
2023 Index num_layers = 0;
2024 for(Index i(0); i < max_layers; ++i)
2025 {
2026 Index k = num_layers;
2027 if(aux[i] > 0)
2028 {
2029 ++num_layers;
2030 }
2031 aux[i] = k;
2032 }
2033
2034 // compute layering
2035 Adjacency::Coloring layering(num_elems, num_layers);
2036 Index* layers = layering.get_coloring();
2037 for(Index i(0); i < num_elems; ++i)
2038 {
2039 Index qux = slag_target[i] >> Index(shape_dim - 1);
2040 layers[i] = aux[(coarse_layering[qux >> 1ULL] << 1ULL) | (qux & 1ULL)];
2041 }
2042
2043 return layering;
2044 }
2045
2062 const MeshNodeType& slag_node,
2063 const Adjacency::Coloring& parent_layering,
2064 int child_rank) const
2065 {
2067 {
2068 return {};
2069 }
2070
2071 // get element target set of patch
2072 const Geometry::MeshPart<MeshType>* patch_part = slag_node.get_patch(child_rank);
2073 XASSERT(patch_part != nullptr);
2074 const Geometry::TargetSet& patch_target = patch_part->template get_target_set<MeshType::shape_dim>();
2075
2076 // maximum number of layers = number of layers in parent layering; this may actually be less,
2077 // since entire layers might have been removed during the deslagging process
2078 const Index max_layers = parent_layering.get_num_colors();
2079 const Index num_elems = patch_target.get_num_entities();
2080
2081 // compute number of elements for each potential layer
2082 std::vector<Index> aux(max_layers, 0);
2083 for(Index i(0); i < num_elems; ++i)
2084 {
2085 ++aux[parent_layering[patch_target[i]]];
2086 }
2087
2088 // count number of actually used layers
2089 Index num_layers = 0;
2090 for(Index i(0); i < max_layers; ++i)
2091 {
2092 Index k = num_layers;
2093 if(aux[i] > 0)
2094 {
2095 ++num_layers;
2096 }
2097 aux[i] = k;
2098 }
2099
2100 // compute layering
2101 Adjacency::Coloring layering(num_elems, num_layers);
2102 Index* layers = layering.get_coloring();
2103 for(Index i(0); i < num_elems; ++i)
2104 {
2105 layers[i] = aux[parent_layering[patch_target[i]]];
2106 }
2107
2108 return layering;
2109 }
2110
2122 virtual void _create_single_process(std::unique_ptr<MeshNodeType> base_mesh_node)
2123 {
2124 // create and push single layer
2125 this->push_layer(std::make_shared<LayerType>(this->_comm.comm_dup(), 0));
2126
2127 // create single ancestry
2129 Ancestor& ancestor = this->_ancestry.front();
2130
2131 // use the whole base mesh here
2132 ancestor.parti_info = "Using base-mesh";
2133
2134 // save slagged base mesh node
2135 std::unique_ptr<MeshNodeType> base_slag_node = std::move(base_mesh_node);
2136 // deslag base mesh node
2137 base_mesh_node = this->_deslag_mesh_node(0, *base_slag_node);
2138
2139 // compute base-mesh layering
2140 Adjacency::Coloring base_layering = _unstructured_mesh ? this->_compute_unstructured_layering(*base_mesh_node)
2141 : this->_compute_base_mesh_layering(*base_slag_node);
2142
2143 // refine and deslag up to desired minimum level
2144 int lvl = 0;
2145 for(; lvl < ancestor.desired_level_min; ++lvl)
2146 {
2147 base_slag_node = base_mesh_node->refine_unique(this->_adapt_mode);
2148 base_mesh_node = this->_deslag_mesh_node(lvl + 1, *base_slag_node);
2149 base_layering = _unstructured_mesh ? this->_compute_unstructured_layering(*base_mesh_node)
2150 : this->_compute_refined_mesh_layering(*base_slag_node, base_layering);
2151 }
2152
2153 // compute coloring for the coarse level
2154 Adjacency::Coloring base_coloring;
2156 {
2157 base_coloring = this->_compute_unstructured_coloring(*base_mesh_node);
2158 }
2159 else
2160 {
2161 if(lvl == 0)
2162 {
2163 base_coloring = this->_compute_base_mesh_coloring(*base_slag_node);
2164 }
2165 else
2166 {
2167 base_coloring = this->_compute_refined_mesh_coloring(*base_slag_node);
2168 }
2169 }
2170
2171 // save chosen minimum level if it is not equal to the desired maximum level
2172 if(lvl < ancestor.desired_level_max)
2173 {
2174 this->_chosen_levels.push_front(std::make_pair(lvl, 0));
2175 }
2176
2177 // refine up to maximum level and push to control
2178 for(; lvl < ancestor.desired_level_max; ++lvl)
2179 {
2180 // refine the base mesh
2181 auto refined_node = base_mesh_node->refine_unique(this->_adapt_mode);
2182
2183 // push this level
2184 std::shared_ptr<LevelType> level_ptr = std::make_shared<LevelType>(
2185 lvl,
2186 std::move(base_mesh_node),
2187 std::move(base_slag_node),
2188 std::move(base_coloring),
2189 std::move(base_layering));
2190 this->push_level_front(0, level_ptr);
2191
2192 // continue with refined node
2193 base_slag_node = std::move(refined_node);
2194 base_mesh_node = this->_deslag_mesh_node(lvl + 1, *base_slag_node);
2195 base_coloring = _unstructured_mesh ? this->_compute_unstructured_coloring(*base_mesh_node)
2196 : this->_compute_refined_mesh_coloring(*base_slag_node);
2197 base_layering = _unstructured_mesh
2198 ? this->_compute_unstructured_layering(*base_mesh_node)
2199 : this->_compute_refined_mesh_layering(*base_slag_node, level_ptr->element_layering);
2200 }
2201
2202 // save chosen maximum level
2203 this->_chosen_levels.push_front(std::make_pair(lvl, 1));
2204
2205 // push finest level
2206 this->push_level_front(
2207 0,
2208 std::make_shared<LevelType>(
2209 lvl,
2210 std::move(base_mesh_node),
2211 std::move(base_slag_node),
2212 std::move(base_coloring),
2213 std::move(base_layering)));
2214 }
2215
2216 // Note: all following member functions are only required for parallel builds,
2217 // so we enclose them in the following #if-block to reduce compile times.
2218
2219#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
2220
2227 virtual void _create_single_layered(std::unique_ptr<MeshNodeType> base_mesh_node)
2228 {
2229 // create and push single layer
2230 std::shared_ptr<LayerType> layer = std::make_shared<LayerType>(this->_comm.comm_dup(), 0);
2231 this->push_layer(layer);
2232
2233 // create single-layered ancestry
2235 Ancestor& ancestor = this->_ancestry.front();
2236
2237 XASSERTM(!this->_keep_base_levels, "SlaggedDomainControl cannot keep base levels!");
2238
2239 // save slagged base mesh node
2240 std::unique_ptr<MeshNodeType> base_slag_node = std::move(base_mesh_node);
2241
2242 // deslag base mesh node
2243 base_mesh_node = this->_deslag_mesh_node(0, *base_slag_node);
2244 Adjacency::Coloring base_coloring;
2245 Adjacency::Coloring base_layering = _unstructured_mesh ? this->_compute_unstructured_layering(*base_mesh_node)
2246 : this->_compute_base_mesh_layering(*base_slag_node);
2247
2248 // refine up to chosen partition level
2249 int lvl = 0;
2250 for(; lvl <= ancestor.desired_level_max; ++lvl)
2251 {
2252 // can we apply a partitioner?
2253 bool enough_elems =
2254 (base_mesh_node->get_mesh()->get_num_elements() >=
2255 Index(this->_required_elems_per_rank) * Index(ancestor.num_procs));
2256 if((lvl >= ancestor.parti_level) && enough_elems && this->_apply_parti(ancestor, *base_mesh_node))
2257 {
2258 break;
2259 }
2260
2261 // no partitioning found?
2262 if(lvl >= ancestor.desired_level_max)
2263 {
2264 break;
2265 }
2266
2267 // refine and deslag base mesh node
2268 base_slag_node = base_mesh_node->refine_unique(this->_adapt_mode);
2269 base_mesh_node = this->_deslag_mesh_node(lvl + 1, *base_slag_node);
2270 base_coloring = _unstructured_mesh ? this->_compute_unstructured_coloring(*base_mesh_node)
2271 : this->_compute_refined_mesh_coloring(*base_slag_node);
2272 base_layering = _unstructured_mesh ? this->_compute_unstructured_layering(*base_mesh_node)
2273 : this->_compute_refined_mesh_layering(*base_slag_node, base_layering);
2274 }
2275
2276 // no valid partitioning found?
2277 XASSERTM(ancestor.parti_found, "SlaggedDomainControl failed to find a valid partitioning");
2278
2279 // set the selected partitioner level
2280 ancestor.parti_level = lvl;
2281
2282 // compute coloring if we're still on level 0; otherwise the coloring has been computed in the loop above
2283 if(lvl == 0)
2284 {
2285 base_coloring = _unstructured_mesh ? this->_compute_unstructured_coloring(*base_mesh_node)
2286 : this->_compute_refined_mesh_coloring(*base_slag_node);
2287 }
2288
2289 // extract our patch
2290 std::vector<int> neighbor_ranks;
2291 std::unique_ptr<MeshNodeType> patch_mesh_node(
2292 base_mesh_node->extract_patch(neighbor_ranks, ancestor.parti_graph, this->_comm.rank()));
2293
2294 // extract our coloring (only if we need it on this level)
2295 Adjacency::Coloring patch_coloring;
2296 if(lvl == ancestor.desired_level_min)
2297 {
2298 patch_coloring = this->_extract_patch_coloring(*base_mesh_node, base_coloring, this->_comm.rank());
2299 }
2300
2301 // extract layering
2302 Adjacency::Coloring patch_layering =
2303 this->_extract_patch_layering(*base_mesh_node, base_layering, this->_comm.rank());
2304
2305 // set the neighbor ranks of our child layer
2306 layer->set_neighbor_ranks(neighbor_ranks);
2307
2308 // create an empty patch slag node
2309 std::unique_ptr<MeshNodeType> patch_slag_node;
2310
2311 // refine up to minimum level
2312 for(; lvl < ancestor.desired_level_min; ++lvl)
2313 {
2314 // refine the patch mesh
2315 patch_slag_node = patch_mesh_node->refine_unique(this->_adapt_mode);
2316 patch_mesh_node = this->_deslag_mesh_node(lvl + 1, *patch_slag_node, ancestor, false);
2317 patch_layering = _unstructured_mesh ? this->_compute_unstructured_layering(*patch_mesh_node)
2318 : this->_compute_refined_mesh_layering(*patch_slag_node, patch_layering);
2319 }
2320
2321 // compute coloring if we don't have one yet
2322 if(patch_coloring.empty())
2323 {
2324 patch_coloring = _unstructured_mesh ? this->_compute_unstructured_coloring(*patch_mesh_node)
2325 : this->_compute_refined_mesh_coloring(*patch_slag_node);
2326 }
2327
2328 // save chosen minimum level
2329 if(lvl < ancestor.desired_level_max)
2330 {
2331 this->_chosen_levels.push_front(std::make_pair(lvl, 0));
2332 }
2333
2334 // refine up to maximum level
2335 for(; lvl < ancestor.desired_level_max; ++lvl)
2336 {
2337 // refine the patch mesh
2338 auto refined_node = patch_mesh_node->refine_unique(this->_adapt_mode);
2339
2340 // create new level
2341 std::shared_ptr<LevelType> level_ptr = std::make_shared<LevelType>(
2342 lvl,
2343 std::move(patch_mesh_node),
2344 std::move(patch_slag_node),
2345 std::move(patch_coloring),
2346 std::move(patch_layering));
2347
2348 // push this (unrefined) level
2349 this->push_level_front(0, level_ptr);
2350
2351 // continue with refined node
2352 patch_slag_node = std::move(refined_node);
2353 patch_mesh_node = this->_deslag_mesh_node(lvl + 1, *patch_slag_node, ancestor, false);
2354 patch_coloring = _unstructured_mesh ? this->_compute_unstructured_coloring(*patch_mesh_node)
2355 : this->_compute_refined_mesh_coloring(*patch_slag_node);
2356 patch_layering = _unstructured_mesh
2357 ? this->_compute_unstructured_layering(*patch_mesh_node)
2358 : this->_compute_refined_mesh_layering(*patch_slag_node, level_ptr->element_layering);
2359 }
2360
2361 // save chosen maximum level
2362 this->_chosen_levels.push_front(std::make_pair(lvl, this->_comm.size()));
2363
2364 // push finest level
2365 this->push_level_front(
2366 0,
2367 std::make_shared<LevelType>(
2368 lvl,
2369 std::move(patch_mesh_node),
2370 std::move(patch_slag_node),
2371 std::move(patch_coloring),
2372 std::move(patch_layering)));
2373 }
2374
2381 virtual void _create_multi_layered(std::unique_ptr<MeshNodeType> base_mesh_node)
2382 {
2383 // create layers
2385
2386 // create ancestry
2388
2389 XASSERTM(!this->_keep_base_levels, "SlaggedDomainControl cannot keep base levels!");
2390
2391 // we start counting at level 0
2392 int lvl = 0;
2393
2394 // deslag the base-mesh node and move pointer to a new parent-mesh pointer;
2395 // the base-mesh is always the one on the layer with only 1 process
2396 std::unique_ptr<MeshNodeType> parent_slag_node;
2397 std::unique_ptr<MeshNodeType> parent_mesh_node = this->_deslag_mesh_node(lvl, *base_mesh_node);
2398
2399 // create the coloring and layering for the unrefined base mesh
2400 Adjacency::Coloring parent_coloring = _unstructured_mesh ? this->_compute_unstructured_coloring(*parent_mesh_node)
2401 : this->_compute_base_mesh_coloring(*base_mesh_node);
2402 Adjacency::Coloring parent_layering = _unstructured_mesh ? this->_compute_unstructured_layering(*parent_mesh_node)
2403 : this->_compute_base_mesh_layering(*base_mesh_node);
2404
2405 base_mesh_node.reset();
2406
2407 // loop over all global layers in reverse order (coarse to fine)
2408 for(std::size_t slayer = this->_ancestry.size(); slayer > std::size_t(0);)
2409 {
2410 // is this the base-mesh layer aka the 1-process layer?
2411 const bool is_base_layer = (slayer == this->_ancestry.size());
2412
2413 --slayer;
2414
2415 // get the ancestor object
2416 Ancestor& ancestor = this->_ancestry.at(slayer);
2417
2418 // determine the minimum desired level of our parent layer
2419 int parent_min_lvl = -1;
2420 if(!is_base_layer)
2421 {
2422 parent_min_lvl = this->_chosen_levels.front().first;
2423 }
2424 else if(this->_ancestry.size() + 1 < this->_desired_levels.size())
2425 {
2426 parent_min_lvl = this->_desired_levels.back().first;
2427 }
2428
2429 // check available partitioning strategies
2430 this->_check_parti(ancestor, *parent_mesh_node, is_base_layer);
2431
2432 // the check_parti function returns the partitioning level w.r.t. the current
2433 // level (which may be > 0), so we have to compensate that by adding our current level:
2434 // note: contrary to the standard parti domain control, we calculate our partition always to the level
2435 // we split to the subdomains, i.e. on the level we actually change our layer
2436 // due to this, we enforce, that we at least win 1 real level to the parent
2437 ancestor.parti_level += lvl;
2438 ancestor.parti_level = Math::max(ancestor.parti_level, ancestor.desired_level_min);
2439 ancestor.parti_level = Math::max(ancestor.parti_level, parent_min_lvl + int(!is_base_layer));
2440
2441 // Note: each progeny group within the main communicator may have chosen a different
2442 // partitioning level at this point. We will compensate this by adjusting the minimum
2443 // refinement level of the child layer after the partitioning step below.
2444
2445 // refine up to the chosen partitioning level
2446 // for(; lvl < ancestor.parti_level; ++lvl)
2447 for(; lvl <= ancestor.desired_level_max; ++lvl)
2448 {
2449 // can we apply a partitioner?
2450 if(lvl >= ancestor.parti_level)
2451 {
2452 // try to apply the partitioner
2453 this->_apply_parti(ancestor, *parent_mesh_node);
2454
2455 // partitioning successful?
2456 int parti_ok = ancestor.parti_found ? 1 : 0;
2457
2458 // check if all processes found a valid partitioning
2459 this->_comm.allreduce(&parti_ok, &parti_ok, std::size_t(1), Dist::op_min);
2460 if(parti_ok > 0)
2461 {
2462 break;
2463 }
2464
2465 // nope, at least one process did not receive a partition, so keep refining
2466 ancestor.parti_found = false;
2467 }
2468
2469 // no partitioning found?
2470 if(lvl >= ancestor.desired_level_max)
2471 {
2472 break;
2473 }
2474
2475 // refine and deslag the parent mesh node
2476 auto refined_node = parent_mesh_node->refine_unique(this->_adapt_mode);
2477
2478 // a level pointer for the unrefined level; we need this to access the parent layering
2479 std::shared_ptr<LevelType> level_ptr;
2480
2481 // push the base mesh into our parent layer if desired
2482 if((ancestor.layer_p >= 0) && (parent_min_lvl >= 0) && (lvl >= parent_min_lvl))
2483 {
2484 level_ptr = std::make_shared<LevelType>(
2485 lvl,
2486 std::move(parent_mesh_node),
2487 std::move(parent_slag_node),
2488 std::move(parent_coloring),
2489 std::move(parent_layering));
2490 this->push_level_front(ancestor.layer_p, level_ptr);
2491 }
2492
2493 // continue with refined node
2494 parent_slag_node = std::move(refined_node);
2495 parent_mesh_node = this->_deslag_mesh_node(lvl + 1, *parent_slag_node, ancestor, !is_base_layer);
2496 parent_coloring = _unstructured_mesh ? this->_compute_unstructured_coloring(*parent_mesh_node)
2497 : this->_compute_refined_mesh_coloring(*parent_slag_node);
2498 parent_layering = _unstructured_mesh ? this->_compute_unstructured_layering(*parent_mesh_node)
2500 *parent_slag_node,
2501 level_ptr ? level_ptr->element_layering : parent_layering);
2502 }
2503
2504 // no valid partitioning found?
2505 XASSERTM(ancestor.parti_found, "SlaggedDomainControl failed to find a valid partitioning");
2506
2507 // set the selected partitioner level
2508 ancestor.parti_level = lvl;
2509
2510 // extract our patch
2511 std::vector<int> neighbor_ranks;
2512 std::unique_ptr<MeshNodeType> patch_mesh_node(
2513 parent_mesh_node->extract_patch(neighbor_ranks, ancestor.parti_graph, ancestor.progeny_child));
2514
2515 // extract our coloring
2516 Adjacency::Coloring patch_coloring =
2517 this->_extract_patch_coloring(*parent_mesh_node, parent_coloring, ancestor.progeny_child);
2518
2519 // extract our layering
2520 Adjacency::Coloring patch_layering =
2521 this->_extract_patch_layering(*parent_mesh_node, parent_layering, ancestor.progeny_child);
2522
2523 // create an empty patch slag node
2524 std::unique_ptr<MeshNodeType> patch_slag_node;
2525
2526 // translate neighbor ranks by progeny group to obtain the neighbor ranks
2527 // w.r.t. this layer's communicator
2528 {
2529 std::map<int, int> halo_map;
2530 for(auto& i : neighbor_ranks)
2531 {
2532 int old_i(i);
2533 halo_map.emplace(old_i, i += ancestor.progeny_group);
2534 }
2535 patch_mesh_node->rename_halos(halo_map);
2536 }
2537
2538 // does this process participate in the parent layer or do we need to keep the base-meshes anyways?
2539 if(ancestor.layer_p >= 0)
2540 {
2541 // Note: patch mesh-part for rank = 0 was already created by 'extract_patch' call
2542 for(int i(1); i < ancestor.num_parts; ++i)
2543 {
2544 parent_mesh_node->create_patch_meshpart(ancestor.parti_graph, i);
2545 }
2546 }
2547
2548 // make sure we choose the same minimum level for all processes, because we may
2549 // have chosen different partitioning levels for each patch
2550 int global_level_min = Math::max(ancestor.desired_level_min, ancestor.parti_level);
2551 this->_comm.allreduce(&global_level_min, &global_level_min, std::size_t(1), Dist::op_max);
2552
2553 // make sure our minimum level is greater than the minimum level of the previous layer,
2554 // because each layer must contain at least one non-ghost level
2555 if(!is_base_layer)
2556 {
2557 global_level_min = Math::max(global_level_min, this->_chosen_levels.front().first + 1);
2558 }
2559
2560 // refine up to desired minimum level of this layer
2561 XASSERTM(lvl == global_level_min, "INTERNAL ERROR");
2562 /*for(; lvl < global_level_min; ++lvl)
2563 {
2564 // refine the base mesh node
2565 std::unique_ptr<MeshNodeType> coarse_slag_node(std::move(parent_slag_node));
2566 std::unique_ptr<MeshNodeType> coarse_mesh_node(std::move(parent_mesh_node));
2567 parent_slag_node = coarse_mesh_node->refine_unique(this->_adapt_mode);
2568 parent_mesh_node = this->_deslag_mesh_node(*parent_slag_node);
2569
2570 // push base mesh to parent layer if desired
2571 if((ancestor.layer_p >= 0) && (parent_min_lvl >= 0) && (lvl >= parent_min_lvl))
2572 {
2573 // clear patches before pushing this node as they are redundant here
2574 //coarse_mesh_node->clear_patches();
2575 this->push_level_front(ancestor.layer_p, std::make_shared<LevelType>(lvl, std::move(coarse_mesh_node),
2576 std::move(coarse_slag_node)));
2577 }
2578
2579 // refine the patch mesh
2580 //patch_slag_node = patch_mesh_node->refine_unique(this->_adapt_mode);
2581 //patch_mesh_node = this->_deslag_mesh_node(*patch_slag_node);
2582 patch_mesh_node = this->_deslag_mesh_node(*patch_mesh_node->refine_unique(this->_adapt_mode));
2583 }*/
2584
2585 // split the halos of our base-mesh and compute the halos of our patches from that
2586 // this->_split_basemesh_halos(ancestor, *parent_slag_node, *patch_slag_node, neighbor_ranks);
2587 this->_split_basemesh_halos(ancestor, *parent_mesh_node, *patch_mesh_node, neighbor_ranks);
2588
2589 // does this process participate in the child layer?
2590 if(ancestor.layer >= 0)
2591 {
2592 // set the neighbor ranks in our child layer
2593 this->_layers.at(std::size_t(ancestor.layer))->set_neighbor_ranks(neighbor_ranks);
2594 }
2595
2596 // set chosen minimum level for this layer
2597 if(!is_base_layer)
2598 {
2599 this->_chosen_levels.push_front(std::make_pair(lvl, this->_ancestry.at(slayer + 1).num_procs));
2600 }
2601 else if(parent_min_lvl < 0)
2602 {
2603 this->_chosen_levels.push_front(std::make_pair(lvl, 0));
2604 }
2605 else
2606 {
2607 this->_chosen_levels.push_front(std::make_pair(parent_min_lvl, 0));
2608 this->_chosen_levels.push_front(std::make_pair(lvl, 1));
2609 }
2610
2611 // a level pointer for the unrefined level; we need this to access the parent layering
2612 std::shared_ptr<LevelType> level_ptr;
2613
2614 // push the finest base-mesh
2615 if(ancestor.layer_p >= 0)
2616 {
2617 this->push_level_front(
2618 ancestor.layer_p,
2619 std::make_shared<LevelType>(
2620 lvl,
2621 std::move(parent_mesh_node),
2622 std::move(parent_slag_node),
2623 std::move(parent_coloring),
2624 std::move(parent_layering)));
2625 }
2626
2627 // continue with the next layer
2628 parent_slag_node = std::move(patch_slag_node);
2629 parent_mesh_node = std::move(patch_mesh_node);
2630 parent_coloring = std::move(patch_coloring);
2631 parent_layering = std::move(patch_layering);
2632 }
2633
2634 // get the desired maximum level
2635 // if we have more than one layer, make sure that the finest one contains at
2636 // least one level, as otherwise the finest global level would be a ghost level
2637 int desired_level_max = Math::max(this->_ancestry.front().desired_level_max, lvl + 1);
2638
2639 for(; lvl < desired_level_max; ++lvl)
2640 {
2641 // refine the patch mesh
2642 auto refined_node = parent_mesh_node->refine_unique(this->_adapt_mode);
2643
2644 // a level pointer for the unrefined level; we need this to access the parent layering
2645 std::shared_ptr<LevelType> level_ptr = std::make_shared<LevelType>(
2646 lvl,
2647 std::move(parent_mesh_node),
2648 std::move(parent_slag_node),
2649 std::move(parent_coloring),
2650 std::move(parent_layering));
2651
2652 // push patch mesh to this level
2653 this->push_level_front(0, level_ptr);
2654
2655 // continue with refined node
2656 parent_slag_node = std::move(refined_node);
2657 parent_mesh_node = this->_deslag_mesh_node(lvl + 1, *parent_slag_node, this->_ancestry.front(), false);
2658 parent_coloring = _unstructured_mesh ? this->_compute_unstructured_coloring(*parent_mesh_node)
2659 : this->_compute_refined_mesh_coloring(*parent_slag_node);
2660 parent_layering = _unstructured_mesh
2661 ? this->_compute_unstructured_layering(*parent_mesh_node)
2662 : this->_compute_refined_mesh_layering(*parent_slag_node, level_ptr->element_layering);
2663 }
2664
2665 // set chosen maximum level for finest layer
2666 this->_chosen_levels.push_front(std::make_pair(lvl, this->_comm.size()));
2667
2668 // push finest level
2669 this->push_level_front(
2670 0,
2671 std::make_shared<LevelType>(
2672 lvl,
2673 std::move(parent_mesh_node),
2674 std::move(parent_slag_node),
2675 std::move(parent_coloring),
2676 std::move(parent_layering)));
2677 }
2678
2679#endif // defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
2680 }; // class SlaggedDomainControl<...>
2681} // namespace FEAT::Control::Domain
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#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.
Coloring object implementation.
Definition: coloring.hpp:30
bool empty() const
Checks whether the coloring is empty.
Definition: coloring.hpp:216
Index * get_coloring()
Returns the coloring array.
Definition: coloring.hpp:168
Index get_num_colors() const
Returns the number of colors.
Definition: coloring.hpp:210
Adjacency Graph implementation.
Definition: graph.hpp:34
Deslagging predicate based on a CGALWrapper.
CGALDeslaggingPredicate(const FEAT::String &file, Geometry::CGALFileMode file_mode)
Construct from filename.
CGALDeslaggingPredicate(const Dist::Comm &comm, const FEAT::String &file, Geometry::CGALFileMode file_mode)
Construct from filename.
void cheap_weight_calculation(bool enable)
Enable or disable cheap weight calculation.
CGALDeslaggingPredicate(std::unique_ptr< CGALWrapperType > &&cgal_wrapper)
Construct from existing wrapper.
std::vector< WeightType > gather_element_weights(const MeshType &mesh) const override
Gathers the element slag weights for a given mesh.
std::vector< Index > gather_elements(const MeshType &mesh, const Dist::Comm &comm) const override
Gather a vector of element indices that intersect the domain.
const CGALWrapperType & get_cgal_wrapper() const
const CGALWrapper accessor
std::vector< Index > gather_elements(const MeshType &mesh) const override
Gather a vector of element indices that intersect the domain.
CGALWrapperType & get_cgal_wrapper()
CGALWrapper accessor.
Hierarchical partitioned Slagged Domain Control.
DeslaggedDomainControl & operator=(DeslaggedDomainControl &&other)=default
Move-assignment.
const StopWatch & get_watch_hierarchy() const
Returns a const reference to the StopWatch that measures the base-mesh creation phase.
Adjacency::Coloring _compute_base_mesh_layering(const MeshNodeType &slag_node) const
Computes the element layering for the unpartitioned base mesh.
const StopWatch & get_watch_base_mesh() const
Returns a const reference to the StopWatch that measures the base-mesh creation phase.
virtual void create(Geometry::MeshFileReader &)
Creates a domain control from a MeshFileReader.
Adjacency::Coloring _compute_refined_mesh_layering(const MeshNodeType &slag_node, const Adjacency::Coloring &coarse_layering) const
Computes the layering for a refined mesh.
Adjacency::Coloring _compute_base_mesh_coloring(const MeshNodeType &slag_node) const
Computes the coloring for the unpartitioned base mesh.
virtual std::unique_ptr< MeshNodeType > _deslag_mesh_node(int lvl, MeshNodeType &mesh_node, const Ancestor &ancestor, bool is_child)
Deslaggs a (potentially partitioned) mesh node including its mesh-parts and halos.
std::vector< Index > _gather_masked_elements(const MeshType &mesh, const Dist::Comm &sibling_comm) const
Gather vector of elements that intersect the masked domain, synced across ranks.
DeslaggingPredicateType & get_deslagging_predicate()
Returns a reference to the internal slagging predicate.
virtual void create(const std::deque< String > &filenames, const String &dirpath="")
Creates a domain control from a list of filenames.
VertexType _bbox_min
the bounding box of the structured domain
int _deslag_lvl_min
Lowest level to deslag on. Inclusive.
DeslaggedDomainControl & operator=(const DeslaggedDomainControl &other)=delete
Deleted copy-assignment.
Adjacency::Coloring _extract_patch_coloring(const MeshNodeType &slag_node, const Adjacency::Coloring &parent_coloring, int child_rank) const
Extracts a patch coloring from the parent mesh coloring.
virtual void _deslag_patch_halos(MeshNodeType &patch_mesh_node, const MeshNodeType &base_mesh_node, const Ancestor &ancestor, bool is_child)
Deslags the halos of a patch mesh node.
bool _keep_deslagging_predicate
specifies whether to keep the slagging predicate after hierarchy creation
Geometry::RootMeshNode< MeshType > MeshNodeType
our root mesh node type
MeshNodeType & create_base_mesh_3d(Index num_x, Index num_y, Index num_z, CoordType x_min, CoordType x_max, CoordType y_min, CoordType y_max, CoordType z_min, CoordType z_max)
Creates a 3D structured cuboid base mesh and returns its base-mesh node.
typename MeshType::CoordType CoordType
coordinate type
int _deslag_lvl_max
Highest level to deslag on. Inclusive.
Adjacency::Coloring _extract_patch_layering(const MeshNodeType &slag_node, const Adjacency::Coloring &parent_layering, int child_rank) const
Extracts a patch layering from the parent mesh layering.
String dump_slag_layer_levels() const
Debugging function: Returns a string containing encoded slag layer level information.
DeslaggedDomainControl(const DeslaggedDomainControl &other)=delete
Deleted copy constructor.
virtual void _create_single_layered(std::unique_ptr< MeshNodeType > base_mesh_node)
Creates a single-layered mesh hierarchy.
DeslaggingPredicateBase< MeshType, WeightType > DeslaggingPredicateType
deslagging predicate type
void create_hierarchy()
Creates the domain level hierarchy.
StopWatch _watch_create_base_mesh
a bunch of stop-watches
void set_deslag_level_range(int min, int max)
Set the deslag level range.
const StopWatch & get_watch_voxel_map() const
Returns a const reference to the StopWatch that measures the voxel-map creation phase.
virtual void create(std::unique_ptr< MeshNodeType > &)
Creates a domain control from a base-mesh node.
void set_deslagging_predicate(std::unique_ptr< DeslaggingPredicateType > &&pred)
Setter for deslagging predicate.
virtual std::unique_ptr< MeshNodeType > _deslag_mesh_node(int lvl, MeshNodeType &mesh_node)
Deslags an unpartitioned mesh node including its mesh-parts.
virtual ~DeslaggedDomainControl()=default
virtual destructor
virtual void _create_single_process(std::unique_ptr< MeshNodeType > base_mesh_node)
Creates a single-layered mesh hierarchy for a single process.
virtual void _create_multi_layered(std::unique_ptr< MeshNodeType > base_mesh_node)
Creates a multi-layered mesh hierarchy.
const DeslaggingPredicateType & get_deslagging_predicate() const
Returns a const reference to the internal slagging predicate.
DeslaggedDomainControl(DeslaggedDomainControl &&other)=default
Move-constructor.
bool _unstructured_mesh
specifies whether we use a non voxel base mesh
std::unique_ptr< DeslaggingPredicateType > _deslagging_predicate
Predicate for deciding which parts of a mesh to keep and which to discard.
std::vector< WeightType > gather_element_weights(Index level) const
Gathers the element weights on a given domain level.
std::vector< Index > _gather_masked_elements(const MeshType &mesh)
Gather vector of elements that intersect the masked domain.
DeslaggedDomainControl(const Dist::Comm &comm_, bool support_multi_layered)
Constructor.
std::array< Index, shape_dim > _base_slices
number of element slices on base mesh level 0
Adjacency::Coloring _compute_refined_mesh_coloring(const MeshNodeType &slag_node) const
Computes the coloring for a refined mesh.
MeshNodeType & create_base_mesh_2d(Index num_x, Index num_y, CoordType x_min, CoordType x_max, CoordType y_min, CoordType y_max)
Creates a 2D structured rectangle base mesh and returns its base-mesh node.
std::unique_ptr< MeshNodeType > _base_mesh_node
the input base-mesh node on level 0
void keep_deslagging_predicate()
Instructs the domain controller to keep the slagging predicate after hierarchy creation.
typename MeshType::VertexType VertexType
vertex type
Wrapper class for domain levels for SlaggingDomainControl.
std::shared_ptr< DomainLevel_ > slag_level
attachable slag level
Adjacency::Coloring element_layering
element layering (stored as coloring)
DomainLevel_ BaseClass
our base-class; the actual domain level
Abstract base class for deslagging predicates.
virtual std::vector< Index > gather_elements(const MeshType &mesh, const Dist::Comm &comm) const =0
Gather a vector of element indices that intersect the domain.
virtual std::vector< Index > gather_elements(const MeshType &mesh) const =0
Gather a vector of element indices that intersect the domain.
virtual std::vector< WeightType > gather_element_weights(const MeshType &mesh) const =0
Gathers the element slag weights for a given mesh.
bool _keep_base_levels
keep base-mesh levels on root process?
int desired_level_max
the desired minimum and maximum refinement levels for this layer
int parti_level
the refinement level on which the patch is to be partitioned
String parti_info
a string containing some information about the chosen partitioning
bool parti_found
specifies whether a partitioning was found
int num_procs
the number of processes that participate in this layer
Adjacency::Graph parti_graph
this is the actual elements-at-rank partitioning graph
int layer_p
the index of the parent layer or -1, if this process is not part of the parent layer
int layer
the index of the layer that this ancestor object belongs to
int num_parts
the number of partitions for each patch of the parent layer
int progeny_child
Which direct child this process belongs to in its progeny group.
Base-Class for Hierarchical Domain Control implementations.
std::deque< std::pair< int, int > > _desired_levels
desired level deque
int _required_elems_per_rank
required number of elements per rank for a-posteriori partitioning
std::deque< std::pair< int, int > > _chosen_levels
chosen level deque
virtual void _create_ancestry_scattered()
Creates the layers for a multi-layered domain control in a scattered fashion.
Real WeightType
weight type for partitioner element weights; always Real
std::deque< Ancestor > _ancestry
the partition ancestry deque
Geometry::RootMeshNode< MeshType > MeshNodeType
our root mesh node type
LevelType::PartType MeshPartType
our mesh-part type
virtual void _create_ancestry_single()
Creates the ancestry for a single layer (or a single process)
Geometry::AdaptMode _adapt_mode
the adapt mode for refinement
bool _was_created
specifies whether the domain control was already created
virtual bool _check_parti(Ancestor &ancestor, const MeshNodeType &mesh_node, bool is_base_layer)
Checks for an appropriate partitioning strategy.
bool _support_multi_layered
support multi-layered hierarchy?
virtual bool _apply_parti(Ancestor &ancestor, MeshNodeType &base_mesh_node)
Applies an a-posteriori partitioner.
virtual void _create_multi_layers_scattered()
Creates the layers for a multi-layered domain control in a scattered fashion.
virtual void _split_basemesh_halos(const Ancestor &ancestor, const MeshNodeType &base_mesh_node, MeshNodeType &patch_mesh_node, std::vector< int > &neighbor_ranks)
Splits the base-mesh halos and computes the inter-patch-mesh halos.
void compute_from_off(const Dist::Comm &comm, std::istream &is, bool invert)
Computes the voxel map based on a surface triangulation stored in an OFF file.
std::vector< int > gather_vertex_voxel_map(const MeshType &mesh) const
Gathers the vertex voxel map on a given mesh.
VoxelMapDeslaggingPredicate(const VertexType &min, const VertexType &max, const Tiny::Vector< Index, dim > &slices, Index level)
Slice based constructor.
Geometry::VoxelMap & get_voxel_map()
VoxelMap accessor.
void set_out_of_bounds_value(bool value)
Sets the out-of-bounds value for the voxel map.
std::vector< WeightType > gather_element_weights(const MeshType &mesh) const override
std::vector< Index > gather_elements(const MeshType &mesh, const Dist::Comm &comm) const override
const Geometry::VoxelMap & get_voxel_map() const
VoxelMap accessor.
void compute_from_voxel_masker(const Dist::Comm &comm, Geometry::VoxelMasker< CoordType, dim > &masker)
Computes the voxel map based on a VoxelMasker object.
Geometry::VoxelMap::ReadResult read_voxel_map(const Dist::Comm &comm, const String &filename)
Reads the voxel map from a voxel map file.
VoxelMapDeslaggingPredicate(const VertexType &min, const VertexType &max, Real resolution)
Resolution based constructor.
void compute_from_off(const Dist::Comm &comm, const String &filename, bool invert)
Computes the voxel map based on a surface triangulation stored in an OFF file.
std::vector< Index > gather_elements(const MeshType &mesh) const override
Gather a vector of element indices that intersect the masked domain.
void compute_from_chart(const Dist::Comm &comm, const Geometry::Atlas::ChartBase< MeshType > &chart, bool invert)
Computes the voxel map based on a chart.
void compute_from_lambda(const Dist::Comm &comm, Lambda_ &&lambda)
Computes the voxel map based on a lambda expression.
Cubature Rule class template.
Definition: rule.hpp:38
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
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
Comm comm_dup() const
Creates a copy of this communicator.
Definition: dist.cpp:462
void allgatherv(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, const int *recvcounts, const int *displs, const Datatype &recvtype) const
Blocking gather-to-all.
Definition: dist.cpp:610
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
void wait_all()
Blocks until all active requests are fulfilled.
Definition: dist.cpp:327
static void read_common(std::stringstream &stream, const String &filename, const Dist::Comm &comm, int root_rank=0)
Reads a common text file for all ranks.
Wrapper for the CGAL Library.
Definition: cgal.hpp:173
MeshType * get_mesh()
Returns the mesh of this node.
Definition: mesh_node.hpp:225
Class template for partial meshes.
Definition: mesh_part.hpp:90
Base-Mesh Patch Halo splitter.
std::unique_ptr< RootMeshNode > extract_patch(std::vector< Index > &&elements, bool split_meshparts, bool split_halos, bool split_patches)
Extracts a patch from the root mesh as a new mesh node.
Definition: mesh_node.hpp:1154
const MeshPartType * get_patch(int rank) const
Returns a patch meshpart for a given child.
Definition: mesh_node.hpp:962
static std::unique_ptr< RootMeshNode > make_unique(std::unique_ptr< MeshType > mesh, MeshAtlasType *atlas=nullptr)
Creates a new RootMeshNode on the heap and returns a unique pointer to it.
Definition: mesh_node.hpp:837
const std::map< int, std::unique_ptr< MeshPartType > > & get_halo_map() const
Definition: mesh_node.hpp:896
void add_halo(int rank, std::unique_ptr< MeshPartType > halo_part)
Adds a halo mesh part to this mesh node.
Definition: mesh_node.hpp:874
Structured unit-cube mesh factory.
Target set class.
Definition: target_set.hpp:27
Index get_num_entities() const
Returns the number of entities.
Definition: target_set.hpp:92
helper class for read function result
Definition: voxel_map.hpp:502
Interface for voxel masker for the VoxelMap class.
Definition: voxel_map.hpp:71
Math Limits class template.
Definition: math.hpp:1475
static double toe_partition
time of partitioning in seconds, needs initialization
Definition: statistics.hpp:167
Stop-Watch class.
Definition: stop_watch.hpp:21
double elapsed() const
Returns the total elapsed time in seconds.
Definition: stop_watch.hpp:70
void start()
Starts the stop-watch.
Definition: stop_watch.hpp:43
void stop()
Stops the stop-watch and increments elapsed time.
Definition: stop_watch.hpp:51
String class implementation.
Definition: string.hpp:47
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
Definition: string.hpp:393
Tiny Vector class template.
Standard transformation mapping class template.
Definition: mapping.hpp:39
@ transpose
Render-Transpose mode.
@ injectify
Render-Injectified mode.
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
void minimax(T_ x, T_ &a, T_ &b)
Updates the minimum and maximum.
Definition: math.hpp:195
T_ min(T_ a, T_ b)
Returns the minimum of two values.
Definition: math.hpp:123
T_ max(T_ a, T_ b)
Returns the maximum of two values.
Definition: math.hpp:137
double Real
Real data type.
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
@ value
specifies whether the space should supply basis function values
int feat_omp_get_max_threads()
Returns the maximum number of omp threads.
Definition: omp_util.hpp:23
std::uint64_t Index
Index data type.
TrafoTags
Trafo configuration tags enum.
Definition: eval_tags.hpp:22
@ img_point
specifies whether the trafo should supply image point coordinates
@ dom_point
specifies whether the trafo should supply domain point coordinates
@ jac_det
specifies whether the trafo should supply jacobian determinants