MFEM  v4.2.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
blockvector.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, 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 #include "../general/array.hpp"
13 #include "vector.hpp"
14 #include "blockvector.hpp"
15 
16 namespace mfem
17 {
18 
20 {
21  for (int i = 0; i < numBlocks; ++i)
22  {
25  BlockSize(i), true);
26  }
27 }
28 
30  Vector(),
31  numBlocks(0),
32  blockOffsets(NULL),
33  blocks(NULL)
34 {
35 
36 }
37 
38 //! Standard constructor
40  Vector(bOffsets.Last()),
41  numBlocks(bOffsets.Size()-1),
42  blockOffsets(bOffsets.GetData())
43 {
44  blocks = new Vector[numBlocks];
45  SetBlocks();
46 }
47 
49  : Vector(bOffsets.Last(), mt),
50  numBlocks(bOffsets.Size()-1),
51  blockOffsets(bOffsets.GetData())
52 {
53  blocks = new Vector[numBlocks];
54  SetBlocks();
55 }
56 
57 //! Copy constructor
59  Vector(v),
60  numBlocks(v.numBlocks),
61  blockOffsets(v.blockOffsets)
62 {
63  blocks = new Vector[numBlocks];
64  SetBlocks();
65 }
66 
67 //! View constructor
68 BlockVector::BlockVector(double *data, const Array<int> & bOffsets):
69  Vector(data, bOffsets.Last()),
70  numBlocks(bOffsets.Size()-1),
71  blockOffsets(bOffsets.GetData())
72 {
73  blocks = new Vector[numBlocks];
74  SetBlocks();
75 }
76 
77 void BlockVector::Update(double *data, const Array<int> & bOffsets)
78 {
79  NewDataAndSize(data, bOffsets.Last());
80  blockOffsets = bOffsets.GetData();
81  if (numBlocks != bOffsets.Size()-1)
82  {
83  delete [] blocks;
84  numBlocks = bOffsets.Size()-1;
85  blocks = new Vector[numBlocks];
86  }
87  SetBlocks();
88 }
89 
90 void BlockVector::Update(Vector & data, const Array<int> & bOffsets)
91 {
92  blockOffsets = bOffsets.GetData();
93  if (numBlocks != bOffsets.Size()-1)
94  {
95  delete [] blocks;
96  numBlocks = bOffsets.Size()-1;
97  blocks = new Vector[numBlocks];
98  }
99 
100  for (int i = 0; i < numBlocks; ++i)
101  {
102  blocks[i].MakeRef(data, blockOffsets[i], BlockSize(i));
103  }
104 }
105 
106 void BlockVector::Update(const Array<int> &bOffsets)
107 {
108  Update(bOffsets, data.GetMemoryType());
109 }
110 
111 void BlockVector::Update(const Array<int> &bOffsets, MemoryType mt)
112 {
113  blockOffsets = bOffsets.GetData();
114  if (OwnsData() && data.GetMemoryType() == mt)
115  {
116  // check if 'bOffsets' agree with the 'blocks'
117  if (bOffsets.Size() == numBlocks+1)
118  {
119  if (numBlocks == 0) { return; }
120  if (Size() == bOffsets.Last())
121  {
122  for (int i = numBlocks - 1; true; i--)
123  {
124  if (i < 0) { return; }
125  if (blocks[i].Size() != bOffsets[i+1] - bOffsets[i]) { break; }
126  MFEM_ASSERT(blocks[i].GetData() == data + bOffsets[i],
127  "invalid blocks[" << i << ']');
128  }
129  }
130  }
131  }
132  else
133  {
134  Destroy();
135  }
136  SetSize(bOffsets.Last(), mt);
137  if (numBlocks != bOffsets.Size()-1)
138  {
139  delete [] blocks;
140  numBlocks = bOffsets.Size()-1;
141  blocks = new Vector[numBlocks];
142  }
143  SetBlocks();
144 }
145 
147 {
148  if (numBlocks!=original.numBlocks)
149  {
150  mfem_error("Number of Blocks don't match in BlockVector::operator=");
151  }
152 
153  for (int i(0); i <= numBlocks; ++i)
154  {
155  if (blockOffsets[i]!=original.blockOffsets[i])
156  {
157  mfem_error("Size of Blocks don't match in BlockVector::operator=");
158  }
159  }
160 
161  Vector::operator=(original);
162 
163  return *this;
164 }
165 
167 {
168  Vector::operator=(val);
169  return *this;
170 }
171 
172 //! Destructor
174 {
175  delete [] blocks;
176 }
177 
178 void BlockVector::GetBlockView(int i, Vector & blockView)
179 {
180  blockView.NewMemoryAndSize(
182  BlockSize(i), true);
183 }
184 
185 }
~BlockVector()
Destructor.
int Size() const
Return the logical size of the array.
Definition: array.hpp:124
int BlockSize(int i)
Definition: blockvector.hpp:94
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:134
Memory< double > data
Definition: vector.hpp:55
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
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:459
BlockVector()
empty constructor
Definition: blockvector.cpp:29
const int * blockOffsets
Offset for each block start. (length numBlocks+1)
Definition: blockvector.hpp:42
T * GetData()
Returns the data.
Definition: array.hpp:98
int Size() const
Returns the size of the vector.
Definition: vector.hpp:160
void Update(double *data, const Array< int > &bOffsets)
Update method.
Definition: blockvector.cpp:77
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:169
MemoryType GetMemoryType() const
Return a MemoryType that is currently valid. If both the host and the device pointers are currently v...
Vector & operator=(const double *v)
Copy Size() entries from v.
Definition: vector.cpp:120
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
Definition: vector.hpp:197
void mfem_error(const char *msg)
Function called when an error is encountered. Used by the macros MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY.
Definition: error.cpp:153
int numBlocks
Number of blocks in the blockVector.
Definition: blockvector.hpp:35
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:28
void NewMemoryAndSize(const Memory< double > &mem, int s, bool own_mem)
Reset the Vector to use the given external Memory mem and size s.
Definition: vector.hpp:508
void Destroy()
Destroy a vector.
Definition: vector.hpp:530
T & Last()
Return the last element in the array.
Definition: array.hpp:759
Vector data type.
Definition: vector.hpp:51
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
Definition: vector.hpp:517
Vector * blocks
array of Vector objects used to extract blocks without allocating memory.
Definition: blockvector.hpp:45