GEOS
VectorBase.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
21 #define GEOS_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
22 
24 #include "common/MpiWrapper.hpp"
25 #include "common/GEOS_RAJA_Interface.hpp"
26 
27 namespace geos
28 {
29 
47 template< typename VECTOR >
49 {
50 protected:
51 
53  using Vector = VECTOR;
54 
59 
64  inline bool closed() const { return m_closed; }
65 
70  virtual bool created() const = 0;
71 
76  inline bool ready() const { return created() && closed(); }
77 
79 
84 
93  virtual void create( localIndex const localSize, MPI_Comm const & comm )
94  {
98  reset();
99 
100  // Ideally, resizing to the same size should be a no-op.
101  // But bufferManipulation::resize() always forces a touch in host memory.
102  // We want to avoid moving values to device again in that case.
103  if( m_values.size() != localSize )
104  {
105  m_values.resize( localSize );
106  }
107  }
108 
113  void setName( string const & name )
114  {
115  m_values.setName( name );
116  }
117 
119 
124 
130  {
131  GEOS_LAI_ASSERT( ready() );
132  m_closed = false;
133  return m_values.toView();
134  }
135 
141  virtual void close() = 0;
142 
149  virtual void touch() = 0;
150 
154  virtual void reset()
155  {
156  // Clearing the array causes issues on GPU when it is later resized again
157  // (the bug is in bufferManipulation::resize(), which calls buf.data() without
158  // moving the buffer to host, if a capacity increase does not occur, i.e. the new
159  // array size is exactly the same as the one prior to clearing).
160 
161  //m_values.clear();
162  m_closed = true;
163  };
164 
166 
171 
176  virtual void set( real64 const value ) = 0;
177 
181  virtual void zero()
182  {
183  set( 0.0 );
184  }
185 
190  virtual void rand( unsigned const seed ) = 0;
191 
193 
198 
203  virtual void scale( real64 const factor ) = 0;
204 
209  virtual void reciprocal() = 0;
210 
216  virtual real64 dot( Vector const & vec ) const = 0;
217 
224  virtual void copy( Vector const & x ) = 0;
225 
231  virtual void axpy( real64 const alpha,
232  Vector const & x ) = 0;
233 
240  virtual void axpby( real64 const alpha,
241  Vector const & x,
242  real64 const beta ) = 0;
243 
249  virtual void pointwiseProduct( Vector const & x,
250  Vector & y ) const = 0;
251 
256  virtual real64 norm1() const = 0;
257 
262  virtual real64 norm2() const = 0;
263 
268  virtual real64 normInf() const = 0;
269 
271 
276 
281  virtual globalIndex globalSize() const = 0;
282 
287  virtual localIndex localSize() const = 0;
288 
293  virtual globalIndex ilower() const = 0;
294 
300  virtual globalIndex iupper() const = 0;
301 
306  {
307  GEOS_LAI_ASSERT( ready() );
308  return m_values.toViewConst();
309  }
310 
315  virtual MPI_Comm comm() const = 0;
316 
318 
323 
328  virtual void print( std::ostream & os = std::cout ) const = 0;
329 
335  virtual void write( string const & filename,
336  LAIOutputFormat const format = LAIOutputFormat::MATRIX_MARKET ) const = 0;
337 
339 
346  friend std::ostream & operator<<( std::ostream & os, Vector const & vec )
347  {
348  vec.print( os );
349  return os;
350  }
351 
353  bool m_closed = true;
354 
357 };
358 
359 } // namespace geos
360 
361 #endif //GEOS_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
#define GEOS_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:84
Common base template for all vector wrapper types.
Definition: VectorBase.hpp:49
virtual void axpy(real64 const alpha, Vector const &x)=0
Update vector y as y = alpha*x + y.
virtual void touch()=0
Notify the vector about external modification through direct data pointer.
virtual void axpby(real64 const alpha, Vector const &x, real64 const beta)=0
Update vector y as y = alpha*x + beta*y.
bool ready() const
Query vector ready status.
Definition: VectorBase.hpp:76
virtual void write(string const &filename, LAIOutputFormat const format=LAIOutputFormat::MATRIX_MARKET) const =0
Write the vector to a file.
virtual void close()=0
Close vector for modification.
virtual void pointwiseProduct(Vector const &x, Vector &y) const =0
Compute the component-wise multiplication y = v * x.
VECTOR Vector
Alias for VECTOR.
Definition: VectorBase.hpp:53
virtual void rand(unsigned const seed)=0
Set vector elements to random entries.
friend std::ostream & operator<<(std::ostream &os, Vector const &vec)
Stream insertion operator for all vector types.
Definition: VectorBase.hpp:346
bool m_closed
Flag indicating whether the vector is closed.
Definition: VectorBase.hpp:353
virtual bool created() const =0
Query vector creation status.
array1d< real64 > m_values
Actual storage for the local vector values.
Definition: VectorBase.hpp:356
virtual void reciprocal()=0
Replace vector elements by their reciprocals.
virtual arrayView1d< real64 > open()
Open the vector for modifying entries.
Definition: VectorBase.hpp:129
virtual real64 normInf() const =0
Infinity-norm of the vector.
virtual globalIndex ilower() const =0
Get lower bound of local partition.
virtual void scale(real64 const factor)=0
Multiply all elements by factor.
virtual MPI_Comm comm() const =0
Get the communicator used by this vector.
arrayView1d< real64 const > values() const
Definition: VectorBase.hpp:305
virtual globalIndex globalSize() const =0
Returns the global of the vector.
virtual localIndex localSize() const =0
Returns the local size of the vector.
virtual real64 dot(Vector const &vec) const =0
Dot product with the vector vec.
virtual void set(real64 const value)=0
Set all elements to a constant value.
virtual void zero()
Set vector elements to zero.
Definition: VectorBase.hpp:181
virtual globalIndex iupper() const =0
Get upper bound of local partition.
virtual real64 norm2() const =0
2-norm of the vector.
virtual real64 norm1() const =0
1-norm of the vector.
virtual void create(localIndex const localSize, MPI_Comm const &comm)
Create a vector based on local number of elements.
Definition: VectorBase.hpp:93
virtual void reset()
Reset the vector to default state.
Definition: VectorBase.hpp:154
bool closed() const
Query vector closed status.
Definition: VectorBase.hpp:64
virtual void print(std::ostream &os=std::cout) const =0
Print the vector in Trilinos format to the terminal.
virtual void copy(Vector const &x)=0
Update vector y as y = x.
void setName(string const &name)
Set a name for the vector (mainly used during various logging).
Definition: VectorBase.hpp:113
#define GEOS_LAI_ASSERT_GE(lhs, rhs)
Definition: common.hpp:70
#define GEOS_LAI_ASSERT(expr)
Definition: common.hpp:35
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:180
GEOS_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:88
LAIOutputFormat
Definition: common.hpp:142
double real64
64-bit floating point type.
Definition: DataTypes.hpp:99
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:85
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:176