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 // ***********************************************************************
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
InputTraits< User >::scalar_t scalar_t
Zoltan2::BaseAdapter< userTypes_t > base_adapter_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.
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.
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...
InputTraits< User >::lno_t lno_t
Defines the VectorAdapter interface.
virtual void getCCSView(const offset_t *&offsets, const gno_t *&rowIds) const
Sets pointers to this process&#39; matrix entries using compressed sparse column (CCS) format...
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
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 void getCCSView(const offset_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...
virtual void getCRSView(const offset_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.
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...
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 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.
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.
InputTraits< User >::offset_t offset_t
virtual void getCRSView(const offset_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...
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.
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:74