Zoltan2
Loading...
Searching...
No Matches
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
56namespace Zoltan2 {
57
63
105template <typename User, typename UserCoord=User>
106 class MatrixAdapter : public AdapterWithCoordsWrapper<User, UserCoord> {
107private:
108 enum MatrixEntityType primaryEntityType_;
109 VectorAdapter<UserCoord> *coordinateInput_;
110 bool haveCoordinateInput_;
111
112public:
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;
123 typedef MatrixAdapter<User,UserCoord> base_adapter_t;
124#endif
125
126 enum BaseAdapterType adapterType() const override {return MatrixAdapterType;}
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(ArrayRCP<const offset_t> &offsets, ArrayRCP<const gno_t> &colIds) const
179 {
180 // Default implementation; no CRS view provided.
181 offsets = ArrayRCP<const offset_t>();
182 colIds = ArrayRCP<const gno_t>();
184 }
185
200 virtual void getCRSView(ArrayRCP<const offset_t> &offsets,
201 ArrayRCP<const gno_t> &colIds,
202 ArrayRCP<const scalar_t> &values) const
203 {
204 // Default implementation; no CRS view provided.
205 offsets = ArrayRCP<const offset_t>();
206 colIds = ArrayRCP<const gno_t>();
207 values = ArrayRCP<const scalar_t>();
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(ArrayRCP<const offset_t> &offsets,
267 ArrayRCP<const gno_t> &rowIds) const
268 {
269 // Default implementation; no CCS view provided.
270 offsets = ArrayRCP<const offset_t>();
271 rowIds = ArrayRCP<const gno_t>();
273 }
274
289 virtual void getCCSView(ArrayRCP<const offset_t> &offsets,
290 ArrayRCP<const gno_t> &rowIds,
291 ArrayRCP<const scalar_t> &values) const
292 {
293 // Default implementation; no CCS view provided.
294 offsets = ArrayRCP<const offset_t>();
295 rowIds = ArrayRCP<const gno_t>();
296 values = ArrayRCP<const scalar_t>();
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
330 virtual bool symmetricStorage() const {return false;}
331#endif
332
341 void setCoordinateInput(VectorAdapter<UserCoord> *coordData) override
342 {
343 coordinateInput_ = coordData;
344 haveCoordinateInput_ = true;
345 }
346
350 bool coordinatesAvailable() const { return haveCoordinateInput_; }
351
355 VectorAdapter<UserCoord> *getCoordinateInput() const override
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 override
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 override
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 override
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 override
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::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition Metric.cpp:74
#define Z2_THROW_NOT_IMPLEMENTED
Defines the VectorAdapter interface.
InputTraits< User >::node_t node_t
InputTraits< User >::offset_t offset_t
InputTraits< User >::part_t part_t
InputTraits< User >::scalar_t scalar_t
InputTraits< User >::lno_t lno_t
InputTraits< User >::gno_t gno_t
MatrixAdapter defines the adapter interface for matrices.
bool useDegreeAsWeight(int idx) const
void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const override
void setPrimaryEntityType(std::string typestr)
Sets the primary entity type. Called by algorithm based on parameter value in parameter list from app...
void getIDsView(const gno_t *&Ids) const override
Provide a pointer to this process' identifiers.
VectorAdapter< UserCoord > * getCoordinateInput() const override
Obtain the coordinate data registered by the user.
virtual int getNumWeightsPerColumn() const
Returns the number of weights per column (0 or greater). Column weights may be used when partitioning...
virtual size_t getLocalNumEntries() const =0
Returns the number of nonzeros on this process.
virtual bool useNumNonzerosAsRowWeight(int idx) const
Indicate whether row weight with index idx should be the global number of nonzeros in the row.
enum BaseAdapterType adapterType() const override
Returns the type of adapter.
virtual ~MatrixAdapter()
Destructor.
virtual void getColumnWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the column weights, if any.
virtual void getColumnIDsView(const gno_t *&colIds) const
Sets pointer to this process' columns' global IDs.
virtual void getCCSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &rowIds) const
Sets pointers to this process' matrix entries using compressed sparse column (CCS) format....
virtual int getNumWeightsPerRow() const
Returns the number of weights per row (0 or greater). Row weights may be used when partitioning matri...
virtual bool useNumNonzerosAsColumnWeight(int idx) const
Indicate whether column weight with index idx should be the global number of nonzeros in the column.
virtual size_t getLocalNumColumns() const =0
Returns the number of columns on this process.
virtual void getRowWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the row weights, if any.
bool coordinatesAvailable() const
Indicate whether coordinate information has been set for this MatrixAdapter.
virtual bool CCSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse column (CCS)...
size_t getLocalNumIDs() const override
Returns the number of objects 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' matrix entries and their values using compressed sparse row (CRS) form...
void setCoordinateInput(VectorAdapter< UserCoord > *coordData) override
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
virtual void getCCSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &rowIds, ArrayRCP< const scalar_t > &values) const
Sets pointers to this process' matrix entries and their values using compressed sparse column (CCS) f...
virtual void getRowIDsView(const gno_t *&rowIds) const
Sets pointer to this process' rows' global IDs.
virtual bool CRSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse row (CRS) fo...
enum MatrixEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc. Valid values are MATRIX_ROW,...
virtual size_t getLocalNumRows() const =0
Returns the number of rows on this process.
virtual void getCRSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &colIds) const
Sets pointers to this process' matrix entries using compressed sparse row (CRS) format....
int getNumWeightsPerID() const override
Returns the number of weights per object. Number of weights per object should be zero or greater....
Created by mbenlioglu on Aug 31, 2020.
BaseAdapterType
An enum to identify general types of adapters.
@ MatrixAdapterType
matrix data
static ArrayRCP< ArrayRCP< zscalar_t > > weights
default_offset_t offset_t
The data type to represent offsets.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices.
default_part_t part_t
The data type to represent part numbers.
default_scalar_t scalar_t
The data type for weights and coordinates.