MFEM  v4.1.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(const Array<int> &bOffsets)
91 {
92  Update(bOffsets, data.GetMemoryType());
93 }
94 
95 void BlockVector::Update(const Array<int> &bOffsets, MemoryType mt)
96 {
97  blockOffsets = bOffsets.GetData();
98  if (OwnsData() && data.GetMemoryType() == mt)
99  {
100  // check if 'bOffsets' agree with the 'blocks'
101  if (bOffsets.Size() == numBlocks+1)
102  {
103  if (numBlocks == 0) { return; }
104  if (Size() == bOffsets.Last())
105  {
106  for (int i = numBlocks - 1; true; i--)
107  {
108  if (i < 0) { return; }
109  if (blocks[i].Size() != bOffsets[i+1] - bOffsets[i]) { break; }
110  MFEM_ASSERT(blocks[i].GetData() == data + bOffsets[i],
111  "invalid blocks[" << i << ']');
112  }
113  }
114  }
115  }
116  else
117  {
118  Destroy();
119  }
120  SetSize(bOffsets.Last(), mt);
121  if (numBlocks != bOffsets.Size()-1)
122  {
123  delete [] blocks;
124  numBlocks = bOffsets.Size()-1;
125  blocks = new Vector[numBlocks];
126  }
127  SetBlocks();
128 }
129 
131 {
132  if (numBlocks!=original.numBlocks)
133  {
134  mfem_error("Number of Blocks don't match in BlockVector::operator=");
135  }
136 
137  for (int i(0); i <= numBlocks; ++i)
138  {
139  if (blockOffsets[i]!=original.blockOffsets[i])
140  {
141  mfem_error("Size of Blocks don't match in BlockVector::operator=");
142  }
143  }
144 
145  Vector::operator=(original);
146 
147  return *this;
148 }
149 
151 {
152  Vector::operator=(val);
153  return *this;
154 }
155 
156 //! Destructor
158 {
159  delete [] blocks;
160 }
161 
162 void BlockVector::GetBlockView(int i, Vector & blockView)
163 {
164  blockView.NewMemoryAndSize(
166  BlockSize(i), true);
167 }
168 
169 }
~BlockVector()
Destructor.
int Size() const
Logical size of the array.
Definition: array.hpp:124
int BlockSize(int i)
Definition: blockvector.hpp:91
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:131
Memory< double > data
Definition: vector.hpp:52
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:407
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:157
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:166
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:106
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
Definition: vector.hpp:194
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:27
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:456
void Destroy()
Destroy a vector.
Definition: vector.hpp:478
T & Last()
Return the last element in the array.
Definition: array.hpp:740
Vector data type.
Definition: vector.hpp:48
Vector * blocks
array of Vector objects used to extract blocks without allocating memory.
Definition: blockvector.hpp:45