Tpetra Matrix/Vector Services  Version of the Day
Tpetra_VbrMatrix_decl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 #ifndef TPETRA_VBRMATRIX_DECL_HPP
43 #define TPETRA_VBRMATRIX_DECL_HPP
44 
47 
48 #include <Tpetra_ConfigDefs.hpp>
49 #include <Tpetra_Operator.hpp>
50 #include <Tpetra_VbrUtils.hpp>
51 #include <Kokkos_DefaultNode.hpp>
52 #include <Kokkos_DefaultKernels.hpp>
53 
54 // Forward declarations, to avoid including files that we don't really
55 // need just for declaring VbrMatrix. The #ifndef ... #endif prevents
56 // Doxygen from generating spurious documentation for the forward
57 // declarations.
58 #ifndef DOXYGEN_SHOULD_SKIP_THIS
59 namespace Tpetra {
60  template<class LocalOrdinal, class GlobalOrdinal, class Node>
61  class BlockMap;
62 
63  template<class LocalOrdinal, class GlobalOrdinal, class Node>
64  class BlockCrsGraph;
65 
66  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
67  class Vector;
68 } // namespace Tpetra
69 
70 namespace Kokkos {
71  template<class Scalar, class LocalOrdinal, class Node>
72  class VbrMatrix;
73 } // namespace Kokkos
74 
75 namespace Teuchos {
76  template<class OrdinalType, class ScalarType>
77  class SerialDenseMatrix;
78 } // namespace Teuchos
79 #endif // DOXYGEN_SHOULD_SKIP_THIS
80 
81 namespace Tpetra {
82 
84 
112 template <class Scalar,
113  class LocalOrdinal = int,
114  class GlobalOrdinal = LocalOrdinal,
115  class Node = Kokkos::DefaultNode::DefaultNodeType,
116  class LocalMatOps = typename Kokkos::DefaultKernels<Scalar,LocalOrdinal,Node>::BlockSparseOps >
117 class VbrMatrix :
118  public Tpetra::DistObject<char, LocalOrdinal, GlobalOrdinal, Node>,
119  public Tpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> {
120 public:
121  typedef Scalar scalar_type;
122  typedef LocalOrdinal local_ordinal_type;
123  typedef GlobalOrdinal global_ordinal_type;
124  typedef Node node_type;
125  typedef LocalMatOps mat_vec_type;
126 
128 
129 
131 
136  VbrMatrix(const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &blkRowMap, size_t maxNumEntriesPerRow, ProfileType pftype = DynamicProfile);
137 
139 
150 
152  virtual ~VbrMatrix();
153 
155 
157 
158 
160 
165  template <class DomainScalar, class RangeScalar>
166  void multiply(const MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> & X, MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> &Y, Teuchos::ETransp trans, RangeScalar alpha, RangeScalar beta) const;
167 
169 
181  template <class DomainScalar, class RangeScalar>
183 
185 
187 
188 
190 
192  const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getDomainMap() const;
193 
195 
197  const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRangeMap() const;
198 
200 
208  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
209  Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const;
210 
212 
215  void applyInverse(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> & Y,
217  Teuchos::ETransp trans) const;
218 
220 
223  bool hasTransposeApply() const;
224 
226 
228 
229 
231  const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > & getBlockRowMap() const;
232 
234  const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > & getBlockColMap() const;
235 
237  const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > & getBlockDomainMap() const;
238 
240  const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > & getBlockRangeMap() const;
241 
243  const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getPointRowMap() const;
244 
246  const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getPointColMap() const;
247 
249  bool isFillComplete() const;
251 
253 
254 
256 
259  void putScalar(Scalar s);
260 
262 
277  void setGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, const Teuchos::SerialDenseMatrix<int,Scalar>& blockEntry);
278 
280 
287  void setLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, const Teuchos::SerialDenseMatrix<int,Scalar>& blockEntry);
288 
290 
305  void sumIntoGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, const Teuchos::SerialDenseMatrix<int,Scalar>& blockEntry);
306 
308 
315  void sumIntoLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, const Teuchos::SerialDenseMatrix<int,Scalar>& blockEntry);
316 
318 
333  void setGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry);
334 
336 
343  void setLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry);
344 
346 
361  void sumIntoGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry);
362 
364 
371  void sumIntoLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry);
372 
374 
376 
377 
379 
383  void fillComplete(const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockDomainMap, const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockRangeMap);
384 
386 
389  void fillComplete();
390 
392  void globalAssemble();
394 
396 
397 
399 
401  void getGlobalBlockRowView(GlobalOrdinal globalBlockRow,
402  LocalOrdinal& numPtRows,
404  Teuchos::Array<LocalOrdinal>& ptColsPerBlockCol,
405  Teuchos::Array<Teuchos::ArrayRCP<const Scalar> >& blockEntries) const;
406 
408 
410  void getLocalBlockRowView(LocalOrdinal localBlockRow,
411  LocalOrdinal& numPtRows,
413  Teuchos::Array<LocalOrdinal>& ptColsPerBlockCol,
414  Teuchos::ArrayRCP<const Scalar>& blockEntries) const;
415 
417 
425  void getGlobalBlockEntryView(GlobalOrdinal globalBlockRow,
426  GlobalOrdinal globalBlockCol,
427  LocalOrdinal& numPtRows,
428  LocalOrdinal& numPtCols,
429  Teuchos::ArrayRCP<const Scalar>& blockEntry) const;
430 
432 
442  void getGlobalBlockEntryViewNonConst(GlobalOrdinal globalBlockRow,
443  GlobalOrdinal globalBlockCol,
444  LocalOrdinal& numPtRows,
445  LocalOrdinal& numPtCols,
446  Teuchos::ArrayRCP<Scalar>& blockEntry);
447 
449 
459  void getLocalBlockEntryView(LocalOrdinal localBlockRow,
460  LocalOrdinal localBlockCol,
461  LocalOrdinal& numPtRows,
462  LocalOrdinal& numPtCols,
463  Teuchos::ArrayRCP<const Scalar>& blockEntry) const;
464 
466 
482  void getLocalBlockEntryViewNonConst(LocalOrdinal localBlockRow,
483  LocalOrdinal localBlockCol,
484  LocalOrdinal& numPtRows,
485  LocalOrdinal& numPtCols,
486  Teuchos::ArrayRCP<Scalar>& blockEntry);
487 
489 
493  void getLocalDiagCopy (Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& diag) const;
494 
496  getBlockCrsGraph () {
497  return constBlkGraph_;
498  }
499 
501 
503 
504  virtual bool checkSizes (const SrcDistObject& source);
505 
506  virtual void
507  copyAndPermute (const SrcDistObject& source,
508  size_t numSameIDs,
509  const Teuchos::ArrayView<const LocalOrdinal>& permuteToLIDs,
510  const Teuchos::ArrayView<const LocalOrdinal>& permuteFromLIDs);
511 
512  virtual void
513  packAndPrepare (const SrcDistObject& source,
514  const Teuchos::ArrayView<const LocalOrdinal>& exportLIDs,
515  Teuchos::Array<char>& exports,
516  const Teuchos::ArrayView<size_t>& numPacketsPerLID,
517  size_t& constantNumPackets,
518  Distributor& distor);
519 
520  virtual void
521  unpackAndCombine (const Teuchos::ArrayView<const LocalOrdinal>& importLIDs,
522  const Teuchos::ArrayView<const char>& imports,
523  const Teuchos::ArrayView<size_t>& numPacketsPerLID,
524  size_t constantNumPackets,
525  Distributor& distor,
526  CombineMode CM);
527 
529 
531 
532  std::string description() const;
533 
537 
538  private:
539  Teuchos::RCP<Node> getNode() const;
540 
541  void updateImport(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X) const;
542  void updateExport(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const;
543 
544  void createImporterExporter();
545  void optimizeStorage();
546  void fillLocalMatrix();
547  void fillLocalMatVec();
548 
549  //private data members:
550 
551  //We hold two graph pointers, one const and the other non-const.
552  //If a BlockCrsGraph is provided at construction, it is const and VbrMatrix
553  //never changes it.
554  //If a BlockCrsGraph is not provided at construction, VbrMatrix creates one
555  //internally and fills it as the matrix is filled, up until fillComplete()
556  //is called.
557  //
558  //blkGraph_ is either the internally created graph, or is null.
559  //constBlkGraph_ is either a pointer to blkGraph_, or a pointer to the
560  //graph provided at construction time.
561  //
564 
565  // We use a pointer so that the _decl header file only needs a
566  // forward declaration, not an include. We don't really need an RCP
567  // here, since the local matrix object never gets shared outside the
568  // class. std::unique_ptr (C++11) would be more appropriate.
570 
571  //A variable-block-row matrix is represented by 6 arrays
572  //in packed (contiguous storage) form. For a description of these
573  //arrays, see the text at the bottom of this file.
574  //(2 of those arrays, rptr and cptr, are represented by arrays in the
575  //getBlockRowMap() and getBlockColMap() objects, and
576  //another two of those arrays, bptr and bindx, are represented by arrays in
577  //the BlockCrsGraph object.)
578  //This is noted in the comments for rptr,cptr,bptr,bindx below.
579  //
580  //These arrays are handled as if they may point to memory that resides on
581  //a separate device (e.g., a GPU). In other words, when the contents of these
582  //arrays are manipulated, we use views or buffers obtained from the Node
583  //object.
584  Teuchos::ArrayRCP<Scalar> pbuf_values1D_;
586 
587  LocalMatOps lclMatOps_;
592 
593  typedef typename std::map<GlobalOrdinal,Teuchos::ArrayRCP<Scalar> > RowGlobalCols;
594 
595  //We use an array-of-maps to represent the variable-block-row matrix in
596  //un-packed '2D' form.
597  //
598  //This unpacked data is assumed to be resident in CPU (host) memory.
599  //It doesn't make sense to copy this data back and forth to a separate
600  //compute device (e.g., a GPU), since we don't support doing matrix-vector
601  //products until after fillComplete is called, at which time contiguous
602  //arrays are allocated on the device and matrix data is copied into them.
604 
605  VbrUtils::VbrData<LocalOrdinal,GlobalOrdinal,Scalar> nonlocal_data_;
606 
607  bool is_fill_completed_;
608  bool is_storage_optimized_;
609 };//class VbrMatrix
610 
611 }//namespace Tpetra
612 
613 //----------------------------------------------------------------------------
614 // Description of arrays representing the VBR format:
615 //
616 // (For more description, see this URL (valid as of 5/26/2010):
617 // http://docs.sun.com/source/817-0086-10/prog-sparse-support.html)
618 // ...and of course more can be found using google...
619 // The old Aztec manual was a great resource for this but I can't
620 // find a copy of that these days...
621 //
622 //
623 // Here is a brief description of the 6 arrays that are required to
624 // represent a VBR matrix in packed (contiguous-memory-storage) format:
625 //
626 // rptr: length num_block_rows + 1
627 // rptr[i]: the pt-row corresponding to the i-th block-row
628 // Note: rptr is getBlockRowMap()->getNodeFirstPointInBlocks().
629 //
630 // cptr: length num_distinct_block_cols + 1
631 // cptr[j]: the pt-col corresponding to the j-th block-col
632 // Note: cptr is getBlockColMap()->getNodeFirstPointInBlocks().
633 //
634 // bptr: length num_block_rows + 1
635 // bptr[i]: location in bindx of the first nonzero block-entry
636 // of the i-th block-row
637 // Note: bptr is blkGraph_->getNodeRowOffsets();
638 //
639 // bindx: length num-nonzero-block-entries
640 // bindx[j]: block-col-index of j-th block-entry
641 // Note: bindx is blkGraph_->getNodePackedIndices();
642 //
643 // indx: length num-nonzero-block-entries + 1
644 // indx[j]: location in vals of the beginning of the j-th
645 // block-entry
646 //
647 // vals: length num-nonzero-scalar-entries
648 //
649 //
650 // Some example look-ups:
651 //
652 // int nbr = num_block_rows;
653 // int total_num_block_nonzeros = bptr[nbr];
654 // int total_num_scalar_nonzeros = indx[num_block_nonzeros];
655 //
656 // //get arrays for i-th block-row:
657 // int* bindx_i = &bindx[bptr[i]];
658 // double* vals_i = &val[indx[bptr[i]]];
659 // int num_block_nonzeros_in_row_i = bptr[i+1]-bptr[i];
660 //
661 //----------------------------------------------------------------------------
662 
663 #endif //TPETRA_VBRMATRIX_DECL_HPP
664 
Block-entry counterpart to Tpetra::Map.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
GlobalOrdinal global_ordinal_type
The global index type.
Block-entry counterpart to Tpetra::CrsGraph.
Scalar scalar_type
The type of the entries of the input and output multivectors.
One or more distributed dense vectors.
VbrMatrix: Variable block row matrix.
Abstract interface for linear operators accepting Tpetra MultiVector objects.
LocalOrdinal local_ordinal_type
The local index type.
Sets up and executes a communication plan for a Tpetra DistObject.
CombineMode
Tpetra::Combine Mode enumerable type.
Abstract base class for objects that can be the source of an Import or Export operation.
Node node_type
The Kokkos Node type.
static const EVerbosityLevel verbLevel_default
A distributed dense vector.
Base class for distributed Tpetra objects that support data redistribution.