Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_CreateOverlapGraph.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #ifndef IFPACK2_CREATEOVERLAPGRAPH_HPP
44 #define IFPACK2_CREATEOVERLAPGRAPH_HPP
45 
46 #include "Ifpack2_ConfigDefs.hpp"
47 #include "Tpetra_CrsGraph.hpp"
48 #include "Tpetra_CrsMatrix.hpp"
49 #include "Tpetra_Import.hpp"
50 #include "Teuchos_RefCountPtr.hpp"
51 
52 
53 namespace Ifpack2 {
54 
56 
59 template<class LocalOrdinal, class GlobalOrdinal, class Node>
60 Teuchos::RCP<const Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > CreateOverlapGraph(const Teuchos::RCP<const Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> >& input_graph, int OverlapLevel)
61 {
62  typedef Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> GraphType;
63  typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> MapType;
64  typedef Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> ImportType;
65 
66  TEUCHOS_TEST_FOR_EXCEPTION(OverlapLevel < 0, std::runtime_error, "Ifpack2::CreateOverlapGraph: OverlapLevel must be >= 0.");
67 
68  Teuchos::RCP<GraphType> OverlapGraph;
69 
70  const int numProcs = input_graph->getMap()->getComm()->getSize();
71  if (OverlapLevel == 0 || numProcs < 2) return input_graph;
72 
73  Teuchos::RCP<const MapType> OverlapRowMap = input_graph->getRowMap();
74 
75  Teuchos::RCP<const GraphType> OldGraph;
76  Teuchos::RCP<const MapType> OldRowMap;
77  const Teuchos::RCP<const MapType> DomainMap = input_graph->getDomainMap();
78  const Teuchos::RCP<const MapType> RangeMap = input_graph->getRangeMap();
79 
80  for (int level=0; level < OverlapLevel; level++) {
81  OldGraph = OverlapGraph;
82  OldRowMap = OverlapRowMap;
83 
84  Teuchos::RCP<const ImportType> OverlapImporter;
85  if(level==0) OverlapImporter = input_graph->getImporter();
86  else OverlapImporter = OldGraph->getImporter();
87 
88  OverlapRowMap = OverlapImporter->getTargetMap();
89  if (level<OverlapLevel-1) {
90  OverlapGraph = Teuchos::rcp( new GraphType(OverlapRowMap, 0) );
91  }
92  else {
93  // On last iteration, we want to filter out all columns except those that
94  // correspond to rows in the graph. This assures that our matrix is square
95  OverlapGraph = Teuchos::rcp( new GraphType(OverlapRowMap, OverlapRowMap, 0) );
96  }
97 
98  OverlapGraph->doImport(*input_graph, *OverlapImporter, Tpetra::INSERT);
99  OverlapGraph->fillComplete(DomainMap, RangeMap);
100  }
101 
102  return OverlapGraph;
103 }
104 
105 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
106 Teuchos::RCP<const Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > CreateOverlapMatrix(const Teuchos::RCP<const Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& input_graph, int OverlapLevel)
107 {
108  typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> MatrixType;
109  typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> MapType;
110  typedef Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> ImportType;
111 
112  TEUCHOS_TEST_FOR_EXCEPTION(OverlapLevel < 0, std::runtime_error, "Ifpack2::CreateOverlapMatrix: OverlapLevel must be >= 0.");
113 
114  Teuchos::RCP<MatrixType> OverlapGraph;
115 
116  const int numProcs = input_graph->getMap()->getComm()->getSize();
117  if (OverlapLevel == 0 || numProcs < 2) return input_graph;
118 
119  Teuchos::RCP<const MapType> OverlapRowMap = input_graph->getRowMap();
120 
121  Teuchos::RCP<const MatrixType> OldGraph;
122  Teuchos::RCP<const MapType> OldRowMap;
123  const Teuchos::RCP<const MapType> DomainMap = input_graph->getDomainMap();
124  const Teuchos::RCP<const MapType> RangeMap = input_graph->getRangeMap();
125 
126  for (int level=0; level < OverlapLevel; level++) {
127  OldGraph = OverlapGraph;
128  OldRowMap = OverlapRowMap;
129 
130  Teuchos::RCP<const ImportType> OverlapImporter;
131  if(level==0) OverlapImporter = input_graph->getGraph()->getImporter();
132  else OverlapImporter = OldGraph->getGraph()->getImporter();
133 
134  OverlapRowMap = OverlapImporter->getTargetMap();
135  if (level<OverlapLevel-1) {
136  OverlapGraph = Teuchos::rcp( new MatrixType(OverlapRowMap, 0) );
137  }
138  else {
139  // On last iteration, we want to filter out all columns except those that
140  // correspond to rows in the graph. This assures that our matrix is square
141  OverlapGraph = Teuchos::rcp( new MatrixType(OverlapRowMap, OverlapRowMap, 0) );
142  }
143 
144  OverlapGraph->doImport(*input_graph, *OverlapImporter, Tpetra::INSERT);
145  OverlapGraph->fillComplete(DomainMap, RangeMap);
146  }
147 
148  return OverlapGraph;
149 }
150 
151 
152 
153 }//namespace Ifpack2
154 
155 #endif // IFPACK2_CREATEOVERLAPGRAPH_HPP
Teuchos::RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > CreateOverlapGraph(const Teuchos::RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &input_graph, int OverlapLevel)
Constructs an overlapped graph for use with Ifpack2 preconditioners.
Definition: Ifpack2_CreateOverlapGraph.hpp:60
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)