Tpetra Matrix/Vector Services  Version of the Day
Tpetra_RowMatrixTransposer_def.hpp
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_ROWMATRIXTRANSPOSER_DEF_HPP
43 #define TPETRA_ROWMATRIXTRANSPOSER_DEF_HPP
44 
45 #include "Tpetra_Export.hpp"
46 #include "Tpetra_Import.hpp"
47 #include "Tpetra_Map.hpp"
48 #include "Teuchos_DefaultSerialComm.hpp"
49 #ifdef DOXYGEN_USE_ONLY
50  // #include "Tpetra_RowMatrixtransposer_decl.hpp"
51 #endif
52 
53 namespace Tpetra {
54 
55 template<class Scalar,
56  class LocalOrdinal,
57  class GlobalOrdinal,
58  class Node,
59  class SpMatOps>
62  : origMatrix_ (origMatrix) {}
63 
64 template<class Scalar,
65  class LocalOrdinal,
66  class GlobalOrdinal,
67  class Node,
68  class SpMatOps>
69 TEUCHOS_DEPRECATED
72  : origMatrix_ (Teuchos::rcpFromRef (origMatrix)) {}
73 
74 // mfh 03 Feb 2013: In a definition outside the class like this, the
75 // return value is considered outside the class scope (for things like
76 // resolving typedefs), but the arguments are considered inside the
77 // class scope.
78 template<class Scalar,
79  class LocalOrdinal,
80  class GlobalOrdinal,
81  class Node,
82  class SpMatOps>
86 {
87  using Teuchos::Array;
88  using Teuchos::ArrayView;
90  using Teuchos::parameterList;
91  using Teuchos::RCP;
92  using Teuchos::rcp;
93  using Tpetra::Import;
94  using Tpetra::Export;
95  typedef LocalOrdinal LO;
96 
97  //
98  // This transpose is based upon the approach in EpetraExt.
99  //
100  size_t numLocalCols = origMatrix_->getNodeNumCols();
101  size_t numLocalRows = origMatrix_->getNodeNumRows();
102  size_t numLocalNnz = origMatrix_->getNodeNumEntries();
103 
104  ArrayRCP<size_t> rowptr_rcp(numLocalCols+1);
105  ArrayRCP<LO> colind_rcp(numLocalNnz);
106  ArrayRCP<Scalar> values_rcp(numLocalNnz);
107 
108  // Since ArrayRCP's are slow...
109  ArrayView<size_t> TransRowptr = rowptr_rcp();
110  ArrayView<LO> TransColind = colind_rcp();
111  ArrayView<Scalar> TransValues = values_rcp();
112 
113  // Determine how many nonzeros there are per row in the transpose.
114  Array<size_t> CurrentStart(numLocalCols,0);
115  ArrayView<const LO> localIndices;
116  ArrayView<const Scalar> localValues;
117  for (size_t i=0; i<numLocalRows; ++i) {
118  const size_t numEntriesInRow = origMatrix_->getNumEntriesInLocalRow(i);
119  origMatrix_->getLocalRowView(i, localIndices, localValues);
120  for (size_t j=0; j<numEntriesInRow; ++j) {
121  ++CurrentStart[ localIndices[j] ];
122  }
123  }
124 
125  //create temporary row-major storage for the transposed matrix
126 
127  // Scansum the TransRowptr; reset CurrentStart
128  TransRowptr[0]=0;
129  for (size_t i=1; i<numLocalCols+1; ++i) TransRowptr[i] = CurrentStart[i-1] + TransRowptr[i-1];
130  for (size_t i=0; i<numLocalCols; ++i) CurrentStart[i] = TransRowptr[i];
131 
132  //populate the row-major storage so that the data for the transposed matrix is easy to access
133  for (size_t i=0; i<numLocalRows; ++i) {
134  const size_t numEntriesInRow = origMatrix_->getNumEntriesInLocalRow(i);
135  origMatrix_->getLocalRowView(i, localIndices, localValues);
136 
137  for (size_t j=0; j<numEntriesInRow; ++j) {
138  size_t idx = CurrentStart[localIndices[j]];
139  TransColind[idx] = Teuchos::as<LO>(i);
140  TransValues[idx] = localValues[j];
141  ++CurrentStart[localIndices[j]];
142  }
143  } //for (size_t i=0; i<numLocalRows; ++i)
144 
145 
146  //Allocate and populate temporary matrix with rows not uniquely owned
147  RCP<crs_matrix_type> transMatrixWithSharedRows(new crs_matrix_type(origMatrix_->getColMap(),origMatrix_->getRowMap(),0));
148  transMatrixWithSharedRows->setAllValues(rowptr_rcp,colind_rcp,values_rcp);
149 
150 
151  // Prebuild the importers and exporters the no-communication way, flipping the importers
152  // and exporters around.
155  if(!origMatrix_->getGraph()->getImporter().is_null())
156  myExport = rcp(new Export<LocalOrdinal,GlobalOrdinal,Node>(*origMatrix_->getGraph()->getImporter()));
157  if(!origMatrix_->getGraph()->getExporter().is_null())
158  myImport = rcp(new Import<LocalOrdinal,GlobalOrdinal,Node>(*origMatrix_->getGraph()->getExporter()));
159 
160  // Call ESFC
161  transMatrixWithSharedRows->expertStaticFillComplete(origMatrix_->getRangeMap(),origMatrix_->getDomainMap(),myImport,myExport);
162 
163  // If transMatrixWithSharedRows has an exporter, that's what we want. If it doesn't, the rows aren't actually shared,
164  // and we're done!
165  RCP<const Export<LocalOrdinal,GlobalOrdinal,Node> > exporter = transMatrixWithSharedRows->getGraph()->getExporter();
166  if(exporter == Teuchos::null) {
167  return transMatrixWithSharedRows;
168  }
169 
170  // Finish using fusedexport
171  return exportAndFillCompleteCrsMatrix<crs_matrix_type>(transMatrixWithSharedRows,*exporter);
172 
173 }
174 //
175 // Explicit instantiation macro
176 //
177 // Must be expanded from within the Tpetra namespace!
178 //
179 
180 #define TPETRA_ROWMATRIXTRANSPOSE_INSTANT(SCALAR,LO,GO,NODE) \
181  \
182  template class RowMatrixTransposer< SCALAR, LO , GO , NODE >;
183 
184 
185 }
186 
187 #endif
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Teuchos::RCP< crs_matrix_type > createTranspose()
RowMatrixTransposer(const Teuchos::RCP< const crs_matrix_type > &origMatrix)
Constructor that takes the matrix to transpose.
void setAllValues(const ArrayRCP< size_t > &rowPointers, const ArrayRCP< LocalOrdinal > &columnIndices, const ArrayRCP< Scalar > &values)
Sets the 1D pointer arrays of the graph.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void expertStaticFillComplete(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &domainMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rangeMap, const RCP< const Import< LocalOrdinal, GlobalOrdinal, Node > > &importer=Teuchos::null, const RCP< const Export< LocalOrdinal, GlobalOrdinal, Node > > &exporter=Teuchos::null, const RCP< ParameterList > &params=Teuchos::null)
Perform a fillComplete on a matrix that already has data.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
RCP< const RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.