sortedPairs.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 
22 #ifndef __sortedPairs_hpp__
23 #define __sortedPairs_hpp__
24 
25 #include "unsortedPairs.hpp"
26 #include "KokkosUtilities.hpp"
27 
28 namespace pFlow
29 {
30 
31 
32 template<typename executionSpace, typename idType>
34 :
35  public unsortedPairs<executionSpace, idType>
36 {
37 public:
38 
40 
41  using IdType = typename UnsortedPairs::IdType;
42 
44 
45  using memory_space = typename ExecutionSpace::memory_space;
46 
48 
50 
51  struct pairAccessor
52  {
53  using PairType = typename sortedPairs::PairType;
54 
56 
58 
60  int32 size()const { return size_; }
61 
63  int32 loopCount()const { return size_; }
64 
66  bool isValid(int32 i)const { return i<size_; }
67 
69  PairType getPair(int i)const { return sortedParis_[i]; }
70 
72  bool getPair(int32 i, PairType& pair)const {
73  if(i<size_) {
74  pair = sortedParis_[i];
75  return true;
76  }
77  return false;
78  }
79  };
80 
81  class TagFillFlag{};
82 
83  class TagFillPairs{};
84 
85 protected:
86 
88  int32 size_ = 0;
89 
91 
93 
94  using rpFillFlag = Kokkos::RangePolicy<
96  Kokkos::Schedule<Kokkos::Static>,
97  Kokkos::IndexType<int32>,
99 
100  using rpFillPairs = Kokkos::RangePolicy<
102  Kokkos::Schedule<Kokkos::Static>,
103  Kokkos::IndexType<int32>,
105 
106 public:
107 
108  // - type info
109  TypeInfoNV("sortedPairs");
110 
111 
112  // constructors
113  sortedPairs(int32 initialSize =1)
114  :
115  UnsortedPairs(initialSize),
116  flags_("flags_",UnsortedPairs::capacity()+1),
117  sortedPairs_("sortedPairs_",UnsortedPairs::capacity())
118  {}
119 
120 
122  {
123  this->clear();
124  return true;
125  }
126 
128  {
129  prepareSorted();
130  return true;
131  }
132 
133  // - Device call
134  // return the pair at index idx
135  // perform no check for size and existance
138  {
139  return sortedPairs_[idx];
140  }
141 
142  // - Device/host call
143  // return the pair at index idx
145  bool getPair(int32 idx, PairType& p)const
146  {
147  if(isValid(idx))
148  {
149  p = getPair(idx);
150  return true;
151  }
152  else
153  {
154  return false;
155  }
156  }
157 
159  bool isValid(int32 idx)const
160  {
161  return idx < size_;
162  }
163 
164 
165  //use this when the value of size_ is updated
167  int32 size()const
168  {
169  return size_;
170  }
171 
173  {
174  return size_;
175  }
176 
178  {
179  return {size_, sortedPairs_};
180  }
181 
183  void clear()
184  {
186  size_ = 0;
187  }
188 
190  {
191  // first check the size of flags_
193 
194  if( capacity+1 > flags_.size() )
195  {
197  }
198 
199  // fill the flags
200  Kokkos::parallel_for(
201  "contactPairsSorted::fillFlag",
202  rpFillFlag(0,capacity+1),
203  *this);
204  Kokkos::fence();
205 
206 
207  // inclusive scan on flags_
209 
210  Kokkos::fence(); // Kokkos's scan is blocking I guess. So, this could be removed.
211 
212  // get the last value of flags_ to obtain the size of sortedPairs_
214 
215  if(size_ == 0 )return;
216 
217  // resize sortedPairs_ if necessary;
218  if( size_>sortedPairs_.size() )
219  {
220  // get more space to prevent reallocations in next iterations
221  int32 len = size_*1.1+1;
223  }
224 
225  Kokkos::parallel_for(
226  "contactPairsSorted::fillPairs",
227  rpFillPairs(0,this->capacity()),
228  *this);
229  Kokkos::fence();
230 
231  // - sort paris based on the first and second
232  sort(sortedPairs_, 0, size_ );
233 
234 
235  }
236 
239  {
240  if(this->container_.valid_at(i) )
241  flags_[i] = 1;
242  else
243  flags_[i] = 0;
244  }
245 
248  {
249  auto fi = flags_[i];
250  if(fi!=flags_[i+1])
251  sortedPairs_[fi] = this->container_.key_at(i);
252  }
253 
254 };
255 
256 }
257 
258 #endif //__sortedPairs_hpp__
pFlow::sortedPairs::IdType
typename UnsortedPairs::IdType IdType
Definition: sortedPairs.hpp:41
pFlow::sortedPairs::loopCount
int32 loopCount() const
Definition: sortedPairs.hpp:172
pFlow::sortedPairs::sortedPairs_
ViewType1D< PairType, ExecutionSpace > sortedPairs_
Definition: sortedPairs.hpp:92
pFlow::sortedPairs::operator()
INLINE_FUNCTION_HD void operator()(TagFillPairs, int32 i) const
Definition: sortedPairs.hpp:247
pFlow::sortedPairs::pairAccessor::size
INLINE_FUNCTION_HD int32 size() const
Definition: sortedPairs.hpp:60
pFlow::sortedPairs::TagFillFlag
Definition: sortedPairs.hpp:81
KokkosUtilities.hpp
pFlow::sortedPairs::beforeBroadSearch
bool beforeBroadSearch()
Definition: sortedPairs.hpp:121
pFlow::unsortedPairs::PairType
kPair< idType, idType > PairType
Definition: unsortedPairs.hpp:44
pFlow::unsortedPairs::capacity
INLINE_FUNCTION_HD int32 capacity() const
Definition: unsortedPairs.hpp:168
pFlow::sortedPairs::TagFillPairs
Definition: sortedPairs.hpp:83
pFlow::sortedPairs::operator()
INLINE_FUNCTION_HD void operator()(TagFillFlag, int32 i) const
Definition: sortedPairs.hpp:238
pFlow::sortedPairs::flags_
ViewType1D< int32, ExecutionSpace > flags_
Definition: sortedPairs.hpp:90
pFlow::sortedPairs::PairType
typename UnsortedPairs::PairType PairType
Definition: sortedPairs.hpp:47
pFlow::sortedPairs::rpFillPairs
Kokkos::RangePolicy< ExecutionSpace, Kokkos::Schedule< Kokkos::Static >, Kokkos::IndexType< int32 >, TagFillPairs > rpFillPairs
Definition: sortedPairs.hpp:104
pFlow::reallocNoInit
INLINE_FUNCTION_H void reallocNoInit(ViewType1D< Type, Properties... > &view, int32 len)
Definition: KokkosUtilities.hpp:60
pFlow::unsortedPairs
Definition: unsortedPairs.hpp:32
pFlow::unsortedPairs::IdType
idType IdType
Definition: unsortedPairs.hpp:38
pFlow
Definition: demComponent.hpp:28
pFlow::sortedPairs::pairAccessor::size_
int32 size_
Definition: sortedPairs.hpp:55
pFlow::unsortedPairs::container_
ContainerType container_
Definition: unsortedPairs.hpp:78
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::sortedPairs::size_
int32 size_
size of pair list
Definition: sortedPairs.hpp:88
pFlow::sortedPairs::getPair
INLINE_FUNCTION_HD bool getPair(int32 idx, PairType &p) const
Definition: sortedPairs.hpp:145
pFlow::sortedPairs::clear
INLINE_FUNCTION_H void clear()
Definition: sortedPairs.hpp:183
pFlow::sortedPairs::memory_space
typename ExecutionSpace::memory_space memory_space
Definition: sortedPairs.hpp:45
pFlow::sortedPairs::sortedPairs
sortedPairs(int32 initialSize=1)
Definition: sortedPairs.hpp:113
pFlow::sortedPairs::pairAccessor
Definition: sortedPairs.hpp:51
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:53
unsortedPairs.hpp
pFlow::sortedPairs::afterBroadSearch
bool afterBroadSearch()
Definition: sortedPairs.hpp:127
pFlow::sortedPairs::size
INLINE_FUNCTION_H int32 size() const
Definition: sortedPairs.hpp:167
pFlow::unsortedPairs::clear
INLINE_FUNCTION_H void clear()
Definition: unsortedPairs.hpp:201
pFlow::sortedPairs::pairAccessor::sortedParis_
ViewType1D< PairType, ExecutionSpace > sortedParis_
Definition: sortedPairs.hpp:57
pFlow::sortedPairs::pairAccessor::loopCount
INLINE_FUNCTION_HD int32 loopCount() const
Definition: sortedPairs.hpp:63
pFlow::unsortedPairs::ContainerType
unorderedSet< PairType, ExecutionSpace > ContainerType
Definition: unsortedPairs.hpp:46
pFlow::sortedPairs::pairAccessor::getPair
INLINE_FUNCTION_HD PairType getPair(int i) const
Definition: sortedPairs.hpp:69
pFlow::getNth
INLINE_FUNCTION_H void getNth(dType &dst, const ViewType1D< sType, sProperties... > &src, const int32 n)
Definition: ViewAlgorithms.hpp:333
pFlow::sortedPairs::ContainerType
typename UnsortedPairs::ContainerType ContainerType
Definition: sortedPairs.hpp:49
pFlow::sortedPairs::getPair
INLINE_FUNCTION_HD PairType getPair(int32 idx) const
Definition: sortedPairs.hpp:137
pFlow::sortedPairs
Definition: sortedPairs.hpp:33
pFlow::sortedPairs::pairAccessor::isValid
INLINE_FUNCTION_HD bool isValid(int32 i) const
Definition: sortedPairs.hpp:66
pFlow::sort
void sort(Vector< T, Allocator > &vec)
Definition: VectorAlgorithm.hpp:62
pFlow::sortedPairs::getPairs
pairAccessor getPairs() const
Definition: sortedPairs.hpp:177
pFlow::sortedPairs::isValid
INLINE_FUNCTION_HD bool isValid(int32 idx) const
Definition: sortedPairs.hpp:159
pFlow::sortedPairs::prepareSorted
void prepareSorted()
Definition: sortedPairs.hpp:189
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
Definition: KokkosTypes.hpp:62
pFlow::sortedPairs::rpFillFlag
Kokkos::RangePolicy< ExecutionSpace, Kokkos::Schedule< Kokkos::Static >, Kokkos::IndexType< int32 >, TagFillFlag > rpFillFlag
Definition: sortedPairs.hpp:98
pFlow::sortedPairs::pairAccessor::PairType
typename sortedPairs::PairType PairType
Definition: sortedPairs.hpp:53
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:51
pFlow::exclusiveScan
void exclusiveScan(const ViewType1D< Type, properties... > &view, int32 start, int32 end, ViewType1D< dType, dProperties... > &dView, int32 dStart, typename std::enable_if_t< areAccessible< typename ViewType1D< Type, properties... >::execution_space, typename ViewType1D< dType, dProperties... >::memory_space >(), bool >=true)
Definition: ViewAlgorithms.hpp:518
pFlow::unsortedPairs::ExecutionSpace
executionSpace ExecutionSpace
Definition: unsortedPairs.hpp:40
pFlow::sortedPairs::ExecutionSpace
typename UnsortedPairs::ExecutionSpace ExecutionSpace
Definition: sortedPairs.hpp:43
pFlow::sortedPairs::pairAccessor::getPair
INLINE_FUNCTION_HD bool getPair(int32 i, PairType &pair) const
Definition: sortedPairs.hpp:72
pFlow::sortedPairs::TypeInfoNV
TypeInfoNV("sortedPairs")