MFEM  v4.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, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_VECTOR
13 #define MFEM_VECTOR
14 
15 #include "../general/array.hpp"
16 #include "../general/globals.hpp"
17 #include "../general/mem_manager.hpp"
18 #include "../general/device.hpp"
19 #ifdef MFEM_USE_SUNDIALS
20 #include <nvector/nvector_serial.h>
21 #endif
22 #include <cmath>
23 #include <iostream>
24 #include <limits>
25 #if defined(_MSC_VER) && (_MSC_VER < 1800)
26 #include <float.h>
27 #define isfinite _finite
28 #endif
29 
30 #ifdef MFEM_USE_MPI
31 #include <mpi.h>
32 #endif
33 
34 namespace mfem
35 {
36 
37 /** Count the number of entries in an array of doubles for which isfinite
38  is false, i.e. the entry is a NaN or +/-Inf. */
39 inline int CheckFinite(const double *v, const int n);
40 
41 /// Define a shortcut for std::numeric_limits<double>::infinity()
42 inline double infinity()
43 {
45 }
46 
47 /// Vector data type.
48 class Vector
49 {
50 protected:
51 
53  int size;
54 
55 public:
56 
57  /// Default constructor for Vector. Sets size = 0 and data = NULL.
58  Vector() { data.Reset(); size = 0; }
59 
60  /// Copy constructor. Allocates a new data array and copies the data.
61  Vector(const Vector &);
62 
63  /// @brief Creates vector of size s.
64  /// @warning Entries are not initialized to zero!
65  explicit Vector(int s);
66 
67  /// Creates a vector referencing an array of doubles, owned by someone else.
68  /** The pointer @a _data can be NULL. The data array can be replaced later
69  with SetData(). */
70  Vector(double *_data, int _size)
71  { data.Wrap(_data, _size, false); size = _size; }
72 
73  /// Create a Vector of size @a size_ using MemoryType @a mt.
74  Vector(int size_, MemoryType mt)
75  : data(size_, mt), size(size_) { }
76 
77  /// Enable execution of Vector operations using the mfem::Device.
78  /** The default is to use Backend::CPU (serial execution on each MPI rank),
79  regardless of the mfem::Device configuration.
80 
81  When appropriate, MFEM functions and class methods will enable the use
82  of the mfem::Device for their Vector parameters.
83 
84  Some derived classes, e.g. GridFunction, enable the use of the
85  mfem::Device by default. */
86  void UseDevice(bool use_dev) const { data.UseDevice(use_dev); }
87 
88  /// Return the device flag of the Memory object used by the Vector
89  bool UseDevice() const { return data.UseDevice(); }
90 
91  /// Reads a vector from multiple files
92  void Load(std::istream ** in, int np, int * dim);
93 
94  /// Load a vector from an input stream.
95  void Load(std::istream &in, int Size);
96 
97  /// Load a vector from an input stream, reading the size from the stream.
98  void Load(std::istream &in) { int s; in >> s; Load(in, s); }
99 
100  /// @brief Resize the vector to size @a s.
101  /** If the new size is less than or equal to Capacity() then the internal
102  data array remains the same. Otherwise, the old array is deleted, if
103  owned, and a new array of size @a s is allocated without copying the
104  previous content of the Vector.
105  @warning In the second case above (new size greater than current one),
106  the vector will allocate new data array, even if it did not own the
107  original data! Also, new entries are not initialized! */
108  void SetSize(int s);
109 
110  /// Resize the vector to size @a s using MemoryType @a mt.
111  void SetSize(int s, MemoryType mt);
112 
113  /// Resize the vector to size @a s using the MemoryType of @a v.
114  void SetSize(int s, Vector &v) { SetSize(s, v.GetMemory().GetMemoryType()); }
115 
116  /// Set the Vector data.
117  /// @warning This method should be called only when OwnsData() is false.
118  void SetData(double *d) { data.Wrap(d, data.Capacity(), false); }
119 
120  /// Set the Vector data and size.
121  /** The Vector does not assume ownership of the new data. The new size is
122  also used as the new Capacity().
123  @warning This method should be called only when OwnsData() is false.
124  @sa NewDataAndSize(). */
125  void SetDataAndSize(double *d, int s) { data.Wrap(d, s, false); size = s; }
126 
127  /// Set the Vector data and size, deleting the old data, if owned.
128  /** The Vector does not assume ownership of the new data. The new size is
129  also used as the new Capacity().
130  @sa SetDataAndSize(). */
131  void NewDataAndSize(double *d, int s)
132  {
133  data.Delete();
134  SetDataAndSize(d, s);
135  }
136 
137  /// Reset the Vector to use the given external Memory @a mem and size @a s.
138  /** If @a own_mem is false, the Vector will not own any of the pointers of
139  @a mem.
140  @sa NewDataAndSize(). */
141  inline void NewMemoryAndSize(const Memory<double> &mem, int s, bool own_mem);
142 
143  /// Set the Vector data (host pointer) ownership flag.
144  void MakeDataOwner() const { data.SetHostPtrOwner(true); }
145 
146  /// Destroy a vector
147  void Destroy();
148 
149  /// Returns the size of the vector.
150  inline int Size() const { return size; }
151 
152  /// Return the size of the currently allocated data array.
153  /** It is always true that Capacity() >= Size(). */
154  inline int Capacity() const { return data.Capacity(); }
155 
156  /// Return a pointer to the beginning of the Vector data.
157  /** @warning This method should be used with caution as it gives write access
158  to the data of const-qualified Vector%s. */
159  inline double *GetData() const
160  { return const_cast<double*>((const double*)data); }
161 
162  /// Conversion to `double *`.
163  /** @note This conversion function makes it possible to use [] for indexing
164  in addition to the overloaded operator()(int). */
165  inline operator double *() { return data; }
166 
167  /// Conversion to `const double *`.
168  /** @note This conversion function makes it possible to use [] for indexing
169  in addition to the overloaded operator()(int). */
170  inline operator const double *() const { return data; }
171 
172  /// Return a reference to the Memory object used by the Vector.
174 
175  /** @brief Return a reference to the Memory object used by the Vector, const
176  version. */
177  const Memory<double> &GetMemory() const { return data; }
178 
179  /// Update the memory location of the vector to match @a v.
180  void SyncMemory(const Vector &v) { GetMemory().Sync(v.GetMemory()); }
181 
182  /// Update the alias memory location of the vector to match @a v.
183  void SyncAliasMemory(const Vector &v)
184  { GetMemory().SyncAlias(v.GetMemory(),Size()); }
185 
186  /// Read the Vector data (host pointer) ownership flag.
187  inline bool OwnsData() const { return data.OwnsHostPtr(); }
188 
189  /// Changes the ownership of the data; after the call the Vector is empty
190  inline void StealData(double **p)
191  { *p = data; data.Reset(); size = 0; }
192 
193  /// Changes the ownership of the data; after the call the Vector is empty
194  inline double *StealData() { double *p; StealData(&p); return p; }
195 
196  /// Access Vector entries. Index i = 0 .. size-1.
197  double &Elem(int i);
198 
199  /// Read only access to Vector entries. Index i = 0 .. size-1.
200  const double &Elem(int i) const;
201 
202  /// Access Vector entries using () for 0-based indexing.
203  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
204  inline double &operator()(int i);
205 
206  /// Read only access to Vector entries using () for 0-based indexing.
207  /** @note If MFEM_DEBUG is enabled, bounds checking is performed. */
208  inline const double &operator()(int i) const;
209 
210  /// Dot product with a `double *` array.
211  double operator*(const double *) const;
212 
213  /// Return the inner-product.
214  double operator*(const Vector &v) const;
215 
216  /// Copy Size() entries from @a v.
217  Vector &operator=(const double *v);
218 
219  /// Copy assignment.
220  /** @note Defining this method overwrites the implicitly defined copy
221  assignemnt operator. */
222  Vector &operator=(const Vector &v);
223 
224  /// Redefine '=' for vector = constant.
225  Vector &operator=(double value);
226 
227  Vector &operator*=(double c);
228 
229  Vector &operator/=(double c);
230 
231  Vector &operator-=(double c);
232 
233  Vector &operator-=(const Vector &v);
234 
235  Vector &operator+=(const Vector &v);
236 
237  /// (*this) += a * Va
238  Vector &Add(const double a, const Vector &Va);
239 
240  /// (*this) = a * x
241  Vector &Set(const double a, const Vector &x);
242 
243  void SetVector(const Vector &v, int offset);
244 
245  /// (*this) = -(*this)
246  void Neg();
247 
248  /// Swap the contents of two Vectors
249  inline void Swap(Vector &other);
250 
251  /// Set v = v1 + v2.
252  friend void add(const Vector &v1, const Vector &v2, Vector &v);
253 
254  /// Set v = v1 + alpha * v2.
255  friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
256 
257  /// z = a * (x + y)
258  friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
259 
260  /// z = a * x + b * y
261  friend void add(const double a, const Vector &x,
262  const double b, const Vector &y, Vector &z);
263 
264  /// Set v = v1 - v2.
265  friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
266 
267  /// z = a * (x - y)
268  friend void subtract(const double a, const Vector &x,
269  const Vector &y, Vector &z);
270 
271  /// v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
272  void median(const Vector &lo, const Vector &hi);
273 
274  void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
275  void GetSubVector(const Array<int> &dofs, double *elem_data) const;
276 
277  /// Set the entries listed in `dofs` to the given `value`.
278  void SetSubVector(const Array<int> &dofs, const double value);
279  void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
280  void SetSubVector(const Array<int> &dofs, double *elem_data);
281 
282  /// Add (element) subvector to the vector.
283  void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
284  void AddElementVector(const Array<int> & dofs, double *elem_data);
285  void AddElementVector(const Array<int> & dofs, const double a,
286  const Vector & elemvect);
287 
288  /// Set all vector entries NOT in the 'dofs' array to the given 'val'.
289  void SetSubVectorComplement(const Array<int> &dofs, const double val);
290 
291  /// Prints vector to stream out.
292  void Print(std::ostream &out = mfem::out, int width = 8) const;
293 
294  /// Prints vector to stream out in HYPRE_Vector format.
295  void Print_HYPRE(std::ostream &out) const;
296 
297  /// Set random values in the vector.
298  void Randomize(int seed = 0);
299  /// Returns the l2 norm of the vector.
300  double Norml2() const;
301  /// Returns the l_infinity norm of the vector.
302  double Normlinf() const;
303  /// Returns the l_1 norm of the vector.
304  double Norml1() const;
305  /// Returns the l_p norm of the vector.
306  double Normlp(double p) const;
307  /// Returns the maximal element of the vector.
308  double Max() const;
309  /// Returns the minimal element of the vector.
310  double Min() const;
311  /// Return the sum of the vector entries
312  double Sum() const;
313  /// Compute the square of the Euclidean distance to another vector.
314  inline double DistanceSquaredTo(const double *p) const;
315  /// Compute the Euclidean distance to another vector.
316  inline double DistanceTo(const double *p) const;
317 
318  /** @brief Count the number of entries in the Vector for which isfinite
319  is false, i.e. the entry is a NaN or +/-Inf. */
320  int CheckFinite() const { return mfem::CheckFinite(data, size); }
321 
322  /// Destroys vector.
323  virtual ~Vector();
324 
325  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
326  const double *Read(bool on_dev = true) const
327  { return mfem::Read(data, size, on_dev); }
328 
329  /// Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
330  const double *HostRead() const
331  { return mfem::Read(data, size, false); }
332 
333  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
334  double *Write(bool on_dev = true)
335  { return mfem::Write(data, size, on_dev); }
336 
337  /// Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
338  double *HostWrite()
339  { return mfem::Write(data, size, false); }
340 
341  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
342  double *ReadWrite(bool on_dev = true)
343  { return mfem::ReadWrite(data, size, on_dev); }
344 
345  /// Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
346  double *HostReadWrite()
347  { return mfem::ReadWrite(data, size, false); }
348 
349 #ifdef MFEM_USE_SUNDIALS
350  /// Construct a wrapper Vector from SUNDIALS N_Vector.
351  explicit Vector(N_Vector nv);
352 
353  /// Return a new wrapper SUNDIALS N_Vector of type SUNDIALS_NVEC_SERIAL.
354  /** The returned N_Vector must be destroyed by the caller. */
355  virtual N_Vector ToNVector() { return N_VMake_Serial(Size(), GetData()); }
356 
357  /** @brief Update an existing wrapper SUNDIALS N_Vector to point to this
358  Vector. */
359  virtual void ToNVector(N_Vector &nv);
360 #endif
361 };
362 
363 // Inline methods
364 
365 inline bool IsFinite(const double &val)
366 {
367  // isfinite didn't appear in a standard until C99, and later C++11. It wasn't
368  // standard in C89 or C++98. PGI as of 14.7 still defines it as a macro.
369 #ifdef isfinite
370  return isfinite(val);
371 #else
372  return std::isfinite(val);
373 #endif
374 }
375 
376 inline int CheckFinite(const double *v, const int n)
377 {
378  int bad = 0;
379  for (int i = 0; i < n; i++)
380  {
381  if (!IsFinite(v[i])) { bad++; }
382  }
383  return bad;
384 }
385 
386 inline Vector::Vector(int s)
387 {
388  if (s > 0)
389  {
390  size = s;
391  data.New(s);
392  }
393  else
394  {
395  size = 0;
396  data.Reset();
397  }
398 }
399 
400 inline void Vector::SetSize(int s)
401 {
402  if (s == size)
403  {
404  return;
405  }
406  if (s <= data.Capacity())
407  {
408  size = s;
409  return;
410  }
411  // preserve a valid MemoryType and device flag
412  const MemoryType mt = data.GetMemoryType();
413  const bool use_dev = data.UseDevice();
414  data.Delete();
415  size = s;
416  data.New(s, mt);
417  data.UseDevice(use_dev);
418 }
419 
420 inline void Vector::SetSize(int s, MemoryType mt)
421 {
422  if (mt == data.GetMemoryType())
423  {
424  if (s == size)
425  {
426  return;
427  }
428  if (s <= data.Capacity())
429  {
430  size = s;
431  return;
432  }
433  }
434  const bool use_dev = data.UseDevice();
435  data.Delete();
436  if (s > 0)
437  {
438  data.New(s, mt);
439  size = s;
440  }
441  else
442  {
443  data.Reset();
444  size = 0;
445  }
446  data.UseDevice(use_dev);
447 }
448 
449 inline void Vector::NewMemoryAndSize(const Memory<double> &mem, int s,
450  bool own_mem)
451 {
452  data.Delete();
453  size = s;
454  data = mem;
455  if (!own_mem) { data.ClearOwnerFlags(); }
456 }
457 
458 inline void Vector::Destroy()
459 {
460  const bool use_dev = data.UseDevice();
461  data.Delete();
462  size = 0;
463  data.Reset();
464  data.UseDevice(use_dev);
465 }
466 
467 inline double &Vector::operator()(int i)
468 {
469  MFEM_ASSERT(data && i >= 0 && i < size,
470  "index [" << i << "] is out of range [0," << size << ")");
471 
472  return data[i];
473 }
474 
475 inline const double &Vector::operator()(int i) const
476 {
477  MFEM_ASSERT(data && i >= 0 && i < size,
478  "index [" << i << "] is out of range [0," << size << ")");
479 
480  return data[i];
481 }
482 
483 inline void Vector::Swap(Vector &other)
484 {
485  mfem::Swap(data, other.data);
486  mfem::Swap(size, other.size);
487 }
488 
489 /// Specialization of the template function Swap<> for class Vector
490 template<> inline void Swap<Vector>(Vector &a, Vector &b)
491 {
492  a.Swap(b);
493 }
494 
496 {
497  data.Delete();
498 }
499 
500 inline double DistanceSquared(const double *x, const double *y, const int n)
501 {
502  double d = 0.0;
503 
504  for (int i = 0; i < n; i++)
505  {
506  d += (x[i]-y[i])*(x[i]-y[i]);
507  }
508 
509  return d;
510 }
511 
512 inline double Distance(const double *x, const double *y, const int n)
513 {
514  return std::sqrt(DistanceSquared(x, y, n));
515 }
516 
517 inline double Vector::DistanceSquaredTo(const double *p) const
518 {
519  return DistanceSquared(data, p, size);
520 }
521 
522 inline double Vector::DistanceTo(const double *p) const
523 {
524  return Distance(data, p, size);
525 }
526 
527 /// Returns the inner product of x and y
528 /** In parallel this computes the inner product of the local vectors,
529  producing different results on each MPI rank.
530 */
531 inline double InnerProduct(const Vector &x, const Vector &y)
532 {
533  return x * y;
534 }
535 
536 #ifdef MFEM_USE_MPI
537 /// Returns the inner product of x and y in parallel
538 /** In parallel this computes the inner product of the global vectors,
539  producing identical results on each MPI rank.
540 */
541 inline double InnerProduct(MPI_Comm comm, const Vector &x, const Vector &y)
542 {
543  double loc_prod = x * y;
544  double glb_prod;
545  MPI_Allreduce(&loc_prod, &glb_prod, 1, MPI_DOUBLE, MPI_SUM, comm);
546  return glb_prod;
547 }
548 #endif
549 
550 } // namespace mfem
551 
552 #endif
void SetSubVector(const Array< int > &dofs, const double value)
Set the entries listed in dofs to the given value.
Definition: vector.cpp:498
void SetVector(const Vector &v, int offset)
Definition: vector.cpp:217
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:376
double * HostReadWrite()
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:346
Vector()
Default constructor for Vector. Sets size = 0 and data = NULL.
Definition: vector.hpp:58
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:131
void MakeDataOwner() const
Set the Vector data (host pointer) ownership flag.
Definition: vector.hpp:144
Memory< double > data
Definition: vector.hpp:52
double & Elem(int i)
Access Vector entries. Index i = 0 .. size-1.
Definition: vector.cpp:83
void SyncMemory(const Vector &v)
Update the memory location of the vector to match v.
Definition: vector.hpp:180
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:400
void Delete()
Delete the owned pointers. The Memory is not reset by this method.
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 - v2.
Definition: vector.cpp:385
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:709
double & operator()(int i)
Access Vector entries using () for 0-based indexing.
Definition: vector.hpp:467
void UseDevice(bool use_dev) const
Enable execution of Vector operations using the mfem::Device.
Definition: vector.hpp:86
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Definition: vector.cpp:472
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:190
int Capacity() const
Return the size of the currently allocated data array.
Definition: vector.hpp:154
T * Write(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for write access to mem with the mfem::Device MemoryClass, if on_dev = true...
Definition: device.hpp:249
int Size() const
Returns the size of the vector.
Definition: vector.hpp:150
void Swap< Vector >(Vector &a, Vector &b)
Specialization of the template function Swap&lt;&gt; for class Vector.
Definition: vector.hpp:490
double Normlinf() const
Returns the l_infinity norm of the vector.
Definition: vector.cpp:746
void Randomize(int seed=0)
Set random values in the vector.
Definition: vector.cpp:690
double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:334
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:159
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:173
double operator*(const double *) const
Dot product with a double * array.
Definition: vector.cpp:93
MemoryType GetMemoryType() const
Return a MemoryType that is currently valid. If both the host and the device pointers are currently v...
Vector & operator=(const double *v)
Copy Size() entries from v.
Definition: vector.cpp:106
bool IsFinite(const double &val)
Definition: vector.hpp:365
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
Definition: vector.cpp:51
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
Definition: vector.hpp:187
void Wrap(T *ptr, int size, bool own)
Wrap an externally allocated host pointer, ptr with type MemoryType::HOST.
const double * HostRead() const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:330
double Normlp(double p) const
Returns the l_p norm of the vector.
Definition: vector.cpp:766
void SetSize(int s, Vector &v)
Resize the vector to size s using the MemoryType of v.
Definition: vector.hpp:114
int dim
Definition: ex3.cpp:48
double * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:342
bool UseDevice() const
Return the device flag of the Memory object used by the Vector.
Definition: vector.hpp:89
void Swap(Vector &other)
Swap the contents of two Vectors.
Definition: vector.hpp:483
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo &lt;= hi.
Definition: vector.cpp:448
double DistanceSquared(const double *x, const double *y, const int n)
Definition: vector.hpp:500
void Load(std::istream &in)
Load a vector from an input stream, reading the size from the stream.
Definition: vector.hpp:98
double * StealData()
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:194
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:118
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 MemoryClass, if on_dev = true...
Definition: device.hpp:225
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 (element) subvector to the vector.
Definition: vector.cpp:564
Vector & operator/=(double c)
Definition: vector.cpp:147
double DistanceTo(const double *p) const
Compute the Euclidean distance to another vector.
Definition: vector.hpp:522
void SetSubVectorComplement(const Array< int > &dofs, const double val)
Set all vector entries NOT in the &#39;dofs&#39; array to the given &#39;val&#39;.
Definition: vector.cpp:633
Vector(double *_data, int _size)
Creates a vector referencing an array of doubles, owned by someone else.
Definition: vector.hpp:70
Vector & operator*=(double c)
Definition: vector.cpp:138
double Min() const
Returns the minimal element of the vector.
Definition: vector.cpp:980
double Norml1() const
Returns the l_1 norm of the vector.
Definition: vector.cpp:756
void Swap(Array< T > &, Array< T > &)
Definition: array.hpp:575
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:512
void Print(std::ostream &out=mfem::out, int width=8) const
Prints vector to stream out.
Definition: vector.cpp:647
MemoryType
Memory types supported by MFEM.
Definition: mem_manager.hpp:27
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:671
double DistanceSquaredTo(const double *p) const
Compute the square of the Euclidean distance to another vector.
Definition: vector.hpp:517
void SetDataAndSize(double *d, int s)
Set the Vector data and size.
Definition: vector.hpp:125
Vector & Set(const double a, const Vector &x)
(*this) = a * x
Definition: vector.cpp:205
double InnerProduct(HypreParVector *x, HypreParVector *y)
Definition: hypre.cpp:253
Vector & Add(const double a, const Vector &Va)
(*this) += a * Va
Definition: vector.cpp:190
T * ReadWrite(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for read+write access to mem with the mfem::Device MemoryClass, if on_dev = true...
Definition: device.hpp:273
const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:326
double Max() const
Returns the maximal element of the vector.
Definition: vector.cpp:816
void New(int size)
Allocate host memory for size entries with type MemoryType::HOST.
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:449
void Destroy()
Destroy a vector.
Definition: vector.hpp:458
Vector & operator-=(double c)
Definition: vector.cpp:157
double infinity()
Define a shortcut for std::numeric_limits&lt;double&gt;::infinity()
Definition: vector.hpp:42
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:320
double * HostWrite()
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:338
const double alpha
Definition: ex15.cpp:339
Vector data type.
Definition: vector.hpp:48
friend void add(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 + v2.
Definition: vector.cpp:238
Vector(int size_, MemoryType mt)
Create a Vector of size size_ using MemoryType mt.
Definition: vector.hpp:74
bool UseDevice() const
Read the internal device flag.
Vector & operator+=(const Vector &v)
Definition: vector.cpp:178
const Memory< double > & GetMemory() const
Return a reference to the Memory object used by the Vector, const version.
Definition: vector.hpp:177
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:64
double Sum() const
Return the sum of the vector entries.
Definition: vector.cpp:833
virtual N_Vector ToNVector()
Return a new wrapper SUNDIALS N_Vector of type SUNDIALS_NVEC_SERIAL.
Definition: vector.hpp:355
virtual ~Vector()
Destroys vector.
Definition: vector.hpp:495
void Neg()
(*this) = -(*this)
Definition: vector.cpp:230
void SyncAliasMemory(const Vector &v)
Update the alias memory location of the vector to match v.
Definition: vector.hpp:183