www.cemf.ir
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  uint32 size()const { return size_; }
61 
63  uint32 loopCount()const { return size_; }
64 
66  bool isValid(uint32 i)const { return i<size_; }
67 
69  PairType getPair(uint32 i)const { return sortedParis_[i]; }
70 
72  bool getPair(uint32 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 
89 
91 
93 
94  using rpFillFlag = Kokkos::RangePolicy<
96  Kokkos::Schedule<Kokkos::Static>,
97  Kokkos::IndexType<uint32>,
99 
100  using rpFillPairs = Kokkos::RangePolicy<
102  Kokkos::Schedule<Kokkos::Static>,
103  Kokkos::IndexType<uint32>,
105 
106 public:
107 
108  // - type info
109  TypeInfoNV("sortedPairs");
110 
111 
112  // constructors
113  explicit sortedPairs(uint32 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(uint32 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(uint32 idx)const
160  {
161  return idx < size_;
162  }
163 
164 
165  //use this when the value of size_ is updated
167  uint32 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  uint32 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::sortedPairs
sortedPairs(uint32 initialSize=1)
Definition: sortedPairs.hpp:113
pFlow::sortedPairs::operator()
INLINE_FUNCTION_HD void operator()(TagFillFlag, uint32 i) const
Definition: sortedPairs.hpp:238
pFlow::sortedPairs::IdType
typename UnsortedPairs::IdType IdType
Definition: sortedPairs.hpp:41
pFlow::sortedPairs::sortedPairs_
ViewType1D< PairType, ExecutionSpace > sortedPairs_
Definition: sortedPairs.hpp:92
pFlow::unsortedPairs::capacity
INLINE_FUNCTION_HD uint32 capacity() const
Definition: unsortedPairs.hpp:168
pFlow::sortedPairs::pairAccessor::getPair
INLINE_FUNCTION_HD bool getPair(uint32 i, PairType &pair) const
Definition: sortedPairs.hpp:72
pFlow::sortedPairs::TagFillFlag
Definition: sortedPairs.hpp:81
KokkosUtilities.hpp
pFlow::sortedPairs::beforeBroadSearch
bool beforeBroadSearch()
Definition: sortedPairs.hpp:121
pFlow::unsortedPairs::PairType
Pair< idType, idType > PairType
Definition: unsortedPairs.hpp:44
pFlow::sortedPairs::TagFillPairs
Definition: sortedPairs.hpp:83
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::reallocNoInit
INLINE_FUNCTION_H void reallocNoInit(ViewType1D< Type, Properties... > &view, uint32 len)
Definition: KokkosUtilities.hpp:64
pFlow::sortedPairs::PairType
typename UnsortedPairs::PairType PairType
Definition: sortedPairs.hpp:47
pFlow::sortedPairs::rpFillFlag
Kokkos::RangePolicy< ExecutionSpace, Kokkos::Schedule< Kokkos::Static >, Kokkos::IndexType< uint32 >, TagFillFlag > rpFillFlag
Definition: sortedPairs.hpp:98
pFlow::sortedPairs::isValid
INLINE_FUNCTION_HD bool isValid(uint32 idx) const
Definition: sortedPairs.hpp:159
pFlow::getNth
INLINE_FUNCTION_H void getNth(Type &dst, const ViewType1D< Type, sProperties... > &src, const uint32 n)
Definition: ViewAlgorithms.hpp:270
pFlow::unsortedPairs
Definition: unsortedPairs.hpp:32
pFlow::unsortedPairs::IdType
idType IdType
Definition: unsortedPairs.hpp:38
pFlow
Definition: demGeometry.hpp:27
pFlow::sortedPairs::size_
uint32 size_
size of pair list
Definition: sortedPairs.hpp:88
pFlow::unsortedPairs::container_
ContainerType container_
Definition: unsortedPairs.hpp:78
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::pairAccessor
Definition: sortedPairs.hpp:51
pFlow::sortedPairs::pairAccessor::getPair
INLINE_FUNCTION_HD PairType getPair(uint32 i) const
Definition: sortedPairs.hpp:69
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:57
pFlow::sortedPairs::flags_
ViewType1D< uint32, ExecutionSpace > flags_
Definition: sortedPairs.hpp:90
unsortedPairs.hpp
pFlow::sortedPairs::afterBroadSearch
bool afterBroadSearch()
Definition: sortedPairs.hpp:127
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::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
1D veiw as a vector
Definition: KokkosTypes.hpp:93
pFlow::unsortedPairs::ContainerType
unorderedSet< PairType, ExecutionSpace > ContainerType
Definition: unsortedPairs.hpp:46
pFlow::sortedPairs::getPair
INLINE_FUNCTION_HD PairType getPair(uint32 idx) const
Definition: sortedPairs.hpp:137
pFlow::sortedPairs::ContainerType
typename UnsortedPairs::ContainerType ContainerType
Definition: sortedPairs.hpp:49
pFlow::sortedPairs
Definition: sortedPairs.hpp:33
pFlow::sortedPairs::pairAccessor::isValid
INLINE_FUNCTION_HD bool isValid(uint32 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::prepareSorted
void prepareSorted()
Definition: sortedPairs.hpp:189
pFlow::sortedPairs::size
INLINE_FUNCTION_H uint32 size() const
Definition: sortedPairs.hpp:167
pFlow::sortedPairs::rpFillPairs
Kokkos::RangePolicy< ExecutionSpace, Kokkos::Schedule< Kokkos::Static >, Kokkos::IndexType< uint32 >, TagFillPairs > rpFillPairs
Definition: sortedPairs.hpp:104
pFlow::sortedPairs::operator()
INLINE_FUNCTION_HD void operator()(TagFillPairs, uint32 i) const
Definition: sortedPairs.hpp:247
pFlow::sortedPairs::pairAccessor::PairType
typename sortedPairs::PairType PairType
Definition: sortedPairs.hpp:53
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:55
pFlow::sortedPairs::pairAccessor::size_
uint32 size_
Definition: sortedPairs.hpp:55
pFlow::unsortedPairs::ExecutionSpace
executionSpace ExecutionSpace
Definition: unsortedPairs.hpp:40
pFlow::sortedPairs::ExecutionSpace
typename UnsortedPairs::ExecutionSpace ExecutionSpace
Definition: sortedPairs.hpp:43
pFlow::sortedPairs::loopCount
uint32 loopCount() const
Definition: sortedPairs.hpp:172
pFlow::sortedPairs::pairAccessor::loopCount
INLINE_FUNCTION_HD uint32 loopCount() const
Definition: sortedPairs.hpp:63
pFlow::exclusiveScan
void exclusiveScan(const ViewType1D< Type, properties... > &view, uint32 start, uint32 end, ViewType1D< Type, dProperties... > &dView, uint32 dStart)
Definition: ViewAlgorithms.hpp:439
pFlow::sortedPairs::getPair
INLINE_FUNCTION_HD bool getPair(uint32 idx, PairType &p) const
Definition: sortedPairs.hpp:145
pFlow::sortedPairs::pairAccessor::size
INLINE_FUNCTION_HD uint32 size() const
Definition: sortedPairs.hpp:60
pFlow::sortedPairs::TypeInfoNV
TypeInfoNV("sortedPairs")