GEOSX
SparsityPatternView.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
13 #pragma once
14 
15 // Source includes
16 #include "ArrayOfSetsView.hpp"
17 
18 // System includes
19 #include <limits>
20 
21 #ifdef LVARRAY_BOUNDS_CHECK
22 
28 #define SPARSITYPATTERN_COLUMN_CHECK( col ) \
29  LVARRAY_ERROR_IF( !arrayManipulation::isPositive( col ) || col >= this->numColumns(), \
30  "Column Check Failed: col=" << col << " numColumns=" << this->numColumns() )
31 
32 #else // LVARRAY_BOUNDS_CHECK
33 
39 #define SPARSITYPATTERN_COLUMN_CHECK( col )
40 
41 #endif
42 
43 namespace LvArray
44 {
45 
58 template< typename COL_TYPE,
59  typename INDEX_TYPE,
60  template< typename > class BUFFER_TYPE >
61 class SparsityPatternView : protected ArrayOfSetsView< COL_TYPE, INDEX_TYPE, BUFFER_TYPE >
62 {
63 protected:
66 
68  using typename ParentClass::INDEX_TYPE_NC;
69 
70  using typename ParentClass::SIZE_TYPE;
71 
72 public:
73  static_assert( std::is_integral< COL_TYPE >::value, "COL_TYPE must be integral." );
74  static_assert( std::is_integral< INDEX_TYPE >::value, "INDEX_TYPE must be integral." );
76  "INDEX_TYPE must be able to hold values at least as large as COL_TYPE." );
77 
79  using ColType = COL_TYPE;
80  using typename ParentClass::IndexType;
81 
85 
91  SparsityPatternView() = default;
92 
97  inline
98  SparsityPatternView( SparsityPatternView const & ) = default;
99 
104  inline
106  ParentClass( std::move( src ) ),
107  m_numCols( src.m_numCols )
108  { src.m_numCols = 0; }
109 
118  LVARRAY_HOST_DEVICE constexpr inline
119  SparsityPatternView( INDEX_TYPE const nRows,
120  INDEX_TYPE const nCols,
121  BUFFER_TYPE< INDEX_TYPE > const & offsets,
122  BUFFER_TYPE< SIZE_TYPE > const & nnz,
123  BUFFER_TYPE< COL_TYPE > const & columns ):
124  ParentClass( nRows, offsets, nnz, columns ),
125  m_numCols( nCols )
126  {}
127 
132  inline
133  SparsityPatternView & operator=( SparsityPatternView const & ) = default;
134 
140  inline
142  {
143  ParentClass::operator=( std::move( src ) );
144  m_numCols = src.m_numCols;
145  src.m_numCols = 0;
146  return *this;
147  }
148 
150 
154 
156 
160  LVARRAY_HOST_DEVICE constexpr inline
162  toView() const
163  {
165  numColumns(),
166  this->m_offsets,
167  this->m_sizes,
168  this->m_values );
169  }
170 
174  LVARRAY_HOST_DEVICE constexpr inline
176  toViewConst() const
177  {
179  numColumns(),
180  this->m_offsets,
181  this->m_sizes,
182  this->m_values );
183  }
184 
186 
190 
195  LVARRAY_HOST_DEVICE constexpr inline
197  { return ParentClass::size(); }
198 
202  LVARRAY_HOST_DEVICE constexpr inline
204  { return m_numCols; }
205 
209  LVARRAY_HOST_DEVICE inline
211  {
212  INDEX_TYPE_NC nnz = 0;
213  for( INDEX_TYPE_NC row = 0; row < numRows(); ++row )
214  {
215  nnz += numNonZeros( row );
216  }
217 
218  return nnz;
219  }
220 
225  LVARRAY_HOST_DEVICE constexpr inline
226  INDEX_TYPE_NC numNonZeros( INDEX_TYPE const row ) const
227  { return ParentClass::sizeOfSet( row ); }
228 
232  LVARRAY_HOST_DEVICE constexpr inline
234  { return ParentClass::valueCapacity(); }
235 
241  LVARRAY_HOST_DEVICE constexpr inline
242  INDEX_TYPE_NC nonZeroCapacity( INDEX_TYPE const row ) const
243  { return ParentClass::capacityOfSet( row ); }
244 
248  LVARRAY_HOST_DEVICE inline
249  bool empty() const
250  { return numNonZeros() == 0; }
251 
256  LVARRAY_HOST_DEVICE constexpr inline
257  bool empty( INDEX_TYPE const row ) const
258  { return numNonZeros( row ) == 0; }
259 
265  LVARRAY_HOST_DEVICE inline
266  bool empty( INDEX_TYPE const row, COL_TYPE const col ) const
267  { return !ParentClass::contains( row, col ); }
268 
270 
274 
280  LVARRAY_HOST_DEVICE constexpr inline
282  { return (*this)[row]; }
283 
287  LVARRAY_HOST_DEVICE constexpr inline
288  INDEX_TYPE const * getOffsets() const
289  { return this->m_offsets.data(); }
290 
292 
296 
306  LVARRAY_HOST_DEVICE inline
307  bool insertNonZero( INDEX_TYPE const row, COL_TYPE const col ) const
308  {
311  return ParentClass::insertIntoSet( row, col );
312  }
313 
325  template< typename ITER >
326  LVARRAY_HOST_DEVICE inline
327  INDEX_TYPE_NC insertNonZeros( INDEX_TYPE const row, ITER const first, ITER const last ) const
328  {
330 
331  #ifdef LVARRAY_BOUNDS_CHECK
332  for( ITER iter = first; iter != last; ++iter )
333  { SPARSITYPATTERN_COLUMN_CHECK( *iter ); }
334  #endif
335 
336  return ParentClass::insertIntoSet( row, first, last );
337  }
338 
345  LVARRAY_HOST_DEVICE inline
346  bool removeNonZero( INDEX_TYPE const row, COL_TYPE const col ) const
347  {
350  return ParentClass::removeFromSet( row, col );
351  }
352 
363  template< typename ITER >
364  LVARRAY_HOST_DEVICE inline
365  INDEX_TYPE_NC removeNonZeros( INDEX_TYPE const row, ITER const first, ITER const last ) const
366  {
368 
369  #ifdef LVARRAY_BOUNDS_CHECK
370  for( ITER iter = first; iter != last; ++iter )
371  { SPARSITYPATTERN_COLUMN_CHECK( *iter ); }
372  #endif
373 
374  return ParentClass::removeFromSet( row, first, last );
375  }
376 
378 
382 
390  void move( MemorySpace const space, bool const touch=true ) const
391  { return ParentClass::move( space, touch ); }
392 
394 
395 protected:
396 
402  ParentClass( true )
403  {};
404 
410  {
412  *this = std::move( src );
413  }
414 
423  template< class ... BUFFERS >
424  void resize( INDEX_TYPE const nrows,
425  INDEX_TYPE const ncols,
426  INDEX_TYPE_NC initialRowCapacity,
427  BUFFERS & ... buffers )
428  {
429  LVARRAY_ERROR_IF( !arrayManipulation::isPositive( nrows ), "nrows must be positive." );
430  LVARRAY_ERROR_IF( !arrayManipulation::isPositive( ncols ), "ncols must be positive." );
432  "COL_TYPE must be able to hold the range of columns: [0, " << ncols - 1 << "]." );
433 
434  m_numCols = ncols;
435  ParentClass::resizeImpl( nrows, initialRowCapacity, buffers ... );
436  }
437 
440 };
441 
442 } // namespace LvArray
#define SPARSITYPATTERN_COLUMN_CHECK(col)
Check that col is a valid column in the matrix.
This class provides a view into a compressed row storage sparsity pattern.
constexpr INDEX_TYPE_NC numColumns() const
bool empty(INDEX_TYPE const row, COL_TYPE const col) const
#define ARRAYOFARRAYS_CHECK_BOUNDS(i)
Check that i is a valid array index.
SparsityPatternView(SparsityPatternView &&src)
Move constructor.
constexpr SparsityPatternView(INDEX_TYPE const nRows, INDEX_TYPE const nCols, BUFFER_TYPE< INDEX_TYPE > const &offsets, BUFFER_TYPE< SIZE_TYPE > const &nnz, BUFFER_TYPE< COL_TYPE > const &columns)
Construct a new CRSMatrixView from the given buffers.
#define LVARRAY_ERROR_IF(EXP, MSG)
Abort execution if EXP is true.
Definition: Macros.hpp:101
This class serves to provide a sliced multidimensional interface to the family of LvArray classes...
Definition: ArraySlice.hpp:89
constexpr SparsityPatternView< COL_TYPE, INDEX_TYPE const, BUFFER_TYPE > toView() const
constexpr INDEX_TYPE_NC sizeOfSet(INDEX_TYPE const i) const
void assimilate(SparsityPatternView &&src)
Steal the resources of src, clearing it in the process.
constexpr INDEX_TYPE_NC nonZeroCapacity(INDEX_TYPE const row) const
bool insertIntoSet(INDEX_TYPE const i, COL_TYPE const &value) const
Insert a value into the given set.
constexpr INDEX_TYPE const * getOffsets() const
bool removeNonZero(INDEX_TYPE const row, COL_TYPE const col) const
Remove a non-zero entry at the given position.
constexpr std::enable_if< std::is_signed< INDEX_TYPE >::value, bool >::type isPositive(INDEX_TYPE const i)
constexpr bool empty(INDEX_TYPE const row) const
constexpr INDEX_TYPE_NC numRows() const
constexpr INDEX_TYPE_NC nonZeroCapacity() const
bool insertNonZero(INDEX_TYPE const row, COL_TYPE const col) const
Insert a non-zero entry at the given position.
void resize(INDEX_TYPE const nrows, INDEX_TYPE const ncols, INDEX_TYPE_NC initialRowCapacity, BUFFERS &... buffers)
Resize the SparsityPattern to the given size.
constexpr ArraySlice< COL_TYPE const, 1, 0, INDEX_TYPE_NC > getColumns(INDEX_TYPE const row) const
This class provides a view into an array of sets like object.
SparsityPatternView()=default
A constructor to create an uninitialized SparsityPatternView.
bool contains(INDEX_TYPE const i, COL_TYPE const &value) const
INDEX_TYPE IndexType
The integer type used for indexing.
void resizeImpl(INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity, BUFFERS &... buffers)
Set the number of arrays.
constexpr INDEX_TYPE_NC capacityOfSet(INDEX_TYPE const i) const
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
constexpr SparsityPatternView< COL_TYPE const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const
MemorySpace
An enum containing the available memory spaces.
The top level namespace.
Definition: Array.hpp:24
INDEX_TYPE_NC m_numCols
The number of columns in the matrix.
SparsityPatternView & operator=(SparsityPatternView const &)=default
Default copy assignment operator, this does a shallow copy.
globalIndex const ColType
The integer type used to enumerate the columns.
INDEX_TYPE_NC removeNonZeros(INDEX_TYPE const row, ITER const first, ITER const last) const
Remove multiple non-zero entries from the given row.
typename ParentClass::INDEX_TYPE_NC INDEX_TYPE_NC
Since INDEX_TYPE should always be const we need an alias for the non const version.
void free(BUFFERS &... buffers)
Destroy all the objects held by this array and free all associated memory.
std::conditional_t< CONST_SIZES, INDEX_TYPE const, INDEX_TYPE_NC > SIZE_TYPE
The type contained by the m_sizes buffer.
std::remove_const_t< INDEX_TYPE > INDEX_TYPE_NC
Since INDEX_TYPE should always be const we need an alias for the non const version.
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
SparsityPatternView & operator=(SparsityPatternView &&src)
Move assignment operator, this does a shallow copy.
ArrayOfSetsView & operator=(ArrayOfSetsView const &)=default
Default copy assignment operator, this does a shallow copy.
Contains the implementation of LvArray::ArrayOfSetsView.
constexpr INDEX_TYPE_NC numNonZeros(INDEX_TYPE const row) const
INDEX_TYPE_NC insertNonZeros(INDEX_TYPE const row, ITER const first, ITER const last) const
Inserts multiple non-zero entries into the given row.
bool removeFromSet(INDEX_TYPE const i, COL_TYPE const &value) const
Remove a value from the given set.
SparsityPatternView(bool)
Protected constructor to be used by parent classes.
#define DISABLE_HD_WARNING
Disable host device warnings.
Definition: Macros.hpp:401
constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > max(T const a, T const b)
Definition: math.hpp:46
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:389