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 "span.hpp"
25 #include "KokkosTypes.hpp"
26 #include "KokkosUtilities.hpp"
27 #include "ViewAlgorithms.hpp"
28 
29 
30 namespace pFlow
31 {
32 
33 template<typename IndexType>
35 {
36 public:
37 
38  using DualViewType = Kokkos::DualView<IndexType*>;
39 
40  // - viewType of data on device
41  using DeviceViewType = typename DualViewType::t_dev;
42 
43  // - viewType of data on host
44  using HostViewType = typename DualViewType::t_host;
45 
46  using HostType = typename HostViewType::device_type;
47 
48  using DeviceType = typename DeviceViewType::device_type;
49 
50  template<typename ViewType>
52  {
53  protected:
54  ViewType view_;
55 
56  public:
57  IndexAccessor(ViewType v):
58  view_(v){}
59 
61  IndexType operator()(int32 i)const
62  {
63  return view_[i];
64  }
65  };
66 
67 protected:
68 
69  int32 min_ = 0;
70  int32 max_ = 0;
71  size_t size_ = 0;
72 
74 
75 public:
76 
78 
79  // half open [begin,end)
80  indexContainer(IndexType begin, IndexType end)
81  :
82  min_(begin),
83  max_(end-1),
84  size_(end-begin),
85  views_("indexContainer", size_)
86  {
87  pFlow::fillSequence(views_.h_view, 0, size_, min_);
88  copy(views_.d_view, views_.h_view);
89  }
90 
91 
92  indexContainer(IndexType* data, int32 numElems)
93  :
94  size_(numElems),
95  views_("indexContainer", numElems)
96  {
97  HostViewType hData(data, numElems);
98  copy(views_.h_view, hData);
99  copy(views_.d_view, views_.h_view);
100  min_ = pFlow::min(views_.d_view, 0, numElems);
101  max_ = pFlow::max(views_.d_view, 0, numElems);
102  }
103 
104  indexContainer(const indexContainer&) = default;
105 
106  indexContainer& operator = (const indexContainer&) = default;
107 
108  indexContainer(indexContainer&&) = default;
109 
111 
112  ~indexContainer() = default;
113 
115  size_t size()const
116  {
117  return size_;
118  }
119 
121  size_t empty()const
122  {
123  return size_==0;
124  }
125 
127  IndexType min()const
128  {
129  return min_;
130  }
131 
133  IndexType max()const
134  {
135  return max_;
136  }
137 
138  template<typename executionSpace>
141  {
142  if constexpr (isHostAccessible<executionSpace>())
143  {
144  return views_.h_view(i);
145  }else
146  {
147  return views_.d_view(i);
148  }
149  }
150 
151  const HostViewType& hostView()const
152  {
153  return views_.h_view;
154  }
155 
157  {
158  return views_.d_view;
159  }
160 
162  {
163  return views_.h_view;
164  }
165 
167  {
168  return views_.d_view;
169  }
170 
171  auto indicesHost()const
172  {
173  return IndexAccessor<HostViewType>(views_.h_view);
174  }
175 
176  auto indicesDevice()const
177  {
178  return IndexAccessor<DeviceViewType>(views_.d_view);
179  }
180 
182  {
183  views_.modify_host();
184  }
185 
187  {
188  views_.modify_device();
189  }
190 
191  void syncViews()
192  {
193  bool findMinMax = false;
194  if(views_.template need_sync<HostType>())
195  {
196  Kokkos::deep_copy(views_.d_view, views_.h_view);
197  findMinMax = true;
198  }
199  else if(views_.template need_sync<DeviceType>())
200  {
201  Kokkos::deep_copy(views_.h_view, views_.d_view);
202  findMinMax = true;
203  }
204 
205  if(findMinMax)
206  {
207  min_ = pFlow::min(views_.d_view, 0, size_);
208  max_ = pFlow::max(views_.d_view, 0, size_);
209  }
210  }
211 
212  size_t setSize(size_t ns)
213  {
214  auto tmp = size_;
215  size_ = ns;
216  return tmp;
217  }
218 };
219 
220 
223 
224 
225 }
226 
227 
228 #endif
pFlow::indexContainer::indicesDevice
auto indicesDevice() const
Definition: indexContainer.hpp:176
pFlow::fillSequence
void fillSequence(Vector< T, Allocator > &vec, int32 start, int32 end, const T &startVal)
Definition: VectorAlgorithm.hpp:50
pFlow::indexContainer::indexContainer
indexContainer(IndexType *data, int32 numElems)
Definition: indexContainer.hpp:92
KokkosUtilities.hpp
pFlow::indexContainer::size_
size_t size_
Definition: indexContainer.hpp:71
pFlow::indexContainer::max
INLINE_FUNCTION_HD IndexType max() const
Definition: indexContainer.hpp:133
pFlow::indexContainer::empty
INLINE_FUNCTION_HD size_t empty() const
Definition: indexContainer.hpp:121
pFlow::indexContainer::views_
DualViewType views_
Definition: indexContainer.hpp:73
pFlow::indexContainer::min_
int32 min_
Definition: indexContainer.hpp:69
pFlow::copy
INLINE_FUNCTION_H void copy(const ViewType1D< dType, dProperties... > &dst, const ViewType1D< sType, sProperties... > &src)
Definition: ViewAlgorithms.hpp:296
pFlow::indexContainer::~indexContainer
~indexContainer()=default
ViewAlgorithms.hpp
pFlow::indexContainer::max_
int32 max_
Definition: indexContainer.hpp:70
KokkosTypes.hpp
pFlow::indexContainer::syncViews
void syncViews()
Definition: indexContainer.hpp:191
pFlow::indexContainer::modifyOnHost
void modifyOnHost()
Definition: indexContainer.hpp:181
pFlow::indexContainer::size
INLINE_FUNCTION_HD size_t size() const
Definition: indexContainer.hpp:115
pFlow::indexContainer< int32 >::DeviceViewType
typename DualViewType::t_dev DeviceViewType
Definition: indexContainer.hpp:41
pFlow::indexContainer::indexContainer
indexContainer(IndexType begin, IndexType end)
Definition: indexContainer.hpp:80
pFlow::indexContainer::IndexAccessor::operator()
INLINE_FUNCTION_HD IndexType operator()(int32 i) const
Definition: indexContainer.hpp:61
pFlow::indexContainer< int32 >::DeviceType
typename DeviceViewType::device_type DeviceType
Definition: indexContainer.hpp:48
pFlow::indexContainer::IndexAccessor::view_
ViewType view_
Definition: indexContainer.hpp:54
pFlow::indexContainer< int32 >::HostType
typename HostViewType::device_type HostType
Definition: indexContainer.hpp:46
pFlow
Definition: demComponent.hpp:28
pFlow::indexContainer::deviceView
DeviceViewType & deviceView()
Definition: indexContainer.hpp:166
pFlow::indexContainer::operator=
indexContainer & operator=(const indexContainer &)=default
pFlow::indexContainer::min
INLINE_FUNCTION_HD IndexType min() const
Definition: indexContainer.hpp:127
pFlow::indexContainer::hostView
HostViewType & hostView()
Definition: indexContainer.hpp:161
pFlow::indexContainer::operator()
INLINE_FUNCTION_HD IndexType operator()(selectSide< executionSpace >, int32 i) const
Definition: indexContainer.hpp:140
pFlow::indexContainer::hostView
const HostViewType & hostView() const
Definition: indexContainer.hpp:151
pFlow::indexContainer::IndexAccessor::IndexAccessor
IndexAccessor(ViewType v)
Definition: indexContainer.hpp:57
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::indexContainer::setSize
size_t setSize(size_t ns)
Definition: indexContainer.hpp:212
pFlow::indexContainer::modifyOnDevice
void modifyOnDevice()
Definition: indexContainer.hpp:186
pFlow::indexContainer::indicesHost
auto indicesHost() const
Definition: indexContainer.hpp:171
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pFlow::indexContainer< int32 >::HostViewType
typename DualViewType::t_host HostViewType
Definition: indexContainer.hpp:44
pFlow::selectSide
Definition: KokkosTypes.hpp:39
pFlow::indexContainer::indexContainer
indexContainer()
Definition: indexContainer.hpp:77
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:51
pFlow::indexContainer::IndexAccessor
Definition: indexContainer.hpp:51
span.hpp
pFlow::min
T min(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:138
pFlow::indexContainer< int32 >::DualViewType
Kokkos::DualView< int32 * > DualViewType
Definition: indexContainer.hpp:38
pFlow::indexContainer
Definition: indexContainer.hpp:34
pFlow::indexContainer::deviceView
const DeviceViewType & deviceView() const
Definition: indexContainer.hpp:156