Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_MatrixAdapter.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #ifndef _ZOLTAN2_MATRIXADAPTER_HPP_
15 #define _ZOLTAN2_MATRIXADAPTER_HPP_
16 
17 #include <Zoltan2_Adapter.hpp>
19 
20 namespace Zoltan2 {
21 
26 };
27 
69 template <typename User, typename UserCoord=User>
70  class MatrixAdapter : public AdapterWithCoordsWrapper<User, UserCoord> {
71 private:
72  enum MatrixEntityType primaryEntityType_;
73  VectorAdapter<UserCoord> *coordinateInput_;
74  bool haveCoordinateInput_;
75 
76 public:
77 
78 #ifndef DOXYGEN_SHOULD_SKIP_THIS
79  using scalar_t = typename InputTraits<User>::scalar_t;
80  using lno_t = typename InputTraits<User>::lno_t;
81  using gno_t = typename InputTraits<User>::gno_t;
82  using part_t = typename InputTraits<User>::part_t;
83  using node_t = typename InputTraits<User>::node_t;
84  using offset_t = typename InputTraits<User>::offset_t;
85  using user_t = User;
86  using userCoord_t = UserCoord;
88  using device_t = typename node_t::device_type;
89 #endif
90 
91  enum BaseAdapterType adapterType() const override {return MatrixAdapterType;}
92 
93  // Constructor; sets default primaryEntityType to MATRIX_ROW.
94  MatrixAdapter() : primaryEntityType_(MATRIX_ROW),
95  coordinateInput_(),
96  haveCoordinateInput_(false) {}
97 
100  virtual size_t getLocalNumRows() const = 0;
101 
104  virtual size_t getLocalNumColumns() const = 0;
105 
108  virtual size_t getLocalNumEntries() const = 0;
109 
110 
111 
117  virtual bool CRSViewAvailable() const { return false; }
118 
122  virtual void getRowIDsView(const gno_t *&rowIds) const
123  {
124  rowIds = NULL;
126  }
127 
128  virtual void getRowIDsHostView(typename BaseAdapter<User>::ConstIdsHostView& rowIds) const
129  {
131  }
132 
133  virtual void getRowIDsDeviceView(typename BaseAdapter<User>::ConstIdsDeviceView& rowIds) const
134  {
136  }
137 
149  virtual void getCRSView(ArrayRCP<const offset_t> &offsets, ArrayRCP<const gno_t> &colIds) const
150  {
151  // Default implementation; no CRS view provided.
152  offsets = ArrayRCP<const offset_t>();
153  colIds = ArrayRCP<const gno_t>();
155  }
156 
158  typename BaseAdapter<User>::ConstIdsHostView& colIds) const
159  {
161  }
162 
164  typename BaseAdapter<User>::ConstIdsDeviceView& colIds) const
165  {
167  }
168 
169 
184  virtual void getCRSView(ArrayRCP<const offset_t> &offsets,
185  ArrayRCP<const gno_t> &colIds,
186  ArrayRCP<const scalar_t> &values) const
187  {
188  // Default implementation; no CRS view provided.
189  offsets = ArrayRCP<const offset_t>();
190  colIds = ArrayRCP<const gno_t>();
191  values = ArrayRCP<const scalar_t>();
193  }
194 
196  typename BaseAdapter<User>::ConstIdsHostView& colIds,
197  typename BaseAdapter<User>::ConstScalarsHostView& values) const
198  {
200  }
201 
203  typename BaseAdapter<User>::ConstIdsDeviceView& colIds,
204  typename BaseAdapter<User>::ConstScalarsDeviceView& values) const
205  {
207  }
208 
209 
213  virtual int getNumWeightsPerRow() const { return 0;}
214 
221  virtual void getRowWeightsView(const scalar_t *&weights, int &stride,
222  int idx = 0) const
223  {
224  // Default implementation
225  weights = NULL;
226  stride = 0;
228  }
229 
231  int /* idx */ = 0) const {
233  }
234 
237  }
238 
240  int /* idx */ = 0) const {
242  }
243 
246  }
247 
251  virtual bool useNumNonzerosAsRowWeight(int idx) const
252  {
254  }
255 
261  virtual bool CCSViewAvailable() const { return false; }
262 
266  virtual void getColumnIDsView(const gno_t *&colIds) const
267  {
268  colIds = NULL;
270  }
271 
272  virtual void getColumnIDsHostView(typename BaseAdapter<User>::ConstIdsHostView& colIds) const
273  {
275  }
276 
278  {
280  }
281 
293  virtual void getCCSView(ArrayRCP<const offset_t> &offsets,
294  ArrayRCP<const gno_t> &rowIds) const
295  {
296  // Default implementation; no CCS view provided.
297  offsets = ArrayRCP<const offset_t>();
298  rowIds = ArrayRCP<const gno_t>();
300  }
301 
316  virtual void getCCSView(ArrayRCP<const offset_t> &offsets,
317  ArrayRCP<const gno_t> &rowIds,
318  ArrayRCP<const scalar_t> &values) const
319  {
320  // Default implementation; no CCS view provided.
321  offsets = ArrayRCP<const offset_t>();
322  rowIds = ArrayRCP<const gno_t>();
323  values = ArrayRCP<const scalar_t>();
325  }
326 
330  virtual int getNumWeightsPerColumn() const { return 0; }
331 
338  virtual void getColumnWeightsView(const scalar_t *&weights, int &stride,
339  int idx = 0) const
340  {
341  // Default implementation
342  weights = NULL;
343  stride = 0;
345  }
346 
348  int /* idx */ = 0) const {
350  }
351 
354  }
355 
357  int /* idx */ = 0) const {
359  }
360 
363  }
364 
368  virtual bool useNumNonzerosAsColumnWeight(int idx) const { return 0; }
369 
370 #ifdef FUTURE_FEATURE
371 
375  virtual bool symmetricStorage() const {return false;}
376 #endif
377 
387  {
388  coordinateInput_ = coordData;
389  haveCoordinateInput_ = true;
390  }
391 
395  bool coordinatesAvailable() const { return haveCoordinateInput_; }
396 
401  {
402  return coordinateInput_;
403  }
404 
406  // Implementations of base-class methods and other methods shared by all
407 
412  {
413  return this->primaryEntityType_;
414  }
415 
421  void setPrimaryEntityType(std::string typestr)
422  {
423  if (typestr == "row") {
424  this->primaryEntityType = MATRIX_ROW;
425  }
426  else if (typestr == "column") {
427  this->primaryEntityType = MATRIX_COLUMN;
428  }
429  else if (typestr == "nonzero") {
430  this->primaryEntityType = MATRIX_NONZERO;
431  }
432  else {
433  std::ostringstream emsg;
434  emsg << __FILE__ << "," << __LINE__
435  << " error: Invalid MatrixEntityType " << typestr << std::endl;
436  emsg << "Valid values are 'row', 'column' and 'nonzero'." << std::endl;
437  throw std::runtime_error(emsg.str());
438  }
439  }
440 
441  // Functions from the BaseAdapter interface
442  size_t getLocalNumIDs() const override
443  {
444  switch (getPrimaryEntityType()) {
445  case MATRIX_ROW:
446  return getLocalNumRows();
447  case MATRIX_COLUMN:
448  return getLocalNumColumns();
449  case MATRIX_NONZERO:
450  return getLocalNumEntries();
451  default: // Shouldn't reach default; just making compiler happy
452  return 0;
453  }
454  }
455 
456  void getIDsView(const gno_t *&Ids) const override
457  {
458  switch (getPrimaryEntityType()) {
459  case MATRIX_ROW:
460  getRowIDsView(Ids);
461  break;
462  case MATRIX_COLUMN:
463  getColumnIDsView(Ids);
464  break;
465  case MATRIX_NONZERO: {
466  // TODO: Need getNonzeroIDsView? What is a Nonzero ID?
467  // TODO: std::pair<gno_t, gno_t>?
468  std::ostringstream emsg;
469  emsg << __FILE__ << "," << __LINE__
470  << " error: getIDsView not yet supported for matrix nonzeros."
471  << std::endl;
472  throw std::runtime_error(emsg.str());
473  }
474  default: // Shouldn't reach default; just making compiler happy
475  break;
476  }
477  }
478 
479  void getIDsHostView(typename BaseAdapter<User>::ConstIdsHostView& ids) const override {
480  switch (getPrimaryEntityType()) {
481  case MATRIX_ROW:
482  getRowIDsHostView(ids);
483  break;
484  case MATRIX_COLUMN:
486  break;
487  case MATRIX_NONZERO: {
488  // TODO: Need getNonzeroIDsHostView? What is a Nonzero ID?
489  // TODO: std::pair<gno_t, gno_t>?
490  std::ostringstream emsg;
491  emsg << __FILE__ << "," << __LINE__
492  << " error: getIDsView not yet supported for matrix nonzeros."
493  << std::endl;
494  throw std::runtime_error(emsg.str());
495  }
496  default: // Shouldn't reach default; just making compiler happy
497  break;
498  }
499  }
500 
501  void getIDsDeviceView(typename BaseAdapter<User>::ConstIdsDeviceView& ids) const override {
502  switch (getPrimaryEntityType()) {
503  case MATRIX_ROW:
504  getRowIDsDeviceView(ids);
505  break;
506  case MATRIX_COLUMN:
508  break;
509  case MATRIX_NONZERO: {
510  // TODO: Need getNonzeroIDsDeviceView? What is a Nonzero ID?
511  // TODO: std::pair<gno_t, gno_t>?
512  std::ostringstream emsg;
513  emsg << __FILE__ << "," << __LINE__
514  << " error: getIDsView not yet supported for matrix nonzeros."
515  << std::endl;
516  throw std::runtime_error(emsg.str());
517  }
518  default: // Shouldn't reach default; just making compiler happy
519  break;
520  }
521  }
522 
523  int getNumWeightsPerID() const override
524  {
525  switch (getPrimaryEntityType()) {
526  case MATRIX_ROW:
527  return getNumWeightsPerRow();
528  case MATRIX_COLUMN:
529  return getNumWeightsPerColumn();
530  case MATRIX_NONZERO:
531  return 0; //TODO: weights not yet supported for nonzeros
532  default: // Shouldn't reach default; just making compiler happy
533  return 0;
534  }
535  }
536 
537  void getWeightsView(const scalar_t *&wgt, int &stride,
538  int idx = 0) const override
539  {
540  switch (getPrimaryEntityType()) {
541  case MATRIX_ROW:
542  getRowWeightsView(wgt, stride, idx);
543  break;
544  case MATRIX_COLUMN:
545  getColumnWeightsView(wgt, stride, idx);
546  break;
547  case MATRIX_NONZERO:
548  {
549  // TODO: Need getNonzeroWeightsView with Nonzeros as primary object?
550  // TODO: That is, get Nonzeros' weights based on some nonzero ID?
551  std::ostringstream emsg;
552  emsg << __FILE__ << "," << __LINE__
553  << " error: getWeightsView not yet supported for matrix nonzeros."
554  << std::endl;
555  throw std::runtime_error(emsg.str());
556  }
557  default: // Shouldn't reach default; just making compiler happy
558  break;
559  }
560  }
561 
562  void getWeightsHostView(typename BaseAdapter<User>::WeightsHostView &hostWgts) const override {
563  switch (getPrimaryEntityType()) {
564  case MATRIX_ROW:
565  getRowWeightsHostView(hostWgts);
566  break;
567  case MATRIX_COLUMN:
568  getColumnWeightsHostView(hostWgts);
569  break;
570  case MATRIX_NONZERO:
571  {
572  // TODO: Need getNonzeroWeightsView with Nonzeros as primary object?
573  // TODO: That is, get Nonzeros' weights based on some nonzero ID?
574  std::ostringstream emsg;
575  emsg << __FILE__ << "," << __LINE__
576  << " error: getWeightsView not yet supported for matrix nonzeros."
577  << std::endl;
578  throw std::runtime_error(emsg.str());
579  }
580  default: // Shouldn't reach default; just making compiler happy
581  break;
582  } }
583 
585  int idx = 0) const override {
586  switch (getPrimaryEntityType()) {
587  case MATRIX_ROW:
588  getRowWeightsHostView(hostWgts, idx);
589  break;
590  case MATRIX_COLUMN:
591  getColumnWeightsHostView(hostWgts, idx);
592  break;
593  case MATRIX_NONZERO:
594  {
595  // TODO: Need getNonzeroWeightsView with Nonzeros as primary object?
596  // TODO: That is, get Nonzeros' weights based on some nonzero ID?
597  std::ostringstream emsg;
598  emsg << __FILE__ << "," << __LINE__
599  << " error: getWeightsView not yet supported for matrix nonzeros."
600  << std::endl;
601  throw std::runtime_error(emsg.str());
602  }
603  default: // Shouldn't reach default; just making compiler happy
604  break;
605  } }
606 
607  void getWeightsDeviceView(typename BaseAdapter<User>::WeightsDeviceView& deviceWgts) const override {
608  switch (getPrimaryEntityType()) {
609  case MATRIX_ROW:
610  getRowWeightsDeviceView(deviceWgts);
611  break;
612  case MATRIX_COLUMN:
613  getColumnWeightsDeviceView(deviceWgts);
614  break;
615  case MATRIX_NONZERO:
616  {
617  // TODO: Need getNonzeroWeightsView with Nonzeros as primary object?
618  // TODO: That is, get Nonzeros' weights based on some nonzero ID?
619  std::ostringstream emsg;
620  emsg << __FILE__ << "," << __LINE__
621  << " error: getWeightsView not yet supported for matrix nonzeros."
622  << std::endl;
623  throw std::runtime_error(emsg.str());
624  }
625  default: // Shouldn't reach default; just making compiler happy
626  break;
627  } }
628 
630  int idx = 0) const override {
631  switch (getPrimaryEntityType()) {
632  case MATRIX_ROW:
633  getRowWeightsDeviceView(deviceWgts, idx);
634  break;
635  case MATRIX_COLUMN:
636  getColumnWeightsDeviceView(deviceWgts, idx);
637  break;
638  case MATRIX_NONZERO:
639  {
640  // TODO: Need getNonzeroWeightsView with Nonzeros as primary object?
641  // TODO: That is, get Nonzeros' weights based on some nonzero ID?
642  std::ostringstream emsg;
643  emsg << __FILE__ << "," << __LINE__
644  << " error: getWeightsView not yet supported for matrix nonzeros."
645  << std::endl;
646  throw std::runtime_error(emsg.str());
647  }
648  default: // Shouldn't reach default; just making compiler happy
649  break;
650  } }
651 
652  bool useDegreeAsWeight(int idx) const
653  {
654  if (this->getPrimaryEntityType() == MATRIX_ROW)
655  return useNumNonzerosAsRowWeight(idx);
656  else {
657  std::ostringstream emsg;
658  emsg << __FILE__ << "," << __LINE__
659  << " error: useDegreeAsWeight is currently supported only for rows"
660  << std::endl;
661  throw std::runtime_error(emsg.str());
662  }
663  }
664 };
665 
666 } //namespace Zoltan2
667 
668 #endif
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
VectorAdapter< UserCoord > * getCoordinateInput() const override
Obtain the coordinate data registered by the user.
size_t getLocalNumIDs() const override
Returns the number of objects on this process.
void setPrimaryEntityType(std::string typestr)
Sets the primary entity type. Called by algorithm based on parameter value in parameter list from app...
virtual void getCCSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &rowIds) const
Sets pointers to this process&#39; matrix entries using compressed sparse column (CCS) format...
virtual void getRowIDsDeviceView(typename BaseAdapter< User >::ConstIdsDeviceView &rowIds) const
typename InputTraits< User >::scalar_t scalar_t
MatrixAdapter defines the adapter interface for matrices.
static ArrayRCP< ArrayRCP< zscalar_t > > weights
Kokkos::View< scalar_t *, device_t > WeightsDeviceView1D
virtual bool useNumNonzerosAsColumnWeight(int idx) const
Indicate whether column weight with index idx should be the global number of nonzeros in the column...
void getWeightsHostView(typename BaseAdapter< User >::WeightsHostView1D &hostWgts, int idx=0) const override
virtual void getColumnWeightsDeviceView(typename BaseAdapter< User >::WeightsDeviceView &weights) const
bool coordinatesAvailable() const
Indicate whether coordinate information has been set for this MatrixAdapter.
default_part_t part_t
The data type to represent part numbers.
default_offset_t offset_t
The data type to represent offsets.
virtual int getNumWeightsPerColumn() const
Returns the number of weights per column (0 or greater). Column weights may be used when partitioning...
Defines the VectorAdapter interface.
enum MatrixEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc. Valid values are MATRIX_ROW, MATRIX_COLUMN, MATRIX_NONZERO.
virtual void getRowWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the row weights, if any.
#define Z2_THROW_NOT_IMPLEMENTED
virtual void getCRSDeviceView(typename BaseAdapter< User >::ConstOffsetsDeviceView &offsets, typename BaseAdapter< User >::ConstIdsDeviceView &colIds, typename BaseAdapter< User >::ConstScalarsDeviceView &values) const
typename node_t::device_type device_t
typename InputTraits< User >::part_t part_t
Kokkos::View< const gno_t *, device_t > ConstIdsDeviceView
void getWeightsDeviceView(typename BaseAdapter< User >::WeightsDeviceView &deviceWgts) const override
typename ConstScalarsDeviceView::HostMirror ConstScalarsHostView
void getIDsView(const gno_t *&Ids) const override
Provide a pointer to this process&#39; identifiers.
virtual void getRowWeightsDeviceView(typename BaseAdapter< User >::WeightsDeviceView1D &weights, int=0) const
virtual void getColumnIDsHostView(typename BaseAdapter< User >::ConstIdsHostView &colIds) const
virtual void getCRSHostView(typename BaseAdapter< User >::ConstOffsetsHostView &offsets, typename BaseAdapter< User >::ConstIdsHostView &colIds) const
typename WeightsDeviceView::HostMirror WeightsHostView
virtual size_t getLocalNumColumns() const =0
Returns the number of columns on this process.
virtual void getCRSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &colIds, ArrayRCP< const scalar_t > &values) const
Sets pointers to this process&#39; matrix entries and their values using compressed sparse row (CRS) form...
Kokkos::View< const scalar_t *, device_t > ConstScalarsDeviceView
void getIDsDeviceView(typename BaseAdapter< User >::ConstIdsDeviceView &ids) const override
typename InputTraits< User >::node_t node_t
typename ConstOffsetsDeviceView::HostMirror ConstOffsetsHostView
virtual bool useNumNonzerosAsRowWeight(int idx) const
Indicate whether row weight with index idx should be the global number of nonzeros in the row...
virtual void getRowIDsHostView(typename BaseAdapter< User >::ConstIdsHostView &rowIds) const
virtual bool CCSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse column (CCS)...
typename InputTraits< User >::gno_t gno_t
typename ConstIdsDeviceView::HostMirror ConstIdsHostView
virtual void getRowWeightsDeviceView(typename BaseAdapter< User >::WeightsDeviceView &weights) const
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
BaseAdapterType
An enum to identify general types of adapters.
virtual void getRowWeightsHostView(typename BaseAdapter< User >::WeightsHostView &weights) const
virtual int getNumWeightsPerRow() const
Returns the number of weights per row (0 or greater). Row weights may be used when partitioning matri...
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
enum BaseAdapterType adapterType() const override
Returns the type of adapter.
virtual void getColumnWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the column weights, if any.
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
virtual void getRowWeightsHostView(typename BaseAdapter< User >::WeightsHostView1D &weights, int=0) const
virtual void getCRSHostView(typename BaseAdapter< User >::ConstOffsetsHostView &offsets, typename BaseAdapter< User >::ConstIdsHostView &colIds, typename BaseAdapter< User >::ConstScalarsHostView &values) const
void getIDsHostView(typename BaseAdapter< User >::ConstIdsHostView &ids) const override
typename InputTraits< User >::offset_t offset_t
typename WeightsDeviceView1D::HostMirror WeightsHostView1D
int getNumWeightsPerID() const override
Returns the number of weights per object. Number of weights per object should be zero or greater...
virtual void getColumnWeightsDeviceView(typename BaseAdapter< User >::WeightsDeviceView1D &weights, int=0) const
void getWeightsHostView(typename BaseAdapter< User >::WeightsHostView &hostWgts) const override
bool useDegreeAsWeight(int idx) const
Kokkos::View< scalar_t **, device_t > WeightsDeviceView
virtual void getCCSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &rowIds, ArrayRCP< const scalar_t > &values) const
Sets pointers to this process&#39; matrix entries and their values using compressed sparse column (CCS) f...
void setCoordinateInput(VectorAdapter< UserCoord > *coordData) override
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
virtual void getRowIDsView(const gno_t *&rowIds) const
Sets pointer to this process&#39; rows&#39; global IDs.
virtual bool CRSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse row (CRS) fo...
virtual void getCRSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &colIds) const
Sets pointers to this process&#39; matrix entries using compressed sparse row (CRS) format. All matrix adapters must implement either getCRSView or getCCSView, but implementation of both is not required.
virtual size_t getLocalNumRows() const =0
Returns the number of rows on this process.
void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const override
Provide pointer to a weight array with stride.
virtual void getColumnIDsDeviceView(typename BaseAdapter< User >::ConstIdsDeviceView &colIds) const
virtual size_t getLocalNumEntries() const =0
Returns the number of nonzeros on this process.
Kokkos::View< const offset_t *, device_t > ConstOffsetsDeviceView
virtual void getCRSDeviceView(typename BaseAdapter< User >::ConstOffsetsDeviceView &offsets, typename BaseAdapter< User >::ConstIdsDeviceView &colIds) const
void getWeightsDeviceView(typename BaseAdapter< User >::WeightsDeviceView1D &deviceWgts, int idx=0) const override
typename InputTraits< User >::lno_t lno_t
default_scalar_t scalar_t
The data type for weights and coordinates.
virtual void getColumnWeightsHostView(typename BaseAdapter< User >::WeightsHostView1D &weights, int=0) const
virtual void getColumnIDsView(const gno_t *&colIds) const
Sets pointer to this process&#39; columns&#39; global IDs.
virtual void getColumnWeightsHostView(typename BaseAdapter< User >::WeightsHostView &weights) const