15 #include "../general/array.hpp"
16 #ifdef MFEM_USE_ADIOS2
17 #include "../general/adios2stream.hpp"
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>
28 #if defined(_MSC_VER) && (_MSC_VER < 1800)
30 #define isfinite _finite
42 inline int CheckFinite(
const double *v,
const int n);
101 :
data(size_, h_mt, d_mt),
size(size_) { }
106 { std::copy(values, values + N,
GetData()); }
123 void Load(std::istream ** in,
int np,
int *
dim);
126 void Load(std::istream &in,
int Size);
209 {
return const_cast<double*
>((
const double*)
data); }
214 inline operator double *() {
return data; }
219 inline operator const double *()
const {
return data; }
261 const double &
Elem(
int i)
const;
333 friend void add(
const double a,
const Vector &x,
393 #ifdef MFEM_USE_ADIOS2
438 virtual const double *
Read(
bool on_dev =
true)
const
446 virtual double *
Write(
bool on_dev =
true)
461 #ifdef MFEM_USE_SUNDIALS
463 MFEM_DEPRECATED
explicit Vector(N_Vector nv);
478 MFEM_DEPRECATED
virtual void ToNVector(N_Vector &nv,
long global_length = 0);
484 template <
typename T>
487 return (std::fpclassify(val) == FP_SUBNORMAL) ? 0.0 : val;
495 return isfinite(val);
497 return std::isfinite(val);
504 for (
int i = 0; i < n; i++)
513 MFEM_ASSERT(s>=0,
"Unexpected negative size.");
609 MFEM_ASSERT(
data && i >= 0 && i <
size,
610 "index [" << i <<
"] is out of range [0," <<
size <<
")");
617 MFEM_ASSERT(
data && i >= 0 && i <
size,
618 "index [" << i <<
"] is out of range [0," <<
size <<
")");
644 for (
int i = 0; i < n; i++)
646 d += (x[i]-y[i])*(x[i]-y[i]);
652 inline double Distance(
const double *x,
const double *y,
const int n)
683 double loc_prod = x * y;
685 MPI_Allreduce(&loc_prod, &glb_prod, 1, MPI_DOUBLE, MPI_SUM, comm);
void SetSubVector(const Array< int > &dofs, const double value)
Set the entries listed in dofs to the given value.
void SetVector(const Vector &v, int offset)
int CheckFinite(const double *v, const int n)
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 NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
void MakeDataOwner() const
Set the Vector data (host pointer) ownership flag.
const double * begin() const
STL-like begin (const version).
Vector(double *data_, int size_)
Creates a vector referencing an array of doubles, owned by someone else.
double & Elem(int i)
Access Vector entries. Index i = 0 .. size-1.
void SetSize(int s)
Resize the vector to size s.
void Delete()
Delete the owned pointers and reset the Memory object.
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 - v2.
double Norml2() const
Returns the l2 norm of the vector.
double & operator()(int i)
Access Vector entries using () for 0-based indexing.
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Extract entries listed in dofs to the output Vector elemvect.
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.
int Capacity() const
Return the size of the currently allocated data array.
T * Write(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for write access to mem with the mfem::Device's DeviceMemoryClass, if on_dev = true...
virtual double * HostWrite()
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
int Size() const
Returns the size of the vector.
void Swap< Vector >(Vector &a, Vector &b)
Specialization of the template function Swap<> for class Vector.
double Normlinf() const
Returns the l_infinity norm of the vector.
void Randomize(int seed=0)
Set random values in the vector.
void SyncAliasMemory(const Vector &v) const
Update the alias memory location of the vector to match v.
double * GetData() const
Return a pointer to the beginning of the Vector data.
virtual void UseDevice(bool use_dev) const
Enable execution of Vector operations using the mfem::Device.
int Capacity() const
Return the size of the allocated memory.
Memory< double > & GetMemory()
Return a reference to the Memory object used by the Vector.
double operator*(const double *) const
Dot product with a double * array.
MemoryType GetMemoryType() const
Return a MemoryType that is currently valid. If both the host and the device pointers are currently v...
const double * end() const
STL-like end (const version).
Vector(Vector &base, int base_offset, int size_)
Create a Vector referencing a sub-vector of the Vector base starting at the given offset...
Vector & operator=(const double *v)
Copy Size() entries from v.
bool IsFinite(const double &val)
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
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.
void SetSize(int s, Vector &v)
Resize the vector to size s using the MemoryType of v.
void Swap(Vector &other)
Swap the contents of two Vectors.
virtual double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Vector(const double(&values)[N])
Create a vector using a braced initializer list.
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
double DistanceSquared(const double *x, const double *y, const int n)
void Load(std::istream &in)
Load a vector from an input stream, reading the size from the stream.
double * StealData()
Changes the ownership of the data; after the call the Vector is empty.
bool OwnsHostPtr() const
Return true if the host pointer is owned. Ownership indicates whether the pointer will be deleted by ...
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's DeviceMemoryClass, if on_dev = true...
void SyncMemory(const Vector &v) const
Update the memory location of the vector to match v.
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...
virtual const double * HostRead() const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
Vector & operator/=(double c)
double DistanceTo(const double *p) const
Compute the Euclidean distance to another vector.
void SetSubVectorComplement(const Array< int > &dofs, const double val)
Set all vector entries NOT in the dofs Array to the given val.
Vector & operator*=(double c)
double Min() const
Returns the minimal element of the vector.
Vector & operator+=(double c)
double Norml1() const
Returns the l_1 norm of the vector.
void Swap(Array< T > &, Array< T > &)
double p(const Vector &x, double t)
double Distance(const double *x, const double *y, const int n)
void Print(std::ostream &out=mfem::out, int width=8) const
Prints vector to stream out.
MemoryType
Memory types supported by MFEM.
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.
double DistanceSquaredTo(const double *p) const
Compute the square of the Euclidean distance to another vector.
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.
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
double * begin()
STL-like begin.
double InnerProduct(HypreParVector *x, HypreParVector *y)
virtual double * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Vector & Add(const double a, const Vector &Va)
(*this) += a * Va
T * ReadWrite(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for read+write access to mem with the mfem::Device's DeviceMemoryClass, if on_dev = true, or the mfem::Device's HostMemoryClass, otherwise.
virtual MFEM_DEPRECATED N_Vector ToNVector()
(DEPRECATED) Return a new wrapper SUNDIALS N_Vector of type SUNDIALS_NVEC_SERIAL. ...
double Max() const
Returns the maximal element of the vector.
void New(int size)
Allocate host memory for size entries with the current host memory type returned by MemoryManager::Ge...
FDualNumber< tbase > sqrt(const FDualNumber< tbase > &f)
sqrt([dual number])
void NewMemoryAndSize(const Memory< double > &mem, int s, bool own_mem)
Reset the Vector to use the given external Memory mem and size s.
void Destroy()
Destroy a vector.
Vector & operator-=(double c)
double infinity()
Define a shortcut for std::numeric_limits<double>::infinity()
int CheckFinite() const
Count the number of entries in the Vector for which isfinite is false, i.e. the entry is a NaN or +/-...
double * end()
STL-like end.
friend void add(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 + v2.
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
virtual bool UseDevice() const
Return the device flag of the Memory object used by the Vector.
Vector(int size_, MemoryType mt)
Create a Vector of size size_ using MemoryType mt.
bool UseDevice() const
Read the internal device flag.
void PrintHash(std::ostream &out) const
Print the Vector size and hash of its data.
const Memory< double > & GetMemory() const
Return a reference to the Memory object used by the Vector, const version.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
double Sum() const
Return the sum of the vector entries.
virtual const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
virtual double * HostReadWrite()
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
virtual ~Vector()
Destroys vector.
void Neg()
(*this) = -(*this)
Vector(int size_, MemoryType h_mt, MemoryType d_mt)
Create a Vector of size size_ using host MemoryType h_mt and device MemoryType d_mt.