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