GEOSX
SparsityPattern.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 #include "SparsityPatternView.hpp"
16 
17 namespace LvArray
18 {
19 
26 template< typename COL_TYPE,
27  typename INDEX_TYPE,
28  template< typename > class BUFFER_TYPE >
29 class SparsityPattern : protected SparsityPatternView< COL_TYPE, INDEX_TYPE, BUFFER_TYPE >
30 {
31 
33  using ParentClass = SparsityPatternView< COL_TYPE, INDEX_TYPE, BUFFER_TYPE >;
34 
35 public:
36 
37  using typename ParentClass::ColType;
38  using typename ParentClass::IndexType;
39 
43 
51  inline
52  SparsityPattern( INDEX_TYPE const nrows=0,
53  INDEX_TYPE const ncols=0,
54  INDEX_TYPE initialRowCapacity=0 ):
55  ParentClass( true )
56  {
57  resize( nrows, ncols, initialRowCapacity );
58  setName( "" );
59  }
60 
65  inline
67  ParentClass( true )
68  { *this = src; }
69 
73  inline
74  SparsityPattern( SparsityPattern && ) = default;
75 
79  inline
81  { ParentClass::free(); }
82 
84 
88 
95  inline
97  {
98  this->m_numCols = src.m_numCols;
99  ParentClass::setEqualTo( src.m_numArrays,
100  src.m_offsets[ src.m_numArrays ],
101  src.m_offsets,
102  src.m_sizes,
103  src.m_values );
104  return *this;
105  }
106 
112  inline
114  {
116  ParentClass::operator=( std::move( src ) );
117  return *this;
118  }
119 
129  template< typename POLICY >
130  void resizeFromRowCapacities( INDEX_TYPE const nRows, INDEX_TYPE const nCols, INDEX_TYPE const * const rowCapacities )
131  {
132  LVARRAY_ERROR_IF( !arrayManipulation::isPositive( nCols ), "nCols must be positive." );
134  "COL_TYPE must be able to hold the range of columns: [0, " << nCols - 1 << "]." );
135 
136  this->m_numCols = nCols;
137  ParentClass::template resizeFromCapacities< POLICY >( nRows, rowCapacities );
138  }
139 
141 
145 
153  constexpr inline
155  toView() const &
156  { return ParentClass::toView(); }
157 
165  constexpr inline
167  toView() const && = delete;
168 
175  LVARRAY_HOST_DEVICE constexpr inline
177  toViewConst() const &
178  { return ParentClass::toViewConst(); }
179 
187  LVARRAY_HOST_DEVICE constexpr inline
189  toViewConst() const && = delete;
190 
192 
196 
198  using ParentClass::numRows;
202  using ParentClass::empty;
203 
205 
209 
213 
215 
219 
225  inline
226  void reserveRows( INDEX_TYPE const rowCapacity )
227  { ParentClass::reserve( rowCapacity ); }
228 
233  inline
234  void reserveNonZeros( INDEX_TYPE const nnz )
235  { ParentClass::reserveValues( nnz ); }
236 
243  inline
244  void reserveNonZeros( INDEX_TYPE const row, INDEX_TYPE const nnz )
245  {
246  if( nonZeroCapacity( row ) >= nnz ) return;
247  setRowCapacity( row, nnz );
248  }
249 
260  inline
261  void setRowCapacity( INDEX_TYPE const row, INDEX_TYPE newCapacity )
262  {
263  if( newCapacity > numColumns() ) newCapacity = numColumns();
264  ParentClass::setCapacityOfArray( row, newCapacity );
265  }
266 
272  inline
273  void compress()
274  { ParentClass::compress(); }
275 
277 
281 
293  void resize( INDEX_TYPE const nRows, INDEX_TYPE const nCols, INDEX_TYPE const initialRowCapacity )
294  { ParentClass::resize( nRows, nCols, initialRowCapacity ); }
295 
296 
301  inline
302  void appendRow( INDEX_TYPE const nzCapacity=0 )
303  {
304  INDEX_TYPE const maxOffset = this->m_offsets[ this->m_numArrays ];
305  bufferManipulation::emplaceBack( this->m_offsets, this->m_numArrays + 1, maxOffset );
307  ++this->m_numArrays;
308 
309  setRowCapacity( this->m_numArrays - 1, nzCapacity );
310  }
311 
313 
317 
325  inline
326  bool insertNonZero( INDEX_TYPE const row, COL_TYPE const col )
327  { return ParentClass::insertIntoSetImpl( row, col, CallBacks( *this, row ) ); }
328 
338  template< typename ITER >
339  inline
340  INDEX_TYPE insertNonZeros( INDEX_TYPE const row, ITER const first, ITER const last )
341  { return ParentClass::insertIntoSetImpl( row, first, last, CallBacks( *this, row ) ); }
342 
344 
349  template< typename ITER >
350  inline INDEX_TYPE
351  removeNonZeros( INDEX_TYPE const row, ITER const first, ITER const last ) const
352  { return ParentClass::removeNonZeros( row, first, last ); }
353 
355 
359 
367  void move( MemorySpace const space, bool const touch=true ) const
368  { return ParentClass::move( space, touch ); }
369 
371 
376  void setName( std::string const & name )
377  { ParentClass::template setName< decltype( *this ) >( name ); }
378 
379 private:
380 
388  inline
389  void dynamicallyGrowRow( INDEX_TYPE const row, INDEX_TYPE const newNNZ )
390  { setRowCapacity( row, newNNZ * 2 ); }
391 
396  class CallBacks : public sortedArrayManipulation::CallBacks< COL_TYPE >
397  {
398 public:
399 
405  inline
406  CallBacks( SparsityPattern & sp, INDEX_TYPE const row ):
407  m_sp( sp ),
408  m_row( row )
409  {}
410 
418  inline
419  COL_TYPE * incrementSize( COL_TYPE * const curPtr, INDEX_TYPE const nToAdd ) const
420  {
421  LVARRAY_UNUSED_VARIABLE( curPtr );
422  INDEX_TYPE const newNNZ = m_sp.numNonZeros( m_row ) + nToAdd;
423  if( newNNZ > m_sp.nonZeroCapacity( m_row ) )
424  {
425  m_sp.dynamicallyGrowRow( m_row, newNNZ );
426  }
427 
428  return m_sp.getSetValues( m_row );
429  }
430 
431 private:
433  SparsityPattern & m_sp;
434 
436  INDEX_TYPE const m_row;
437  };
438 };
439 
440 } /* namespace LvArray */
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
Definition: Macros.hpp:51
This class implements a compressed row storage sparsity pattern.
Definition: CRSMatrix.hpp:22
SparsityPattern & operator=(SparsityPattern const &src)
Copy assignment operator, performs a deep copy.
This class provides a view into a compressed row storage sparsity pattern.
void resize(INDEX_TYPE const nRows, INDEX_TYPE const nCols, INDEX_TYPE const initialRowCapacity)
Set the dimensions of the matrix.
constexpr INDEX_TYPE_NC numColumns() const
void emplaceBack(BUFFER &buf, std::ptrdiff_t const size, ARGS &&... args)
Construct a new value at the end of the buffer.
LvArray::SparsityPatternView< COL_INDEX, INDEX_TYPE const, LvArray::ChaiBuffer > SparsityPatternView
Alias for Sparsity pattern View.
Definition: DataTypes.hpp:323
void reserveRows(INDEX_TYPE const rowCapacity)
Reserve space for the given number of rows.
bool insertIntoSetImpl(INDEX_TYPE const i, COL_TYPE const &value, CALLBACKS &&cbacks) const
Helper function to insert a value into the given set.
constexpr SparsityPatternView< COL_TYPE, INDEX_TYPE const, BUFFER_TYPE > toView() const &
void reserve(INDEX_TYPE const newCapacity)
Reserve space for the given number of arrays.
#define LVARRAY_ERROR_IF(EXP, MSG)
Abort execution if EXP is true.
Definition: Macros.hpp:101
SparsityPattern & operator=(SparsityPattern &&src)
Default move assignment operator, performs a shallow copy.
constexpr SparsityPatternView< COL_TYPE, INDEX_TYPE const, BUFFER_TYPE > toView() const
SparsityPattern(INDEX_TYPE const nrows=0, INDEX_TYPE const ncols=0, INDEX_TYPE initialRowCapacity=0)
Constructor.
void appendRow(INDEX_TYPE const nzCapacity=0)
Append a row with the given capacity.
void reserveNonZeros(INDEX_TYPE const row, INDEX_TYPE const nnz)
Reserve space to hold at least the given number of non zero entries in the given row without either r...
void setRowCapacity(INDEX_TYPE const row, INDEX_TYPE newCapacity)
Set the non zero capacity of the given row.
constexpr INDEX_TYPE const * getOffsets() const
void compress(BUFFERS &... buffers)
Compress the arrays so that the values of each array are contiguous with no extra capacity in between...
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)
void resizeFromRowCapacities(INDEX_TYPE const nRows, INDEX_TYPE const nCols, INDEX_TYPE const *const rowCapacities)
Clears the matrix and creates a new matrix with the given number of rows and columns.
INDEX_TYPE insertNonZeros(INDEX_TYPE const row, ITER const first, ITER const last)
Inserts multiple non-zero entries into the given row.
constexpr INDEX_TYPE_NC numRows() const
void setName(std::string const &name)
Set the name associated with this SparsityPattern which is used in the chai callback.
void compress()
Compress the SparsityPattern so that the non-zeros of each row are contiguous with no extra capacity ...
constexpr INDEX_TYPE_NC nonZeroCapacity() const
void resize(INDEX_TYPE const nrows, INDEX_TYPE const ncols, INDEX_TYPE_NC initialRowCapacity, BUFFERS &... buffers)
Resize the SparsityPattern to the given size.
INDEX_TYPE removeNonZeros(INDEX_TYPE const row, ITER const first, ITER const last) const
Remove multiple non-zero entries from the given row.
constexpr ArraySlice< COL_TYPE const, 1, 0, INDEX_TYPE_NC > getColumns(INDEX_TYPE const row) const
This class provides a no-op callbacks interface for the ArrayManipulation sorted routines.
SparsityPattern(SparsityPattern const &src)
Copy constructor, performs a deep copy.
Contains the implementation of LvArray:SparsityPatternView.
~SparsityPattern()
Destructor, frees the values, sizes and offsets Buffers.
INDEX_TYPE IndexType
The integer type used for indexing.
constexpr SparsityPatternView< COL_TYPE const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const
MemorySpace
An enum containing the available memory spaces.
constexpr SparsityPatternView< COL_TYPE const, INDEX_TYPE const, BUFFER_TYPE > toViewConst() const &
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.
void setEqualTo(INDEX_TYPE const srcNumArrays, INDEX_TYPE const srcMaxOffset, BUFFER_TYPE< INDEX_TYPE > const &srcOffsets, BUFFER_TYPE< INDEX_TYPE > const &srcSizes, BUFFER_TYPE< COL_TYPE > const &srcValues, PAIRS_OF_BUFFERS &&... pairs)
Set this ArrayOfArraysView equal to the provided arrays.
COL_TYPE 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.
void free(BUFFERS &... buffers)
Destroy all the objects held by this array and free all associated memory.
bool insertNonZero(INDEX_TYPE const row, COL_TYPE const col)
Insert a non zero entry in the entry (row, col).
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
void setCapacityOfArray(INDEX_TYPE const i, INDEX_TYPE const newCapacity, BUFFERS &... buffers)
Set the capacity of the given array.
std::string string
String type.
Definition: DataTypes.hpp:131
LvArray::SparsityPattern< COL_INDEX, INDEX_TYPE, LvArray::ChaiBuffer > SparsityPattern
Alias for Sparsity pattern class.
Definition: DataTypes.hpp:319
constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > max(T const a, T const b)
Definition: math.hpp:46
void reserveValues(INDEX_TYPE const newValueCapacity, BUFFERS &... buffers)
Reserve space for the given number of values.
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:389
void move(MemorySpace const space, bool const touch=true) const
Move this ArrayOfSets to the given memory space.
void reserveNonZeros(INDEX_TYPE const nnz)
Reserve space to hold at least the given total number of non zero entries without reallocation...