Zoltan2
Zoltan2_MatrixAdapter.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_MATRIXADAPTER_HPP_
51 #define _ZOLTAN2_MATRIXADAPTER_HPP_
52 
53 #include <Zoltan2_Adapter.hpp>
55 
56 namespace Zoltan2 {
57 
62 };
63 
105 template <typename User, typename UserCoord=User>
106  class MatrixAdapter : public BaseAdapter<User> {
107 private:
108  enum MatrixEntityType primaryEntityType_;
109  VectorAdapter<UserCoord> *coordinateInput_;
110  bool haveCoordinateInput_;
111 
112 public:
113 
114 #ifndef DOXYGEN_SHOULD_SKIP_THIS
115  typedef typename InputTraits<User>::scalar_t scalar_t;
116  typedef typename InputTraits<User>::lno_t lno_t;
117  typedef typename InputTraits<User>::gno_t gno_t;
118  typedef typename InputTraits<User>::part_t part_t;
119  typedef typename InputTraits<User>::node_t node_t;
120  typedef typename InputTraits<User>::offset_t offset_t;
121  typedef User user_t;
122  typedef UserCoord userCoord_t;
124 #endif
125 
127 
128  // Constructor; sets default primaryEntityType to MATRIX_ROW.
129  MatrixAdapter() : primaryEntityType_(MATRIX_ROW),
130  coordinateInput_(),
131  haveCoordinateInput_(false) {}
132 
135  virtual ~MatrixAdapter(){}
136 
139  virtual size_t getLocalNumRows() const = 0;
140 
143  virtual size_t getLocalNumColumns() const = 0;
144 
147  virtual size_t getLocalNumEntries() const = 0;
148 
149 
150 
156  virtual bool CRSViewAvailable() const { return false; }
157 
161  virtual void getRowIDsView(const gno_t *&rowIds) const
162  {
163  rowIds = NULL;
165  }
166 
178  virtual void getCRSView(const offset_t *&offsets, const gno_t *&colIds) const
179  {
180  // Default implementation; no CRS view provided.
181  offsets = NULL;
182  colIds = NULL;
184  }
185 
200  virtual void getCRSView(const offset_t *&offsets,
201  const gno_t *& colIds,
202  const scalar_t *&values) const
203  {
204  // Default implementation; no CRS view provided.
205  offsets = NULL;
206  colIds = NULL;
207  values = NULL;
209  }
210 
214  virtual int getNumWeightsPerRow() const { return 0;}
215 
222  virtual void getRowWeightsView(const scalar_t *&weights, int &stride,
223  int idx = 0) const
224  {
225  // Default implementation
226  weights = NULL;
227  stride = 0;
229  }
230 
234  virtual bool useNumNonzerosAsRowWeight(int idx) const
235  {
237  }
238 
244  virtual bool CCSViewAvailable() const { return false; }
245 
249  virtual void getColumnIDsView(const gno_t *&colIds) const
250  {
251  colIds = NULL;
253  }
254 
266  virtual void getCCSView(const offset_t *&offsets,
267  const gno_t *&rowIds) const
268  {
269  // Default implementation; no CCS view provided.
270  offsets = NULL;
271  rowIds = NULL;
273  }
274 
289  virtual void getCCSView(const offset_t *&offsets,
290  const gno_t *&rowIds,
291  const scalar_t *&values) const
292  {
293  // Default implementation; no CCS view provided.
294  offsets = NULL;
295  rowIds = NULL;
296  values = NULL;
298  }
299 
303  virtual int getNumWeightsPerColumn() const { return 0; }
304 
311  virtual void getColumnWeightsView(const scalar_t *&weights, int &stride,
312  int idx = 0) const
313  {
314  // Default implementation
315  weights = NULL;
316  stride = 0;
318  }
319 
323  virtual bool useNumNonzerosAsColumnWeight(int idx) const { return 0; }
324 
325 #ifdef FUTURE_FEATURE
326 
330  virtual bool symmetricStorage() const {return false;}
331 #endif
332 
342  {
343  coordinateInput_ = coordData;
344  haveCoordinateInput_ = true;
345  }
346 
350  bool coordinatesAvailable() const { return haveCoordinateInput_; }
351 
356  {
357  return coordinateInput_;
358  }
359 
361  // Implementations of base-class methods and other methods shared by all
362 
367  {
368  return this->primaryEntityType_;
369  }
370 
376  void setPrimaryEntityType(std::string typestr)
377  {
378  if (typestr == "row") {
379  this->primaryEntityType = MATRIX_ROW;
380  }
381  else if (typestr == "column") {
382  this->primaryEntityType = MATRIX_COLUMN;
383  }
384  else if (typestr == "nonzero") {
385  this->primaryEntityType = MATRIX_NONZERO;
386  }
387  else {
388  std::ostringstream emsg;
389  emsg << __FILE__ << "," << __LINE__
390  << " error: Invalid MatrixEntityType " << typestr << std::endl;
391  emsg << "Valid values are 'row', 'column' and 'nonzero'." << std::endl;
392  throw std::runtime_error(emsg.str());
393  }
394  }
395 
396  // Functions from the BaseAdapter interface
397  size_t getLocalNumIDs() const
398  {
399  switch (getPrimaryEntityType()) {
400  case MATRIX_ROW:
401  return getLocalNumRows();
402  case MATRIX_COLUMN:
403  return getLocalNumColumns();
404  case MATRIX_NONZERO:
405  return getLocalNumEntries();
406  default: // Shouldn't reach default; just making compiler happy
407  return 0;
408  }
409  }
410 
411  void getIDsView(const gno_t *&Ids) const
412  {
413  switch (getPrimaryEntityType()) {
414  case MATRIX_ROW:
415  getRowIDsView(Ids);
416  break;
417  case MATRIX_COLUMN:
418  getColumnIDsView(Ids);
419  break;
420  case MATRIX_NONZERO: {
421  // TODO: Need getNonzeroIDsView? What is a Nonzero ID?
422  // TODO: std::pair<gno_t, gno_t>?
423  std::ostringstream emsg;
424  emsg << __FILE__ << "," << __LINE__
425  << " error: getIDsView not yet supported for matrix nonzeros."
426  << std::endl;
427  throw std::runtime_error(emsg.str());
428  }
429  default: // Shouldn't reach default; just making compiler happy
430  break;
431  }
432  }
433 
434  int getNumWeightsPerID() const
435  {
436  switch (getPrimaryEntityType()) {
437  case MATRIX_ROW:
438  return getNumWeightsPerRow();
439  case MATRIX_COLUMN:
440  return getNumWeightsPerColumn();
441  case MATRIX_NONZERO:
442  return 0; //TODO: weights not yet supported for nonzeros
443  default: // Shouldn't reach default; just making compiler happy
444  return 0;
445  }
446  }
447 
448  void getWeightsView(const scalar_t *&wgt, int &stride, int idx = 0) const
449  {
450  switch (getPrimaryEntityType()) {
451  case MATRIX_ROW:
452  getRowWeightsView(wgt, stride, idx);
453  break;
454  case MATRIX_COLUMN:
455  getColumnWeightsView(wgt, stride, idx);
456  break;
457  case MATRIX_NONZERO:
458  {
459  // TODO: Need getNonzeroWeightsView with Nonzeros as primary object?
460  // TODO: That is, get Nonzeros' weights based on some nonzero ID?
461  std::ostringstream emsg;
462  emsg << __FILE__ << "," << __LINE__
463  << " error: getWeightsView not yet supported for matrix nonzeros."
464  << std::endl;
465  throw std::runtime_error(emsg.str());
466  }
467  default: // Shouldn't reach default; just making compiler happy
468  break;
469  }
470  }
471 
472  bool useDegreeAsWeight(int idx) const
473  {
474  if (this->getPrimaryEntityType() == MATRIX_ROW)
475  return useNumNonzerosAsRowWeight(idx);
476  else {
477  std::ostringstream emsg;
478  emsg << __FILE__ << "," << __LINE__
479  << " error: useDegreeAsWeight is currently supported only for rows"
480  << std::endl;
481  throw std::runtime_error(emsg.str());
482  }
483  }
484 };
485 
486 } //namespace Zoltan2
487 
488 #endif
Zoltan2::MatrixAdapter::getWeightsView
void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const
Definition: Zoltan2_MatrixAdapter.hpp:448
Zoltan2::InputTraits::scalar_t
default_scalar_t scalar_t
The data type for weights and coordinates.
Definition: Zoltan2_InputTraits.hpp:181
Zoltan2::MATRIX_ROW
Definition: Zoltan2_MatrixAdapter.hpp:59
Z2_THROW_NOT_IMPLEMENTED
#define Z2_THROW_NOT_IMPLEMENTED
Definition: Zoltan2_Exceptions.hpp:92
Zoltan2::MatrixAdapter
MatrixAdapter defines the adapter interface for matrices.
Definition: Zoltan2_MatrixAdapter.hpp:106
Zoltan2::MatrixAdapter::getCRSView
virtual void getCRSView(const offset_t *&offsets, const gno_t *&colIds, const scalar_t *&values) const
Sets pointers to this process' matrix entries and their values using compressed sparse row (CRS) form...
Definition: Zoltan2_MatrixAdapter.hpp:200
Zoltan2::MATRIX_COLUMN
Definition: Zoltan2_MatrixAdapter.hpp:60
Zoltan2::VectorAdapter< UserCoord >
Zoltan2::BaseAdapter::offset_t
InputTraits< User >::offset_t offset_t
Definition: Zoltan2_Adapter.hpp:108
Zoltan2::MatrixAdapter::getLocalNumEntries
virtual size_t getLocalNumEntries() const =0
Returns the number of nonzeros on this process.
Zoltan2::MatrixAdapter::coordinatesAvailable
bool coordinatesAvailable() const
Indicate whether coordinate information has been set for this MatrixAdapter.
Definition: Zoltan2_MatrixAdapter.hpp:350
Zoltan2::MatrixAdapter::getCCSView
virtual void getCCSView(const offset_t *&offsets, const gno_t *&rowIds, const scalar_t *&values) const
Sets pointers to this process' matrix entries and their values using compressed sparse column (CCS) f...
Definition: Zoltan2_MatrixAdapter.hpp:289
Zoltan2_TestingFramework::base_adapter_t
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Definition: Zoltan2_Typedefs.hpp:168
Zoltan2::InputTraits::part_t
default_part_t part_t
The data type to represent part numbers.
Definition: Zoltan2_InputTraits.hpp:199
Zoltan2::InputTraits::offset_t
default_offset_t offset_t
The data type to represent offsets.
Definition: Zoltan2_InputTraits.hpp:195
Zoltan2::MatrixAdapter::~MatrixAdapter
virtual ~MatrixAdapter()
Destructor.
Definition: Zoltan2_MatrixAdapter.hpp:135
Zoltan2::MatrixAdapter::getRowWeightsView
virtual void getRowWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the row weights, if any.
Definition: Zoltan2_MatrixAdapter.hpp:222
Zoltan2::MatrixAdapter::getCCSView
virtual void getCCSView(const offset_t *&offsets, const gno_t *&rowIds) const
Sets pointers to this process' matrix entries using compressed sparse column (CCS) format....
Definition: Zoltan2_MatrixAdapter.hpp:266
Zoltan2::MatrixEntityType
MatrixEntityType
Definition: Zoltan2_MatrixAdapter.hpp:58
Zoltan2::BaseAdapter::part_t
InputTraits< User >::part_t part_t
Definition: Zoltan2_Adapter.hpp:107
Zoltan2::MatrixAdapter::getCRSView
virtual void getCRSView(const offset_t *&offsets, const gno_t *&colIds) const
Sets pointers to this process' matrix entries using compressed sparse row (CRS) format....
Definition: Zoltan2_MatrixAdapter.hpp:178
Zoltan2::MatrixAdapter::getNumWeightsPerID
int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater....
Definition: Zoltan2_MatrixAdapter.hpp:434
Zoltan2_VectorAdapter.hpp
Defines the VectorAdapter interface.
Zoltan2::BaseAdapter::lno_t
InputTraits< User >::lno_t lno_t
Definition: Zoltan2_Adapter.hpp:104
Zoltan2::BaseAdapter::gno_t
InputTraits< User >::gno_t gno_t
Definition: Zoltan2_Adapter.hpp:105
Zoltan2::MatrixAdapter::getLocalNumColumns
virtual size_t getLocalNumColumns() const =0
Returns the number of columns on this process.
Zoltan2::MatrixAdapter::getPrimaryEntityType
enum MatrixEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc. Valid values are MATRIX_ROW,...
Definition: Zoltan2_MatrixAdapter.hpp:366
Zoltan2::MatrixAdapter::useDegreeAsWeight
bool useDegreeAsWeight(int idx) const
Definition: Zoltan2_MatrixAdapter.hpp:472
Zoltan2::MatrixAdapter::getNumWeightsPerColumn
virtual int getNumWeightsPerColumn() const
Returns the number of weights per column (0 or greater). Column weights may be used when partitioning...
Definition: Zoltan2_MatrixAdapter.hpp:303
Zoltan2_Adapter.hpp
Zoltan2::MATRIX_NONZERO
Definition: Zoltan2_MatrixAdapter.hpp:61
Zoltan2::InputTraits::node_t
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
Definition: Zoltan2_InputTraits.hpp:204
Zoltan2::InputTraits::lno_t
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices.
Definition: Zoltan2_InputTraits.hpp:186
Zoltan2::MatrixAdapter::useNumNonzerosAsRowWeight
virtual bool useNumNonzerosAsRowWeight(int idx) const
Indicate whether row weight with index idx should be the global number of nonzeros in the row.
Definition: Zoltan2_MatrixAdapter.hpp:234
Zoltan2::MatrixAdapter::MatrixAdapter
MatrixAdapter()
Definition: Zoltan2_MatrixAdapter.hpp:129
Zoltan2::MatrixAdapter::getRowIDsView
virtual void getRowIDsView(const gno_t *&rowIds) const
Sets pointer to this process' rows' global IDs.
Definition: Zoltan2_MatrixAdapter.hpp:161
Zoltan2::BaseAdapterType
BaseAdapterType
An enum to identify general types of adapters.
Definition: Zoltan2_Adapter.hpp:63
Zoltan2::MatrixAdapter::getIDsView
void getIDsView(const gno_t *&Ids) const
Provide a pointer to this process' identifiers.
Definition: Zoltan2_MatrixAdapter.hpp:411
Zoltan2::InputTraits::gno_t
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
Definition: Zoltan2_InputTraits.hpp:191
Zoltan2::MatrixAdapter::getColumnWeightsView
virtual void getColumnWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the column weights, if any.
Definition: Zoltan2_MatrixAdapter.hpp:311
Zoltan2::MatrixAdapter::setCoordinateInput
void setCoordinateInput(VectorAdapter< UserCoord > *coordData)
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
Definition: Zoltan2_MatrixAdapter.hpp:341
Zoltan2::MatrixAdapter::getLocalNumRows
virtual size_t getLocalNumRows() const =0
Returns the number of rows on this process.
Zoltan2::MatrixAdapter::getCoordinateInput
VectorAdapter< UserCoord > * getCoordinateInput() const
Obtain the coordinate data registered by the user.
Definition: Zoltan2_MatrixAdapter.hpp:355
Zoltan2::MatrixAdapter::useNumNonzerosAsColumnWeight
virtual bool useNumNonzerosAsColumnWeight(int idx) const
Indicate whether column weight with index idx should be the global number of nonzeros in the column.
Definition: Zoltan2_MatrixAdapter.hpp:323
Zoltan2::BaseAdapter
Definition: Zoltan2_Adapter.hpp:101
Zoltan2::MatrixAdapter::getLocalNumIDs
size_t getLocalNumIDs() const
Returns the number of objects on this process.
Definition: Zoltan2_MatrixAdapter.hpp:397
weights
static ArrayRCP< ArrayRCP< zscalar_t > > weights
Definition: rcbPerformanceZ1.cpp:82
Zoltan2
Definition: Zoltan2_AlgSerialGreedy.hpp:56
Zoltan2::MatrixAdapterType
matrix data
Definition: Zoltan2_Adapter.hpp:67
Zoltan2::MatrixAdapter::getNumWeightsPerRow
virtual int getNumWeightsPerRow() const
Returns the number of weights per row (0 or greater). Row weights may be used when partitioning matri...
Definition: Zoltan2_MatrixAdapter.hpp:214
Zoltan2::MatrixAdapter::CCSViewAvailable
virtual bool CCSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse column (CCS)...
Definition: Zoltan2_MatrixAdapter.hpp:244
Zoltan2::MatrixAdapter::CRSViewAvailable
virtual bool CRSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse row (CRS) fo...
Definition: Zoltan2_MatrixAdapter.hpp:156
Zoltan2::MatrixAdapter::setPrimaryEntityType
void setPrimaryEntityType(std::string typestr)
Sets the primary entity type. Called by algorithm based on parameter value in parameter list from app...
Definition: Zoltan2_MatrixAdapter.hpp:376
Zoltan2::BaseAdapter::scalar_t
InputTraits< User >::scalar_t scalar_t
Definition: Zoltan2_Adapter.hpp:106
Zoltan2::MatrixAdapter::adapterType
enum BaseAdapterType adapterType() const
Returns the type of adapter.
Definition: Zoltan2_MatrixAdapter.hpp:126
Zoltan2::MatrixAdapter::getColumnIDsView
virtual void getColumnIDsView(const gno_t *&colIds) const
Sets pointer to this process' columns' global IDs.
Definition: Zoltan2_MatrixAdapter.hpp:249