Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
TpetraExt_MMHelpers_def.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2008) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 
00042 #ifndef TPETRA_MMHELPERS_DEF_HPP
00043 #define TPETRA_MMHELPERS_DEF_HPP
00044 
00045 #include "TpetraExt_MMHelpers_decl.hpp"
00046 #include "Teuchos_VerboseObject.hpp"
00047 
00052 namespace Tpetra {
00053 
00054 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00055 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CrsMatrixStruct()
00056 {
00057 }
00058 
00059 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00060 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::~CrsMatrixStruct()
00061 {
00062   deleteContents();
00063 }
00064 
00065 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00066 void CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00067 deleteContents ()
00068 {
00069   importMatrix.reset();
00070   origMatrix = Teuchos::null;
00071 }
00072 
00073 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00074 int dumpCrsMatrixStruct (const CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& M)
00075 {
00076   std::cout << "proc " << M.rowMap->Comm().MyPID()<<std::endl;
00077   std::cout << "numRows: " << M.numRows<<std::endl;
00078   for(LocalOrdinal i=0; i<M.numRows; ++i) {
00079     for(LocalOrdinal j=0; j<M.numEntriesPerRow[i]; ++j) {
00080       std::cout << "   "<<M.rowMap->GID(i)<<"   "
00081                 <<M.colMap->GID(M.indices[i][j])<<"   "<<M.values[i][j]<<std::endl;
00082     }
00083   }
00084 
00085   return 0;
00086 }
00087 
00088 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00089 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00090 CrsWrapper_CrsMatrix (CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& crsmatrix)
00091  : crsmat_ (crsmatrix)
00092 {
00093 }
00094 
00095 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00096 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::~CrsWrapper_CrsMatrix()
00097 {
00098 }
00099 
00100 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00101 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
00102 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::getRowMap() const
00103 {
00104   return crsmat_.getRowMap();
00105 }
00106 
00107 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00108 bool CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00109 isFillComplete ()
00110 {
00111   return crsmat_.isFillComplete ();
00112 }
00113 
00114 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00115 void
00116 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00117 insertGlobalValues (GlobalOrdinal globalRow,
00118                     const Teuchos::ArrayView<const GlobalOrdinal> &indices,
00119                     const Teuchos::ArrayView<const Scalar> &values)
00120 {
00121   crsmat_.insertGlobalValues (globalRow, indices, values);
00122 }
00123 
00124 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00125 void
00126 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00127 sumIntoGlobalValues (GlobalOrdinal globalRow,
00128                      const Teuchos::ArrayView<const GlobalOrdinal> &indices,
00129                      const Teuchos::ArrayView<const Scalar> &values)
00130 {
00131   crsmat_.sumIntoGlobalValues (globalRow, indices, values);
00132 }
00133 
00134 
00135 
00136 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00137 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00138 CrsWrapper_GraphBuilder (const Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >& map)
00139  : graph_(),
00140    rowmap_(map),
00141    max_row_length_(0)
00142 {
00143   Teuchos::ArrayView<const GlobalOrdinal> rows = map->getNodeElementList ();
00144   const LocalOrdinal numRows = static_cast<LocalOrdinal> (rows.size ());
00145   for (LocalOrdinal i = 0; i < numRows; ++i) {
00146     graph_[rows[i]] = new std::set<GlobalOrdinal>;
00147   }
00148 }
00149 
00150 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00151 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00152 ~CrsWrapper_GraphBuilder ()
00153 {
00154   typename std::map<GlobalOrdinal,std::set<GlobalOrdinal>*>::iterator
00155     iter = graph_.begin(), iter_end = graph_.end();
00156   for (; iter != iter_end; ++iter) {
00157     delete iter->second;
00158   }
00159   graph_.clear ();
00160 }
00161 
00162 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00163 bool CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::isFillComplete()
00164 {
00165   return false;
00166 }
00167 
00168 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00169 void
00170 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00171 insertGlobalValues (GlobalOrdinal globalRow,
00172                     const Teuchos::ArrayView<const GlobalOrdinal> &indices,
00173                     const Teuchos::ArrayView<const Scalar> &values)
00174 {
00175   typename std::map<GlobalOrdinal,std::set<GlobalOrdinal>*>::iterator
00176     iter = graph_.find (globalRow);
00177 
00178   TEUCHOS_TEST_FOR_EXCEPTION(
00179     iter == graph_.end(), std::runtime_error,
00180     "Tpetra::CrsWrapper_GraphBuilder::insertGlobalValues could not find row "
00181     << globalRow << " in the graph. Super bummer man. Hope you figure it out.");
00182 
00183   std::set<GlobalOrdinal>& cols = * (iter->second);
00184 
00185   for (typename Teuchos::ArrayView<const GlobalOrdinal>::size_type i = 0;
00186        i < indices.size (); ++i) {
00187     cols.insert (indices[i]);
00188   }
00189 
00190   const global_size_t row_length = static_cast<global_size_t> (cols.size ());
00191   if (row_length > max_row_length_) {
00192     max_row_length_ = row_length;
00193   }
00194 }
00195 
00196 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00197 void
00198 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
00199 sumIntoGlobalValues (GlobalOrdinal globalRow,
00200                      const Teuchos::ArrayView<const GlobalOrdinal> &indices,
00201                      const Teuchos::ArrayView<const Scalar> &values)
00202 {
00203   insertGlobalValues (globalRow, indices, values);
00204 }
00205 
00206 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00207 std::map<GlobalOrdinal,std::set<GlobalOrdinal>*>&
00208 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::get_graph ()
00209 {
00210   return graph_;
00211 }
00212 
00213 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
00214 void
00215 insert_matrix_locations (CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>& graphbuilder,
00216                          CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& C)
00217 {
00218   global_size_t max_row_length = graphbuilder.get_max_row_length();
00219   if (max_row_length < 1) return;
00220 
00221   Teuchos::Array<GlobalOrdinal> indices(max_row_length);
00222   Teuchos::Array<Scalar> zeros(max_row_length, Teuchos::ScalarTraits<Scalar>::zero());
00223 
00224   typedef std::map<GlobalOrdinal,std::set<GlobalOrdinal>*> Graph;
00225   typedef typename Graph::iterator GraphIter;
00226   Graph& graph = graphbuilder.get_graph ();
00227 
00228   const GraphIter iter_end = graph.end ();
00229   for (GraphIter iter = graph.begin (); iter != iter_end; ++iter) {
00230     const GlobalOrdinal row = iter->first;
00231     const std::set<GlobalOrdinal>& cols = * (iter->second);
00232     // "copy" entries out of set into contiguous array storage
00233     const size_t num_entries = std::copy (cols.begin (), cols.end (), indices.begin ()) - indices.begin ();
00234     // insert zeros into the result matrix at the appropriate locations
00235     C.insertGlobalValues (row, indices (0, num_entries), zeros (0, num_entries));
00236   }
00237 }
00238 
00239 } // namespace Tpetra
00240 
00241 //
00242 // Explicit instantiation macro
00243 //
00244 // Must be expanded from within the Tpetra namespace!
00245 //
00246 
00247 #define TPETRA_CRSMATRIXSTRUCT_INSTANT(SCALAR,LO,GO,NODE) \
00248   \
00249   template class CrsMatrixStruct< SCALAR , LO , GO , NODE >;
00250 
00251 #define TPETRA_CRSWRAPPER_INSTANT(SCALAR,LO,GO,NODE) \
00252   \
00253   template class CrsWrapper< SCALAR , LO , GO , NODE >;
00254 
00255 #define TPETRA_CRSWRAPPER_CRSMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
00256   \
00257   template class CrsWrapper_CrsMatrix< SCALAR , LO , GO , NODE >;
00258 
00259 #define TPETRA_CRSWRAPPER_GRAPHBUILDER_INSTANT(SCALAR,LO,GO,NODE) \
00260   \
00261   template class CrsWrapper_GraphBuilder< SCALAR , LO , GO , NODE >;
00262 
00263 #endif // TPETRA_MMHELPERS_DEF_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines