MFEM  v4.5.2 Finite element discretization library
vector.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, Lawrence Livermore National Security, LLC. Produced
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"
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 *. Deprecated.
212  MFEM_DEPRECATED inline operator double *() { return data; }
213
214  /// Conversion to const double *. Deprecated.
215  MFEM_DEPRECATED inline operator const double *() const { return data; }
216
217  /// STL-like begin.
218  inline double *begin() { return data; }
219
220  /// STL-like end.
221  inline double *end() { return data + size; }
222
223  /// STL-like begin (const version).
224  inline const double *begin() const { return data; }
225
226  /// STL-like end (const version).
227  inline const double *end() const { return data + size; }
228
229  /// Return a reference to the Memory object used by the Vector.
231
232  /** @brief Return a reference to the Memory object used by the Vector, const
233  version. */
234  const Memory<double> &GetMemory() const { return data; }
235
236  /// Update the memory location of the vector to match @a v.
237  void SyncMemory(const Vector &v) const { GetMemory().Sync(v.GetMemory()); }
238
239  /// Update the alias memory location of the vector to match @a v.
240  void SyncAliasMemory(const Vector &v) const
241  { GetMemory().SyncAlias(v.GetMemory(),Size()); }
242
243  /// Read the Vector data (host pointer) ownership flag.
244  inline bool OwnsData() const { return data.OwnsHostPtr(); }
245
246  /// Changes the ownership of the data; after the call the Vector is empty
247  inline void StealData(double **p)
248  { *p = data; data.Reset(); size = 0; }
249
250  /// Changes the ownership of the data; after the call the Vector is empty
251  inline double *StealData() { double *p; StealData(&p); return p; }
252
253  /// Access Vector entries. Index i = 0 .. size-1.
254  double &Elem(int i);
255
257  const double &Elem(int i) const;
258
259  /// Access Vector entries using () for 0-based indexing.
260  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
261  inline double &operator()(int i);
262
264  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
265  inline const double &operator()(int i) const;
266
267  /// Access Vector entries using [] for 0-based indexing.
268  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
269  inline double &operator[](int i) { return (*this)(i); }
270
272  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
273  inline const double &operator[](int i) const { return (*this)(i); }
274
275  /// Dot product with a double * array.
276  double operator*(const double *) const;
277
278  /// Return the inner-product.
279  double operator*(const Vector &v) const;
280
281  /// Copy Size() entries from @a v.
282  Vector &operator=(const double *v);
283
284  /// Copy assignment.
285  /** @note Defining this method overwrites the implicitly defined copy
286  assignment operator. */
287  Vector &operator=(const Vector &v);
288
289  /// Move assignment
290  Vector &operator=(Vector&& v);
291
292  /// Redefine '=' for vector = constant.
293  Vector &operator=(double value);
294
295  Vector &operator*=(double c);
296
297  /// Component-wise scaling: (*this)(i) *= v(i)
298  Vector &operator*=(const Vector &v);
299
300  Vector &operator/=(double c);
301
302  /// Component-wise division: (*this)(i) /= v(i)
303  Vector &operator/=(const Vector &v);
304
305  Vector &operator-=(double c);
306
307  Vector &operator-=(const Vector &v);
308
309  Vector &operator+=(double c);
310
311  Vector &operator+=(const Vector &v);
312
313  /// (*this) += a * Va
314  Vector &Add(const double a, const Vector &Va);
315
316  /// (*this) = a * x
317  Vector &Set(const double a, const Vector &x);
318
319  void SetVector(const Vector &v, int offset);
320
321  void AddSubVector(const Vector &v, int offset);
322
323  /// (*this) = -(*this)
324  void Neg();
325
326  /// Swap the contents of two Vectors
327  inline void Swap(Vector &other);
328
329  /// Set v = v1 + v2.
330  friend void add(const Vector &v1, const Vector &v2, Vector &v);
331
332  /// Set v = v1 + alpha * v2.
333  friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
334
335  /// z = a * (x + y)
336  friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
337
338  /// z = a * x + b * y
339  friend void add(const double a, const Vector &x,
340  const double b, const Vector &y, Vector &z);
341
342  /// Set v = v1 - v2.
343  friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
344
345  /// z = a * (x - y)
346  friend void subtract(const double a, const Vector &x,
347  const Vector &y, Vector &z);
348
349  /// v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
350  void median(const Vector &lo, const Vector &hi);
351
352  /// Extract entries listed in @a dofs to the output Vector @a elemvect.
353  /** Negative dof values cause the -dof-1 position in @a elemvect to receive
354  the -val in from this Vector. */
355  void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
356
357  /// Extract entries listed in @a dofs to the output array @a elem_data.
358  /** Negative dof values cause the -dof-1 position in @a elem_data to receive
359  the -val in from this Vector. */
360  void GetSubVector(const Array<int> &dofs, double *elem_data) const;
361
362  /// Set the entries listed in @a dofs to the given @a value.
363  /** Negative dof values cause the -dof-1 position in this Vector to receive
364  the -value. */
365  void SetSubVector(const Array<int> &dofs, const double value);
366
367  /** @brief Set the entries listed in @a dofs to the values given in the @a
368  elemvect Vector. Negative dof values cause the -dof-1 position in this
369  Vector to receive the -val from @a elemvect. */
370  void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
371
372  /** @brief Set the entries listed in @a dofs to the values given the @a ,
373  elem_data array. Negative dof values cause the -dof-1 position in this
374  Vector to receive the -val from @a elem_data. */
375  void SetSubVector(const Array<int> &dofs, double *elem_data);
376
377  /** @brief Add elements of the @a elemvect Vector to the entries listed in @a
378  dofs. Negative dof values cause the -dof-1 position in this Vector to add
379  the -val from @a elemvect. */
380  void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
381
382  /** @brief Add elements of the @a elem_data array 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 elem_data. */
385  void AddElementVector(const Array<int> & dofs, double *elem_data);
386
387  /** @brief Add @a times the elements of the @a elemvect Vector to the entries
388  listed in @a dofs. Negative dof values cause the -dof-1 position in this
389  Vector to add the -a*val from @a elemvect. */
390  void AddElementVector(const Array<int> & dofs, const double a,
391  const Vector & elemvect);
392
393  /// Set all vector entries NOT in the @a dofs Array to the given @a val.
394  void SetSubVectorComplement(const Array<int> &dofs, const double val);
395
396  /// Prints vector to stream out.
397  void Print(std::ostream &out = mfem::out, int width = 8) const;
398
400  /// Prints vector to stream out.
401  /// @param out adios2stream output
402  /// @param variable_name variable name associated with current Vector
403  void Print(adios2stream & out, const std::string& variable_name) const;
404 #endif
405
406  /// Prints vector to stream out in HYPRE_Vector format.
407  void Print_HYPRE(std::ostream &out) const;
408
409  /// Print the Vector size and hash of its data.
410  /** This is a compact text representation of the Vector contents that can be
411  used to compare vectors from different runs without the need to save the
412  whole vector. */
413  void PrintHash(std::ostream &out) const;
414
415  /// Set random values in the vector.
416  void Randomize(int seed = 0);
417  /// Returns the l2 norm of the vector.
418  double Norml2() const;
419  /// Returns the l_infinity norm of the vector.
420  double Normlinf() const;
421  /// Returns the l_1 norm of the vector.
422  double Norml1() const;
423  /// Returns the l_p norm of the vector.
424  double Normlp(double p) const;
425  /// Returns the maximal element of the vector.
426  double Max() const;
427  /// Returns the minimal element of the vector.
428  double Min() const;
429  /// Return the sum of the vector entries
430  double Sum() const;
431  /// Compute the square of the Euclidean distance to another vector.
432  inline double DistanceSquaredTo(const double *p) const;
433  /// Compute the square of the Euclidean distance to another vector.
434  inline double DistanceSquaredTo(const Vector &p) const;
435  /// Compute the Euclidean distance to another vector.
436  inline double DistanceTo(const double *p) const;
437  /// Compute the Euclidean distance to another vector.
438  inline double DistanceTo(const Vector &p) const;
439
440  /** @brief Count the number of entries in the Vector for which isfinite
441  is false, i.e. the entry is a NaN or +/-Inf. */
442  int CheckFinite() const { return mfem::CheckFinite(HostRead(), size); }
443
444  /// Destroys vector.
445  virtual ~Vector();
446
447  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
448  virtual const double *Read(bool on_dev = true) const
449  { return mfem::Read(data, size, on_dev); }
450
451  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
452  virtual const double *HostRead() const
453  { return mfem::Read(data, size, false); }
454
455  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
456  virtual double *Write(bool on_dev = true)
457  { return mfem::Write(data, size, on_dev); }
458
459  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
460  virtual double *HostWrite()
461  { return mfem::Write(data, size, false); }
462
463  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
464  virtual double *ReadWrite(bool on_dev = true)
465  { return mfem::ReadWrite(data, size, on_dev); }
466
467  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
469  { return mfem::ReadWrite(data, size, false); }
470
471 };
472
473 // Inline methods
474
475 template <typename T>
476 inline T ZeroSubnormal(T val)
477 {
478  return (std::fpclassify(val) == FP_SUBNORMAL) ? 0.0 : val;
479 }
480
481 inline bool IsFinite(const double &val)
482 {
483  // isfinite didn't appear in a standard until C99, and later C++11. It wasn't
484  // standard in C89 or C++98. PGI as of 14.7 still defines it as a macro.
485 #ifdef isfinite
486  return isfinite(val);
487 #else
488  return std::isfinite(val);
489 #endif
490 }
491
492 inline int CheckFinite(const double *v, const int n)
493 {
495  for (int i = 0; i < n; i++)
496  {
497  if (!IsFinite(v[i])) { bad++; }
498  }
500 }
501
502 inline Vector::Vector(int s)
503 {
504  MFEM_ASSERT(s>=0,"Unexpected negative size.");
505  size = s;
506  if (s > 0)
507  {
508  data.New(s);
509  }
510 }
511
512 inline void Vector::SetSize(int s)
513 {
514  if (s == size)
515  {
516  return;
517  }
518  if (s <= data.Capacity())
519  {
520  size = s;
521  return;
522  }
523  // preserve a valid MemoryType and device flag
524  const MemoryType mt = data.GetMemoryType();
525  const bool use_dev = data.UseDevice();
526  data.Delete();
527  size = s;
528  data.New(s, mt);
529  data.UseDevice(use_dev);
530 }
531
532 inline void Vector::SetSize(int s, MemoryType mt)
533 {
534  if (mt == data.GetMemoryType())
535  {
536  if (s == size)
537  {
538  return;
539  }
540  if (s <= data.Capacity())
541  {
542  size = s;
543  return;
544  }
545  }
546  const bool use_dev = data.UseDevice();
547  data.Delete();
548  if (s > 0)
549  {
550  data.New(s, mt);
551  size = s;
552  }
553  else
554  {
555  data.Reset();
556  size = 0;
557  }
558  data.UseDevice(use_dev);
559 }
560
561 inline void Vector::NewMemoryAndSize(const Memory<double> &mem, int s,
562  bool own_mem)
563 {
564  data.Delete();
565  size = s;
566  if (own_mem)
567  {
568  data = mem;
569  }
570  else
571  {
572  data.MakeAlias(mem, 0, s);
573  }
574 }
575
576 inline void Vector::MakeRef(Vector &base, int offset, int s)
577 {
578  data.Delete();
579  size = s;
580  data.MakeAlias(base.GetMemory(), offset, s);
581 }
582
583 inline void Vector::MakeRef(Vector &base, int offset)
584 {
585  data.Delete();
586  data.MakeAlias(base.GetMemory(), offset, size);
587 }
588
589 inline void Vector::Destroy()
590 {
591  const bool use_dev = data.UseDevice();
592  data.Delete();
593  size = 0;
594  data.Reset();
595  data.UseDevice(use_dev);
596 }
597
598 inline double &Vector::operator()(int i)
599 {
600  MFEM_ASSERT(data && i >= 0 && i < size,
601  "index [" << i << "] is out of range [0," << size << ")");
602
603  return data[i];
604 }
605
606 inline const double &Vector::operator()(int i) const
607 {
608  MFEM_ASSERT(data && i >= 0 && i < size,
609  "index [" << i << "] is out of range [0," << size << ")");
610
611  return data[i];
612 }
613
614 inline void Vector::Swap(Vector &other)
615 {
616  mfem::Swap(data, other.data);
617  mfem::Swap(size, other.size);
618 }
619
620 /// Specialization of the template function Swap<> for class Vector
621 template<> inline void Swap<Vector>(Vector &a, Vector &b)
622 {
623  a.Swap(b);
624 }
625
627 {
628  data.Delete();
629 }
630
631 inline double DistanceSquared(const double *x, const double *y, const int n)
632 {
633  double d = 0.0;
634
635  for (int i = 0; i < n; i++)
636  {
637  d += (x[i]-y[i])*(x[i]-y[i]);
638  }
639
640  return d;
641 }
642
643 inline double Distance(const double *x, const double *y, const int n)
644 {
645  return std::sqrt(DistanceSquared(x, y, n));
646 }
647
648 inline double Distance(const Vector &x, const Vector &y)
649 {
650  return x.DistanceTo(y);
651 }
652
653 inline double Vector::DistanceSquaredTo(const double *p) const
654 {
655  return DistanceSquared(data, p, size);
656 }
657
658 inline double Vector::DistanceSquaredTo(const Vector &p) const
659 {
660  MFEM_ASSERT(p.Size() == Size(), "Incompatible vector sizes.");
661  return DistanceSquared(data, p.data, size);
662 }
663
664 inline double Vector::DistanceTo(const double *p) const
665 {
666  return Distance(data, p, size);
667 }
668
669 inline double Vector::DistanceTo(const Vector &p) const
670 {
671  MFEM_ASSERT(p.Size() == Size(), "Incompatible vector sizes.");
672  return Distance(data, p.data, size);
673 }
674
675 /// Returns the inner product of x and y
676 /** In parallel this computes the inner product of the local vectors,
677  producing different results on each MPI rank.
678 */
679 inline double InnerProduct(const Vector &x, const Vector &y)
680 {
681  return x * y;
682 }
683
684 #ifdef MFEM_USE_MPI
685 /// Returns the inner product of x and y in parallel
686 /** In parallel this computes the inner product of the global vectors,
687  producing identical results on each MPI rank.
688 */
689 inline double InnerProduct(MPI_Comm comm, const Vector &x, const Vector &y)
690 {
691  double loc_prod = x * y;
692  double glb_prod;
693  MPI_Allreduce(&loc_prod, &glb_prod, 1, MPI_DOUBLE, MPI_SUM, comm);
694  return glb_prod;
695 }
696 #endif
697
698 } // namespace mfem
699
700 #endif
void SetSubVector(const Array< int > &dofs, const double value)
Set the entries listed in dofs to the given value.
Definition: vector.cpp:574
void SetVector(const Vector &v, int offset)
Definition: vector.cpp:280
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:492
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
Memory< double > data
Definition: vector.hpp:64
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 Print_HYPRE(std::ostream &out) const
Prints vector to stream out in HYPRE_Vector format.
Definition: vector.cpp:759
const Memory< double > & GetMemory() const
Return a reference to the Memory object used by the Vector, const version.
Definition: vector.hpp:234
void SyncMemory(const Vector &v) const
Update the memory location of the vector to match v.
Definition: vector.hpp:237
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:512
void Delete()
Delete the owned pointers and reset the Memory object.
virtual const double * HostRead() const
Definition: vector.hpp:452
virtual void UseDevice(bool use_dev) const
Enable execution of Vector operations using the mfem::Device.
Definition: vector.hpp:117
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 - v2.
Definition: vector.cpp:461
void Print(std::ostream &out=mfem::out, int width=8) const
Prints vector to stream out.
Definition: vector.cpp:725
double & operator()(int i)
Access Vector entries using () for 0-based indexing.
Definition: vector.hpp:598
void StealData(double **p)
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:247
int Size() const
Returns the size of the vector.
Definition: vector.hpp:199
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:460
void Swap< Vector >(Vector &a, Vector &b)
Specialization of the template function Swap<> for class Vector.
Definition: vector.hpp:621
virtual const double * Read(bool on_dev=true) const
Definition: vector.hpp:448
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Extract entries listed in dofs to the output Vector elemvect.
Definition: vector.cpp:548
void Randomize(int seed=0)
Set random values in the vector.
Definition: vector.cpp:786
Memory< double > & GetMemory()
Return a reference to the Memory object used by the Vector.
Definition: vector.hpp:230
T ZeroSubnormal(T val)
Definition: vector.hpp:476
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
void AddSubVector(const Vector &v, int offset)
Definition: vector.cpp:293
Vector & operator=(const double *v)
Copy Size() entries from v.
Definition: vector.cpp:124
const double * end() const
STL-like end (const version).
Definition: vector.hpp:227
bool IsFinite(const double &val)
Definition: vector.hpp:481
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
Definition: vector.cpp:57
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:269
void SetSize(int s, Vector &v)
Resize the vector to size s using the MemoryType of v.
Definition: vector.hpp:145
double DistanceTo(const double *p) const
Compute the Euclidean distance to another vector.
Definition: vector.hpp:664
void Swap(Vector &other)
Swap the contents of two Vectors.
Definition: vector.hpp:614
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:442
virtual double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:456
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...
Vector(const double(&values)[N])
Create a vector using a braced initializer list.
Definition: vector.hpp:105
double Sum() const
Return the sum of the vector entries.
Definition: vector.cpp:912
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
Definition: vector.cpp:524
double DistanceSquared(const double *x, const double *y, const int n)
Definition: vector.hpp:631
Load a vector from an input stream, reading the size from the stream.
Definition: vector.hpp:129
const double & operator[](int i) const
Definition: vector.hpp:273
double * StealData()
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
int Capacity() const
Return the size of the allocated memory.
void SetData(double *d)
Definition: vector.hpp:149
const T * Read(const Memory< T > &mem, int size, bool on_dev=true)
Definition: device.hpp:319
double Normlp(double p) const
Returns the l_p norm of the vector.
Definition: vector.cpp:844
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:640
Vector & operator/=(double c)
Definition: vector.cpp:189
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:709
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:208
Vector & operator*=(double c)
Definition: vector.cpp:168
Vector & operator+=(double c)
Definition: vector.cpp:232
void Swap(Array< T > &, Array< T > &)
Definition: array.hpp:635
double Distance(const double *x, const double *y, const int n)
Definition: vector.hpp:643
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:1194
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:778
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
Definition: vector.hpp:244
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
double Max() const
Returns the maximal element of the vector.
Definition: vector.cpp:894
double Norml1() const
Returns the l_1 norm of the vector.
Definition: vector.cpp:833
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:268
double * begin()
STL-like begin.
Definition: vector.hpp:218
double a
Definition: lissajous.cpp:41
void MakeDataOwner() const
Set the Vector data (host pointer) ownership flag.
Definition: vector.hpp:187
double InnerProduct(HypreParVector *x, HypreParVector *y)
Definition: hypre.cpp:414
Definition: vector.hpp:464
Vector & Add(const double a, const Vector &Va)
(*this) += a * Va
Definition: vector.cpp:253
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:240
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:561
void Destroy()
Destroy a vector.
Definition: vector.hpp:589
Vector & operator-=(double c)
Definition: vector.cpp:211
double infinity()
Define a shortcut for std::numeric_limits<double>::infinity()
Definition: vector.hpp:46
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:804
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:221
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:120
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:314
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
Definition: vector.hpp:576
bool UseDevice() const
Vector(int size_, MemoryType mt)
Create a Vector of size size_ using MemoryType mt.
Definition: vector.hpp:95
RefCoord s[3]
double Normlinf() const
Returns the l_infinity norm of the vector.
Definition: vector.cpp:822
double DistanceSquaredTo(const double *p) const
Compute the square of the Euclidean distance to another vector.
Definition: vector.hpp:653
const double * begin() const
STL-like begin (const version).
Definition: vector.hpp:224
Definition: vector.hpp:468
virtual ~Vector()
Destroys vector.
Definition: vector.hpp:626
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:111
void Neg()
(*this) = -(*this)
Definition: vector.cpp:306
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