Amesos2 - Direct Sparse Solver Interfaces Version of the Day
Amesos2_EpetraRowMatrix_AbstractMatrixAdapter_def.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //           Amesos2: Templated Direct Sparse Solver Package 
00006 //                  Copyright 2011 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 //
00042 // @HEADER
00043 
00044 
00053 #ifndef AMESOS2_EPETRAROWMATRIX_ABSTRACTMATRIXADAPTER_DEF_HPP
00054 #define AMESOS2_EPETRAROWMATRIX_ABSTRACTMATRIXADAPTER_DEF_HPP
00055 
00056 #include <Epetra_RowMatrix.h>
00057 #include <Epetra_Map.h>
00058 #include <Epetra_Comm.h>
00059 
00060 #include "Amesos2_EpetraRowMatrix_AbstractMatrixAdapter_decl.hpp"
00061 
00062 
00063 namespace Amesos2 {
00064 
00065   using Teuchos::RCP;
00066   using Teuchos::ArrayView;
00067 
00068   template <class DerivedMat>
00069   AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::AbstractConcreteMatrixAdapter(RCP<DerivedMat> m)
00070       : MatrixAdapter<DerivedMat>(m)
00071   {
00072     // anything else? probs not
00073   }
00074 
00075   // implementation functions
00076   template <class DerivedMat>
00077   void
00078   AbstractConcreteMatrixAdapter<
00079     Epetra_RowMatrix,
00080     DerivedMat>::getGlobalRowCopy_impl(global_ordinal_t row,
00081                const ArrayView<global_ordinal_t>& indices,
00082                const ArrayView<scalar_t>& vals,
00083                size_t& nnz) const
00084   {
00085     using Teuchos::as;
00086     
00087     local_ordinal_t local_row = this->row_map_->getLocalElement(row);
00088     int nnz_ret = 0;
00089     int rowmatrix_return_val
00090       = this->mat_->ExtractMyRowCopy(as<int>(local_row),
00091              as<int>(std::min(indices.size(), vals.size())),
00092              nnz_ret,
00093              vals.getRawPtr(),
00094              indices.getRawPtr());
00095     TEUCHOS_TEST_FOR_EXCEPTION( rowmatrix_return_val != 0,
00096       std::runtime_error,
00097       "Epetra_RowMatrix object returned error code "
00098       << rowmatrix_return_val << " from ExtractMyRowCopy." );
00099     nnz = as<size_t>(nnz_ret);
00100 
00101     // Epetra_CrsMatrix::ExtractMyRowCopy returns local column
00102     // indices, so transform these into global indices
00103     for( size_t i = 0; i < nnz; ++i ){
00104       indices[i] = this->col_map_->getGlobalElement(indices[i]);
00105     }
00106   }
00107 
00108   template <class DerivedMat>
00109   void
00110   AbstractConcreteMatrixAdapter<
00111     Epetra_RowMatrix,
00112     DerivedMat>::getGlobalColCopy_impl(global_ordinal_t col,
00113                const ArrayView<global_ordinal_t>& indices,
00114                const ArrayView<scalar_t>& vals,
00115                size_t& nnz) const
00116   {
00117     TEUCHOS_TEST_FOR_EXCEPTION( true,
00118       std::runtime_error,
00119       "Column access to row-based object not yet supported.  "
00120       "Please contact the Amesos2 developers." );
00121   }
00122 
00123 
00124   template <class DerivedMat>
00125   typename AbstractConcreteMatrixAdapter<
00126     Epetra_RowMatrix,
00127     DerivedMat>::global_size_t
00128   AbstractConcreteMatrixAdapter<
00129     Epetra_RowMatrix,
00130     DerivedMat>::getGlobalNNZ_impl() const
00131   {
00132     return Teuchos::as<global_size_t>(this->mat_->NumGlobalNonzeros());
00133   }
00134 
00135   template <class DerivedMat>
00136   size_t
00137   AbstractConcreteMatrixAdapter<
00138     Epetra_RowMatrix,
00139     DerivedMat>::getLocalNNZ_impl() const
00140   {
00141     return Teuchos::as<size_t>(this->mat_->NumMyNonzeros());
00142   }
00143 
00144   template <class DerivedMat>
00145   size_t
00146   AbstractConcreteMatrixAdapter<
00147     Epetra_RowMatrix,
00148     DerivedMat>::getMaxRowNNZ_impl() const
00149   {
00150     return Teuchos::as<size_t>(this->mat_->MaxNumEntries());
00151   }
00152 
00153   template <class DerivedMat>
00154   size_t
00155   AbstractConcreteMatrixAdapter<
00156     Epetra_RowMatrix,
00157     DerivedMat>::getMaxColNNZ_impl() const
00158   {
00159     TEUCHOS_TEST_FOR_EXCEPTION( true,
00160       std::runtime_error,
00161       "Column access to row-based object not yet supported.  "
00162       "Please contact the Amesos2 developers." );
00163     return 0;
00164   }
00165 
00166   template <class DerivedMat>
00167   size_t
00168   AbstractConcreteMatrixAdapter<
00169     Epetra_RowMatrix,
00170     DerivedMat>::getGlobalRowNNZ_impl(global_ordinal_t row) const
00171   {
00172     // check whether row is local, then transform to local index
00173     Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
00174     int gid = Teuchos::as<int>(row);
00175     TEUCHOS_TEST_FOR_EXCEPTION( !rowmap.MyGID(gid),
00176       std::invalid_argument,
00177       "The specified global row id does not belong to me" );
00178     int lid = rowmap.LID(gid);
00179     int nnz = 0;
00180     this->mat_->NumMyRowEntries(lid, nnz);
00181     return nnz;
00182   }
00183 
00184   template <class DerivedMat>
00185   size_t
00186   AbstractConcreteMatrixAdapter<
00187     Epetra_RowMatrix,
00188     DerivedMat>::getLocalRowNNZ_impl(local_ordinal_t row) const
00189   {
00190     Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
00191     int lid = Teuchos::as<int>(row);
00192     TEUCHOS_TEST_FOR_EXCEPTION( !rowmap.MyLID(lid),
00193       std::invalid_argument,
00194       "The specified local row id does not beloing to me" );
00195     int num_entries = 0;
00196     this->mat_->NumMyRowEntries(row, num_entries);
00197     return num_entries;
00198   }
00199 
00200   template <class DerivedMat>
00201   size_t
00202   AbstractConcreteMatrixAdapter<
00203     Epetra_RowMatrix,
00204     DerivedMat>::getGlobalColNNZ_impl(global_ordinal_t col) const
00205   {
00206     TEUCHOS_TEST_FOR_EXCEPTION( true,
00207       std::runtime_error,
00208       "Column access to row-based object not yet supported.  "
00209       "Please contact the Amesos2 developers." );
00210     return 0;
00211   }
00212 
00213   template <class DerivedMat>
00214   size_t
00215   AbstractConcreteMatrixAdapter<
00216     Epetra_RowMatrix,
00217     DerivedMat>::getLocalColNNZ_impl(local_ordinal_t col) const
00218   {
00219     TEUCHOS_TEST_FOR_EXCEPTION( true,
00220       std::runtime_error,
00221       "Column access to row-based object not yet supported.  "
00222       "Please contact the Amesos2 developers." );
00223     return 0;
00224   }
00225 
00226   template <class DerivedMat>
00227   const RCP<const Tpetra::Map<MatrixTraits<Epetra_RowMatrix>::local_ordinal_t,
00228             MatrixTraits<Epetra_RowMatrix>::global_ordinal_t,
00229             MatrixTraits<Epetra_RowMatrix>::node_t> >
00230   AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getRowMap_impl() const
00231   {
00232     // Must transform to a Tpetra::Map
00233     const Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
00234     return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(rowmap) );
00235   }
00236 
00237   template <class DerivedMat>
00238   const RCP<const Tpetra::Map<MatrixTraits<Epetra_RowMatrix>::local_ordinal_t,
00239             MatrixTraits<Epetra_RowMatrix>::global_ordinal_t,
00240             MatrixTraits<Epetra_RowMatrix>::node_t> >
00241   AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getColMap_impl() const
00242   {
00243     // Must transform this matrix' Epetra_Map to a Tpetra::Map
00244     const Epetra_Map colmap = this->mat_->RowMatrixColMap();
00245     return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(colmap) );
00246   }
00247 
00248   template <class DerivedMat>
00249   const RCP<const Teuchos::Comm<int> >
00250   AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getComm_impl() const
00251   {
00252     return Util::to_teuchos_comm(Teuchos::rcpFromRef(this->mat_->Comm()));
00253   }
00254 
00255   template <class DerivedMat>
00256   bool
00257   AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::isLocallyIndexed_impl() const
00258   {
00259     return this->mat_->IndicesAreLocal();
00260   }
00261 
00262   template <class DerivedMat>
00263   bool
00264   AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::isGloballyIndexed_impl() const
00265   {
00266     return this->mat_->IndicesAreGlobal();
00267   }
00268 
00269   template <class DerivedMat>
00270   RCP<const MatrixAdapter<DerivedMat> >
00271   AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::get_impl(const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > map) const
00272   {
00273     // Delegate implementation to subclass
00274     return static_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->get_impl(map);
00275   }
00276 
00277 } // end namespace Amesos2
00278 
00279 #endif  // AMESOS2_EPETRAROWMATRIX_ABSTRACTMATRIXADAPTER_DEF_HPP