FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
pmdcdsc_matrix.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/global/vector.hpp>
10#include <kernel/global/matrix.hpp>
11#include <kernel/lafem/dense_vector.hpp>
12#include <kernel/lafem/dense_vector_blocked.hpp>
13#include <kernel/lafem/sparse_matrix_csr.hpp>
14#include <kernel/lafem/sparse_matrix_bcsr.hpp>
15#include <kernel/lafem/sparse_matrix_cscr.hpp>
16#include <kernel/util/stop_watch.hpp>
17
18#include <vector>
19#include <map>
20#include <set>
21
22namespace FEAT
23{
24 namespace Global
25 {
29 template<typename MatrixB_, typename MatrixD_>
31
57 template<typename DT_, typename IT_, int dim_, typename MirrorV_, typename MirrorP_>
59 Global::Matrix<LAFEM::SparseMatrixBCSR<DT_, IT_, dim_, 1>, MirrorV_, MirrorP_>,
60 Global::Matrix<LAFEM::SparseMatrixBCSR<DT_, IT_, 1, dim_>, MirrorP_, MirrorV_>>
61 {
62 public:
63 typedef DT_ DataType;
64 typedef IT_ IndexType;
65 static constexpr int dim = dim_;
66
67 typedef MirrorV_ MirrorTypeV;
68 typedef MirrorP_ MirrorTypeP;
69
74
77
81
84
87
90
94
99
100 static constexpr bool is_global = true;
101 static constexpr bool is_local = false;
102
103 protected:
110
118
145
147 std::vector<int> _ranks;
148
158 std::vector<MirrorTypeP> _pres_mirrors;
159
167 std::vector<MirrorTypeP> _data_mirrors;
168
175 std::vector<Adjacency::Graph> _neighbor_graphs;
176
184 std::vector<NeighMatrixTypeS> _neighbor_matrices;
185
198
209
216
217 public:
234 const GlobalVectorTypeV& diagonal_a,
235 const GlobalMatrixTypeB& matrix_b,
236 const GlobalMatrixTypeD& matrix_d) :
237 _diagonal_a(diagonal_a),
238 _matrix_b(matrix_b),
239 _matrix_d(matrix_d)
240 {
241 }
242
245 {
246 }
247
249 const Dist::Comm* get_comm() const
250 {
251 return _diagonal_a.get_comm();
252 }
253
256 {
257 return _matrix_d.create_vector_l();
258 }
259
262 {
263 return _matrix_b.create_vector_r();
264 }
265
268 {
269 return _matrix_s;
270 }
271
283 void extract_diag(GlobalVectorTypeP& vec_diag) const
284 {
285 _matrix_s.extract_diag(vec_diag.local());
286 }
287
300 {
301 GlobalVectorTypeP vec_diag = _matrix_d.create_vector_l();
302 _matrix_s.extract_diag(vec_diag.local());
303 return vec_diag;
304 }
305
308 {
309 watch_init_symbolic.reset();
310 watch_init_sym_matrix_loc.reset();
311 watch_init_sym_pres_mirror.reset();
312 watch_init_sym_reduced_b.reset();
313 watch_init_sym_data_mirror.reset();
314 watch_init_sym_neighbor_s.reset();
315 watch_init_numeric.reset();
316 watch_init_num_matrix_loc.reset();
317 watch_init_num_gather_b.reset();
318 watch_init_num_premult_da.reset();
319 watch_init_num_neighbor_s.reset();
320 watch_apply.reset();
321 watch_apply_matrix_loc.reset();
322 watch_apply_neighbor_s.reset();
323 }
324
334 {
335 static constexpr std::size_t nt = 14;
336 double tsum[nt], tmax[nt], tloc[nt] =
337 {
338 watch_init_symbolic.elapsed(),
339 watch_init_sym_matrix_loc.elapsed(),
340 watch_init_sym_pres_mirror.elapsed(),
341 watch_init_sym_reduced_b.elapsed(),
342 watch_init_sym_data_mirror.elapsed(),
343 watch_init_sym_neighbor_s.elapsed(),
344 watch_init_numeric.elapsed(),
345 watch_init_num_matrix_loc.elapsed(),
346 watch_init_num_gather_b.elapsed(),
347 watch_init_num_premult_da.elapsed(),
348 watch_init_num_neighbor_s.elapsed(),
349 watch_apply.elapsed(),
350 watch_apply_matrix_loc.elapsed(),
351 watch_apply_neighbor_s.elapsed()
352 };
353
354 this->get_comm()->allreduce(tloc, tsum, nt, Dist::op_sum);
355 this->get_comm()->allreduce(tloc, tmax, nt, Dist::op_max);
356
357 // divide sum by number of ranks to obtain mean
358 {
359 const double ds = 1.0 / double(this->get_comm()->size());
360 for(std::size_t i(0); i < nt; ++i)
361 tsum[i] *= ds;
362 }
363
364 String s;
365 s += String(34, ' ') + "Mean Time Max Time\n";
366 s += _fmt_time(tsum[0], tmax[0], "Total Symbolic Initialization");
367 s += _fmt_time(tsum[0], tmax[0], tsum[1], tmax[1], "Local Schur Matrix Structure");
368 s += _fmt_time(tsum[0], tmax[0], tsum[2], tmax[2], "Pressure Mirror");
369 s += _fmt_time(tsum[0], tmax[0], tsum[3], tmax[3], "Reduced-B Matrix Structure");
370 s += _fmt_time(tsum[0], tmax[0], tsum[4], tmax[4], "Reduced-B Data Mirror");
371 s += _fmt_time(tsum[0], tmax[0], tsum[5], tmax[5], "Neighbor Matrix Structure");
372 double tsym_other_sum = tsum[0] - tsum[1] - tsum[2] - tsum[3] - tsum[4] - tsum[5];
373 double tsym_other_max = tmax[0] - tmax[1] - tmax[2] - tmax[3] - tmax[4] - tmax[5];
374 s += _fmt_time(tsum[0], tmax[0], tsym_other_sum, tsym_other_max, "Other Symbolic");
375
376 s += _fmt_time(tsum[6], tmax[6], "Total Numeric Initialization");
377 s += _fmt_time(tsum[6], tmax[6], tsum[7], tmax[7], "Local Schur Matrix Values");
378 s += _fmt_time(tsum[6], tmax[6], tsum[8], tmax[8], "Reduced-B Gather");
379 s += _fmt_time(tsum[6], tmax[6], tsum[9], tmax[9], "Pre-Multiply D*A");
380 s += _fmt_time(tsum[6], tmax[6], tsum[10], tmax[10], "Neighbor Matrix Values");
381 double tnum_other_sum = tsum[6] - tsum[7] - tsum[8] - tsum[9] - tsum[10];
382 double tnum_other_max = tmax[6] - tmax[7] - tmax[8] - tmax[9] - tmax[10];
383 s += _fmt_time(tsum[6], tmax[6], tnum_other_sum, tnum_other_max, "Other Numeric");
384
385 s += _fmt_time(tsum[11], tmax[11], "Total Matrix Apply");
386 s += _fmt_time(tsum[11], tmax[11], tsum[12], tmax[12], "Local Schur Matrix");
387 s += _fmt_time(tsum[11], tmax[11], tsum[13], tmax[13], "Neighbor Schur Matrix");
388 double tapp_other_sum = tsum[11] - tsum[12] - tsum[13];
389 double tapp_other_max = tmax[11] - tmax[12] - tmax[13];
390 s += _fmt_time(tsum[11], tmax[11], tapp_other_sum, tapp_other_max, "Other Apply");
391 return s;
392 }
393
397 void init()
398 {
399 init_symbolic();
400 init_numeric();
401 }
402
424 {
425 watch_init_symbolic.start();
426
427 // get the velocity and pressure gates
428 const GateTypeV* gate_v = this->_matrix_b.get_row_gate();
429 const GateTypeP* gate_p = this->_matrix_d.get_row_gate();
430 XASSERT(gate_v != nullptr);
431 XASSERT(gate_p != nullptr);
432
433 // the pressure gate must be empty, otherwise the pressure space is not discontinuous
434 XASSERTM(gate_p->_ranks.empty(), "pressure space is not discontinuous");
435
436 // compute the local matrix structure of S by D * B
437 {
438 watch_init_sym_matrix_loc.start();
439
440 // compose structures of D and B
441 Adjacency::Graph graph_s(Adjacency::RenderType::injectify_sorted, _matrix_d.local().adjactor(), _matrix_b.local().adjactor());
442 // create the matrix layout of S
443 _matrix_s = LocalMatrixTypeS(graph_s);
444
445 watch_init_sym_matrix_loc.stop();
446 }
447
448 // get our communicator
449 const Dist::Comm& comm = *gate_v->get_comm();
450
451 // get neighbor ranks
452 this->_ranks = gate_v->_ranks;
453
454 // get the number of our neighbors
455 const std::size_t num_neighs = this->_ranks.size();
456 if(num_neighs <= std::size_t(0))
457 {
458 watch_init_symbolic.stop();
459 return; // no neighbors, no problems :)
460 }
461
462 // copy the layout of B into (D*A)^T
463 this->_matrix_da = this->_matrix_b.local().clone(LAFEM::CloneMode::Layout);
464
465 // resize our member arrays
466 this->_pres_mirrors.resize(num_neighs);
467 this->_data_mirrors.resize(num_neighs);
468 this->_neighbor_graphs.resize(num_neighs);
469 this->_neighbor_matrices.resize(num_neighs);
470
471 // allocate a vector of graphs for B
472 std::vector<Adjacency::Graph> my_graphs(num_neighs);
473
474 // loop over all neighbor processes
475 for(std::size_t i(0); i < num_neighs; ++i)
476 {
477 // get the velocity mirror
478 const MirrorTypeV& mirror_v = gate_v->_mirrors.at(i);
479
480 // assemble the pressure mirror for this neighbor
481 watch_init_sym_pres_mirror.start();
482 MirrorTypeP& mirror_p = this->_pres_mirrors.at(i);
483 this->_asm_pres_mirror(mirror_p, mirror_v, this->_matrix_b.local());
484 watch_init_sym_pres_mirror.stop();
485
486 // assemble reduced B-matrix graph
487 watch_init_sym_reduced_b.start();
488 my_graphs.at(i) = this->_asm_reduced_b( mirror_p, mirror_v, this->_matrix_b.local());
489 watch_init_sym_reduced_b.stop();
490
491 // assemble B' data mirror
492 watch_init_sym_data_mirror.start();
493 this->_data_mirrors.at(i) = this->_asm_data_mirror(mirror_p, mirror_v, this->_matrix_b.local(), my_graphs.at(i));
494 watch_init_sym_data_mirror.stop();
495 }
496
497 // dimension send/receive buffers and requests
498 std::vector<std::array<Index,3>> recv_dims(num_neighs), send_dims(num_neighs);
499 Dist::RequestVector recv_reqs(num_neighs), send_reqs(num_neighs);
500
501 // post receive requests for dimensions
502 for(std::size_t i(0); i < num_neighs; ++i)
503 {
504 recv_reqs[i] = comm.irecv(recv_dims.at(i).data(), std::size_t(3), this->_ranks.at(i));
505 }
506
507 // send dimensions
508 for(std::size_t i(0); i < num_neighs; ++i)
509 {
510 const Adjacency::Graph& g = my_graphs.at(i);
511 auto& sdim = send_dims.at(i);
512 sdim[0] = g.get_num_nodes_domain(); // corresponds to velocity mirror index size
513 sdim[1] = g.get_num_nodes_image();
514 sdim[2] = g.get_num_indices();
515 send_reqs[i] = comm.isend(sdim.data(), std::size_t(3), this->_ranks.at(i));
516 }
517
518 // process all pending receives
519 for(std::size_t i(0u); recv_reqs.wait_any(i); )
520 {
521 // get received dimension
522 auto& rdim = recv_dims.at(i);
523
524 // the first dimension must match our velocity mirror index set size
525 XASSERT(rdim[0] == gate_v->_mirrors.at(i).num_indices());
526
527 // allocate graph of corresponding dimensions
528 this->_neighbor_graphs.at(i) = Adjacency::Graph(rdim[0], rdim[1], rdim[2]);
529 }
530
531 // post domain-pointer array receives
532 for(std::size_t i(0); i < num_neighs; ++i)
533 {
534 recv_reqs[i] = comm.irecv(this->_neighbor_graphs.at(i).get_domain_ptr(),
535 recv_dims.at(i)[0] + std::size_t(1), this->_ranks.at(i));
536 }
537
538 // wait for all previous sends to finish
539 send_reqs.wait_all();
540
541 // post domain-pointer array sends
542 for(std::size_t i(0); i < num_neighs; ++i)
543 {
544 send_reqs[i] = comm.isend(my_graphs.at(i).get_domain_ptr(),
545 send_dims.at(i)[0] + std::size_t(1), this->_ranks.at(i));
546 }
547
548 // wait for all pending receives to finish
549 recv_reqs.wait_all();
550
551 // post image-index array receives
552 for(std::size_t i(0); i < num_neighs; ++i)
553 {
554 recv_reqs[i] = comm.irecv(this->_neighbor_graphs.at(i).get_image_idx(),
555 recv_dims.at(i)[2], this->_ranks.at(i));
556 }
557
558 // wait for all previous sends to finish
559 send_reqs.wait_all();
560
561 // post image-index array sends
562 for(std::size_t i(0); i < num_neighs; ++i)
563 {
564 send_reqs[i] = comm.isend(my_graphs.at(i).get_image_idx(), send_dims.at(i)[2], this->_ranks.at(i));
565 }
566
567 // wait for all pending receives to finish
568 recv_reqs.wait_all();
569
570 // wait for all previous sends to finish
571 send_reqs.wait_all();
572
573 // compute Schur-matrix structures for neighbors
574 for(std::size_t i(0); i < num_neighs; ++i)
575 {
576 watch_init_sym_neighbor_s.start();
577
578 // D*M^T = (M*B)^T
579 Adjacency::Graph graph_dm(Adjacency::RenderType::injectify_transpose, gate_v->_mirrors.at(i).adjactor(), this->_matrix_b.local().adjactor());
580
581 // S = (D*M^T) * B'
582 Adjacency::Graph graph_s(Adjacency::RenderType::injectify_sorted, graph_dm, this->_neighbor_graphs.at(i));
583
584 // allocate Schur-matrix
585 this->_neighbor_matrices.at(i).convert(NeighMatrixTypeS(graph_s));
586
587 watch_init_sym_neighbor_s.stop();
588 }
589
590 // that's it
591 watch_init_symbolic.stop();
592 }
593
606 {
607 watch_init_numeric.start();
608
609 // pre-multiply local matrix product
610 watch_init_num_matrix_loc.start();
611 this->_matrix_s.format();
612 _asm_local_schur_matrix(this->_matrix_s, this->_matrix_d.local(), this->_diagonal_a.local(), this->_matrix_b.local());
613 watch_init_num_matrix_loc.stop();
614
615 // get the number of our neighbors
616 const std::size_t num_neighs = this->_ranks.size();
617 if(num_neighs <= std::size_t(0))
618 {
619 watch_init_numeric.stop();
620 return; // no neighbors, no problems :)
621 }
622
623 // get our communicator
624 const Dist::Comm& comm = *this->_matrix_b.get_comm();
625
626 // send/receive buffers and requests
627 std::vector<BufferVectorType> recv_bufs(num_neighs), send_bufs(num_neighs);
628 Dist::RequestVector recv_reqs(num_neighs), send_reqs(num_neighs);
629 std::vector<Memory::TypedView<DataType>> recv_views(num_neighs), send_views(num_neighs);
630
631 // allocate receive buffer matrices B' and post receives
632 for(std::size_t i(0); i < num_neighs; ++i)
633 {
634 recv_bufs.at(i) = BufferVectorType(Index(dim) * this->_neighbor_graphs.at(i).get_num_indices());
635 recv_views[i] = recv_bufs.at(i).elements_view_w();
636 recv_reqs[i] = comm.irecv(recv_views[i].get_w(), recv_bufs.at(i).size(), this->_ranks.at(i));
637 }
638
639 // extract reduced matrix data and post send
640 for(std::size_t i(0); i < num_neighs; ++i)
641 {
642 watch_init_num_gather_b.start();
643 send_bufs.at(i) = _gather_b(this->_data_mirrors.at(i), this->_matrix_b.local());
644 send_views[i] = send_bufs.at(i).elements_view_r();
645 watch_init_num_gather_b.stop();
646 send_reqs[i] = comm.isend(send_views[i].get_r(), send_bufs.at(i).size(), this->_ranks.at(i));
647 }
648
649 // pre-multiply D*A and store in transposed form, i.e. CSC rather than CSR
650 watch_init_num_premult_da.start();
651 _premult_da(this->_matrix_da, this->_matrix_d.local(), this->_diagonal_a.local());
652 watch_init_num_premult_da.stop();
653
654 // process receives and compute neighbor schur matrices
655 for(std::size_t i(0u); recv_reqs.wait_any(i); )
656 {
657 watch_init_num_neighbor_s.start();
658 recv_views[i].release();
659 this->_neighbor_matrices.at(i).format();
660 _asm_neighbor_schur_matrix(this->_neighbor_matrices.at(i), this->_matrix_da,
661 this->_matrix_b.get_row_gate()->_mirrors.at(i), this->_neighbor_graphs.at(i), recv_bufs.at(i));
662 watch_init_num_neighbor_s.stop();
663 }
664
665 // wait for all previous sends to finish
666 send_reqs.wait_all();
667 watch_init_numeric.stop();
668 }
669
676 void apply(VectorTypeL& r, const VectorTypeR& x) const
677 {
678 watch_apply.start();
679 this->_apply(r.local(), x.local(), r.local(), DataType(1), true);
680 watch_apply.stop();
681 }
682
691 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, const DataType alpha = DataType(1)) const
692 {
693 watch_apply.start();
694 this->_apply(r.local(), x.local(), y.local(), alpha, false);
695 watch_apply.stop();
696 }
697
704 void adp_compute_counts(Index& global_dof_offset, Index& global_dof_count,
705 Index& owned_dof_count, Index& owned_num_nzes, Index& global_num_nzes) const
706 {
707 // initialize values for serial case
708 global_dof_offset = Index(0);
709 global_dof_count = owned_dof_count = _matrix_s.num_rows();
710
711 // compute number of non-zero entries
712 owned_num_nzes = _matrix_s.num_nzes();
713 for(const auto& x : _neighbor_matrices)
714 owned_num_nzes += x.num_nzes();
715 global_num_nzes = owned_num_nzes;
716
717 // compute our global DOF offset and count
718 if(!_ranks.empty())
719 {
720 this->get_comm()->exscan(&owned_dof_count, &global_dof_offset, std::size_t(1), Dist::op_sum);
721 this->get_comm()->allreduce(&owned_dof_count, &global_dof_count, std::size_t(1), Dist::op_sum);
722 this->get_comm()->allreduce(&owned_num_nzes, &global_num_nzes, std::size_t(1), Dist::op_sum);
723 }
724 }
725
745 template<typename RPT_, typename CIT_>
746 void adp_upload_symbolic(RPT_* row_ptr, CIT_* col_idx, Index global_dof_offset) const
747 {
748 // maximum allowed row-pointer/column index values assuming signed int types
749 static constexpr std::uint64_t max_rpt = 1ull << (8*sizeof(RPT_) - 1);
750 static constexpr std::uint64_t max_cit = 1ull << (8*sizeof(CIT_) - 1);
751
752 // prevent "unused variable" warnings in non-debug builds
753 (void)max_rpt;
754 (void)max_cit;
755
756 // no neighbors?
757 if(_ranks.empty())
758 {
759 // simply copy our local matrix S
760 const Index n = _matrix_s.num_rows();
761 const Index m = _matrix_s.num_nzes();
762 const Memory::TypedView<IndexType> row_ptr_s = _matrix_s.row_ptr_view_r();
763 const Memory::TypedView<IndexType> col_idx_s = _matrix_s.col_idx_view_r();
764
765 FEAT_PRAGMA_OMP(parallel for)
766 for(Index i = 0; i <= n; ++i)
767 {
768 ASSERTM(std::uint64_t(row_ptr_s[i]) < max_rpt, "row-pointer exceeds RPT_ type range!");
769 row_ptr[i] = RPT_(row_ptr_s[i]);
770 }
771
772 FEAT_PRAGMA_OMP(parallel for)
773 for(Index i = 0; i < m; ++i)
774 {
775 ASSERTM(std::uint64_t(col_idx_s[i]) < max_cit, "column-index exceeds CIT_ type range!");
776 col_idx[i] = CIT_(col_idx_s[i]);
777 }
778
779 return;
780 }
781
782 // get our communicator
783 const Dist::Comm& comm = *this->get_comm();
784
785 const std::size_t num_neighs = this->_ranks.size();
786
787 // The columns of our neighbor matrices correspond to the entries in the pressure mirror.
788 // However, for the desired ADP matrix, we have to translate these into global DOF indices.
789 // For this, each process has to map the DOF in its pressure mirrors to global DOFs and then
790 // send these DOF indices to the corresponding neighbor, so that it can map the column
791 // indices of its neighbor matrix to global DOF indices.
792
793 // send/receive mirrors and requests
794 std::vector<std::vector<IndexType>> recv_dofs(num_neighs), send_dofs(num_neighs);
795 Dist::RequestVector recv_reqs(num_neighs), send_reqs(num_neighs);
796
797 // allocate receive vectors and post receives
798 for(std::size_t i(0); i < num_neighs; ++i)
799 {
800 recv_dofs.at(i).resize(_neighbor_graphs.at(i).get_num_nodes_image());
801 recv_reqs[i] = comm.irecv(recv_dofs.at(i).data(), recv_dofs.at(i).size(), this->_ranks.at(i));
802 }
803
804 // translate our pressure mirrors to global DOF vectors and post sends
805 for(std::size_t i(0); i < num_neighs; ++i)
806 {
807 const Index num_idx = _pres_mirrors.at(i).num_indices();
808 const Memory::TypedView<IndexType> pidx = _pres_mirrors.at(i).indices_view_r();
809 send_dofs.at(i).resize(num_idx);
810 IndexType* sidx = send_dofs.at(i).data();
811 for(Index k(0); k < num_idx; ++k)
812 sidx[k] = global_dof_offset + pidx[k];
813 send_reqs[i] = comm.isend(send_dofs.at(i).data(), send_dofs.at(i).size(), this->_ranks.at(i));
814 }
815
816 // get local matrix stuff
817 const Index num_rows = _matrix_s.num_rows();
818 const Memory::TypedView<IndexType> row_ptr_s = _matrix_s.row_ptr_view_r();
819 const Memory::TypedView<IndexType> col_idx_s = _matrix_s.col_idx_view_r();
820
821 // compute number of non-zeros per row and total
822 std::vector<IndexType> row_aux(num_rows, IndexType(0));
823 for(Index i(0); i < num_rows; ++i)
824 row_aux[i] = (row_ptr_s[i+1] - row_ptr_s[i]);
825
826 for(const auto& x : _neighbor_matrices)
827 {
828 const Index used_rows = x.num_nonzero_rows();
829 const Memory::TypedView<IndexType> row_ptr_x = x.row_ptr_view_r();
830 const Memory::TypedView<IndexType> row_idx_x = x.row_idx_view_r();
831 for(Index i(0); i < used_rows; ++i)
832 row_aux[row_idx_x[i]] += (row_ptr_x[i+1] - row_ptr_x[i]);
833 }
834
835 // compute row pointer array and store backup in aux
836 row_ptr[0] = RPT_(0);
837 for(Index i(0); i < num_rows; ++i)
838 {
839 row_ptr[i+1] = row_ptr[i] + row_aux[i];
840 row_aux[i] = IndexType(row_ptr[i]);
841 }
842
843 // Note: For the sake of compatibility with picky third-party libraries, we want to
844 // ensure that the column indices of the output matrix are in ascending order.
845 // For this, we have to combine the matrix layout from our own local matrix and
846 // the matrices of our neighbors in rank-ascending order. So we first create two
847 // rank maps for our neighbors with lower and higher ranks, so that we can easily
848 // loop over all matrices in rank-order.
849
850 // create two neighbors maps: one of all lower ranks and one of all higher ranks
851 std::map<int, std::size_t> neigh_map_l, neigh_map_h;
852 for(std::size_t ineigh(0); ineigh < num_neighs; ++ineigh)
853 {
854 if(_ranks.at(ineigh) < comm.rank())
855 neigh_map_l.emplace(_ranks.at(ineigh), ineigh);
856 else
857 neigh_map_h.emplace(_ranks.at(ineigh), ineigh);
858 }
859
860 // wait for all receive requests to finish
861 recv_reqs.wait_all();
862
863 // first, insert all neighbor matrices with a lower rank in rank-ascending order
864 for(auto it = neigh_map_l.begin(); it != neigh_map_l.end(); ++it)
865 {
866 std::size_t ineigh = it->second;
867 const Index used_rows = _neighbor_matrices.at(ineigh).num_nonzero_rows();
868 const Memory::TypedView<IndexType> row_ptr_x = _neighbor_matrices.at(ineigh).row_ptr_view_r();
869 const Memory::TypedView<IndexType> row_idx_x = _neighbor_matrices.at(ineigh).row_idx_view_r();
870 const Memory::TypedView<IndexType> col_idx_x = _neighbor_matrices.at(ineigh).col_idx_view_r();
871 const IndexType* dof_idx_x = recv_dofs.at(ineigh).data();
872 for(Index i(0); i < used_rows; ++i)
873 {
874 IndexType& k = row_aux[row_idx_x[i]];
875 for(IndexType j(row_ptr_x[i]); j < row_ptr_x[i + 1]; ++j, ++k)
876 col_idx[k] = CIT_(dof_idx_x[col_idx_x[j]]);
877 }
878 }
879
880 // now insert our local matrix S
881 for(Index i(0); i < num_rows; ++i)
882 {
883 IndexType k = row_aux[i];
884 for(IndexType j(row_ptr_s[i]); j < row_ptr_s[i + 1]; ++j, ++k)
885 col_idx[k] = CIT_(global_dof_offset + col_idx_s[j]);
886 row_aux[i] = k;
887 }
888
889 // finally, insert all neighbor matrices with a higher rank in rank-ascending order
890 for(auto it = neigh_map_h.begin(); it != neigh_map_h.end(); ++it)
891 {
892 std::size_t ineigh = it->second;
893 const Index used_rows = _neighbor_matrices.at(ineigh).num_nonzero_rows();
894 const Memory::TypedView<IndexType> row_ptr_x = _neighbor_matrices.at(ineigh).row_ptr_view_r();
895 const Memory::TypedView<IndexType> row_idx_x = _neighbor_matrices.at(ineigh).row_idx_view_r();
896 const Memory::TypedView<IndexType> col_idx_x = _neighbor_matrices.at(ineigh).col_idx_view_r();
897 const IndexType* dof_idx_x = recv_dofs.at(ineigh).data();
898 for(Index i(0); i < used_rows; ++i)
899 {
900 IndexType& k = row_aux[row_idx_x[i]];
901 for(IndexType j(row_ptr_x[i]); j < row_ptr_x[i + 1]; ++j, ++k)
902 col_idx[k] = CIT_(dof_idx_x[col_idx_x[j]]);
903 }
904 }
905
906 // wait for all send requests to finish
907 send_reqs.wait_all();
908
909#ifdef DEBUG
910 // sanity check: ensure that the column indices are sorted correctly
911 for(Index i = 0; i < num_rows; ++i)
912 {
913 for(RPT_ j(row_ptr[i]); j + 1 < row_ptr[i + 1]; ++j)
914 {
915 ASSERT(col_idx[j] < col_idx[j+1]);
916 }
917 }
918#endif // DEBUG
919 }
920
936 template<typename DTV_, typename RPT_, typename CIT_>
937 void adp_upload_numeric(DTV_* val, const RPT_* row_ptr, const CIT_* DOXY(col_idx)) const
938 {
939 // no neighbors?
940 if(_ranks.empty())
941 {
942 const Index num_nzes = _matrix_s.num_nzes();
943 const Memory::TypedView<DataType> val_s = _matrix_s.val_view_r();
944
945 FEAT_PRAGMA_OMP(parallel for)
946 for(Index i = 0; i < num_nzes; ++i)
947 val[i] = DTV_(val_s[i]);
948
949 return;
950 }
951
952 // get number of local DOFs
953 const Index num_rows = _matrix_s.num_rows();
954
955 // get row pointer arrays
956 const Memory::TypedView<IndexType> row_ptr_s = _matrix_s.row_ptr_view_r();
957 const Memory::TypedView<DataType> val_s = _matrix_s.val_view_r();
958
959 // make a copy of the row pointer
960 std::vector<IndexType> row_aux(num_rows);
961
962 // copy the row-pointer array
963 for(Index i(0); i < num_rows; ++i)
964 row_aux[i] = IndexType(row_ptr[i]);
965
966 // get this process's rank
967 const int my_rank = this->get_comm()->rank();
968
969 // create two neighbors maps: one of all lower ranks and one of all higher ranks
970 std::map<int, std::size_t> neigh_map_l, neigh_map_h;
971 for(std::size_t ineigh(0); ineigh < _ranks.size(); ++ineigh)
972 {
973 if(_ranks.at(ineigh) < my_rank)
974 neigh_map_l.emplace(_ranks.at(ineigh), ineigh);
975 else
976 neigh_map_h.emplace(_ranks.at(ineigh), ineigh);
977 }
978
979 // first, copy all neighbor matrices with a lower rank in rank-ascending order
980 for(auto it = neigh_map_l.begin(); it != neigh_map_l.end(); ++it)
981 {
982 std::size_t ineigh = it->second;
983 const Index used_rows = _neighbor_matrices.at(ineigh).num_nonzero_rows();
984 const Memory::TypedView<IndexType> row_ptr_x = _neighbor_matrices.at(ineigh).row_ptr_view_r();
985 const Memory::TypedView<IndexType> row_idx_x = _neighbor_matrices.at(ineigh).row_idx_view_r();
986 const Memory::TypedView<DataType> val_x = _neighbor_matrices.at(ineigh).val_view_r();
987 for(Index i(0); i < used_rows; ++i)
988 {
989 IndexType& k = row_aux[row_idx_x[i]];
990 for(IndexType j(row_ptr_x[i]); j < row_ptr_x[i + 1]; ++j, ++k)
991 val[k] = DTV_(val_x[j]);
992 }
993 }
994
995 // now copy our own matrix
996 for(Index i(0); i < num_rows; ++i)
997 {
998 Index k = row_aux[i];
999 for(Index j(row_ptr_s[i]); j < row_ptr_s[i+1]; ++j, ++k)
1000 val[k] = DTV_(val_s[j]);
1001 row_aux[i] = k;
1002 }
1003
1004 // finally, copy all neighbor matrices with a higher rank in rank-ascending order
1005 for(auto it = neigh_map_h.begin(); it != neigh_map_h.end(); ++it)
1006 {
1007 std::size_t ineigh = it->second;
1008 const Index used_rows = _neighbor_matrices.at(ineigh).num_nonzero_rows();
1009 const Memory::TypedView<IndexType> row_ptr_x = _neighbor_matrices.at(ineigh).row_ptr_view_r();
1010 const Memory::TypedView<IndexType> row_idx_x = _neighbor_matrices.at(ineigh).row_idx_view_r();
1011 const Memory::TypedView<DataType> val_x = _neighbor_matrices.at(ineigh).val_view_r();
1012 for(Index i(0); i < used_rows; ++i)
1013 {
1014 IndexType& k = row_aux[row_idx_x[i]];
1015 for(IndexType j(row_ptr_x[i]); j < row_ptr_x[i + 1]; ++j, ++k)
1016 val[k] = DTV_(val_x[j]);
1017 }
1018 }
1019
1020#ifdef DEBUG
1021 // sanity check: ensure that all entries have been processed
1022 for(Index i(0); i < num_rows; ++i)
1023 {
1024 ASSERT(row_aux[i] == IndexType(row_ptr[i+1]));
1025 }
1026#endif // DEBUG
1027 }
1028
1029 protected:
1031 static String _fmt_time(double tsum_total, double tmax_total, String st)
1032 {
1033 String s = st.pad_back(30, '.') + ":";
1034 s += stringify_fp_fix(tsum_total, 6, 12) + " :";
1035 s += stringify_fp_fix(tmax_total, 6, 12) + "\n";
1036 return s;
1037 }
1039 static String _fmt_time(double tsum_total, double tmax_total, double tsum, double tmax, String st)
1040 {
1041 String s = st.pad_back(30, '.') + ":";
1042 s += stringify_fp_fix(tsum, 6, 12) + " :";
1043 s += stringify_fp_fix(tmax, 6, 12) + " [";
1044 if(tsum_total > 1E-8 * Math::abs(tsum))
1045 s += stringify_fp_fix(100.0*tsum/tsum_total, 2, 6) + "% :";
1046 else
1047 s += " --- :";
1048 if(tmax_total > 1E-8 * Math::abs(tmax))
1049 s += stringify_fp_fix(100.0*tmax/tmax_total, 2, 6) + "% ]\n";
1050 else
1051 s += " --- ]\n";
1052 return s;
1053 }
1054
1068 static void _asm_pres_mirror(MirrorTypeP& mirror_p, const MirrorTypeV& mirror_v, const LocalMatrixTypeB& matrix_b)
1069 {
1070 const Index num_dof_mir_v = mirror_v.num_indices();
1071 const Memory::TypedView<IndexType> velo_idx = mirror_v.indices_view_r();
1072 const Memory::TypedView<IndexType> row_ptr = matrix_b.row_ptr_view_r();
1073 const Memory::TypedView<IndexType> col_idx = matrix_b.col_idx_view_r();
1074
1075 // loop over all rows of B, which are indexed in the velocity mirror,
1076 // and add all column indices (pressure DOFs) into the pressure DOF set
1077 std::set<IndexType> dof_set;
1078 for(Index i(0); i < num_dof_mir_v; ++i)
1079 {
1080 const Index irow = velo_idx[i];
1081 for(IndexType j(row_ptr[irow]); j < row_ptr[irow+1]; ++j)
1082 dof_set.insert(col_idx[j]);
1083 }
1084
1085 // convert DOF set into a mirror
1086 mirror_p = MirrorTypeP(matrix_b.num_cols(), Index(dof_set.size()));
1087 Memory::TypedView<IndexType> idx_v = mirror_p.indices_view_w();
1088 IndexType* pidx = idx_v.get_w();
1089 for(auto it = dof_set.begin(); it != dof_set.end(); ++it, ++pidx)
1090 *pidx = *it;
1091 }
1092
1108 static Adjacency::Graph _asm_reduced_b(const MirrorTypeP& mirror_p, const MirrorTypeV& mirror_v,
1109 const LocalMatrixTypeB& matrix_b)
1110 {
1111 const Index num_dof_mir_v = mirror_v.num_indices();
1112 const Index num_dof_mir_p = mirror_p.num_indices();
1113 const Memory::TypedView<IndexType> velo_idx = mirror_v.indices_view_r();
1114 const Memory::TypedView<IndexType> pres_idx = mirror_p.indices_view_r();
1115 const Memory::TypedView<IndexType> row_ptr = matrix_b.row_ptr_view_r();
1116 const Memory::TypedView<IndexType> col_idx = matrix_b.col_idx_view_r();
1117
1118 // count number of non-zeros in indexed rows of B = non-zeros in B'
1119 Index num_red_nzes = Index(0);
1120 for(Index i(0); i < num_dof_mir_v; ++i)
1121 {
1122 const Index irow = velo_idx[i];
1123 num_red_nzes += Index(row_ptr[irow+1] - row_ptr[irow]);
1124 }
1125
1126 // allocate matrix graph for reduced part B' of B
1127 Adjacency::Graph graph(num_dof_mir_v, num_dof_mir_p, num_red_nzes);
1128 Index* dom_ptr = graph.get_domain_ptr();
1129 Index* img_idx = graph.get_image_idx();
1130
1131 // loop over all rows of reduced part B'
1132 dom_ptr[0] = Index(0);
1133 for(Index i(0); i < num_dof_mir_v; ++i)
1134 {
1135 // get the B-row index of the i-th row of B'
1136 const IndexType irow = velo_idx[i];
1137 // loop over all non-zeroes in the row of B / B'
1138 for(IndexType j(row_ptr[irow]), k(dom_ptr[i]); j < row_ptr[irow+1]; ++j, ++k)
1139 {
1140 // initialize invalid index for assertion below
1141 img_idx[k] = ~IndexType(0);
1142
1143 // try to find this column index (=pressure DOF) in our pressure DOF mirror
1144 for(Index l(0); l < num_dof_mir_p; ++l)
1145 {
1146 if(col_idx[j] == pres_idx[l])
1147 {
1148 // that's our pressure DOF, so store its index as column index of B'
1149 img_idx[k] = l;
1150 break;
1151 }
1152 }
1153 ASSERT(img_idx[k] != ~IndexType(0));
1154 }
1155 // set next row pointer of B'
1156 dom_ptr[i+1] = dom_ptr[i] + (row_ptr[irow+1] - row_ptr[irow]);
1157 }
1158
1159 // sort indices of B' and return graph
1160 graph.sort_indices();
1161 return graph;
1162 }
1163
1182 static MirrorTypeP _asm_data_mirror(const MirrorTypeP& mirror_p, const MirrorTypeV& mirror_v,
1183 const LocalMatrixTypeB& matrix_b, const Adjacency::Graph& graph)
1184 {
1185 const Index num_dof_mir_v = mirror_v.num_indices();
1186 const Memory::TypedView<IndexType> velo_idx = mirror_v.indices_view_r();
1187 const Memory::TypedView<IndexType> pres_idx = mirror_p.indices_view_r();
1188 const Memory::TypedView<IndexType> row_ptr = matrix_b.row_ptr_view_r();
1189 const Memory::TypedView<IndexType> col_idx = matrix_b.col_idx_view_r();
1190 const Index* dom_ptr = graph.get_domain_ptr();
1191 const Index* img_idx = graph.get_image_idx();
1192
1193 // allocate mirror for data array of B'
1194 MirrorTypeP data_mirror(matrix_b.num_nzes(), graph.get_num_indices());
1195 Memory::TypedView<IndexType> dat_idx = data_mirror.indices_view_w();
1196
1197 // loop over all rows of B' again
1198 for(Index i(0); i < num_dof_mir_v; ++i)
1199 {
1200 // get the B-row index (=velocity DOF index)
1201 const IndexType irow = velo_idx[i];
1202
1203 // loop over all non-zeroes in the row of B / B'
1204 for(Index k(dom_ptr[i]); k < dom_ptr[i+1]; ++k)
1205 {
1206 // initialize invalid index for assertion below
1207 dat_idx[k] = ~IndexType(0);
1208
1209 // get the B-column index (=pressure DOF index)
1210 const Index jcol = pres_idx[img_idx[k]];
1211
1212 // try to find that column in our matrix
1213 for(IndexType j(row_ptr[irow]); j < row_ptr[irow + 1]; ++j)
1214 {
1215 if(jcol == col_idx[j])
1216 {
1217 // that's the entry we were looking for
1218 dat_idx[k] = j;
1219 break;
1220 }
1221 }
1222 ASSERT(dat_idx[k] != ~IndexType(0));
1223 }
1224 }
1225
1226 // that's it
1227 return data_mirror;
1228 }
1229
1242 static BufferVectorType _gather_b(const MirrorTypeP& data_mirror, const LocalMatrixTypeB& matrix_b)
1243 {
1244 const Index num_idx = data_mirror.num_indices();
1245 const Memory::TypedView<IndexType> idx = data_mirror.indices_view_r();
1246 const Memory::TypedView<ValueTypeB> mat_val = matrix_b.val_view_r();
1247
1248 BufferVectorType buf(Index(dim)*num_idx);
1249 Memory::TypedView<ValueTypeB> buf_val(std::forward<Memory::View&&>(buf.elements_view_w()));
1250
1251 for(Index i(0); i < num_idx; ++i)
1252 {
1253 buf_val[i] = mat_val[idx[i]];
1254 }
1255
1256 return buf;
1257 }
1258
1260 inline static ValueTypeD _mat_mult_d_a(const ValueTypeD& val_d, const ValueTypeA& val_a)
1261 {
1262 ValueTypeD da;
1263 for(int i(0); i < dim; ++i)
1264 da(0, i) = val_d(0, i) * val_a(i);
1265 return da;
1266 }
1267
1269 inline static DataType _mat_mult_da_b(const ValueTypeD& val_da, const ValueTypeB& val_b)
1270 {
1271 DataType s = DataType(0);
1272 for(int i(0); i < dim; ++i)
1273 s += val_da(0, i) * val_b(i, 0);
1274 return s;
1275 }
1276
1278 inline static void _mat_mult_d_a(ValueTypeB& val_da, const ValueTypeD& val_d, const ValueTypeA& val_a)
1279 {
1280 for(int i(0); i < dim; ++i)
1281 val_da(i, 0) = val_d(0, i) * val_a(i);
1282 }
1283
1285 inline static DataType _mat_mult_da_b(const ValueTypeB& val_da, const ValueTypeB& val_b)
1286 {
1287 DataType s = DataType(0);
1288 for(int i(0); i < dim; ++i)
1289 s += val_da(i, 0) * val_b(i, 0);
1290 return s;
1291 }
1292
1305 static void _premult_da(LocalMatrixTypeB& mat_da, const LocalMatrixTypeD& mat_d, const LocalVectorTypeV& mat_a)
1306 {
1307 const Index num_rows = mat_d.num_rows();
1308 const Index num_cols = mat_d.num_cols();
1309
1310 const Memory::TypedView<IndexType> row_ptr = mat_d.row_ptr_view_r();
1311 const Memory::TypedView<IndexType> col_idx = mat_d.col_idx_view_r();
1312 const Memory::TypedView<IndexType> row_ptr_da = mat_da.row_ptr_view_r();
1313
1314 Memory::TypedView<ValueTypeB> val_da = mat_da.val_view_w();
1315 const Memory::TypedView<ValueTypeD> val_d = mat_d.val_view_r();
1316 const Memory::TypedView<ValueTypeA>val_a = mat_a.elements_view_r();
1317
1318 // create a temporary copy of the row-pointer of D^T
1319 std::vector<Index> ptr(num_cols);
1320 for(Index i(0); i < num_cols; ++i)
1321 ptr[i] = row_ptr_da[i];
1322
1323 // transpose matrix
1324 for(Index i(0); i < num_rows; ++i)
1325 {
1326 for(Index j(row_ptr[i]); j < row_ptr[i + 1]; ++j)
1327 {
1328 const Index col = col_idx[j];
1329 _mat_mult_d_a(val_da[ptr[col]++], val_d[j], val_a[col]);
1330 }
1331 }
1332 }
1333
1344 const LocalVectorTypeV& a, const LocalMatrixTypeB& b)
1345 {
1346 // Note: this is a modified version of SparseMatrixCSR::add_double_mat_product for BCSR multiplicands
1347
1348 // validate matrix dimensions
1349 XASSERT(s.num_rows() == d.num_rows());
1350 XASSERT(d.num_cols() == a.size());
1351 XASSERT(a.size() == b.num_rows());
1352 XASSERT(b.num_cols() == s.num_cols());
1353
1354 // fetch matrix arrays:
1355 Memory::TypedView<DataType> data_s = s.val_view_rw();
1356 const Memory::TypedView<ValueTypeD> data_d = d.val_view_r();
1357 const Memory::TypedView<ValueTypeA> data_a = a.elements_view_r();
1358 const Memory::TypedView<ValueTypeB> data_b = b.val_view_r();
1359 const Memory::TypedView<IndexType> row_ptr_s = s.row_ptr_view_r();
1360 const Memory::TypedView<IndexType> col_idx_s = s.col_idx_view_r();
1361 const Memory::TypedView<IndexType> row_ptr_d = d.row_ptr_view_r();
1362 const Memory::TypedView<IndexType> col_idx_d = d.col_idx_view_r();
1363 const Memory::TypedView<IndexType> row_ptr_b = b.row_ptr_view_r();
1364 const Memory::TypedView<IndexType> col_idx_b = b.col_idx_view_r();
1365
1366 // loop over all rows of D and S, resp.
1367 for(IndexType i(0); i < IndexType(s.num_rows()); ++i)
1368 {
1369 // loop over all non-zeros D_ik in row i of D
1370 for(IndexType ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
1371 {
1372 // get column index k
1373 const IndexType k = col_idx_d[ik];
1374
1375 // pre-compute (D_ik * A_kk)
1376 const ValueTypeD val_da = _mat_mult_d_a(data_d[ik], data_a[k]);
1377
1378 // S_i. += (D_ik * A_kk) * B_k.
1379 for(IndexType ij(row_ptr_s[i]), kj(row_ptr_b[k]); kj < row_ptr_b[k+1]; ++ij)
1380 {
1381 ASSERT(ij < row_ptr_s[i+1]);
1382 ASSERT(col_idx_s[ij] <= col_idx_b[kj]);
1383 if(col_idx_s[ij] == col_idx_b[kj])
1384 {
1385 data_s[ij] += _mat_mult_da_b(val_da, data_b[kj]);
1386 ++kj;
1387 }
1388 }
1389 }
1390 }
1391 }
1392
1412 const MirrorTypeV& mirror_v, const Adjacency::Graph& graph_b, const BufferVectorType& buffer_b)
1413 {
1414 // validate matrix dimensions
1415 XASSERT(s.num_rows() == da.num_cols());
1416 XASSERT(da.num_rows() == mirror_v.size());
1417 XASSERT(graph_b.get_num_nodes_domain() == mirror_v.num_indices());
1418 XASSERT(graph_b.get_num_nodes_image() == s.num_cols());
1419 XASSERT(mirror_v.size() == da.num_rows());
1420
1421 // fetch matrix arrays:
1422 Memory::TypedView<DataType>data_s = s.val_view_rw();
1423 const Memory::TypedView<IndexType> row_ptr_s = s.row_ptr_view_r();
1424 const Memory::TypedView<IndexType> row_idx_s = s.row_idx_view_r();
1425 const Memory::TypedView<IndexType> col_idx_s = s.col_idx_view_r();
1426 const Index used_rows_s = s.num_nonzero_rows();
1427
1428 // we use CSR for (D*A)^T here, which is effectively a CSC storage of (D*A),
1429 // thus rows and columns swap their meaning here
1430 const Memory::TypedView<ValueTypeB> data_da = da.val_view_r();
1431 const Memory::TypedView<IndexType> col_ptr_da = da.row_ptr_view_r();
1432 const Memory::TypedView<IndexType> row_idx_da = da.col_idx_view_r();
1433
1434 const Index num_mir_idx = mirror_v.num_indices();
1435 const Memory::TypedView<IndexType> mir_idx = mirror_v.indices_view_r();
1436
1437 const Memory::TypedView<ValueTypeB> data_b(std::forward<Memory::View&&>(buffer_b.elements_view_r()));
1438 const Index* dom_ptr_b = graph_b.get_domain_ptr();
1439 const Index* img_idx_b = graph_b.get_image_idx();
1440
1441 // loop over all velocity mirror entries, which correspond to the rows of B and the columns of D
1442 for(Index l(0); l < num_mir_idx; ++l)
1443 {
1444 // get velocity DOF index k = column index of D = row index of B
1445 const Index k = mir_idx[l];
1446
1447 // loop over all columns k of D
1448 for(Index ik(col_ptr_da[k]), si(0); (ik < col_ptr_da[k + 1]) && (si < used_rows_s); )
1449 {
1450 // check if we have found a common row of S and the k-th column of D
1451 if(row_idx_da[ik] < row_idx_s[si])
1452 {
1453 // row i exists in column k of D, but not in matrix S
1454 ++ik;
1455 continue;
1456 }
1457 if(row_idx_s[si] < row_idx_da[ik])
1458 {
1459 // row i exists in matrix S, but not in column k of D
1460 ++si;
1461 continue;
1462 }
1463
1464 // S_i. += (D_ik * A_kk) * B_l.
1465 for(IndexType ij(row_ptr_s[si]), kj(dom_ptr_b[l]); kj < dom_ptr_b[l+1]; ++ij)
1466 {
1467 ASSERT(ij < row_ptr_s[si+1]);
1468 ASSERT(col_idx_s[ij] <= img_idx_b[kj]);
1469 if(col_idx_s[ij] == img_idx_b[kj])
1470 {
1471 data_s[ij] += _mat_mult_da_b(data_da[ik], data_b[kj]);
1472 ++kj;
1473 }
1474 }
1475
1476 // okay, continue with next row of S and D
1477 ++ik;
1478 ++si;
1479 }
1480 }
1481 }
1482
1501 void _apply(LocalVectorTypeP& r, const LocalVectorTypeP& x, const LocalVectorTypeP& y, const DataType alpha, bool only_Ax) const
1502 {
1503 // get the number of our neighbors
1504 const std::size_t num_neighs = this->_ranks.size();
1505
1506 // no neighbors?
1507 if(num_neighs <= std::size_t(0))
1508 {
1509 // multiply by local schur matrix and return
1510 watch_apply_matrix_loc.start();
1511 if(only_Ax)
1512 this->_matrix_s.apply(r, x);
1513 else
1514 this->_matrix_s.apply(r, x, y, alpha);
1515 watch_apply_matrix_loc.stop();
1516 return;
1517 }
1518
1519 // get our communicator
1520 const Dist::Comm& comm = *this->_matrix_b.get_comm();
1521
1522 // send/receive buffers and requests
1523 std::vector<BufferVectorType> recv_bufs(num_neighs), send_bufs(num_neighs);
1524 Dist::RequestVector recv_reqs(num_neighs), send_reqs(num_neighs);
1525 std::vector<Memory::TypedView<DataType>> recv_views(num_neighs), send_views(num_neighs);
1526
1527 // allocate receive buffer vectors and post receives
1528 for(std::size_t i(0); i < num_neighs; ++i)
1529 {
1530 recv_bufs.at(i) = BufferVectorType(this->_neighbor_matrices.at(i).num_cols());
1531 recv_views[i] = recv_bufs.at(i).elements_view_w();
1532 recv_reqs[i] = comm.irecv(recv_views[i].get_w(), recv_bufs.at(i).size(), this->_ranks.at(i));
1533 }
1534
1535 // extract pressure dofs and post sends
1536 for(std::size_t i(0); i < num_neighs; ++i)
1537 {
1538 send_bufs.at(i) = BufferVectorType(this->_pres_mirrors.at(i).num_indices());
1539 this->_pres_mirrors.at(i).gather(send_bufs.at(i), x);
1540 send_views[i] = send_bufs.at(i).elements_view_w();
1541 send_reqs[i] = comm.isend(send_views[i].get_r(), send_bufs.at(i).size(), this->_ranks.at(i));
1542 }
1543
1544 // multiply by local schur matrix
1545 watch_apply_matrix_loc.start();
1546 if(only_Ax)
1547 this->_matrix_s.apply(r, x);
1548 else
1549 this->_matrix_s.apply(r, x, y, alpha);
1550 watch_apply_matrix_loc.stop();
1551
1552 // process receives and multiply by neighbor schur matrices
1553 for(std::size_t i(0u); recv_reqs.wait_any(i); )
1554 {
1555 watch_apply_neighbor_s.start();
1556 recv_views[i].release();
1557 this->_neighbor_matrices.at(i).apply(r, recv_bufs.at(i), r, (only_Ax ? DataType(1) : alpha));
1558 watch_apply_neighbor_s.stop();
1559 }
1560
1561 // wait for all previous sends to finish
1562 send_reqs.wait_all();
1563 }
1564 }; // class PMDCDSCMatrix<...>
1565 } // namespace Global
1566} // namespace FEAT
#define ASSERT(expr)
Debug-Assertion macro definition.
Definition: assertion.hpp:229
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Adjacency Graph implementation.
Definition: graph.hpp:34
void sort_indices()
Sorts the image indices to non-descending order.
Definition: graph.cpp:206
Index * get_domain_ptr()
Returns the domain pointer array.
Definition: graph.hpp:359
Index * get_image_idx()
Returns the image node index array.
Definition: graph.hpp:374
Index get_num_indices() const
Returns the total number indices.
Definition: graph.hpp:390
Communicator class.
Definition: dist.hpp:1657
Request irecv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag=0) const
Nonblocking Receive.
Definition: dist.cpp:719
Request isend(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Nonblocking Send.
Definition: dist.cpp:707
int rank() const
Returns the rank of this process in this communicator.
Definition: dist.hpp:1802
Communication Request vector class.
Definition: dist.hpp:948
void wait_all()
Blocks until all active requests are fulfilled.
Definition: dist.cpp:327
Global gate implementation.
Definition: gate.hpp:78
const Dist::Comm * get_comm() const
Returns a const pointer to the underlying communicator.
Definition: gate.hpp:185
std::vector< Mirror_ > _mirrors
vector mirrors
Definition: gate.hpp:100
std::vector< int > _ranks
communication ranks
Definition: gate.hpp:98
Global Matrix wrapper class template.
Definition: matrix.hpp:40
VectorTypeR create_vector_r() const
Creates and returns a new R-compatible global vector object.
Definition: matrix.hpp:217
VectorTypeL create_vector_l() const
Creates and returns a new L-compatible global vector object.
Definition: matrix.hpp:207
const GateRowType * get_row_gate() const
Returns a const pointer to the internal row gate of the matrix.
Definition: matrix.hpp:164
const Dist::Comm * get_comm() const
Returns a const pointer to the internal communicator of the gates of the matrix.
Definition: matrix.hpp:184
LocalMatrix_ & local()
Returns a reference to the internal local LAFEM matrix object.
Definition: matrix.hpp:136
void adp_upload_symbolic(RPT_ *row_ptr, CIT_ *col_idx, Index global_dof_offset) const
Assembles the matrix structure of an algebraic DOF partitioned matrix.
static ValueTypeD _mat_mult_d_a(const ValueTypeD &val_d, const ValueTypeA &val_a)
auxiliary function for _asm_local_schur_matrix: multiply two values D*A
static MirrorTypeP _asm_data_mirror(const MirrorTypeP &mirror_p, const MirrorTypeV &mirror_v, const LocalMatrixTypeB &matrix_b, const Adjacency::Graph &graph)
Auxiliary Function: computes a data mirror from B to B'.
static Adjacency::Graph _asm_reduced_b(const MirrorTypeP &mirror_p, const MirrorTypeV &mirror_v, const LocalMatrixTypeB &matrix_b)
Auxiliary Function: reduces the matrix B to B'.
static void _premult_da(LocalMatrixTypeB &mat_da, const LocalMatrixTypeD &mat_d, const LocalVectorTypeV &mat_a)
Auxiliary Function: Computes the product (D*A) and stores the result in a CSC matrix.
static void _asm_pres_mirror(MirrorTypeP &mirror_p, const MirrorTypeV &mirror_v, const LocalMatrixTypeB &matrix_b)
Auxiliary function: assembles a pressure mirror from a velocity mirror and the B-matrix.
static void _asm_local_schur_matrix(LocalMatrixTypeS &s, const LocalMatrixTypeD &d, const LocalVectorTypeV &a, const LocalMatrixTypeB &b)
Assembles the local Schur-Matrix S = (D*A*B)
static DataType _mat_mult_da_b(const ValueTypeB &val_da, const ValueTypeB &val_b)
auxiliary function for _asm_neighbor_schur_matrix: multiply two values DA*B with DA in transposed for...
static DataType _mat_mult_da_b(const ValueTypeD &val_da, const ValueTypeB &val_b)
auxiliary function for _asm_local_schur_matrix: multiply two values DA*B
static void _mat_mult_d_a(ValueTypeB &val_da, const ValueTypeD &val_d, const ValueTypeA &val_a)
auxiliary function for _asm_neighbor_schur_matrix: multiply two values D*A and store in transposed fo...
static void _asm_neighbor_schur_matrix(NeighMatrixTypeS &s, const LocalMatrixTypeB &da, const MirrorTypeV &mirror_v, const Adjacency::Graph &graph_b, const BufferVectorType &buffer_b)
Assembles a neighbor Schur-Matrix S_k = (D*A*M_k^T*B_k)
void adp_compute_counts(Index &global_dof_offset, Index &global_dof_count, Index &owned_dof_count, Index &owned_num_nzes, Index &global_num_nzes) const
Compute the counts required for an algebraic dof partitioning of this matrix.
void _apply(LocalVectorTypeP &r, const LocalVectorTypeP &x, const LocalVectorTypeP &y, const DataType alpha, bool only_Ax) const
Applies this matrix onto a vector.
static BufferVectorType _gather_b(const MirrorTypeP &data_mirror, const LocalMatrixTypeB &matrix_b)
Auxiliary Function: Gathers the data array from B to B' using the data mirror.
Pre-Multiplied Discontinuous Diagonal Schur-Complement Matrix.
Global vector wrapper class template.
Definition: vector.hpp:68
const Dist::Comm * get_comm() const
Returns a const pointer to the internal communicator of the gate of the vector.
Definition: vector.hpp:159
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Definition: vector.hpp:122
void format()
Reset all elements of the container to a given value or zero if missing.
Definition: container.hpp:831
Blocked Dense data vector class template.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Dense data vector class template.
CSR based blocked sparse matrix.
Index num_rows() const
Retrieve matrix row count.
Index num_nzes() const
Retrieve non zero element count.
Index num_cols() const
Retrieve matrix column count.
CSCR based sparse matrix.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Index num_nonzero_rows() const
Retrieve used matrix non zero row count.
CSR based sparse matrix.
void extract_diag(VectorTypeL &diag) const
extract main diagonal vector from matrix
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Index num_nzes() const
Retrieve non zero element count.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
Typed memory view class.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
Stop-Watch class.
Definition: stop_watch.hpp:21
double elapsed() const
Returns the total elapsed time in seconds.
Definition: stop_watch.hpp:70
void start()
Starts the stop-watch.
Definition: stop_watch.hpp:43
void reset()
Resets the elapsed time.
Definition: stop_watch.hpp:36
void stop()
Stops the stop-watch and increments elapsed time.
Definition: stop_watch.hpp:51
String class implementation.
Definition: string.hpp:47
String pad_back(size_type len, char c=' ') const
Pads the back of the string up to a desired length.
Definition: string.hpp:416
Tiny Matrix class template.
Tiny Vector class template.
@ injectify_sorted
Render-Injectified mode, sort image indices.
@ injectify_transpose
Render-Injectified-Transpose mode.
const Operation op_max(MPI_MAX)
Operation wrapper for MPI_MAX.
Definition: dist.hpp:573
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
Definition: dist.hpp:571
T_ abs(T_ x)
Returns the absolute value.
Definition: math.hpp:275
FEAT namespace.
Definition: adjactor.hpp:12
String stringify_fp_fix(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in fixed-point notation.
Definition: string.hpp:1191
std::uint64_t Index
Index data type.