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 Total, S.A
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 GEOSX_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
20 #define GEOSX_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
21 
22 #include "linearAlgebra/common.hpp"
23 #include "mpiCommunications/MpiWrapper.hpp"
24 #include "rajaInterface/GEOS_RAJA_Interface.hpp"
25 
26 namespace geosx
27 {
28 
46 template< typename VECTOR >
48 {
49 protected:
50 
52  using Vector = VECTOR;
53 
57 
63  : m_closed( true )
64  {}
65 
69  VectorBase( VectorBase const & ) = default;
70 
74  VectorBase( VectorBase && ) = default;
75 
80  VectorBase & operator=( VectorBase const & ) = default;
81 
86  VectorBase & operator=( VectorBase && ) = default;
87 
91  ~VectorBase() = default;
92 
94 
98 
104  inline bool closed() const { return m_closed; }
105 
110  virtual bool created() const = 0;
111 
116  inline bool ready() const { return created() && closed(); }
117 
119 
123 
134  virtual void createWithLocalSize( localIndex const localSize, MPI_Comm const & comm ) = 0;
135 
145  virtual void createWithGlobalSize( globalIndex const globalSize, MPI_Comm const & comm ) = 0;
146 
152  virtual void create( arrayView1d< real64 const > const & localValues, MPI_Comm const & comm ) = 0;
153 
155 
159 
164  virtual void open() = 0;
165 
171  virtual void close() = 0;
172 
176  virtual void reset()
177  {
178  m_closed = true;
179  };
180 
182 
186 
195  virtual void set( globalIndex const globalRow,
196  real64 const value ) = 0;
197 
205  virtual void add( globalIndex const globalRow,
206  real64 const value ) = 0;
207 
216  virtual void set( globalIndex const * globalIndices,
217  real64 const * values,
218  localIndex const size ) = 0;
219 
228  virtual void add( globalIndex const * globalIndices,
229  real64 const * values,
230  localIndex const size ) = 0;
231 
239  virtual void set( arraySlice1d< globalIndex const > const & globalIndices,
240  arraySlice1d< real64 const > const & values ) = 0;
241 
242 
250  virtual void add( arraySlice1d< globalIndex const > const & globalIndices,
251  arraySlice1d< real64 const > const & values ) = 0;
252 
257  virtual void set( real64 const value ) = 0;
258 
262  virtual void zero() = 0;
263 
268  virtual void rand( unsigned const seed = 1984 ) = 0;
269 
271 
275 
281  virtual void scale( real64 const scalingFactor ) = 0;
282 
287  virtual void reciprocal() = 0;
288 
294  virtual real64 dot( Vector const & vec ) const = 0;
295 
300  virtual void copy( Vector const & x ) = 0;
301 
307  virtual void axpy( real64 const alpha,
308  Vector const & x ) = 0;
309 
316  virtual void axpby( real64 const alpha,
317  Vector const & x,
318  real64 const beta ) = 0;
319 
325  virtual void pointwiseProduct( Vector const & x,
326  Vector & y ) const = 0;
327 
332  virtual real64 norm1() const = 0;
333 
338  virtual real64 norm2() const = 0;
339 
344  virtual real64 normInf() const = 0;
345 
347 
351 
357  virtual globalIndex globalSize() const = 0;
358 
363  virtual localIndex localSize() const = 0;
364 
369  virtual globalIndex ilower() const = 0;
370 
376  virtual globalIndex iupper() const = 0;
377 
383  virtual real64 get( globalIndex globalRow ) const = 0;
384 
390  virtual void get( arraySlice1d< globalIndex const > const & globalIndices,
391  arraySlice1d< real64 > const & values ) const = 0;
392 
398  virtual localIndex getLocalRowID( globalIndex const globalRow ) const = 0;
399 
405  virtual globalIndex getGlobalRowID( localIndex const localRow ) const = 0;
406 
411  virtual real64 const * extractLocalVector() const = 0;
412 
417  virtual real64 * extractLocalVector() = 0;
418 
423  virtual void extract( arrayView1d< real64 > const & localVector ) const
424  {
425  real64 const * const data = extractLocalVector();
426  forAll< parallelHostPolicy >( localSize(), [=] ( localIndex const k )
427  {
428  localVector[k] = data[k];
429  } );
430  }
431 
436  virtual MPI_Comm getComm() const = 0;
437 
439 
443 
449  virtual void print( std::ostream & os = std::cout ) const = 0;
450 
456  virtual void write( string const & filename,
457  LAIOutputFormat const format = LAIOutputFormat::MATRIX_MARKET ) const = 0;
458 
460 
467  friend std::ostream & operator<<( std::ostream & os, Vector const & vec )
468  {
469  vec.print( os );
470  return os;
471  }
472 
474  bool m_closed;
475 };
476 
477 } // namespace geosx
478 
479 #endif //GEOSX_LINEARALGEBRA_INTERFACES_VECTORBASE_HPP_
Common base template for all vector wrapper types.
Definition: VectorBase.hpp:47
virtual globalIndex ilower() const =0
Get lower bound of local partition.
VectorBase & operator=(VectorBase const &)=default
Copy assignment.
virtual void copy(Vector const &x)=0
Update vector y as y = x.
virtual void extract(arrayView1d< real64 > const &localVector) const
Extract local solution by copying into a user-provided array.
Definition: VectorBase.hpp:423
long long int globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
virtual void createWithGlobalSize(globalIndex const globalSize, MPI_Comm const &comm)=0
Create a vector based on global number of elements.
virtual void print(std::ostream &os=std::cout) const override
Print the vector in Trilinos format to the terminal.
virtual real64 norm2() const =0
2-norm of the vector.
virtual void reciprocal()=0
Replace vector elements by their reciprocals.
This class serves to provide a sliced multidimensional interface to the family of LvArray classes...
Definition: ArraySlice.hpp:89
virtual real64 const * extractLocalVector() const =0
Extract a view of the local portion of the array.
bool closed() const
Query vector closed status.
Definition: VectorBase.hpp:104
std::string format(int NDIM, INDEX_TYPE const *const dims)
This function returns a string that may be used as the "type" in a call to TV_ttf_add_row(). This will either be a single value or an array.
Definition: tv_helpers.hpp:37
~VectorBase()=default
Destructor.
virtual localIndex getLocalRowID(globalIndex const globalRow) const =0
Map a global row index to local row index.
virtual globalIndex globalSize() const =0
Returns the global of the vector.
virtual globalIndex getGlobalRowID(localIndex const localRow) const =0
Map a local row index to global row index.
This class serves to provide a "view" of a multidimensional array.
Definition: ArrayView.hpp:67
virtual void axpby(real64 const alpha, Vector const &x, real64 const beta)=0
Update vector y as y = alpha*x + beta*y.
double real64
64-bit floating point type.
Definition: DataTypes.hpp:136
virtual real64 dot(Vector const &vec) const =0
Dot product with the vector vec.
virtual void open()=0
Open the vector for modifying entries.
virtual void add(globalIndex const globalRow, real64 const value)=0
Add into vector value.
virtual localIndex localSize() const =0
Returns the local size of the vector.
virtual void scale(real64 const scalingFactor)=0
Multiply all elements by scalingFactor.
LAIOutputFormat
Definition: common.hpp:139
virtual real64 normInf() const =0
Infinity-norm of the vector.
virtual void axpy(real64 const alpha, Vector const &x)=0
Update vector y as y = alpha*x + y.
virtual void createWithLocalSize(localIndex const localSize, MPI_Comm const &comm)=0
Create a vector based on local number of elements.
bool m_closed
Flag indicating whether the vector is closed.
Definition: VectorBase.hpp:474
virtual void pointwiseProduct(Vector const &x, Vector &y) const =0
Compute the componentwise multiplication y = v * x.
std::ptrdiff_t localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
VectorBase()
Constructs a vector in default state.
Definition: VectorBase.hpp:62
virtual void print(std::ostream &os=std::cout) const =0
Print the vector in Trilinos format to the terminal.
virtual globalIndex iupper() const =0
Get upper bound of local partition.
virtual void create(arrayView1d< real64 const > const &localValues, MPI_Comm const &comm)=0
Construct parallel vector from a local array.
virtual void zero()=0
Set vector elements to zero.
virtual void rand(unsigned const seed=1984)=0
Set vector elements to random entries.
virtual void reset()
Reset the matrix to default state.
Definition: VectorBase.hpp:176
virtual void write(string const &filename, LAIOutputFormat const format=LAIOutputFormat::MATRIX_MARKET) const =0
Write the vector to a file.
This class creates and provides basic support for the Epetra_FEVector vector object type used in Tril...
virtual MPI_Comm getComm() const =0
Get the communicator used by this vector.
virtual void close()=0
Assemble vector.
virtual real64 norm1() const =0
1-norm of the vector.
friend std::ostream & operator<<(std::ostream &os, Vector const &vec)
Stream insertion operator for all vector types.
Definition: VectorBase.hpp:467
bool ready() const
Query vector ready status.
Definition: VectorBase.hpp:116
virtual bool created() const =0
Query vector creation status.