bitsetHD.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 __bitsetHD_hpp__
23 #define __bitsetHD_hpp__
24 
25 
26 #include "KokkosTypes.hpp"
27 #include "types.hpp"
28 
29 // a lightweight container for holding bits on host or device
30 // it is NOT concurrent and it does not used atommic operation on memory
31 
32 namespace pFlow
33 {
34 
35 
36 template<typename blockType, typename MemorySpace=void>
37 class bitsetHD
38 {
39 public:
40 
41  using BlockType = blockType;
42 
43  using blockViewType = Kokkos::View<BlockType*, Kokkos::LayoutLeft, MemorySpace>;
44 
45  using deviceType = typename blockViewType::device_type;
46 
47  using memory_space = typename blockViewType::memory_space;
48 
49  using execution_space = typename blockViewType::execution_space;
50 
51 protected:
52 
54 
56 
58 
59  const static inline
60  int32 bitsPerBlock_ = std::numeric_limits<BlockType>::digits;
61 
62  const static inline
64 
65  static INLINE_FUNCTION_HD
67  {
68  return pos/bitsPerBlock_;
69  }
70 
71  static INLINE_FUNCTION_HD
73  {
74  return static_cast<BlockType>(pos%bitsPerBlock_);
75  }
76 
77  static INLINE_FUNCTION_HD
79  {
80  return static_cast<BlockType> (1 <<(pos & blockMask_));
81  }
82 
83  static INLINE_FUNCTION_HD
85  {
86  return numBits/bitsPerBlock_ + 1;
87  }
88 
89 
90 public:
91 
92 
93  bitsetHD(const word& name, int32 numBits)
94  :
97  blocks_(name, numBlocks_)
98  {
99 
100  }
101 
102  bitsetHD(const bitsetHD&) = default;
103 
104  bitsetHD(bitsetHD&&) = default;
105 
106  bitsetHD& operator=(const bitsetHD&) = default;
107 
108  bitsetHD& operator=(bitsetHD&&) = default;
109 
110  void set()
111  {
112  Kokkos::deep_copy(blocks_, ~0);
113  }
114 
115  void reset()
116  {
117  Kokkos::deep_copy( blocks_ , static_cast<BlockType>(0));
118  }
119 
120  void clear()
121  {
122  Kokkos::deep_copy( blocks_ , static_cast<BlockType>(0));
123  }
124 
126  void set(int32 pos) const
127  {
128  BlockType& block = blocks_[blockIndex(pos)];
129  block |= blockMask(pos);
130  }
131 
133  void unset(int32 pos)const
134  {
135  BlockType& block = blocks_[blockIndex(pos)];
136  block &= (~blockMask(pos));
137  }
138 
140  void reset(int32 pos)const
141  {
142  unset(pos);
143  }
144 
146  void flip(int32 pos)const
147  {
148  BlockType& block = blocks_[blockIndex(pos)];
149  block ^= blockMask(pos);
150  }
151 
153  bool isSet(int32 pos)const
154  {
155  BlockType& block = blocks_[blockIndex(pos)];
156  return block & blockMask(pos);
157  }
158 
160  bool isUnset(int32 pos)const
161  {
162  return !isSet(pos);
163  }
164 
166  bool isSetReset(int32 pos)const
167  {
168  BlockType& block = blocks_[blockIndex(pos)];
169  auto mask = blockMask(pos);
170  bool is_set = block & mask;
171  block &= (~mask);
172  return is_set;
173  }
174 
177  {
178  return numBlocks_;
179  }
180 
182  int32 numBits()const
183  {
184  return numBits_;
185  }
186 
188  int32 size()const
189  {
190  return numBits_;
191  }
192 
195  {
196  return numBlocks_*bitsPerBlock_;
197  }
198 
201  {
203  numBits_ = numBits;
205  }
206 
207 
208 };
209 
210 
212 
214 
216 
218 
219 
220 } // namespace pFlow
221 
222 
223 #endif //__bitsetHD_hpp__
pFlow::bitsetHD::numBits_
int32 numBits_
Definition: bitsetHD.hpp:55
pFlow::bitsetHD::bitIndex
static INLINE_FUNCTION_HD BlockType bitIndex(int32 pos)
Definition: bitsetHD.hpp:72
pFlow::bitsetHD::reset
INLINE_FUNCTION_HD void reset(int32 pos) const
Definition: bitsetHD.hpp:140
pFlow::bitsetHD::set
INLINE_FUNCTION_HD void set(int32 pos) const
Definition: bitsetHD.hpp:126
pFlow::bitsetHD::BlockType
blockType BlockType
Definition: bitsetHD.hpp:41
pFlow::bitsetHD::flip
INLINE_FUNCTION_HD void flip(int32 pos) const
Definition: bitsetHD.hpp:146
pFlow::bitsetHD::reset
void reset()
Definition: bitsetHD.hpp:115
pFlow::bitsetHD::blockMask_
const static int32 blockMask_
Definition: bitsetHD.hpp:63
pFlow::bitsetHD::blockMask
static INLINE_FUNCTION_HD BlockType blockMask(int32 pos)
Definition: bitsetHD.hpp:78
pFlow::bitsetHD::size
INLINE_FUNCTION_HD int32 size() const
Definition: bitsetHD.hpp:188
types.hpp
pFlow::bitsetHD::clear
void clear()
Definition: bitsetHD.hpp:120
pFlow::bitsetHD::execution_space
typename blockViewType::execution_space execution_space
Definition: bitsetHD.hpp:49
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::bitsetHD::blockIndex
static INLINE_FUNCTION_HD int32 blockIndex(int32 pos)
Definition: bitsetHD.hpp:66
KokkosTypes.hpp
pFlow::bitsetHD::numBits
INLINE_FUNCTION_HD int32 numBits() const
Definition: bitsetHD.hpp:182
pFlow::bitsetHD::operator=
bitsetHD & operator=(const bitsetHD &)=default
pFlow::bitsetHD::blocks_
blockViewType blocks_
Definition: bitsetHD.hpp:57
pFlow::bitsetHD::blockViewType
Kokkos::View< BlockType *, Kokkos::LayoutLeft, MemorySpace > blockViewType
Definition: bitsetHD.hpp:43
pFlow::bitsetHD::set
void set()
Definition: bitsetHD.hpp:110
pFlow
Definition: demComponent.hpp:28
pFlow::bitsetHD::isSet
INLINE_FUNCTION_HD bool isSet(int32 pos) const
Definition: bitsetHD.hpp:153
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::bitsetHD::bitsetHD
bitsetHD(const word &name, int32 numBits)
Definition: bitsetHD.hpp:93
pFlow::bitsetHD::realloc
INLINE_FUNCTION_H void realloc(int32 numBits)
Definition: bitsetHD.hpp:200
pFlow::bitsetHD::unset
INLINE_FUNCTION_HD void unset(int32 pos) const
Definition: bitsetHD.hpp:133
pFlow::bitsetHD::numBlocks_
int32 numBlocks_
Definition: bitsetHD.hpp:53
pFlow::bitsetHD::deviceType
typename blockViewType::device_type deviceType
Definition: bitsetHD.hpp:45
pFlow::bitsetHD::numBlocks
INLINE_FUNCTION_HD int32 numBlocks() const
Definition: bitsetHD.hpp:176
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:53
pFlow::bitsetHD::bitsPerBlock_
const static int32 bitsPerBlock_
Definition: bitsetHD.hpp:60
pFlow::bitsetHD::isSetReset
INLINE_FUNCTION_HD bool isSetReset(int32 pos) const
Definition: bitsetHD.hpp:166
pFlow::bitsetHD::memory_space
typename blockViewType::memory_space memory_space
Definition: bitsetHD.hpp:47
pFlow::bitsetHD::isUnset
INLINE_FUNCTION_HD bool isUnset(int32 pos) const
Definition: bitsetHD.hpp:160
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:51
pFlow::bitsetHD
Definition: bitsetHD.hpp:37
pFlow::bitsetHD::calculateBlockSize
static INLINE_FUNCTION_HD int32 calculateBlockSize(int32 numBits)
Definition: bitsetHD.hpp:84
pFlow::bitsetHD::capacity
INLINE_FUNCTION_HD int32 capacity() const
Definition: bitsetHD.hpp:194