www.cemf.ir
VectorSingle.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 #ifndef __VectorSingle_hpp__
21 #define __VectorSingle_hpp__
22 
23 #include <vector>
24 
25 #include "globalSettings.hpp"
26 #include "phasicFlowKokkos.hpp"
27 #include "stdVectorHelper.hpp"
28 #include "error.hpp"
29 #include "indexContainer.hpp"
30 #include "streams.hpp"
31 
32 
33 
34 #ifndef __RESERVE__
35 #define __RESERVE__
36  struct RESERVE{};
37 #endif
38 
39 namespace pFlow
40 {
41 
42 
43 template<typename T, typename MemorySpace=void>
45 {
46 public:
47 
48  //- typedefs for accessing data
49 
51 
53 
54  using iterator = T*;
55 
56  using const_iterator = const T*;
57 
58  using reference = T&;
59 
60  using const_reference = const T&;
61 
62  using value_type = T;
63 
64  using pointer = T*;
65 
66  using const_pointer = const T*;
67 
68  //- typedefs related to memory management
69 
71 
72  using device_type = typename viewType::device_type;
73 
74  using memory_space = typename viewType::memory_space;
75 
76  using execution_space = typename viewType::execution_space;
77 
78 private:
79 
80  // - Data members
81 
84 
87 
88  // - protected members and methods
89 
91  static constexpr
92  bool isHostAccessible_ = isHostAccessible<execution_space>();
93 
95  static constexpr
96  bool isDeviceAccessible_ = isDeviceAccessible<execution_space>();
97 
98  static constexpr
99  bool isTriviallyCopyable_ = std::is_trivially_copyable_v<T>;
100 
101  static_assert(isTriviallyCopyable_, "This type is not trivially copyable");
102 
105  {
106  return static_cast<uint32>(n*pFlow::gSettings::vectorGrowthFactor__+1);
107  }
108 
112  uint32 changeSize(uint32 n, bool withInit= false);
113 
116  uint32 changeCapacitySize(uint32 actualCap, uint32 n, bool withInit= false);
117 
119  void changeCapacity(uint32 actualCap, bool withInit= false);
120 
123 
126 
127 public:
128 
130  TypeInfoTemplateNV111("VectorSingle", T, memoerySpaceName());
131 
133 
135  VectorSingle();
136 
138  explicit VectorSingle(const word& name);
139 
141  VectorSingle(const word& name, uint32 n);
142 
144  VectorSingle(const word& name, uint32 n, const T& val);
145 
147  VectorSingle(const word& name, uint32 cap, uint32 n, RESERVE );
148 
150  VectorSingle(const word& name, const std::vector<T> & src);
151 
153  VectorSingle(const word& name, const std::vector<T> & src, uint32 cap);
154 
156  VectorSingle(const VectorSingle& src);
157 
159  VectorSingle(const word& name, const VectorSingle& src);
160 
162  VectorSingle(const word& name, const ViewType1D<T, MemorySpace>& src);
163 
165  VectorSingle& operator = (const VectorSingle& rhs) ;
166 
168  VectorSingle(VectorSingle&&) = default;
169 
171  VectorSingle& operator= (VectorSingle&&) = default;
172 
176  ~VectorSingle();
177 
181 
182 
184 
188 
191  const VectorType& VectorField()const;
192 
195  auto& deviceViewAll();
196 
199  const auto& deviceViewAll() const;
200 
203  auto deviceView()const;
204 
207  auto hostViewAll()const;
208 
209 
212  auto hostView()const;
213 
216  word name()const;
217 
218 
221  uint32 size()const;
222 
223 
224  // Capcity of the vector
226  uint32 capacity()const;
227 
230  bool empty()const;
231 
235  void reserve(uint32 cap);
236 
239  void reallocate(uint32 cap);
240 
244  void reallocate(uint32 cap, uint32 newSize);
245 
248  void resize(uint32 n);
249 
252  void resize(uint32 n, const T& val);
253 
256  void clear();
257 
260  void fill(const T& val);
261 
263  void fill(rangeU32 r, const T& val);
264 
267  void assign(size_t n, const T& val);
268 
272  void assign(const std::vector<T>& src, uint32 cap);
273 
274 
279  void assign(const std::vector<T>& src);
280 
285  void assignFromHost(const VectorTypeHost& src);
286 
288  void assign(const VectorType& src, bool srcCapacity = true);
289 
290  template<typename MSpace>
292  void assignFromDevice(const VectorSingle<T, MSpace>& src, bool srcCapacity = true);
293 
295  void append(const ViewType1D<T,MemorySpace>& appVec);
296 
298  void append(const std::vector<T>& appVec);
299 
301  void append(const VectorType& appVec);
302 
304  auto getSpan();
305 
307  auto getSpan()const;
308 
310  bool insertSetElement(const uint32IndexContainer& indices, const T& val);
311 
313  bool insertSetElement(const uint32IndexContainer& indices, const std::vector<T>& vals);
314 
316  bool insertSetElement(
317  const uint32IndexContainer& indices,
318  const ViewType1D<T, memory_space> vals);
319 
321  bool reorderItems(const uint32IndexContainer& indices);
322 
325  template<bool Enable = true>
327  typename std::enable_if_t<isHostAccessible_ && Enable, void>
328  push_back(const T& val)
329  {
330  auto n = changeSize(size_+1);
331  data()[n] = val;
332  }
333 
336  return view_.data();
337  }
338 
341  return view_.data();
342  }
343 
345  template<bool Enable = true>
347  typename std::enable_if_t<isHostAccessible_ && Enable,iterator>
348  begin(){
349  return data();
350  }
351 
353  template<bool Enable = true>
355  typename std::enable_if_t<isHostAccessible_ && Enable,const_iterator>
356  begin()const {
357  return data();
358  }
359 
360 
362  template<bool Enable = true>
364  typename std::enable_if_t<isHostAccessible_ && Enable,iterator>
365  end(){
366  return size_ > 0 ? data() + size_: data();
367  }
368 
369 
371  template<bool Enable = true>
373  typename std::enable_if_t<isHostAccessible_ && Enable,const_iterator>
374  end()const{
375  return size_ > 0 ? data() + size_: data();
376  }
377 
379  template<bool Enable = true>
381  typename std::enable_if_t<isHostAccessible_ && Enable,reference>
382  operator[](size_t i){
383  return view_[i];
384  }
385 
387  template<bool Enable = true>
389  typename std::enable_if_t<isHostAccessible_ && Enable,const_reference>
390  operator[](size_t i)const{
391  return view_[i];
392  }
393 
395 
397  FUNCTION_H
398  bool read(iIstream& is)
399  {
400  std::vector<T> vecFromFile;
401  if(! readStdVector(is, vecFromFile)) return false;
402 
403  this->assign(vecFromFile);
404 
405  return true;
406  }
407 
409  FUNCTION_H
410  bool read(iIstream& is, const IOPattern& iop)
411  {
412  std::vector<T> vecFromFile;
413  if(! readStdVector(is, vecFromFile, iop)) return false;
414  this->assign(vecFromFile);
415 
416  return true;
417  }
418 
420  FUNCTION_H
421  bool write(iOstream& os, const IOPattern& iop)const
422  {
423  auto hVec = hostView();
424  auto sp = span<T>( const_cast<T*>(hVec.data()), hVec.size());
425 
426  return writeSpan(os, sp, iop);
427 
428  }
429 
430  FUNCTION_H
431  bool write(iOstream& os)const
432  {
433  auto hVec = hostView();
434  auto sp = span<T>( const_cast<T*>(hVec.data()), hVec.size());
435 
436  return writeSpan(os, sp);
437 
438  }
439 
440  template<typename HostMask>
441  FUNCTION_H
442  bool write(iOstream& os, const IOPattern& iop, const HostMask& mask)const
443  {
444  auto hVec = hostView();
445 
446  auto numActive = mask.numActive();
447  std::vector<T> finalField;
448  finalField.clear();
449  finalField.reserve(numActive);
450 
451  uint32 start = mask.activeRange().start();
452  uint32 end = mask.activeRange().end();
453 
454  for(uint32 i=start; i<end; i++ )
455  {
456  if( mask() )
457  finalField.push_back(hVec[i]);
458  }
459 
460  auto sp = span<T>( finalField.data(), finalField.size());
461 
462  return writeSpan(os, sp, iop);
463 
464  }
465 
466 
468  static
469  constexpr const char* memoerySpaceName()
470  {
471  return memory_space::name();
472  }
473 
474 }; // class VectorSingle
475 
476 template<typename T, typename MemorySpace>
478 {
479  if( !ivec.read(is) )
480  {
481  ioErrorInFile (is.name(), is.lineNumber());
482  fatalExit;
483  }
484  return is;
485 }
486 
487 template<typename T, typename MemorySpace>
489 {
490 
491  if( !ovec.write(os) )
492  {
493  ioErrorInFile(os.name(), os.lineNumber());
494  fatalExit;
495  }
496 
497  return os;
498 }
499 
500 
501 } // - pFlow
502 
504 #include "VectorSingle.cpp"
505 
506 
507 
508 
509 #endif //__VectorSingle_hpp__
510 
511 
512 /*
513 INLINE_FUNCTION_H
514  bool append(const deviceViewType1D<T>& dVec, size_t numElems)
515  {
516 
517  if(numElems == 0 )return true;
518  auto oldSize = size_;
519  auto newSize = size_ + numElems;
520 
521  if(this->empty() || newSize > capacity() )
522  {
523  resize(newSize);
524  }
525  else
526  {
527  size_ = size_+numElems;
528  }
529 
530  auto dSubView = Kokkos::subview(view_, Kokkos::make_pair(oldSize, newSize));
531  copy(dSubView, dVec);
532 
533  return true;
534  }
535 
536  INLINE_FUNCTION_H
537  bool append(const VectorSingle& Vec)
538  {
539  return append(Vec.deviceView(), Vec.size());
540  }*/
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