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