GEOSX
VectorBase.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
20 #define GEOS_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
21 
23 #include "common/MpiWrapper.hpp"
24 #include "common/GEOS_RAJA_Interface.hpp"
25 
26 namespace geos
27 {
28 
46 template< typename VECTOR >
48 {
49 protected:
50 
52  using Vector = VECTOR;
53 
58 
63  inline bool closed() const { return m_closed; }
64 
69  virtual bool created() const = 0;
70 
75  inline bool ready() const { return created() && closed(); }
76 
78 
83 
92  virtual void create( localIndex const localSize, MPI_Comm const & comm )
93  {
97  reset();
98 
99  // Ideally, resizing to the same size should be a no-op.
100  // But bufferManipulation::resize() always forces a touch in host memory.
101  // We want to avoid moving values to device again in that case.
102  if( m_values.size() != localSize )
103  {
104  m_values.resize( localSize );
105  }
106  }
107 
112  void setName( string const & name )
113  {
114  m_values.setName( name );
115  }
116 
118 
123 
129  {
130  GEOS_LAI_ASSERT( ready() );
131  m_closed = false;
132  return m_values.toView();
133  }
134 
140  virtual void close() = 0;
141 
148  virtual void touch() = 0;
149 
153  virtual void reset()
154  {
155  // Clearing the array causes issues on GPU when it is later resized again
156  // (the bug is in bufferManipulation::resize(), which calls buf.data() without
157  // moving the buffer to host, if a capacity increase does not occur, i.e. the new
158  // array size is exactly the same as the one prior to clearing).
159 
160  //m_values.clear();
161  m_closed = true;
162  };
163 
165 
170 
175  virtual void set( real64 const value ) = 0;
176 
180  virtual void zero()
181  {
182  set( 0.0 );
183  }
184 
189  virtual void rand( unsigned const seed ) = 0;
190 
192 
197 
202  virtual void scale( real64 const factor ) = 0;
203 
208  virtual void reciprocal() = 0;
209 
215  virtual real64 dot( Vector const & vec ) const = 0;
216 
223  virtual void copy( Vector const & x ) = 0;
224 
230  virtual void axpy( real64 const alpha,
231  Vector const & x ) = 0;
232 
239  virtual void axpby( real64 const alpha,
240  Vector const & x,
241  real64 const beta ) = 0;
242 
248  virtual void pointwiseProduct( Vector const & x,
249  Vector & y ) const = 0;
250 
255  virtual real64 norm1() const = 0;
256 
261  virtual real64 norm2() const = 0;
262 
267  virtual real64 normInf() const = 0;
268 
270 
275 
280  virtual globalIndex globalSize() const = 0;
281 
286  virtual localIndex localSize() const = 0;
287 
292  virtual globalIndex ilower() const = 0;
293 
299  virtual globalIndex iupper() const = 0;
300 
305  {
306  GEOS_LAI_ASSERT( ready() );
307  return m_values.toViewConst();
308  }
309 
314  virtual MPI_Comm comm() const = 0;
315 
317 
322 
327  virtual void print( std::ostream & os = std::cout ) const = 0;
328 
334  virtual void write( string const & filename,
335  LAIOutputFormat const format = LAIOutputFormat::MATRIX_MARKET ) const = 0;
336 
338 
345  friend std::ostream & operator<<( std::ostream & os, Vector const & vec )
346  {
347  vec.print( os );
348  return os;
349  }
350 
352  bool m_closed = true;
353 
356 };
357 
358 } // namespace geos
359 
360 #endif //GEOS_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
#define GEOS_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:83
Common base template for all vector wrapper types.
Definition: VectorBase.hpp:48
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:75
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:52
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:345
bool m_closed
Flag indicating whether the vector is closed.
Definition: VectorBase.hpp:352
virtual bool created() const =0
Query vector creation status.
array1d< real64 > m_values
Actual storage for the local vector values.
Definition: VectorBase.hpp:355
virtual void reciprocal()=0
Replace vector elements by their reciprocals.
virtual arrayView1d< real64 > open()
Open the vector for modifying entries.
Definition: VectorBase.hpp:128
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:304
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:180
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:92
virtual void reset()
Reset the vector to default state.
Definition: VectorBase.hpp:153
bool closed() const
Query vector closed status.
Definition: VectorBase.hpp:63
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:112
#define GEOS_LAI_ASSERT_GE(lhs, rhs)
Definition: common.hpp:68
#define GEOS_LAI_ASSERT(expr)
Definition: common.hpp:33
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:220
LAIOutputFormat
Definition: common.hpp:140
double real64
64-bit floating point type.
Definition: DataTypes.hpp:139
GEOSX_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:216