Ifpack2 Templated Preconditioning Package Version 1.0
Ifpack2_CreateOverlapGraph.hpp
00001 /*@HEADER
00002 // ***********************************************************************
00003 // 
00004 //       Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
00005 //                 Copyright (2009) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 //@HEADER
00028 */
00029 
00030 #ifndef IFPACK2_CREATEOVERLAPGRAPH_HPP
00031 #define IFPACK2_CREATEOVERLAPGRAPH_HPP
00032 
00033 #include "Ifpack2_ConfigDefs.hpp"
00034 #include "Tpetra_CrsGraph.hpp"
00035 #include "Tpetra_CrsMatrix.hpp"
00036 #include "Tpetra_Import.hpp"
00037 #include "Teuchos_RefCountPtr.hpp"
00038 
00039 
00040 namespace Ifpack2 {
00041 
00043 
00046 template<class LocalOrdinal, class GlobalOrdinal, class Node>
00047 Teuchos::RCP<const Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > CreateOverlapGraph(const Teuchos::RCP<const Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> >& input_graph, int OverlapLevel)
00048 {
00049   typedef Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> GraphType;
00050   typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> MapType;
00051   typedef Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> ImportType;
00052 
00053   TEUCHOS_TEST_FOR_EXCEPTION(OverlapLevel < 0, std::runtime_error, "Ifpack2::CreateOverlapGraph: OverlapLevel must be >= 0.");
00054 
00055   Teuchos::RCP<GraphType> OverlapGraph;
00056 
00057   const int numProcs = input_graph->getMap()->getComm()->getSize();
00058   if (OverlapLevel == 0 || numProcs < 2) return input_graph;
00059 
00060   Teuchos::RCP<const MapType> OverlapRowMap = input_graph->getRowMap();
00061 
00062   Teuchos::RCP<const GraphType> OldGraph;
00063   Teuchos::RCP<const MapType> OldRowMap;
00064   const Teuchos::RCP<const MapType> DomainMap = input_graph->getDomainMap();
00065   const Teuchos::RCP<const MapType> RangeMap = input_graph->getRangeMap();
00066 
00067   for (int level=0; level < OverlapLevel; level++) {
00068     OldGraph = OverlapGraph;
00069     OldRowMap = OverlapRowMap;
00070 
00071     Teuchos::RCP<const ImportType> OverlapImporter; 
00072     if(level==0) OverlapImporter = input_graph->getImporter();
00073     else OverlapImporter = OldGraph->getImporter();
00074 
00075     OverlapRowMap = OverlapImporter->getTargetMap();
00076     if (level<OverlapLevel-1) {
00077       OverlapGraph = Teuchos::rcp( new GraphType(OverlapRowMap, 0) );
00078     }
00079     else {
00080       // On last iteration, we want to filter out all columns except those that
00081       // correspond to rows in the graph.  This assures that our matrix is square
00082       OverlapGraph = Teuchos::rcp( new GraphType(OverlapRowMap, OverlapRowMap, 0) );
00083     }
00084 
00085     OverlapGraph->doImport(*input_graph, *OverlapImporter, Tpetra::INSERT);
00086     OverlapGraph->fillComplete(DomainMap, RangeMap);
00087   }
00088 
00089   return OverlapGraph;
00090 }
00091 
00092 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00093 Teuchos::RCP<const Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > CreateOverlapMatrix(const Teuchos::RCP<const Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& input_graph, int OverlapLevel)
00094 {
00095   typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> MatrixType;
00096   typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> MapType;
00097   typedef Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> ImportType;
00098 
00099   TEUCHOS_TEST_FOR_EXCEPTION(OverlapLevel < 0, std::runtime_error, "Ifpack2::CreateOverlapMatrix: OverlapLevel must be >= 0.");
00100 
00101   Teuchos::RCP<MatrixType> OverlapGraph;
00102 
00103   const int numProcs = input_graph->getMap()->getComm()->getSize();
00104   if (OverlapLevel == 0 || numProcs < 2) return input_graph;
00105 
00106   Teuchos::RCP<const MapType> OverlapRowMap = input_graph->getRowMap();
00107 
00108   Teuchos::RCP<const MatrixType> OldGraph;
00109   Teuchos::RCP<const MapType> OldRowMap;
00110   const Teuchos::RCP<const MapType> DomainMap = input_graph->getDomainMap();
00111   const Teuchos::RCP<const MapType> RangeMap = input_graph->getRangeMap();
00112 
00113   for (int level=0; level < OverlapLevel; level++) {
00114     OldGraph = OverlapGraph;
00115     OldRowMap = OverlapRowMap;
00116 
00117     Teuchos::RCP<const ImportType> OverlapImporter; 
00118     if(level==0) OverlapImporter = input_graph->getGraph()->getImporter();
00119     else OverlapImporter = OldGraph->getGraph()->getImporter();
00120 
00121     OverlapRowMap = OverlapImporter->getTargetMap();
00122     if (level<OverlapLevel-1) {
00123       OverlapGraph = Teuchos::rcp( new MatrixType(OverlapRowMap, 0) );
00124     }
00125     else {
00126       // On last iteration, we want to filter out all columns except those that
00127       // correspond to rows in the graph.  This assures that our matrix is square
00128       OverlapGraph = Teuchos::rcp( new MatrixType(OverlapRowMap, OverlapRowMap, 0) );
00129     }
00130 
00131     OverlapGraph->doImport(*input_graph, *OverlapImporter, Tpetra::INSERT);
00132     OverlapGraph->fillComplete(DomainMap, RangeMap);
00133   }
00134 
00135   return OverlapGraph;
00136 }
00137 
00138 
00139 
00140 }//namespace Ifpack2
00141 
00142 #endif // IFPACK2_CREATEOVERLAPGRAPH_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends