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