MFEM  v4.6.0
Finite element discretization library
blockvector.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef MFEM_BLOCKVECTOR
13 #define MFEM_BLOCKVECTOR
14 
15 #include "../config/config.hpp"
16 #include "../general/array.hpp"
17 #include "vector.hpp"
18 
19 namespace mfem
20 {
21 
22 //! @class BlockVector
23 /**
24  * \brief A class to handle Vectors in a block fashion
25  *
26  * All data is contained in Vector::data, while blockVector is just a viewer for
27  * this data.
28  *
29  */
30 class BlockVector: public Vector
31 {
32 protected:
33 
34  //! Number of blocks in the blockVector
35  int numBlocks;
36  //! Offset for each block start. (length numBlocks+1)
37  /**
38  * blockOffsets[i+1] - blockOffsets[i] is the size of block i.
39  *
40  * This array is not owned.
41  */
42  const int *blockOffsets;
43  //! array of Vector objects used to extract blocks without allocating memory.
44  /** This array is owned. */
46 
47  void SetBlocks();
48 
49 public:
50  //! empty constructor
51  BlockVector();
52 
53  //! Constructor
54  /**
55  * bOffsets is an array of integers (length nBlocks+1) that tells the offsets
56  * of each block start.
57  */
58  BlockVector(const Array<int> & bOffsets);
59 
60  /// Construct a BlockVector with the given MemoryType @a mt.
61  BlockVector(const Array<int> & bOffsets, MemoryType mt);
62 
63  //! Copy constructor
64  BlockVector(const BlockVector & block);
65 
66  //! View constructor
67  /**
68  * data is an array of double of length at least blockOffsets[numBlocks] that
69  * contain all the values of the monolithic vector. bOffsets is an array of
70  * integers (length nBlocks+1) that tells the offsets of each block start.
71  * nBlocks is the number of blocks.
72  */
73  BlockVector(double *data, const Array<int> & bOffsets);
74 
75  /// Wrap a Vector as a BlockVector
76  BlockVector(Vector &v, const Array<int> &bOffsets);
77 
78  /// Wrap a Vector as a BlockVector with offset
79  BlockVector(Vector &v, int offset, const Array<int> &bOffsets);
80 
81  //! Return the number of blocks
82  int NumBlocks() const { return numBlocks; }
83 
84  //! Assignment operator. this and original must have the same block structure.
85  BlockVector & operator=(const BlockVector & original);
86  //! Set each entry of this equal to val
87  BlockVector & operator=(double val);
88 
89  //! Destructor
90  ~BlockVector();
91 
92  //! Get the i-th vector in the block.
93  Vector & GetBlock(int i) { return blocks[i]; }
94  //! Get the i-th vector in the block (const version).
95  const Vector & GetBlock(int i) const { return blocks[i]; }
96 
97  //! Get the i-th vector in the block
98  void GetBlockView(int i, Vector & blockView);
99 
100  int BlockSize(int i) { return blockOffsets[i+1] - blockOffsets[i]; }
101 
102  //! Update method
103  /**
104  * data is an array of double of length at least blockOffsets[numBlocks] that
105  * contain all the values of the monolithic vector. bOffsets is an array of
106  * integers (length nBlocks+1) that tells the offsets of each block start.
107  * nBlocks is the number of blocks.
108  */
109  void Update(double *data, const Array<int> & bOffsets);
110 
111  void Update(Vector & data, const Array<int> & bOffsets);
112 
113  /// Update a BlockVector with new @a bOffsets and make sure it owns its data.
114  /** The block-vector will be re-allocated if either:
115  - the offsets @a bOffsets are different from the current offsets, or
116  - currently, the block-vector does not own its data. */
117  void Update(const Array<int> &bOffsets);
118 
119  /** @brief Update a BlockVector with new @a bOffsets and make sure it owns
120  its data and uses the MemoryType @a mt. */
121  /** The block-vector will be re-allocated if either:
122  - the offsets @a bOffsets are different from the current offsets, or
123  - currently, the block-vector does not own its data, or
124  - currently, the block-vector does not use MemoryType @a mt. */
125  void Update(const Array<int> &bOffsets, MemoryType mt);
126 
127  /** @brief Synchronize the memory location flags (i.e. the memory validity
128  flags) of the big/monolithic block-vector with its sub-vector blocks. The
129  big/monolithic vector has the correct memory location flags. */
130  /** This method will copy the data validity flags from the big/monolithic
131  block-vector to its sub-vector block. */
132  void SyncToBlocks() const;
133 
134  /** @brief Synchronize the memory location flags (i.e. the memory validity
135  flags) of the big/monolithic block-vector with its sub-vector blocks. The
136  sub-vector blocks have the correct memory location flags. */
137  /** This method will copy/move the data of the sub-vector blocks (if
138  necessary) so that each block matches the memory location flags of the
139  big/monolithic block-vector. */
140  void SyncFromBlocks() const;
141 };
142 
143 }
144 
145 #endif /* MFEM_BLOCKVECTOR */
~BlockVector()
Destructor.
int BlockSize(int i)
Memory< double > data
Definition: vector.hpp:62
void GetBlockView(int i, Vector &blockView)
Get the i-th vector in the block.
A class to handle Vectors in a block fashion.
Definition: blockvector.hpp:30
BlockVector()
empty constructor
Definition: blockvector.cpp:27
const int * blockOffsets
Offset for each block start. (length numBlocks+1)
Definition: blockvector.hpp:42
void SyncFromBlocks() const
Synchronize the memory location flags (i.e. the memory validity flags) of the big/monolithic block-ve...
void Update(double *data, const Array< int > &bOffsets)
Update method.
Definition: blockvector.cpp:95
int numBlocks
Number of blocks in the blockVector.
Definition: blockvector.hpp:35
const Vector & GetBlock(int i) const
Get the i-th vector in the block (const version).
Definition: blockvector.hpp:95
BlockVector & operator=(const BlockVector &original)
Assignment operator. this and original must have the same block structure.
MemoryType
Memory types supported by MFEM.
Definition: mem_manager.hpp:31
void SyncToBlocks() const
Synchronize the memory location flags (i.e. the memory validity flags) of the big/monolithic block-ve...
int NumBlocks() const
Return the number of blocks.
Definition: blockvector.hpp:82
Vector data type.
Definition: vector.hpp:58
Vector * blocks
array of Vector objects used to extract blocks without allocating memory.
Definition: blockvector.hpp:45
Vector & GetBlock(int i)
Get the i-th vector in the block.
Definition: blockvector.hpp:93