GEOSX
BlockVectorView.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_UTILITIES_BLOCKVECTORVIEW_HPP_
20 #define GEOS_LINEARALGEBRA_UTILITIES_BLOCKVECTORVIEW_HPP_
21 
22 #include "common/common.hpp"
24 
25 namespace geos
26 {
27 
35 template< typename VECTOR >
37 {
38 public:
39 
41  using Vector = VECTOR;
42 
45 
51  BlockVectorView & operator=( BlockVectorView const & rhs ) = delete;
52 
58  BlockVectorView & operator=( BlockVectorView && rhs ) noexcept = delete;
59 
63  virtual ~BlockVectorView() = default;
64 
66 
69 
74  void copy( BlockVectorView const & src );
75 
77 
80 
85  void scale( real64 const factor );
86 
90  void zero();
91 
96  void rand( unsigned const seed );
97 
103  real64 dot( BlockVectorView const & x ) const;
104 
109  real64 norm2() const;
110 
115  real64 normInf() const;
116 
123  void axpy( real64 const alpha,
124  BlockVectorView const & x );
125 
134  void axpby( real64 const alpha,
135  BlockVectorView const & x,
136  real64 const beta );
137 
139 
142 
147  localIndex blockSize() const;
148 
153  globalIndex globalSize() const;
154 
159  localIndex localSize() const;
160 
165  void print( std::ostream & os = std::cout ) const;
166 
172  VECTOR const & block( localIndex const blockIndex ) const
173  {
174  GEOS_LAI_ASSERT( m_vectors[blockIndex] != nullptr );
175  return *m_vectors[blockIndex];
176  }
177 
181  VECTOR & block( localIndex const blockIndex )
182  {
183  GEOS_LAI_ASSERT( m_vectors[blockIndex] != nullptr );
184  return *m_vectors[blockIndex];
185  }
186 
190  VECTOR const & operator()( localIndex const blockIndex ) const
191  {
192  return block( blockIndex );
193  }
194 
198  VECTOR & operator()( localIndex const blockIndex )
199  {
200  return block( blockIndex );
201  }
202 
204 
205 protected:
206 
214 
219  explicit BlockVectorView( localIndex const nBlocks )
220  : m_vectors( nBlocks )
221  { }
222 
226  BlockVectorView( BlockVectorView const & ) = default;
227 
232 
234 
239  void resize( localIndex const size )
240  {
241  m_vectors.resize( size );
242  }
243 
249  void setPointer( localIndex i, VECTOR * vec )
250  {
251  m_vectors( i ) = vec;
252  }
253 
254 private:
255 
257  array1d< VECTOR * > m_vectors;
258 };
259 
260 template< typename VECTOR >
262 {
263  GEOS_LAI_ASSERT_EQ( blockSize(), src.blockSize() );
264  for( localIndex i = 0; i < blockSize(); i++ )
265  {
266  block( i ).copy( src.block( i ) );
267  }
268 }
269 
270 template< typename VECTOR >
272 {
273  for( localIndex i = 0; i < blockSize(); i++ )
274  {
275  block( i ).scale( factor );
276  }
277 }
278 
279 template< typename VECTOR >
281 {
282  for( localIndex i = 0; i < blockSize(); i++ )
283  {
284  block( i ).zero();
285  }
286 }
287 
288 template< typename VECTOR >
289 void BlockVectorView< VECTOR >::rand( unsigned const seed )
290 {
291  for( localIndex i = 0; i < blockSize(); i++ )
292  {
293  block( i ).rand( seed );
294  }
295 }
296 
297 template< typename VECTOR >
299 {
300  GEOS_LAI_ASSERT_EQ( blockSize(), src.blockSize() );
301  real64 accum = 0;
302  for( localIndex i = 0; i < blockSize(); i++ )
303  {
304  accum += block( i ).dot( src.block( i ) );
305  }
306  return accum;
307 }
308 
309 template< typename VECTOR >
311 {
312  real64 accum = 0;
313  for( localIndex i = 0; i < blockSize(); i++ )
314  {
315  real64 const temp = block( i ).norm2();
316  accum = accum + temp * temp;
317  }
318  return std::sqrt( accum );
319 }
320 
321 template< typename VECTOR >
323 {
324  real64 result = 0.0;
325  for( localIndex i = 0; i < blockSize(); i++ )
326  {
327  result = std::fmax( result, block( i ).normInf() );
328  }
329  return result;
330 }
331 
332 template< typename VECTOR >
334  BlockVectorView const & x )
335 {
336  GEOS_LAI_ASSERT_EQ( blockSize(), x.blockSize() );
337  for( localIndex i = 0; i < blockSize(); i++ )
338  {
339  block( i ).axpy( alpha, x.block( i ) );
340  }
341 }
342 
343 template< typename VECTOR >
345  BlockVectorView const & x,
346  real64 const beta )
347 {
348  GEOS_LAI_ASSERT_EQ( blockSize(), x.blockSize() );
349  for( localIndex i = 0; i < blockSize(); i++ )
350  {
351  block( i ).axpby( alpha, x.block( i ), beta );
352  }
353 }
354 
355 template< typename VECTOR >
357 {
358  return m_vectors.size();
359 }
360 
361 template< typename VECTOR >
363 {
364  globalIndex size = 0;
365  for( localIndex i = 0; i < blockSize(); i++ )
366  {
367  size += block( i ).globalSize();
368  }
369  return size;
370 }
371 
372 template< typename VECTOR >
374 {
375  localIndex size = 0;
376  for( localIndex i = 0; i < blockSize(); i++ )
377  {
378  size += block( i ).localSize();
379  }
380  return size;
381 }
382 
383 template< typename VECTOR >
384 void BlockVectorView< VECTOR >::print( std::ostream & os ) const
385 {
386  for( localIndex i = 0; i < m_vectors.size(); i++ )
387  {
388  os << "Block " << i << " of " << blockSize() << ":" << std::endl;
389  os << "=============" << std::endl;
390  block( i ).print( os );
391  }
392 }
393 
400 template< typename VECTOR >
401 std::ostream & operator<<( std::ostream & os,
402  BlockVectorView< VECTOR > const & vec )
403 {
404  vec.print( os );
405  return os;
406 }
407 
408 } // end geosx namespace
409 
410 
411 #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:47
#define GEOS_LAI_ASSERT(expr)
Definition: common.hpp:33
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
std::ostream & operator<<(std::ostream &stream, mapBase< K, V, SORTED > const &map)
Stream output operator for map types.
Definition: DataTypes.hpp:396
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