Zoltan2  Version of the Day
Zoltan2_XpetraRowMatrixInput.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_RowMatrix.hpp>
59 
60 namespace Zoltan2 {
61 
63 
84 template <typename User>
85  class XpetraRowMatrixInput : public MatrixInput<User> {
86 public:
87 
88 #ifndef DOXYGEN_SHOULD_SKIP_THIS
89  typedef typename InputTraits<User>::scalar_t scalar_t;
90  typedef typename InputTraits<User>::lno_t lno_t;
91  typedef typename InputTraits<User>::gno_t gno_t;
92  typedef typename InputTraits<User>::gid_t gid_t;
93  typedef typename InputTraits<User>::node_t node_t;
94  typedef Xpetra::RowMatrix<scalar_t, lno_t, gno_t, node_t> xmatrix_t;
95  typedef MatrixInput<User> base_adapter_t;
96  typedef User user_t;
97 #endif
98 
102 
112  XpetraRowMatrixInput(const RCP<const User> &inmatrix, int coordDim=0,
113  int weightDim=0);
114 
129  void setRowCoordinates(int dim, const scalar_t *coordVal, int stride);
130 
146  void setRowWeights(int dim, const scalar_t *weightVal, int stride);
147 
153  void setRowWeightIsNumberOfNonZeros(int dim);
154 
158  const RCP<const xmatrix_t> &getMatrix() const
159  {
160  return matrix_;
161  }
162 
164  // The InputAdapter interface.
166 
167  string inputAdapterName()const { return string("XpetraRowMatrix");}
168 
169  size_t getLocalNumberOfObjects() const { return getLocalNumRows();}
170 
171  int getNumberOfWeightsPerObject() const { return 0;}
172 
173  size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
174  {
175  return getRowWeights(dim, wgt, stride);
176  }
177 
179  // The MatrixInput interface.
181 
182  size_t getLocalNumRows() const {
183  return matrix_->getNodeNumRows();
184  }
185 
187  return matrix_->getGlobalNumRows();
188  }
189 
190  size_t getLocalNumColumns() const {
191  return matrix_->getNodeNumCols();
192  }
193 
195  return matrix_->getGlobalNumCols();
196  }
197 
198  size_t getRowListView(const gid_t *&rowIds,
199  const lno_t *&offsets, const gid_t *& colIds) const
200  {
201  size_t nrows = getLocalNumRows();
202 
203  ArrayView<const gid_t> rowView = rowMap_->getNodeElementList();
204  rowIds = rowView.getRawPtr();
205  offsets = offset_.getRawPtr();
206  colIds = columnIds_.getRawPtr();
207  return nrows;
208  }
209 
211  {
212  return weightDim_;
213  }
214 
215  size_t getRowWeights(int dim,
216  const scalar_t *&weights, int &stride) const
217  {
218  env_->localInputAssertion(__FILE__, __LINE__,
219  "invalid weight dimension",
220  dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
221 
222  size_t length;
223  rowWeights_[dim].getStridedList(length, weights, stride);
224  return length;
225  }
226 
227  bool getRowWeightIsNumberOfNonZeros(int dim) const { return numNzWeight_[dim];}
228 
229  int getCoordinateDimension() const {return coordinateDim_;}
230 
231  size_t getRowCoordinates(int dim,
232  const scalar_t *&coords, int &stride) const
233  {
234  env_->localInputAssertion(__FILE__, __LINE__,
235  "invalid coordinate dimension",
236  dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
237 
238  size_t length;
239  rowCoords_[dim].getStridedList(length, coords, stride);
240  return length;
241  }
242 
244  // End of MatrixInput interface.
246 
247  template <typename Adapter>
248  size_t applyPartitioningSolution(const User &in, User *&out,
249  const PartitioningSolution<Adapter> &solution) const;
250 
251 private:
252 
253  RCP<Environment> env_; // for error messages, etc.
254 
255  RCP<const User> inmatrix_;
256  RCP<const xmatrix_t> matrix_;
257  RCP<const Xpetra::Map<lno_t, gno_t, node_t> > rowMap_;
258  RCP<const Xpetra::Map<lno_t, gno_t, node_t> > colMap_;
259  lno_t base_;
260  ArrayRCP<lno_t> offset_;
261  ArrayRCP<gno_t> columnIds_;
262 
263  int coordinateDim_;
264  ArrayRCP<StridedData<lno_t, scalar_t> > rowCoords_;
265 
266  int weightDim_;
267  ArrayRCP<StridedData<lno_t, scalar_t> > rowWeights_;
268  ArrayRCP<bool> numNzWeight_;
269 
270  bool mayHaveDiagonalEntries;
271 };
272 
274 // Definitions
276 
277 template <typename User>
279  const RCP<const User> &inmatrix, int coordDim, int weightDim):
280  env_(rcp(new Environment)),
281  inmatrix_(inmatrix), matrix_(), rowMap_(), colMap_(), base_(),
282  offset_(), columnIds_(),
283  coordinateDim_(coordDim), rowCoords_(),
284  weightDim_(weightDim), rowWeights_(), numNzWeight_(),
285  mayHaveDiagonalEntries(true)
286 {
288  matrix_ = XpetraTraits<User>::convertToXpetra(inmatrix);
289 
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  size_t maxnumentries = matrix_->getNodeMaxNumRowEntries();
297 
298  offset_.resize(nrows+1, 0);
299  columnIds_.resize(nnz);
300  ArrayRCP<lno_t> indices(maxnumentries);
301  ArrayRCP<scalar_t> nzs(maxnumentries);
302 
303  lno_t next = 0;
304  for (size_t i=0; i < nrows; i++){
305  lno_t row = i + base_;
306  matrix_->getLocalRowCopy(row,indices(),nzs(),nnz);
307  for (size_t j=0; j < nnz; j++){
308  // TODO - this will be slow
309  // Is it possible that global columns ids might be stored in order?
310  columnIds_[next++] = colMap_->getGlobalElement(indices[j]);
311  }
312  offset_[i+1] = offset_[i] + nnz;
313  }
314 
315  if (coordinateDim_ > 0)
316  rowCoords_ = arcp(new input_t [coordinateDim_], 0, coordinateDim_, true);
317 
318  if (weightDim_ > 0){
319  rowWeights_ = arcp(new input_t [weightDim_], 0, weightDim_, true);
320  numNzWeight_ = arcp(new bool [weightDim_], 0, weightDim_, true);
321  for (int i=0; i < weightDim_; i++)
322  numNzWeight_[i] = false;
323  }
324 }
325 
326 // TODO (from 3/21/12 mtg): Consider changing interface to take an XpetraMultivector
327 template <typename User>
329  const scalar_t *coordVal, int stride)
330 {
332 
333  env_->localInputAssertion(__FILE__, __LINE__,
334  "invalid row coordinate dimension",
335  dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
336 
337  size_t nvtx = getLocalNumRows();
338 
339  ArrayRCP<const scalar_t> coordV(coordVal, 0, nvtx, false);
340  rowCoords_[dim] = input_t(coordV, stride);
341 }
342 
343 template <typename User>
345  const scalar_t *weightVal, int stride)
346 {
348 
349  env_->localInputAssertion(__FILE__, __LINE__,
350  "invalid row weight dimension",
351  dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
352 
353  size_t nvtx = getLocalNumRows();
354 
355  ArrayRCP<const scalar_t> weightV(weightVal, 0, nvtx, false);
356  rowWeights_[dim] = input_t(weightV, stride);
357 }
358 
359 template <typename User>
361 {
362  env_->localInputAssertion(__FILE__, __LINE__,
363  "invalid row weight dimension",
364  dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
365 
366  numNzWeight_[dim] = true;
367 }
368 
369 template <typename User>
370  template <typename Adapter>
372  const User &in, User *&out,
373  const PartitioningSolution<Adapter> &solution) const
374 {
375  // Get an import list
376 
377  size_t len = solution.getLocalNumberOfIds();
378  const gid_t *gids = solution.getIdList();
379  const partId_t *parts = solution.getPartList();
380  ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false);
381  ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len,
382  false);
383  ArrayRCP<lno_t> dummyIn;
384  ArrayRCP<gid_t> importList;
385  ArrayRCP<lno_t> dummyOut;
386  size_t numNewRows;
387  const RCP<const Comm<int> > comm = matrix_->getRowMap()->getComm();
388 
389  try{
390  numNewRows = solution.convertSolutionToImportList(
391  0, dummyIn, importList, dummyOut);
392  }
394 
395  gno_t lsum = numNewRows;
396  gno_t gsum = 0;
397  reduceAll<int, gno_t>(*comm, Teuchos::REDUCE_SUM, 1, &lsum, &gsum);
398 
399  RCP<const User> inPtr = rcp(&in, false);
400 
401  RCP<const User> outPtr = XpetraTraits<User>::doMigration(
402  inPtr, lsum, importList.getRawPtr());
403 
404  out = const_cast<User *>(outPtr.get());
405  outPtr.release();
406  return numNewRows;
407 }
408 
409 } //namespace Zoltan2
410 
411 #endif
XpetraRowMatrixInput(const RCP< const User > &inmatrix, int coordDim=0, int weightDim=0)
Constructor.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
size_t applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
string inputAdapterName() const
Returns a descriptive name that identifies the concrete adapter.
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
void setRowCoordinates(int dim, const scalar_t *coordVal, int stride)
Specify geometric coordinates for matrix rows.
size_t global_size_t
zoltan2_partId_t partId_t
Provides access for Zoltan2 to Xpetra::CRowatrix data.
Traits of Xpetra classes, including migration method.
Defines the PartitioningSolution class.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
const RCP< const xmatrix_t > & getMatrix() const
Access to Xpetra-wrapped user matrix.
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.
void setRowWeights(int dim, const scalar_t *weightVal, int stride)
Specify a weight for each row.
size_t getLocalNumColumns() const
Returns the number columns on this process.
const zoltan2_partId_t * getPartList() const
Returns the part list corresponding to the global ID list.
A PartitioningSolution is a solution to a partitioning problem.
size_t getRowCoordinates(int dim, const scalar_t *&coords, int &stride) const
Provide a pointer to one dimension of row coordinates.
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.
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...
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.
int getNumberOfWeightsPerObject() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
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.
bool getRowWeightIsNumberOfNonZeros(int dim) const
Is the row weight for a dimension the number of row non-zeros?
size_t getLocalNumberOfObjects() const
Returns the number of objects in the input.
Kokkos::DefaultNode::DefaultNodeType node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
Defines the MatrixInput adapter interface.
int getRowWeightDimension() const
Returns the dimension (0 or greater) of row weights.
size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
Provide pointer to a weight array with stride.
void setRowWeightIsNumberOfNonZeros(int dim)
Specify whether or not row weights for a dimension should be the count of row non zeros...
int getCoordinateDimension() const
Returns the dimension of the geometry, if any.
Xpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > xmatrix_t
size_t getLocalNumRows() const
Returns the number rows on this process.
size_t getRowWeights(int dim, const scalar_t *&weights, int &stride) const
Provide a pointer to the row weights, if any.
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.