-
Notifications
You must be signed in to change notification settings - Fork 708
/
tria_base.cc
1742 lines (1452 loc) · 62.2 KB
/
tria_base.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------------------------------------------
//
// Copyright (C) 2015 - 2021 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#include <deal.II/base/logstream.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/base/mpi.templates.h>
#include <deal.II/base/utilities.h>
#include <deal.II/distributed/shared_tria.h>
#include <deal.II/distributed/tria_base.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/grid/tria.h>
#include <deal.II/grid/tria_accessor.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/lac/sparsity_pattern.h>
#include <deal.II/lac/sparsity_tools.h>
#include <deal.II/lac/vector_memory.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <numeric>
DEAL_II_NAMESPACE_OPEN
namespace parallel
{
template <int dim, int spacedim>
TriangulationBase<dim, spacedim>::TriangulationBase(
const MPI_Comm &mpi_communicator,
const typename dealii::Triangulation<dim, spacedim>::MeshSmoothing
smooth_grid,
const bool check_for_distorted_cells)
: dealii::Triangulation<dim, spacedim>(smooth_grid,
check_for_distorted_cells)
, mpi_communicator(mpi_communicator)
, my_subdomain(Utilities::MPI::this_mpi_process(this->mpi_communicator))
, n_subdomains(Utilities::MPI::n_mpi_processes(this->mpi_communicator))
{
#ifndef DEAL_II_WITH_MPI
Assert(false, ExcNeedsMPI());
#endif
}
template <int dim, int spacedim>
void
TriangulationBase<dim, spacedim>::copy_triangulation(
const dealii::Triangulation<dim, spacedim> &other_tria)
{
#ifndef DEAL_II_WITH_MPI
(void)other_tria;
Assert(false, ExcNeedsMPI());
#else
dealii::Triangulation<dim, spacedim>::copy_triangulation(other_tria);
if (const dealii::parallel::TriangulationBase<dim, spacedim> *other_tria_x =
dynamic_cast<const dealii::parallel::TriangulationBase<dim, spacedim>
*>(&other_tria))
{
// release unused vector memory because we will have very different
// vectors now
::dealii::internal::GrowingVectorMemoryImplementation::
release_all_unused_memory();
}
#endif
}
template <int dim, int spacedim>
std::size_t
TriangulationBase<dim, spacedim>::memory_consumption() const
{
std::size_t mem =
this->dealii::Triangulation<dim, spacedim>::memory_consumption() +
MemoryConsumption::memory_consumption(this->mpi_communicator) +
MemoryConsumption::memory_consumption(my_subdomain) +
MemoryConsumption::memory_consumption(
number_cache.n_global_active_cells) +
MemoryConsumption::memory_consumption(number_cache.n_global_levels);
return mem;
}
template <int dim, int spacedim>
TriangulationBase<dim, spacedim>::~TriangulationBase()
{
// release unused vector memory because the vector layout is going to look
// very different now
::dealii::internal::GrowingVectorMemoryImplementation::
release_all_unused_memory();
}
template <int dim, int spacedim>
TriangulationBase<dim, spacedim>::NumberCache::NumberCache()
: n_locally_owned_active_cells(0)
, number_of_global_coarse_cells(0)
, n_global_levels(0)
{}
template <int dim, int spacedim>
unsigned int
TriangulationBase<dim, spacedim>::n_locally_owned_active_cells() const
{
return number_cache.n_locally_owned_active_cells;
}
template <int dim, int spacedim>
unsigned int
TriangulationBase<dim, spacedim>::n_global_levels() const
{
return number_cache.n_global_levels;
}
template <int dim, int spacedim>
types::global_cell_index
TriangulationBase<dim, spacedim>::n_global_active_cells() const
{
return number_cache.n_global_active_cells;
}
template <int dim, int spacedim>
MPI_Comm
TriangulationBase<dim, spacedim>::get_communicator() const
{
return mpi_communicator;
}
#ifdef DEAL_II_WITH_MPI
template <int dim, int spacedim>
void
TriangulationBase<dim, spacedim>::update_number_cache()
{
number_cache.ghost_owners.clear();
number_cache.level_ghost_owners.clear();
number_cache.n_locally_owned_active_cells = 0;
if (this->n_levels() == 0)
{
// Skip communication done below if we do not have any cells
// (meaning the Triangulation is empty on all processors). This will
// happen when called from the destructor of Triangulation, which
// can get called during exception handling causing a hang in this
// function.
number_cache.n_global_active_cells = 0;
number_cache.n_global_levels = 0;
return;
}
{
// find ghost owners
for (const auto &cell : this->active_cell_iterators())
if (cell->is_ghost())
number_cache.ghost_owners.insert(cell->subdomain_id());
Assert(number_cache.ghost_owners.size() <
Utilities::MPI::n_mpi_processes(this->mpi_communicator),
ExcInternalError());
}
if (this->n_levels() > 0)
for (const auto &cell : this->active_cell_iterators())
if (cell->subdomain_id() == my_subdomain)
++number_cache.n_locally_owned_active_cells;
// Potentially cast to a 64 bit type before accumulating to avoid overflow:
number_cache.n_global_active_cells =
Utilities::MPI::sum(static_cast<types::global_cell_index>(
number_cache.n_locally_owned_active_cells),
this->mpi_communicator);
number_cache.n_global_levels =
Utilities::MPI::max(this->n_levels(), this->mpi_communicator);
// Store MPI ranks of level ghost owners of this processor on all levels.
if (this->is_multilevel_hierarchy_constructed() == true)
{
number_cache.level_ghost_owners.clear();
// if there is nothing to do, then do nothing
if (this->n_levels() == 0)
return;
// find level ghost owners
for (typename Triangulation<dim, spacedim>::cell_iterator cell =
this->begin();
cell != this->end();
++cell)
if (cell->level_subdomain_id() != numbers::artificial_subdomain_id &&
cell->level_subdomain_id() != this->locally_owned_subdomain())
this->number_cache.level_ghost_owners.insert(
cell->level_subdomain_id());
# ifdef DEBUG
// Check that level_ghost_owners is symmetric by sending a message to
// everyone
{
int ierr = MPI_Barrier(this->mpi_communicator);
AssertThrowMPI(ierr);
const int mpi_tag = Utilities::MPI::internal::Tags::
triangulation_base_fill_level_ghost_owners;
// important: preallocate to avoid (re)allocation:
std::vector<MPI_Request> requests(
this->number_cache.level_ghost_owners.size());
unsigned int dummy = 0;
unsigned int req_counter = 0;
for (const auto &it : this->number_cache.level_ghost_owners)
{
ierr = MPI_Isend(&dummy,
1,
MPI_UNSIGNED,
it,
mpi_tag,
this->mpi_communicator,
&requests[req_counter]);
AssertThrowMPI(ierr);
++req_counter;
}
for (const auto &it : this->number_cache.level_ghost_owners)
{
unsigned int dummy;
ierr = MPI_Recv(&dummy,
1,
MPI_UNSIGNED,
it,
mpi_tag,
this->mpi_communicator,
MPI_STATUS_IGNORE);
AssertThrowMPI(ierr);
}
if (requests.size() > 0)
{
ierr = MPI_Waitall(requests.size(),
requests.data(),
MPI_STATUSES_IGNORE);
AssertThrowMPI(ierr);
}
ierr = MPI_Barrier(this->mpi_communicator);
AssertThrowMPI(ierr);
}
# endif
Assert(this->number_cache.level_ghost_owners.size() <
Utilities::MPI::n_mpi_processes(this->mpi_communicator),
ExcInternalError());
}
this->number_cache.number_of_global_coarse_cells = this->n_cells(0);
// reset global cell ids
this->reset_global_cell_indices();
}
#else
template <int dim, int spacedim>
void
TriangulationBase<dim, spacedim>::update_number_cache()
{
Assert(false, ExcNeedsMPI());
}
#endif
template <int dim, int spacedim>
void
TriangulationBase<dim, spacedim>::update_reference_cells()
{
// run algorithm for locally-owned cells
dealii::Triangulation<dim, spacedim>::update_reference_cells();
// translate ReferenceCell to unsigned int (needed by
// Utilities::MPI::compute_set_union)
std::vector<unsigned int> reference_cells_ui;
for (const auto &i : this->reference_cells)
reference_cells_ui.push_back(static_cast<unsigned int>(i));
// create union
reference_cells_ui =
Utilities::MPI::compute_set_union(reference_cells_ui,
this->mpi_communicator);
// transform back and store result
this->reference_cells.clear();
for (const auto &i : reference_cells_ui)
this->reference_cells.emplace_back(
dealii::internal::ReferenceCell::make_reference_cell_from_int(i));
}
template <int dim, int spacedim>
types::subdomain_id
TriangulationBase<dim, spacedim>::locally_owned_subdomain() const
{
return my_subdomain;
}
template <int dim, int spacedim>
const std::set<types::subdomain_id> &
TriangulationBase<dim, spacedim>::ghost_owners() const
{
return number_cache.ghost_owners;
}
template <int dim, int spacedim>
const std::set<types::subdomain_id> &
TriangulationBase<dim, spacedim>::level_ghost_owners() const
{
return number_cache.level_ghost_owners;
}
template <int dim, int spacedim>
std::vector<types::boundary_id>
TriangulationBase<dim, spacedim>::get_boundary_ids() const
{
return Utilities::MPI::compute_set_union(
dealii::Triangulation<dim, spacedim>::get_boundary_ids(),
this->mpi_communicator);
}
template <int dim, int spacedim>
std::vector<types::manifold_id>
TriangulationBase<dim, spacedim>::get_manifold_ids() const
{
return Utilities::MPI::compute_set_union(
dealii::Triangulation<dim, spacedim>::get_manifold_ids(),
this->mpi_communicator);
}
template <int dim, int spacedim>
void
TriangulationBase<dim, spacedim>::reset_global_cell_indices()
{
#ifndef DEAL_II_WITH_MPI
Assert(false, ExcNeedsMPI());
#else
if (const auto pst =
dynamic_cast<const parallel::shared::Triangulation<dim, spacedim> *>(
this))
if (pst->with_artificial_cells() == false)
{
// Specialization for parallel::shared::Triangulation without
// artificial cells. The code below only works if a halo of a single
// ghost cells is needed.
std::vector<unsigned int> cell_counter(n_subdomains + 1);
// count number of cells of each process
for (const auto &cell : this->active_cell_iterators())
cell_counter[cell->subdomain_id() + 1]++;
// take prefix sum to obtain offset of each process
for (unsigned int i = 0; i < n_subdomains; ++i)
cell_counter[i + 1] += cell_counter[i];
AssertDimension(cell_counter.back(), this->n_active_cells());
// create partitioners
IndexSet is_local(this->n_active_cells());
is_local.add_range(cell_counter[my_subdomain],
cell_counter[my_subdomain + 1]);
number_cache.active_cell_index_partitioner =
std::make_shared<const Utilities::MPI::Partitioner>(
is_local,
complete_index_set(this->n_active_cells()),
this->mpi_communicator);
// set global active cell indices and increment process-local counters
for (const auto &cell : this->active_cell_iterators())
cell->set_global_active_cell_index(
cell_counter[cell->subdomain_id()]++);
Assert(this->is_multilevel_hierarchy_constructed() == false,
ExcNotImplemented());
return;
}
// 1) determine number of active locally-owned cells
const types::global_cell_index n_locally_owned_cells =
this->n_locally_owned_active_cells();
// 2) determine the offset of each process
types::global_cell_index cell_index = 0;
const int ierr =
MPI_Exscan(&n_locally_owned_cells,
&cell_index,
1,
Utilities::MPI::internal::mpi_type_id(&n_locally_owned_cells),
MPI_SUM,
this->mpi_communicator);
AssertThrowMPI(ierr);
// 3) give global indices to locally-owned cells and mark all other cells as
// invalid
for (const auto &cell : this->active_cell_iterators())
if (cell->is_locally_owned())
cell->set_global_active_cell_index(cell_index++);
else
cell->set_global_active_cell_index(numbers::invalid_dof_index);
// 4) determine the global indices of ghost cells
GridTools::exchange_cell_data_to_ghosts<types::global_cell_index>(
*this,
[](const auto &cell) { return cell->global_active_cell_index(); },
[](const auto &cell, const auto &id) {
cell->set_global_active_cell_index(id);
});
// 5) set up new partitioner
std::vector<types::global_dof_index> is_local_vector;
std::vector<types::global_dof_index> is_ghost_vector;
for (const auto &cell : this->active_cell_iterators())
if (!cell->is_artificial())
{
const auto index = cell->global_active_cell_index();
if (index == numbers::invalid_dof_index)
continue;
if (cell->is_locally_owned())
is_local_vector.push_back(index);
else
is_ghost_vector.push_back(index);
}
std::sort(is_local_vector.begin(), is_local_vector.end());
IndexSet is_local(this->n_global_active_cells());
is_local.add_indices(is_local_vector.begin(), is_local_vector.end());
std::sort(is_ghost_vector.begin(), is_ghost_vector.end());
IndexSet is_ghost(this->n_global_active_cells());
is_ghost.add_indices(is_ghost_vector.begin(), is_ghost_vector.end());
number_cache.active_cell_index_partitioner =
std::make_shared<const Utilities::MPI::Partitioner>(
is_local, is_ghost, this->mpi_communicator);
// 6) proceed with multigrid levels if requested
if (this->is_multilevel_hierarchy_constructed() == true)
{
// 1) determine number of locally-owned cells on levels
std::vector<types::global_cell_index> n_locally_owned_cells(
this->n_global_levels(), 0);
for (auto cell : this->cell_iterators())
if (cell->level_subdomain_id() == this->locally_owned_subdomain())
n_locally_owned_cells[cell->level()]++;
// 2) determine the offset of each process
std::vector<types::global_cell_index> cell_index(
this->n_global_levels(), 0);
int ierr = MPI_Exscan(n_locally_owned_cells.data(),
cell_index.data(),
this->n_global_levels(),
Utilities::MPI::internal::mpi_type_id(
n_locally_owned_cells.data()),
MPI_SUM,
this->mpi_communicator);
AssertThrowMPI(ierr);
// 3) determine global number of "active" cells on each level
std::vector<types::global_cell_index> n_cells_level(
this->n_global_levels(), 0);
for (unsigned int l = 0; l < this->n_global_levels(); ++l)
n_cells_level[l] = n_locally_owned_cells[l] + cell_index[l];
ierr =
MPI_Bcast(n_cells_level.data(),
this->n_global_levels(),
Utilities::MPI::internal::mpi_type_id(n_cells_level.data()),
this->n_subdomains - 1,
this->mpi_communicator);
AssertThrowMPI(ierr);
// 4) give global indices to locally-owned cells on level and mark
// all other cells as invalid
for (auto cell : this->cell_iterators())
if (cell->level_subdomain_id() == this->locally_owned_subdomain())
cell->set_global_level_cell_index(cell_index[cell->level()]++);
else
cell->set_global_level_cell_index(numbers::invalid_dof_index);
// 5) update the numbers of ghost level cells
GridTools::exchange_cell_data_to_level_ghosts<
types::global_cell_index,
dealii::Triangulation<dim, spacedim>>(
*this,
[](const auto &cell) { return cell->global_level_cell_index(); },
[](const auto &cell, const auto &id) {
return cell->set_global_level_cell_index(id);
});
number_cache.level_cell_index_partitioners.resize(
this->n_global_levels());
// 6) set up cell partitioners for each level
for (unsigned int l = 0; l < this->n_global_levels(); ++l)
{
std::vector<types::global_dof_index> is_local_vector;
std::vector<types::global_dof_index> is_ghost_vector;
for (const auto &cell : this->cell_iterators_on_level(l))
if (cell->level_subdomain_id() !=
dealii::numbers::artificial_subdomain_id)
{
const auto index = cell->global_level_cell_index();
if (index == numbers::invalid_dof_index)
continue;
if (cell->level_subdomain_id() ==
this->locally_owned_subdomain())
is_local_vector.push_back(index);
else
is_ghost_vector.push_back(index);
;
}
IndexSet is_local(n_cells_level[l]);
std::sort(is_local_vector.begin(), is_local_vector.end());
is_local.add_indices(is_local_vector.begin(),
is_local_vector.end());
IndexSet is_ghost(n_cells_level[l]);
std::sort(is_ghost_vector.begin(), is_ghost_vector.end());
is_ghost.add_indices(is_ghost_vector.begin(),
is_ghost_vector.end());
number_cache.level_cell_index_partitioners[l] =
std::make_shared<const Utilities::MPI::Partitioner>(
is_local, is_ghost, this->mpi_communicator);
}
}
#endif
}
template <int dim, int spacedim>
void
TriangulationBase<dim, spacedim>::communicate_locally_moved_vertices(
const std::vector<bool> &vertex_locally_moved)
{
AssertDimension(vertex_locally_moved.size(), this->n_vertices());
#ifdef DEBUG
{
const std::vector<bool> locally_owned_vertices =
dealii::GridTools::get_locally_owned_vertices(*this);
for (unsigned int i = 0; i < locally_owned_vertices.size(); ++i)
Assert((vertex_locally_moved[i] == false) ||
(locally_owned_vertices[i] == true),
ExcMessage("The vertex_locally_moved argument must not "
"contain vertices that are not locally owned"));
}
#endif
Point<spacedim> invalid_point;
for (int d = 0; d < spacedim; ++d)
invalid_point[d] = std::numeric_limits<double>::quiet_NaN();
const auto pack = [&](const auto &cell) {
std::vector<Point<spacedim>> vertices(cell->n_vertices());
for (const auto v : cell->vertex_indices())
if (vertex_locally_moved[cell->vertex_index(v)])
vertices[v] = cell->vertex(v);
else
vertices[v] = invalid_point;
return vertices;
};
const auto unpack = [&](const auto &cell, const auto &vertices) {
for (const auto v : cell->vertex_indices())
if (std::isnan(vertices[v][0]) == false)
cell->vertex(v) = vertices[v];
};
if (this->is_multilevel_hierarchy_constructed())
GridTools::exchange_cell_data_to_level_ghosts<
std::vector<Point<spacedim>>>(*this, pack, unpack);
else
GridTools::exchange_cell_data_to_ghosts<std::vector<Point<spacedim>>>(
*this, pack, unpack);
}
template <int dim, int spacedim>
const std::weak_ptr<const Utilities::MPI::Partitioner>
TriangulationBase<dim, spacedim>::global_active_cell_index_partitioner() const
{
return number_cache.active_cell_index_partitioner;
}
template <int dim, int spacedim>
const std::weak_ptr<const Utilities::MPI::Partitioner>
TriangulationBase<dim, spacedim>::global_level_cell_index_partitioner(
const unsigned int level) const
{
Assert(this->is_multilevel_hierarchy_constructed(), ExcNotImplemented());
AssertIndexRange(level, this->n_global_levels());
return number_cache.level_cell_index_partitioners[level];
}
template <int dim, int spacedim>
types::coarse_cell_id
TriangulationBase<dim, spacedim>::n_global_coarse_cells() const
{
return number_cache.number_of_global_coarse_cells;
}
template <int dim, int spacedim>
DistributedTriangulationBase<dim, spacedim>::DistributedTriangulationBase(
const MPI_Comm &mpi_communicator,
const typename dealii::Triangulation<dim, spacedim>::MeshSmoothing
smooth_grid,
const bool check_for_distorted_cells)
: dealii::parallel::TriangulationBase<dim, spacedim>(
mpi_communicator,
smooth_grid,
check_for_distorted_cells)
, cell_attached_data({0, 0, {}, {}})
, data_transfer(mpi_communicator)
{}
template <int dim, int spacedim>
void
DistributedTriangulationBase<dim, spacedim>::clear()
{
cell_attached_data = {0, 0, {}, {}};
data_transfer.clear();
dealii::Triangulation<dim, spacedim>::clear();
}
template <int dim, int spacedim>
void
DistributedTriangulationBase<dim, spacedim>::save_attached_data(
const unsigned int global_first_cell,
const unsigned int global_num_cells,
const std::string &filename) const
{
// cast away constness
auto tria = const_cast<
dealii::parallel::DistributedTriangulationBase<dim, spacedim> *>(this);
if (this->cell_attached_data.n_attached_data_sets > 0)
{
// pack attached data first
tria->data_transfer.pack_data(
tria->local_cell_relations,
tria->cell_attached_data.pack_callbacks_fixed,
tria->cell_attached_data.pack_callbacks_variable);
// then store buffers in file
tria->data_transfer.save(global_first_cell, global_num_cells, filename);
// and release the memory afterwards
tria->data_transfer.clear();
}
// clear all of the callback data, as explained in the documentation of
// register_data_attach()
{
tria->cell_attached_data.n_attached_data_sets = 0;
tria->cell_attached_data.pack_callbacks_fixed.clear();
tria->cell_attached_data.pack_callbacks_variable.clear();
}
}
template <int dim, int spacedim>
void
DistributedTriangulationBase<dim, spacedim>::load_attached_data(
const unsigned int global_first_cell,
const unsigned int global_num_cells,
const unsigned int local_num_cells,
const std::string &filename,
const unsigned int n_attached_deserialize_fixed,
const unsigned int n_attached_deserialize_variable)
{
// load saved data, if any was stored
if (this->cell_attached_data.n_attached_deserialize > 0)
{
this->data_transfer.load(global_first_cell,
global_num_cells,
local_num_cells,
filename,
n_attached_deserialize_fixed,
n_attached_deserialize_variable);
this->data_transfer.unpack_cell_status(this->local_cell_relations);
// the CellStatus of all stored cells should always be CELL_PERSIST.
for (const auto &cell_rel : this->local_cell_relations)
{
(void)cell_rel;
Assert(
(cell_rel.second == // cell_status
parallel::DistributedTriangulationBase<dim,
spacedim>::CELL_PERSIST),
ExcInternalError());
}
}
}
template <int dim, int spacedim>
unsigned int
DistributedTriangulationBase<dim, spacedim>::register_data_attach(
const std::function<std::vector<char>(const cell_iterator &,
const CellStatus)> &pack_callback,
const bool returns_variable_size_data)
{
unsigned int handle = numbers::invalid_unsigned_int;
// Add new callback function to the corresponding register.
// Encode handles according to returns_variable_size_data.
if (returns_variable_size_data)
{
handle = 2 * cell_attached_data.pack_callbacks_variable.size();
cell_attached_data.pack_callbacks_variable.push_back(pack_callback);
}
else
{
handle = 2 * cell_attached_data.pack_callbacks_fixed.size() + 1;
cell_attached_data.pack_callbacks_fixed.push_back(pack_callback);
}
// Increase overall counter.
++cell_attached_data.n_attached_data_sets;
return handle;
}
template <int dim, int spacedim>
void
DistributedTriangulationBase<dim, spacedim>::notify_ready_to_unpack(
const unsigned int handle,
const std::function<
void(const cell_iterator &,
const CellStatus,
const boost::iterator_range<std::vector<char>::const_iterator> &)>
&unpack_callback)
{
// perform unpacking
data_transfer.unpack_data(local_cell_relations, handle, unpack_callback);
// decrease counters
--cell_attached_data.n_attached_data_sets;
if (cell_attached_data.n_attached_deserialize > 0)
--cell_attached_data.n_attached_deserialize;
// important: only remove data if we are not in the deserialization
// process. There, each SolutionTransfer registers and unpacks before
// the next one does this, so n_attached_data_sets is only 1 here. This
// would destroy the saved data before the second SolutionTransfer can
// get it. This created a bug that is documented in
// tests/mpi/p4est_save_03 with more than one SolutionTransfer.
if (cell_attached_data.n_attached_data_sets == 0 &&
cell_attached_data.n_attached_deserialize == 0)
{
// everybody got their data, time for cleanup!
cell_attached_data.pack_callbacks_fixed.clear();
cell_attached_data.pack_callbacks_variable.clear();
}
if (this->cell_attached_data.n_attached_data_sets == 0 &&
this->cell_attached_data.n_attached_deserialize == 0)
{
// reset all cell_status entries after coarsening/refinement
for (auto &cell_rel : local_cell_relations)
cell_rel.second =
parallel::DistributedTriangulationBase<dim, spacedim>::CELL_PERSIST;
}
}
/* ------------------ class DataTransfer<dim,spacedim> ----------------- */
template <int dim, int spacedim>
DistributedTriangulationBase<dim, spacedim>::DataTransfer::DataTransfer(
const MPI_Comm &mpi_communicator)
: variable_size_data_stored(false)
, mpi_communicator(mpi_communicator)
{}
template <int dim, int spacedim>
void
DistributedTriangulationBase<dim, spacedim>::DataTransfer::pack_data(
const std::vector<cell_relation_t> &cell_relations,
const std::vector<typename CellAttachedData::pack_callback_t>
&pack_callbacks_fixed,
const std::vector<typename CellAttachedData::pack_callback_t>
&pack_callbacks_variable)
{
Assert(src_data_fixed.size() == 0,
ExcMessage("Previously packed data has not been released yet!"));
Assert(src_sizes_variable.size() == 0, ExcInternalError());
const unsigned int n_callbacks_fixed = pack_callbacks_fixed.size();
const unsigned int n_callbacks_variable = pack_callbacks_variable.size();
// Store information that we packed variable size data in
// a member variable for later.
variable_size_data_stored = (n_callbacks_variable > 0);
// If variable transfer is scheduled, we will store the data size that
// each variable size callback function writes in this auxiliary
// container. The information will be stored by each cell in this vector
// temporarily.
std::vector<unsigned int> cell_sizes_variable_cumulative(
n_callbacks_variable);
// Prepare the buffer structure, in which each callback function will
// store its data for each active cell.
// The outmost shell in this container construct corresponds to the
// data packed per cell. The next layer resembles the data that
// each callback function packs on the corresponding cell. These
// buffers are chains of chars stored in an std::vector<char>.
// A visualisation of the data structure:
/* clang-format off */
// | cell_1 | | cell_2 | ...
// || callback_1 || callback_2 |...| || callback_1 || callback_2 |...| ...
// |||char|char|...|||char|char|...|...| |||char|char|...|||char|char|...|...| ...
/* clang-format on */
std::vector<std::vector<std::vector<char>>> packed_fixed_size_data(
cell_relations.size());
std::vector<std::vector<std::vector<char>>> packed_variable_size_data(
variable_size_data_stored ? cell_relations.size() : 0);
//
// --------- Pack data for fixed and variable size transfer ---------
//
// Iterate over all cells, call all callback functions on each cell,
// and store their data in the corresponding buffer scope.
{
auto cell_rel_it = cell_relations.cbegin();
auto data_cell_fixed_it = packed_fixed_size_data.begin();
auto data_cell_variable_it = packed_variable_size_data.begin();
for (; cell_rel_it != cell_relations.cend(); ++cell_rel_it)
{
const auto &dealii_cell = cell_rel_it->first;
const auto &cell_status = cell_rel_it->second;
// Assertions about the tree structure.
switch (cell_status)
{
case parallel::DistributedTriangulationBase<dim, spacedim>::
CELL_PERSIST:
case parallel::DistributedTriangulationBase<dim, spacedim>::
CELL_REFINE:
// double check the condition that we will only ever attach
// data to active cells when we get here
Assert(dealii_cell->is_active(), ExcInternalError());
break;
case parallel::DistributedTriangulationBase<dim, spacedim>::
CELL_COARSEN:
// double check the condition that we will only ever attach
// data to cells with children when we get here. however, we
// can only tolerate one level of coarsening at a time, so
// check that the children are all active
Assert(dealii_cell->is_active() == false, ExcInternalError());
for (unsigned int c = 0;
c < GeometryInfo<dim>::max_children_per_cell;
++c)
Assert(dealii_cell->child(c)->is_active(),
ExcInternalError());
break;
case parallel::DistributedTriangulationBase<dim, spacedim>::
CELL_INVALID:
// do nothing on invalid cells
break;
default:
Assert(false, ExcInternalError());
break;
}
// Reserve memory corresponding to the number of callback
// functions that will be called.
// If variable size transfer is scheduled, we need to leave
// room for an array that holds information about how many
// bytes each of the variable size callback functions will
// write.
// On cells flagged with CELL_INVALID, only its CellStatus
// will be stored.
const unsigned int n_fixed_size_data_sets_on_cell =
1 +
((cell_status ==
parallel::DistributedTriangulationBase<dim,
spacedim>::CELL_INVALID) ?
0 :
((variable_size_data_stored ? 1 : 0) + n_callbacks_fixed));
data_cell_fixed_it->resize(n_fixed_size_data_sets_on_cell);
// We continue with packing all data on this specific cell.
auto data_fixed_it = data_cell_fixed_it->begin();
// First, we pack the CellStatus information.
// to get consistent data sizes on each cell for the fixed size
// transfer, we won't allow compression
*data_fixed_it =
Utilities::pack(cell_status, /*allow_compression=*/false);
++data_fixed_it;
// Proceed with all registered callback functions.
// Skip cells with the CELL_INVALID flag.
if (cell_status !=
parallel::DistributedTriangulationBase<dim,
spacedim>::CELL_INVALID)
{
// Pack fixed size data.
for (auto callback_it = pack_callbacks_fixed.cbegin();
callback_it != pack_callbacks_fixed.cend();
++callback_it, ++data_fixed_it)
{
*data_fixed_it = (*callback_it)(dealii_cell, cell_status);
}
// Pack variable size data.
// If we store variable size data, we need to transfer
// the sizes of each corresponding callback function
// via fixed size transfer as well.
if (variable_size_data_stored)
{
const unsigned int n_variable_size_data_sets_on_cell =
((cell_status ==
parallel::DistributedTriangulationBase<dim, spacedim>::
CELL_INVALID) ?
0 :
n_callbacks_variable);
data_cell_variable_it->resize(
n_variable_size_data_sets_on_cell);
auto callback_it = pack_callbacks_variable.cbegin();
auto data_variable_it = data_cell_variable_it->begin();
auto sizes_variable_it =
cell_sizes_variable_cumulative.begin();
for (; callback_it != pack_callbacks_variable.cend();
++callback_it, ++data_variable_it, ++sizes_variable_it)
{
*data_variable_it =