Zoltan2  Version of the Day
Zoltan2_XpetraCrsMatrixInput.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef _ZOLTAN2_XPETRACRSMATRIXINPUT_HPP_
51 #define _ZOLTAN2_XPETRACRSMATRIXINPUT_HPP_
52 
53 #include <Zoltan2_MatrixInput.hpp>
54 #include <Zoltan2_StridedData.hpp>
55 #include <Zoltan2_XpetraTraits.hpp>
57 
58 #include <Xpetra_CrsMatrix.hpp>
59 
60 namespace Zoltan2 {
61 
63 
85 template <typename User>
86  class XpetraCrsMatrixInput : public MatrixInput<User> {
87 public:
88 
89 #ifndef DOXYGEN_SHOULD_SKIP_THIS
90  typedef typename InputTraits<User>::scalar_t scalar_t;
91  typedef typename InputTraits<User>::lno_t lno_t;
92  typedef typename InputTraits<User>::gno_t gno_t;
93  typedef typename InputTraits<User>::gid_t gid_t;
94  typedef typename InputTraits<User>::node_t node_t;
95  typedef Xpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> xmatrix_t;
96  typedef MatrixInput<User> base_adapter_t;
97  typedef User user_t;
98 #endif
99 
103 
113  XpetraCrsMatrixInput(const RCP<const User> &inmatrix, int coordDim=0,
114  int weightDim=0);
115 
130  void setRowCoordinates(int dim, const scalar_t *coordVal, int stride);
131 
147  void setRowWeights(int dim, const scalar_t *weightVal, int stride);
148 
154  void setRowWeightIsNumberOfNonZeros(int dim);
155 
159  const RCP<const xmatrix_t> &getMatrix() const
160  {
161  return matrix_;
162  }
163 
165  // The InputAdapter interface.
167 
168  string inputAdapterName()const { return string("XpetraCrsMatrix");}
169 
170  size_t getLocalNumberOfObjects() const { return getLocalNumRows();}
171 
172  int getNumberOfWeightsPerObject() const { return 0;}
173 
174  size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
175  {
176  return getRowWeights(dim, wgt, stride);
177  }
178 
180  // The MatrixInput interface.
182 
183  size_t getLocalNumRows() const {
184  return matrix_->getNodeNumRows();
185  }
186 
188  return matrix_->getGlobalNumRows();
189  }
190 
191  size_t getLocalNumColumns() const {
192  return matrix_->getNodeNumCols();
193  }
194 
196  return matrix_->getGlobalNumCols();
197  }
198 
199  size_t getRowListView(const gid_t *&rowIds,
200  const lno_t *&offsets, const gid_t *& colIds) const
201  {
202  size_t nrows = getLocalNumRows();
203 
204  ArrayView<const gid_t> rowView = rowMap_->getNodeElementList();
205  rowIds = rowView.getRawPtr();
206  offsets = offset_.getRawPtr();
207  colIds = columnIds_.getRawPtr();
208  return nrows;
209  }
210 
212  {
213  return weightDim_;
214  }
215 
216  size_t getRowWeights(int dim,
217  const scalar_t *&weights, int &stride) const
218  {
219  env_->localInputAssertion(__FILE__, __LINE__,
220  "invalid weight dimension",
221  dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
222 
223  size_t length;
224  rowWeights_[dim].getStridedList(length, weights, stride);
225  return length;
226  }
227 
228  bool getRowWeightIsNumberOfNonZeros(int dim) const { return numNzWeight_[dim];}
229 
230  int getCoordinateDimension() const {return coordinateDim_;}
231 
232  size_t getRowCoordinates(int dim,
233  const scalar_t *&coords, int &stride) const
234  {
235  env_->localInputAssertion(__FILE__, __LINE__,
236  "invalid coordinate dimension",
237  dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
238 
239  size_t length;
240  rowCoords_[dim].getStridedList(length, coords, stride);
241  return length;
242  }
243 
245  // End of MatrixInput interface.
247 
248  template <typename Adapter>
249  size_t applyPartitioningSolution(const User &in, User *&out,
250  const PartitioningSolution<Adapter> &solution) const;
251 
252 private:
253 
254  RCP<Environment> env_; // for error messages, etc.
255 
256  RCP<const User> inmatrix_;
257  RCP<const xmatrix_t> matrix_;
258  RCP<const Xpetra::Map<lno_t, gno_t, node_t> > rowMap_;
259  RCP<const Xpetra::Map<lno_t, gno_t, node_t> > colMap_;
260  lno_t base_;
261  ArrayRCP<lno_t> offset_;
262  ArrayRCP<gno_t> columnIds_;
263 
264  int coordinateDim_;
265  ArrayRCP<StridedData<lno_t, scalar_t> > rowCoords_;
266 
267  int weightDim_;
268  ArrayRCP<StridedData<lno_t, scalar_t> > rowWeights_;
269  ArrayRCP<bool> numNzWeight_;
270 
271  bool mayHaveDiagonalEntries;
272 };
273 
275 // Definitions
277 
278 template <typename User>
280  const RCP<const User> &inmatrix, int coordDim, int weightDim):
281  env_(rcp(new Environment)),
282  inmatrix_(inmatrix), matrix_(), rowMap_(), colMap_(), base_(),
283  offset_(), columnIds_(),
284  coordinateDim_(coordDim), rowCoords_(),
285  weightDim_(weightDim), rowWeights_(), numNzWeight_(),
286  mayHaveDiagonalEntries(true)
287 {
289  matrix_ = XpetraTraits<User>::convertToXpetra(inmatrix);
290  rowMap_ = matrix_->getRowMap();
291  colMap_ = matrix_->getColMap();
292  base_ = rowMap_->getIndexBase();
293 
294  size_t nrows = matrix_->getNodeNumRows();
295  size_t nnz = matrix_->getNodeNumEntries();
296 
297  offset_.resize(nrows+1, 0);
298  columnIds_.resize(nnz);
299  ArrayView<const lno_t> indices;
300  ArrayView<const scalar_t> nzs;
301  lno_t next = 0;
302  for (size_t i=0; i < nrows; i++){
303  lno_t row = i + base_;
304  nnz = matrix_->getNumEntriesInLocalRow(row);
305  matrix_->getLocalRowView(row, indices, nzs);
306  for (size_t j=0; j < nnz; j++){
307  // TODO - this will be slow
308  // Is it possible that global columns ids might be stored in order?
309  columnIds_[next++] = colMap_->getGlobalElement(indices[j]);
310  }
311  offset_[i+1] = offset_[i] + nnz;
312  }
313 
314  if (coordinateDim_ > 0)
315  rowCoords_ = arcp(new input_t [coordinateDim_], 0, coordinateDim_, true);
316 
317  if (weightDim_ > 0){
318  rowWeights_ = arcp(new input_t [weightDim_], 0, weightDim_, true);
319  numNzWeight_ = arcp(new bool [weightDim_], 0, weightDim_, true);
320  for (int i=0; i < weightDim_; i++)
321  numNzWeight_[i] = false;
322  }
323 }
324 
325 // TODO (from 3/21/12 mtg): Consider changing interface to take an XpetraMultivector
326 template <typename User>
328  const scalar_t *coordVal, int stride)
329 {
331 
332  env_->localInputAssertion(__FILE__, __LINE__,
333  "invalid row coordinate dimension",
334  dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
335 
336  size_t nvtx = getLocalNumRows();
337 
338  ArrayRCP<const scalar_t> coordV(coordVal, 0, nvtx, false);
339  rowCoords_[dim] = input_t(coordV, stride);
340 }
341 
342 template <typename User>
344  const scalar_t *weightVal, int stride)
345 {
347 
348  env_->localInputAssertion(__FILE__, __LINE__,
349  "invalid row weight dimension",
350  dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
351 
352  size_t nvtx = getLocalNumRows();
353 
354  ArrayRCP<const scalar_t> weightV(weightVal, 0, nvtx, false);
355  rowWeights_[dim] = input_t(weightV, stride);
356 }
357 
358 template <typename User>
360 {
361  env_->localInputAssertion(__FILE__, __LINE__,
362  "invalid row weight dimension",
363  dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
364 
365  numNzWeight_[dim] = true;
366 }
367 
368 template <typename User>
369  template <typename Adapter>
371  const User &in, User *&out,
372  const PartitioningSolution<Adapter> &solution) const
373 {
374  // Get an import list
375 
376  size_t len = solution.getLocalNumberOfIds();
377  const gid_t *gids = solution.getIdList();
378  const partId_t *parts = solution.getPartList();
379  ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false);
380  ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len,
381  false);
382  ArrayRCP<lno_t> dummyIn;
383  ArrayRCP<gid_t> importList;
384  ArrayRCP<lno_t> dummyOut;
385  size_t numNewRows;
386  const RCP<const Comm<int> > comm = matrix_->getRowMap()->getComm();
387 
388  try{
389  numNewRows = solution.convertSolutionToImportList(
390  0, dummyIn, importList, dummyOut);
391  }
393 
394  gno_t lsum = numNewRows;
395  gno_t gsum = 0;
396  reduceAll<int, gno_t>(*comm, Teuchos::REDUCE_SUM, 1, &lsum, &gsum);
397 
398  RCP<const User> inPtr = rcp(&in, false);
399 
400  RCP<const User> outPtr = XpetraTraits<User>::doMigration(
401  inPtr, lsum, importList.getRawPtr());
402 
403  out = const_cast<User *>(outPtr.get());
404  outPtr.release();
405  return numNewRows;
406 }
407 
408 } //namespace Zoltan2
409 
410 #endif
size_t getRowWeights(int dim, const scalar_t *&weights, int &stride) const
Provide a pointer to the row weights, if any.
int getCoordinateDimension() const
Returns the dimension of the geometry, if any.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
Provide pointer to a weight array with stride.
XpetraCrsMatrixInput(const RCP< const User > &inmatrix, int coordDim=0, int weightDim=0)
Constructor.
size_t global_size_t
size_t getLocalNumRows() const
Returns the number rows on this process.
void setRowWeightIsNumberOfNonZeros(int dim)
Specify whether or not row weights for a dimension should be the count of row non zeros...
zoltan2_partId_t partId_t
const RCP< const xmatrix_t > & getMatrix() const
Access to Xpetra-wrapped user matrix.
Traits of Xpetra classes, including migration method.
Defines the PartitioningSolution class.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
size_t getRowListView(const gid_t *&rowIds, const lno_t *&offsets, const gid_t *&colIds) const
Sets pointers to this process&#39; matrix entries. If this optional call is defined in the adapter...
string inputAdapterName() const
Returns a descriptive name that identifies the concrete adapter.
size_t applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
size_t convertSolutionToImportList(int numExtra, ArrayRCP< Extra > &xtraInfo, ArrayRCP< typename Adapter::gid_t > &imports, ArrayRCP< Extra > &newXtraInfo) const
Create an import list from the export list.
int getRowWeightDimension() const
Returns the dimension (0 or greater) of row weights.
const zoltan2_partId_t * getPartList() const
Returns the part list corresponding to the global ID list.
void setRowCoordinates(int dim, const scalar_t *coordVal, int stride)
Specify geometric coordinates for matrix rows.
size_t getRowCoordinates(int dim, const scalar_t *&coords, int &stride) const
Provide a pointer to one dimension of row coordinates.
A PartitioningSolution is a solution to a partitioning problem.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
The StridedData class manages lists of weights or coordinates.
static RCP< const User > doMigration(const RCP< const User > &from, lno_t numLocalRows, const gno_t *myNewRows)
Migrate the object Given a user object and a new row distribution, create and return a new user objec...
static RCP< const User > convertToXpetra(const RCP< const User > &a)
Convert the object to its Xpetra wrapped version.
default_gno_t gid_t
The data type that the user uses for global Identifiers.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
MatrixInput defines the interface for input adapters for matrices.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
double scalar_t
const gid_t * getIdList() const
Returns the user&#39;s global ID list.
size_t getLocalNumColumns() const
Returns the number columns on this process.
size_t getLocalNumberOfObjects() const
Returns the number of objects in the input.
int getNumberOfWeightsPerObject() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
Kokkos::DefaultNode::DefaultNodeType node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
Defines the MatrixInput adapter interface.
bool getRowWeightIsNumberOfNonZeros(int dim) const
Is the row weight for a dimension the number of row non-zeros?
void setRowWeights(int dim, const scalar_t *weightVal, int stride)
Specify a weight for each row.
Xpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > xmatrix_t
default_scalar_t scalar_t
The data type for weights and coordinates.
Kokkos::DefaultNode::DefaultNodeType node_t
This file defines the StridedData class.
size_t getLocalNumberOfIds() const
Returns the local number of Ids.