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 User user_t;
121  typedef UserCoord userCoord_t;
122 #endif
123 
125 
126  // Constructor; sets default primaryEntityType to MATRIX_ROW.
127  MatrixAdapter() : primaryEntityType_(MATRIX_ROW),
128  coordinateInput_(),
129  haveCoordinateInput_(false) {}
130 
133  virtual ~MatrixAdapter(){}
134 
137  virtual size_t getLocalNumRows() const = 0;
138 
141  virtual size_t getLocalNumColumns() const = 0;
142 
145  virtual size_t getLocalNumEntries() const = 0;
146 
147 
148 
154  virtual bool CRSViewAvailable() const { return false; }
155 
159  virtual void getRowIDsView(const gno_t *&rowIds) const
160  {
161  rowIds = NULL;
163  }
164 
176  virtual void getCRSView(const lno_t *&offsets,
177  const gno_t *&colIds) const
178  {
179  // Default implementation; no CRS view provided.
180  offsets = NULL;
181  colIds = NULL;
183  }
184 
199  virtual void getCRSView(const lno_t *&offsets,
200  const gno_t *& colIds,
201  const scalar_t *&values) const
202  {
203  // Default implementation; no CRS view provided.
204  offsets = NULL;
205  colIds = NULL;
206  values = NULL;
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 
233  virtual bool useNumNonzerosAsRowWeight(int idx) const
234  {
236  }
237 
243  virtual bool CCSViewAvailable() const { return false; }
244 
248  virtual void getColumnIDsView(const gno_t *&colIds) const
249  {
250  colIds = NULL;
252  }
253 
265  virtual void getCCSView(const lno_t *&offsets,
266  const gno_t *&rowIds) const
267  {
268  // Default implementation; no CCS view provided.
269  offsets = NULL;
270  rowIds = NULL;
272  }
273 
288  virtual void getCCSView(const lno_t *&offsets,
289  const gno_t *&rowIds,
290  const scalar_t *&values) const
291  {
292  // Default implementation; no CCS view provided.
293  offsets = NULL;
294  rowIds = NULL;
295  values = NULL;
297  }
298 
302  virtual int getNumWeightsPerColumn() const { return 0; }
303 
310  virtual void getColumnWeightsView(const scalar_t *&weights, int &stride,
311  int idx = 0) const
312  {
313  // Default implementation
314  weights = NULL;
315  stride = 0;
317  }
318 
322  virtual bool useNumNonzerosAsColumnWeight(int idx) const { return 0; }
323 
324 #ifdef FUTURE_FEATURE
325 
329  virtual bool symmetricStorage() const {return false;}
330 #endif
331 
341  {
342  coordinateInput_ = coordData;
343  haveCoordinateInput_ = true;
344  }
345 
349  bool coordinatesAvailable() const { return haveCoordinateInput_; }
350 
355  {
356  return coordinateInput_;
357  }
358 
360  // Implementations of base-class methods and other methods shared by all
361 
366  {
367  return this->primaryEntityType_;
368  }
369 
375  void setPrimaryEntityType(std::string typestr)
376  {
377  if (typestr == "row") {
378  this->primaryEntityType = MATRIX_ROW;
379  }
380  else if (typestr == "column") {
381  this->primaryEntityType = MATRIX_COLUMN;
382  }
383  else if (typestr == "nonzero") {
384  this->primaryEntityType = MATRIX_NONZERO;
385  }
386  else {
387  std::ostringstream emsg;
388  emsg << __FILE__ << "," << __LINE__
389  << " error: Invalid MatrixEntityType " << typestr << std::endl;
390  emsg << "Valid values are 'row', 'column' and 'nonzero'." << std::endl;
391  throw std::runtime_error(emsg.str());
392  }
393  }
394 
395  // Functions from the BaseAdapter interface
396  size_t getLocalNumIDs() const
397  {
398  switch (getPrimaryEntityType()) {
399  case MATRIX_ROW:
400  return getLocalNumRows();
401  case MATRIX_COLUMN:
402  return getLocalNumColumns();
403  case MATRIX_NONZERO:
404  return getLocalNumEntries();
405  default: // Shouldn't reach default; just making compiler happy
406  return 0;
407  }
408  }
409 
410  void getIDsView(const gno_t *&Ids) const
411  {
412  switch (getPrimaryEntityType()) {
413  case MATRIX_ROW:
414  getRowIDsView(Ids);
415  break;
416  case MATRIX_COLUMN:
417  getColumnIDsView(Ids);
418  break;
419  case MATRIX_NONZERO: {
420  // TODO: Need getNonzeroIDsView? What is a Nonzero ID?
421  // TODO: std::pair<gno_t, gno_t>?
422  std::ostringstream emsg;
423  emsg << __FILE__ << "," << __LINE__
424  << " error: getIDsView not yet supported for matrix nonzeros."
425  << std::endl;
426  throw std::runtime_error(emsg.str());
427  }
428  default: // Shouldn't reach default; just making compiler happy
429  break;
430  }
431  }
432 
433  int getNumWeightsPerID() const
434  {
435  switch (getPrimaryEntityType()) {
436  case MATRIX_ROW:
437  return getNumWeightsPerRow();
438  case MATRIX_COLUMN:
439  return getNumWeightsPerColumn();
440  case MATRIX_NONZERO:
441  return 0; //TODO: weights not yet supported for nonzeros
442  default: // Shouldn't reach default; just making compiler happy
443  return 0;
444  }
445  }
446 
447  void getWeightsView(const scalar_t *&wgt, int &stride, int idx = 0) const
448  {
449  switch (getPrimaryEntityType()) {
450  case MATRIX_ROW:
451  getRowWeightsView(wgt, stride, idx);
452  break;
453  case MATRIX_COLUMN:
454  getColumnWeightsView(wgt, stride, idx);
455  break;
456  case MATRIX_NONZERO:
457  {
458  // TODO: Need getNonzeroWeightsView with Nonzeros as primary object?
459  // TODO: That is, get Nonzeros' weights based on some nonzero ID?
460  std::ostringstream emsg;
461  emsg << __FILE__ << "," << __LINE__
462  << " error: getWeightsView not yet supported for matrix nonzeros."
463  << std::endl;
464  throw std::runtime_error(emsg.str());
465  }
466  default: // Shouldn't reach default; just making compiler happy
467  break;
468  }
469  }
470 
471  bool useDegreeAsWeight(int idx) const
472  {
473  if (this->getPrimaryEntityType() == MATRIX_ROW)
474  return useNumNonzerosAsRowWeight(idx);
475  else {
476  std::ostringstream emsg;
477  emsg << __FILE__ << "," << __LINE__
478  << " error: useDegreeAsWeight is currently supported only for rows"
479  << std::endl;
480  throw std::runtime_error(emsg.str());
481  }
482  }
483 };
484 
485 } //namespace Zoltan2
486 
487 #endif
InputTraits< User >::scalar_t scalar_t
void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const
Provide pointer to a weight array with stride.
virtual ~MatrixAdapter()
Destructor.
enum BaseAdapterType adapterType() const
Returns the type of adapter.
virtual void getCCSView(const lno_t *&offsets, const gno_t *&rowIds, const scalar_t *&values) const
Sets pointers to this process&#39; matrix entries and their values using compressed sparse column (CCS) f...
void setPrimaryEntityType(std::string typestr)
Sets the primary entity type. Called by algorithm based on parameter value in parameter list from app...
MatrixAdapter defines the adapter interface for matrices.
InputTraits< User >::gno_t gno_t
virtual bool useNumNonzerosAsColumnWeight(int idx) const
Indicate whether column weight with index idx should be the global number of nonzeros in the column...
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.
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.
static ArrayRCP< ArrayRCP< zscalar_t > > weights
void setCoordinateInput(VectorAdapter< UserCoord > *coordData)
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
virtual size_t getLocalNumColumns() const =0
Returns the number of columns on this process.
int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
virtual void getCRSView(const lno_t *&offsets, 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.
void getIDsView(const gno_t *&Ids) const
Provide a pointer to this process&#39; identifiers.
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 getCRSView(const lno_t *&offsets, const gno_t *&colIds, const scalar_t *&values) const
Sets pointers to this process&#39; matrix entries and their values using compressed sparse row (CRS) form...
virtual bool CCSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse column (CCS)...
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 int getNumWeightsPerRow() const
Returns the number of weights per row (0 or greater). Row weights may be used when partitioning matri...
InputTraits< User >::part_t part_t
VectorAdapter< UserCoord > * getCoordinateInput() const
Obtain the coordinate data registered by the user.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
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 getCCSView(const lno_t *&offsets, const gno_t *&rowIds) const
Sets pointers to this process&#39; matrix entries using compressed sparse column (CCS) format...
#define Z2_THROW_NOT_IMPLEMENTED_IN_ADAPTER
BaseAdapter defines methods required by all Adapters.
size_t getLocalNumIDs() const
Returns the number of objects on this process.
bool useDegreeAsWeight(int idx) const
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 size_t getLocalNumRows() const =0
Returns the number of rows on this process.
virtual size_t getLocalNumEntries() const =0
Returns the number of nonzeros on this process.
default_scalar_t scalar_t
The data type for weights and coordinates.
virtual void getColumnIDsView(const gno_t *&colIds) const
Sets pointer to this process&#39; columns&#39; global IDs.