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 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_UTILITIES_BLOCKVECTORVIEW_HPP_
20 #define GEOSX_LINEARALGEBRA_UTILITIES_BLOCKVECTORVIEW_HPP_
21 
22 #include "linearAlgebra/common.hpp"
23 
24 namespace geosx
25 {
26 
34 template< typename VECTOR >
35 class BlockVectorView
36 {
37 public:
38 
40  using Vector = VECTOR;
41 
44 
50  BlockVectorView & operator=( BlockVectorView const & rhs ) = delete;
51 
57  BlockVectorView & operator=( BlockVectorView && rhs ) noexcept = delete;
58 
62  virtual ~BlockVectorView() = default;
63 
65 
68 
73  void copy( BlockVectorView const & src );
74 
76 
79 
84  void scale( real64 const factor );
85 
89  void zero();
90 
95  void rand( unsigned const seed = 1984 );
96 
102  real64 dot( BlockVectorView const & x ) const;
103 
108  real64 norm2() const;
109 
114  real64 normInf() const;
115 
122  void axpy( real64 const alpha,
123  BlockVectorView const & x );
124 
133  void axpby( real64 const alpha,
134  BlockVectorView const & x,
135  real64 const beta );
136 
138 
141 
146  localIndex blockSize() const;
147 
152  globalIndex globalSize() const;
153 
158  localIndex localSize() const;
159 
164  void print( std::ostream & os = std::cout ) const;
165 
171  VECTOR const & block( localIndex const blockIndex ) const
172  {
173  GEOSX_LAI_ASSERT( m_vectors[blockIndex] != nullptr );
174  return *m_vectors[blockIndex];
175  }
176 
180  VECTOR & block( localIndex const blockIndex )
181  {
182  GEOSX_LAI_ASSERT( m_vectors[blockIndex] != nullptr );
183  return *m_vectors[blockIndex];
184  }
185 
189  VECTOR const & operator()( localIndex const blockIndex ) const
190  {
191  return block( blockIndex );
192  }
193 
197  VECTOR & operator()( localIndex const blockIndex )
198  {
199  return block( blockIndex );
200  }
201 
203 
204 protected:
205 
212 
218  explicit BlockVectorView( localIndex const nBlocks )
219  : m_vectors( nBlocks )
220  { }
221 
225  BlockVectorView( BlockVectorView const & ) = default;
226 
230  BlockVectorView( BlockVectorView && ) = default;
231 
233 
238  void resize( localIndex const size )
239  {
240  m_vectors.resize( size );
241  }
242 
248  void setPointer( localIndex i, VECTOR * vec )
249  {
250  m_vectors( i ) = vec;
251  }
252 
253 private:
254 
256  array1d< VECTOR * > m_vectors;
257 };
258 
259 template< typename VECTOR >
261 {
262  GEOSX_LAI_ASSERT_EQ( blockSize(), src.blockSize() );
263  for( localIndex i = 0; i < blockSize(); i++ )
264  {
265  block( i ).copy( src.block( i ) );
266  }
267 }
268 
269 template< typename VECTOR >
271 {
272  for( localIndex i = 0; i < blockSize(); i++ )
273  {
274  block( i ).scale( factor );
275  }
276 }
277 
278 template< typename VECTOR >
280 {
281  for( localIndex i = 0; i < blockSize(); i++ )
282  {
283  block( i ).zero();
284  }
285 }
286 
287 template< typename VECTOR >
288 void BlockVectorView< VECTOR >::rand( unsigned const seed )
289 {
290  for( localIndex i = 0; i < blockSize(); i++ )
291  {
292  block( i ).rand( seed );
293  }
294 }
295 
296 template< typename VECTOR >
298 {
299  GEOSX_LAI_ASSERT_EQ( blockSize(), src.blockSize() );
300  real64 accum = 0;
301  for( localIndex i = 0; i < blockSize(); i++ )
302  {
303  accum += block( i ).dot( src.block( i ) );
304  }
305  return accum;
306 }
307 
308 template< typename VECTOR >
310 {
311  real64 accum = 0;
312  for( localIndex i = 0; i < blockSize(); i++ )
313  {
314  real64 const temp = block( i ).norm2();
315  accum = accum + temp * temp;
316  }
317  return std::sqrt( accum );
318 }
319 
320 template< typename VECTOR >
322 {
323  real64 result = 0.0;
324  for( localIndex i = 0; i < blockSize(); i++ )
325  {
326  result = std::fmax( result, block( i ).normInf() );
327  }
328  return result;
329 }
330 
331 template< typename VECTOR >
333  BlockVectorView const & x )
334 {
335  GEOSX_LAI_ASSERT_EQ( blockSize(), x.blockSize() );
336  for( localIndex i = 0; i < blockSize(); i++ )
337  {
338  block( i ).axpy( alpha, x.block( i ) );
339  }
340 }
341 
342 template< typename VECTOR >
344  BlockVectorView const & x,
345  real64 const beta )
346 {
347  GEOSX_LAI_ASSERT_EQ( blockSize(), x.blockSize() );
348  for( localIndex i = 0; i < blockSize(); i++ )
349  {
350  block( i ).axpby( alpha, x.block( i ), beta );
351  }
352 }
353 
354 template< typename VECTOR >
356 {
357  return m_vectors.size();
358 }
359 
360 template< typename VECTOR >
362 {
363  globalIndex size = 0;
364  for( localIndex i = 0; i < blockSize(); i++ )
365  {
366  size += block( i ).globalSize();
367  }
368  return size;
369 }
370 
371 template< typename VECTOR >
373 {
374  localIndex size = 0;
375  for( localIndex i = 0; i < blockSize(); i++ )
376  {
377  size += block( i ).localSize();
378  }
379  return size;
380 }
381 
382 template< typename VECTOR >
383 void BlockVectorView< VECTOR >::print( std::ostream & os ) const
384 {
385  for( localIndex i = 0; i < m_vectors.size(); i++ )
386  {
387  os << "Block " << i << " of " << blockSize() << ":" << std::endl;
388  os << "=============" << std::endl;
389  block( i ).print( os );
390  }
391 }
392 
399 template< typename VECTOR >
400 std::ostream & operator<<( std::ostream & os,
401  BlockVectorView< VECTOR > const & vec )
402 {
403  vec.print( os );
404  return os;
405 }
406 
407 } // end geosx namespace
408 
409 
410 #endif /* GEOSX_LINEARALGEBRA_UTILITIES_BLOCKVECTORVIEW_HPP_ */
localIndex localSize() const
Get local size.
globalIndex globalSize() const
Get global size.
void zero()
Set the vector to zero.
void print(std::ostream &os=std::cout) const
Print the block vector.
long long int globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
BlockVectorView & operator=(BlockVectorView const &rhs)=delete
Deleted copy assignment.
void setPointer(localIndex i, VECTOR *vec)
Set pointer to a vector.
void resize(localIndex const size)
Resize to a new number of blocks.
void copy(BlockVectorView const &src)
Update vector y as y = x.
std::ostream & operator<<(std::ostream &stream, mapBase< K, V, SORTED > const &map)
Stream output operator for map types.
Definition: DataTypes.hpp:373
float sqrt(float const x)
Definition: math.hpp:121
real64 dot(BlockVectorView const &x) const
Dot product.
double real64
64-bit floating point type.
Definition: DataTypes.hpp:136
localIndex blockSize() const
Get block size.
real64 norm2() const
2-norm of the block vector.
void rand(unsigned const seed=1984)
Set vector elements to random entries.
virtual ~BlockVectorView()=default
Destructor.
void scale(real64 const factor)
Scale the block vector with factor.
VECTOR const & operator()(localIndex const blockIndex) const
Get a reference to the vector corresponding to block blockRowIndex.
void axpy(real64 const alpha, BlockVectorView const &x)
Update vector y as y = alpha*x + y.
#define GEOSX_LAI_ASSERT_EQ(lhs, rhs)
Definition: common.hpp:47
VECTOR & block(localIndex const blockIndex)
Get a reference to the vector corresponding to block blockRowIndex.
void axpby(real64 const alpha, BlockVectorView const &x, real64 const beta)
Update vector y as y = alpha*x + beta*y.
Vector Vector
Alias for sub-vector type.
real64 normInf() const
Inf-norm of the block vector.
std::ptrdiff_t localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
VECTOR const & block(localIndex const blockIndex) const
Get a reference to the vector corresponding to block blockRowIndex.
VECTOR & operator()(localIndex const blockIndex)
Get a reference to the vector corresponding to block blockRowIndex.
#define GEOSX_LAI_ASSERT(expr)
Definition: common.hpp:33
Abstract view of a block vector.
BlockVectorView(localIndex const nBlocks)
Create a vector of nBlocks blocks.
This class provides a fixed dimensional resizeable array interface in addition to an interface simila...
Definition: Array.hpp:55