// @HEADER // ***************************************************************************** // Xpetra: A linear algebra interface package // // Copyright 2012 NTESS and the Xpetra contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef XPETRA_REORDEREDBLOCKEDCRSMATRIX_HPP #define XPETRA_REORDEREDBLOCKEDCRSMATRIX_HPP #include #include "Xpetra_ConfigDefs.hpp" #include "Xpetra_Exceptions.hpp" #include "Xpetra_MapUtils.hpp" #include "Xpetra_BlockedMultiVector.hpp" #include "Xpetra_ReorderedBlockedMultiVector.hpp" #include "Xpetra_CrsMatrixWrap.hpp" #include "Xpetra_BlockedCrsMatrix.hpp" /** \file Xpetra_ReorderedBlockedCrsMatrix.hpp Declarations for the class Xpetra::ReorderedBlockedCrsMatrix. */ namespace Xpetra { typedef std::string viewLabel_t; template class ReorderedBlockedCrsMatrix : public BlockedCrsMatrix { public: typedef Scalar scalar_type; typedef LocalOrdinal local_ordinal_type; typedef GlobalOrdinal global_ordinal_type; typedef Node node_type; private: #undef XPETRA_REORDEREDBLOCKEDCRSMATRIX_SHORT #include "Xpetra_UseShortNames.hpp" public: //! @name Constructor/Destructor Methods //@{ //! Constructor /*! * \param rangeMaps range maps for all blocks * \param domainMaps domain maps for all blocks * \param npr extimated number of entries per row in each block(!) * \param brm of type BlockReorderManager * \param bmat original full blocked operator (we keep the RCP to make sure all subblocks are available) */ ReorderedBlockedCrsMatrix(Teuchos::RCP& rangeMaps, Teuchos::RCP& domainMaps, size_t npr, Teuchos::RCP brm, Teuchos::RCP> bmat) : Xpetra::BlockedCrsMatrix(rangeMaps, domainMaps, npr) { brm_ = brm; fullOp_ = bmat; } // protected: //! Destructor virtual ~ReorderedBlockedCrsMatrix() {} //@} private: Teuchos::RCP> mergeSubBlockMaps(Teuchos::RCP brm) { RCP fullRangeMapExtractor = fullOp_->getRangeMapExtractor(); // number of sub blocks size_t numBlocks = brm->GetNumBlocks(); Teuchos::RCP map = Teuchos::null; if (numBlocks == 0) { // it is a leaf node Teuchos::RCP leaf = Teuchos::rcp_dynamic_cast(brm); // never extract Thyra style maps (since we have to merge them) map = fullRangeMapExtractor->getMap(Teuchos::as(leaf->GetIndex()), false); } else { // initialize vector for sub maps std::vector> subMaps(numBlocks, Teuchos::null); for (size_t i = 0; i < numBlocks; i++) { Teuchos::RCP blkMgr = brm->GetBlock(Teuchos::as(i)); subMaps[i] = mergeSubBlockMaps(blkMgr); TEUCHOS_ASSERT(subMaps[i].is_null() == false); } map = MapUtils::concatenateMaps(subMaps); } TEUCHOS_ASSERT(map.is_null() == false); return map; } public: //! @name Methods implementing Matrix //@{ //! sparse matrix-multivector multiplication for the region layout matrices (currently no blocked implementation) virtual void apply(const MultiVector& X, MultiVector& Y, Teuchos::ETransp mode, Scalar alpha, Scalar beta, bool sumInterfaceValues, const RCP>& regionInterfaceImporter, const Teuchos::ArrayRCP& regionInterfaceLIDs) const {} //! \brief Computes the sparse matrix-multivector multiplication. /*! Performs \f$Y = \alpha A^{\textrm{mode}} X + \beta Y\f$, with one special exceptions: - if beta == 0, apply() overwrites \c Y, so that any values in \c Y (including NaNs) are ignored. */ virtual void apply(const MultiVector& X, MultiVector& Y, Teuchos::ETransp mode = Teuchos::NO_TRANS, Scalar alpha = ScalarTraits::one(), Scalar beta = ScalarTraits::zero()) const { // Nested sub-operators should just use the provided X and B vectors if (fullOp_->getLocalNumRows() != this->getLocalNumRows()) { Xpetra::BlockedCrsMatrix::apply(X, Y, mode, alpha, beta); return; } // Special handling for the top level of the nested operator // check whether input parameters are blocked or not RCP refX = rcpFromRef(X); RCP refbX = Teuchos::rcp_dynamic_cast(refX); RCP tmpY = rcpFromRef(Y); RCP tmpbY = Teuchos::rcp_dynamic_cast(tmpY); bool bCopyResultX = false; bool bCopyResultY = false; // TODO create a nested ReorderedBlockedMultiVector out of a nested BlockedMultiVector! // probably not necessary, is it? // check whether X and B are blocked but not ReorderedBlocked operators /*if (refbX != Teuchos::null && fullOp_->getLocalNumRows() == this->getLocalNumRows()) { RCP rbCheck = Teuchos::rcp_dynamic_cast(refbX); if(rbCheck == Teuchos::null) { RCP bX = Teuchos::rcp_dynamic_cast(Xpetra::buildReorderedBlockedMultiVector(brm_, refbX)); TEUCHOS_ASSERT(bX.is_null()==false); refbX.swap(bX); } } if (tmpbY != Teuchos::null && fullOp_->getLocalNumRows() == this->getLocalNumRows()) { RCP rbCheck = Teuchos::rcp_dynamic_cast(tmpbY); if(rbCheck == Teuchos::null) { RCP bY = Teuchos::rcp_dynamic_cast(Xpetra::buildReorderedBlockedMultiVector(brm_, tmpbY)); TEUCHOS_ASSERT(bY.is_null()==false); tmpbY.swap(bY); } }*/ // if X and B are not blocked, create a blocked version here and use the blocked vectors // for the internal (nested) apply call. // Check whether "this" operator is the reordered variant of the underlying fullOp_. // Note, that nested ReorderedBlockedCrsMatrices always have the same full operator "fullOp_" // stored underneath for being able to "translate" the block ids. if (refbX == Teuchos::null && fullOp_->getLocalNumRows() == this->getLocalNumRows()) { // create a new (non-nested) blocked multi vector (using the blocked range map of fullOp_) RCP blkRgMap = Teuchos::rcp_dynamic_cast(fullOp_->getRangeMap()); TEUCHOS_ASSERT(blkRgMap.is_null() == false); RCP bXtemp = Teuchos::rcp(new BlockedMultiVector(blkRgMap, refX)); TEUCHOS_ASSERT(bXtemp.is_null() == false); RCP bX = Teuchos::rcp_dynamic_cast(Xpetra::buildReorderedBlockedMultiVector(brm_, bXtemp)); TEUCHOS_ASSERT(bX.is_null() == false); refbX.swap(bX); bCopyResultX = true; } if (tmpbY == Teuchos::null && fullOp_->getLocalNumRows() == this->getLocalNumRows()) { // create a new (non-nested) blocked multi vector (using the blocked range map of fullOp_) RCP blkRgMap = Teuchos::rcp_dynamic_cast(fullOp_->getRangeMap()); TEUCHOS_ASSERT(blkRgMap.is_null() == false); RCP tmpbYtemp = Teuchos::rcp(new BlockedMultiVector(blkRgMap, tmpY)); TEUCHOS_ASSERT(tmpbYtemp.is_null() == false); RCP bY = Teuchos::rcp_dynamic_cast(Xpetra::buildReorderedBlockedMultiVector(brm_, tmpbYtemp)); TEUCHOS_ASSERT(bY.is_null() == false); tmpbY.swap(bY); bCopyResultY = true; } TEUCHOS_ASSERT(refbX.is_null() == false); TEUCHOS_ASSERT(tmpbY.is_null() == false); Xpetra::BlockedCrsMatrix::apply(*refbX, *tmpbY, mode, alpha, beta); if (bCopyResultX == true) { RCP Xmerged = refbX->Merge(); RCP nonconstX = Teuchos::rcp_const_cast(refX); nonconstX->update(Teuchos::ScalarTraits::one(), *Xmerged, Teuchos::ScalarTraits::zero()); } if (bCopyResultY == true) { RCP Ymerged = tmpbY->Merge(); Y.update(Teuchos::ScalarTraits::one(), *Ymerged, Teuchos::ScalarTraits::zero()); } } // @} //! @name Access functions //@{ /** \brief Returns internal BlockReorderManager object */ Teuchos::RCP getBlockReorderManager() { return brm_; } /** \brief Returns internal unmodified BlockedCrsMatrix object */ Teuchos::RCP> getBlockedCrsMatrix() { return fullOp_; } // @} //! @name Overridden from Teuchos::Describable //@{ /** \brief Return a simple one-line description of this object. */ std::string description() const { return "ReorderedBlockedCrsMatrix"; } /** \brief Print the object with some verbosity level to an FancyOStream object. */ void describe(Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default) const { out << "Xpetra::ReorderedBlockedCrsMatrix: " << BlockedCrsMatrix::Rows() << " x " << BlockedCrsMatrix::Cols() << std::endl; if (BlockedCrsMatrix::isFillComplete()) { out << "ReorderedBlockMatrix is fillComplete" << std::endl; out << "fullRowMap" << std::endl; BlockedCrsMatrix::getRangeMap(0, false)->describe(out, verbLevel); // out << "fullColMap" << std::endl; // fullcolmap_->describe(out,verbLevel); } else { out << "Xpetra::ReorderedBlockedCrsMatrix is NOT fillComplete" << std::endl; } for (size_t r = 0; r < BlockedCrsMatrix::Rows(); ++r) for (size_t c = 0; c < BlockedCrsMatrix::Cols(); ++c) { out << "Block(" << r << "," << c << ")" << std::endl; BlockedCrsMatrix::getMatrix(r, c)->describe(out, verbLevel); } } //@} private: Teuchos::RCP brm_; Teuchos::RCP> fullOp_; }; template Teuchos::RCP> mergeSubBlockMaps(Teuchos::RCP brm, Teuchos::RCP> bmat, bool bThyraMode) { typedef Xpetra::MapUtils MapUtils; // TODO distinguish between range and domain map extractor! provide MapExtractor as parameter! RCP> fullRangeMapExtractor = bmat->getRangeMapExtractor(); // number of sub blocks size_t numBlocks = brm->GetNumBlocks(); Teuchos::RCP> map = Teuchos::null; if (numBlocks == 0) { // it is a leaf node Teuchos::RCP leaf = Teuchos::rcp_dynamic_cast(brm); map = fullRangeMapExtractor->getMap(Teuchos::as(leaf->GetIndex()), bThyraMode); } else { // initialize vector for sub maps std::vector>> subMaps(numBlocks, Teuchos::null); for (size_t i = 0; i < numBlocks; i++) { Teuchos::RCP blkMgr = brm->GetBlock(Teuchos::as(i)); subMaps[i] = mergeSubBlockMaps(blkMgr, bmat, bThyraMode); TEUCHOS_ASSERT(subMaps[i].is_null() == false); } #if 1 // concatenate submaps // for Thyra mode this map isn't important Teuchos::RCP> fullMap = MapUtils::concatenateMaps(subMaps); // create new BlockedMap (either in Thyra Mode or Xpetra mode) map = Teuchos::rcp(new Xpetra::BlockedMap(fullMap, subMaps, bThyraMode)); #else // TAW: 11/27/16 we just concatenate the submaps to one monolithic Map object. // Alternatively, we could create a new BlockedMap using the concatenated map and the submaps // However, the block smoothers only need the concatenated map for creating MultiVectors... // But for the Thyra mode version concatenating would not be ok for the whole map map = MapUtils::concatenateMaps(subMaps); #endif } TEUCHOS_ASSERT(map.is_null() == false); return map; } template Teuchos::RCP> mergeSubBlocks(Teuchos::RCP rowMgr, Teuchos::RCP colMgr, Teuchos::RCP> bmat) { typedef Xpetra::Map Map; typedef Xpetra::MapUtils MapUtils; typedef Xpetra::MapExtractor MapExtractor; typedef Xpetra::Matrix Matrix; typedef Xpetra::BlockedCrsMatrix BlockedCrsMatrix; typedef Xpetra::ReorderedBlockedCrsMatrix ReorderedBlockedCrsMatrix; // number of sub blocks size_t rowSz = rowMgr->GetNumBlocks(); size_t colSz = colMgr->GetNumBlocks(); Teuchos::RCP rbmat = Teuchos::null; if (rowSz == 0 && colSz == 0) { // it is a leaf node Teuchos::RCP rowleaf = Teuchos::rcp_dynamic_cast(rowMgr); Teuchos::RCP colleaf = Teuchos::rcp_dynamic_cast(colMgr); // extract leaf node Teuchos::RCP mat = bmat->getMatrix(rowleaf->GetIndex(), colleaf->GetIndex()); if (mat == Teuchos::null) return Teuchos::null; // check, whether leaf node is of type Xpetra::CrsMatrixWrap Teuchos::RCP> matwrap = Teuchos::rcp_dynamic_cast>(mat); if (matwrap != Teuchos::null) { // If the leaf node is of type Xpetra::CrsMatrixWrap wrap it into a 1x1 ReorderedBlockMatrix // with the corresponding MapExtractors for translating Thyra to Xpetra GIDs if necessary RCP fullRangeMapExtractor = bmat->getRangeMapExtractor(); Teuchos::RCP submap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(), false); std::vector> rowSubMaps(1, submap); Teuchos::RCP rgMapExtractor = Teuchos::rcp(new MapExtractor(submap, rowSubMaps, false)); RCP fullDomainMapExtractor = bmat->getDomainMapExtractor(); Teuchos::RCP submap2 = fullDomainMapExtractor->getMap(colleaf->GetIndex(), false); std::vector> colSubMaps(1, submap2); Teuchos::RCP doMapExtractor = Teuchos::rcp(new MapExtractor(submap2, colSubMaps, false)); rbmat = Teuchos::rcp(new ReorderedBlockedCrsMatrix(rgMapExtractor, doMapExtractor, 33, rowMgr, bmat)); rbmat->setMatrix(0, 0, mat); } else { // If leaf node is already wrapped into a blocked matrix do not wrap it again. rbmat = Teuchos::rcp_dynamic_cast(mat); TEUCHOS_ASSERT(rbmat != Teuchos::null); } TEUCHOS_ASSERT(mat->getLocalNumEntries() == rbmat->getLocalNumEntries()); } else { // create the map extractors // we cannot create block matrix in thyra mode since merged maps might not start with 0 GID Teuchos::RCP rgMapExtractor = Teuchos::null; if (rowSz > 0) { std::vector> rowSubMaps(rowSz, Teuchos::null); for (size_t i = 0; i < rowSz; i++) { Teuchos::RCP rowSubMgr = rowMgr->GetBlock(Teuchos::as(i)); rowSubMaps[i] = mergeSubBlockMaps(rowSubMgr, bmat, false /*xpetra*/); TEUCHOS_ASSERT(rowSubMaps[i].is_null() == false); } Teuchos::RCP rgMergedSubMaps = MapUtils::concatenateMaps(rowSubMaps); rgMapExtractor = Teuchos::rcp(new MapExtractor(rgMergedSubMaps, rowSubMaps, false)); } else { Teuchos::RCP rowleaf = Teuchos::rcp_dynamic_cast(rowMgr); RCP fullRangeMapExtractor = bmat->getRangeMapExtractor(); // TODO think about Thyra style maps: we cannot use thyra style maps when recombining several blocks!!! // The GIDs might not start with 0 and may not be consecutive! Teuchos::RCP submap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(), false); std::vector> rowSubMaps(1, submap); rgMapExtractor = Teuchos::rcp(new MapExtractor(submap, rowSubMaps, false)); } Teuchos::RCP doMapExtractor = Teuchos::null; if (colSz > 0) { std::vector> colSubMaps(colSz, Teuchos::null); for (size_t j = 0; j < colSz; j++) { Teuchos::RCP colSubMgr = colMgr->GetBlock(Teuchos::as(j)); colSubMaps[j] = mergeSubBlockMaps(colSubMgr, bmat, false /*xpetra*/); TEUCHOS_ASSERT(colSubMaps[j].is_null() == false); } Teuchos::RCP doMergedSubMaps = MapUtils::concatenateMaps(colSubMaps); doMapExtractor = Teuchos::rcp(new MapExtractor(doMergedSubMaps, colSubMaps, false)); } else { Teuchos::RCP colleaf = Teuchos::rcp_dynamic_cast(colMgr); RCP fullDomainMapExtractor = bmat->getDomainMapExtractor(); // TODO think about Thyra style maps: we cannot use thyra style maps when recombining several blocks!!! // The GIDs might not start with 0 and may not be consecutive! Teuchos::RCP submap = fullDomainMapExtractor->getMap(colleaf->GetIndex(), false); std::vector> colSubMaps(1, submap); doMapExtractor = Teuchos::rcp(new MapExtractor(submap, colSubMaps, false)); } rbmat = Teuchos::rcp(new ReorderedBlockedCrsMatrix(rgMapExtractor, doMapExtractor, 33, rowMgr, bmat)); size_t cntNNZ = 0; if (rowSz == 0 && colSz > 0) { for (size_t j = 0; j < colSz; j++) { Teuchos::RCP colSubMgr = colMgr->GetBlock(Teuchos::as(j)); Teuchos::RCP submat = mergeSubBlocks(rowMgr, colSubMgr, bmat); rbmat->setMatrix(0, j, Teuchos::rcp_const_cast(submat)); if (submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries(); } } else if (rowSz > 0 && colSz == 0) { for (size_t i = 0; i < rowSz; i++) { Teuchos::RCP rowSubMgr = rowMgr->GetBlock(Teuchos::as(i)); Teuchos::RCP submat = mergeSubBlocks(rowSubMgr, colMgr, bmat); rbmat->setMatrix(i, 0, Teuchos::rcp_const_cast(submat)); if (submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries(); } } else { for (size_t i = 0; i < rowSz; i++) { Teuchos::RCP rowSubMgr = rowMgr->GetBlock(Teuchos::as(i)); for (size_t j = 0; j < colSz; j++) { Teuchos::RCP colSubMgr = colMgr->GetBlock(Teuchos::as(j)); Teuchos::RCP submat = mergeSubBlocks(rowSubMgr, colSubMgr, bmat); rbmat->setMatrix(i, j, Teuchos::rcp_const_cast(submat)); if (submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries(); } } } TEUCHOS_ASSERT(rbmat->getLocalNumEntries() == cntNNZ); } rbmat->fillComplete(); return rbmat; } // MapExtractor(const std::vector >& maps, const std::vector >& thyramaps); template Teuchos::RCP> mergeSubBlocksThyra(Teuchos::RCP rowMgr, Teuchos::RCP colMgr, Teuchos::RCP> bmat) { typedef Xpetra::Map Map; typedef Xpetra::MapExtractor MapExtractor; typedef Xpetra::Matrix Matrix; typedef Xpetra::BlockedCrsMatrix BlockedCrsMatrix; typedef Xpetra::ReorderedBlockedCrsMatrix ReorderedBlockedCrsMatrix; TEUCHOS_ASSERT(bmat->getRangeMapExtractor()->getThyraMode() == true); TEUCHOS_ASSERT(bmat->getDomainMapExtractor()->getThyraMode() == true); // number of sub blocks size_t rowSz = rowMgr->GetNumBlocks(); size_t colSz = colMgr->GetNumBlocks(); Teuchos::RCP rbmat = Teuchos::null; if (rowSz == 0 && colSz == 0) { // it is a leaf node Teuchos::RCP rowleaf = Teuchos::rcp_dynamic_cast(rowMgr); Teuchos::RCP colleaf = Teuchos::rcp_dynamic_cast(colMgr); // this matrix uses Thyra style GIDs as global row, range, domain and column indices Teuchos::RCP mat = bmat->getMatrix(rowleaf->GetIndex(), colleaf->GetIndex()); if (mat == Teuchos::null) return Teuchos::null; // std::cout << "Block " << rowleaf->GetIndex() << "," << colleaf->GetIndex() << " is zero" << std::endl; // check, whether leaf node is of type Xpetra::CrsMatrixWrap Teuchos::RCP> matwrap = Teuchos::rcp_dynamic_cast>(mat); if (matwrap != Teuchos::null) { /////////////////////////////////////////////////////////////////////////// // build map extractors RCP fullRangeMapExtractor = bmat->getRangeMapExtractor(); // extract Xpetra and Thyra based GIDs Teuchos::RCP xpsubmap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(), false); Teuchos::RCP thysubmap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(), true); std::vector> rowXpSubMaps(1, xpsubmap); std::vector> rowTySubMaps(1, thysubmap); // use expert constructor Teuchos::RCP rgMapExtractor = Teuchos::rcp(new MapExtractor(rowXpSubMaps, rowTySubMaps)); RCP fullDomainMapExtractor = bmat->getDomainMapExtractor(); // extract Xpetra and Thyra based GIDs Teuchos::RCP xpsubmap2 = fullDomainMapExtractor->getMap(colleaf->GetIndex(), false); Teuchos::RCP tysubmap2 = fullDomainMapExtractor->getMap(colleaf->GetIndex(), true); std::vector> colXpSubMaps(1, xpsubmap2); std::vector> colTySubMaps(1, tysubmap2); // use expert constructor Teuchos::RCP doMapExtractor = Teuchos::rcp(new MapExtractor(colXpSubMaps, colTySubMaps)); /////////////////////////////////////////////////////////////////////////// // build reordered block operator rbmat = Teuchos::rcp(new ReorderedBlockedCrsMatrix(rgMapExtractor, doMapExtractor, 33, rowMgr, bmat)); rbmat->setMatrix(0, 0, mat); } else { // If leaf node is already wrapped into a blocked matrix do not wrap it again. rbmat = Teuchos::rcp_dynamic_cast(mat); TEUCHOS_ASSERT(rbmat != Teuchos::null); } TEUCHOS_ASSERT(mat->getLocalNumEntries() == rbmat->getLocalNumEntries()); } else { // create the map extractors // we cannot create block matrix in thyra mode since merged maps might not start with 0 GID Teuchos::RCP rgMapExtractor = Teuchos::null; if (rowSz > 0) { std::vector> rowXpSubMaps(rowSz, Teuchos::null); std::vector> rowTySubMaps(rowSz, Teuchos::null); for (size_t i = 0; i < rowSz; i++) { Teuchos::RCP rowSubMgr = rowMgr->GetBlock(Teuchos::as(i)); // extract Xpetra and Thyra based merged GIDs rowXpSubMaps[i] = mergeSubBlockMaps(rowSubMgr, bmat, false); rowTySubMaps[i] = mergeSubBlockMaps(rowSubMgr, bmat, true); TEUCHOS_ASSERT(rowXpSubMaps[i].is_null() == false); TEUCHOS_ASSERT(rowTySubMaps[i].is_null() == false); } // use expert constructor rgMapExtractor = Teuchos::rcp(new MapExtractor(rowXpSubMaps, rowTySubMaps)); } else { Teuchos::RCP rowleaf = Teuchos::rcp_dynamic_cast(rowMgr); RCP fullRangeMapExtractor = bmat->getRangeMapExtractor(); // extract Xpetra and Thyra based GIDs Teuchos::RCP xpsubmap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(), false); Teuchos::RCP thysubmap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(), true); std::vector> rowXpSubMaps(1, xpsubmap); std::vector> rowTySubMaps(1, thysubmap); // use expert constructor rgMapExtractor = Teuchos::rcp(new MapExtractor(rowXpSubMaps, rowTySubMaps)); } Teuchos::RCP doMapExtractor = Teuchos::null; if (colSz > 0) { std::vector> colXpSubMaps(colSz, Teuchos::null); std::vector> colTySubMaps(colSz, Teuchos::null); for (size_t j = 0; j < colSz; j++) { Teuchos::RCP colSubMgr = colMgr->GetBlock(Teuchos::as(j)); // extract Xpetra and Thyra based merged GIDs colXpSubMaps[j] = mergeSubBlockMaps(colSubMgr, bmat, false); colTySubMaps[j] = mergeSubBlockMaps(colSubMgr, bmat, true); TEUCHOS_ASSERT(colXpSubMaps[j].is_null() == false); TEUCHOS_ASSERT(colTySubMaps[j].is_null() == false); } // use expert constructor doMapExtractor = Teuchos::rcp(new MapExtractor(colXpSubMaps, colTySubMaps)); } else { Teuchos::RCP colleaf = Teuchos::rcp_dynamic_cast(colMgr); RCP fullDomainMapExtractor = bmat->getDomainMapExtractor(); // extract Xpetra and Thyra based GIDs Teuchos::RCP xpsubmap = fullDomainMapExtractor->getMap(colleaf->GetIndex(), false); Teuchos::RCP tysubmap = fullDomainMapExtractor->getMap(colleaf->GetIndex(), true); std::vector> colXpSubMaps(1, xpsubmap); std::vector> colTySubMaps(1, tysubmap); // use expert constructor doMapExtractor = Teuchos::rcp(new MapExtractor(colXpSubMaps, colTySubMaps)); } // TODO matrix should have both rowMgr and colMgr?? rbmat = Teuchos::rcp(new ReorderedBlockedCrsMatrix(rgMapExtractor, doMapExtractor, 33, rowMgr, bmat)); size_t cntNNZ = 0; if (rowSz == 0 && colSz > 0) { for (size_t j = 0; j < colSz; j++) { Teuchos::RCP colSubMgr = colMgr->GetBlock(Teuchos::as(j)); Teuchos::RCP submat = mergeSubBlocksThyra(rowMgr, colSubMgr, bmat); rbmat->setMatrix(0, j, Teuchos::rcp_const_cast(submat)); if (submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries(); } } else if (rowSz > 0 && colSz == 0) { for (size_t i = 0; i < rowSz; i++) { Teuchos::RCP rowSubMgr = rowMgr->GetBlock(Teuchos::as(i)); Teuchos::RCP submat = mergeSubBlocksThyra(rowSubMgr, colMgr, bmat); rbmat->setMatrix(i, 0, Teuchos::rcp_const_cast(submat)); if (submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries(); } } else { for (size_t i = 0; i < rowSz; i++) { Teuchos::RCP rowSubMgr = rowMgr->GetBlock(Teuchos::as(i)); for (size_t j = 0; j < colSz; j++) { Teuchos::RCP colSubMgr = colMgr->GetBlock(Teuchos::as(j)); Teuchos::RCP submat = mergeSubBlocksThyra(rowSubMgr, colSubMgr, bmat); rbmat->setMatrix(i, j, Teuchos::rcp_const_cast(submat)); if (submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries(); } } } TEUCHOS_ASSERT(rbmat->getLocalNumEntries() == cntNNZ); } rbmat->fillComplete(); return rbmat; } template Teuchos::RCP> buildReorderedBlockedCrsMatrix(Teuchos::RCP brm, Teuchos::RCP> bmat) { TEUCHOS_ASSERT(bmat->getRangeMapExtractor()->getThyraMode() == bmat->getDomainMapExtractor()->getThyraMode()); Teuchos::RCP> rbmat = Teuchos::null; if (bmat->getRangeMapExtractor()->getThyraMode() == false) { rbmat = mergeSubBlocks(brm, brm, bmat); } else { rbmat = mergeSubBlocksThyra(brm, brm, bmat); } // TAW, 6/7/2016: rbmat might be Teuchos::null for empty blocks! return rbmat; } } // namespace Xpetra #define XPETRA_REORDEREDBLOCKEDCRSMATRIX_SHORT #endif /* XPETRA_REORDEREDBLOCKEDCRSMATRIX_HPP */