|
Tpetra Matrix/Vector Services Version of the Day
|
00001 //@HEADER 00002 // ************************************************************************ 00003 // 00004 // Tpetra: Templated Linear Algebra Services Package 00005 // Copyright (2008) 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 #ifndef TPETRA_VBRMATRIX_DECL_HPP 00030 #define TPETRA_VBRMATRIX_DECL_HPP 00031 00032 #include <Kokkos_DefaultNode.hpp> 00033 #include <Kokkos_DefaultKernels.hpp> 00034 #include <Kokkos_VbrMatrix.hpp> 00035 00036 #include "Tpetra_ConfigDefs.hpp" 00037 #include "Tpetra_Operator.hpp" 00038 #include "Tpetra_BlockMap.hpp" 00039 #include "Tpetra_BlockCrsGraph.hpp" 00040 00045 namespace Tpetra { 00046 00048 00076 template <class Scalar, 00077 class LocalOrdinal = int, 00078 class GlobalOrdinal = LocalOrdinal, 00079 class Node = Kokkos::DefaultNode::DefaultNodeType, 00080 class LocalMatOps = typename Kokkos::DefaultKernels<Scalar,LocalOrdinal,Node>::BlockSparseOps > 00081 class VbrMatrix : public Tpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> { 00082 public: 00083 typedef Scalar scalar_type; 00084 typedef LocalOrdinal local_ordinal_type; 00085 typedef GlobalOrdinal global_ordinal_type; 00086 typedef Node node_type; 00087 typedef LocalMatOps mat_vec_type; 00088 00090 00091 00093 00098 VbrMatrix(const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &blkRowMap, size_t maxNumEntriesPerRow, ProfileType pftype = DynamicProfile); 00099 00101 00111 VbrMatrix(const Teuchos::RCP<const BlockCrsGraph<LocalOrdinal,GlobalOrdinal,Node> >& blkGraph); 00112 00114 virtual ~VbrMatrix(); 00115 00117 00119 00121 00126 template <class DomainScalar, class RangeScalar> 00127 void multiply(const MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> & X, MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> &Y, Teuchos::ETransp trans, RangeScalar alpha, RangeScalar beta) const; 00128 00130 00132 00144 template <class DomainScalar, class RangeScalar> 00145 void solve(const MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> & Y, MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> &X, Teuchos::ETransp trans) const; 00146 00148 00150 00151 00153 00155 const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getDomainMap() const; 00156 00158 00160 const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRangeMap() const; 00161 00163 00168 void apply(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X, 00169 MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y, 00170 Teuchos::ETransp trans = Teuchos::NO_TRANS, 00171 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(), 00172 Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const; 00173 00175 00178 void applyInverse(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> & Y, 00179 MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X, 00180 Teuchos::ETransp trans) const; 00181 00183 bool hasTransposeApply() const; 00184 00186 00188 00189 00191 const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > & getBlockRowMap() const; 00192 00194 const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > & getBlockColMap() const; 00195 00197 const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > & getBlockDomainMap() const; 00198 00200 const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > & getBlockRangeMap() const; 00201 00203 const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getPointRowMap() const; 00204 00206 const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getPointColMap() const; 00207 00209 bool isFillComplete() const; 00211 00213 00214 00216 00219 void putScalar(Scalar s); 00220 00222 00231 void setGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, const Teuchos::SerialDenseMatrix<GlobalOrdinal,Scalar>& blockEntry); 00232 00234 00241 void setLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, const Teuchos::SerialDenseMatrix<LocalOrdinal,Scalar>& blockEntry); 00242 00244 00253 void sumIntoGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, const Teuchos::SerialDenseMatrix<GlobalOrdinal,Scalar>& blockEntry); 00254 00256 00263 void sumIntoLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, const Teuchos::SerialDenseMatrix<LocalOrdinal,Scalar>& blockEntry); 00264 00266 00275 void setGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry); 00276 00278 00285 void setLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry); 00286 00288 00297 void sumIntoGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry); 00298 00300 00307 void sumIntoLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry); 00308 00310 00312 00313 00315 00318 void fillComplete(const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockDomainMap, const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockRangeMap); 00319 00321 00324 void fillComplete(); 00326 00328 00329 00331 00339 void getGlobalBlockEntryView(GlobalOrdinal globalBlockRow, 00340 GlobalOrdinal globalBlockCol, 00341 LocalOrdinal& numPtRows, 00342 LocalOrdinal& numPtCols, 00343 Teuchos::ArrayRCP<const Scalar>& blockEntry) const; 00344 00346 00356 void getGlobalBlockEntryViewNonConst(GlobalOrdinal globalBlockRow, 00357 GlobalOrdinal globalBlockCol, 00358 LocalOrdinal& numPtRows, 00359 LocalOrdinal& numPtCols, 00360 Teuchos::ArrayRCP<Scalar>& blockEntry); 00361 00363 00373 void getLocalBlockEntryView(LocalOrdinal localBlockRow, 00374 LocalOrdinal localBlockCol, 00375 LocalOrdinal& numPtRows, 00376 LocalOrdinal& numPtCols, 00377 Teuchos::ArrayRCP<const Scalar>& blockEntry) const; 00378 00380 00396 void getLocalBlockEntryViewNonConst(LocalOrdinal localBlockRow, 00397 LocalOrdinal localBlockCol, 00398 LocalOrdinal& numPtRows, 00399 LocalOrdinal& numPtCols, 00400 Teuchos::ArrayRCP<Scalar>& blockEntry); 00401 00403 00407 void getLocalDiagCopy(Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& diag) const; 00408 00410 00412 00413 std::string description() const; 00414 00417 void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const; 00419 00420 private: 00421 //private methods: 00422 00423 Teuchos::RCP<Node> getNode() const; 00424 00425 void updateImport(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X) const; 00426 void updateExport(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const; 00427 00428 void createImporterExporter(); 00429 void optimizeStorage(); 00430 void fillLocalMatrix(); 00431 void fillLocalMatVec(); 00432 00433 //private data members: 00434 00435 //We hold two graph pointers, one const and the other non-const. 00436 //If a BlockCrsGraph is provided at construction, it is const and VbrMatrix 00437 //never changes it. 00438 //If a BlockCrsGraph is not provided at construction, VbrMatrix creates one 00439 //internally and fills it as the matrix is filled, up until fillComplete() 00440 //is called. 00441 // 00442 //blkGraph_ is either the internally created graph, or is null. 00443 //constBlkGraph_ is either a pointer to blkGraph_, or a pointer to the 00444 //graph provided at construction time. 00445 // 00446 Teuchos::RCP<BlockCrsGraph<LocalOrdinal,GlobalOrdinal,Node> > blkGraph_; 00447 Teuchos::RCP<const BlockCrsGraph<LocalOrdinal,GlobalOrdinal,Node> > constBlkGraph_; 00448 00449 Kokkos::VbrMatrix<Scalar,LocalOrdinal,Node> lclMatrix_; 00450 00451 //It takes 6 arrays to represent a variable-block-row matrix 00452 //in packed (contiguous storage) form. For a description of these 00453 //arrays, see the text at the bottom of this file. 00454 //(2 of those arrays, rptr and cptr, are represented by arrays in the 00455 //getBlockRowMap() and getBlockColMap() objects, and 00456 //another two of those arrays, bptr and bindx, are represented by arrays in the 00457 //BlockCrsGraph object.) 00458 //This is noted in the comments for rptr,cptr,bptr,bindx below. 00459 // 00460 //These arrays are handled as if they may point to memory that resides on 00461 //a separate device (e.g., a GPU). In other words, when the contents of these 00462 //arrays are manipulated, we use views or buffers obtained from the Node object. 00463 Teuchos::ArrayRCP<Scalar> pbuf_values1D_; 00464 Teuchos::ArrayRCP<LocalOrdinal> pbuf_indx_; 00465 00466 LocalMatOps lclMatOps_; 00467 Teuchos::RCP<Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> > importer_; 00468 Teuchos::RCP<Tpetra::Export<LocalOrdinal,GlobalOrdinal,Node> > exporter_; 00469 mutable Teuchos::RCP<Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > importedVec_; 00470 mutable Teuchos::RCP<Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > exportedVec_; 00471 00472 typedef typename std::map<GlobalOrdinal,Teuchos::ArrayRCP<Scalar> > MapGlobalArrayRCP; 00473 typedef typename std::map<LocalOrdinal,Teuchos::ArrayRCP<Scalar> > MapLocalArrayRCP; 00474 00475 //We use 2 arrays (well, array-of-maps, array-of-array-of-arrays...) to 00476 //represent the variable-block-row matrix in un-packed '2D' form. 00477 // 00478 //Note that these arrays are assumed to be resident in CPU (host) memory. 00479 //It doesn't make sense to copy this kind of data back and forth to a separate 00480 //compute device (e.g., a GPU), since we don't support doing matrix-vector 00481 //products until after fillComplete is called, at which time contiguous 00482 //arrays are allocated on the device and matrix data is copied into them. 00483 Teuchos::RCP<Teuchos::Array<MapGlobalArrayRCP> > col_ind_2D_global_; 00484 Teuchos::RCP<Teuchos::Array<Teuchos::Array<Teuchos::ArrayRCP<Scalar> > > > values2D_; 00485 00486 bool is_fill_completed_; 00487 bool is_storage_optimized_; 00488 };//class VbrMatrix 00489 00490 }//namespace Tpetra 00491 00492 //---------------------------------------------------------------------------- 00493 // Description of arrays representing the VBR format: 00494 // 00495 // (For more description, see this URL (valid as of 5/26/2010): 00496 // http://docs.sun.com/source/817-0086-10/prog-sparse-support.html) 00497 // ...and of course more can be found using google... 00498 // The old Aztec manual was a great resource for this but I can't 00499 // find a copy of that these days... 00500 // 00501 // 00502 // Here is a brief description of the 6 arrays that are required to 00503 // represent a VBR matrix in packed (contiguous-memory-storage) format: 00504 // 00505 // rptr: length num_block_rows + 1 00506 // rptr[i]: the pt-row corresponding to the i-th block-row 00507 // Note: rptr is getBlockRowMap()->getNodeFirstPointInBlocks(). 00508 // 00509 // cptr: length num_distinct_block_cols + 1 00510 // cptr[j]: the pt-col corresponding to the j-th block-col 00511 // Note: cptr is getBlockColMap()->getNodeFirstPointInBlocks(). 00512 // 00513 // bptr: length num_block_rows + 1 00514 // bptr[i]: location in bindx of the first nonzero block-entry 00515 // of the i-th block-row 00516 // Note: bptr is blkGraph_->getNodeRowOffsets(); 00517 // 00518 // bindx: length num-nonzero-block-entries 00519 // bindx[j]: block-col-index of j-th block-entry 00520 // Note: bindx is blkGraph_->getNodePackedIndices(); 00521 // 00522 // indx: length num-nonzero-block-entries + 1 00523 // indx[j]: location in vals of the beginning of the j-th 00524 // block-entry 00525 // 00526 // vals: length num-nonzero-scalar-entries 00527 // 00528 // 00529 // Some example look-ups: 00530 // 00531 // int nbr = num_block_rows; 00532 // int total_num_block_nonzeros = bptr[nbr]; 00533 // int total_num_scalar_nonzeros = indx[num_block_nonzeros]; 00534 // 00535 // //get arrays for i-th block-row: 00536 // int* bindx_i = &bindx[bptr[i]]; 00537 // double* vals_i = &val[indx[bptr[i]]]; 00538 // int num_block_nonzeros_in_row_i = bptr[i+1]-bptr[i]; 00539 // 00540 //---------------------------------------------------------------------------- 00541 00542 #endif //TPETRA_VBRMATRIX_DECL_HPP 00543
1.7.4