-
Notifications
You must be signed in to change notification settings - Fork 708
/
matrix_free.templates.h
2312 lines (2027 loc) · 93 KB
/
matrix_free.templates.h
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) 2011 - 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.
//
// ---------------------------------------------------------------------
#ifndef dealii_matrix_free_templates_h
#define dealii_matrix_free_templates_h
#include <deal.II/base/config.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/base/mpi.h>
#include <deal.II/base/mpi_consensus_algorithms.h>
#include <deal.II/base/polynomials_piecewise.h>
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/tensor_product_polynomials.h>
#include <deal.II/base/utilities.h>
#include <deal.II/distributed/tria.h>
#include <deal.II/dofs/dof_accessor.h>
#include <deal.II/fe/fe_dgp.h>
#include <deal.II/fe/fe_dgq.h>
#include <deal.II/fe/fe_poly.h>
#include <deal.II/fe/fe_q_dg0.h>
#include <deal.II/hp/q_collection.h>
#include <deal.II/matrix_free/face_info.h>
#include <deal.II/matrix_free/face_setup_internal.h>
#include <deal.II/matrix_free/hanging_nodes_internal.h>
#include <deal.II/matrix_free/matrix_free.h>
#ifdef DEAL_II_WITH_TBB
# include <deal.II/base/parallel.h>
DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
# include <tbb/concurrent_unordered_map.h>
DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
#endif
#include <fstream>
//
// TBB with oneAPI API has deprecated and removed the
// <code>tbb::tasks</code> backend. With this it is no longer possible to
// compile the following code that builds a directed acyclic graph (DAG) of
// (thread parallel) tasks without a major porting effort. It turned out
// that such a dynamic handling of dependencies and structures is not as
// competitive as initially assumed. Consequently, this part of the matrix
// free infrastructure has seen less attention than the rest over the last
// years and is (presumably) not used that often.
//
// In case of detected oneAPI backend we simply disable threading in the
// matrix free backend for now.
//
// Matthias Maier, Martin Kronbichler, 2021
//
DEAL_II_NAMESPACE_OPEN
// --------------------- MatrixFree -----------------------------------
template <int dim, typename Number, typename VectorizedArrayType>
MatrixFree<dim, Number, VectorizedArrayType>::MatrixFree()
: Subscriptor()
, indices_are_initialized(false)
, mapping_is_initialized(false)
, mg_level(numbers::invalid_unsigned_int)
{}
template <int dim, typename Number, typename VectorizedArrayType>
MatrixFree<dim, Number, VectorizedArrayType>::MatrixFree(
const MatrixFree<dim, Number, VectorizedArrayType> &other)
: Subscriptor()
{
copy_from(other);
}
template <int dim, typename Number, typename VectorizedArrayType>
std::pair<unsigned int, unsigned int>
MatrixFree<dim, Number, VectorizedArrayType>::create_cell_subrange_hp_by_index(
const std::pair<unsigned int, unsigned int> &range,
const unsigned int fe_index,
const unsigned int dof_handler_index) const
{
if (dof_info[dof_handler_index].max_fe_index == 0)
return range;
AssertIndexRange(fe_index, dof_info[dof_handler_index].max_fe_index);
const std::vector<unsigned int> &fe_indices =
dof_info[dof_handler_index].cell_active_fe_index;
if (fe_indices.empty() == true ||
dof_handlers[dof_handler_index]->get_fe_collection().size() == 1)
return range;
else
{
// the range over which we are searching must be ordered, otherwise we
// got a range that spans over too many cells
#ifdef DEBUG
for (unsigned int i = range.first + 1; i < range.second; ++i)
Assert(
fe_indices[i] >= fe_indices[i - 1],
ExcMessage(
"Cell range must be over sorted range of FE indices in hp-case!"));
AssertIndexRange(range.first, fe_indices.size() + 1);
AssertIndexRange(range.second, fe_indices.size() + 1);
#endif
std::pair<unsigned int, unsigned int> return_range;
return_range.first = std::lower_bound(fe_indices.begin() + range.first,
fe_indices.begin() + range.second,
fe_index) -
fe_indices.begin();
return_range.second =
std::lower_bound(fe_indices.begin() + return_range.first,
fe_indices.begin() + range.second,
fe_index + 1) -
fe_indices.begin();
Assert(return_range.first >= range.first &&
return_range.second <= range.second,
ExcInternalError());
return return_range;
}
}
namespace
{
class FaceRangeCompartor
{
public:
FaceRangeCompartor(const std::vector<unsigned int> &fe_indices,
const bool include,
const bool only_face_type)
: fe_indices(fe_indices)
, include(include)
, only_face_type(only_face_type)
{}
template <int vectorization_width>
bool
operator()(const internal::MatrixFreeFunctions::FaceToCellTopology<
vectorization_width> & face,
const std::array<unsigned int, 2> &fe_index)
{
const unsigned int face_type = face.face_type;
if (only_face_type)
return include ? (face_type <= fe_index[0]) : (face_type < fe_index[0]);
const std::array<unsigned int, 2> fe_index_face = {
{face_type, fe_indices[face.cells_interior[0] / vectorization_width]}};
return include ? (fe_index_face <= fe_index) : (fe_index_face < fe_index);
}
template <int vectorization_width>
bool
operator()(const internal::MatrixFreeFunctions::FaceToCellTopology<
vectorization_width> & face,
const std::array<unsigned int, 3> &fe_index)
{
const unsigned int face_type = face.face_type;
if (only_face_type)
return include ? (face_type <= fe_index[0]) : (face_type < fe_index[0]);
const std::array<unsigned int, 3> fe_index_face = {
{face_type,
fe_indices[face.cells_interior[0] / vectorization_width],
fe_indices[face.cells_exterior[0] / vectorization_width]}};
return include ? (fe_index_face <= fe_index) : (fe_index_face < fe_index);
}
private:
const std::vector<unsigned int> &fe_indices;
const bool include;
const bool only_face_type;
};
} // namespace
template <int dim, typename Number, typename VectorizedArrayType>
void
MatrixFree<dim, Number, VectorizedArrayType>::renumber_dofs(
std::vector<types::global_dof_index> &renumbering,
const unsigned int dof_handler_index)
{
AssertIndexRange(dof_handler_index, dof_info.size());
dof_info[dof_handler_index].compute_dof_renumbering(renumbering);
}
template <int dim, typename Number, typename VectorizedArrayType>
const DoFHandler<dim> &
MatrixFree<dim, Number, VectorizedArrayType>::get_dof_handler(
const unsigned int dof_handler_index) const
{
AssertIndexRange(dof_handler_index, n_components());
return *(dof_handlers[dof_handler_index]);
}
template <int dim, typename Number, typename VectorizedArrayType>
template <typename DoFHandlerType>
const DoFHandlerType &
MatrixFree<dim, Number, VectorizedArrayType>::get_dof_handler(
const unsigned int dof_handler_index) const
{
AssertIndexRange(dof_handler_index, n_components());
auto dh =
dynamic_cast<const DoFHandlerType *>(&*dof_handlers[dof_handler_index]);
Assert(dh != nullptr, ExcNotInitialized());
return *dh;
}
template <int dim, typename Number, typename VectorizedArrayType>
typename DoFHandler<dim>::cell_iterator
MatrixFree<dim, Number, VectorizedArrayType>::get_cell_iterator(
const unsigned int cell_batch_index,
const unsigned int lane_index,
const unsigned int dof_handler_index) const
{
AssertIndexRange(dof_handler_index, dof_handlers.size());
AssertIndexRange(cell_batch_index, task_info.cell_partition_data.back());
AssertIndexRange(lane_index, n_components_filled(cell_batch_index));
std::pair<unsigned int, unsigned int> index =
cell_level_index[cell_batch_index * VectorizedArrayType::size() +
lane_index];
return typename DoFHandler<dim>::cell_iterator(
&dof_handlers[dof_handler_index]->get_triangulation(),
index.first,
index.second,
&*dof_handlers[dof_handler_index]);
}
template <int dim, typename Number, typename VectorizedArrayType>
std::pair<int, int>
MatrixFree<dim, Number, VectorizedArrayType>::get_cell_level_and_index(
const unsigned int cell_batch_index,
const unsigned int lane_index) const
{
AssertIndexRange(cell_batch_index, task_info.cell_partition_data.back());
AssertIndexRange(lane_index, n_components_filled(cell_batch_index));
std::pair<int, int> level_index_pair =
cell_level_index[cell_batch_index * VectorizedArrayType::size() +
lane_index];
return level_index_pair;
}
template <int dim, typename Number, typename VectorizedArrayType>
std::pair<typename DoFHandler<dim>::cell_iterator, unsigned int>
MatrixFree<dim, Number, VectorizedArrayType>::get_face_iterator(
const unsigned int face_batch_index,
const unsigned int lane_index,
const bool interior,
const unsigned int fe_component) const
{
AssertIndexRange(fe_component, dof_handlers.size());
AssertIndexRange(face_batch_index,
n_inner_face_batches() +
(interior ? n_boundary_face_batches() : 0));
AssertIndexRange(lane_index,
n_active_entries_per_face_batch(face_batch_index));
const internal::MatrixFreeFunctions::FaceToCellTopology<
VectorizedArrayType::size()>
face2cell_info = get_face_info(face_batch_index);
const unsigned int cell_index = interior ?
face2cell_info.cells_interior[lane_index] :
face2cell_info.cells_exterior[lane_index];
std::pair<unsigned int, unsigned int> index = cell_level_index[cell_index];
return {typename DoFHandler<dim>::cell_iterator(
&dof_handlers[fe_component]->get_triangulation(),
index.first,
index.second,
&*dof_handlers[fe_component]),
interior ? face2cell_info.interior_face_no :
face2cell_info.exterior_face_no};
}
template <int dim, typename Number, typename VectorizedArrayType>
typename DoFHandler<dim>::active_cell_iterator
MatrixFree<dim, Number, VectorizedArrayType>::get_hp_cell_iterator(
const unsigned int cell_batch_index,
const unsigned int lane_index,
const unsigned int dof_handler_index) const
{
AssertIndexRange(dof_handler_index, dof_handlers.size());
AssertIndexRange(cell_batch_index, task_info.cell_partition_data.back());
AssertIndexRange(lane_index, n_components_filled(cell_batch_index));
std::pair<unsigned int, unsigned int> index =
cell_level_index[cell_batch_index * VectorizedArrayType::size() +
lane_index];
return typename DoFHandler<dim>::cell_iterator(
&dof_handlers[dof_handler_index]->get_triangulation(),
index.first,
index.second,
&*dof_handlers[dof_handler_index]);
}
template <int dim, typename Number, typename VectorizedArrayType>
void
MatrixFree<dim, Number, VectorizedArrayType>::copy_from(
const MatrixFree<dim, Number, VectorizedArrayType> &v)
{
clear();
dof_handlers = v.dof_handlers;
dof_info = v.dof_info;
constraint_pool_data = v.constraint_pool_data;
constraint_pool_row_index = v.constraint_pool_row_index;
mapping_info = v.mapping_info;
shape_info = v.shape_info;
cell_level_index = v.cell_level_index;
cell_level_index_end_local = v.cell_level_index_end_local;
task_info = v.task_info;
face_info = v.face_info;
indices_are_initialized = v.indices_are_initialized;
mapping_is_initialized = v.mapping_is_initialized;
mg_level = v.mg_level;
}
template <int dim, typename Number, typename VectorizedArrayType>
template <typename number2, int q_dim>
void
MatrixFree<dim, Number, VectorizedArrayType>::internal_reinit(
const std::shared_ptr<hp::MappingCollection<dim>> & mapping,
const std::vector<const DoFHandler<dim, dim> *> & dof_handler,
const std::vector<const AffineConstraints<number2> *> &constraints,
const std::vector<IndexSet> & locally_owned_dofs,
const std::vector<hp::QCollection<q_dim>> & quad,
const typename MatrixFree<dim, Number, VectorizedArrayType>::AdditionalData
&additional_data)
{
// Store the level of the mesh to be worked on.
this->mg_level = additional_data.mg_level;
// Reads out the FE information and stores the shape function values,
// gradients and Hessians for quadrature points.
{
unsigned int n_components = 0;
for (unsigned int no = 0; no < dof_handler.size(); ++no)
n_components += dof_handler[no]->get_fe(0).n_base_elements();
const unsigned int n_quad = quad.size();
unsigned int n_fe_in_collection = 0;
for (unsigned int no = 0; no < dof_handler.size(); ++no)
n_fe_in_collection =
std::max(n_fe_in_collection,
dof_handler[no]->get_fe_collection().size());
unsigned int n_quad_in_collection = 0;
for (unsigned int q = 0; q < n_quad; ++q)
n_quad_in_collection = std::max(n_quad_in_collection, quad[q].size());
shape_info.reinit(TableIndices<4>(
n_components, n_quad, n_fe_in_collection, n_quad_in_collection));
for (unsigned int no = 0, c = 0; no < dof_handler.size(); ++no)
for (unsigned int b = 0; b < dof_handler[no]->get_fe(0).n_base_elements();
++b, ++c)
for (unsigned int fe_no = 0;
fe_no < dof_handler[no]->get_fe_collection().size();
++fe_no)
for (unsigned int nq = 0; nq < n_quad; ++nq)
for (unsigned int q_no = 0; q_no < quad[nq].size(); ++q_no)
shape_info(c, nq, fe_no, q_no)
.reinit(quad[nq][q_no], dof_handler[no]->get_fe(fe_no), b);
}
if (additional_data.initialize_indices == true)
{
clear();
Assert(dof_handler.size() > 0, ExcMessage("No DoFHandler is given."));
AssertDimension(dof_handler.size(), constraints.size());
AssertDimension(dof_handler.size(), locally_owned_dofs.size());
task_info.allow_ghosted_vectors_in_loops =
additional_data.allow_ghosted_vectors_in_loops;
// set variables that are independent of FE
if (Utilities::MPI::job_supports_mpi() == true)
{
task_info.communicator = dof_handler[0]->get_communicator();
task_info.my_pid =
Utilities::MPI::this_mpi_process(task_info.communicator);
task_info.n_procs =
Utilities::MPI::n_mpi_processes(task_info.communicator);
task_info.communicator_sm = additional_data.communicator_sm;
}
else
{
task_info.communicator = MPI_COMM_SELF;
task_info.communicator_sm = MPI_COMM_SELF;
task_info.my_pid = 0;
task_info.n_procs = 1;
}
#ifdef DEBUG
for (const auto &constraint : constraints)
Assert(
constraint->is_closed(task_info.communicator),
ExcMessage(
"You have provided a non-empty AffineConstraints object that has not "
"been closed. Please call AffineConstraints::close() before "
"calling MatrixFree::reinit()!"));
#endif
initialize_dof_handlers(dof_handler, additional_data);
for (unsigned int no = 0; no < dof_handler.size(); ++no)
{
dof_info[no].store_plain_indices =
additional_data.store_plain_indices;
dof_info[no].global_base_element_offset =
no > 0 ? dof_info[no - 1].global_base_element_offset +
dof_handler[no - 1]->get_fe(0).n_base_elements() :
0;
}
// initialize the basic multithreading information that needs to be
// passed to the DoFInfo structure
#if defined(DEAL_II_WITH_TBB) && !defined(DEAL_II_TBB_WITH_ONEAPI)
if (additional_data.tasks_parallel_scheme != AdditionalData::none &&
MultithreadInfo::n_threads() > 1)
{
task_info.scheme =
internal::MatrixFreeFunctions::TaskInfo::TasksParallelScheme(
static_cast<int>(additional_data.tasks_parallel_scheme));
task_info.block_size = additional_data.tasks_block_size;
}
else
#endif
task_info.scheme = internal::MatrixFreeFunctions::TaskInfo::none;
// set dof_indices together with constraint_indicator and
// constraint_pool_data. It also reorders the way cells are gone through
// (to separate cells with overlap to other processors from others
// without).
initialize_indices(constraints, locally_owned_dofs, additional_data);
}
// initialize bare structures
else if (dof_info.size() != dof_handler.size())
{
initialize_dof_handlers(dof_handler, additional_data);
std::vector<unsigned int> dummy;
std::vector<unsigned char> dummy2;
task_info.vectorization_length = VectorizedArrayType::size();
task_info.n_active_cells = cell_level_index.size();
task_info.create_blocks_serial(
dummy, 1, false, dummy, false, dummy, dummy, dummy2);
for (unsigned int i = 0; i < dof_info.size(); ++i)
{
Assert(dof_handler[i]->get_fe_collection().size() == 1,
ExcNotImplemented());
dof_info[i].dimension = dim;
dof_info[i].n_base_elements =
dof_handler[i]->get_fe(0).n_base_elements();
dof_info[i].n_components.resize(dof_info[i].n_base_elements);
dof_info[i].start_components.resize(dof_info[i].n_base_elements + 1);
for (unsigned int c = 0; c < dof_info[i].n_base_elements; ++c)
{
dof_info[i].n_components[c] =
dof_handler[i]->get_fe(0).element_multiplicity(c);
for (unsigned int l = 0; l < dof_info[i].n_components[c]; ++l)
dof_info[i].component_to_base_index.push_back(c);
dof_info[i].start_components[c + 1] =
dof_info[i].start_components[c] + dof_info[i].n_components[c];
}
dof_info[i].dofs_per_cell.push_back(
dof_handler[i]->get_fe(0).n_dofs_per_cell());
// if indices are not initialized, the cell_level_index might not be
// divisible by the vectorization length. But it must be for
// mapping_info...
while (cell_level_index.size() % VectorizedArrayType::size() != 0)
cell_level_index.push_back(cell_level_index.back());
}
}
// subdivide cell, face and boundary face partitioner data, s.t., all
// ranges have the same active FE indices
if (task_info.scheme != internal::MatrixFreeFunctions::TaskInfo::
TasksParallelScheme::partition_color)
{
// ... for cells
{
auto &ptr = task_info.cell_partition_data_hp_ptr;
auto &data = task_info.cell_partition_data_hp;
ptr = {0};
data = {};
data.reserve(2 * task_info.cell_partition_data.size());
for (unsigned int i = 0; i < task_info.cell_partition_data.size() - 1;
++i)
{
if (task_info.cell_partition_data[i + 1] >
task_info.cell_partition_data[i])
{
std::pair<unsigned int, unsigned int> range{
task_info.cell_partition_data[i],
task_info.cell_partition_data[i + 1]};
if (range.second > range.first)
for (unsigned int i = 0; i < this->n_active_fe_indices(); ++i)
{
const auto cell_subrange =
this->create_cell_subrange_hp_by_index(range, i);
if (cell_subrange.second <= cell_subrange.first)
continue;
data.push_back(cell_subrange.first);
data.push_back(cell_subrange.second);
}
}
ptr.push_back(data.size() / 2);
}
}
// ... for inner faces
if (task_info.face_partition_data.empty() == false)
{
auto &ptr = task_info.face_partition_data_hp_ptr;
auto &data = task_info.face_partition_data_hp;
ptr = {0};
data = {};
data.reserve(2 * task_info.face_partition_data.size());
const auto create_inner_face_subrange_hp_by_index =
[&](const std::pair<unsigned int, unsigned int> &range,
const unsigned int face_type,
const unsigned int fe_index_interior,
const unsigned int fe_index_exterior,
const unsigned int dof_handler_index =
0) -> std::pair<unsigned int, unsigned int> {
const unsigned int n_face_types =
std::max<unsigned int>(dim - 1, 1);
if ((dof_info[dof_handler_index].max_fe_index == 0 ||
dof_handlers[dof_handler_index]->get_fe_collection().size() ==
1) &&
n_face_types == 1)
return range;
AssertIndexRange(fe_index_interior,
dof_info[dof_handler_index].max_fe_index);
AssertIndexRange(fe_index_exterior,
dof_info[dof_handler_index].max_fe_index);
const std::vector<unsigned int> &fe_indices =
dof_info[dof_handler_index].cell_active_fe_index;
if (fe_indices.empty() == true && n_face_types == 1)
return range;
else
{
const bool only_face_type =
dof_info[dof_handler_index].max_fe_index == 0 ||
dof_handlers[dof_handler_index]->get_fe_collection().size() ==
1 ||
fe_indices.empty() == true;
std::pair<unsigned int, unsigned int> return_range;
return_range.first =
std::lower_bound(
face_info.faces.begin() + range.first,
face_info.faces.begin() + range.second,
std::array<unsigned int, 3>{
{face_type, fe_index_interior, fe_index_exterior}},
FaceRangeCompartor(fe_indices, false, only_face_type)) -
face_info.faces.begin();
return_range.second =
std::lower_bound(
face_info.faces.begin() + return_range.first,
face_info.faces.begin() + range.second,
std::array<unsigned int, 3>{
{face_type, fe_index_interior, fe_index_exterior}},
FaceRangeCompartor(fe_indices, true, only_face_type)) -
face_info.faces.begin();
Assert(return_range.first >= range.first &&
return_range.second <= range.second,
ExcInternalError());
return return_range;
}
};
for (unsigned int i = 0; i < task_info.face_partition_data.size() - 1;
++i)
{
if (task_info.face_partition_data[i + 1] >
task_info.face_partition_data[i])
{
std::pair<unsigned int, unsigned int> range{
task_info.face_partition_data[i],
task_info.face_partition_data[i + 1]};
if (range.second > range.first)
for (unsigned int t = 0;
t < std::max<unsigned int>(dim - 1, 1);
++t)
for (unsigned int i = 0; i < this->n_active_fe_indices();
++i)
for (unsigned int j = 0;
j < this->n_active_fe_indices();
++j)
{
const auto subrange =
create_inner_face_subrange_hp_by_index(range,
t,
i,
j);
if (subrange.second <= subrange.first)
continue;
data.push_back(subrange.first);
data.push_back(subrange.second);
}
}
ptr.push_back(data.size() / 2);
}
}
// ... for boundary faces
if (task_info.boundary_partition_data.empty() == false)
{
auto &ptr = task_info.boundary_partition_data_hp_ptr;
auto &data = task_info.boundary_partition_data_hp;
ptr = {0};
data = {};
data.reserve(2 * task_info.boundary_partition_data.size());
const auto create_boundary_face_subrange_hp_by_index =
[&](const std::pair<unsigned int, unsigned int> &range,
const unsigned int face_type,
const unsigned int fe_index,
const unsigned int dof_handler_index =
0) -> std::pair<unsigned int, unsigned int> {
const unsigned int n_face_types =
std::max<unsigned int>(dim - 1, 1);
if ((dof_info[dof_handler_index].max_fe_index == 0 ||
dof_handlers[dof_handler_index]->get_fe_collection().size() ==
1) &&
n_face_types == 1)
return range;
AssertIndexRange(fe_index,
dof_info[dof_handler_index].max_fe_index);
const std::vector<unsigned int> &fe_indices =
dof_info[dof_handler_index].cell_active_fe_index;
if (fe_indices.empty() == true && n_face_types == 1)
return range;
else
{
const bool only_face_type =
dof_info[dof_handler_index].max_fe_index == 0 ||
dof_handlers[dof_handler_index]->get_fe_collection().size() ==
1 ||
fe_indices.empty() == true;
std::pair<unsigned int, unsigned int> return_range;
return_range.first =
std::lower_bound(
face_info.faces.begin() + range.first,
face_info.faces.begin() + range.second,
std::array<unsigned int, 2>{{face_type, fe_index}},
FaceRangeCompartor(fe_indices, false, only_face_type)) -
face_info.faces.begin();
return_range.second =
std::lower_bound(
face_info.faces.begin() + return_range.first,
face_info.faces.begin() + range.second,
std::array<unsigned int, 2>{{face_type, fe_index}},
FaceRangeCompartor(fe_indices, true, only_face_type)) -
face_info.faces.begin();
Assert(return_range.first >= range.first &&
return_range.second <= range.second,
ExcInternalError());
return return_range;
}
};
for (unsigned int i = 0;
i < task_info.boundary_partition_data.size() - 1;
++i)
{
if (task_info.boundary_partition_data[i + 1] >
task_info.boundary_partition_data[i])
{
std::pair<unsigned int, unsigned int> range{
task_info.boundary_partition_data[i],
task_info.boundary_partition_data[i + 1]};
if (range.second > range.first)
for (unsigned int t = 0;
t < std::max<unsigned int>(dim - 1, 1);
++t)
for (unsigned int i = 0; i < this->n_active_fe_indices();
++i)
{
const auto cell_subrange =
create_boundary_face_subrange_hp_by_index(range,
t,
i);
if (cell_subrange.second <= cell_subrange.first)
continue;
data.push_back(cell_subrange.first);
data.push_back(cell_subrange.second);
}
}
ptr.push_back(data.size() / 2);
}
}
}
// Evaluates transformations from unit to real cell, Jacobian determinants,
// quadrature points in real space, based on the ordering of the cells
// determined in @p extract_local_to_global_indices.
if (additional_data.initialize_mapping == true)
{
if (dof_handler.size() > 1)
{
// check if all DoHandlers are in the same hp-mode; and if hp-
// capabilities are enabled: check if active FE indices of all
// DoFHandlers are the same.
for (unsigned int i = 1; i < dof_handler.size(); ++i)
{
Assert(dof_handler[0]->has_hp_capabilities() ==
dof_handler[i]->has_hp_capabilities(),
ExcNotImplemented());
if (dof_handler[0]->has_hp_capabilities())
{
Assert(dof_info[0].cell_active_fe_index ==
dof_info[i].cell_active_fe_index,
ExcNotImplemented());
}
}
}
mapping_info.initialize(
dof_handler[0]->get_triangulation(),
cell_level_index,
face_info,
dof_handler[0]->has_hp_capabilities() ?
dof_info[0].cell_active_fe_index :
std::vector<unsigned int>(),
mapping,
quad,
additional_data.mapping_update_flags,
additional_data.mapping_update_flags_boundary_faces,
additional_data.mapping_update_flags_inner_faces,
additional_data.mapping_update_flags_faces_by_cells);
mapping_is_initialized = true;
}
}
template <int dim, typename Number, typename VectorizedArrayType>
void
MatrixFree<dim, Number, VectorizedArrayType>::update_mapping(
const Mapping<dim> &mapping)
{
update_mapping(std::make_shared<hp::MappingCollection<dim>>(mapping));
}
template <int dim, typename Number, typename VectorizedArrayType>
void
MatrixFree<dim, Number, VectorizedArrayType>::update_mapping(
const std::shared_ptr<hp::MappingCollection<dim>> &mapping)
{
AssertDimension(shape_info.size(1), mapping_info.cell_data.size());
mapping_info.update_mapping(dof_handlers[0]->get_triangulation(),
cell_level_index,
face_info,
dof_info[0].cell_active_fe_index,
mapping);
}
template <int dim, typename Number, typename VectorizedArrayType>
template <int spacedim>
bool
MatrixFree<dim, Number, VectorizedArrayType>::is_supported(
const FiniteElement<dim, spacedim> &fe)
{
return internal::MatrixFreeFunctions::ShapeInfo<double>::is_supported(fe);
}
namespace internal
{
namespace MatrixFreeFunctions
{
// steps through all children and adds the active cells recursively
template <typename InIterator>
void
resolve_cell(const InIterator & cell,
std::vector<std::pair<unsigned int, unsigned int>> &cell_its)
{
if (cell->has_children())
for (unsigned int child = 0; child < cell->n_children(); ++child)
resolve_cell(cell->child(child), cell_its);
else if (cell->is_locally_owned())
{
Assert(cell->is_active(), ExcInternalError());
cell_its.emplace_back(cell->level(), cell->index());
}
}
} // namespace MatrixFreeFunctions
} // namespace internal
template <int dim, typename Number, typename VectorizedArrayType>
void
MatrixFree<dim, Number, VectorizedArrayType>::initialize_dof_handlers(
const std::vector<const DoFHandler<dim, dim> *> &dof_handler_in,
const AdditionalData & additional_data)
{
cell_level_index.clear();
dof_handlers.resize(dof_handler_in.size());
for (unsigned int no = 0; no < dof_handler_in.size(); ++no)
dof_handlers[no] = dof_handler_in[no];
dof_info.resize(dof_handlers.size());
for (unsigned int no = 0; no < dof_handlers.size(); ++no)
dof_info[no].vectorization_length = VectorizedArrayType::size();
const Triangulation<dim> &tria = dof_handlers[0]->get_triangulation();
const unsigned int level = additional_data.mg_level;
if (level == numbers::invalid_unsigned_int)
{
cell_level_index.reserve(tria.n_active_cells());
// Go through cells on zeroth level and then successively step down into
// children. This gives a z-ordering of the cells, which is beneficial
// when setting up neighboring relations between cells for thread
// parallelization
for (const auto &cell : tria.cell_iterators_on_level(0))
internal::MatrixFreeFunctions::resolve_cell(cell, cell_level_index);
Assert(task_info.n_procs > 1 ||
cell_level_index.size() == tria.n_active_cells(),
ExcInternalError());
}
else
{
AssertIndexRange(level, tria.n_global_levels());
if (level < tria.n_levels())
{
cell_level_index.reserve(tria.n_cells(level));
for (const auto &cell : tria.cell_iterators_on_level(level))
if (cell->is_locally_owned_on_level())
cell_level_index.emplace_back(cell->level(), cell->index());
}
}
// All these are cells local to this processor. Therefore, set
// cell_level_index_end_local to the size of cell_level_index.
cell_level_index_end_local = cell_level_index.size();
}
namespace internal
{
#if defined(DEAL_II_WITH_TBB) && !defined(DEAL_II_TBB_WITH_ONEAPI)
# ifdef DEAL_II_TBB_WITH_ONEAPI
struct unsigned_int_pair_hash
{
std::size_t
operator()(const std::pair<unsigned int, unsigned int> &pair) const
{
return std::hash<unsigned int>()(pair.first) ^
std::hash<unsigned int>()(pair.second);
}
};
# endif
inline void
fill_index_subrange(
const unsigned int begin,
const unsigned int end,
const std::vector<std::pair<unsigned int, unsigned int>> &cell_level_index,
tbb::concurrent_unordered_map<std::pair<unsigned int, unsigned int>,
unsigned int
# ifdef DEAL_II_TBB_WITH_ONEAPI
,
unsigned_int_pair_hash
# endif
> &map)
{
if (cell_level_index.empty())
return;
unsigned int cell = begin;
if (cell == 0)
map.insert(std::make_pair(cell_level_index[cell++], 0U));
for (; cell < end; ++cell)
if (cell_level_index[cell] != cell_level_index[cell - 1])
map.insert(std::make_pair(cell_level_index[cell], cell));
}
template <int dim>
inline void
fill_connectivity_subrange(
const unsigned int begin,
const unsigned int end,
const dealii::Triangulation<dim> & tria,
const std::vector<std::pair<unsigned int, unsigned int>> &cell_level_index,
const tbb::concurrent_unordered_map<std::pair<unsigned int, unsigned int>,
unsigned int
# ifdef DEAL_II_TBB_WITH_ONEAPI
,
unsigned_int_pair_hash
# endif
> &map,
DynamicSparsityPattern & connectivity_direct)
{
std::vector<types::global_dof_index> new_indices;
for (unsigned int cell = begin; cell < end; ++cell)
{
new_indices.clear();
typename dealii::Triangulation<dim>::cell_iterator dcell(
&tria, cell_level_index[cell].first, cell_level_index[cell].second);
for (auto f : dcell->face_indices())
{
// Only inner faces couple different cells
if (dcell->at_boundary(f) == false &&
dcell->neighbor_or_periodic_neighbor(f)->level_subdomain_id() ==
dcell->level_subdomain_id())
{
std::pair<unsigned int, unsigned int> level_index(
dcell->neighbor_or_periodic_neighbor(f)->level(),
dcell->neighbor_or_periodic_neighbor(f)->index());
auto it = map.find(level_index);
if (it != map.end())
{
const unsigned int neighbor_cell = it->second;
if (neighbor_cell != cell)