MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
vector.hpp
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 #ifndef MFEM_VECTOR
13 #define MFEM_VECTOR
14 
15 #include "../general/array.hpp"
16 #include "../general/globals.hpp"
17 #include "../general/mem_manager.hpp"
18 #include "../general/device.hpp"
19 #ifdef MFEM_USE_SUNDIALS
20 #include <nvector/nvector_serial.h>
21 #endif
22 #include <cmath>
23 #include <iostream>
24 #include <limits>
25 #if defined(_MSC_VER) && (_MSC_VER < 1800)
26 #include <float.h>
27 #define isfinite _finite
28 #endif
29 
30 #ifdef MFEM_USE_MPI
31 #include <mpi.h>
32 #endif
33 
34 namespace mfem
35 {
36 
37 /** Count the number of entries in an array of doubles for which isfinite
38  is false, i.e. the entry is a NaN or +/-Inf. */
39 inline int CheckFinite(const double *v, const int n);
40 
41 /// Define a shortcut for std::numeric_limits<double>::infinity()
42 inline double infinity()
43 {
45 }
46 
47 /// Vector data type.
48 class Vector
49 {
50 protected:
51 
53  int size;
54 
55 public:
56 
57  /// Default constructor for Vector. Sets size = 0 and data = NULL.
58  Vector() { data.Reset(); size = 0; }
59 
60  /// Copy constructor. Allocates a new data array and copies the data.
61  Vector(const Vector &);
62 
63  /// @brief Creates vector of size s.
64  /// @warning Entries are not initialized to zero!
65  explicit Vector(int s);
66 
67  /// Creates a vector referencing an array of doubles, owned by someone else.
68  /** The pointer @a _data can be NULL. The data array can be replaced later
69  with SetData(). */
70  Vector(double *_data, int _size)
71  { data.Wrap(_data, _size, false); size = _size; }
72 
73  /// Create a Vector of size @a size_ using MemoryType @a mt.
74  Vector(int size_, MemoryType mt)
75  : data(size_, mt), size(size_) { }
76 
77  /// Enable execution of Vector operations using the mfem::Device.
78  /** The default is to use Backend::CPU (serial execution on each MPI rank),
79  regardless of the mfem::Device configuration.
80 
81  When appropriate, MFEM functions and class methods will enable the use
82  of the mfem::Device for their Vector parameters.
83 
84  Some derived classes, e.g. GridFunction, enable the use of the
85  mfem::Device by default. */
86  void UseDevice(bool use_dev) const { data.UseDevice(use_dev); }
87 
88  /// Return the device flag of the Memory object used by the Vector
89  bool UseDevice() const { return data.UseDevice(); }
90 
91  /// Reads a vector from multiple files
92  void Load(std::istream ** in, int np, int * dim);
93 
94  /// Load a vector from an input stream.
95  void Load(std::istream &in, int Size);
96 
97  /// Load a vector from an input stream, reading the size from the stream.
98  void Load(std::istream &in) { int s; in >> s; Load(in, s); }
99 
100  /// @brief Resize the vector to size @a s.
101  /** If the new size is less than or equal to Capacity() then the internal
102  data array remains the same. Otherwise, the old array is deleted, if
103  owned, and a new array of size @a s is allocated without copying the
104  previous content of the Vector.
105  @warning In the second case above (new size greater than current one),
106  the vector will allocate new data array, even if it did not own the
107  original data! Also, new entries are not initialized! */
108  void SetSize(int s);
109 
110  /// Resize the vector to size @a s using MemoryType @a mt.
111  void SetSize(int s, MemoryType mt);
112 
113  /// Resize the vector to size @a s using the MemoryType of @a v.
114  void SetSize(int s, Vector &v) { SetSize(s, v.GetMemory().GetMemoryType()); }
115 
116  /// Set the Vector data.
117  /// @warning This method should be called only when OwnsData() is false.
118  void SetData(double *d) { data.Wrap(d, data.Capacity(), false); }
119 
120  /// Set the Vector data and size.
121  /** The Vector does not assume ownership of the new data. The new size is
122  also used as the new Capacity().
123  @warning This method should be called only when OwnsData() is false.
124  @sa NewDataAndSize(). */
125  void SetDataAndSize(double *d, int s) { data.Wrap(d, s, false); size = s; }
126 
127  /// Set the Vector data and size, deleting the old data, if owned.
128  /** The Vector does not assume ownership of the new data. The new size is
129  also used as the new Capacity().
130  @sa SetDataAndSize(). */
131  void NewDataAndSize(double *d, int s)
132  {
133  data.Delete();
134  SetDataAndSize(d, s);
135  }
136 
137  /// Reset the Vector to use the given external Memory @a mem and size @a s.
138  /** If @a own_mem is false, the Vector will not own any of the pointers of
139  @a mem.
140  @sa NewDataAndSize(). */
141  inline void NewMemoryAndSize(const Memory<double> &mem, int s, bool own_mem);
142 
143  /// Reset the Vector to be a reference to a sub-vector of @a base.
144  inline void MakeRef(Vector &base, int offset, int size);
145 
146  /** @brief Reset the Vector to be a reference to a sub-vector of @a base
147  without changing its current size. */
148  inline void MakeRef(Vector &base, int offset);
149 
150  /// Set the Vector data (host pointer) ownership flag.
151  void MakeDataOwner() const { data.SetHostPtrOwner(true); }
152 
153  /// Destroy a vector
154  void Destroy();
155 
156  /// Returns the size of the vector.
157  inline int Size() const { return size; }
158 
159  /// Return the size of the currently allocated data array.
160  /** It is always true that Capacity() >= Size(). */
161  inline int Capacity() const { return data.Capacity(); }
162 
163  /// Return a pointer to the beginning of the Vector data.
164  /** @warning This method should be used with caution as it gives write access
165  to the data of const-qualified Vector%s. */
166  inline double *GetData() const
167  { return const_cast<double*>((const double*)data); }
168 
169  /// Conversion to `double *`.
170  /** @note This conversion function makes it possible to use [] for indexing
171  in addition to the overloaded operator()(int). */
172  inline operator double *() { return data; }
173 
174  /// Conversion to `const double *`.
175  /** @note This conversion function makes it possible to use [] for indexing
176  in addition to the overloaded operator()(int). */
177  inline operator const double *() const { return data; }
178 
179  /// Return a reference to the Memory object used by the Vector.
181 
182  /** @brief Return a reference to the Memory object used by the Vector, const
183  version. */
184  const Memory<double> &GetMemory() const { return data; }
185 
186  /// Update the memory location of the vector to match @a v.
187  void SyncMemory(const Vector &v) { GetMemory().Sync(v.GetMemory()); }
188 
189  /// Update the alias memory location of the vector to match @a v.
190  void SyncAliasMemory(const Vector &v)
191  { GetMemory().SyncAlias(v.GetMemory(),Size()); }
192 
193  /// Read the Vector data (host pointer) ownership flag.
194  inline bool OwnsData() const { return data.OwnsHostPtr(); }
195 
196  /// Changes the ownership of the data; after the call the Vector is empty
197  inline void StealData(double **p)
198  { *p = data; data.Reset(); size = 0; }
199 
200  /// Changes the ownership of the data; after the call the Vector is empty
201  inline double *StealData() { double *p; StealData(&p); return p; }
202 
203  /// Access Vector entries. Index i = 0 .. size-1.
204  double &Elem(int i);
205 
206  /// Read only access to Vector entries. Index i = 0 .. size-1.
207  const double &Elem(int i) const;
208 
209  /// Access Vector entries using () for 0-based indexing.
210  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
211  inline double &operator()(int i);
212 
213  /// Read only access to Vector entries using () for 0-based indexing.
214  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
215  inline const double &operator()(int i) const;
216 
217  /// Dot product with a `double *` array.
218  double operator*(const double *) const;
219 
220  /// Return the inner-product.
221  double operator*(const Vector &v) const;
222 
223  /// Copy Size() entries from @a v.
224  Vector &operator=(const double *v);
225 
226  /// Copy assignment.
227  /** @note Defining this method overwrites the implicitly defined copy
228  assignment operator. */
229  Vector &operator=(const Vector &v);
230 
231  /// Redefine '=' for vector = constant.
232  Vector &operator=(double value);
233 
234  Vector &operator*=(double c);
235 
236  Vector &operator/=(double c);
237 
238  Vector &operator-=(double c);
239 
240  Vector &operator-=(const Vector &v);
241 
242  Vector &operator+=(const Vector &v);
243 
244  /// (*this) += a * Va
245  Vector &Add(const double a, const Vector &Va);
246 
247  /// (*this) = a * x
248  Vector &Set(const double a, const Vector &x);
249 
250  void SetVector(const Vector &v, int offset);
251 
252  /// (*this) = -(*this)
253  void Neg();
254 
255  /// Swap the contents of two Vectors
256  inline void Swap(Vector &other);
257 
258  /// Set v = v1 + v2.
259  friend void add(const Vector &v1, const Vector &v2, Vector &v);
260 
261  /// Set v = v1 + alpha * v2.
262  friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
263 
264  /// z = a * (x + y)
265  friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
266 
267  /// z = a * x + b * y
268  friend void add(const double a, const Vector &x,
269  const double b, const Vector &y, Vector &z);
270 
271  /// Set v = v1 - v2.
272  friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
273 
274  /// z = a * (x - y)
275  friend void subtract(const double a, const Vector &x,
276  const Vector &y, Vector &z);
277 
278  /// v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
279  void median(const Vector &lo, const Vector &hi);
280 
281  void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
282  void GetSubVector(const Array<int> &dofs, double *elem_data) const;
283 
284  /// Set the entries listed in `dofs` to the given `value`.
285  void SetSubVector(const Array<int> &dofs, const double value);
286  void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
287  void SetSubVector(const Array<int> &dofs, double *elem_data);
288 
289  /// Add (element) subvector to the vector.
290  void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
291  void AddElementVector(const Array<int> & dofs, double *elem_data);
292  void AddElementVector(const Array<int> & dofs, const double a,
293  const Vector & elemvect);
294 
295  /// Set all vector entries NOT in the 'dofs' array to the given 'val'.
296  void SetSubVectorComplement(const Array<int> &dofs, const double val);
297 
298  /// Prints vector to stream out.
299  void Print(std::ostream &out = mfem::out, int width = 8) const;
300 
301  /// Prints vector to stream out in HYPRE_Vector format.
302  void Print_HYPRE(std::ostream &out) const;
303 
304  /// Set random values in the vector.
305  void Randomize(int seed = 0);
306  /// Returns the l2 norm of the vector.
307  double Norml2() const;
308  /// Returns the l_infinity norm of the vector.
309  double Normlinf() const;
310  /// Returns the l_1 norm of the vector.
311  double Norml1() const;
312  /// Returns the l_p norm of the vector.
313  double Normlp(double p) const;
314  /// Returns the maximal element of the vector.
315  double Max() const;
316  /// Returns the minimal element of the vector.
317  double Min() const;
318  /// Return the sum of the vector entries
319  double Sum() const;
320  /// Compute the square of the Euclidean distance to another vector.
321  inline double DistanceSquaredTo(const double *p) const;
322  /// Compute the Euclidean distance to another vector.
323  inline double DistanceTo(const double *p) const;
324 
325  /** @brief Count the number of entries in the Vector for which isfinite
326  is false, i.e. the entry is a NaN or +/-Inf. */
327  int CheckFinite() const { return mfem::CheckFinite(data, size); }
328 
329  /// Destroys vector.
330  virtual ~Vector();
331 
332  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
333  const double *Read(bool on_dev = true) const
334  { return mfem::Read(data, size, on_dev); }
335 
336  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
337  const double *HostRead() const
338  { return mfem::Read(data, size, false); }
339 
340  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
341  double *Write(bool on_dev = true)
342  { return mfem::Write(data, size, on_dev); }
343 
344  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
345  double *HostWrite()
346  { return mfem::Write(data, size, false); }
347 
348  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
349  double *ReadWrite(bool on_dev = true)
350  { return mfem::ReadWrite(data, size, on_dev); }
351 
352  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
353  double *HostReadWrite()
354  { return mfem::ReadWrite(data, size, false); }
355 
356 #ifdef MFEM_USE_SUNDIALS
357  /// Construct a wrapper Vector from SUNDIALS N_Vector.
358  explicit Vector(N_Vector nv);
359 
360  /// Return a new wrapper SUNDIALS N_Vector of type SUNDIALS_NVEC_SERIAL.
361  /** The returned N_Vector must be destroyed by the caller. */
362  virtual N_Vector ToNVector() { return N_VMake_Serial(Size(), GetData()); }
363 
364  /** @brief Update an existing wrapper SUNDIALS N_Vector to point to this
365  Vector. */
366  virtual void ToNVector(N_Vector &nv);
367 #endif
368 };
369 
370 // Inline methods
371 
372 inline bool IsFinite(const double &val)
373 {
374  // isfinite didn't appear in a standard until C99, and later C++11. It wasn't
375  // standard in C89 or C++98. PGI as of 14.7 still defines it as a macro.
376 #ifdef isfinite
377  return isfinite(val);
378 #else
379  return std::isfinite(val);
380 #endif
381 }
382 
383 inline int CheckFinite(const double *v, const int n)
384 {
385  int bad = 0;
386  for (int i = 0; i < n; i++)
387  {
388  if (!IsFinite(v[i])) { bad++; }
389  }
390  return bad;
391 }
392 
393 inline Vector::Vector(int s)
394 {
395  if (s > 0)
396  {
397  size = s;
398  data.New(s);
399  }
400  else
401  {
402  size = 0;
403  data.Reset();
404  }
405 }
406 
407 inline void Vector::SetSize(int s)
408 {
409  if (s == size)
410  {
411  return;
412  }
413  if (s <= data.Capacity())
414  {
415  size = s;
416  return;
417  }
418  // preserve a valid MemoryType and device flag
419  const MemoryType mt = data.GetMemoryType();
420  const bool use_dev = data.UseDevice();
421  data.Delete();
422  size = s;
423  data.New(s, mt);
424  data.UseDevice(use_dev);
425 }
426 
427 inline void Vector::SetSize(int s, MemoryType mt)
428 {
429  if (mt == data.GetMemoryType())
430  {
431  if (s == size)
432  {
433  return;
434  }
435  if (s <= data.Capacity())
436  {
437  size = s;
438  return;
439  }
440  }
441  const bool use_dev = data.UseDevice();
442  data.Delete();
443  if (s > 0)
444  {
445  data.New(s, mt);
446  size = s;
447  }
448  else
449  {
450  data.Reset();
451  size = 0;
452  }
453  data.UseDevice(use_dev);
454 }
455 
456 inline void Vector::NewMemoryAndSize(const Memory<double> &mem, int s,
457  bool own_mem)
458 {
459  data.Delete();
460  size = s;
461  data = mem;
462  if (!own_mem) { data.ClearOwnerFlags(); }
463 }
464 
465 inline void Vector::MakeRef(Vector &base, int offset, int s)
466 {
467  data.Delete();
468  size = s;
469  data.MakeAlias(base.GetMemory(), offset, s);
470 }
471 
472 inline void Vector::MakeRef(Vector &base, int offset)
473 {
474  data.Delete();
475  data.MakeAlias(base.GetMemory(), offset, size);
476 }
477 
478 inline void Vector::Destroy()
479 {
480  const bool use_dev = data.UseDevice();
481  data.Delete();
482  size = 0;
483  data.Reset();
484  data.UseDevice(use_dev);
485 }
486 
487 inline double &Vector::operator()(int i)
488 {
489  MFEM_ASSERT(data && i >= 0 && i < size,
490  "index [" << i << "] is out of range [0," << size << ")");
491 
492  return data[i];
493 }
494 
495 inline const double &Vector::operator()(int i) const
496 {
497  MFEM_ASSERT(data && i >= 0 && i < size,
498  "index [" << i << "] is out of range [0," << size << ")");
499 
500  return data[i];
501 }
502 
503 inline void Vector::Swap(Vector &other)
504 {
505  mfem::Swap(data, other.data);
506  mfem::Swap(size, other.size);
507 }
508 
509 /// Specialization of the template function Swap<> for class Vector
510 template<> inline void Swap<Vector>(Vector &a, Vector &b)
511 {
512  a.Swap(b);
513 }
514 
516 {
517  data.Delete();
518 }
519 
520 inline double DistanceSquared(const double *x, const double *y, const int n)
521 {
522  double d = 0.0;
523 
524  for (int i = 0; i < n; i++)
525  {
526  d += (x[i]-y[i])*(x[i]-y[i]);
527  }
528 
529  return d;
530 }
531 
532 inline double Distance(const double *x, const double *y, const int n)
533 {
534  return std::sqrt(DistanceSquared(x, y, n));
535 }
536 
537 inline double Vector::DistanceSquaredTo(const double *p) const
538 {
539  return DistanceSquared(data, p, size);
540 }
541 
542 inline double Vector::DistanceTo(const double *p) const
543 {
544  return Distance(data, p, size);
545 }
546 
547 /// Returns the inner product of x and y
548 /** In parallel this computes the inner product of the local vectors,
549  producing different results on each MPI rank.
550 */
551 inline double InnerProduct(const Vector &x, const Vector &y)
552 {
553  return x * y;
554 }
555 
556 #ifdef MFEM_USE_MPI
557 /// Returns the inner product of x and y in parallel
558 /** In parallel this computes the inner product of the global vectors,
559  producing identical results on each MPI rank.
560 */
561 inline double InnerProduct(MPI_Comm comm, const Vector &x, const Vector &y)
562 {
563  double loc_prod = x * y;
564  double glb_prod;
565  MPI_Allreduce(&loc_prod, &glb_prod, 1, MPI_DOUBLE, MPI_SUM, comm);
566  return glb_prod;
567 }
568 #endif
569 
570 } // namespace mfem
571 
572 #endif
void SetSubVector(const Array< int > &dofs, const double value)
Set the entries listed in dofs to the given value.
Definition: vector.cpp:498
void SetVector(const Vector &v, int offset)
Definition: vector.cpp:217
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:383
double * HostReadWrite()
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:353
Vector()
Default constructor for Vector. Sets size = 0 and data = NULL.
Definition: vector.hpp:58
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:131
void MakeDataOwner() const
Set the Vector data (host pointer) ownership flag.
Definition: vector.hpp:151
Memory< double > data
Definition: vector.hpp:52
double & Elem(int i)
Access Vector entries. Index i = 0 .. size-1.
Definition: vector.cpp:83
void SyncMemory(const Vector &v)
Update the memory location of the vector to match v.
Definition: vector.hpp:187
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:407
void Delete()
Delete the owned pointers. The Memory is not reset by this method, i.e. it will, generally, not be Empty() after this call.
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 - v2.
Definition: vector.cpp:385
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:711
double & operator()(int i)
Access Vector entries using () for 0-based indexing.
Definition: vector.hpp:487
void UseDevice(bool use_dev) const
Enable execution of Vector operations using the mfem::Device.
Definition: vector.hpp:86
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Definition: vector.cpp:472
void SyncAlias(const Memory &base, int alias_size) const
Update the alias Memory *this to match the memory location (all valid locations) of its base Memory...
void StealData(double **p)
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:197
int Capacity() const
Return the size of the currently allocated data array.
Definition: vector.hpp:161
T * Write(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for write access to mem with the mfem::Device&#39;s DeviceMemoryClass, if on_dev = true...
Definition: device.hpp:312
int Size() const
Returns the size of the vector.
Definition: vector.hpp:157
void Swap< Vector >(Vector &a, Vector &b)
Specialization of the template function Swap&lt;&gt; for class Vector.
Definition: vector.hpp:510
double Normlinf() const
Returns the l_infinity norm of the vector.
Definition: vector.cpp:729
void Randomize(int seed=0)
Set random values in the vector.
Definition: vector.cpp:692
double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:341
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:166
int Capacity() const
Return the size of the allocated memory.
Memory< double > & GetMemory()
Return a reference to the Memory object used by the Vector.
Definition: vector.hpp:180
double operator*(const double *) const
Dot product with a double * array.
Definition: vector.cpp:93
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 IsFinite(const double &val)
Definition: vector.hpp:372
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
Definition: vector.cpp:51
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
Definition: vector.hpp:194
void Wrap(T *ptr, int size, bool own)
Wrap an externally allocated host pointer, ptr with the current host memory type returned by MemoryMa...
const double * HostRead() const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:337
double Normlp(double p) const
Returns the l_p norm of the vector.
Definition: vector.cpp:749
void SetSize(int s, Vector &v)
Resize the vector to size s using the MemoryType of v.
Definition: vector.hpp:114
double * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:349
bool UseDevice() const
Return the device flag of the Memory object used by the Vector.
Definition: vector.hpp:89
void Swap(Vector &other)
Swap the contents of two Vectors.
Definition: vector.hpp:503
double b
Definition: lissajous.cpp:42
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo &lt;= hi.
Definition: vector.cpp:448
double DistanceSquared(const double *x, const double *y, const int n)
Definition: vector.hpp:520
void Load(std::istream &in)
Load a vector from an input stream, reading the size from the stream.
Definition: vector.hpp:98
double * StealData()
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:201
bool OwnsHostPtr() const
Return true if the host pointer is owned. Ownership indicates whether the pointer will be deleted by ...
void SetData(double *d)
Definition: vector.hpp:118
const T * Read(const Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for read access to mem with the mfem::Device&#39;s DeviceMemoryClass, if on_dev = true...
Definition: device.hpp:295
void Reset()
Reset the memory to be empty, ensuring that Delete() will be a no-op.
void AddElementVector(const Array< int > &dofs, const Vector &elemvect)
Add (element) subvector to the vector.
Definition: vector.cpp:564
Vector & operator/=(double c)
Definition: vector.cpp:147
double DistanceTo(const double *p) const
Compute the Euclidean distance to another vector.
Definition: vector.hpp:542
void SetSubVectorComplement(const Array< int > &dofs, const double val)
Set all vector entries NOT in the &#39;dofs&#39; array to the given &#39;val&#39;.
Definition: vector.cpp:633
Vector(double *_data, int _size)
Creates a vector referencing an array of doubles, owned by someone else.
Definition: vector.hpp:70
Vector & operator*=(double c)
Definition: vector.cpp:138
double Min() const
Returns the minimal element of the vector.
Definition: vector.cpp:1072
double Norml1() const
Returns the l_1 norm of the vector.
Definition: vector.cpp:739
void Swap(Array< T > &, Array< T > &)
Definition: array.hpp:592
void ClearOwnerFlags() const
Clear the ownership flags for the host and device pointers, as well as any internal data allocated by...
double Distance(const double *x, const double *y, const int n)
Definition: vector.hpp:532
void Print(std::ostream &out=mfem::out, int width=8) const
Prints vector to stream out.
Definition: vector.cpp:649
MemoryType
Memory types supported by MFEM.
Definition: mem_manager.hpp:27
void SetHostPtrOwner(bool own) const
Set/clear the ownership flag for the host pointer. Ownership indicates whether the pointer will be de...
void Sync(const Memory &other) const
Copy the host/device pointer validity flags from other to *this.
void Print_HYPRE(std::ostream &out) const
Prints vector to stream out in HYPRE_Vector format.
Definition: vector.cpp:673
double DistanceSquaredTo(const double *p) const
Compute the square of the Euclidean distance to another vector.
Definition: vector.hpp:537
void SetDataAndSize(double *d, int s)
Set the Vector data and size.
Definition: vector.hpp:125
void MakeAlias(const Memory &base, int offset, int size)
Create a memory object that points inside the memory object base.
Vector & Set(const double a, const Vector &x)
(*this) = a * x
Definition: vector.cpp:205
double a
Definition: lissajous.cpp:41
double InnerProduct(HypreParVector *x, HypreParVector *y)
Definition: hypre.cpp:246
Vector & Add(const double a, const Vector &Va)
(*this) += a * Va
Definition: vector.cpp:190
T * ReadWrite(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for read+write access to mem with the mfem::Device&#39;s DeviceMemoryClass, if on_dev = true, or the mfem::Device&#39;s HostMemoryClass, otherwise.
Definition: device.hpp:329
const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:333
double Max() const
Returns the maximal element of the vector.
Definition: vector.cpp:799
void New(int size)
Allocate host memory for size entries with the current host memory type returned by MemoryManager::Ge...
int dim
Definition: ex24.cpp:43
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
Vector & operator-=(double c)
Definition: vector.cpp:157
double infinity()
Define a shortcut for std::numeric_limits&lt;double&gt;::infinity()
Definition: vector.hpp:42
int CheckFinite() const
Count the number of entries in the Vector for which isfinite is false, i.e. the entry is a NaN or +/-...
Definition: vector.hpp:327
double * HostWrite()
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:345
const double alpha
Definition: ex15.cpp:336
Vector data type.
Definition: vector.hpp:48
friend void add(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 + v2.
Definition: vector.cpp:238
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
Definition: vector.hpp:465
Vector(int size_, MemoryType mt)
Create a Vector of size size_ using MemoryType mt.
Definition: vector.hpp:74
bool UseDevice() const
Read the internal device flag.
Vector & operator+=(const Vector &v)
Definition: vector.cpp:178
const Memory< double > & GetMemory() const
Return a reference to the Memory object used by the Vector, const version.
Definition: vector.hpp:184
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66
double Sum() const
Return the sum of the vector entries.
Definition: vector.cpp:816
virtual N_Vector ToNVector()
Return a new wrapper SUNDIALS N_Vector of type SUNDIALS_NVEC_SERIAL.
Definition: vector.hpp:362
virtual ~Vector()
Destroys vector.
Definition: vector.hpp:515
void Neg()
(*this) = -(*this)
Definition: vector.cpp:230
void SyncAliasMemory(const Vector &v)
Update the alias memory location of the vector to match v.
Definition: vector.hpp:190