34 template <
int dim,
int spacedim>
44 namespace fullydistributed
46 template <
int dim,
int spacedim>
52 const
unsigned int n_partitions) {
55 , currently_processing_create_triangulation_for_internal_usage(
false)
56 , currently_processing_prepare_coarsening_and_refinement_for_internal_usage(
62 template <
int dim,
int spacedim>
70 Assert(construction_data.comm == this->mpi_communicator,
71 ExcMessage(
"MPI communicators do not match!"));
74 settings = construction_data.settings;
79 this->set_mesh_smoothing(
81 typename ::Triangulation<dim, spacedim>::MeshSmoothing
>(
85 this->set_mesh_smoothing(
87 typename ::Triangulation<dim, spacedim>::MeshSmoothing
>(
90 this->set_mesh_smoothing(construction_data.smoothing);
93 this->coarse_cell_id_to_coarse_cell_index_vector.clear();
94 this->coarse_cell_index_to_coarse_cell_id_vector.clear();
97 if (construction_data.coarse_cell_vertices.empty())
100 currently_processing_create_triangulation_for_internal_usage =
true;
102 currently_processing_create_triangulation_for_internal_usage =
false;
105 auto cell = this->begin();
107 cell->set_level_subdomain_id(
112 this->coarse_cell_id_to_coarse_cell_index_vector.emplace_back(
114 this->coarse_cell_index_to_coarse_cell_id_vector.emplace_back(
120 this->coarse_cell_index_to_coarse_cell_id_vector =
121 construction_data.coarse_cell_index_to_coarse_cell_id;
124 std::map<types::coarse_cell_id, unsigned int>
125 coarse_cell_id_to_coarse_cell_index_vector;
126 for (
unsigned int i = 0;
127 i < construction_data.coarse_cell_index_to_coarse_cell_id.size();
129 coarse_cell_id_to_coarse_cell_index_vector
130 [construction_data.coarse_cell_index_to_coarse_cell_id[i]] = i;
132 for (
auto i : coarse_cell_id_to_coarse_cell_index_vector)
133 this->coarse_cell_id_to_coarse_cell_index_vector.emplace_back(i);
136 currently_processing_prepare_coarsening_and_refinement_for_internal_usage =
138 currently_processing_create_triangulation_for_internal_usage =
true;
141 currently_processing_prepare_coarsening_and_refinement_for_internal_usage =
143 currently_processing_create_triangulation_for_internal_usage =
false;
146 auto cell_infos = construction_data.cell_infos;
151 std::sort(cell_info.begin(),
158 const auto a_coarse_cell_index =
159 this->coarse_cell_id_to_coarse_cell_index(
161 const auto b_coarse_cell_index =
162 this->coarse_cell_id_to_coarse_cell_index(
170 if (a_coarse_cell_index != b_coarse_cell_index)
171 return a_coarse_cell_index < b_coarse_cell_index;
178 for (
const auto &cell : this->cell_iterators())
180 if (cell->is_active())
181 cell->set_subdomain_id(
184 cell->set_level_subdomain_id(
189 for (
unsigned int level = 0;
193 auto cell = this->begin(
level);
198 while (cell_info->id != cell->id().template to_binary<dim>())
202 if (cell->is_active())
203 cell->set_subdomain_id(cell_info->subdomain_id);
207 construct_multigrid_hierarchy)
208 cell->set_level_subdomain_id(cell_info->level_subdomain_id);
213 this->update_number_cache();
214 this->update_cell_relations();
219 template <
int dim,
int spacedim>
227 currently_processing_create_triangulation_for_internal_usage,
229 "You have called the method parallel::fullydistributed::Triangulation::create_triangulation() \n"
230 "that takes 3 arguments. If you have not called this function directly, \n"
231 "it might have been called via a function from the GridGenerator or GridIn \n"
232 "namespace. To be able to set up a fully-distributed Triangulation with these \n"
233 "utility functions nevertheless, please follow the following three steps:\n"
234 " 1) call the utility function for a (serial) Triangulation, \n"
235 " a parallel::shared::Triangulation, or a parallel::distributed::Triangulation object,\n"
236 " 2) use the functions TriangulationDescription::Utilities::create_description_from_triangulation() \n"
237 " or ::create_description_from_triangulation_in_groups() to create the \n"
238 " description of the local partition, and\n"
239 " 3) pass the created description to parallel::fullydistributed::Triangulation::create_triangulation()."));
248 template <
int dim,
int spacedim>
257 const ::Triangulation<dim, spacedim> *other_tria_ptr = &other_tria;
266 if (
dynamic_cast<const ::parallel::TriangulationBase<dim, spacedim>
267 *
>(&other_tria) ==
nullptr)
273 this->partitioner(serial_tria,
275 this->mpi_communicator));
278 if (this->is_multilevel_hierarchy_constructed())
282 other_tria_ptr = &serial_tria;
288 this->mpi_communicator,
292 this->create_triangulation(construction_data);
297 template <
int dim,
int spacedim>
301 const
unsigned int)> &partitioner,
304 this->partitioner = partitioner;
310 template <
int dim,
int spacedim>
316 this->partitioner_distributed = &partitioner;
322 template <
int dim,
int spacedim>
327 this->signals.pre_distributed_repartition();
333 this->partitioner_distributed->partition(*
this),
338 this->coarse_cell_id_to_coarse_cell_index_vector.clear();
339 this->coarse_cell_index_to_coarse_cell_id_vector.clear();
342 this->create_triangulation(construction_data);
345 this->signals.post_distributed_repartition();
350 template <
int dim,
int spacedim>
359 template <
int dim,
int spacedim>
364 currently_processing_prepare_coarsening_and_refinement_for_internal_usage,
365 ExcMessage(
"No coarsening and refinement is supported!"));
367 return ::Triangulation<dim, spacedim>::
368 prepare_coarsening_and_refinement();
373 template <
int dim,
int spacedim>
377 const std::size_t mem =
381 coarse_cell_id_to_coarse_cell_index_vector) +
383 coarse_cell_index_to_coarse_cell_id_vector);
389 template <
int dim,
int spacedim>
401 template <
int dim,
int spacedim>
404 coarse_cell_id_to_coarse_cell_index(
405 const
types::coarse_cell_id coarse_cell_id)
const
407 const auto coarse_cell_index = std::lower_bound(
408 coarse_cell_id_to_coarse_cell_index_vector.begin(),
409 coarse_cell_id_to_coarse_cell_index_vector.end(),
411 [](
const std::pair<types::coarse_cell_id, unsigned int> &pair,
413 Assert(coarse_cell_index !=
414 coarse_cell_id_to_coarse_cell_index_vector.cend(),
416 return coarse_cell_index->second;
421 template <
int dim,
int spacedim>
425 const unsigned int coarse_cell_index)
const
428 coarse_cell_index_to_coarse_cell_id_vector.size());
430 const auto coarse_cell_id =
431 coarse_cell_index_to_coarse_cell_id_vector[coarse_cell_index];
433 ExcMessage(
"You are trying to access a dummy cell!"));
434 return coarse_cell_id;
438 template <
int dim,
int spacedim>
443 this->local_cell_relations.
clear();
444 this->local_cell_relations.reserve(this->n_locally_owned_active_cells());
446 for (
const auto &cell : this->active_cell_iterators())
447 if (cell->is_locally_owned())
448 this->local_cell_relations.emplace_back(
454 template <
int dim,
int spacedim>
458#ifdef DEAL_II_WITH_MPI
459 AssertThrow(this->cell_attached_data.pack_callbacks_variable.size() == 0,
463 this->cell_attached_data.n_attached_deserialize == 0,
465 "Not all SolutionTransfer objects have been deserialized after the last call to load()."));
466 Assert(this->n_cells() > 0,
467 ExcMessage(
"Can not save() an empty Triangulation."));
475 unsigned int n_locally_owned_cells = this->n_locally_owned_active_cells();
477 unsigned int global_first_cell = 0;
479 int ierr = MPI_Exscan(&n_locally_owned_cells,
484 this->mpi_communicator);
487 global_first_cell *=
sizeof(
unsigned int);
492 std::string fname = std::string(filename) +
".info";
493 std::ofstream f(fname.c_str());
494 f <<
"version nproc n_attached_fixed_size_objs n_attached_variable_size_objs n_global_active_cells"
498 << this->cell_attached_data.pack_callbacks_fixed.size() <<
" "
499 << this->cell_attached_data.pack_callbacks_variable.size() <<
" "
500 << this->n_global_active_cells() << std::endl;
504 this->save_attached_data(global_first_cell,
505 this->n_global_active_cells(),
511 int ierr = MPI_Info_create(&info);
514 const std::string fname_tria = filename +
"_triangulation.data";
518 ierr = MPI_File_open(this->mpi_communicator,
520 MPI_MODE_CREATE | MPI_MODE_WRONLY,
525 ierr = MPI_File_set_size(fh, 0);
529 ierr = MPI_Barrier(this->mpi_communicator);
531 ierr = MPI_Info_free(&info);
538 this->mpi_communicator,
542 std::vector<char> buffer;
546 const std::uint64_t buffer_size = buffer.size();
548 std::uint64_t offset = 0;
556 this->mpi_communicator);
560 ierr = MPI_File_write_at(
562 myrank *
sizeof(std::uint64_t),
570 const std::uint64_t global_position =
571 mpisize *
sizeof(std::uint64_t) + offset;
583 ierr = MPI_File_close(&fh);
595 template <
int dim,
int spacedim>
599#ifdef DEAL_II_WITH_MPI
600 Assert(this->n_cells() == 0,
601 ExcMessage(
"load() only works if the Triangulation is empty!"));
604 unsigned int n_locally_owned_cells = this->n_locally_owned_active_cells();
606 unsigned int global_first_cell = 0;
608 int ierr = MPI_Exscan(&n_locally_owned_cells,
613 this->mpi_communicator);
616 global_first_cell *=
sizeof(
unsigned int);
619 unsigned int version, numcpus, attached_count_fixed,
620 attached_count_variable, n_global_active_cells;
622 std::string fname = std::string(filename) +
".info";
623 std::ifstream f(fname.c_str());
625 std::string firstline;
626 getline(f, firstline);
627 f >> version >> numcpus >> attached_count_fixed >>
628 attached_count_variable >> n_global_active_cells;
632 ExcMessage(
"Incompatible version found in .info file."));
633 Assert(this->n_global_active_cells() == n_global_active_cells,
634 ExcMessage(
"Number of global active cells differ!"));
647 int ierr = MPI_Info_create(&info);
650 const std::string fname_tria = filename +
"_triangulation.data";
653 ierr = MPI_File_open(this->mpi_communicator,
660 ierr = MPI_Info_free(&info);
664 std::uint64_t buffer_size;
666 ierr = MPI_File_read_at(
668 myrank *
sizeof(std::uint64_t),
675 std::uint64_t offset = 0;
683 this->mpi_communicator);
687 const std::uint64_t global_position =
688 mpisize *
sizeof(std::uint64_t) + offset;
691 std::vector<char> buffer(buffer_size);
701 ierr = MPI_File_close(&fh);
704 auto construction_data = ::Utilities::template unpack<
709 construction_data.
comm = this->mpi_communicator;
711 this->create_triangulation(construction_data);
716 this->cell_attached_data.n_attached_data_sets = 0;
717 this->cell_attached_data.n_attached_deserialize =
718 attached_count_fixed + attached_count_variable;
721 this->load_attached_data(global_first_cell,
722 this->n_global_active_cells(),
723 this->n_locally_owned_active_cells(),
725 attached_count_fixed,
726 attached_count_variable);
728 this->update_cell_relations();
729 this->update_periodic_face_map();
730 this->update_number_cache();
740 template <
int dim,
int spacedim>
743 const
bool autopartition)
751 template <
int dim,
int spacedim>
760 for (
const auto &cell : this->active_cell_iterators())
761 if (!cell->is_artificial())
762 number_of_global_coarse_cells =
763 std::max(number_of_global_coarse_cells,
764 cell->id().get_coarse_cell_id());
766 number_of_global_coarse_cells =
768 this->mpi_communicator) +
771 this->number_cache.number_of_global_coarse_cells =
772 number_of_global_coarse_cells;
782#include "fully_distributed_tria.inst"
types::coarse_cell_id get_coarse_cell_id() const
virtual void copy_triangulation(const Triangulation< dim, spacedim > &other_tria)
virtual void clear() override
virtual std::size_t memory_consumption() const override
virtual void update_number_cache()
virtual types::coarse_cell_id coarse_cell_index_to_coarse_cell_id(const unsigned int coarse_cell_index) const override
void create_triangulation(const TriangulationDescription::Description< dim, spacedim > &construction_data) override
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcNeedsMPI()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
void hyper_cube(Triangulation< dim, spacedim > &tria, const double left=0., const double right=1., const bool colorize=false)
std::enable_if_t< std::is_fundamental< T >::value, std::size_t > memory_consumption(const T &t)
Description< dim, spacedim > create_description_from_triangulation(const ::Triangulation< dim, spacedim > &tria, const MPI_Comm comm, const TriangulationDescription::Settings settings=TriangulationDescription::Settings::default_setting, const unsigned int my_rank_in=numbers::invalid_unsigned_int)
@ construct_multigrid_hierarchy
int File_write_at_c(MPI_File fh, MPI_Offset offset, const void *buf, MPI_Count count, MPI_Datatype datatype, MPI_Status *status)
int File_read_at_c(MPI_File fh, MPI_Offset offset, void *buf, MPI_Count count, MPI_Datatype datatype, MPI_Status *status)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
const MPI_Datatype mpi_type_id_for_type
size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
const types::coarse_cell_id invalid_coarse_cell_id
const types::subdomain_id artificial_subdomain_id
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
const TriangulationDescription::Settings settings
std::vector< std::vector< CellData< dim > > > cell_infos
const ::Triangulation< dim, spacedim > & tria