GEOS
BlockVectorView.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_UTILITIES_BLOCKVECTORVIEW_HPP_
21 #define GEOS_LINEARALGEBRA_UTILITIES_BLOCKVECTORVIEW_HPP_
22 
23 #include "common/common.hpp"
25 
26 namespace geos
27 {
28 
36 template< typename VECTOR >
38 {
39 public:
40 
42  using Vector = VECTOR;
43 
46 
52  BlockVectorView & operator=( BlockVectorView const & rhs ) = delete;
53 
59  BlockVectorView & operator=( BlockVectorView && rhs ) noexcept = delete;
60 
64  virtual ~BlockVectorView() = default;
65 
67 
70 
75  void copy( BlockVectorView const & src );
76 
78 
81 
86  void scale( real64 const factor );
87 
91  void zero();
92 
97  void rand( unsigned const seed );
98 
104  real64 dot( BlockVectorView const & x ) const;
105 
110  real64 norm2() const;
111 
116  real64 normInf() const;
117 
124  void axpy( real64 const alpha,
125  BlockVectorView const & x );
126 
135  void axpby( real64 const alpha,
136  BlockVectorView const & x,
137  real64 const beta );
138 
140 
143 
148  localIndex blockSize() const;
149 
154  globalIndex globalSize() const;
155 
160  localIndex localSize() const;
161 
166  void print( std::ostream & os = std::cout ) const;
167 
173  VECTOR const & block( localIndex const blockIndex ) const
174  {
175  GEOS_LAI_ASSERT( m_vectors[blockIndex] != nullptr );
176  return *m_vectors[blockIndex];
177  }
178 
182  VECTOR & block( localIndex const blockIndex )
183  {
184  GEOS_LAI_ASSERT( m_vectors[blockIndex] != nullptr );
185  return *m_vectors[blockIndex];
186  }
187 
191  VECTOR const & operator()( localIndex const blockIndex ) const
192  {
193  return block( blockIndex );
194  }
195 
199  VECTOR & operator()( localIndex const blockIndex )
200  {
201  return block( blockIndex );
202  }
203 
205 
206 protected:
207 
215 
220  explicit BlockVectorView( localIndex const nBlocks )
221  : m_vectors( nBlocks )
222  { }
223 
227  BlockVectorView( BlockVectorView const & ) = default;
228 
233 
235 
240  void resize( localIndex const size )
241  {
242  m_vectors.resize( size );
243  }
244 
250  void setPointer( localIndex i, VECTOR * vec )
251  {
252  m_vectors( i ) = vec;
253  }
254 
255 private:
256 
258  array1d< VECTOR * > m_vectors;
259 };
260 
261 template< typename VECTOR >
263 {
264  GEOS_LAI_ASSERT_EQ( blockSize(), src.blockSize() );
265  for( localIndex i = 0; i < blockSize(); i++ )
266  {
267  block( i ).copy( src.block( i ) );
268  }
269 }
270 
271 template< typename VECTOR >
273 {
274  for( localIndex i = 0; i < blockSize(); i++ )
275  {
276  block( i ).scale( factor );
277  }
278 }
279 
280 template< typename VECTOR >
282 {
283  for( localIndex i = 0; i < blockSize(); i++ )
284  {
285  block( i ).zero();
286  }
287 }
288 
289 template< typename VECTOR >
290 void BlockVectorView< VECTOR >::rand( unsigned const seed )
291 {
292  for( localIndex i = 0; i < blockSize(); i++ )
293  {
294  block( i ).rand( seed );
295  }
296 }
297 
298 template< typename VECTOR >
300 {
301  GEOS_LAI_ASSERT_EQ( blockSize(), src.blockSize() );
302  real64 accum = 0;
303  for( localIndex i = 0; i < blockSize(); i++ )
304  {
305  accum += block( i ).dot( src.block( i ) );
306  }
307  return accum;
308 }
309 
310 template< typename VECTOR >
312 {
313  real64 accum = 0;
314  for( localIndex i = 0; i < blockSize(); i++ )
315  {
316  real64 const temp = block( i ).norm2();
317  accum = accum + temp * temp;
318  }
319  return std::sqrt( accum );
320 }
321 
322 template< typename VECTOR >
324 {
325  real64 result = 0.0;
326  for( localIndex i = 0; i < blockSize(); i++ )
327  {
328  result = std::fmax( result, block( i ).normInf() );
329  }
330  return result;
331 }
332 
333 template< typename VECTOR >
335  BlockVectorView const & x )
336 {
337  GEOS_LAI_ASSERT_EQ( blockSize(), x.blockSize() );
338  for( localIndex i = 0; i < blockSize(); i++ )
339  {
340  block( i ).axpy( alpha, x.block( i ) );
341  }
342 }
343 
344 template< typename VECTOR >
346  BlockVectorView const & x,
347  real64 const beta )
348 {
349  GEOS_LAI_ASSERT_EQ( blockSize(), x.blockSize() );
350  for( localIndex i = 0; i < blockSize(); i++ )
351  {
352  block( i ).axpby( alpha, x.block( i ), beta );
353  }
354 }
355 
356 template< typename VECTOR >
358 {
359  return m_vectors.size();
360 }
361 
362 template< typename VECTOR >
364 {
365  globalIndex size = 0;
366  for( localIndex i = 0; i < blockSize(); i++ )
367  {
368  size += block( i ).globalSize();
369  }
370  return size;
371 }
372 
373 template< typename VECTOR >
375 {
376  localIndex size = 0;
377  for( localIndex i = 0; i < blockSize(); i++ )
378  {
379  size += block( i ).localSize();
380  }
381  return size;
382 }
383 
384 template< typename VECTOR >
385 void BlockVectorView< VECTOR >::print( std::ostream & os ) const
386 {
387  for( localIndex i = 0; i < m_vectors.size(); i++ )
388  {
389  os << "Block " << i << " of " << blockSize() << ":" << std::endl;
390  os << "=============" << std::endl;
391  block( i ).print( os );
392  }
393 }
394 
401 template< typename VECTOR >
402 std::ostream & operator<<( std::ostream & os,
403  BlockVectorView< VECTOR > const & vec )
404 {
405  vec.print( os );
406  return os;
407 }
408 
409 } // end geos namespace
410 
411 
412 #endif /* GEOS_LINEARALGEBRA_UTILITIES_BLOCKVECTORVIEW_HPP_ */
Abstract view of a block vector.
void rand(unsigned const seed)
Set vector elements to random entries.
VECTOR const & block(localIndex const blockIndex) const
Get a reference to the vector corresponding to block blockRowIndex.
BlockVectorView & operator=(BlockVectorView &&rhs) noexcept=delete
Deleted move assignment.
BlockVectorView & operator=(BlockVectorView const &rhs)=delete
Deleted copy assignment.
VECTOR & operator()(localIndex const blockIndex)
Get a reference to the vector corresponding to block blockRowIndex.
void setPointer(localIndex i, VECTOR *vec)
Set pointer to a vector.
VECTOR Vector
Alias for sub-vector type.
VECTOR & block(localIndex const blockIndex)
Get a reference to the vector corresponding to block blockRowIndex.
localIndex localSize() const
Get local size.
void print(std::ostream &os=std::cout) const
Print the block vector.
void scale(real64 const factor)
Scale the block vector with factor.
globalIndex globalSize() const
Get global size.
real64 norm2() const
2-norm of the block vector.
void axpy(real64 const alpha, BlockVectorView const &x)
Update vector y as y = alpha*x + y.
virtual ~BlockVectorView()=default
Destructor.
void zero()
Set the vector to zero.
void copy(BlockVectorView const &src)
Update vector y as y = x.
real64 normInf() const
Inf-norm of the block vector.
void resize(localIndex const size)
Resize to a new number of blocks.
VECTOR const & operator()(localIndex const blockIndex) const
Get a reference to the vector corresponding to block blockRowIndex.
BlockVectorView(localIndex const nBlocks)
Create a vector of nBlocks blocks.
BlockVectorView(BlockVectorView const &)=default
Copy constructor.
real64 dot(BlockVectorView const &x) const
Dot product.
BlockVectorView(BlockVectorView &&)=default
Move constructor.
void axpby(real64 const alpha, BlockVectorView const &x, real64 const beta)
Update vector y as y = alpha*x + beta*y.
localIndex blockSize() const
Get block size.
#define GEOS_LAI_ASSERT_EQ(lhs, rhs)
Definition: common.hpp:49
#define GEOS_LAI_ASSERT(expr)
Definition: common.hpp:35
GEOS_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:88
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
std::ostream & operator<<(std::ostream &stream, mapBase< K, V, SORTED > const &map)
Stream output operator for map types.
Definition: DataTypes.hpp:356
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:176