Tpetra parallel linear algebra  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 
46 #include <Tpetra_CrsMatrix.hpp>
47 #include <Tpetra_Export.hpp>
48 #include <Tpetra_Import.hpp>
49 #include <Teuchos_TimeMonitor.hpp>
50 
51 namespace Tpetra {
52 
53 template<class Scalar,
54  class LocalOrdinal,
55  class GlobalOrdinal,
56  class Node>
58 RowMatrixTransposer (const Teuchos::RCP<const crs_matrix_type>& origMatrix,const std::string & label)
59  : origMatrix_(origMatrix), label_(label) {}
60 
61 template<class Scalar,
62  class LocalOrdinal,
63  class GlobalOrdinal,
64  class Node>
65 Teuchos::RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
67 createTranspose (const Teuchos::RCP<Teuchos::ParameterList> &params)
68 {
69  using Teuchos::RCP;
70  // Do the local transpose
71  RCP<crs_matrix_type> transMatrixWithSharedRows = createTransposeLocal (params);
72 
73 #ifdef HAVE_TPETRA_MMM_TIMINGS
74  std::string prefix = std::string("Tpetra ")+ label_ + std::string(": ");
75  using Teuchos::TimeMonitor;
76  Teuchos::RCP<Teuchos::TimeMonitor> MM = Teuchos::rcp(new TimeMonitor(*TimeMonitor::getNewTimer(prefix + std::string("Transpose TAFC"))));
77 #endif
78 
79  // If transMatrixWithSharedRows has an exporter, that's what we
80  // want. If it doesn't, the rows aren't actually shared, and we're
81  // done!
82  RCP<const Export<LocalOrdinal,GlobalOrdinal,Node> > exporter =
83  transMatrixWithSharedRows->getGraph ()->getExporter ();
84  if (exporter.is_null ()) {
85  return transMatrixWithSharedRows;
86  }
87  else {
88  Teuchos::ParameterList labelList;
89 #ifdef HAVE_TPETRA_MMM_TIMINGS
90  labelList.set("Timer Label",label_);
91 #endif
92  if(!params.is_null()) labelList.set("compute global constants",params->get("compute global constants",true));
93  // Use the Export object to do a fused Export and fillComplete.
94  return exportAndFillCompleteCrsMatrix<crs_matrix_type> (transMatrixWithSharedRows, *exporter,Teuchos::null,Teuchos::null,Teuchos::rcp(&labelList,false));
95  }
96 }
97 
98 
99 // mfh 03 Feb 2013: In a definition outside the class like this, the
100 // return value is considered outside the class scope (for things like
101 // resolving typedefs), but the arguments are considered inside the
102 // class scope.
103 template<class Scalar,
104  class LocalOrdinal,
105  class GlobalOrdinal,
106  class Node>
107 Teuchos::RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
109 createTransposeLocal (const Teuchos::RCP<Teuchos::ParameterList> &params)
110 {
111  using Teuchos::Array;
112  using Teuchos::ArrayRCP;
113  using Teuchos::ArrayView;
114  using Teuchos::RCP;
115  using Teuchos::rcp;
116  using Teuchos::rcp_dynamic_cast;
117  typedef LocalOrdinal LO;
118  typedef GlobalOrdinal GO;
119  typedef Tpetra::Import<LO, GO, Node> import_type;
120  typedef Tpetra::Export<LO, GO, Node> export_type;
121 
122 #ifdef HAVE_TPETRA_MMM_TIMINGS
123  std::string prefix = std::string("Tpetra ")+ label_ + std::string(": ");
124  using Teuchos::TimeMonitor;
125  Teuchos::RCP<Teuchos::TimeMonitor> MM = Teuchos::rcp(new TimeMonitor(*TimeMonitor::getNewTimer(prefix + std::string("Transpose Local"))));
126 #endif
127 
128  //
129  // This transpose is based upon the approach in EpetraExt.
130  //
131  size_t numLocalCols = origMatrix_->getNodeNumCols();
132  size_t numLocalRows = origMatrix_->getNodeNumRows();
133  size_t numLocalNnz = origMatrix_->getNodeNumEntries();
134 
135  // Determine how many nonzeros there are per row in the transpose.
136  Array<size_t> CurrentStart(numLocalCols,0);
137  ArrayView<const LO> localIndices;
138  ArrayView<const Scalar> localValues;
139  RCP<const crs_matrix_type> crsMatrix =
140  rcp_dynamic_cast<const crs_matrix_type> (origMatrix_);
141  if (crsMatrix == Teuchos::null) {
142  for (size_t i=0; i<numLocalRows; ++i) {
143  const size_t numEntriesInRow = origMatrix_->getNumEntriesInLocalRow(i);
144  origMatrix_->getLocalRowView(i, localIndices, localValues);
145  for (size_t j=0; j<numEntriesInRow; ++j) {
146  ++CurrentStart[ localIndices[j] ];
147  }
148  }
149  } else {
150  ArrayRCP<const size_t> origRowPtr_rcp;
151  ArrayRCP<const LO> origColInd_rcp;
152  ArrayRCP<const Scalar> origValues_rcp;
153  crsMatrix->getAllValues(origRowPtr_rcp, origColInd_rcp, origValues_rcp);
154  ArrayView<const LO> origColInd = origColInd_rcp();
155  for (LO j=0; j<origColInd.size(); ++j) {
156  ++CurrentStart[ origColInd[j] ];
157  }
158  }
159 
160  // create temporary row-major storage for the transposed matrix
161 
162  ArrayRCP<size_t> rowptr_rcp(numLocalCols+1);
163  ArrayRCP<LO> colind_rcp(numLocalNnz);
164  ArrayRCP<Scalar> values_rcp(numLocalNnz);
165 
166  // Since ArrayRCP's are slow...
167  ArrayView<size_t> TransRowptr = rowptr_rcp();
168  ArrayView<LO> TransColind = colind_rcp();
169  ArrayView<Scalar> TransValues = values_rcp();
170 
171  // Scansum the TransRowptr; reset CurrentStart
172  TransRowptr[0]=0;
173  for (size_t i=1; i<numLocalCols+1; ++i) TransRowptr[i] = CurrentStart[i-1] + TransRowptr[i-1];
174  for (size_t i=0; i<numLocalCols; ++i) CurrentStart[i] = TransRowptr[i];
175 
176  // populate the row-major storage so that the data for the transposed
177  // matrix is easy to access
178  if (crsMatrix == Teuchos::null) {
179  for (size_t i=0; i<numLocalRows; ++i) {
180  const size_t numEntriesInRow = origMatrix_->getNumEntriesInLocalRow (i);
181  origMatrix_->getLocalRowView(i, localIndices, localValues);
182 
183  for (size_t j=0; j<numEntriesInRow; ++j) {
184  size_t idx = CurrentStart[localIndices[j]];
185  TransColind[idx] = Teuchos::as<LO>(i);
186  TransValues[idx] = localValues[j];
187  ++CurrentStart[localIndices[j]];
188  }
189  } //for (size_t i=0; i<numLocalRows; ++i)
190  } else {
191  ArrayRCP<const size_t> origRowPtr_rcp;
192  ArrayRCP<const LO> origColInd_rcp;
193  ArrayRCP<const Scalar> origValues_rcp;
194  crsMatrix->getAllValues(origRowPtr_rcp, origColInd_rcp, origValues_rcp);
195  ArrayView<const size_t> origRowPtr = origRowPtr_rcp();
196  ArrayView<const LO> origColInd = origColInd_rcp();
197  ArrayView<const Scalar> origValues = origValues_rcp();
198  size_t k=0;
199  for (LO i=0; i<origRowPtr.size()-1; ++i) {
200  const LO rowIndex = Teuchos::as<LO>(i);
201  size_t rowStart = origRowPtr[i], rowEnd = origRowPtr[i+1];
202  for (size_t j = rowStart; j < rowEnd; ++j) {
203  size_t idx = CurrentStart[origColInd[k]];
204  TransColind[idx] = rowIndex;
205  TransValues[idx] = origValues[k];
206  ++CurrentStart[origColInd[k++]];
207  }
208  }
209  }
210 
211  //Allocate and populate temporary matrix with rows not uniquely owned
212  RCP<crs_matrix_type> transMatrixWithSharedRows =
213  rcp (new crs_matrix_type (origMatrix_->getColMap (),
214  origMatrix_->getRowMap (), 0));
215  transMatrixWithSharedRows->setAllValues (rowptr_rcp, colind_rcp, values_rcp);
216 
217  // Prebuild the importers and exporters the no-communication way,
218  // flipping the importers and exporters around.
219  RCP<const import_type> myImport;
220  RCP<const export_type> myExport;
221  if (! origMatrix_->getGraph ()->getImporter ().is_null ()) {
222  myExport = rcp (new export_type (*origMatrix_->getGraph ()->getImporter ()));
223  }
224  if (! origMatrix_->getGraph ()->getExporter ().is_null ()) {
225  myImport = rcp (new import_type (*origMatrix_->getGraph ()->getExporter ()));
226  }
227 
228  // Call ESFC & return
229  Teuchos::ParameterList eParams;
230 #ifdef HAVE_TPETRA_MMM_TIMINGS
231  eParams.set("Timer Label",label_);
232 #endif
233  if(!params.is_null()) eParams.set("compute global constants",params->get("compute global constants",true));
234 
235  transMatrixWithSharedRows->expertStaticFillComplete (origMatrix_->getRangeMap (),
236  origMatrix_->getDomainMap (),
237  myImport, myExport,rcp(&eParams,false));
238  return transMatrixWithSharedRows;
239 }
240 //
241 // Explicit instantiation macro
242 //
243 // Must be expanded from within the Tpetra namespace!
244 //
245 
246 #define TPETRA_ROWMATRIXTRANSPOSER_INSTANT(SCALAR,LO,GO,NODE) \
247  \
248  template class RowMatrixTransposer< SCALAR, LO , GO , NODE >;
249 
250 
251 }
252 
253 #endif
RowMatrixTransposer(const Teuchos::RCP< const crs_matrix_type > &origMatrix, const std::string &label=std::string())
Constructor that takes the matrix to transpose.
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.
Sparse matrix that presents a row-oriented interface that lets users read or modify entries...
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
Teuchos::RCP< crs_matrix_type > createTransposeLocal(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Compute and return the transpose of the matrix given to the constructor.
Teuchos::RCP< crs_matrix_type > createTranspose(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Compute and return the transpose of the matrix given to the constructor.