# PhasicFlow: src/phasicFlow/containers/VectorHD/VectorSingle.hpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

VectorSingle.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#ifndef __VectorSingle_hpp__
#define __VectorSingle_hpp__
#include <vector>
#include "globalSettings.hpp"
#include "phasicFlowKokkos.hpp"
#include "stdVectorHelper.hpp"
#include "error.hpp"
#include "indexContainer.hpp"
#include "streams.hpp"
#ifndef __RESERVE__
#define __RESERVE__
struct RESERVE{};
#endif
namespace pFlow
{
template<typename T, typename MemorySpace=void>
class VectorSingle
{
public:
//- typedefs for accessing data
using VectorType = VectorSingle<T, MemorySpace>;
using VectorTypeHost = VectorSingle<T, HostSpace>;
using iterator = T*;
using const_iterator = const T*;
using reference = T&;
using const_reference = const T&;
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
//- typedefs related to memory management
using viewType = ViewType1D<T, MemorySpace>;
using device_type = typename viewType::device_type;
using memory_space = typename viewType::memory_space;
using execution_space = typename viewType::execution_space;
private:
// - Data members
uint32 size_ = 0;
viewType view_;
// - protected members and methods
static constexpr
bool isHostAccessible_ = isHostAccessible<execution_space>();
static constexpr
bool isDeviceAccessible_ = isDeviceAccessible<execution_space>();
static constexpr
bool isTriviallyCopyable_ = std::is_trivially_copyable_v<T>;
static_assert(isTriviallyCopyable_, "This type is not trivially copyable");
static INLINE_FUNCTION_H uint32 evalCapacity(uint32 n)
{
return static_cast< uint32 >(n*pFlow::gSettings::vectorGrowthFactor__+1);
}
INLINE_FUNCTION_H
uint32 changeSize(uint32 n, bool withInit= false);
INLINE_FUNCTION_H
uint32 changeCapacitySize(uint32 actualCap, uint32 n, bool withInit= false);
INLINE_FUNCTION_H
void changeCapacity(uint32 actualCap, bool withInit= false);
INLINE_FUNCTION_H
uint32 reallocateCapacitySize(uint32 cap, uint32 s);
INLINE_FUNCTION_H
uint32 setSize(uint32 n);
public:
TypeInfoTemplateNV111("VectorSingle", T, memoerySpaceName());
VectorSingle();
explicit VectorSingle(const word& name);
VectorSingle(const word& name, uint32 n);
VectorSingle(const word& name, uint32 n, const T& val);
VectorSingle(const word& name, uint32 cap, uint32 n, RESERVE );
VectorSingle(const word& name, const std::vector<T> & src);
VectorSingle(const word& name, const std::vector<T> & src, uint32 cap);
VectorSingle(const VectorSingle& src);
VectorSingle(const word& name, const VectorSingle& src);
VectorSingle(const word& name, const ViewType1D<T, MemorySpace>& src);
VectorSingle& operator = (const VectorSingle& rhs) ;
VectorSingle(VectorSingle&&) = default;
VectorSingle& operator= (VectorSingle&&) = default;
~VectorSingle();
INLINE_FUNCTION_H
uniquePtr<VectorSingle> clone() const;
INLINE_FUNCTION_H
VectorType& VectorField();
INLINE_FUNCTION_H
const VectorType& VectorField()const;
INLINE_FUNCTION_H
auto& deviceViewAll();
INLINE_FUNCTION_H
const auto& deviceViewAll() const;
INLINE_FUNCTION_H
auto deviceView()const;
INLINE_FUNCTION_H
auto hostViewAll()const;
INLINE_FUNCTION_H
auto hostView()const;
INLINE_FUNCTION_H
word name()const;
INLINE_FUNCTION_H
uint32 size()const;
// Capcity of the vector
INLINE_FUNCTION_H
uint32 capacity()const;
INLINE_FUNCTION_H
bool empty()const;
INLINE_FUNCTION_H
void reserve(uint32 cap);
INLINE_FUNCTION_H
void reallocate(uint32 cap);
INLINE_FUNCTION_H
void reallocate(uint32 cap, uint32 newSize);
INLINE_FUNCTION_H
void resize(uint32 n);
INLINE_FUNCTION_H
void resize(uint32 n, const T& val);
INLINE_FUNCTION_H
void clear();
INLINE_FUNCTION_H
void fill(const T& val);
INLINE_FUNCTION_H
void fill(rangeU32 r, const T& val);
INLINE_FUNCTION_H
void assign(size_t n, const T& val);
INLINE_FUNCTION_H
void assign(const std::vector<T>& src, uint32 cap);
INLINE_FUNCTION_H
void assign(const std::vector<T>& src);
INLINE_FUNCTION_H
void assignFromHost(const VectorTypeHost& src);
INLINE_FUNCTION_H
void assign(const VectorType& src, bool srcCapacity = true);
template<typename MSpace>
INLINE_FUNCTION_H
void assignFromDevice(const VectorSingle<T, MSpace>& src, bool srcCapacity = true);
INLINE_FUNCTION_H
void append(const ViewType1D<T,MemorySpace>& appVec);
INLINE_FUNCTION_H
void append(const std::vector<T>& appVec);
INLINE_FUNCTION_H
void append(const VectorType& appVec);
INLINE_FUNCTION_H
auto getSpan();
INLINE_FUNCTION_H
auto getSpan()const;
INLINE_FUNCTION_H
bool insertSetElement(const uint32IndexContainer& indices, const T& val);
INLINE_FUNCTION_H
bool insertSetElement(const uint32IndexContainer& indices, const std::vector<T>& vals);
INLINE_FUNCTION_H
bool insertSetElement(
const uint32IndexContainer& indices,
const ViewType1D<T, memory_space> vals);
INLINE_FUNCTION_H
bool reorderItems(const uint32IndexContainer& indices);
template<bool Enable = true>
INLINE_FUNCTION_H
typename std::enable_if_t<isHostAccessible_ && Enable, void>
push_back(const T& val)
{
auto n = changeSize(size_+1);
data()[n] = val;
}
INLINE_FUNCTION_H
pointer data(){
return view_.data();
}
INLINE_FUNCTION_H
const_pointer data()const{
return view_.data();
}
template<bool Enable = true>
INLINE_FUNCTION_H
typename std::enable_if_t<isHostAccessible_ && Enable,iterator>
begin(){
return data();
}
template<bool Enable = true>
INLINE_FUNCTION_H
typename std::enable_if_t<isHostAccessible_ && Enable,const_iterator>
begin()const {
return data();
}
template<bool Enable = true>
INLINE_FUNCTION_H
typename std::enable_if_t<isHostAccessible_ && Enable,iterator>
end(){
return size_ > 0 ? data() + size_: data();
}
template<bool Enable = true>
INLINE_FUNCTION_H
typename std::enable_if_t<isHostAccessible_ && Enable,const_iterator>
end()const{
return size_ > 0 ? data() + size_: data();
}
template<bool Enable = true>
INLINE_FUNCTION_H
typename std::enable_if_t<isHostAccessible_ && Enable,reference>
operator[](size_t i){
return view_[i];
}
template<bool Enable = true>
INLINE_FUNCTION_H
typename std::enable_if_t<isHostAccessible_ && Enable,const_reference>
operator[](size_t i)const{
return view_[i];
}
FUNCTION_H
bool read(iIstream& is)
{
std::vector<T> vecFromFile;
if(! readStdVector(is, vecFromFile)) return false;
this->assign(vecFromFile);
return true;
}
FUNCTION_H
bool read(iIstream& is, const IOPattern& iop)
{
std::vector<T> vecFromFile;
if(! readStdVector(is, vecFromFile, iop)) return false;
this->assign(vecFromFile);
return true;
}
FUNCTION_H
bool write(iOstream& os, const IOPattern& iop)const
{
auto hVec = hostView();
auto sp = span<T>( const_cast<T*>(hVec.data()), hVec.size());
return writeSpan(os, sp, iop);
}
FUNCTION_H
bool write(iOstream& os)const
{
auto hVec = hostView();
auto sp = span<T>( const_cast<T*>(hVec.data()), hVec.size());
return writeSpan(os, sp);
}
template<typename HostMask>
FUNCTION_H
bool write(iOstream& os, const IOPattern& iop, const HostMask& mask)const
{
auto hVec = hostView();
auto numActive = mask.numActive();
std::vector<T> finalField;
finalField.clear();
finalField.reserve(numActive);
uint32 start = mask.activeRange().start();
uint32 end = mask.activeRange().end();
for(uint32 i=start; i<end; i++ )
{
if( mask() )
finalField.push_back(hVec[i]);
}
auto sp = span<T>( finalField.data(), finalField.size());
return writeSpan(os, sp, iop);
}
static
constexpr const char* memoerySpaceName()
{
return memory_space::name();
}
}; // class VectorSingle
template<typename T, typename MemorySpace>
inline iIstream& operator >> (iIstream & is, VectorSingle<T, MemorySpace> & ivec )
{
if( !ivec.read(is) )
{
ioErrorInFile (is.name(), is.lineNumber());
fatalExit;
}
return is;
}
template<typename T, typename MemorySpace>
inline iOstream& operator << (iOstream& os, const VectorSingle<T, MemorySpace>& ovec )
{
if( !ovec.write(os) )
{
ioErrorInFile(os.name(), os.lineNumber());
fatalExit;
}
return os;
}
} // - pFlow
#include "VectorSingleAlgorithms.hpp"
#include "VectorSingle.cpp"
#endif //__VectorSingle_hpp__
/*
INLINE_FUNCTION_H
bool append(const deviceViewType1D<T>& dVec, size_t numElems)
{
if(numElems == 0 )return true;
auto oldSize = size_;
auto newSize = size_ + numElems;
if(this->empty() || newSize > capacity() )
{
resize(newSize);
}
else
{
size_ = size_+numElems;
}
auto dSubView = Kokkos::subview(view_, Kokkos::make_pair(oldSize, newSize));
copy(dSubView, dVec);
return true;
}
INLINE_FUNCTION_H
bool append(const VectorSingle& Vec)
{
return append(Vec.deviceView(), Vec.size());
}*/
pFlow::VectorSingle::operator[]
INLINE_FUNCTION_H std::enable_if_t< isHostAccessible_ &&Enable, reference > operator[](size_t i)
Return reference to element i. it works when host is accessible.
Definition: VectorSingle.hpp:382
pFlow::span
Definition: span.hpp:31
pFlow::VectorSingle::fill
INLINE_FUNCTION_H void fill(const T &val)
Fill the range [0,size) with val.
Definition: VectorSingle.cpp:365
pFlow::VectorSingle::reserve
INLINE_FUNCTION_H void reserve(uint32 cap)
Reserve capacity for vector Preserve the content.
Definition: VectorSingle.cpp:318
pFlow::VectorSingle< uint32, HostSpace >::const_pointer
const uint32 * const_pointer
Definition: VectorSingle.hpp:66
pFlow::VectorSingle::begin
INLINE_FUNCTION_H std::enable_if_t< isHostAccessible_ &&Enable, const_iterator > begin() const
Return begin iterator. it works when host is accessible.
Definition: VectorSingle.hpp:356
pFlow::VectorSingle::resize
INLINE_FUNCTION_H void resize(uint32 n)
Resize the vector and preserve the content.
Definition: VectorSingle.cpp:344
pFlow::VectorSingle< uint32, HostSpace >::iterator
uint32 * iterator
Definition: VectorSingle.hpp:54
pFlow::VectorSingle::append
INLINE_FUNCTION_H void append(const ViewType1D< T, MemorySpace > &appVec)
Definition: VectorSingle.cpp:506
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::readStdVector
bool readStdVector(iIstream &is, std::vector< T, Allocator > &vec)
Definition: stdVectorHelper.hpp:126
pFlow::VectorSingle::VectorSingle
VectorSingle()
Empty vector.
Definition: VectorSingle.cpp:95
pFlow::VectorSingle::assignFromDevice
INLINE_FUNCTION_H void assignFromDevice(const VectorSingle< T, MSpace > &src, bool srcCapacity=true)
Definition: VectorSingle.cpp:487
pFlow::VectorSingle::begin
INLINE_FUNCTION_H std::enable_if_t< isHostAccessible_ &&Enable, iterator > begin()
Return begin iterator. It works when devices is host accessible.
Definition: VectorSingle.hpp:348
pFlow::VectorSingle::reallocate
INLINE_FUNCTION_H void reallocate(uint32 cap)
Reallocate memory to new cap and set size to 0.
Definition: VectorSingle.cpp:326
pFlow::VectorSingle::push_back
INLINE_FUNCTION_H std::enable_if_t< isHostAccessible_ &&Enable, void > push_back(const T &val)
push a new element at the end (host call only) resize if necessary and works on host accessible vecto...
Definition: VectorSingle.hpp:328
pFlow::VectorSingle::clear
INLINE_FUNCTION_H void clear()
Clear the vector, but keep the allocated memory unchanged.
Definition: VectorSingle.cpp:358
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::VectorSingle::read
FUNCTION_H bool read(iIstream &is)
Read vector from stream.
Definition: VectorSingle.hpp:398
pFlow::VectorSingle::setSize
INLINE_FUNCTION_H uint32 setSize(uint32 n)
Definition: VectorSingle.cpp:84
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::VectorSingle::write
FUNCTION_H bool write(iOstream &os, const IOPattern &iop, const HostMask &mask) const
Definition: VectorSingle.hpp:442
pFlow::VectorSingle::clone
INLINE_FUNCTION_H uniquePtr< VectorSingle > clone() const
Clone as a uniquePtr (perform deep copy)
Definition: VectorSingle.cpp:225
pFlow::VectorSingle::changeCapacitySize
INLINE_FUNCTION_H uint32 changeCapacitySize(uint32 actualCap, uint32 n, bool withInit=false)
Change the size and capacity of Vector.
Definition: VectorSingle.cpp:40
pFlow::VectorSingle::empty
INLINE_FUNCTION_H bool empty() const
If vector is empty.
Definition: VectorSingle.cpp:311
pFlow::VectorSingle< uint32, HostSpace >::memory_space
typename viewType::memory_space memory_space
Definition: VectorSingle.hpp:74
pFlow::VectorSingle::read
FUNCTION_H bool read(iIstream &is, const IOPattern &iop)
Read vector from stream.
Definition: VectorSingle.hpp:410
pFlow::VectorSingle< uint32, HostSpace >::pointer
uint32 * pointer
Definition: VectorSingle.hpp:64
pFlow
Definition: demGeometry.hpp:27
pFlow::VectorSingle< uint32, HostSpace >::execution_space
typename viewType::execution_space execution_space
Definition: VectorSingle.hpp:76
pFlow::VectorSingle::reorderItems
INLINE_FUNCTION_H bool reorderItems(const uint32IndexContainer &indices)
Definition: VectorSingle.cpp:738
pFlow::VectorSingle::deviceView
INLINE_FUNCTION_H auto deviceView() const
Device view range [0, size)
Definition: VectorSingle.cpp:263
FUNCTION_H
#define FUNCTION_H
Definition: pFlowMacros.hpp:62
pFlow::VectorSingle::name
INLINE_FUNCTION_H word name() const
Name of the vector.
Definition: VectorSingle.cpp:290
pFlow::VectorSingle::changeCapacity
INLINE_FUNCTION_H void changeCapacity(uint32 actualCap, bool withInit=false)
Definition: VectorSingle.cpp:53
RESERVE
Definition: Vector.hpp:40
pFlow::VectorSingle::size_
uint32 size_
Size of the vector.
Definition: VectorSingle.hpp:83
phasicFlowKokkos.hpp
pFlow::VectorSingle< uint32, HostSpace >::reference
uint32 & reference
Definition: VectorSingle.hpp:58
globalSettings.hpp
pFlow::VectorSingle< uint32, HostSpace >::device_type
typename viewType::device_type device_type
Definition: VectorSingle.hpp:72
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::VectorSingle::data
INLINE_FUNCTION_H pointer data()
Definition: VectorSingle.hpp:335
pFlow::VectorSingle::assignFromHost
INLINE_FUNCTION_H void assignFromHost(const VectorTypeHost &src)
Assign source vector from host side.
Definition: VectorSingle.cpp:442
pFlow::VectorSingle::~VectorSingle
~VectorSingle()
Descructor This may not destroy the underlying memory, sice view is shared_ptr and maybe referenced b...
Definition: VectorSingle.cpp:216
pFlow::VectorSingle::evalCapacity
static INLINE_FUNCTION_H uint32 evalCapacity(uint32 n)
Evaluate capacity based on the input size.
Definition: VectorSingle.hpp:104
pFlow::VectorSingle::getSpan
INLINE_FUNCTION_H auto getSpan()
Definition: VectorSingle.cpp:569
pFlow::VectorSingle::changeSize
INLINE_FUNCTION_H uint32 changeSize(uint32 n, bool withInit=false)
Change size to n and preserve the conetent if realloc occurs
Definition: VectorSingle.cpp:24
pFlow::VectorSingle::hostView
INLINE_FUNCTION_H auto hostView() const
Return a view accessible on Host in range [0,size)
Definition: VectorSingle.cpp:281
VectorSingle.cpp
stdVectorHelper.hpp
pFlow::VectorSingle< uint32, HostSpace >::const_iterator
const uint32 * const_iterator
Definition: VectorSingle.hpp:56
pFlow::VectorSingle::TypeInfoTemplateNV111
TypeInfoTemplateNV111("VectorSingle", T, memoerySpaceName())
Type info.
pFlow::VectorSingle::end
INLINE_FUNCTION_H std::enable_if_t< isHostAccessible_ &&Enable, iterator > end()
Return end iterator. it works when host is accessible.
Definition: VectorSingle.hpp:365
pFlow::VectorSingle::VectorTypeHost
VectorSingle< T, HostSpace > VectorTypeHost
Definition: VectorSingle.hpp:52
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOPattern
Definition: IOPattern.hpp:32
pFlow::gSettings::vectorGrowthFactor__
const double vectorGrowthFactor__
Definition: globalSettings.cpp:9
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:57
pFlow::writeSpan
bool writeSpan(iOstream &os, span< T > sp)
Definition: stdVectorHelper.hpp:70
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::VectorSingle
Definition: VectorSingle.hpp:44
pFlow::VectorSingle::operator[]
INLINE_FUNCTION_H std::enable_if_t< isHostAccessible_ &&Enable, const_reference > operator[](size_t i) const
Return reference to element i. it works when host is accessible.
Definition: VectorSingle.hpp:390
pFlow::Range< uint32 >
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::VectorSingle::isDeviceAccessible_
static constexpr bool isDeviceAccessible_
Is the memory of this vector accessiple from Divce.
Definition: VectorSingle.hpp:96
streams.hpp
pFlow::VectorSingle::data
INLINE_FUNCTION_H const_pointer data() const
Definition: VectorSingle.hpp:340
pFlow::VectorSingle::deviceViewAll
INLINE_FUNCTION_H auto & deviceViewAll()
Device view range [0,capcity)
Definition: VectorSingle.cpp:249
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
1D veiw as a vector
Definition: KokkosTypes.hpp:93
pFlow::VectorSingle::end
INLINE_FUNCTION_H std::enable_if_t< isHostAccessible_ &&Enable, const_iterator > end() const
Return end iterator. it works when host is accessible.
Definition: VectorSingle.hpp:374
pFlow::VectorSingle::hostViewAll
INLINE_FUNCTION_H auto hostViewAll() const
Return a view accessible on Host in range [0,capacity)
Definition: VectorSingle.cpp:272
n
uint32 n
Definition: NBSLoop.hpp:24
pFlow::VectorSingle::memoerySpaceName
static constexpr const char * memoerySpaceName()
Name of the memory space.
Definition: VectorSingle.hpp:469
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::VectorSingle::assign
INLINE_FUNCTION_H void assign(size_t n, const T &val)
Change size of the vector and assign val to vector and.
Definition: VectorSingle.cpp:386
pFlow::VectorSingle::write
FUNCTION_H bool write(iOstream &os, const IOPattern &iop) const
Write the vector to os.
Definition: VectorSingle.hpp:421
pFlow::VectorSingle::write
FUNCTION_H bool write(iOstream &os) const
Definition: VectorSingle.hpp:431
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::VectorSingle::capacity
INLINE_FUNCTION_H uint32 capacity() const
Definition: VectorSingle.cpp:304
pFlow::VectorSingle::VectorField
INLINE_FUNCTION_H VectorType & VectorField()
Return *this.
Definition: VectorSingle.cpp:234
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
pFlow::VectorSingle::VectorType
VectorSingle< T, MemorySpace > VectorType
Definition: VectorSingle.hpp:50
VectorSingleAlgorithms.hpp
pFlow::VectorSingle::isHostAccessible_
static constexpr bool isHostAccessible_
Is the memory of this vector accessible from Host.
Definition: VectorSingle.hpp:92
pFlow::VectorSingle::reallocateCapacitySize
INLINE_FUNCTION_H uint32 reallocateCapacitySize(uint32 cap, uint32 s)
Definition: VectorSingle.cpp:73
pFlow::VectorSingle::operator=
VectorSingle & operator=(const VectorSingle &rhs)
Copy assignment (perform deep copy from rhs to *this)
Definition: VectorSingle.cpp:204
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::VectorSingle::isTriviallyCopyable_
static constexpr bool isTriviallyCopyable_
Definition: VectorSingle.hpp:99
pFlow::VectorSingle< uint32, HostSpace >::const_reference
const uint32 & const_reference
Definition: VectorSingle.hpp:60
indexContainer.hpp
pFlow::VectorSingle::insertSetElement
INLINE_FUNCTION_H bool insertSetElement(const uint32IndexContainer &indices, const T &val)
Definition: VectorSingle.cpp:583
pFlow::indexContainer
It holds two vectors of indecis on Host and Device.
Definition: indexContainer.hpp:39
pFlow::VectorSingle::size
INLINE_FUNCTION_H uint32 size() const
Size of the vector.
Definition: VectorSingle.cpp:297
pFlow::VectorSingle< uint32, HostSpace >::value_type
uint32 value_type
Definition: VectorSingle.hpp:62
error.hpp
pFlow::VectorSingle< uint32, HostSpace >::viewType
ViewType1D< uint32, HostSpace > viewType
Definition: VectorSingle.hpp:70
pFlow::VectorSingle::view_
viewType view_
view of the vector
Definition: VectorSingle.hpp:86