symArrayHD.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 
21 #ifndef __symArray_hpp__
22 #define __symArray_hpp__
23 
24 #include "KokkosTypes.hpp"
25 
26 
27 #include "types.hpp"
28 #include "typeInfo.hpp"
29 #include "Vector.hpp"
30 
31 
32 /*
33 stores the elemnt of a symetric array in the following order in a 1D vector
34 
35  0 1 2 3
36  4 5 6
37  7 8
38  9
39 
40 */
41 
42 namespace pFlow
43 {
44 
45 
46 template<typename Type>
48 void SWAP(Type &x, Type& y)
49 {
50  auto temp = y;
51  y = x;
52  x = temp;
53 }
54 
55 template<typename T, typename MemorySpace=void>
56 class symArray
57 {
58 
60 
61  using iterator = T*;
62 
63  using constIterator = const T*;
64 
65  using reference = T&;
66 
67  using constReference = const T&;
68 
69  using valueType = T;
70 
71  using pointer = T*;
72 
73  using constPointer = const T*;
74 
75  // type defs related to Kokkos
77 
78  using deviceType = typename ViewType::device_type;
79 
80  using memory_space = typename ViewType::memory_space;
81 
82  using execution_space = typename ViewType::execution_space;
83 
84 
85 protected:
86 
88 
90 
91 
92  constexpr static inline const char* memoerySpaceName()
93  {
94  return memory_space::name();
95  }
96 
97 public:
98 
99  // - type info
100  TypeInfoTemplateNV2("symArray", T, memoerySpaceName());
101 
104  symArray();
105 
108  :
109  symArray("symArray",n)
110  {}
111 
114  :
115  n_(n),
116  view_(name, numElem(n))
117  {}
118 
120  symArray(word name, uint32 n , const T& val)
121  :
122  symArray(name, n)
123  {
124  this->fill(val);
125  }
126 
129  :
130  view_(name)
131  {
132  if( !assign(src))
133  {
135  "error in creating symArray " << name << endl;
136  fatalExit;
137  }
138  }
139 
141  symArray(const symArray&) = default;
142 
144  symArray& operator=(const symArray&) = default;
145 
147  symArray(symArray&&) = delete;
148 
150  symArray& operator=(symArray&&) = delete;
151 
153  ~symArray()=default;
154 
155 
157  void fill(const T& val)
158  {
159  if(n_==0)return;
160  Kokkos::deep_copy(view_, val);
161  }
162 
165  {
166  if(i>j) SWAP(i,j);
167  return view_(j+i*n_-numElem(i));
168  }
169 
171  const T& operator()(uint32 i, uint32 j)const
172  {
173  if(i>j) SWAP(i,j);
174  return view_(j+i*n_-numElem(i));
175  }
176 
177  bool assign(const Vector<T> src)
178  {
179  uint32 nElem = src.size();
180  uint32 n;
181  if( !getN(nElem,n))
182  {
184  "size of input vector do not match a symetric array "<< nElem<<endl;
185  return false;
186  }
187 
188  if(n == 0 )
189  {
190  n_ = 0;
191  return true;
192  }
193 
194  if( n > n_ )
195  Kokkos::realloc(view_, nElem );
196 
197  n_ = n;
198  hostViewType1D<const T> temp(src.data(), nElem );
199  Kokkos::deep_copy(view_, temp);
200 
201  return true;
202  }
203 
204 
206 
207  FUNCTION_H
208  bool read(iIstream& is)
209  {
210  Vector<T> vecFromFile;
211  if( !vecFromFile.read(is) ) return false;
212 
213  this->assign(vecFromFile);
214 
215  return true;
216  }
217 
218  FUNCTION_H
219  bool write(iOstream& os)const
220  {
221 
222  int32 s = numElem(n_);
223  Vector<T, noConstructAllocator<T>> vecToFile(s);
224 
225  const auto dVec = Kokkos::subview(view_, kPair<int32,int32>(0, s));
226  hostViewType1D<T> mirror(vecToFile.data(), vecToFile.size());
227  Kokkos::deep_copy(mirror,dVec);
228 
229  return vecToFile.write(os);
230  }
231 
232 
235  {
236  if(n==0)return 0;
237  return n*(n+1)/2;
238  }
239 
240  static bool getN(uint32 nElem, uint32& n)
241  {
242  real nr = 0.5*(sqrt(8.0*nElem+1)-1);
243  n = static_cast<uint32>(nr);
244  if( equal(nr-static_cast<real>(n) , 0.0) ) return true;
245 
246  return false;
247  }
248 
249 
250 };
251 
252 template<typename T, typename MemorySpace>
254 {
255  if( !iArr.read(is) )
256  {
257  ioErrorInFile (is.name(), is.lineNumber());
258  fatalExit;
259  }
260  return is;
261 }
262 
263 template<typename T, typename MemorySpace>
265 {
266 
267  if( !oArr.write(os) )
268  {
269  ioErrorInFile(os.name(), os.lineNumber());
270  fatalExit;
271  }
272 
273  return os;
274 }
275 
276 }
277 
278 #endif //__symArray_hpp__
pFlow::Vector::read
bool read(iIstream &is)
Definition: Vector.hpp:378
pFlow::symArray::operator()
const INLINE_FUNCTION_HD T & operator()(uint32 i, uint32 j) const
Definition: symArrayHD.hpp:171
pFlow::symArray< nonLinearProperties >::reference
nonLinearProperties & reference
Definition: symArrayHD.hpp:65
pFlow::symArray< nonLinearProperties >::iterator
nonLinearProperties * iterator
Definition: symArrayHD.hpp:61
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray()
pFlow::real
float real
Definition: builtinTypes.hpp:46
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, uint32 n)
Definition: symArrayHD.hpp:113
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::symArray::operator=
INLINE_FUNCTION_H symArray & operator=(const symArray &)=default
pFlow::symArray::read
FUNCTION_H bool read(iIstream &is)
Definition: symArrayHD.hpp:208
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(uint32 n)
Definition: symArrayHD.hpp:107
pFlow::symArray::memoerySpaceName
constexpr static const char * memoerySpaceName()
Definition: symArrayHD.hpp:92
types.hpp
pFlow::symArray< nonLinearProperties >::deviceType
typename ViewType::device_type deviceType
Definition: symArrayHD.hpp:78
pFlow::symArray< nonLinearProperties >::execution_space
typename ViewType::execution_space execution_space
Definition: symArrayHD.hpp:82
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::symArray::n_
uint32 n_
Definition: symArrayHD.hpp:87
KokkosTypes.hpp
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:299
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::symArray::getN
static bool getN(uint32 nElem, uint32 &n)
Definition: symArrayHD.hpp:240
pFlow
Definition: demComponent.hpp:28
pFlow::symArray::operator()
INLINE_FUNCTION_HD T & operator()(uint32 i, uint32 j)
Definition: symArrayHD.hpp:164
FUNCTION_H
#define FUNCTION_H
Definition: pFlowMacros.hpp:58
pFlow::symArray< nonLinearProperties >::pointer
nonLinearProperties * pointer
Definition: symArrayHD.hpp:71
pFlow::SWAP
INLINE_FUNCTION_HD void SWAP(Type &x, Type &y)
Definition: symArrayHD.hpp:48
pFlow::symArray::assign
bool assign(const Vector< T > src)
Definition: symArrayHD.hpp:177
pFlow::symArray::fill
void fill(const T &val)
Definition: symArrayHD.hpp:157
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, Vector< T > src)
Definition: symArrayHD.hpp:128
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, uint32 n, const T &val)
Definition: symArrayHD.hpp:120
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::symArray::view_
ViewType view_
Definition: symArrayHD.hpp:89
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::realloc
INLINE_FUNCTION_H void realloc(ViewType1D< Type, Properties... > &view, int32 len)
Definition: KokkosUtilities.hpp:51
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::symArray< nonLinearProperties >::constReference
const nonLinearProperties & constReference
Definition: symArrayHD.hpp:67
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::symArray< nonLinearProperties >::memory_space
typename ViewType::memory_space memory_space
Definition: symArrayHD.hpp:80
pFlow::symArray::write
FUNCTION_H bool write(iOstream &os) const
Definition: symArrayHD.hpp:219
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:53
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::symArray< nonLinearProperties >::constPointer
const nonLinearProperties * constPointer
Definition: symArrayHD.hpp:73
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::symArray< nonLinearProperties >::constIterator
const nonLinearProperties * constIterator
Definition: symArrayHD.hpp:63
pFlow::symArray::TypeInfoTemplateNV2
TypeInfoTemplateNV2("symArray", T, memoerySpaceName())
pFlow::hostViewType1D
Kokkos::View< T *, Kokkos::HostSpace > hostViewType1D
Definition: KokkosTypes.hpp:104
pFlow::Vector::write
bool write(iOstream &os) const
Definition: Vector.hpp:383
pFlow::symArray< nonLinearProperties >::ViewType
ViewType1D< nonLinearProperties, void > ViewType
Definition: symArrayHD.hpp:76
pFlow::symArray::numElem
static INLINE_FUNCTION_HD uint32 numElem(uint32 n)
Definition: symArrayHD.hpp:234
pFlow::symArray::~symArray
INLINE_FUNCTION_H ~symArray()=default
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
typeInfo.hpp
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
Definition: KokkosTypes.hpp:62
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::sqrt
INLINE_FUNCTION_HD real sqrt(real x)
Definition: math.hpp:148
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:51
pFlow::symArray< nonLinearProperties >::valueType
nonLinearProperties valueType
Definition: symArrayHD.hpp:69
pFlow::Vector
Definition: Vector.hpp:46
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::symArray
Definition: symArrayHD.hpp:56
pFlow::equal
INLINE_FUNCTION_HD bool equal(const real &s1, const real &s2)
Definition: bTypesFunctions.hpp:188
Vector.hpp
pFlow::kPair
Kokkos::pair< T1, T2 > kPair
Definition: KokkosTypes.hpp:52