www.cemf.ir
indexContainer.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 __indexContainer_hpp__
22 #define __indexContainer_hpp__
23 
24 #include <vector>
25 
26 #include "phasicFlowKokkos.hpp"
27 #include "typeInfo.hpp"
28 
29 namespace pFlow
30 {
31 
38 template<typename IndexType>
40 {
41 public:
42 
45  using DeviceViewType = typename DVType::t_dev;
46 
48  using HostViewType = typename DVType::t_host;
49 
51  using HostType = typename HostViewType::device_type;
52 
54  using DeviceType = typename DeviceViewType::device_type;
55 
59  template<typename ViewType>
61  {
62  protected:
63  ViewType view_;
64 
65  public:
66  IndexAccessor(ViewType v):
67  view_(v){}
68 
70  IndexType operator()(uint32 i)const
71  {
72  return view_[i];
73  }
74 
75  uint32 size()const
76  {
77  return view_.extent(0);
78  }
79  };
80 
81 protected:
82 
84  IndexType min_ = 0;
85 
87  IndexType max_ = 0;
88 
90  uint32 size_ = 0;
91 
94 
95 public:
96 
97  TypeInfoTemplateNV11("indexContainer", IndexType);
98 
100 
103 
105  template<typename T>
107  :
109  (
110  static_cast<IndexType>(rng.begin()),
111  static_cast<IndexType>(rng.end())
112  )
113  {}
114 
116  indexContainer(IndexType begin, IndexType end)
117  :
118  min_(begin),
119  max_(end-1),
120  size_(end-begin),
121  views_("indexContainer", size_)
122  {
123  pFlow::fillSequence(views_.h_view, 0, size_, min_);
124  copy(views_.d_view, views_.h_view);
125  }
126 
129  indexContainer(IndexType* data, int32 numElems)
130  :
131  size_(numElems),
132  views_("indexContainer", numElems)
133  {
134  HostViewType hData(data, numElems);
135  copy(views_.h_view, hData);
136  copy(views_.d_view, views_.h_view);
137  min_ = pFlow::min(views_.d_view, 0, numElems);
138  max_ = pFlow::max(views_.d_view, 0, numElems);
139  }
140 
141  indexContainer(std::vector<IndexType> &ind)
142  :
143  indexContainer(ind.data(), ind.size())
144  {}
145 
147  size_(ind.size()),
148  views_("indexContainer", size_)
149  {
150  copy(views_.h_view, ind);
151  copy(views_.d_view, ind);
152  min_ = pFlow::min(views_.d_view, 0, size_);
153  max_ = pFlow::max(views_.d_view, 0, size_);
154  }
155 
157  indexContainer(const indexContainer&) = default;
158 
160  indexContainer& operator = (const indexContainer&) = default;
161 
163  indexContainer(indexContainer&&) = default;
164 
167 
169  ~indexContainer() = default;
170 
172 
175  auto size()const
176  {
177  return size_;
178  }
179 
182  bool empty()const
183  {
184  return size_==0;
185  }
186 
189  IndexType min()const
190  {
191  return min_;
192  }
193 
196  IndexType max()const
197  {
198  return max_;
199  }
200 
202  const HostViewType& hostView()const
203  {
204  return views_.h_view;
205  }
206 
209  {
210  return views_.d_view;
211  }
212 
215  {
216  return views_.h_view;
217  }
218 
221  {
222  return views_.d_view;
223  }
224 
226  auto indicesHost()const
227  {
228  return IndexAccessor<HostViewType>(views_.h_view);
229  }
230 
232  auto indicesDevice()const
233  {
234  return IndexAccessor<DeviceViewType>(views_.d_view);
235  }
236 
239  {
240  views_.modify_host();
241  }
242 
245  {
246  views_.modify_device();
247  }
248 
250  void syncViews()
251  {
252  bool findMinMax = false;
253  if(views_.template need_sync<DeviceType>())
254  {
255  Kokkos::deep_copy(views_.d_view, views_.h_view);
256  views_.clear_sync_state();
257  findMinMax = true;
258  }
259  else if(views_.template need_sync<HostType>())
260  {
261  Kokkos::deep_copy(views_.h_view, views_.d_view);
262  views_.clear_sync_state();
263  findMinMax = true;
264  }
265 
266  if(findMinMax)
267  {
268  min_ = pFlow::min(views_.d_view, 0, size_);
269  max_ = pFlow::max(views_.d_view, 0, size_);
270  }
271  }
272 
273  void syncViews(uint32 newSize)
274  {
275  size_ = newSize;
276  syncViews();
277  }
278 
279 };
280 
281 
282 
285 
288 
289 
290 }
291 
292 
293 #endif
pFlow::indexContainer::max_
IndexType max_
max value in the indices
Definition: indexContainer.hpp:87
pFlow::indexContainer::indicesDevice
auto indicesDevice() const
Return index accessor that works on Device.
Definition: indexContainer.hpp:232
pFlow::indexContainer::min_
IndexType min_
min value in indices
Definition: indexContainer.hpp:84
pFlow::indexContainer::IndexAccessor::size
uint32 size() const
Definition: indexContainer.hpp:75
pFlow::indexContainer::indexContainer
indexContainer(const DeviceViewType &ind)
Definition: indexContainer.hpp:146
pFlow::indexContainer::indexContainer
indexContainer(IndexType *data, int32 numElems)
From data and number of elements in data.
Definition: indexContainer.hpp:129
pFlow::indexContainer::indexContainer
indexContainer(const Range< T > &rng)
Construct from a Range (half open)
Definition: indexContainer.hpp:106
pFlow::indexContainer::max
INLINE_FUNCTION_HD IndexType max() const
Max value of indices.
Definition: indexContainer.hpp:196
pFlow::indexContainer::views_
DVType views_
views to hold indices on Host and Device
Definition: indexContainer.hpp:93
pFlow::copy
INLINE_FUNCTION_H void copy(const ViewType1D< dType, dProperties... > &dst, const ViewType1D< sType, sProperties... > &src)
Definition: ViewAlgorithms.hpp:234
pFlow::indexContainer::~indexContainer
~indexContainer()=default
Destructor.
pFlow::indexContainer::size_
uint32 size_
number/size of index vector
Definition: indexContainer.hpp:90
pFlow::indexContainer::DVType
DualViewType1D< IndexType > DVType
Definition: indexContainer.hpp:43
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::max
T max(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:79
pFlow::indexContainer::syncViews
void syncViews()
synchronize views
Definition: indexContainer.hpp:250
pFlow::indexContainer::modifyOnHost
void modifyOnHost()
Mark host is modified.
Definition: indexContainer.hpp:238
pFlow::indexContainer::DeviceViewType
typename DVType::t_dev DeviceViewType
Device type on device.
Definition: indexContainer.hpp:45
pFlow::indexContainer::indexContainer
indexContainer(IndexType begin, IndexType end)
Construct half open [begin,end)
Definition: indexContainer.hpp:116
pFlow::indexContainer::DeviceType
typename DeviceViewType::device_type DeviceType
Device memory ype.
Definition: indexContainer.hpp:54
pFlow::indexContainer::IndexAccessor::view_
ViewType view_
Definition: indexContainer.hpp:63
pFlow::DualViewType1D
Kokkos::DualView< T *, properties... > DualViewType1D
1D dual view as a vector
Definition: KokkosTypes.hpp:105
pFlow::indexContainer::HostType
typename HostViewType::device_type HostType
Host memory type.
Definition: indexContainer.hpp:51
pFlow
Definition: demGeometry.hpp:27
pFlow::indexContainer::indexContainer
indexContainer(std::vector< IndexType > &ind)
Definition: indexContainer.hpp:141
pFlow::indexContainer::deviceView
DeviceViewType & deviceView()
Return Device veiw.
Definition: indexContainer.hpp:220
pFlow::indexContainer::operator=
indexContainer & operator=(const indexContainer &)=default
Copy assignment.
pFlow::indexContainer::min
INLINE_FUNCTION_HD IndexType min() const
Min value of indices.
Definition: indexContainer.hpp:189
pFlow::indexContainer::syncViews
void syncViews(uint32 newSize)
Definition: indexContainer.hpp:273
pFlow::indexContainer::TypeInfoTemplateNV11
TypeInfoTemplateNV11("indexContainer", IndexType)
phasicFlowKokkos.hpp
pFlow::indexContainer::hostView
HostViewType & hostView()
Return Host veiw.
Definition: indexContainer.hpp:214
pFlow::indexContainer::hostView
const HostViewType & hostView() const
Return Host veiw.
Definition: indexContainer.hpp:202
pFlow::indexContainer::IndexAccessor::IndexAccessor
IndexAccessor(ViewType v)
Definition: indexContainer.hpp:66
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::indexContainer::modifyOnDevice
void modifyOnDevice()
Mark device is modified.
Definition: indexContainer.hpp:244
pFlow::indexContainer::indicesHost
auto indicesHost() const
Return index accessor that works on Host.
Definition: indexContainer.hpp:226
pFlow::indexContainer::empty
INLINE_FUNCTION_HD bool empty() const
If the container empty.
Definition: indexContainer.hpp:182
pFlow::min
T min(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:28
pFlow::Range
Range for elements in an vector [start,end)
Definition: Range.hpp:38
typeInfo.hpp
pFlow::indexContainer::size
INLINE_FUNCTION_HD auto size() const
Size.
Definition: indexContainer.hpp:175
pFlow::indexContainer::IndexAccessor::operator()
INLINE_FUNCTION_HD IndexType operator()(uint32 i) const
Definition: indexContainer.hpp:70
pFlow::indexContainer::indexContainer
indexContainer()
Default.
Definition: indexContainer.hpp:102
pFlow::fillSequence
void fillSequence(internalField< T, MemorySpace > &iField, const T &startVal)
Definition: internalFieldAlgorithms.hpp:198
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:55
pFlow::indexContainer::IndexAccessor
Helper class for accessing index on host or device.
Definition: indexContainer.hpp:60
pFlow::indexContainer::HostViewType
typename DVType::t_host HostViewType
Host type on device.
Definition: indexContainer.hpp:48
pFlow::indexContainer
It holds two vectors of indecis on Host and Device.
Definition: indexContainer.hpp:39
pFlow::indexContainer::deviceView
const DeviceViewType & deviceView() const
Return Device view.
Definition: indexContainer.hpp:208