MFEM  v4.6.0
Finite element discretization library
vector.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_VECTOR
13 #define MFEM_VECTOR
14 
15 #include "../general/array.hpp"
16 #ifdef MFEM_USE_ADIOS2
17 #include "../general/adios2stream.hpp"
18 #endif
19 #include "../general/globals.hpp"
20 #include "../general/mem_manager.hpp"
21 #include "../general/device.hpp"
22 
23 #include <cmath>
24 #include <iostream>
25 #include <limits>
26 #if defined(_MSC_VER) && (_MSC_VER < 1800)
27 #include <float.h>
28 #define isfinite _finite
29 #endif
30 
31 #ifdef MFEM_USE_MPI
32 #include <mpi.h>
33 #endif
34 
35 namespace mfem
36 {
37 
38 /** Count the number of entries in an array of doubles for which isfinite
39  is false, i.e. the entry is a NaN or +/-Inf. */
40 inline int CheckFinite(const double *v, const int n);
41 
42 /// Define a shortcut for std::numeric_limits<double>::infinity()
43 #ifndef __CYGWIN__
44 inline double infinity()
45 {
47 }
48 #else
49 // On Cygwin math.h defines a function 'infinity()' which will conflict with the
50 // above definition if we have 'using namespace mfem;' and try to use something
51 // like 'double a = infinity();'. This 'infinity()' function is non-standard and
52 // is defined by the Newlib C standard library implementation used by Cygwin,
53 // see https://en.wikipedia.org/wiki/Newlib, http://www.sourceware.org/newlib.
55 #endif
56 
57 /// Vector data type.
58 class Vector
59 {
60 protected:
61 
63  int size;
64 
65 public:
66 
67  /** Default constructor for Vector. Sets size = 0, and calls Memory::Reset on
68  data through Memory<double>'s default constructor. */
69  Vector(): size(0) { }
70 
71  /// Copy constructor. Allocates a new data array and copies the data.
72  Vector(const Vector &);
73 
74  /// Move constructor. "Steals" data from its argument.
75  Vector(Vector&& v);
76 
77  /// @brief Creates vector of size s.
78  /// @warning Entries are not initialized to zero!
79  explicit Vector(int s);
80 
81  /// Creates a vector referencing an array of doubles, owned by someone else.
82  /** The pointer @a data_ can be NULL. The data array can be replaced later
83  with SetData(). */
84  Vector(double *data_, int size_)
85  { data.Wrap(data_, size_, false); size = size_; }
86 
87  /** @brief Create a Vector referencing a sub-vector of the Vector @a base
88  starting at the given offset, @a base_offset, and size @a size_. */
89  Vector(Vector &base, int base_offset, int size_)
90  : data(base.data, base_offset, size_), size(size_) { }
91 
92  /// Create a Vector of size @a size_ using MemoryType @a mt.
93  Vector(int size_, MemoryType mt)
94  : data(size_, mt), size(size_) { }
95 
96  /** @brief Create a Vector of size @a size_ using host MemoryType @a h_mt and
97  device MemoryType @a d_mt. */
98  Vector(int size_, MemoryType h_mt, MemoryType d_mt)
99  : data(size_, h_mt, d_mt), size(size_) { }
100 
101  /// Create a vector using a braced initializer list
102  template <int N>
103  explicit Vector(const double (&values)[N]) : Vector(N)
104  { std::copy(values, values + N, GetData()); }
105 
106  /// Enable execution of Vector operations using the mfem::Device.
107  /** The default is to use Backend::CPU (serial execution on each MPI rank),
108  regardless of the mfem::Device configuration.
109 
110  When appropriate, MFEM functions and class methods will enable the use
111  of the mfem::Device for their Vector parameters.
112 
113  Some derived classes, e.g. GridFunction, enable the use of the
114  mfem::Device by default. */
115  virtual void UseDevice(bool use_dev) const { data.UseDevice(use_dev); }
116 
117  /// Return the device flag of the Memory object used by the Vector
118  virtual bool UseDevice() const { return data.UseDevice(); }
119 
120  /// Reads a vector from multiple files
121  void Load(std::istream ** in, int np, int * dim);
122 
123  /// Load a vector from an input stream.
124  void Load(std::istream &in, int Size);
125 
126  /// Load a vector from an input stream, reading the size from the stream.
127  void Load(std::istream &in) { int s; in >> s; Load(in, s); }
128 
129  /// @brief Resize the vector to size @a s.
130  /** If the new size is less than or equal to Capacity() then the internal
131  data array remains the same. Otherwise, the old array is deleted, if
132  owned, and a new array of size @a s is allocated without copying the
133  previous content of the Vector.
134  @warning In the second case above (new size greater than current one),
135  the vector will allocate new data array, even if it did not own the
136  original data! Also, new entries are not initialized! */
137  void SetSize(int s);
138 
139  /// Resize the vector to size @a s using MemoryType @a mt.
140  void SetSize(int s, MemoryType mt);
141 
142  /// Resize the vector to size @a s using the MemoryType of @a v.
143  void SetSize(int s, const Vector &v) { SetSize(s, v.GetMemory().GetMemoryType()); }
144 
145  /// Set the Vector data.
146  /// @warning This method should be called only when OwnsData() is false.
147  void SetData(double *d) { data.Wrap(d, data.Capacity(), false); }
148 
149  /// Set the Vector data and size.
150  /** The Vector does not assume ownership of the new data. The new size is
151  also used as the new Capacity().
152  @warning This method should be called only when OwnsData() is false.
153  @sa NewDataAndSize(). */
154  void SetDataAndSize(double *d, int s) { data.Wrap(d, s, false); size = s; }
155 
156  /// Set the Vector data and size, deleting the old data, if owned.
157  /** The Vector does not assume ownership of the new data. The new size is
158  also used as the new Capacity().
159  @sa SetDataAndSize(). */
160  void NewDataAndSize(double *d, int s)
161  {
162  data.Delete();
163  SetDataAndSize(d, s);
164  }
165 
166  /// Reset the Vector to use the given external Memory @a mem and size @a s.
167  /** If @a own_mem is false, the Vector will not own any of the pointers of
168  @a mem.
169 
170  Note that when @a own_mem is true, the @a mem object can be destroyed
171  immediately by the caller but `mem.Delete()` should NOT be called since
172  the Vector object takes ownership of all pointers owned by @a mem.
173 
174  @sa NewDataAndSize(). */
175  inline void NewMemoryAndSize(const Memory<double> &mem, int s, bool own_mem);
176 
177  /// Reset the Vector to be a reference to a sub-vector of @a base.
178  inline void MakeRef(Vector &base, int offset, int size);
179 
180  /** @brief Reset the Vector to be a reference to a sub-vector of @a base
181  without changing its current size. */
182  inline void MakeRef(Vector &base, int offset);
183 
184  /// Set the Vector data (host pointer) ownership flag.
185  void MakeDataOwner() const { data.SetHostPtrOwner(true); }
186 
187  /// Destroy a vector
188  void Destroy();
189 
190  /** @brief Delete the device pointer, if owned. If @a copy_to_host is true
191  and the data is valid only on device, move it to host before deleting.
192  Invalidates the device memory. */
193  void DeleteDevice(bool copy_to_host = true)
194  { data.DeleteDevice(copy_to_host); }
195 
196  /// Returns the size of the vector.
197  inline int Size() const { return size; }
198 
199  /// Return the size of the currently allocated data array.
200  /** It is always true that Capacity() >= Size(). */
201  inline int Capacity() const { return data.Capacity(); }
202 
203  /// Return a pointer to the beginning of the Vector data.
204  /** @warning This method should be used with caution as it gives write access
205  to the data of const-qualified Vector%s. */
206  inline double *GetData() const
207  { return const_cast<double*>((const double*)data); }
208 
209  /// Conversion to `double *`. Deprecated.
210  MFEM_DEPRECATED inline operator double *() { return data; }
211 
212  /// Conversion to `const double *`. Deprecated.
213  MFEM_DEPRECATED inline operator const double *() const { return data; }
214 
215  /// STL-like begin.
216  inline double *begin() { return data; }
217 
218  /// STL-like end.
219  inline double *end() { return data + size; }
220 
221  /// STL-like begin (const version).
222  inline const double *begin() const { return data; }
223 
224  /// STL-like end (const version).
225  inline const double *end() const { return data + size; }
226 
227  /// Return a reference to the Memory object used by the Vector.
229 
230  /** @brief Return a reference to the Memory object used by the Vector, const
231  version. */
232  const Memory<double> &GetMemory() const { return data; }
233 
234  /// Update the memory location of the vector to match @a v.
235  void SyncMemory(const Vector &v) const { GetMemory().Sync(v.GetMemory()); }
236 
237  /// Update the alias memory location of the vector to match @a v.
238  void SyncAliasMemory(const Vector &v) const
239  { GetMemory().SyncAlias(v.GetMemory(),Size()); }
240 
241  /// Read the Vector data (host pointer) ownership flag.
242  inline bool OwnsData() const { return data.OwnsHostPtr(); }
243 
244  /// Changes the ownership of the data; after the call the Vector is empty
245  inline void StealData(double **p)
246  { *p = data; data.Reset(); size = 0; }
247 
248  /// Changes the ownership of the data; after the call the Vector is empty
249  inline double *StealData() { double *p; StealData(&p); return p; }
250 
251  /// Access Vector entries. Index i = 0 .. size-1.
252  double &Elem(int i);
253 
254  /// Read only access to Vector entries. Index i = 0 .. size-1.
255  const double &Elem(int i) const;
256 
257  /// Access Vector entries using () for 0-based indexing.
258  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
259  inline double &operator()(int i);
260 
261  /// Read only access to Vector entries using () for 0-based indexing.
262  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
263  inline const double &operator()(int i) const;
264 
265  /// Access Vector entries using [] for 0-based indexing.
266  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
267  inline double &operator[](int i) { return (*this)(i); }
268 
269  /// Read only access to Vector entries using [] for 0-based indexing.
270  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
271  inline const double &operator[](int i) const { return (*this)(i); }
272 
273  /// Dot product with a `double *` array.
274  double operator*(const double *) const;
275 
276  /// Return the inner-product.
277  double operator*(const Vector &v) const;
278 
279  /// Copy Size() entries from @a v.
280  Vector &operator=(const double *v);
281 
282  /// Copy assignment.
283  /** @note Defining this method overwrites the implicitly defined copy
284  assignment operator. */
285  Vector &operator=(const Vector &v);
286 
287  /// Move assignment
288  Vector &operator=(Vector&& v);
289 
290  /// Redefine '=' for vector = constant.
291  Vector &operator=(double value);
292 
293  Vector &operator*=(double c);
294 
295  /// Component-wise scaling: (*this)(i) *= v(i)
296  Vector &operator*=(const Vector &v);
297 
298  Vector &operator/=(double c);
299 
300  /// Component-wise division: (*this)(i) /= v(i)
301  Vector &operator/=(const Vector &v);
302 
303  Vector &operator-=(double c);
304 
305  Vector &operator-=(const Vector &v);
306 
307  Vector &operator+=(double c);
308 
309  Vector &operator+=(const Vector &v);
310 
311  /// (*this) += a * Va
312  Vector &Add(const double a, const Vector &Va);
313 
314  /// (*this) = a * x
315  Vector &Set(const double a, const Vector &x);
316 
317  void SetVector(const Vector &v, int offset);
318 
319  void AddSubVector(const Vector &v, int offset);
320 
321  /// (*this) = -(*this)
322  void Neg();
323 
324  /// (*this)(i) = 1.0 / (*this)(i)
325  void Reciprocal();
326 
327  /// Swap the contents of two Vectors
328  inline void Swap(Vector &other);
329 
330  /// Set v = v1 + v2.
331  friend void add(const Vector &v1, const Vector &v2, Vector &v);
332 
333  /// Set v = v1 + alpha * v2.
334  friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
335 
336  /// z = a * (x + y)
337  friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
338 
339  /// z = a * x + b * y
340  friend void add(const double a, const Vector &x,
341  const double b, const Vector &y, Vector &z);
342 
343  /// Set v = v1 - v2.
344  friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
345 
346  /// z = a * (x - y)
347  friend void subtract(const double a, const Vector &x,
348  const Vector &y, Vector &z);
349 
350  /// Computes cross product of this vector with another 3D vector.
351  /// vout = this x vin.
352  void cross3D(const Vector &vin, Vector &vout) const;
353 
354  /// v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
355  void median(const Vector &lo, const Vector &hi);
356 
357  /// Extract entries listed in @a dofs to the output Vector @a elemvect.
358  /** Negative dof values cause the -dof-1 position in @a elemvect to receive
359  the -val in from this Vector. */
360  void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
361 
362  /// Extract entries listed in @a dofs to the output array @a elem_data.
363  /** Negative dof values cause the -dof-1 position in @a elem_data to receive
364  the -val in from this Vector. */
365  void GetSubVector(const Array<int> &dofs, double *elem_data) const;
366 
367  /// Set the entries listed in @a dofs to the given @a value.
368  /** Negative dof values cause the -dof-1 position in this Vector to receive
369  the -value. */
370  void SetSubVector(const Array<int> &dofs, const double value);
371 
372  /** @brief Set the entries listed in @a dofs to the values given in the @a
373  elemvect Vector. Negative dof values cause the -dof-1 position in this
374  Vector to receive the -val from @a elemvect. */
375  void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
376 
377  /** @brief Set the entries listed in @a dofs to the values given the @a ,
378  elem_data array. Negative dof values cause the -dof-1 position in this
379  Vector to receive the -val from @a elem_data. */
380  void SetSubVector(const Array<int> &dofs, double *elem_data);
381 
382  /** @brief Add elements of the @a elemvect Vector to the entries listed in @a
383  dofs. Negative dof values cause the -dof-1 position in this Vector to add
384  the -val from @a elemvect. */
385  void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
386 
387  /** @brief Add elements of the @a elem_data array to the entries listed in @a
388  dofs. Negative dof values cause the -dof-1 position in this Vector to add
389  the -val from @a elem_data. */
390  void AddElementVector(const Array<int> & dofs, double *elem_data);
391 
392  /** @brief Add @a times the elements of the @a elemvect Vector to the entries
393  listed in @a dofs. Negative dof values cause the -dof-1 position in this
394  Vector to add the -a*val from @a elemvect. */
395  void AddElementVector(const Array<int> & dofs, const double a,
396  const Vector & elemvect);
397 
398  /// Set all vector entries NOT in the @a dofs Array to the given @a val.
399  void SetSubVectorComplement(const Array<int> &dofs, const double val);
400 
401  /// Prints vector to stream out.
402  void Print(std::ostream &out = mfem::out, int width = 8) const;
403 
404 #ifdef MFEM_USE_ADIOS2
405  /// Prints vector to stream out.
406  /// @param out adios2stream output
407  /// @param variable_name variable name associated with current Vector
408  void Print(adios2stream & out, const std::string& variable_name) const;
409 #endif
410 
411  /// Prints vector to stream out in HYPRE_Vector format.
412  void Print_HYPRE(std::ostream &out) const;
413 
414  /// Print the Vector size and hash of its data.
415  /** This is a compact text representation of the Vector contents that can be
416  used to compare vectors from different runs without the need to save the
417  whole vector. */
418  void PrintHash(std::ostream &out) const;
419 
420  /// Set random values in the vector.
421  void Randomize(int seed = 0);
422  /// Returns the l2 norm of the vector.
423  double Norml2() const;
424  /// Returns the l_infinity norm of the vector.
425  double Normlinf() const;
426  /// Returns the l_1 norm of the vector.
427  double Norml1() const;
428  /// Returns the l_p norm of the vector.
429  double Normlp(double p) const;
430  /// Returns the maximal element of the vector.
431  double Max() const;
432  /// Returns the minimal element of the vector.
433  double Min() const;
434  /// Return the sum of the vector entries
435  double Sum() const;
436  /// Compute the square of the Euclidean distance to another vector.
437  inline double DistanceSquaredTo(const double *p) const;
438  /// Compute the square of the Euclidean distance to another vector.
439  inline double DistanceSquaredTo(const Vector &p) const;
440  /// Compute the Euclidean distance to another vector.
441  inline double DistanceTo(const double *p) const;
442  /// Compute the Euclidean distance to another vector.
443  inline double DistanceTo(const Vector &p) const;
444 
445  /** @brief Count the number of entries in the Vector for which isfinite
446  is false, i.e. the entry is a NaN or +/-Inf. */
447  int CheckFinite() const { return mfem::CheckFinite(HostRead(), size); }
448 
449  /// Destroys vector.
450  virtual ~Vector();
451 
452  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
453  virtual const double *Read(bool on_dev = true) const
454  { return mfem::Read(data, size, on_dev); }
455 
456  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
457  virtual const double *HostRead() const
458  { return mfem::Read(data, size, false); }
459 
460  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
461  virtual double *Write(bool on_dev = true)
462  { return mfem::Write(data, size, on_dev); }
463 
464  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
465  virtual double *HostWrite()
466  { return mfem::Write(data, size, false); }
467 
468  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
469  virtual double *ReadWrite(bool on_dev = true)
470  { return mfem::ReadWrite(data, size, on_dev); }
471 
472  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
473  virtual double *HostReadWrite()
474  { return mfem::ReadWrite(data, size, false); }
475 
476 };
477 
478 // Inline methods
479 
480 template <typename T>
481 inline T ZeroSubnormal(T val)
482 {
483  return (std::fpclassify(val) == FP_SUBNORMAL) ? 0.0 : val;
484 }
485 
486 inline bool IsFinite(const double &val)
487 {
488  // isfinite didn't appear in a standard until C99, and later C++11. It wasn't
489  // standard in C89 or C++98. PGI as of 14.7 still defines it as a macro.
490 #ifdef isfinite
491  return isfinite(val);
492 #else
493  return std::isfinite(val);
494 #endif
495 }
496 
497 inline int CheckFinite(const double *v, const int n)
498 {
499  int bad = 0;
500  for (int i = 0; i < n; i++)
501  {
502  if (!IsFinite(v[i])) { bad++; }
503  }
504  return bad;
505 }
506 
507 inline Vector::Vector(int s)
508 {
509  MFEM_ASSERT(s>=0,"Unexpected negative size.");
510  size = s;
511  if (s > 0)
512  {
513  data.New(s);
514  }
515 }
516 
517 inline void Vector::SetSize(int s)
518 {
519  if (s == size)
520  {
521  return;
522  }
523  if (s <= data.Capacity())
524  {
525  size = s;
526  return;
527  }
528  // preserve a valid MemoryType and device flag
529  const MemoryType mt = data.GetMemoryType();
530  const bool use_dev = data.UseDevice();
531  data.Delete();
532  size = s;
533  data.New(s, mt);
534  data.UseDevice(use_dev);
535 }
536 
537 inline void Vector::SetSize(int s, MemoryType mt)
538 {
539  if (mt == data.GetMemoryType())
540  {
541  if (s == size)
542  {
543  return;
544  }
545  if (s <= data.Capacity())
546  {
547  size = s;
548  return;
549  }
550  }
551  const bool use_dev = data.UseDevice();
552  data.Delete();
553  if (s > 0)
554  {
555  data.New(s, mt);
556  size = s;
557  }
558  else
559  {
560  data.Reset();
561  size = 0;
562  }
563  data.UseDevice(use_dev);
564 }
565 
566 inline void Vector::NewMemoryAndSize(const Memory<double> &mem, int s,
567  bool own_mem)
568 {
569  data.Delete();
570  size = s;
571  if (own_mem)
572  {
573  data = mem;
574  }
575  else
576  {
577  data.MakeAlias(mem, 0, s);
578  }
579 }
580 
581 inline void Vector::MakeRef(Vector &base, int offset, int s)
582 {
583  data.Delete();
584  size = s;
585  data.MakeAlias(base.GetMemory(), offset, s);
586 }
587 
588 inline void Vector::MakeRef(Vector &base, int offset)
589 {
590  data.Delete();
591  data.MakeAlias(base.GetMemory(), offset, size);
592 }
593 
594 inline void Vector::Destroy()
595 {
596  const bool use_dev = data.UseDevice();
597  data.Delete();
598  size = 0;
599  data.Reset();
600  data.UseDevice(use_dev);
601 }
602 
603 inline double &Vector::operator()(int i)
604 {
605  MFEM_ASSERT(data && i >= 0 && i < size,
606  "index [" << i << "] is out of range [0," << size << ")");
607 
608  return data[i];
609 }
610 
611 inline const double &Vector::operator()(int i) const
612 {
613  MFEM_ASSERT(data && i >= 0 && i < size,
614  "index [" << i << "] is out of range [0," << size << ")");
615 
616  return data[i];
617 }
618 
619 inline void Vector::Swap(Vector &other)
620 {
621  mfem::Swap(data, other.data);
622  mfem::Swap(size, other.size);
623 }
624 
625 /// Specialization of the template function Swap<> for class Vector
626 template<> inline void Swap<Vector>(Vector &a, Vector &b)
627 {
628  a.Swap(b);
629 }
630 
632 {
633  data.Delete();
634 }
635 
636 inline double DistanceSquared(const double *x, const double *y, const int n)
637 {
638  double d = 0.0;
639 
640  for (int i = 0; i < n; i++)
641  {
642  d += (x[i]-y[i])*(x[i]-y[i]);
643  }
644 
645  return d;
646 }
647 
648 inline double Distance(const double *x, const double *y, const int n)
649 {
650  return std::sqrt(DistanceSquared(x, y, n));
651 }
652 
653 inline double Distance(const Vector &x, const Vector &y)
654 {
655  return x.DistanceTo(y);
656 }
657 
658 inline double Vector::DistanceSquaredTo(const double *p) const
659 {
660  return DistanceSquared(data, p, size);
661 }
662 
663 inline double Vector::DistanceSquaredTo(const Vector &p) const
664 {
665  MFEM_ASSERT(p.Size() == Size(), "Incompatible vector sizes.");
666  return DistanceSquared(data, p.data, size);
667 }
668 
669 inline double Vector::DistanceTo(const double *p) const
670 {
671  return Distance(data, p, size);
672 }
673 
674 inline double Vector::DistanceTo(const Vector &p) const
675 {
676  MFEM_ASSERT(p.Size() == Size(), "Incompatible vector sizes.");
677  return Distance(data, p.data, size);
678 }
679 
680 /// Returns the inner product of x and y
681 /** In parallel this computes the inner product of the local vectors,
682  producing different results on each MPI rank.
683 */
684 inline double InnerProduct(const Vector &x, const Vector &y)
685 {
686  return x * y;
687 }
688 
689 #ifdef MFEM_USE_MPI
690 /// Returns the inner product of x and y in parallel
691 /** In parallel this computes the inner product of the global vectors,
692  producing identical results on each MPI rank.
693 */
694 inline double InnerProduct(MPI_Comm comm, const Vector &x, const Vector &y)
695 {
696  double loc_prod = x * y;
697  double glb_prod;
698  MPI_Allreduce(&loc_prod, &glb_prod, 1, MPI_DOUBLE, MPI_SUM, comm);
699  return glb_prod;
700 }
701 #endif
702 
703 } // namespace mfem
704 
705 #endif
void SetSubVector(const Array< int > &dofs, const double value)
Set the entries listed in dofs to the given value.
Definition: vector.cpp:605
void SetVector(const Vector &v, int offset)
Definition: vector.cpp:275
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:497
void DeleteDevice(bool copy_to_host=true)
Delete the device pointer, if owned. If copy_to_host is true and the data is valid only on device...
Definition: vector.hpp:193
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:160
Memory< double > data
Definition: vector.hpp:62
Vector(double *data_, int size_)
Creates a vector referencing an array of doubles, owned by someone else.
Definition: vector.hpp:84
double & Elem(int i)
Access Vector entries. Index i = 0 .. size-1.
Definition: vector.cpp:96
void Print_HYPRE(std::ostream &out) const
Prints vector to stream out in HYPRE_Vector format.
Definition: vector.cpp:790
const Memory< double > & GetMemory() const
Return a reference to the Memory object used by the Vector, const version.
Definition: vector.hpp:232
void SyncMemory(const Vector &v) const
Update the memory location of the vector to match v.
Definition: vector.hpp:235
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:517
void Delete()
Delete the owned pointers and reset the Memory object.
virtual const double * HostRead() const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:457
virtual void UseDevice(bool use_dev) const
Enable execution of Vector operations using the mfem::Device.
Definition: vector.hpp:115
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 - v2.
Definition: vector.cpp:473
void Print(std::ostream &out=mfem::out, int width=8) const
Prints vector to stream out.
Definition: vector.cpp:756
double & operator()(int i)
Access Vector entries using () for 0-based indexing.
Definition: vector.hpp:603
void SetSize(int s, const Vector &v)
Resize the vector to size s using the MemoryType of v.
Definition: vector.hpp:143
void StealData(double **p)
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:245
int Size() const
Returns the size of the vector.
Definition: vector.hpp:197
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:336
virtual double * HostWrite()
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:465
void Swap< Vector >(Vector &a, Vector &b)
Specialization of the template function Swap<> for class Vector.
Definition: vector.hpp:626
virtual const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:453
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Extract entries listed in dofs to the output Vector elemvect.
Definition: vector.cpp:579
void Randomize(int seed=0)
Set random values in the vector.
Definition: vector.cpp:817
Memory< double > & GetMemory()
Return a reference to the Memory object used by the Vector.
Definition: vector.hpp:228
T ZeroSubnormal(T val)
Definition: vector.hpp:481
Vector(Vector &base, int base_offset, int size_)
Create a Vector referencing a sub-vector of the Vector base starting at the given offset...
Definition: vector.hpp:89
void AddSubVector(const Vector &v, int offset)
Definition: vector.cpp:288
Vector & operator=(const double *v)
Copy Size() entries from v.
Definition: vector.cpp:119
const double * end() const
STL-like end (const version).
Definition: vector.hpp:225
bool IsFinite(const double &val)
Definition: vector.hpp:486
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
Definition: vector.cpp:50
void Wrap(T *ptr, int size, bool own)
Wrap an externally allocated host pointer, ptr with the current host memory type returned by MemoryMa...
double & operator[](int i)
Access Vector entries using [] for 0-based indexing.
Definition: vector.hpp:267
double DistanceTo(const double *p) const
Compute the Euclidean distance to another vector.
Definition: vector.hpp:669
void Swap(Vector &other)
Swap the contents of two Vectors.
Definition: vector.hpp:619
void Sync(const Memory &other) const
Copy the host/device pointer validity flags from other to *this.
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:447
virtual double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:461
double b
Definition: lissajous.cpp:42
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 Reciprocal()
(*this)(i) = 1.0 / (*this)(i)
Definition: vector.cpp:309
Vector(const double(&values)[N])
Create a vector using a braced initializer list.
Definition: vector.hpp:103
double Sum() const
Return the sum of the vector entries.
Definition: vector.cpp:1286
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
Definition: vector.cpp:555
double DistanceSquared(const double *x, const double *y, const int n)
Definition: vector.hpp:636
void Load(std::istream &in)
Load a vector from an input stream, reading the size from the stream.
Definition: vector.hpp:127
const double & operator[](int i) const
Read only access to Vector entries using [] for 0-based indexing.
Definition: vector.hpp:271
double * StealData()
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:249
int Capacity() const
Return the size of the currently allocated data array.
Definition: vector.hpp:201
int Capacity() const
Return the size of the allocated memory.
void SetData(double *d)
Definition: vector.hpp:147
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:319
double Normlp(double p) const
Returns the l_p norm of the vector.
Definition: vector.cpp:875
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 elements of the elemvect Vector to the entries listed in dofs. Negative dof values cause the -dof...
Definition: vector.cpp:671
Vector & operator/=(double c)
Definition: vector.cpp:184
void SetSubVectorComplement(const Array< int > &dofs, const double val)
Set all vector entries NOT in the dofs Array to the given val.
Definition: vector.cpp:740
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:206
Vector & operator*=(double c)
Definition: vector.cpp:163
Vector & operator+=(double c)
Definition: vector.cpp:227
void Swap(Array< T > &, Array< T > &)
Definition: array.hpp:638
double Distance(const double *x, const double *y, const int n)
Definition: vector.hpp:648
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
MemoryType
Memory types supported by MFEM.
Definition: mem_manager.hpp:31
double Min() const
Returns the minimal element of the vector.
Definition: vector.cpp:1215
void SetHostPtrOwner(bool own) const
Set/clear the ownership flag for the host pointer. Ownership indicates whether the pointer will be de...
void PrintHash(std::ostream &out) const
Print the Vector size and hash of its data.
Definition: vector.cpp:809
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
Definition: vector.hpp:242
void DeleteDevice(bool copy_to_host=true)
Delete the device pointer, if owned. If copy_to_host is true and the data is valid only on device...
void SetDataAndSize(double *d, int s)
Set the Vector data and size.
Definition: vector.hpp:154
double Max() const
Returns the maximal element of the vector.
Definition: vector.cpp:925
double Norml1() const
Returns the l_1 norm of the vector.
Definition: vector.cpp:864
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:263
double * begin()
STL-like begin.
Definition: vector.hpp:216
double a
Definition: lissajous.cpp:41
void MakeDataOwner() const
Set the Vector data (host pointer) ownership flag.
Definition: vector.hpp:185
double InnerProduct(HypreParVector *x, HypreParVector *y)
Definition: hypre.cpp:414
virtual double * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:469
Vector & Add(const double a, const Vector &Va)
(*this) += a * Va
Definition: vector.cpp:248
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:353
void SyncAliasMemory(const Vector &v) const
Update the alias memory location of the vector to match v.
Definition: vector.hpp:238
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:53
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:566
void Destroy()
Destroy a vector.
Definition: vector.hpp:594
Vector & operator-=(double c)
Definition: vector.cpp:206
double infinity()
Define a shortcut for std::numeric_limits<double>::infinity()
Definition: vector.hpp:44
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:835
bool OwnsHostPtr() const
Return true if the host pointer is owned. Ownership indicates whether the pointer will be deleted by ...
double * end()
STL-like end.
Definition: vector.hpp:219
const double alpha
Definition: ex15.cpp:369
virtual bool UseDevice() const
Return the device flag of the Memory object used by the Vector.
Definition: vector.hpp:118
Vector data type.
Definition: vector.hpp:58
friend void add(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 + v2.
Definition: vector.cpp:317
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
Definition: vector.hpp:581
bool UseDevice() const
Read the internal device flag.
Vector(int size_, MemoryType mt)
Create a Vector of size size_ using MemoryType mt.
Definition: vector.hpp:93
RefCoord s[3]
void cross3D(const Vector &vin, Vector &vout) const
Definition: vector.cpp:542
double Normlinf() const
Returns the l_infinity norm of the vector.
Definition: vector.cpp:853
double DistanceSquaredTo(const double *p) const
Compute the square of the Euclidean distance to another vector.
Definition: vector.hpp:658
const double * begin() const
STL-like begin (const version).
Definition: vector.hpp:222
virtual double * HostReadWrite()
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:473
virtual ~Vector()
Destroys vector.
Definition: vector.hpp:631
MemoryType GetMemoryType() const
Return a MemoryType that is currently valid. If both the host and the device pointers are currently v...
double operator*(const double *) const
Dot product with a double * array.
Definition: vector.cpp:106
void Neg()
(*this) = -(*this)
Definition: vector.cpp:301
Vector(int size_, MemoryType h_mt, MemoryType d_mt)
Create a Vector of size size_ using host MemoryType h_mt and device MemoryType d_mt.
Definition: vector.hpp:98