Create adjacent matrices using different indices.
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
41struct ElementColumns {
42 struct Segment {
45 };
46 std::vector<int> indices;
47 std::vector<Segment> segments;
48 std::vector<std::array<int, 2>> elementSegments;
49 std::unordered_map<const EntFiniteElement *, int> slot;
50};
51
52struct CreateRowCompressedADJMatrix :
public Core {
53
54 CreateRowCompressedADJMatrix(moab::Interface &moab,
55 MPI_Comm
comm = PETSC_COMM_WORLD,
56 int verbose = 1)
58
59 using ProblemsByName = Problem_multiIndex::index<Problem_mi_tag>::type;
60
61
62
63
64
65
66
67
68
69
70 template <typename TAG>
72 createMatArrays(ProblemsByName::iterator p_miit,
const MatType
type,
73 std::vector<PetscInt> &
i, std::vector<PetscInt> &
j,
74 const bool no_diagonals =
true,
int verb =
QUIET)
const;
75
76private:
77 using AdjByEnt = FieldEntityEntFiniteElementAdjacencyMap_multiIndex::index<
79
80 using DofByGlobalPetscIndex =
81 NumeredDofEntity_multiIndex::index<PetscGlobalIdx_mi_tag>::type;
82
83
84
85 template <typename TAG>
87 ProblemsByName::iterator p_miit,
89 TAG>::type::iterator mit_row,
90 boost::shared_ptr<FieldEntity> mofem_ent_ptr,
91 std::vector<int> &dofs_col_view, int verb) const;
92
93
94
95
96
97
98
99 template <typename TAG>
101 ElementColumns &columns, int &slot) const;
102};
103
104template <typename TAG>
106 const EntFiniteElement &fe, ElementColumns &columns, int &slot) const {
108 slot = columns.elementSegments.size();
109 columns.slot[&fe] = slot;
110 const int first_segment = columns.segments.size();
111 bool open = false;
112 for (
auto &
it : fe.getColFieldEnts()) {
113 if (
auto e =
it.lock()) {
114 auto cache = e->entityCacheColDofs.lock();
115 if (!cache)
117 if (!open || columns.segments.back().field != e->getId()) {
118 if (open)
119 columns.segments.back().end = columns.indices.size();
120 columns.segments.push_back(
121 {e->getId(), static_cast<int>(columns.indices.size()), 0});
122 open = true;
123 }
124 for (auto vit = cache->loHi[0]; vit != cache->loHi[1]; ++vit) {
125 const int idx = TAG::get_index(vit);
126 if (PetscLikely(idx >= 0))
127 columns.indices.push_back(idx);
128 }
129 }
130 }
131 if (open)
132 columns.segments.back().end = columns.indices.size();
133 columns.elementSegments.push_back(
134 {first_segment, static_cast<int>(columns.segments.size())});
136}
137
138
139
140
142
143template <typename TAG>
145 ProblemsByName::iterator p_miit,
147 TAG>::type::iterator mit_row,
148 boost::shared_ptr<FieldEntity> mofem_ent_ptr,
149 std::vector<int> &dofs_col_view, int verb) const {
151
153 BitRefLevel prb_mask = p_miit->getBitRefLevelMask();
154
156
157 dofs_col_view.clear();
158 for (auto r = entFEAdjacencies.get<Unique_mi_tag>().equal_range(
159 mofem_ent_ptr->getLocalUniqueId());
160 r.first !=
r.second; ++
r.first) {
161
162 if (
r.first->byWhat &
BYROW) {
163
164 if ((
r.first->entFePtr->getId() & p_miit->getBitFEId()).none()) {
165
166 continue;
167 }
168
169 const BitRefLevel &fe_bit =
r.first->entFePtr->getBitRefLevel();
170
171 if ((fe_bit & prb_bit).any() && (fe_bit & prb_mask) == fe_bit) {
172
173 auto check_block = [&empty_field_blocks](auto &r_if, auto &col_id) {
174 for (auto &b : empty_field_blocks) {
175 if ((b.first & r_if).any() && (b.second & col_id).any()) {
176 return false;
177 }
178 }
179 return true;
180 };
181
182 for (
auto &
it :
r.first->entFePtr->getColFieldEnts()) {
183 if (
auto e =
it.lock()) {
184
185 if (check_block((*mit_row)->getId(), e->getId())) {
186
187 if (auto cache = e->entityCacheColDofs.lock()) {
188 const auto lo = cache->loHi[0];
189 const auto hi = cache->loHi[1];
190 for (auto vit = lo; vit != hi; ++vit) {
191
192 const int idx = TAG::get_index(vit);
193 if (PetscLikely(idx >= 0))
194 dofs_col_view.push_back(idx);
195
196#ifndef NDEBUG
197 const DofIdx nb_dofs_col = p_miit->getNbDofsCol();
198 if (PetscUnlikely(idx >= nb_dofs_col)) {
200 << "Problem with dof: " << std::endl
201 << "Rank " << rAnk << " : " << *(*vit);
202 SETERRQ(
203 mofemComm, PETSC_ERR_ARG_SIZ,
204 "Index of dof larger than number of DOFs in column");
205 }
206#endif
207 }
208
209 } else {
211 }
212 }
213 }
214 }
215 }
216 }
217 }
218
220}
221
222template <typename TAG>
224 ProblemsByName::iterator p_miit,
const MatType
type,
225 std::vector<PetscInt> &
i, std::vector<PetscInt> &
j,
const bool no_diagonals,
226 int verb) const {
228
229 PetscLogEventBegin(MOFEM_EVENT_createMat, 0, 0, 0, 0);
230
231 auto cache = boost::make_shared<std::vector<EntityCacheNumeredDofs>>(
232 entsFields.size());
233
234 size_t idx = 0;
235 for (
auto it = entsFields.begin();
it != entsFields.end(); ++
it, ++idx) {
236
237 const auto uid = (*it)->getLocalUniqueId();
238 auto r = entFEAdjacencies.get<Unique_mi_tag>().equal_range(uid);
239 for (
auto lo =
r.first; lo !=
r.second; ++lo) {
240
241 if ((lo->getBitFEId() & p_miit->getBitFEId()).any()) {
242
243 const BitRefLevel &prb_bit = p_miit->getBitRefLevel();
244 const BitRefLevel &prb_mask = p_miit->getBitRefLevelMask();
245 const BitRefLevel &fe_bit = lo->entFePtr->getBitRefLevel();
246
247
248 if ((fe_bit & prb_mask) != fe_bit)
249 continue;
250 if ((fe_bit & prb_bit).none())
251 continue;
252
253 auto dit = p_miit->numeredColDofsPtr->lower_bound(uid);
254
255 decltype(dit) hi_dit;
256 if (dit != p_miit->numeredColDofsPtr->end())
257 hi_dit = p_miit->numeredColDofsPtr->upper_bound(
259 else
260 hi_dit = dit;
261
262 (*it)->entityCacheColDofs =
263 boost::shared_ptr<EntityCacheNumeredDofs>(cache, &((*cache)[idx]));
264 (*cache)[idx].loHi = {dit, hi_dit};
265
266 break;
267 }
268 }
269 }
270
271 using NumeredDofEntitysByIdx =
274
275
276 const NumeredDofEntitysByIdx &dofs_row_by_idx =
277 p_miit->numeredRowDofsPtr->get<TAG>();
278 int nb_dofs_row = p_miit->getNbDofsRow();
279 if (nb_dofs_row == 0) {
281 p_miit->getName().c_str());
282 }
283
284
285 std::map<int, std::vector<int>> adjacent_dofs_on_other_parts;
286
287
288
289
290
291
292
293 const bool rows_by_layout = strcmp(
type, MATMPIADJ) == 0;
294 typename NumeredDofEntitysByIdx::iterator miit_row, hi_miit_row;
295
296 if (rows_by_layout) {
297
298
299 PetscLayout layout;
300 CHKERR PetscLayoutCreate(mofemComm, &layout);
301 CHKERR PetscLayoutSetBlockSize(layout, 1);
302 CHKERR PetscLayoutSetSize(layout, nb_dofs_row);
303 CHKERR PetscLayoutSetUp(layout);
304 PetscInt rstart, rend;
305 CHKERR PetscLayoutGetRange(layout, &rstart, &rend);
306 CHKERR PetscLayoutDestroy(&layout);
307
310 << "row lower " << rstart << " row upper " << rend;
312 }
313
314 miit_row = dofs_row_by_idx.lower_bound(rstart);
315 hi_miit_row = dofs_row_by_idx.lower_bound(rend);
316 if (std::distance(miit_row, hi_miit_row) != rend - rstart) {
317 SETERRQ(
318 mofemComm, PETSC_ERR_ARG_SIZ,
319 "data inconsistency, std::distance(miit_row,hi_miit_row) != rend - "
320 "rstart (%ld != %d - %d = %d) ",
321 std::distance(miit_row, hi_miit_row), rend, rstart, rend - rstart);
322 }
323
324 } else {
325
327
328 CHKERR PetscCommDuplicate(get_comm(), &
comm, NULL);
329
330
331 std::vector<std::vector<int>> dofs_vec(sIze);
332
333 boost::shared_ptr<FieldEntity> mofem_ent_ptr;
334 std::vector<int> dofs_col_view;
335
336 typename NumeredDofEntitysByIdx::iterator mit_row, hi_mit_row;
337
338 mit_row = dofs_row_by_idx.begin();
339 hi_mit_row = dofs_row_by_idx.end();
340 for (; mit_row != hi_mit_row; mit_row++) {
341
342
343
344
345
346
347
348 unsigned char pstatus = (*mit_row)->getPStatus();
349 if ((pstatus & PSTATUS_NOT_OWNED) &&
350 (pstatus & (PSTATUS_SHARED | PSTATUS_MULTISHARED))) {
351
352 bool get_adj_col = true;
353 if (mofem_ent_ptr) {
354 if (mofem_ent_ptr->getLocalUniqueId() ==
355 (*mit_row)->getFieldEntityPtr()->getLocalUniqueId()) {
356 get_adj_col = false;
357 }
358 }
359
360 if (get_adj_col) {
361
362 mofem_ent_ptr = (*mit_row)->getFieldEntityPtr();
363 CHKERR getEntityAdjacencies<TAG>(p_miit, mit_row, mofem_ent_ptr,
364 dofs_col_view, verb);
365
366 {
367 std::sort(dofs_col_view.begin(), dofs_col_view.end());
368 std::vector<int>::iterator new_end =
369 std::unique(dofs_col_view.begin(), dofs_col_view.end());
370 int new_size = std::distance(dofs_col_view.begin(), new_end);
371 dofs_col_view.resize(new_size);
372 }
373
374
375 int owner = (*mit_row)->getOwnerProc();
376 dofs_vec[
owner].emplace_back(TAG::get_index(mit_row));
377 dofs_vec[
owner].emplace_back(
378 dofs_col_view.size());
379
380 dofs_vec[
owner].insert(dofs_vec[
owner].
end(), dofs_col_view.begin(),
381 dofs_col_view.end());
382 }
383 }
384 }
385
386 int nsends = 0;
387 std::vector<int> dofs_vec_length(sIze);
388 for (int proc = 0; proc < sIze; proc++) {
389
390 if (!dofs_vec[proc].
empty()) {
391
392 dofs_vec_length[proc] = dofs_vec[proc].size();
393 nsends++;
394
395 } else {
396
397 dofs_vec_length[proc] = 0;
398 }
399 }
400
401 std::vector<MPI_Status>
status(sIze);
402
403
404 int nrecvs;
405 CHKERR PetscGatherNumberOfMessages(
comm, NULL, &dofs_vec_length[0],
406 &nrecvs);
407
408
409
410 int *onodes;
411 int *olengths;
412 CHKERR PetscGatherMessageLengths(
comm, nsends, nrecvs, &dofs_vec_length[0],
413 &onodes, &olengths);
414
415
418
419
420
421 int **rbuf;
422 MPI_Request *r_waits;
423
424
425
426
427 CHKERR PetscPostIrecvInt(
comm,
tag, nrecvs, onodes, olengths, &rbuf,
428 &r_waits);
429
430 MPI_Request *s_waits;
431 CHKERR PetscMalloc1(nsends, &s_waits);
432
433
434 for (int proc = 0, kk = 0; proc < sIze; proc++) {
435 if (!dofs_vec_length[proc])
436 continue;
437 CHKERR MPI_Isend(&(dofs_vec[proc])[0],
438 dofs_vec_length[proc],
439 MPIU_INT, proc,
441 kk++;
442 }
443
444
445 if (nrecvs) {
447 }
448
449 if (nsends) {
451 }
452
453 for (int kk = 0; kk < nrecvs; kk++) {
454
455 int len = olengths[kk];
456 int *data_from_proc = rbuf[kk];
457
458 for (int ii = 0; ii < len;) {
459
460 int row_idx = data_from_proc[ii++];
461 int nb_adj_dofs = data_from_proc[ii++];
462
464
465 DofByGlobalPetscIndex::iterator dit;
466 dit = p_miit->numeredRowDofsPtr->get<PetscGlobalIdx_mi_tag>().find(
467 row_idx);
468 if (dit ==
469 p_miit->numeredRowDofsPtr->get<PetscGlobalIdx_mi_tag>().end()) {
471 "dof %d can not be found in problem", row_idx);
472 }
473 }
474
475 for (int jj = 0; jj < nb_adj_dofs; jj++) {
476 adjacent_dofs_on_other_parts[row_idx].push_back(data_from_proc[ii++]);
477 }
478 }
479 }
480
481
482 CHKERR PetscFree(s_waits);
483 CHKERR PetscFree(rbuf[0]);
485 CHKERR PetscFree(r_waits);
487 CHKERR PetscFree(olengths);
488
489 miit_row = dofs_row_by_idx.begin();
490 hi_miit_row = dofs_row_by_idx.end();
491
493 }
494
495 boost::shared_ptr<FieldEntity> mofem_ent_ptr;
496 int row_last_evaluated_idx = -1;
497
498 std::vector<int> dofs_vec;
499
500
501
502
503 ElementColumns columns;
504 columns.slot.reserve(p_miit->numeredFiniteElementsPtr->size());
505 std::vector<int> stamp(p_miit->getNbDofsCol(), -1);
506 int stamp_id = -1;
507 const auto &empty_field_blocks = p_miit->getEmptyFieldBlocks();
508 auto check_block = [&empty_field_blocks](
const BitFieldId &row_field,
510 for (auto &b : empty_field_blocks)
511 if ((b.first & row_field).any() && (b.second & col_field).any())
512 return false;
513 return true;
514 };
515 const BitRefLevel prb_bit = p_miit->getBitRefLevel();
516 const BitRefLevel prb_mask = p_miit->getBitRefLevelMask();
517 auto push_once = [&](const int idx) {
519 if (PetscUnlikely(idx >= static_cast<int>(stamp.size())))
520 SETERRQ(mofemComm, PETSC_ERR_ARG_SIZ,
521 "Index of dof larger than number of DOFs in column");
522 if (stamp[idx] != stamp_id) {
523 stamp[idx] = stamp_id;
524 dofs_vec.push_back(idx);
525 }
527 };
528
529
530 int nb_loc_row_from_iterators = 0;
531 unsigned int rows_to_fill = p_miit->getNbLocalDofsRow();
532
533
534 struct RowPattern {
535 std::size_t begin,
end;
536 int firstRow, lastRow;
537 };
538 std::vector<RowPattern> row_patterns;
539 row_patterns.reserve(std::min<std::size_t>(rows_to_fill, entsFields.size()));
540 std::vector<PetscInt> pattern_columns;
541 std::vector<PetscInt> row_diagonals;
542 if (no_diagonals)
543 row_diagonals.reserve(rows_to_fill);
544 std::size_t nb_nonzeros =
j.size();
545 i.reserve(rows_to_fill + 1);
546 for (; miit_row != hi_miit_row; miit_row++) {
547
548 if (!rows_by_layout) {
549 if (static_cast<int>((*miit_row)->getPart()) != rAnk)
550 continue;
551 }
552
553 nb_loc_row_from_iterators++;
554
555
556 i.push_back(
static_cast<PetscInt
>(nb_nonzeros));
557
558
559
560
561 if ((!mofem_ent_ptr)
562 ? 1
563 : (mofem_ent_ptr->getLocalUniqueId() !=
564 (*miit_row)->getFieldEntityPtr()->getLocalUniqueId())) {
565
566
567 mofem_ent_ptr = (*miit_row)->getFieldEntityPtr();
568 row_last_evaluated_idx = TAG::get_index(miit_row);
569 const BitFieldId row_field = (*miit_row)->getId();
570
571 dofs_vec.resize(0);
572 ++stamp_id;
573 for (auto r = entFEAdjacencies.get<Unique_mi_tag>().equal_range(
574 mofem_ent_ptr->getLocalUniqueId());
575 r.first !=
r.second; ++
r.first) {
576 if (!(
r.first->byWhat &
BYROW))
577 continue;
578 const auto &fe_ptr =
r.first->entFePtr;
579 if ((fe_ptr->getId() & p_miit->getBitFEId()).none())
580 continue;
581 const BitRefLevel &fe_bit = fe_ptr->getBitRefLevel();
582 if (!((fe_bit & prb_bit).any() && (fe_bit & prb_mask) == fe_bit))
583 continue;
584 int slot;
585 auto slot_it = columns.slot.find(fe_ptr.get());
586 if (slot_it != columns.slot.end())
587 slot = slot_it->second;
588 else
589 CHKERR addElementColumns<TAG>(*fe_ptr, columns, slot);
590 const auto segment_range = columns.elementSegments[slot];
591 for (
int s = segment_range[0];
s != segment_range[1]; ++
s) {
592 const auto &segment = columns.segments[
s];
593 if (!check_block(row_field, segment.field))
594 continue;
595 for (
int k = segment.begin;
k != segment.end; ++
k)
596 CHKERR push_once(columns.indices[
k]);
597 }
598 }
599
600 unsigned char pstatus = (*miit_row)->getPStatus();
601 if (pstatus > 0) {
602 std::map<int, std::vector<int>>::iterator mit;
603 mit = adjacent_dofs_on_other_parts.find(row_last_evaluated_idx);
604 if (mit == adjacent_dofs_on_other_parts.end()) {
605
606
607
608
609
610
611 } else {
612 for (auto idx : mit->second)
614 }
615 }
616
617
618 std::sort(dofs_vec.begin(), dofs_vec.end());
619 const auto begin = pattern_columns.size();
620 pattern_columns.insert(pattern_columns.end(), dofs_vec.begin(),
621 dofs_vec.end());
622 row_patterns.push_back({begin, pattern_columns.size(),
623 nb_loc_row_from_iterators - 1,
624 nb_loc_row_from_iterators});
625 }
626
627 row_patterns.back().lastRow = nb_loc_row_from_iterators;
628 auto row_size = dofs_vec.size();
629 if (no_diagonals) {
630 const auto diagonal = TAG::get_index(miit_row);
632 if (std::binary_search(dofs_vec.begin(), dofs_vec.end(),
diagonal))
633 --row_size;
634 }
635 if (row_size > std::numeric_limits<PetscInt>::max() - nb_nonzeros)
636 SETERRQ(mofemComm, PETSC_ERR_ARG_SIZ,
637 "CSR column offsets exceed the PetscInt range");
638 nb_nonzeros += row_size;
639 }
640
641 i.push_back(
static_cast<PetscInt
>(nb_nonzeros));
642
643
644 j.reserve(nb_nonzeros);
645 for (const auto &pattern : row_patterns) {
646 const auto begin = pattern_columns.begin() + pattern.begin;
647 const auto end = pattern_columns.begin() + pattern.end;
648 for (
int row = pattern.firstRow;
row != pattern.lastRow; ++
row) {
649 if (!no_diagonals ||
i[
row + 1] -
i[
row] == pattern.end - pattern.begin)
650 j.insert(
j.end(), begin,
end);
651 else {
652 const auto diagonal = std::lower_bound(begin,
end, row_diagonals[
row]);
655 }
656 }
657 }
658
659 if (strcmp(
type, MATMPIADJ) == 0) {
660
661
662 if (
i.size() - 1 != (
unsigned int)nb_loc_row_from_iterators) {
663 SETERRQ(
664 get_comm(), PETSC_ERR_ARG_SIZ,
665 "Number of rows from iterator is different than size of rows in "
666 "compressed row "
667 "matrix (unsigned int)nb_local_dofs_row != i.size() - 1, i.e. %u != "
668 "%zu",
669 (
unsigned int)nb_loc_row_from_iterators,
i.size() - 1);
670 }
671
672 }
else if (strcmp(
type, MATMPIAIJ) == 0) {
673
674
675 if (
i.size() - 1 != (
unsigned int)nb_loc_row_from_iterators) {
676 SETERRQ(
677 get_comm(), PETSC_ERR_ARG_SIZ,
678 "Number of rows from iterator is different than size of rows in "
679 "compressed row "
680 "matrix (unsigned int)nb_local_dofs_row != i.size() - 1, i.e. %u != "
681 "%zu",
682 (
unsigned int)nb_loc_row_from_iterators,
i.size() - 1);
683 }
684 PetscInt nb_local_dofs_row = p_miit->getNbLocalDofsRow();
685 if ((
unsigned int)nb_local_dofs_row !=
i.size() - 1) {
686 SETERRQ(
687 get_comm(), PETSC_ERR_ARG_SIZ,
688 "Number of rows is different than size of rows in compressed row "
689 "matrix (unsigned int)nb_local_dofs_row != i.size() - 1, i.e. %u != "
690 "%zu",
691 (
unsigned int)nb_local_dofs_row,
i.size() - 1);
692 }
693
694 }
else if (strcmp(
type, MATAIJ) == 0) {
695
696
697 if (
i.size() - 1 != (
unsigned int)nb_loc_row_from_iterators) {
698 SETERRQ(
699 get_comm(), PETSC_ERR_ARG_SIZ,
700 "Number of rows form iterator is different than size of rows in "
701 "compressed row "
702 "matrix (unsigned int)nb_local_dofs_row != i.size() - 1, i.e. %u != "
703 "%zu",
704 (
unsigned int)nb_loc_row_from_iterators,
i.size() - 1);
705 }
706 PetscInt nb_local_dofs_row = p_miit->getNbLocalDofsRow();
707 if ((
unsigned int)nb_local_dofs_row !=
i.size() - 1) {
708 SETERRQ(
709 get_comm(), PETSC_ERR_ARG_SIZ,
710 "Number of rows is different than size of rows in compressed row "
711 "matrix (unsigned int)nb_local_dofs_row != i.size() - 1, i.e. %u != "
712 "%zu",
713 (
unsigned int)nb_local_dofs_row,
i.size() - 1);
714 }
715
716 }
else if (strcmp(
type, MATAIJCUSPARSE) == 0) {
717
718 if (
i.size() - 1 != (
unsigned int)nb_loc_row_from_iterators) {
719 SETERRQ(get_comm(), PETSC_ERR_ARG_SIZ, "data inconsistency");
720 }
721 PetscInt nb_local_dofs_row = p_miit->getNbLocalDofsRow();
722 if ((
unsigned int)nb_local_dofs_row !=
i.size() - 1) {
723 SETERRQ(get_comm(), PETSC_ERR_ARG_SIZ, "data inconsistency");
724 }
725
726 } else {
727
728 SETERRQ(get_comm(), PETSC_ERR_ARG_NULL, "not implemented");
729 }
730
731 PetscLogEventEnd(MOFEM_EVENT_createMat, 0, 0, 0, 0);
733}
734
736MatrixManager::query_interface(boost::typeindex::type_index type_index,
737 UnknownInterface **iface) const {
738 *iface = const_cast<MatrixManager *>(this);
739 return 0;
740}
741
742MatrixManager::MatrixManager(
const MoFEM::Core &core)
744 PetscLogEventRegister("MatMngCrtMPIAIJ", 0, &MOFEM_EVENT_createMPIAIJ);
745 PetscLogEventRegister("MatMngCrtMPIAIJWthArr", 0,
746 &MOFEM_EVENT_createMPIAIJWithArrays);
747 PetscLogEventRegister("MatMngCrtMPIAdjWithArr", 0,
748 &MOFEM_EVENT_createMPIAdjWithArrays);
749 PetscLogEventRegister("MatMngCrtMPIAIJCUSPARSEWthArr", 0,
750 &MOFEM_EVENT_createMPIAIJCUSPARSEWithArrays);
751 PetscLogEventRegister("MatMngCrtSeqAIJCUSPARSEWthArrs", 0,
752 &MOFEM_EVENT_createSeqAIJCUSPARSEWithArrays);
753 PetscLogEventRegister("MatMngCrtSeqAIJWthArrs", 0,
754 &MOFEM_EVENT_createSeqAIJWithArrays);
755 PetscLogEventRegister("MatMngCrtCheckMPIAIJWthArrFillIn", 0,
756 &MOFEM_EVENT_checkMatrixFillIn);
757}
758
759template <>
760MoFEMErrorCode MatrixManager::createMPIAIJWithArrays<PetscGlobalIdx_mi_tag>(
761 const std::string
name, Mat *Aij,
int verb) {
763
765 CreateRowCompressedADJMatrix *core_ptr =
766 static_cast<CreateRowCompressedADJMatrix *
>(&
cOre);
767 PetscLogEventBegin(MOFEM_EVENT_createMPIAIJWithArrays, 0, 0, 0, 0);
768
770 auto &prb = problems_ptr->get<Problem_mi_tag>();
771 auto p_miit = prb.find(
name);
772 if (p_miit == prb.end()) {
774 "problem < %s > is not found (top tip: check spelling)",
776 }
777
778 std::vector<int> i_vec, j_vec;
779 j_vec.reserve(10000);
780 CHKERR core_ptr->createMatArrays<PetscGlobalIdx_mi_tag>(
781 p_miit, MATMPIAIJ, i_vec, j_vec, false, verb);
782
783 int nb_row_dofs = p_miit->getNbDofsRow();
784 int nb_col_dofs = p_miit->getNbDofsCol();
785 int nb_local_dofs_row = p_miit->getNbLocalDofsRow();
786 int nb_local_dofs_col = p_miit->getNbLocalDofsCol();
787
788 CHKERR ::MatCreateMPIAIJWithArrays(
789 m_field.
get_comm(), nb_local_dofs_row, nb_local_dofs_col, nb_row_dofs,
790 nb_col_dofs, &*i_vec.begin(), &*j_vec.begin(), PETSC_NULLPTR, Aij);
791
792 PetscLogEventEnd(MOFEM_EVENT_createMPIAIJWithArrays, 0, 0, 0, 0);
794}
795
796template <>
798MatrixManager::createMPIAIJCUSPARSEWithArrays<PetscGlobalIdx_mi_tag>(
799 const std::string
name, Mat *Aij,
int verb) {
801
803 CreateRowCompressedADJMatrix *core_ptr =
804 static_cast<CreateRowCompressedADJMatrix *
>(&
cOre);
805 PetscLogEventBegin(MOFEM_EVENT_createMPIAIJCUSPARSEWithArrays, 0, 0, 0, 0);
806
808 auto &prb = problems_ptr->get<Problem_mi_tag>();
809 auto p_miit = prb.find(
name);
810 if (p_miit == prb.end()) {
812 "problem < %s > is not found (top tip: check spelling)",
814 }
815
816 std::vector<int> i_vec, j_vec;
817 j_vec.reserve(10000);
818 CHKERR core_ptr->createMatArrays<PetscGlobalIdx_mi_tag>(
819 p_miit, MATAIJCUSPARSE, i_vec, j_vec, false, verb);
820
821 int nb_local_dofs_row = p_miit->getNbLocalDofsRow();
822 int nb_local_dofs_col = p_miit->getNbLocalDofsCol();
823
824 auto get_layout = [&]() {
825 int start_ranges, end_ranges;
826 PetscLayout layout;
828 CHKERR PetscLayoutSetBlockSize(layout, 1);
829 CHKERR PetscLayoutSetLocalSize(layout, nb_local_dofs_col);
830 CHKERR PetscLayoutSetUp(layout);
831 CHKERR PetscLayoutGetRange(layout, &start_ranges, &end_ranges);
832 CHKERR PetscLayoutDestroy(&layout);
833 return std::make_pair(start_ranges, end_ranges);
834 };
835
836 auto get_nnz = [&](auto &d_nnz, auto &o_nnz) {
838 auto layout = get_layout();
840 for (
int i = 0;
i != nb_local_dofs_row; ++
i) {
841 for (;
j != i_vec[
i + 1]; ++
j) {
842 if (j_vec[
j] < layout.second && j_vec[
j] >= layout.first)
844 else
846 }
847 }
849 };
850
851 std::vector<int> d_nnz(nb_local_dofs_row, 0), o_nnz(nb_local_dofs_row, 0);
852 CHKERR get_nnz(d_nnz, o_nnz);
853
854#ifdef PETSC_HAVE_CUDA
855 CHKERR ::MatCreateAIJCUSPARSE(m_field.
get_comm(), nb_local_dofs_row,
856 nb_local_dofs_col, nb_row_dofs, nb_col_dofs, 0,
857 &*d_nnz.begin(), 0, &*o_nnz.begin(), Aij);
858#else
860 "Error: To use this matrix type compile PETSc with CUDA.");
861#endif
862
863 PetscLogEventEnd(MOFEM_EVENT_createMPIAIJCUSPARSEWithArrays, 0, 0, 0, 0);
864
866}
867
868template <>
870MatrixManager::createSeqAIJCUSPARSEWithArrays<PetscLocalIdx_mi_tag>(
871 const std::string
name, Mat *Aij,
int verb) {
873
874#ifdef PETSC_HAVE_CUDA
875
876
877
879 "Not implemented type of matrix yet, try MPI version (aijcusparse)");
880#endif
881
883}
884
885template <>
887MatrixManager::createMPIAIJ<PetscGlobalIdx_mi_tag>(
const std::string
name,
888 Mat *Aij, int verb) {
890 CreateRowCompressedADJMatrix *core_ptr =
891 static_cast<CreateRowCompressedADJMatrix *
>(&
cOre);
893 PetscLogEventBegin(MOFEM_EVENT_createMPIAIJ, 0, 0, 0, 0);
894
896 auto &prb = problems_ptr->get<Problem_mi_tag>();
897 auto p_miit = prb.find(
name);
898 if (p_miit == prb.end()) {
900 "problem < %s > is not found (top tip: check spelling)",
902 }
903
904 std::vector<int> i_vec, j_vec;
905 j_vec.reserve(10000);
906 CHKERR core_ptr->createMatArrays<PetscGlobalIdx_mi_tag>(
907 p_miit, MATMPIAIJ, i_vec, j_vec, false, verb);
908
909 int nb_row_dofs = p_miit->getNbDofsRow();
910 int nb_col_dofs = p_miit->getNbDofsCol();
911 int nb_local_dofs_row = p_miit->getNbLocalDofsRow();
912 int nb_local_dofs_col = p_miit->getNbLocalDofsCol();
913
914 auto get_layout = [&]() {
915 int start_ranges, end_ranges;
916 PetscLayout layout;
918 CHKERR PetscLayoutSetBlockSize(layout, 1);
919 CHKERR PetscLayoutSetLocalSize(layout, nb_local_dofs_col);
920 CHKERR PetscLayoutSetUp(layout);
921 CHKERR PetscLayoutGetRange(layout, &start_ranges, &end_ranges);
922 CHKERR PetscLayoutDestroy(&layout);
923 return std::make_pair(start_ranges, end_ranges);
924 };
925
926 auto get_nnz = [&](auto &d_nnz, auto &o_nnz) {
928 auto layout = get_layout();
930 for (
int i = 0;
i != nb_local_dofs_row; ++
i) {
931 for (;
j != i_vec[
i + 1]; ++
j) {
932 if (j_vec[
j] < layout.second && j_vec[
j] >= layout.first)
934 else
936 }
937 }
939 };
940
941 std::vector<int> d_nnz(nb_local_dofs_row, 0), o_nnz(nb_local_dofs_row, 0);
942 CHKERR get_nnz(d_nnz, o_nnz);
943
945 CHKERR MatSetSizes(*Aij, nb_local_dofs_row, nb_local_dofs_col, nb_row_dofs,
946 nb_col_dofs);
947 CHKERR MatSetType(*Aij, MATMPIAIJ);
948 CHKERR MatMPIAIJSetPreallocation(*Aij, 0, &*d_nnz.begin(), 0,
949 &*o_nnz.begin());
950
951 PetscLogEventEnd(MOFEM_EVENT_createMPIAIJ, 0, 0, 0, 0);
953}
954
955template <>
957MatrixManager::createMPIAdjWithArrays<PetscGlobalIdx_mi_tag>(
958 const std::string
name, Mat *Adj,
int verb) {
960 CreateRowCompressedADJMatrix *core_ptr =
961 static_cast<CreateRowCompressedADJMatrix *
>(&
cOre);
963 PetscLogEventBegin(MOFEM_EVENT_createMPIAdjWithArrays, 0, 0, 0, 0);
964
966 auto &prb = problems_ptr->get<Problem_mi_tag>();
967 auto p_miit = prb.find(
name);
968 if (p_miit == prb.end()) {
970 "problem < %s > is not found (top tip: check spelling)",
972 }
973
974 std::vector<int> i_vec, j_vec;
975 j_vec.reserve(10000);
976 CHKERR core_ptr->createMatArrays<PetscGlobalIdx_mi_tag>(
977 p_miit, MATMPIADJ, i_vec, j_vec, true, verb);
978 int *_i, *_j;
979 CHKERR PetscMalloc(i_vec.size() *
sizeof(
int), &_i);
980 CHKERR PetscMalloc(j_vec.size() *
sizeof(
int), &_j);
981 copy(i_vec.begin(), i_vec.end(), _i);
982 copy(j_vec.begin(), j_vec.end(), _j);
983
984 int nb_col_dofs = p_miit->getNbDofsCol();
985 CHKERR MatCreateMPIAdj(m_field.
get_comm(), i_vec.size() - 1, nb_col_dofs, _i,
986 _j, PETSC_NULLPTR, Adj);
987 CHKERR MatSetOption(*Adj, MAT_STRUCTURALLY_SYMMETRIC, PETSC_TRUE);
988
989 PetscLogEventEnd(MOFEM_EVENT_createMPIAdjWithArrays, 0, 0, 0, 0);
991}
992
993template <>
994MoFEMErrorCode MatrixManager::createSeqAIJWithArrays<PetscLocalIdx_mi_tag>(
995 const std::string
name, Mat *Aij,
int verb) {
997 CreateRowCompressedADJMatrix *core_ptr =
998 static_cast<CreateRowCompressedADJMatrix *
>(&
cOre);
1000 PetscLogEventBegin(MOFEM_EVENT_createMPIAIJWithArrays, 0, 0, 0, 0);
1001
1003 auto &prb = problems_ptr->get<Problem_mi_tag>();
1004 auto p_miit = prb.find(
name);
1005 if (p_miit == prb.end()) {
1007 "problem < %s > is not found (top tip: check spelling)",
1009 }
1010
1011 std::vector<int> i_vec, j_vec;
1012 j_vec.reserve(10000);
1013 CHKERR core_ptr->createMatArrays<PetscGlobalIdx_mi_tag>(p_miit, MATAIJ, i_vec,
1014 j_vec, false, verb);
1015
1016 int nb_local_dofs_row = p_miit->getNbLocalDofsRow();
1017 int nb_local_dofs_col = p_miit->getNbLocalDofsCol();
1018
1019 double *_a;
1020 CHKERR PetscMalloc(j_vec.size() *
sizeof(
double), &_a);
1021
1022 Mat tmpMat;
1023 CHKERR ::MatCreateSeqAIJWithArrays(PETSC_COMM_SELF, nb_local_dofs_row,
1024 nb_local_dofs_col, &*i_vec.begin(),
1025 &*j_vec.begin(), _a, &tmpMat);
1026 CHKERR MatDuplicate(tmpMat, MAT_SHARE_NONZERO_PATTERN, Aij);
1027 CHKERR MatDestroy(&tmpMat);
1028
1030
1031 PetscLogEventEnd(MOFEM_EVENT_createMPIAIJWithArrays, 0, 0, 0, 0);
1033}
1034
1035MoFEMErrorCode MatrixManager::checkMatrixFillIn(
const std::string problem_name,
1036 int row_print, int col_print,
1040
1041 PetscLogEventBegin(MOFEM_EVENT_checkMatrixFillIn, 0, 0, 0, 0);
1042
1043 struct TestMatrixFillIn :
public FEMethod {
1044 CoreInterface *mFieldPtr;
1045
1047
1048 int rowPrint, colPrint;
1049
1050 TestMatrixFillIn(CoreInterface *m_field_ptr, Mat
a,
int row_print,
1051 int col_print)
1052 : mFieldPtr(m_field_ptr),
A(
a), rowPrint(row_print),
1053 colPrint(col_print){};
1054
1058 }
1059
1062
1063 if (refinedFiniteElementsPtr->find(
1064 numeredEntFiniteElementPtr->getEnt()) ==
1065 refinedFiniteElementsPtr->end()) {
1067 "data inconsistency");
1068 }
1069
1070 auto row_dofs = getRowDofsPtr();
1071 auto col_dofs = getColDofsPtr();
1072
1073 for (auto cit = col_dofs->begin(); cit != col_dofs->end(); cit++) {
1074
1075 if (refinedEntitiesPtr->find((*cit)->getEnt()) ==
1076 refinedEntitiesPtr->end()) {
1078 "data inconsistency");
1079 }
1080 if (!(*cit)->getActive()) {
1082 "data inconsistency");
1083 }
1084
1085 FieldEntityEntFiniteElementAdjacencyMap_multiIndex::index<
1086 Composite_Unique_mi_tag>::type::iterator ait;
1087 ait = adjacenciesPtr->get<Composite_Unique_mi_tag>().find(
1088 boost::make_tuple((*cit)->getFieldEntityPtr()->getLocalUniqueId(),
1089 numeredEntFiniteElementPtr->getLocalUniqueId()));
1090 if (ait == adjacenciesPtr->end()) {
1092 "adjacencies data inconsistency");
1093 } else {
1094 UId uid = ait->getEntUniqueId();
1095 if (entitiesPtr->find(uid) == entitiesPtr->end()) {
1097 "data inconsistency");
1098 }
1099 if (dofsPtr->find((*cit)->getLocalUniqueId()) == dofsPtr->end()) {
1101 "data inconsistency");
1102 }
1103 }
1104
1105 if ((*cit)->getEntType() != MBVERTEX) {
1106
1107 auto range =
1108 col_dofs->get<Ent_mi_tag>().equal_range((*cit)->getEnt());
1109 int nb_dofs_on_ent = std::distance(range.first, range.second);
1110
1111 int max_order = (*cit)->getMaxOrder();
1112 if ((*cit)->getNbOfCoeffs() * (*cit)->getOrderNbDofs(max_order) !=
1113 nb_dofs_on_ent) {
1114
1115
1116
1117
1118
1120 << "Warning: Number of Dofs in Col different than number "
1121 "of dofs for given entity order: "
1122 << "Field name: " << (*cit)->getName() << " "
1123 << (*cit)->getNbOfCoeffs() * (*cit)->getOrderNbDofs(max_order)
1124 << " != " << nb_dofs_on_ent;
1125 }
1126 }
1127 }
1128
1129 for (auto rit = row_dofs->begin(); rit != row_dofs->end(); rit++) {
1130
1131 if (refinedEntitiesPtr->find((*rit)->getEnt()) ==
1132 refinedEntitiesPtr->end()) {
1134 "data inconsistency");
1135 }
1136 if (!(*rit)->getActive()) {
1138 "data inconsistency");
1139 }
1140
1141 FieldEntityEntFiniteElementAdjacencyMap_multiIndex::index<
1142 Composite_Unique_mi_tag>::type::iterator ait;
1143 ait = adjacenciesPtr->get<Composite_Unique_mi_tag>().find(
1144 boost::make_tuple((*rit)->getFieldEntityPtr()->getLocalUniqueId(),
1145 numeredEntFiniteElementPtr->getLocalUniqueId()));
1146 if (ait == adjacenciesPtr->end()) {
1148 MOFEM_LOG(
"SELF", Sev::error) << *(*rit);
1149 MOFEM_LOG(
"SELF", Sev::error) << *(*rit);
1150 MOFEM_LOG(
"SELF", Sev::error) << *numeredEntFiniteElementPtr;
1151 MOFEM_LOG(
"SELF", Sev::error) <<
"dof: " << (*rit)->getBitRefLevel();
1153 << "fe: " << numeredEntFiniteElementPtr->getBitRefLevel();
1155 << "problem: " << problemPtr->getBitRefLevel();
1157 << "problem mask: " << problemPtr->getBitRefLevelMask();
1159 "adjacencies data inconsistency");
1160 } else {
1161 UId uid = ait->getEntUniqueId();
1162 if (entitiesPtr->find(uid) == entitiesPtr->end()) {
1164 "data inconsistency");
1165 }
1166 if (dofsPtr->find((*rit)->getLocalUniqueId()) == dofsPtr->end()) {
1168 "data inconsistency");
1169 }
1170 }
1171 int row = (*rit)->getPetscGlobalDofIdx();
1172
1173 auto col_dofs = getColDofsPtr();
1174 for (auto cit = col_dofs->begin(); cit != col_dofs->end(); cit++) {
1175
1176 int col = (*cit)->getPetscGlobalDofIdx();
1177
1178 if (
row == rowPrint &&
col == colPrint) {
1179 MOFEM_LOG(
"SELF", Sev::noisy) <<
"fe:\n"
1180 << *numeredEntFiniteElementPtr;
1181 MOFEM_LOG(
"SELF", Sev::noisy) <<
"row:\n" << *(*rit);
1182 MOFEM_LOG(
"SELF", Sev::noisy) <<
"col:\n" << *(*cit);
1184 << "fe:\n"
1185 << numeredEntFiniteElementPtr->getBitRefLevel();
1186 MOFEM_LOG(
"SELF", Sev::noisy) <<
"row:\n"
1187 << (*rit)->getBitRefLevel();
1188 MOFEM_LOG(
"SELF", Sev::noisy) <<
"col:\n"
1189 << (*cit)->getBitRefLevel();
1190 }
1191
1193 }
1194
1195 if ((*rit)->getEntType() != MBVERTEX) {
1196
1197 auto range =
1198 row_dofs->get<Ent_mi_tag>().equal_range((*rit)->getEnt());
1199 int nb_dofs_on_ent = std::distance(range.first, range.second);
1200
1201 int max_order = (*rit)->getMaxOrder();
1202 if ((*rit)->getNbOfCoeffs() * (*rit)->getOrderNbDofs(max_order) !=
1203 nb_dofs_on_ent) {
1204
1205
1206
1207
1208
1210 << "Warning: Number of Dofs in Row different than number "
1211 "of dofs for given entity order "
1212 << "Field name: " << (*rit)->getName() << " "
1213 << (*rit)->getNbOfCoeffs() * (*rit)->getOrderNbDofs(max_order)
1214 << " != " << nb_dofs_on_ent;
1215 }
1216 }
1217 }
1218
1220 }
1221
1224
1225
1226 CHKERR MatAssemblyBegin(
A, MAT_FLUSH_ASSEMBLY);
1227 CHKERR MatAssemblyEnd(
A, MAT_FLUSH_ASSEMBLY);
1228
1230 }
1231 };
1232
1233
1234 CHKERR MatSetOption(
A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
1235
1237 MatView(
A, PETSC_VIEWER_STDOUT_WORLD);
1238 }
1239
1240 if (verb >=
NOISY) {
1241 MatView(
A, PETSC_VIEWER_DRAW_WORLD);
1242 std::string wait;
1243 std::cin >> wait;
1244 }
1245
1246 TestMatrixFillIn method(&m_field,
A, row_print, col_print);
1247
1248
1250 auto &prb_set = problems_ptr->get<Problem_mi_tag>();
1251 auto p_miit = prb_set.find(problem_name);
1252 if (p_miit == prb_set.end())
1254 "problem < %s > not found (top tip: check spelling)",
1255 problem_name.c_str());
1256 MOFEM_LOG_C(
"WORLD", Sev::inform,
"check problem < %s >",
1257 problem_name.c_str());
1258
1259
1261 for (auto &fe : *fe_ptr) {
1262 MOFEM_LOG_C(
"WORLD", Sev::verbose,
"\tcheck element %s",
1263 fe->getName().c_str());
1267 }
1268
1269 CHKERR MatAssemblyBegin(
A, MAT_FINAL_ASSEMBLY);
1270 CHKERR MatAssemblyEnd(
A, MAT_FINAL_ASSEMBLY);
1271
1272 PetscLogEventEnd(MOFEM_EVENT_checkMatrixFillIn, 0, 0, 0, 0);
1273
1275}
1276
1277template <>
1279MatrixManager::checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>(
1280 const std::string problem_name, int row_print, int col_print, int verb) {
1282
1283 SmartPetscObj<Mat>
A;
1284 CHKERR createMPIAIJWithArrays<PetscGlobalIdx_mi_tag>(problem_name,
A, verb);
1285 CHKERR MatSetOption(
A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
1286 CHKERR checkMatrixFillIn(problem_name, row_print, col_print,
A, verb);
1288}
1289
1290template <>
1291MoFEMErrorCode MatrixManager::checkMPIAIJMatrixFillIn<PetscGlobalIdx_mi_tag>(
1292 const std::string problem_name, int row_print, int col_print, int verb) {
1294
1295 SmartPetscObj<Mat>
A;
1296 CHKERR createMPIAIJ<PetscGlobalIdx_mi_tag>(problem_name,
A, verb);
1297 CHKERR MatSetOption(
A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
1298 CHKERR checkMatrixFillIn(problem_name, row_print, col_print,
A, verb);
1300}
1301
1302template <>
1303MoFEMErrorCode MatrixManager::createHybridL2MPIAIJ<PetscGlobalIdx_mi_tag>(
1304 const std::string problem_name, SmartPetscObj<Mat> &aij_ptr, int verb) {
1307
1309 auto p_miit = prb_ptr->get<Problem_mi_tag>().find(problem_name);
1310 if (p_miit == prb_ptr->get<Problem_mi_tag>().end()) {
1312 "problem < %s > is not found (top tip: check spelling)",
1313 problem_name.c_str());
1314 }
1315
1316 auto nb_rows = p_miit->getNbDofsRow();
1317 auto nb_cols = p_miit->getNbDofsCol();
1318 auto nb_loc_rows = p_miit->getNbLocalDofsRow();
1319 auto nb_loc_cols = p_miit->getNbLocalDofsCol();
1320
1321 auto row_ptr = p_miit->getNumeredRowDofsPtr();
1322 auto col_ptr = p_miit->getNumeredColDofsPtr();
1323
1325 for (
auto &
c : *col_ptr) {
1326 fields_ids |=
c->getId();
1327 }
1329 std::vector<int> fields_bit_numbers;
1330 for (
auto &
f : *fields) {
1331 if ((fields_ids &
f->getId()).any()) {
1332 fields_bit_numbers.push_back(
f->getBitNumber());
1333 }
1334 }
1335
1337 EntityHandle prev_ent = 0;
1338 int prev_dim = -1;
1339
1340
1341
1342
1343
1344 std::unordered_map<EntityHandle, Range> bridge_faces;
1346 const auto prb_bit = p_miit->getBitRefLevel();
1347 const auto prb_mask = p_miit->getBitRefLevelMask();
1348 auto on_problem_level = [&](EntityHandle cell) {
1349 auto it = ref_ents->find(cell);
1350 if (
it == ref_ents->end())
1351 return false;
1352 const auto &
bit = (*it)->getBitRefLevel();
1353 return (
bit & prb_bit).any() && (
bit & prb_mask) ==
bit;
1354 };
1355 auto get_adj = [&](
auto ent,
auto dim) ->
const Range & {
1356 if (prev_ent == ent && prev_dim == dim) {
1357 return adj;
1358 } else {
1359 adj.clear();
1362 false, bridge),
1363 "bridge adjacency");
1364 for (auto b : bridge) {
1365 auto it = bridge_faces.find(b);
1366 if (
it == bridge_faces.end()) {
1368 if (on_problem_level(b))
1371 "bridge faces");
1372 it = bridge_faces.emplace(b, std::move(
faces)).first;
1373 }
1374 adj.merge(
it->second);
1375 }
1376 prev_ent = ent;
1377 prev_dim = dim;
1378 return adj;
1379 }
1380 };
1381
1382
1383
1384 auto *pcomm = ParallelComm::get_pcomm(
1386
1387 int prev_bit_number = -1;
1388 EntityHandle prev_first_ent = 0;
1389 EntityHandle prev_second_ent = 0;
1391 Unique_mi_tag>::type::iterator;
1392 std::pair<IT, IT> pair_lo_hi;
1393 auto get_col_it = [&](auto bit_number, auto first_ent, auto second_ent) {
1394 if (bit_number == prev_bit_number && first_ent == prev_first_ent &&
1395 second_ent == prev_second_ent) {
1396 return pair_lo_hi;
1397 } else {
1398 auto lo_it = col_ptr->get<Unique_mi_tag>().lower_bound(
1399 DofEntity::getLoFieldEntityUId(bit_number, first_ent));
1400 auto hi_it = col_ptr->get<Unique_mi_tag>().upper_bound(
1401 DofEntity::getHiFieldEntityUId(bit_number, second_ent));
1402 pair_lo_hi = std::make_pair(lo_it, hi_it);
1403 prev_bit_number = bit_number;
1404 prev_first_ent = first_ent;
1405 prev_second_ent = second_ent;
1406 return pair_lo_hi;
1407 }
1408 };
1409
1410 auto create_ghost_vec = [&]() {
1411 SmartPetscObj<Vec>
v;
1415 CHKERR VecGhostUpdateBegin(
v, INSERT_VALUES, SCATTER_FORWARD);
1416 CHKERR VecGhostUpdateEnd(
v, INSERT_VALUES, SCATTER_FORWARD);
1418 };
1419
1420 auto v_o_nnz = create_ghost_vec();
1421 auto v_d_nnz = create_ghost_vec();
1422
1423
1424 double *o_nnz_real;
1425 CHKERR VecGetArray(v_o_nnz, &o_nnz_real);
1426 double *d_nnz_real;
1427 CHKERR VecGetArray(v_d_nnz, &d_nnz_real);
1428
1429 EntityHandle counted_ent = 0;
1430 int counted_owner = -1;
1431 PetscInt diagonal_nnz = 0, off_diagonal_nnz = 0;
1432 for (
auto r = row_ptr->begin();
r != row_ptr->end(); ++
r) {
1433
1434 auto row_loc_idx = (*r)->getPetscLocalDofIdx();
1435 if (row_loc_idx < 0)
1436 continue;
1437
1438 auto ent = (*r)->getEnt();
1439 const int row_owner = (*r)->getOwnerProc(pcomm);
1440
1441
1442 if (ent != counted_ent || row_owner != counted_owner) {
1443 diagonal_nnz = off_diagonal_nnz = 0;
1445 for (auto p = adj.pair_begin(); p != adj.pair_end(); ++p) {
1446 for (auto bit_number : fields_bit_numbers) {
1447 auto [lo_it, hi_it] = get_col_it(bit_number, p->first, p->second);
1448 for (; lo_it != hi_it; ++lo_it) {
1449 if ((*lo_it)->getPetscLocalDofIdx() < 0)
1450 continue;
1451 if ((*lo_it)->getOwnerProc(pcomm) == row_owner)
1452 ++diagonal_nnz;
1453 else
1454 ++off_diagonal_nnz;
1455 }
1456 }
1457 }
1458 counted_ent = ent;
1459 counted_owner = row_owner;
1460 }
1461 d_nnz_real[row_loc_idx] += diagonal_nnz;
1462 o_nnz_real[row_loc_idx] += off_diagonal_nnz;
1463 }
1464
1465 CHKERR VecRestoreArray(v_o_nnz, &o_nnz_real);
1466 CHKERR VecRestoreArray(v_d_nnz, &d_nnz_real);
1467
1468 auto update_vec = [&](
auto v) {
1470 CHKERR VecGhostUpdateBegin(
v, ADD_VALUES, SCATTER_REVERSE);
1471 CHKERR VecGhostUpdateEnd(
v, ADD_VALUES, SCATTER_REVERSE);
1475 };
1476
1477 CHKERR update_vec(v_o_nnz);
1478 CHKERR update_vec(v_d_nnz);
1479
1480 int o_nz = 0;
1481 int d_nz = 0;
1482
1483 CHKERR VecGetArray(v_d_nnz, &d_nnz_real);
1484 CHKERR VecGetArray(v_o_nnz, &o_nnz_real);
1485
1486 std::vector<int> d_nnz(nb_loc_rows, 0);
1487 std::vector<int> o_nnz(nb_loc_rows, 0);
1488 for (
auto r = row_ptr->begin();
r != row_ptr->end(); ++
r) {
1489
1490 auto row_loc_idx = (*r)->getPetscLocalDofIdx();
1491
1492 if (
1493
1494 row_loc_idx >= 0 && row_loc_idx < nb_loc_rows
1495
1496 ) {
1497 auto row_loc_idx = (*r)->getPetscLocalDofIdx();
1498 d_nz += d_nnz_real[row_loc_idx];
1499 d_nnz[row_loc_idx] = d_nnz_real[row_loc_idx];
1500 o_nz += o_nnz_real[row_loc_idx];
1501 o_nnz[row_loc_idx] = o_nnz_real[row_loc_idx];
1502 }
1503
1504 }
1505
1506 CHKERR VecRestoreArray(v_o_nnz, &o_nnz_real);
1507 CHKERR VecRestoreArray(v_d_nnz, &d_nnz_real);
1508
1509 if (verb >=
QUIET) {
1511 << "Hybrid L2 matrix d_nz: " << d_nz << " o_nz: " << o_nz;
1513 }
1514
1516 MOFEM_LOG(
"SYNC", Sev::noisy) <<
"Hybrid L2 matrix";
1517 int idx = 0;
1518 for (
auto &
d : d_nnz) {
1519 MOFEM_LOG(
"SYNC", Sev::noisy) << idx <<
": " <<
d;
1520 ++idx;
1521 }
1523 }
1524
1525 Mat a_raw;
1526 CHKERR MatCreateAIJ(m_field.
get_comm(), nb_loc_rows, nb_loc_cols, nb_rows,
1527 nb_cols, 0, d_nnz.data(), 0, o_nnz.data(), &a_raw);
1528 CHKERR MatSetOption(a_raw, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
1529 aij_ptr = SmartPetscObj<Mat>(a_raw);
1530
1533
1535}
1536
1537}
CGALInterfaceImpl & owner
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
std::vector< double > empty
One value per component.
#define MOFEM_LOG_SEVERITY_SYNC(comm, severity)
Synchronise "SYNC" on curtain severity level.
#define MOFEM_LOG_SYNCHRONISE(comm)
Synchronise "SYNC" channel.
#define MOFEM_LOG_C(channel, severity, format,...)
#define MatrixManagerFunctionBegin
Create adjacent matrices using different indices.
std::vector< FieldBitNumber > row
std::vector< FieldBitNumber > col
SmartPetscObj< Vec > diagonal
#define MAX_DOFS_ON_ENTITY
Maximal number of DOFs on entity.
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
@ MOFEM_DATA_INCONSISTENCY
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
multi_index_container< boost::shared_ptr< NumeredDofEntity >, indexed_by< ordered_unique< tag< Unique_mi_tag >, const_mem_fun< NumeredDofEntity::interface_type_DofEntity, UId, &NumeredDofEntity::getLocalUniqueId > >, ordered_non_unique< tag< Part_mi_tag >, member< NumeredDofEntity, unsigned int, &NumeredDofEntity::pArt > >, ordered_non_unique< tag< PetscGlobalIdx_mi_tag >, member< NumeredDofEntity, DofIdx, &NumeredDofEntity::petscGloablDofIdx > >, ordered_non_unique< tag< PetscLocalIdx_mi_tag >, member< NumeredDofEntity, DofIdx, &NumeredDofEntity::petscLocalDofIdx > >, ordered_non_unique< tag< Ent_mi_tag >, const_mem_fun< NumeredDofEntity::interface_type_DofEntity, EntityHandle, &NumeredDofEntity::getEnt > > > > NumeredDofEntity_multiIndex
MultiIndex container keeps NumeredDofEntity.
virtual const FiniteElement_multiIndex * get_finite_elements() const =0
Get the finite elements object.
virtual const Problem_multiIndex * get_problems() const =0
Get the problems object.
virtual const RefEntity_multiIndex * get_ref_ents() const =0
Get the ref ents object.
virtual const Field_multiIndex * get_fields() const =0
Get the fields object.
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
#define MOFEM_LOG_ATTRIBUTES(channel, bit)
Add attributes to channel.
virtual MoFEMErrorCode loop_finite_elements(const std::string problem_name, const std::string &fe_name, FEMethod &method, boost::shared_ptr< NumeredEntFiniteElement_multiIndex > fe_ptr=nullptr, MoFEMTypes bh=MF_EXIST, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr(), int verb=DEFAULT_VERBOSITY)=0
Make a loop over finite elements.
FTensor::Index< 'i', SPACE_DIM > i
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'j', 3 > j
FTensor::Index< 'k', 3 > k
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
std::bitset< BITFIELDID_SIZE > BitFieldId
Field Id.
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
implementation of Data Operators for Forces and Sources
CreateRowCompressedADJMatrix CreateRowComressedADJMatrix
Problem::EmptyFieldBlocks EmptyFieldBlocks
auto dimension_from_handle(const EntityHandle h)
get entity dimension form handle
boost::shared_ptr< CacheTuple > CacheTupleSharedPtr
virtual moab::Interface & get_moab()=0
virtual MPI_Comm & get_comm() const =0
virtual boost::shared_ptr< BasicEntityData > & get_basic_entity_data_ptr()=0
Get pointer to basic entity data.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
constexpr IntegrationType IT