MFEM  v4.3.0 Finite element discretization library
vector.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2021, 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 data = NULL.
70  Vector() { data.Reset(); size = 0; }
71
72  /// Copy constructor. Allocates a new data array and copies the data.
73  Vector(const Vector &);
74
75  /// @brief Creates vector of size s.
76  /// @warning Entries are not initialized to zero!
77  explicit Vector(int s);
78
79  /// Creates a vector referencing an array of doubles, owned by someone else.
80  /** The pointer @a data_ can be NULL. The data array can be replaced later
81  with SetData(). */
82  Vector(double *data_, int size_)
83  { data.Wrap(data_, size_, false); size = size_; }
84
85  /// Create a Vector of size @a size_ using MemoryType @a mt.
86  Vector(int size_, MemoryType mt)
87  : data(size_, mt), size(size_) { }
88
89  /** @brief Create a Vector of size @a size_ using host MemoryType @a h_mt and
90  device MemoryType @a d_mt. */
91  Vector(int size_, MemoryType h_mt, MemoryType d_mt)
92  : data(size_, h_mt, d_mt), size(size_) { }
93
94  /// Create a vector using a braced initializer list
95  template <int N>
96  explicit Vector(const double (&values)[N]) : Vector(N)
97  { std::copy(values, values + N, GetData()); }
98
99  /// Enable execution of Vector operations using the mfem::Device.
100  /** The default is to use Backend::CPU (serial execution on each MPI rank),
101  regardless of the mfem::Device configuration.
102
103  When appropriate, MFEM functions and class methods will enable the use
104  of the mfem::Device for their Vector parameters.
105
106  Some derived classes, e.g. GridFunction, enable the use of the
107  mfem::Device by default. */
108  virtual void UseDevice(bool use_dev) const { data.UseDevice(use_dev); }
109
110  /// Return the device flag of the Memory object used by the Vector
111  virtual bool UseDevice() const { return data.UseDevice(); }
112
113  /// Reads a vector from multiple files
114  void Load(std::istream ** in, int np, int * dim);
115
116  /// Load a vector from an input stream.
117  void Load(std::istream &in, int Size);
118
119  /// Load a vector from an input stream, reading the size from the stream.
120  void Load(std::istream &in) { int s; in >> s; Load(in, s); }
121
122  /// @brief Resize the vector to size @a s.
123  /** If the new size is less than or equal to Capacity() then the internal
124  data array remains the same. Otherwise, the old array is deleted, if
125  owned, and a new array of size @a s is allocated without copying the
126  previous content of the Vector.
127  @warning In the second case above (new size greater than current one),
128  the vector will allocate new data array, even if it did not own the
129  original data! Also, new entries are not initialized! */
130  void SetSize(int s);
131
132  /// Resize the vector to size @a s using MemoryType @a mt.
133  void SetSize(int s, MemoryType mt);
134
135  /// Resize the vector to size @a s using the MemoryType of @a v.
136  void SetSize(int s, Vector &v) { SetSize(s, v.GetMemory().GetMemoryType()); }
137
138  /// Set the Vector data.
139  /// @warning This method should be called only when OwnsData() is false.
140  void SetData(double *d) { data.Wrap(d, data.Capacity(), false); }
141
142  /// Set the Vector data and size.
143  /** The Vector does not assume ownership of the new data. The new size is
144  also used as the new Capacity().
145  @warning This method should be called only when OwnsData() is false.
146  @sa NewDataAndSize(). */
147  void SetDataAndSize(double *d, int s) { data.Wrap(d, s, false); size = s; }
148
149  /// Set the Vector data and size, deleting the old data, if owned.
150  /** The Vector does not assume ownership of the new data. The new size is
151  also used as the new Capacity().
152  @sa SetDataAndSize(). */
153  void NewDataAndSize(double *d, int s)
154  {
155  data.Delete();
156  SetDataAndSize(d, s);
157  }
158
159  /// Reset the Vector to use the given external Memory @a mem and size @a s.
160  /** If @a own_mem is false, the Vector will not own any of the pointers of
161  @a mem.
162
163  Note that when @a own_mem is true, the @a mem object can be destroyed
164  immediately by the caller but mem.Delete() should NOT be called since
165  the Vector object takes ownership of all pointers owned by @a mem.
166
167  @sa NewDataAndSize(). */
168  inline void NewMemoryAndSize(const Memory<double> &mem, int s, bool own_mem);
169
170  /// Reset the Vector to be a reference to a sub-vector of @a base.
171  inline void MakeRef(Vector &base, int offset, int size);
172
173  /** @brief Reset the Vector to be a reference to a sub-vector of @a base
174  without changing its current size. */
175  inline void MakeRef(Vector &base, int offset);
176
177  /// Set the Vector data (host pointer) ownership flag.
178  void MakeDataOwner() const { data.SetHostPtrOwner(true); }
179
180  /// Destroy a vector
181  void Destroy();
182
183  /** @brief Delete the device pointer, if owned. If @a copy_to_host is true
184  and the data is valid only on device, move it to host before deleting.
185  Invalidates the device memory. */
186  void DeleteDevice(bool copy_to_host = true)
187  { data.DeleteDevice(copy_to_host); }
188
189  /// Returns the size of the vector.
190  inline int Size() const { return size; }
191
192  /// Return the size of the currently allocated data array.
193  /** It is always true that Capacity() >= Size(). */
194  inline int Capacity() const { return data.Capacity(); }
195
196  /// Return a pointer to the beginning of the Vector data.
197  /** @warning This method should be used with caution as it gives write access
198  to the data of const-qualified Vector%s. */
199  inline double *GetData() const
200  { return const_cast<double*>((const double*)data); }
201
202  /// Conversion to double *.
203  /** @note This conversion function makes it possible to use [] for indexing
205  inline operator double *() { return data; }
206
207  /// Conversion to const double *.
208  /** @note This conversion function makes it possible to use [] for indexing
210  inline operator const double *() const { return data; }
211
212  /// STL-like begin.
213  inline double *begin() { return data; }
214
215  /// STL-like end.
216  inline double *end() { return data + size; }
217
218  /// STL-like begin (const version).
219  inline const double *begin() const { return data; }
220
221  /// STL-like end (const version).
222  inline const double *end() const { return data + size; }
223
224  /// Return a reference to the Memory object used by the Vector.
226
227  /** @brief Return a reference to the Memory object used by the Vector, const
228  version. */
229  const Memory<double> &GetMemory() const { return data; }
230
231  /// Update the memory location of the vector to match @a v.
232  void SyncMemory(const Vector &v) const { GetMemory().Sync(v.GetMemory()); }
233
234  /// Update the alias memory location of the vector to match @a v.
235  void SyncAliasMemory(const Vector &v) const
236  { GetMemory().SyncAlias(v.GetMemory(),Size()); }
237
238  /// Read the Vector data (host pointer) ownership flag.
239  inline bool OwnsData() const { return data.OwnsHostPtr(); }
240
241  /// Changes the ownership of the data; after the call the Vector is empty
242  inline void StealData(double **p)
243  { *p = data; data.Reset(); size = 0; }
244
245  /// Changes the ownership of the data; after the call the Vector is empty
246  inline double *StealData() { double *p; StealData(&p); return p; }
247
248  /// Access Vector entries. Index i = 0 .. size-1.
249  double &Elem(int i);
250
252  const double &Elem(int i) const;
253
254  /// Access Vector entries using () for 0-based indexing.
255  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
256  inline double &operator()(int i);
257
259  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
260  inline const double &operator()(int i) const;
261
262  /// Dot product with a double * array.
263  double operator*(const double *) const;
264
265  /// Return the inner-product.
266  double operator*(const Vector &v) const;
267
268  /// Copy Size() entries from @a v.
269  Vector &operator=(const double *v);
270
271  /// Copy assignment.
272  /** @note Defining this method overwrites the implicitly defined copy
273  assignment operator. */
274  Vector &operator=(const Vector &v);
275
276  /// Redefine '=' for vector = constant.
277  Vector &operator=(double value);
278
279  Vector &operator*=(double c);
280
281  /// Component-wise scaling: (*this)(i) *= v(i)
282  Vector &operator*=(const Vector &v);
283
284  Vector &operator/=(double c);
285
286  /// Component-wise division: (*this)(i) /= v(i)
287  Vector &operator/=(const Vector &v);
288
289  Vector &operator-=(double c);
290
291  Vector &operator-=(const Vector &v);
292
293  Vector &operator+=(double c);
294
295  Vector &operator+=(const Vector &v);
296
297  /// (*this) += a * Va
298  Vector &Add(const double a, const Vector &Va);
299
300  /// (*this) = a * x
301  Vector &Set(const double a, const Vector &x);
302
303  void SetVector(const Vector &v, int offset);
304
305  /// (*this) = -(*this)
306  void Neg();
307
308  /// Swap the contents of two Vectors
309  inline void Swap(Vector &other);
310
311  /// Set v = v1 + v2.
312  friend void add(const Vector &v1, const Vector &v2, Vector &v);
313
314  /// Set v = v1 + alpha * v2.
315  friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
316
317  /// z = a * (x + y)
318  friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
319
320  /// z = a * x + b * y
321  friend void add(const double a, const Vector &x,
322  const double b, const Vector &y, Vector &z);
323
324  /// Set v = v1 - v2.
325  friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
326
327  /// z = a * (x - y)
328  friend void subtract(const double a, const Vector &x,
329  const Vector &y, Vector &z);
330
331  /// v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
332  void median(const Vector &lo, const Vector &hi);
333
334  /// Extract entries listed in @a dofs to the output Vector @a elemvect.
335  /** Negative dof values cause the -dof-1 position in @a elemvect to receive
336  the -val in from this Vector. */
337  void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
338
339  /// Extract entries listed in @a dofs to the output array @a elem_data.
340  /** Negative dof values cause the -dof-1 position in @a elem_data to receive
341  the -val in from this Vector. */
342  void GetSubVector(const Array<int> &dofs, double *elem_data) const;
343
344  /// Set the entries listed in @a dofs to the given @a value.
345  /** Negative dof values cause the -dof-1 position in this Vector to receive
346  the -value. */
347  void SetSubVector(const Array<int> &dofs, const double value);
348
349  /** @brief Set the entries listed in @a dofs to the values given in the @a
350  elemvect Vector. Negative dof values cause the -dof-1 position in this
351  Vector to receive the -val from @a elemvect. */
352  void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
353
354  /** @brief Set the entries listed in @a dofs to the values given the @a ,
355  elem_data array. Negative dof values cause the -dof-1 position in this
356  Vector to receive the -val from @a elem_data. */
357  void SetSubVector(const Array<int> &dofs, double *elem_data);
358
359  /** @brief Add elements of the @a elemvect Vector to the entries listed in @a
360  dofs. Negative dof values cause the -dof-1 position in this Vector to add
361  the -val from @a elemvect. */
362  void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
363
364  /** @brief Add elements of the @a elem_data array to the entries listed in @a
365  dofs. Negative dof values cause the -dof-1 position in this Vector to add
366  the -val from @a elem_data. */
367  void AddElementVector(const Array<int> & dofs, double *elem_data);
368
369  /** @brief Add @a times the elements of the @a elemvect Vector to the entries
370  listed in @a dofs. Negative dof values cause the -dof-1 position in this
371  Vector to add the -a*val from @a elemvect. */
372  void AddElementVector(const Array<int> & dofs, const double a,
373  const Vector & elemvect);
374
375  /// Set all vector entries NOT in the @a dofs Array to the given @a val.
376  void SetSubVectorComplement(const Array<int> &dofs, const double val);
377
378  /// Prints vector to stream out.
379  void Print(std::ostream &out = mfem::out, int width = 8) const;
380
382  /// Prints vector to stream out.
383  /// @param out adios2stream output
384  /// @param variable_name variable name associated with current Vector
385  void Print(adios2stream & out, const std::string& variable_name) const;
386 #endif
387
388  /// Prints vector to stream out in HYPRE_Vector format.
389  void Print_HYPRE(std::ostream &out) const;
390
391  /// Print the Vector size and hash of its data.
392  /** This is a compact text representation of the Vector contents that can be
393  used to compare vectors from different runs without the need to save the
394  whole vector. */
395  void PrintHash(std::ostream &out) const;
396
397  /// Set random values in the vector.
398  void Randomize(int seed = 0);
399  /// Returns the l2 norm of the vector.
400  double Norml2() const;
401  /// Returns the l_infinity norm of the vector.
402  double Normlinf() const;
403  /// Returns the l_1 norm of the vector.
404  double Norml1() const;
405  /// Returns the l_p norm of the vector.
406  double Normlp(double p) const;
407  /// Returns the maximal element of the vector.
408  double Max() const;
409  /// Returns the minimal element of the vector.
410  double Min() const;
411  /// Return the sum of the vector entries
412  double Sum() const;
413  /// Compute the square of the Euclidean distance to another vector.
414  inline double DistanceSquaredTo(const double *p) const;
415  /// Compute the Euclidean distance to another vector.
416  inline double DistanceTo(const double *p) const;
417
418  /** @brief Count the number of entries in the Vector for which isfinite
419  is false, i.e. the entry is a NaN or +/-Inf. */
420  int CheckFinite() const { return mfem::CheckFinite(data, size); }
421
422  /// Destroys vector.
423  virtual ~Vector();
424
425  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
426  virtual const double *Read(bool on_dev = true) const
427  { return mfem::Read(data, size, on_dev); }
428
429  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
430  virtual const double *HostRead() const
431  { return mfem::Read(data, size, false); }
432
433  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
434  virtual double *Write(bool on_dev = true)
435  { return mfem::Write(data, size, on_dev); }
436
437  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
438  virtual double *HostWrite()
439  { return mfem::Write(data, size, false); }
440
441  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
442  virtual double *ReadWrite(bool on_dev = true)
443  { return mfem::ReadWrite(data, size, on_dev); }
444
445  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
447  { return mfem::ReadWrite(data, size, false); }
448
449 #ifdef MFEM_USE_SUNDIALS
450  /// (DEPRECATED) Construct a wrapper Vector from SUNDIALS N_Vector.
451  MFEM_DEPRECATED explicit Vector(N_Vector nv);
452
453  /// (DEPRECATED) Return a new wrapper SUNDIALS N_Vector of type SUNDIALS_NVEC_SERIAL.
454  /** @deprecated The returned N_Vector must be destroyed by the caller. */
455  MFEM_DEPRECATED virtual N_Vector ToNVector()
456  { return N_VMake_Serial(Size(), GetData()); }
457
458  /** @deprecated @brief Update an existing wrapper SUNDIALS N_Vector to point to this
459  Vector.
460
461  \param[in] nv N_Vector to assign this vector's data to
462  \param[in] global_length An optional parameter that designates the global
463  length. If nv is a parallel vector and global_length == 0 then this
464  method will perform a global reduction and calculate the global length
465  */
466  MFEM_DEPRECATED virtual void ToNVector(N_Vector &nv, long global_length = 0);
467 #endif
468 };
469
470 // Inline methods
471
472 template <typename T>
473 inline T ZeroSubnormal(T val)
474 {
475  return (std::fpclassify(val) == FP_SUBNORMAL) ? 0.0 : val;
476 }
477
478 inline bool IsFinite(const double &val)
479 {
480  // isfinite didn't appear in a standard until C99, and later C++11. It wasn't
481  // standard in C89 or C++98. PGI as of 14.7 still defines it as a macro.
482 #ifdef isfinite
483  return isfinite(val);
484 #else
485  return std::isfinite(val);
486 #endif
487 }
488
489 inline int CheckFinite(const double *v, const int n)
490 {
492  for (int i = 0; i < n; i++)
493  {
494  if (!IsFinite(v[i])) { bad++; }
495  }
497 }
498
499 inline Vector::Vector(int s)
500 {
501  if (s > 0)
502  {
503  size = s;
504  data.New(s);
505  }
506  else
507  {
508  size = 0;
509  data.Reset();
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:551
void SetVector(const Vector &v, int offset)
Definition: vector.cpp:270
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:489
Vector()
Default constructor for Vector. Sets size = 0 and data = NULL.
Definition: vector.hpp:70
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:186
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:153
void MakeDataOwner() const
Set the Vector data (host pointer) ownership flag.
Definition: vector.hpp:178
Memory< double > data
Definition: vector.hpp:64
const double * begin() const
STL-like begin (const version).
Definition: vector.hpp:219
Vector(double *data_, int size_)
Creates a vector referencing an array of doubles, owned by someone else.
Definition: vector.hpp:82
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. The Memory is not reset by this method, i.e. it will, generally, not be Empty() after this call.
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 - v2.
Definition: vector.cpp:438
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:783
double & operator()(int i)
Access Vector entries using () for 0-based indexing.
Definition: vector.hpp:599
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Extract entries listed in dofs to the output Vector elemvect.
Definition: vector.cpp:525
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:242
int Capacity() const
Return the size of the currently allocated data array.
Definition: vector.hpp:194
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:438
int Size() const
Returns the size of the vector.
Definition: vector.hpp:190
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:801
void Randomize(int seed=0)
Set random values in the vector.
Definition: vector.cpp:763
void SyncAliasMemory(const Vector &v) const
Update the alias memory location of the vector to match v.
Definition: vector.hpp:235
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:199
virtual void UseDevice(bool use_dev) const
Enable execution of Vector operations using the mfem::Device.
Definition: vector.hpp:108
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:225
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:473
const double * end() const
STL-like end (const version).
Definition: vector.hpp:222
Vector & operator=(const double *v)
Copy Size() entries from v.
Definition: vector.cpp:124
bool IsFinite(const double &val)
Definition: vector.hpp:478
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:239
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:823
void SetSize(int s, Vector &v)
Resize the vector to size s using the MemoryType of v.
Definition: vector.hpp:136
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:434
double b
Definition: lissajous.cpp:42
Vector(const double(&values)[N])
Create a vector using a braced initializer list.
Definition: vector.hpp:96
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo &lt;= hi.
Definition: vector.cpp:501
double DistanceSquared(const double *x, const double *y, const int n)
Definition: vector.hpp:632
Load a vector from an input stream, reading the size from the stream.
Definition: vector.hpp:120
double * StealData()
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:246
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:140
const T * Read(const Memory< T > &mem, int size, bool 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:232
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:617
virtual const double * HostRead() const
Definition: vector.hpp:430
Vector & operator/=(double c)
Definition: vector.cpp:179
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:686
Vector & operator*=(double c)
Definition: vector.cpp:158
double Min() const
Returns the minimal element of the vector.
Definition: vector.cpp:1173
Vector & operator+=(double c)
Definition: vector.cpp:222
double Norml1() const
Returns the l_1 norm of the vector.
Definition: vector.cpp:812
void Swap(Array< T > &, Array< T > &)
Definition: array.hpp:625
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:702
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:736
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:147
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:258
double * begin()
STL-like begin.
Definition: vector.hpp:213
double a
Definition: lissajous.cpp:41
double InnerProduct(HypreParVector *x, HypreParVector *y)
Definition: hypre.cpp:316
Definition: vector.hpp:442
Vector & Add(const double a, const Vector &Va)
(*this) += a * Va
Definition: vector.cpp:243
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:455
double Max() const
Returns the maximal element of the vector.
Definition: vector.cpp:873
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:201
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:420
double * end()
STL-like end.
Definition: vector.hpp:216
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:291
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:111
Vector(int size_, MemoryType mt)
Create a Vector of size size_ using MemoryType mt.
Definition: vector.hpp:86
bool UseDevice() const
void PrintHash(std::ostream &out) const
Print the Vector size and hash of its data.
Definition: vector.cpp:755
RefCoord s[3]
const Memory< double > & GetMemory() const
Return a reference to the Memory object used by the Vector, const version.
Definition: vector.hpp:229
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:891
virtual const double * Read(bool on_dev=true) const