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); }
212 MFEM_DEPRECATED
inline operator double *() {
return data; }
215 MFEM_DEPRECATED
inline operator const double *()
const {
return data; }
257 const double &
Elem(
int i)
const;
273 inline const double &
operator[](
int i)
const {
return (*
this)(i); }
339 friend void add(
const double a,
const Vector &x,
399 #ifdef MFEM_USE_ADIOS2 448 virtual const double *
Read(
bool on_dev =
true)
const 456 virtual double *
Write(
bool on_dev =
true)
475 template <
typename T>
478 return (std::fpclassify(val) == FP_SUBNORMAL) ? 0.0 : val;
486 return isfinite(val);
488 return std::isfinite(val);
495 for (
int i = 0; i < n; i++)
504 MFEM_ASSERT(
s>=0,
"Unexpected negative size.");
600 MFEM_ASSERT(
data && i >= 0 && i <
size,
601 "index [" << i <<
"] is out of range [0," <<
size <<
")");
608 MFEM_ASSERT(
data && i >= 0 && i <
size,
609 "index [" << i <<
"] is out of range [0," <<
size <<
")");
635 for (
int i = 0; i < n; i++)
637 d += (x[i]-y[i])*(x[i]-y[i]);
643 inline double Distance(
const double *x,
const double *y,
const int n)
660 MFEM_ASSERT(
p.Size() ==
Size(),
"Incompatible vector sizes.");
671 MFEM_ASSERT(
p.Size() ==
Size(),
"Incompatible vector sizes.");
691 double loc_prod = x * y;
693 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.
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 Print_HYPRE(std::ostream &out) const
Prints vector to stream out in HYPRE_Vector format.
const Memory< double > & GetMemory() const
Return a reference to the Memory object used by the Vector, const version.
void SyncMemory(const Vector &v) const
Update the memory location of the vector to match v.
void SetSize(int s)
Resize the vector to size s.
void Delete()
Delete the owned pointers and reset the Memory object.
virtual const double * HostRead() const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
virtual void UseDevice(bool use_dev) const
Enable execution of Vector operations using the mfem::Device.
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Set v = v1 - v2.
void Print(std::ostream &out=mfem::out, int width=8) const
Prints vector to stream out.
double & operator()(int i)
Access Vector entries using () for 0-based indexing.
void StealData(double **p)
Changes the ownership of the data; after the call the Vector is empty.
int Size() const
Returns the size of the vector.
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).
void Swap< Vector >(Vector &a, Vector &b)
Specialization of the template function Swap<> for class Vector.
virtual const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Extract entries listed in dofs to the output Vector elemvect.
void Randomize(int seed=0)
Set random values in the vector.
Memory< double > & GetMemory()
Return a reference to the Memory object used by the Vector.
Vector(Vector &base, int base_offset, int size_)
Create a Vector referencing a sub-vector of the Vector base starting at the given offset...
void AddSubVector(const Vector &v, int offset)
Vector & operator=(const double *v)
Copy Size() entries from v.
const double * end() const
STL-like end (const version).
bool IsFinite(const double &val)
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
void Wrap(T *ptr, int size, bool own)
Wrap an externally allocated host pointer, ptr with the current host memory type returned by MemoryMa...
double & operator[](int i)
Access Vector entries using [] for 0-based indexing.
void SetSize(int s, Vector &v)
Resize the vector to size s using the MemoryType of v.
double DistanceTo(const double *p) const
Compute the Euclidean distance to another vector.
void Swap(Vector &other)
Swap the contents of two Vectors.
void Sync(const Memory &other) const
Copy the host/device pointer validity flags from other to *this.
int CheckFinite() const
Count the number of entries in the Vector for which isfinite is false, i.e. the entry is a NaN or +/-...
virtual double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
void SyncAlias(const Memory &base, int alias_size) const
Update the alias Memory *this to match the memory location (all valid locations) of its base Memory...
Vector(const double(&values)[N])
Create a vector using a braced initializer list.
double Sum() const
Return the sum of the vector entries.
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.
const double & operator[](int i) const
Read only access to Vector entries using [] for 0-based indexing.
double * StealData()
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.
int Capacity() const
Return the size of the allocated memory.
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...
double Normlp(double p) const
Returns the l_p norm of the vector.
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...
Vector & operator/=(double c)
void SetSubVectorComplement(const Array< int > &dofs, const double val)
Set all vector entries NOT in the dofs Array to the given val.
double * GetData() const
Return a pointer to the beginning of the Vector data.
Vector & operator*=(double c)
Vector & operator+=(double c)
void Swap(Array< T > &, Array< T > &)
double p(const Vector &x, double t)
double Distance(const double *x, const double *y, const int n)
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
MemoryType
Memory types supported by MFEM.
double Min() const
Returns the minimal element of the vector.
void SetHostPtrOwner(bool own) const
Set/clear the ownership flag for the host pointer. Ownership indicates whether the pointer will be de...
void PrintHash(std::ostream &out) const
Print the Vector size and hash of its data.
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
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.
double Max() const
Returns the maximal element of the vector.
double Norml1() const
Returns the l_1 norm of the vector.
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.
void MakeDataOwner() const
Set the Vector data (host pointer) ownership flag.
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.
void SyncAliasMemory(const Vector &v) const
Update the alias memory location of the vector to match v.
void New(int size)
Allocate host memory for size entries with the current host memory type returned by MemoryManager::Ge...
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()
double Norml2() const
Returns the l2 norm of the vector.
bool OwnsHostPtr() const
Return true if the host pointer is owned. Ownership indicates whether the pointer will be deleted by ...
double * end()
STL-like end.
virtual bool UseDevice() const
Return the device flag of the Memory object used by the Vector.
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.
bool UseDevice() const
Read the internal device flag.
Vector(int size_, MemoryType mt)
Create a Vector of size size_ using MemoryType mt.
double Normlinf() const
Returns the l_infinity norm of the vector.
double DistanceSquaredTo(const double *p) const
Compute the square of the Euclidean distance to another vector.
const double * begin() const
STL-like begin (const version).
virtual double * HostReadWrite()
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
virtual ~Vector()
Destroys vector.
MemoryType GetMemoryType() const
Return a MemoryType that is currently valid. If both the host and the device pointers are currently v...
double operator*(const double *) const
Dot product with a double * array.
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.