16#include <deal.II/base/mpi.templates.h>
35 template <
int dim,
int spacedim>
45 namespace fullydistributed
47 template <
int dim,
int spacedim>
53 const
unsigned int n_partitions) {
56 , currently_processing_create_triangulation_for_internal_usage(
false)
57 , currently_processing_prepare_coarsening_and_refinement_for_internal_usage(
63 template <
int dim,
int spacedim>
71 Assert(construction_data.comm == this->mpi_communicator,
72 ExcMessage(
"MPI communicators do not match!"));
75 settings = construction_data.settings;
80 this->set_mesh_smoothing(
82 typename ::Triangulation<dim, spacedim>::MeshSmoothing
>(
86 this->set_mesh_smoothing(
88 typename ::Triangulation<dim, spacedim>::MeshSmoothing
>(
91 this->set_mesh_smoothing(construction_data.smoothing);
94 this->coarse_cell_ids.clear();
95 this->coarse_cell_indices.clear();
96 this->coarse_cell_index_to_coarse_cell_id_vector.clear();
99 if (construction_data.coarse_cell_vertices.empty())
102 currently_processing_create_triangulation_for_internal_usage =
true;
104 currently_processing_create_triangulation_for_internal_usage =
false;
107 auto cell = this->
begin();
109 cell->set_level_subdomain_id(
114 this->coarse_cell_index_to_coarse_cell_id_vector.emplace_back(
116 this->setup_coarse_cell_id_to_coarse_cell_index();
121 this->coarse_cell_index_to_coarse_cell_id_vector =
122 construction_data.coarse_cell_index_to_coarse_cell_id;
125 this->setup_coarse_cell_id_to_coarse_cell_index();
128 currently_processing_prepare_coarsening_and_refinement_for_internal_usage =
130 currently_processing_create_triangulation_for_internal_usage =
true;
133 currently_processing_prepare_coarsening_and_refinement_for_internal_usage =
135 currently_processing_create_triangulation_for_internal_usage =
false;
140 this->get_sorted_cell_infos(construction_data.cell_infos);
144 for (
const auto &cell : this->cell_iterators())
146 if (cell->is_active())
147 cell->set_subdomain_id(
150 cell->set_level_subdomain_id(
155 for (
unsigned int level = 0;
164 while (cell_info->id != cell->id().template to_binary<dim>())
168 if (cell->is_active())
169 cell->set_subdomain_id(cell_info->subdomain_id);
173 construct_multigrid_hierarchy)
174 cell->set_level_subdomain_id(cell_info->level_subdomain_id);
179 this->update_number_cache();
180 this->update_cell_relations();
187 this->reference_cells =
189 this->mpi_communicator);
194 template <
int dim,
int spacedim>
197 const
std::vector<
Point<spacedim>> &vertices,
202 currently_processing_create_triangulation_for_internal_usage,
204 "You have called the overload of\n"
206 " parallel::fullydistributed::Triangulation::"
207 "create_triangulation()\n"
209 "which takes 3 arguments. This function is not yet implemented for "
210 "this class. If you have not called this function directly, it "
211 "might have been called via a function from the GridGenerator or "
212 "GridIn namespace. To set up a fully-distributed Triangulation with "
213 "these utility functions, please start by using the same process to "
214 "set up a serial Triangulation, parallel::shared::Triangulation, or "
215 "a parallel::distributed::Triangulation. Once that is complete use "
216 "the copy_triangulation() member function to finish setting up the "
217 "original fully distributed Triangulation. Alternatively, you can "
218 "use TriangulationDescription::Utilities::"
219 "create_description_from_triangulation() or "
220 "create_description_from_triangulation_in_groups() to create the "
221 "description of the local partition, and pass that description to "
222 "parallel::fullydistributed::Triangulation::create_triangulation()."));
231 template <
int dim,
int spacedim>
240 const ::Triangulation<dim, spacedim> *other_tria_ptr = &other_tria;
249 if (
dynamic_cast<const ::parallel::TriangulationBase<dim, spacedim>
250 *
>(&other_tria) ==
nullptr)
256 this->partitioner(serial_tria,
258 this->mpi_communicator));
261 if (this->is_multilevel_hierarchy_constructed())
265 other_tria_ptr = &serial_tria;
271 this->mpi_communicator,
275 this->create_triangulation(construction_data);
280 template <
int dim,
int spacedim>
284 const
unsigned int)> &partitioner,
287 this->partitioner = partitioner;
288 this->settings = settings;
293 template <
int dim,
int spacedim>
299 this->partitioner_distributed = &partitioner;
300 this->settings = settings;
305 template <
int dim,
int spacedim>
310 this->signals.pre_distributed_repartition();
316 this->partitioner_distributed->partition(*
this),
321 this->coarse_cell_ids.clear();
322 this->coarse_cell_indices.clear();
323 this->coarse_cell_index_to_coarse_cell_id_vector.clear();
326 this->create_triangulation(construction_data);
329 this->signals.post_distributed_repartition();
334 template <
int dim,
int spacedim>
343 template <
int dim,
int spacedim>
348 currently_processing_prepare_coarsening_and_refinement_for_internal_usage,
349 ExcMessage(
"No coarsening and refinement is supported!"));
351 return ::Triangulation<dim, spacedim>::
352 prepare_coarsening_and_refinement();
357 template <
int dim,
int spacedim>
361 const std::size_t mem =
367 coarse_cell_index_to_coarse_cell_id_vector);
373 template <
int dim,
int spacedim>
385 template <
int dim,
int spacedim>
388 coarse_cell_id_to_coarse_cell_index(
389 const
types::coarse_cell_id coarse_cell_id)
const
394 if (coarse_cell_id >= coarse_cell_ids.size())
397 const auto index = coarse_cell_ids.index_within_set(
404 return coarse_cell_indices[index];
410 template <
int dim,
int spacedim>
413 spacedim>::setup_coarse_cell_id_to_coarse_cell_index()
415 coarse_cell_ids.
clear();
416 coarse_cell_indices.clear();
418 std::vector<std::pair<types::coarse_cell_id, unsigned int>> id_index;
419 id_index.reserve(coarse_cell_index_to_coarse_cell_id_vector.size());
420 for (
unsigned int i = 0;
421 i < coarse_cell_index_to_coarse_cell_id_vector.size();
423 if (coarse_cell_index_to_coarse_cell_id_vector[i] !=
425 id_index.emplace_back(coarse_cell_index_to_coarse_cell_id_vector[i],
430 if (id_index.size() == 0)
433 std::sort(id_index.begin(), id_index.end());
436 std::vector<IndexSet::size_type> ids;
437 ids.reserve(id_index.size());
438 coarse_cell_indices.reserve(id_index.size());
439 for (
const auto &[
id, index] : id_index)
441 Assert(ids.empty() || ids.back() !=
id,
442 ExcMessage(
"Coarse-cell ids must be unique."));
444 coarse_cell_indices.push_back(index);
448 coarse_cell_ids.add_indices(ids.begin(), ids.end());
449 coarse_cell_ids.compress();
454 template <
int dim,
int spacedim>
458 const unsigned int coarse_cell_index)
const
461 coarse_cell_index_to_coarse_cell_id_vector.size());
463 const auto coarse_cell_id =
464 coarse_cell_index_to_coarse_cell_id_vector[coarse_cell_index];
466 ExcMessage(
"You are trying to access a dummy cell!"));
467 return coarse_cell_id;
471 template <
int dim,
int spacedim>
476 this->local_cell_relations.
clear();
477 this->local_cell_relations.reserve(this->n_locally_owned_active_cells());
479 for (
const auto &cell : this->active_cell_iterators())
480 if (cell->is_locally_owned())
481 this->local_cell_relations.emplace_back(
487 template <
int dim,
int spacedim>
491#ifdef DEAL_II_WITH_MPI
494 this->cell_attached_data.n_attached_deserialize == 0,
496 "Not all SolutionTransfer objects have been deserialized after the last call to load()."));
497 Assert(this->n_cells() > 0,
498 ExcMessage(
"Can not save() an empty Triangulation."));
506 unsigned int n_locally_owned_cells = this->n_locally_owned_active_cells();
508 unsigned int global_first_cell = 0;
510 int ierr = MPI_Exscan(&n_locally_owned_cells,
515 this->mpi_communicator);
521 std::string fname = std::string(filename) +
".info";
522 std::ofstream f(fname);
523 f <<
"version nproc n_attached_fixed_size_objs n_attached_variable_size_objs n_global_active_cells"
528 <<
" " << this->cell_attached_data.pack_callbacks_fixed.size()
529 <<
" " << this->cell_attached_data.pack_callbacks_variable.size()
530 <<
" " << this->n_global_active_cells() << std::endl;
534 this->save_attached_data(global_first_cell,
535 this->n_global_active_cells(),
541 int ierr = MPI_Info_create(&info);
544 const std::string fname_tria = filename +
"_triangulation.data";
548 ierr = MPI_File_open(this->mpi_communicator,
550 MPI_MODE_CREATE | MPI_MODE_WRONLY,
555 ierr = MPI_File_set_size(fh, 0);
559 ierr = MPI_Barrier(this->mpi_communicator);
561 ierr = MPI_Info_free(&info);
568 this->mpi_communicator,
572 std::vector<char> buffer;
576 const std::uint64_t buffer_size = buffer.size();
578 std::uint64_t offset = 0;
586 this->mpi_communicator);
590 ierr = MPI_File_write_at(
592 myrank *
sizeof(std::uint64_t),
600 const std::uint64_t global_position =
601 mpisize *
sizeof(std::uint64_t) + offset;
613 ierr = MPI_File_close(&fh);
625 template <
int dim,
int spacedim>
629#ifdef DEAL_II_WITH_MPI
630 Assert(this->n_cells() == 0,
631 ExcMessage(
"load() only works if the Triangulation is empty!"));
634 unsigned int version, numcpus, attached_count_fixed,
635 attached_count_variable, n_global_active_cells;
637 std::string fname = std::string(filename) +
".info";
638 std::ifstream f(fname);
640 std::string firstline;
641 getline(f, firstline);
642 f >> version >> numcpus >> attached_count_fixed >>
643 attached_count_variable >> n_global_active_cells;
646 const auto expected_version = ::internal::
647 CellAttachedDataSerializer<dim, spacedim>::version_number;
650 ExcMessage(
"Incompatible version found in .info file."));
663 int ierr = MPI_Info_create(&info);
666 const std::string fname_tria = filename +
"_triangulation.data";
669 ierr = MPI_File_open(this->mpi_communicator,
676 ierr = MPI_Info_free(&info);
680 std::uint64_t buffer_size;
682 ierr = MPI_File_read_at(
684 myrank *
sizeof(std::uint64_t),
691 std::uint64_t offset = 0;
699 this->mpi_communicator);
703 const std::uint64_t global_position =
704 mpisize *
sizeof(std::uint64_t) + offset;
707 std::vector<char> buffer(buffer_size);
717 ierr = MPI_File_close(&fh);
720 auto construction_data = ::Utilities::template unpack<
725 construction_data.
comm = this->mpi_communicator;
727 this->create_triangulation(construction_data);
731 unsigned int n_locally_owned_cells = this->n_locally_owned_active_cells();
733 unsigned int global_first_cell = 0;
735 int ierr = MPI_Exscan(&n_locally_owned_cells,
740 this->mpi_communicator);
743 Assert(this->n_global_active_cells() == n_global_active_cells,
744 ExcMessage(
"Number of global active cells differ!"));
748 this->cell_attached_data.n_attached_data_sets = 0;
749 this->cell_attached_data.n_attached_deserialize =
750 attached_count_fixed + attached_count_variable;
753 this->load_attached_data(global_first_cell,
754 this->n_global_active_cells(),
755 this->n_locally_owned_active_cells(),
757 attached_count_fixed,
758 attached_count_variable);
760 this->update_cell_relations();
761 this->update_periodic_face_map();
762 this->update_number_cache();
772 template <
int dim,
int spacedim>
781 for (
const auto &cell : this->active_cell_iterators())
782 if (!cell->is_artificial())
783 number_of_global_coarse_cells =
784 std::max(number_of_global_coarse_cells,
785 cell->id().get_coarse_cell_id());
787 number_of_global_coarse_cells =
789 this->mpi_communicator) +
792 this->number_cache.number_of_global_coarse_cells =
793 number_of_global_coarse_cells;
803#include "distributed/fully_distributed_tria.inst"
virtual void copy_triangulation(const Triangulation< dim, spacedim > &other_tria)
virtual std::size_t memory_consumption() const override
virtual void update_number_cache()
virtual void clear() override
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
virtual std::size_t memory_consumption() const override
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NOT_IMPLEMENTED()
static ::ExceptionBase & ExcIO()
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_v< T >, 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)
std::vector< T > compute_set_union(const std::vector< T > &vec, const MPI_Comm comm)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
const MPI_Datatype mpi_type_id_for_type
std::size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
constexpr types::global_dof_index invalid_dof_index
constexpr unsigned int invalid_unsigned_int
constexpr types::subdomain_id artificial_subdomain_id
constexpr types::coarse_cell_id invalid_coarse_cell_id
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
std::vector< std::vector< CellData< dim > > > cell_infos