Xpetra Version of the Day
Loading...
Searching...
No Matches
Xpetra_ReorderedBlockedCrsMatrix.hpp
Go to the documentation of this file.
1// @HEADER
2//
3// ***********************************************************************
4//
5// Xpetra: A linear algebra interface package
6// Copyright 2012 Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact
39// Jonathan Hu (jhu@sandia.gov)
40// Andrey Prokopenko (aprokop@sandia.gov)
41// Ray Tuminaro (rstumin@sandia.gov)
42//
43// ***********************************************************************
44//
45// @HEADER
46#ifndef XPETRA_REORDEREDBLOCKEDCRSMATRIX_HPP
47#define XPETRA_REORDEREDBLOCKEDCRSMATRIX_HPP
48
49#include <Tpetra_KokkosCompat_DefaultNode.hpp>
50
51#include "Xpetra_ConfigDefs.hpp"
52#include "Xpetra_Exceptions.hpp"
53
54#include "Xpetra_MapUtils.hpp"
55
56#include "Xpetra_BlockedMultiVector.hpp"
58#include "Xpetra_CrsMatrixWrap.hpp"
60
61
66namespace Xpetra {
67
68 typedef std::string viewLabel_t;
69
70 template <class Scalar,
71 class LocalOrdinal,
72 class GlobalOrdinal,
73 class Node = Tpetra::KokkosClassic::DefaultNode::DefaultNodeType>
75 public BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
76 public:
77 typedef Scalar scalar_type;
78 typedef LocalOrdinal local_ordinal_type;
79 typedef GlobalOrdinal global_ordinal_type;
80 typedef Node node_type;
81
82 private:
83#undef XPETRA_REORDEREDBLOCKEDCRSMATRIX_SHORT
85
86 public:
87
89
90
92
95 * \param npr extimated number of entries per row in each block(!)
96 * \param brm of type BlockReorderManager
97 * \param bmat original full blocked operator (we keep the RCP to make sure all subblocks are available)
98 */
102 size_t npr,
104 Teuchos::RCP<const Xpetra::BlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > bmat)
105 : Xpetra::BlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>(rangeMaps, domainMaps, npr) {
106 brm_ = brm;
107 fullOp_ = bmat;
108 }
109
110 //protected:
111
116
117 private:
119 RCP<const MapExtractor> fullRangeMapExtractor = fullOp_->getRangeMapExtractor();
120
121 // number of sub blocks
122 size_t numBlocks = brm->GetNumBlocks();
123
124 Teuchos::RCP<const Map> map = Teuchos::null;
125
126 if(numBlocks == 0) {
127 // it is a leaf node
128 Teuchos::RCP<const Xpetra::BlockReorderLeaf> leaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(brm);
129
130 // never extract Thyra style maps (since we have to merge them)
131 map = fullRangeMapExtractor->getMap(Teuchos::as<size_t>(leaf->GetIndex()), false);
132 } else {
133 // initialize vector for sub maps
134 std::vector<Teuchos::RCP<const Map> > subMaps (numBlocks, Teuchos::null);
135
136 for(size_t i = 0; i < numBlocks; i++) {
138 subMaps[i] = mergeSubBlockMaps(blkMgr);
139 TEUCHOS_ASSERT(subMaps[i].is_null()==false);
142 map = MapUtils::concatenateMaps(subMaps);
143 }
144 TEUCHOS_ASSERT(map.is_null()==false);
145 return map;
146 }
147
148 public:
150
151
153 virtual void apply(const MultiVector &X, MultiVector &Y, Teuchos::ETransp mode, Scalar alpha, Scalar beta, bool sumInterfaceValues,
154 const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> >& regionInterfaceImporter,
155 const Teuchos::ArrayRCP<LocalOrdinal>& regionInterfaceLIDs) const
156 { }
157
158
161 virtual void apply(const MultiVector& X, MultiVector& Y,
163 Scalar alpha = ScalarTraits<Scalar>::one(),
164 Scalar beta = ScalarTraits<Scalar>::zero()) const
165 {
166
167 // Nested sub-operators should just use the provided X and B vectors
168 if(fullOp_->getLocalNumRows() != this->getLocalNumRows()) {
169 Xpetra::BlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>::apply(X,Y,mode,alpha,beta);
170 return;
171 }
172
173 // Special handling for the top level of the nested operator
174
175 // check whether input parameters are blocked or not
176 RCP<const MultiVector> refX = rcpFromRef(X);
177 RCP<const BlockedMultiVector> refbX = Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(refX);
178 RCP<MultiVector> tmpY = rcpFromRef(Y);
179 RCP<BlockedMultiVector> tmpbY = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(tmpY);
182 bool bCopyResultX = false;
183 bool bCopyResultY = false;
185 // TODO create a nested ReorderedBlockedMultiVector out of a nested BlockedMultiVector!
186 // probably not necessary, is it?
187 // check whether X and B are blocked but not ReorderedBlocked operators
188 /*if (refbX != Teuchos::null && fullOp_->getLocalNumRows() == this->getLocalNumRows()) {
189 RCP<const ReorderedBlockedMultiVector> rbCheck = Teuchos::rcp_dynamic_cast<const ReorderedBlockedMultiVector>(refbX);
190 if(rbCheck == Teuchos::null) {
191 RCP<const BlockedMultiVector> bX =
192 Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(Xpetra::buildReorderedBlockedMultiVector(brm_, refbX));
193 TEUCHOS_ASSERT(bX.is_null()==false);
194 refbX.swap(bX);
195 }
196 }
197 if (tmpbY != Teuchos::null && fullOp_->getLocalNumRows() == this->getLocalNumRows()) {
198 RCP<ReorderedBlockedMultiVector> rbCheck = Teuchos::rcp_dynamic_cast<ReorderedBlockedMultiVector>(tmpbY);
199 if(rbCheck == Teuchos::null) {
200 RCP<BlockedMultiVector> bY =
201 Teuchos::rcp_dynamic_cast<BlockedMultiVector>(Xpetra::buildReorderedBlockedMultiVector(brm_, tmpbY));
202 TEUCHOS_ASSERT(bY.is_null()==false);
203 tmpbY.swap(bY);
204 }
205 }*/
206
207 // if X and B are not blocked, create a blocked version here and use the blocked vectors
208 // for the internal (nested) apply call.
209
210 // Check whether "this" operator is the reordered variant of the underlying fullOp_.
211 // Note, that nested ReorderedBlockedCrsMatrices always have the same full operator "fullOp_"
212 // stored underneath for being able to "translate" the block ids.
213 if (refbX == Teuchos::null && fullOp_->getLocalNumRows() == this->getLocalNumRows())
215 // create a new (non-nested) blocked multi vector (using the blocked range map of fullOp_)
216 RCP<const BlockedMap> blkRgMap = Teuchos::rcp_dynamic_cast<const BlockedMap>(fullOp_->getRangeMap());
217 TEUCHOS_ASSERT(blkRgMap.is_null()==false);
218 RCP<const BlockedMultiVector> bXtemp = Teuchos::rcp(new BlockedMultiVector(blkRgMap, refX));
219 TEUCHOS_ASSERT(bXtemp.is_null()==false);
221 Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(Xpetra::buildReorderedBlockedMultiVector(brm_, bXtemp));
222 TEUCHOS_ASSERT(bX.is_null()==false);
223 refbX.swap(bX);
224 bCopyResultX = true;
225 }
226
227 if (tmpbY == Teuchos::null && fullOp_->getLocalNumRows() == this->getLocalNumRows()) {
228 // create a new (non-nested) blocked multi vector (using the blocked range map of fullOp_)
229 RCP<const BlockedMap> blkRgMap = Teuchos::rcp_dynamic_cast<const BlockedMap>(fullOp_->getRangeMap());
230 TEUCHOS_ASSERT(blkRgMap.is_null()==false);
231 RCP<BlockedMultiVector> tmpbYtemp = Teuchos::rcp(new BlockedMultiVector(blkRgMap, tmpY));
232 TEUCHOS_ASSERT(tmpbYtemp.is_null()==false);
234 Teuchos::rcp_dynamic_cast<BlockedMultiVector>(Xpetra::buildReorderedBlockedMultiVector(brm_, tmpbYtemp));
235 TEUCHOS_ASSERT(bY.is_null()==false);
236 tmpbY.swap(bY);
237 bCopyResultY = true;
238 }
239
240 TEUCHOS_ASSERT(refbX.is_null()==false);
241 TEUCHOS_ASSERT(tmpbY.is_null()==false);
242
243 Xpetra::BlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>::apply(*refbX,*tmpbY,mode,alpha,beta);
244
245 if (bCopyResultX == true) {
246 RCP<const MultiVector> Xmerged = refbX->Merge();
247 RCP<MultiVector> nonconstX = Teuchos::rcp_const_cast<MultiVector>(refX);
250 if (bCopyResultY == true) {
251 RCP< MultiVector> Ymerged = tmpbY->Merge();
253 }
254
256
257 // @}
262
263
266
267 /** \brief Returns internal unmodified BlockedCrsMatrix object */
269
270 // @}
271
273
274
276 std::string description() const { return "ReorderedBlockedCrsMatrix"; }
277
280
281 out << "Xpetra::ReorderedBlockedCrsMatrix: " << BlockedCrsMatrix::Rows() << " x " << BlockedCrsMatrix::Cols() << std::endl;
282
284 out << "ReorderedBlockMatrix is fillComplete" << std::endl;
285
286 out << "fullRowMap" << std::endl;
287 BlockedCrsMatrix::getRangeMap(0,false)->describe(out,verbLevel);
288
289 //out << "fullColMap" << std::endl;
290 //fullcolmap_->describe(out,verbLevel);
291
292 } else {
293 out << "Xpetra::ReorderedBlockedCrsMatrix is NOT fillComplete" << std::endl;
294 }
295
296 for (size_t r = 0; r < BlockedCrsMatrix::Rows(); ++r)
297 for (size_t c = 0; c < BlockedCrsMatrix::Cols(); ++c) {
298 out << "Block(" << r << "," << c << ")" << std::endl;
299 BlockedCrsMatrix::getMatrix(r,c)->describe(out,verbLevel);
300 }
301 }
302
304
305 private:
308
309
310};
311
312template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
313Teuchos::RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > mergeSubBlockMaps(Teuchos::RCP<const Xpetra::BlockReorderManager> brm, Teuchos::RCP<const Xpetra::BlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > bmat, bool bThyraMode) {
315
316 // TODO distinguish between range and domain map extractor! provide MapExtractor as parameter!
317 RCP<const Xpetra::MapExtractor<Scalar,LocalOrdinal,GlobalOrdinal,Node> > fullRangeMapExtractor = bmat->getRangeMapExtractor();
318
319 // number of sub blocks
320 size_t numBlocks = brm->GetNumBlocks();
321
323
324 if(numBlocks == 0) {
325 // it is a leaf node
326 Teuchos::RCP<const Xpetra::BlockReorderLeaf> leaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(brm);
327
328 map = fullRangeMapExtractor->getMap(Teuchos::as<size_t>(leaf->GetIndex()), bThyraMode);
329 } else {
330 // initialize vector for sub maps
331 std::vector<Teuchos::RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > > subMaps (numBlocks, Teuchos::null);
332
333 for(size_t i = 0; i < numBlocks; i++) {
335 subMaps[i] = mergeSubBlockMaps(blkMgr,bmat,bThyraMode);
336 TEUCHOS_ASSERT(subMaps[i].is_null()==false);
337 }
338
339#if 1
340 // concatenate submaps
341 // for Thyra mode this map isn't important
343
344 // create new BlockedMap (either in Thyra Mode or Xpetra mode)
345 map = Teuchos::rcp(new Xpetra::BlockedMap<LocalOrdinal,GlobalOrdinal,Node>(fullMap, subMaps, bThyraMode));
346#else
347 // TAW: 11/27/16 we just concatenate the submaps to one monolithic Map object.
348 // Alternatively, we could create a new BlockedMap using the concatenated map and the submaps
349 // However, the block smoothers only need the concatenated map for creating MultiVectors...
350 // But for the Thyra mode version concatenating would not be ok for the whole map
351 map = MapUtils::concatenateMaps(subMaps);
352#endif
353 }
354 TEUCHOS_ASSERT(map.is_null()==false);
355 return map;
356}
357
358template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
360
363 typedef Xpetra::MapExtractor<Scalar,LocalOrdinal,GlobalOrdinal,Node> MapExtractor;
365 typedef Xpetra::BlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> BlockedCrsMatrix;
366 typedef Xpetra::ReorderedBlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> ReorderedBlockedCrsMatrix;
367
368 // number of sub blocks
369 size_t rowSz = rowMgr->GetNumBlocks();
370 size_t colSz = colMgr->GetNumBlocks();
371
372 Teuchos::RCP<BlockedCrsMatrix> rbmat = Teuchos::null;
373
374 if(rowSz == 0 && colSz == 0) {
375 // it is a leaf node
376 Teuchos::RCP<const Xpetra::BlockReorderLeaf> rowleaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(rowMgr);
377 Teuchos::RCP<const Xpetra::BlockReorderLeaf> colleaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(colMgr);
378
379 // extract leaf node
380 Teuchos::RCP<Matrix> mat = bmat->getMatrix(rowleaf->GetIndex(), colleaf->GetIndex());
381
382 if (mat == Teuchos::null) return Teuchos::null;
383
384 // check, whether leaf node is of type Xpetra::CrsMatrixWrap
385 Teuchos::RCP<Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> > matwrap = Teuchos::rcp_dynamic_cast<Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(mat);
386 if(matwrap != Teuchos::null) {
387 // If the leaf node is of type Xpetra::CrsMatrixWrap wrap it into a 1x1 ReorderedBlockMatrix
388 // with the corresponding MapExtractors for translating Thyra to Xpetra GIDs if necessary
389 RCP<const MapExtractor> fullRangeMapExtractor = bmat->getRangeMapExtractor();
390 Teuchos::RCP<const Map> submap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(),false);
391 std::vector<Teuchos::RCP<const Map> > rowSubMaps (1, submap);
392 Teuchos::RCP<const MapExtractor> rgMapExtractor = Teuchos::rcp(new MapExtractor(submap, rowSubMaps, false));
393
394 RCP<const MapExtractor> fullDomainMapExtractor = bmat->getDomainMapExtractor();
395 Teuchos::RCP<const Map> submap2 = fullDomainMapExtractor->getMap(colleaf->GetIndex(),false);
396 std::vector<Teuchos::RCP<const Map> > colSubMaps (1, submap2);
397 Teuchos::RCP<const MapExtractor> doMapExtractor = Teuchos::rcp(new MapExtractor(submap2, colSubMaps, false));
398
399 rbmat = Teuchos::rcp(new ReorderedBlockedCrsMatrix(rgMapExtractor,doMapExtractor, 33, rowMgr,bmat));
400 rbmat->setMatrix(0,0,mat);
401 } else {
402 // If leaf node is already wrapped into a blocked matrix do not wrap it again.
403 rbmat = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(mat);
404 TEUCHOS_ASSERT(rbmat != Teuchos::null);
405 }
406 TEUCHOS_ASSERT(mat->getLocalNumEntries() == rbmat->getLocalNumEntries());
407 } else {
408 // create the map extractors
409 // we cannot create block matrix in thyra mode since merged maps might not start with 0 GID
410 Teuchos::RCP<const MapExtractor> rgMapExtractor = Teuchos::null;
411 if(rowSz > 0) {
412 std::vector<Teuchos::RCP<const Map> > rowSubMaps (rowSz, Teuchos::null);
413 for(size_t i = 0; i < rowSz; i++) {
415 rowSubMaps[i] = mergeSubBlockMaps(rowSubMgr,bmat,false /*xpetra*/);
416 TEUCHOS_ASSERT(rowSubMaps[i].is_null()==false);
417 }
418 Teuchos::RCP<const Map> rgMergedSubMaps = MapUtils::concatenateMaps(rowSubMaps);
419 rgMapExtractor = Teuchos::rcp(new MapExtractor(rgMergedSubMaps, rowSubMaps, false));
420 } else {
421 Teuchos::RCP<const Xpetra::BlockReorderLeaf> rowleaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(rowMgr);
422 RCP<const MapExtractor> fullRangeMapExtractor = bmat->getRangeMapExtractor();
423 // TODO think about Thyra style maps: we cannot use thyra style maps when recombining several blocks!!!
424 // The GIDs might not start with 0 and may not be consecutive!
425 Teuchos::RCP<const Map> submap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(),false);
426 std::vector<Teuchos::RCP<const Map> > rowSubMaps (1, submap);
427 rgMapExtractor = Teuchos::rcp(new MapExtractor(submap, rowSubMaps, false));
428 }
429
430 Teuchos::RCP<const MapExtractor> doMapExtractor = Teuchos::null;
431 if(colSz > 0) {
432 std::vector<Teuchos::RCP<const Map> > colSubMaps (colSz, Teuchos::null);
433 for(size_t j = 0; j < colSz; j++) {
435 colSubMaps[j] = mergeSubBlockMaps(colSubMgr,bmat,false/*xpetra*/);
436 TEUCHOS_ASSERT(colSubMaps[j].is_null()==false);
437 }
438 Teuchos::RCP<const Map> doMergedSubMaps = MapUtils::concatenateMaps(colSubMaps);
439 doMapExtractor = Teuchos::rcp(new MapExtractor(doMergedSubMaps, colSubMaps, false));
440 } else {
441 Teuchos::RCP<const Xpetra::BlockReorderLeaf> colleaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(colMgr);
442 RCP<const MapExtractor> fullDomainMapExtractor = bmat->getDomainMapExtractor();
443 // TODO think about Thyra style maps: we cannot use thyra style maps when recombining several blocks!!!
444 // The GIDs might not start with 0 and may not be consecutive!
445 Teuchos::RCP<const Map> submap = fullDomainMapExtractor->getMap(colleaf->GetIndex(),false);
446 std::vector<Teuchos::RCP<const Map> > colSubMaps (1, submap);
447 doMapExtractor = Teuchos::rcp(new MapExtractor(submap, colSubMaps, false));
448 }
449
450 rbmat = Teuchos::rcp(new ReorderedBlockedCrsMatrix(rgMapExtractor,doMapExtractor, 33, rowMgr,bmat));
451
452 size_t cntNNZ = 0;
453
454 if (rowSz == 0 && colSz > 0) {
455 for(size_t j = 0; j < colSz; j++) {
457 Teuchos::RCP<const Matrix> submat = mergeSubBlocks(rowMgr, colSubMgr, bmat);
458 rbmat->setMatrix(0,j,Teuchos::rcp_const_cast<Matrix>(submat));
459 if(submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries();
460 }
461 } else if (rowSz > 0 && colSz == 0) {
462 for(size_t i = 0; i < rowSz; i++) {
464 Teuchos::RCP<const Matrix> submat = mergeSubBlocks(rowSubMgr, colMgr, bmat);
465 rbmat->setMatrix(i,0,Teuchos::rcp_const_cast<Matrix>(submat));
466 if(submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries();
467 }
468 } else {
469 for(size_t i = 0; i < rowSz; i++) {
471 for(size_t j = 0; j < colSz; j++) {
473 Teuchos::RCP<const Matrix> submat = mergeSubBlocks(rowSubMgr, colSubMgr, bmat);
474 rbmat->setMatrix(i,j,Teuchos::rcp_const_cast<Matrix>(submat));
475 if(submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries();
476 }
477 }
478 }
479 TEUCHOS_ASSERT(rbmat->getLocalNumEntries() == cntNNZ);
480 }
481 rbmat->fillComplete();
482 return rbmat;
483}
484
485 //MapExtractor(const std::vector<RCP<const Map> >& maps, const std::vector<RCP<const Map> >& thyramaps);
486
487template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
489
491 typedef Xpetra::MapExtractor<Scalar,LocalOrdinal,GlobalOrdinal,Node> MapExtractor;
493 typedef Xpetra::BlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> BlockedCrsMatrix;
494 typedef Xpetra::ReorderedBlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> ReorderedBlockedCrsMatrix;
495
496 TEUCHOS_ASSERT(bmat->getRangeMapExtractor()->getThyraMode() == true);
497 TEUCHOS_ASSERT(bmat->getDomainMapExtractor()->getThyraMode() == true);
498
499 // number of sub blocks
500 size_t rowSz = rowMgr->GetNumBlocks();
501 size_t colSz = colMgr->GetNumBlocks();
502
503 Teuchos::RCP<BlockedCrsMatrix> rbmat = Teuchos::null;
504
505 if(rowSz == 0 && colSz == 0) {
506 // it is a leaf node
507 Teuchos::RCP<const Xpetra::BlockReorderLeaf> rowleaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(rowMgr);
508 Teuchos::RCP<const Xpetra::BlockReorderLeaf> colleaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(colMgr);
509
510 // this matrix uses Thyra style GIDs as global row, range, domain and column indices
511 Teuchos::RCP<Matrix> mat = bmat->getMatrix(rowleaf->GetIndex(), colleaf->GetIndex());
512
513 if(mat == Teuchos::null) return Teuchos::null; //std::cout << "Block " << rowleaf->GetIndex() << "," << colleaf->GetIndex() << " is zero" << std::endl;
514
515 // check, whether leaf node is of type Xpetra::CrsMatrixWrap
516 Teuchos::RCP<Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> > matwrap = Teuchos::rcp_dynamic_cast<Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(mat);
517 if(matwrap != Teuchos::null) {
519 // build map extractors
520 RCP<const MapExtractor> fullRangeMapExtractor = bmat->getRangeMapExtractor();
521 // extract Xpetra and Thyra based GIDs
522 Teuchos::RCP<const Map> xpsubmap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(),false);
523 Teuchos::RCP<const Map> thysubmap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(),true);
524 std::vector<Teuchos::RCP<const Map> > rowXpSubMaps (1, xpsubmap);
525 std::vector<Teuchos::RCP<const Map> > rowTySubMaps (1, thysubmap);
526 // use expert constructor
527 Teuchos::RCP<const MapExtractor> rgMapExtractor = Teuchos::rcp(new MapExtractor(rowXpSubMaps, rowTySubMaps));
528
529 RCP<const MapExtractor> fullDomainMapExtractor = bmat->getDomainMapExtractor();
530 // extract Xpetra and Thyra based GIDs
531 Teuchos::RCP<const Map> xpsubmap2 = fullDomainMapExtractor->getMap(colleaf->GetIndex(),false);
532 Teuchos::RCP<const Map> tysubmap2 = fullDomainMapExtractor->getMap(colleaf->GetIndex(),true);
533 std::vector<Teuchos::RCP<const Map> > colXpSubMaps (1, xpsubmap2);
534 std::vector<Teuchos::RCP<const Map> > colTySubMaps (1, tysubmap2);
535 // use expert constructor
536 Teuchos::RCP<const MapExtractor> doMapExtractor = Teuchos::rcp(new MapExtractor(colXpSubMaps, colTySubMaps));
537
539 // build reordered block operator
540 rbmat = Teuchos::rcp(new ReorderedBlockedCrsMatrix(rgMapExtractor,doMapExtractor, 33, rowMgr,bmat));
541 rbmat->setMatrix(0,0,mat);
542 } else {
543 // If leaf node is already wrapped into a blocked matrix do not wrap it again.
544 rbmat = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(mat);
545 TEUCHOS_ASSERT(rbmat != Teuchos::null);
546 }
547 TEUCHOS_ASSERT(mat->getLocalNumEntries() == rbmat->getLocalNumEntries());
548 } else {
549 // create the map extractors
550 // we cannot create block matrix in thyra mode since merged maps might not start with 0 GID
551 Teuchos::RCP<const MapExtractor> rgMapExtractor = Teuchos::null;
552 if(rowSz > 0) {
553 std::vector<Teuchos::RCP<const Map> > rowXpSubMaps (rowSz, Teuchos::null);
554 std::vector<Teuchos::RCP<const Map> > rowTySubMaps (rowSz, Teuchos::null);
555 for(size_t i = 0; i < rowSz; i++) {
557 // extract Xpetra and Thyra based merged GIDs
558 rowXpSubMaps[i] = mergeSubBlockMaps(rowSubMgr,bmat,false);
559 rowTySubMaps[i] = mergeSubBlockMaps(rowSubMgr,bmat,true);
560 TEUCHOS_ASSERT(rowXpSubMaps[i].is_null()==false);
561 TEUCHOS_ASSERT(rowTySubMaps[i].is_null()==false);
562 }
563 // use expert constructor
564 rgMapExtractor = Teuchos::rcp(new MapExtractor(rowXpSubMaps, rowTySubMaps));
565 } else {
566 Teuchos::RCP<const Xpetra::BlockReorderLeaf> rowleaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(rowMgr);
567 RCP<const MapExtractor> fullRangeMapExtractor = bmat->getRangeMapExtractor();
568 // extract Xpetra and Thyra based GIDs
569 Teuchos::RCP<const Map> xpsubmap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(),false);
570 Teuchos::RCP<const Map> thysubmap = fullRangeMapExtractor->getMap(rowleaf->GetIndex(),true);
571 std::vector<Teuchos::RCP<const Map> > rowXpSubMaps (1, xpsubmap);
572 std::vector<Teuchos::RCP<const Map> > rowTySubMaps (1, thysubmap);
573 // use expert constructor
574 rgMapExtractor = Teuchos::rcp(new MapExtractor(rowXpSubMaps, rowTySubMaps));
575 }
576
577 Teuchos::RCP<const MapExtractor> doMapExtractor = Teuchos::null;
578 if(colSz > 0) {
579 std::vector<Teuchos::RCP<const Map> > colXpSubMaps (colSz, Teuchos::null);
580 std::vector<Teuchos::RCP<const Map> > colTySubMaps (colSz, Teuchos::null);
581 for(size_t j = 0; j < colSz; j++) {
583 // extract Xpetra and Thyra based merged GIDs
584 colXpSubMaps[j] = mergeSubBlockMaps(colSubMgr,bmat,false);
585 colTySubMaps[j] = mergeSubBlockMaps(colSubMgr,bmat,true);
586 TEUCHOS_ASSERT(colXpSubMaps[j].is_null()==false);
587 TEUCHOS_ASSERT(colTySubMaps[j].is_null()==false);
588 }
589 // use expert constructor
590 doMapExtractor = Teuchos::rcp(new MapExtractor(colXpSubMaps,colTySubMaps));
591 } else {
592 Teuchos::RCP<const Xpetra::BlockReorderLeaf> colleaf = Teuchos::rcp_dynamic_cast<const Xpetra::BlockReorderLeaf>(colMgr);
593 RCP<const MapExtractor> fullDomainMapExtractor = bmat->getDomainMapExtractor();
594 // extract Xpetra and Thyra based GIDs
595 Teuchos::RCP<const Map> xpsubmap = fullDomainMapExtractor->getMap(colleaf->GetIndex(),false);
596 Teuchos::RCP<const Map> tysubmap = fullDomainMapExtractor->getMap(colleaf->GetIndex(),true);
597 std::vector<Teuchos::RCP<const Map> > colXpSubMaps (1, xpsubmap);
598 std::vector<Teuchos::RCP<const Map> > colTySubMaps (1, tysubmap);
599 // use expert constructor
600 doMapExtractor = Teuchos::rcp(new MapExtractor(colXpSubMaps, colTySubMaps));
601 }
602
603 // TODO matrix should have both rowMgr and colMgr??
604 rbmat = Teuchos::rcp(new ReorderedBlockedCrsMatrix(rgMapExtractor,doMapExtractor, 33, rowMgr,bmat));
605
606 size_t cntNNZ = 0;
607
608 if (rowSz == 0 && colSz > 0) {
609 for(size_t j = 0; j < colSz; j++) {
611 Teuchos::RCP<const Matrix> submat = mergeSubBlocksThyra(rowMgr, colSubMgr, bmat);
612 rbmat->setMatrix(0,j,Teuchos::rcp_const_cast<Matrix>(submat));
613 if(submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries();
614 }
615 } else if (rowSz > 0 && colSz == 0) {
616 for(size_t i = 0; i < rowSz; i++) {
618 Teuchos::RCP<const Matrix> submat = mergeSubBlocksThyra(rowSubMgr, colMgr, bmat);
619 rbmat->setMatrix(i,0,Teuchos::rcp_const_cast<Matrix>(submat));
620 if(submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries();
621 }
622 } else {
623 for(size_t i = 0; i < rowSz; i++) {
625 for(size_t j = 0; j < colSz; j++) {
627 Teuchos::RCP<const Matrix> submat = mergeSubBlocksThyra(rowSubMgr, colSubMgr, bmat);
628 rbmat->setMatrix(i,j,Teuchos::rcp_const_cast<Matrix>(submat));
629 if(submat != Teuchos::null) cntNNZ += submat->getLocalNumEntries();
630 }
631 }
632 }
633 TEUCHOS_ASSERT(rbmat->getLocalNumEntries() == cntNNZ);
634 }
635
636 rbmat->fillComplete();
637 return rbmat;
638}
639
640template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
642 TEUCHOS_ASSERT(bmat->getRangeMapExtractor()->getThyraMode() == bmat->getDomainMapExtractor()->getThyraMode());
644 if(bmat->getRangeMapExtractor()->getThyraMode() == false) {
645 rbmat = mergeSubBlocks(brm, brm, bmat);
646 } else {
647 rbmat = mergeSubBlocksThyra(brm, brm, bmat);
648 }
649
650 // TAW, 6/7/2016: rbmat might be Teuchos::null for empty blocks!
651 return rbmat;
652}
653
654} //namespace Xpetra
655
656#define XPETRA_REORDEREDBLOCKEDCRSMATRIX_SHORT
657#endif /* XPETRA_REORDEREDBLOCKEDCRSMATRIX_HPP */
static const EVerbosityLevel verbLevel_default
void swap(RCP< T > &r_ptr)
bool is_null() const
Xpetra utility class for common map-related routines.
static Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > concatenateMaps(const std::vector< Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > > &subMaps)
Helper function to concatenate several maps.
Xpetra-specific matrix class.
virtual void update(const Scalar &alpha, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta)=0
Update multi-vector values with scaled values of A, this = beta*this + alpha*A.
virtual void apply(const MultiVector &X, MultiVector &Y, Teuchos::ETransp mode, Scalar alpha, Scalar beta, bool sumInterfaceValues, const RCP< Xpetra::Import< LocalOrdinal, GlobalOrdinal, Node > > &regionInterfaceImporter, const Teuchos::ArrayRCP< LocalOrdinal > &regionInterfaceLIDs) const
sparse matrix-multivector multiplication for the region layout matrices (currently no blocked impleme...
std::string description() const
Return a simple one-line description of this object.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
virtual void apply(const MultiVector &X, MultiVector &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=ScalarTraits< Scalar >::one(), Scalar beta=ScalarTraits< Scalar >::zero()) const
Computes the sparse matrix-multivector multiplication.
Teuchos::RCP< const Xpetra::BlockReorderManager > brm_
Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > BlockedCrsMatrix
ReorderedBlockedCrsMatrix(Teuchos::RCP< const MapExtractor > &rangeMaps, Teuchos::RCP< const MapExtractor > &domainMaps, size_t npr, Teuchos::RCP< const Xpetra::BlockReorderManager > brm, Teuchos::RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > bmat)
Constructor.
Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > mergeSubBlockMaps(Teuchos::RCP< const Xpetra::BlockReorderManager > brm)
Teuchos::RCP< const Xpetra::BlockReorderManager > getBlockReorderManager()
Returns internal BlockReorderManager object.
Teuchos::RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getBlockedCrsMatrix()
Returns internal unmodified BlockedCrsMatrix object.
Teuchos::RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > fullOp_
#define TEUCHOS_ASSERT(assertion_test)
TypeTo as(const TypeFrom &t)
basic_FancyOStream< char > FancyOStream
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Xpetra namespace
Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > mergeSubBlockMaps(Teuchos::RCP< const Xpetra::BlockReorderManager > brm, Teuchos::RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > bmat, bool bThyraMode)
std::string viewLabel_t
Teuchos::RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > buildReorderedBlockedCrsMatrix(Teuchos::RCP< const Xpetra::BlockReorderManager > brm, Teuchos::RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > bmat)
Teuchos::RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > mergeSubBlocks(Teuchos::RCP< const Xpetra::BlockReorderManager > rowMgr, Teuchos::RCP< const Xpetra::BlockReorderManager > colMgr, Teuchos::RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > bmat)
Teuchos::RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > mergeSubBlocksThyra(Teuchos::RCP< const Xpetra::BlockReorderManager > rowMgr, Teuchos::RCP< const Xpetra::BlockReorderManager > colMgr, Teuchos::RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > bmat)
Teuchos::RCP< const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > buildReorderedBlockedMultiVector(Teuchos::RCP< const Xpetra::BlockReorderManager > brm, Teuchos::RCP< const Xpetra::BlockedMultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > bvec)