# PhasicFlow: src/phasicFlow/containers/bitsetHD/bitsetHD.hpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

bitsetHD.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#ifndef __bitsetHD_hpp__
#define __bitsetHD_hpp__
#include "KokkosTypes.hpp"
#include "types.hpp"
// a lightweight container for holding bits on host or device
// it is NOT concurrent and it does not used atommic operation on memory
namespace pFlow
{
template<typename blockType, typename MemorySpace=void>
class bitsetHD
{
public:
using BlockType = blockType;
using blockViewType = Kokkos::View<BlockType*, Kokkos::LayoutLeft, MemorySpace>;
using deviceType = typename blockViewType::device_type;
using memory_space = typename blockViewType::memory_space;
using execution_space = typename blockViewType::execution_space;
protected:
int32 numBlocks_;
int32 numBits_;
blockViewType blocks_;
const static inline
int32 bitsPerBlock_ = std::numeric_limits<BlockType>::digits;
const static inline
int32 blockMask_ = bitsPerBlock_ - 1;
static INLINE_FUNCTION_HD
int32 blockIndex(int32 pos)
{
return pos/bitsPerBlock_;
}
static INLINE_FUNCTION_HD
BlockType bitIndex(int32 pos)
{
return static_cast< BlockType >(pos%bitsPerBlock_);
}
static INLINE_FUNCTION_HD
BlockType blockMask(int32 pos)
{
return static_cast< BlockType > (1 <<(pos & blockMask_));
}
static INLINE_FUNCTION_HD
int32 calculateBlockSize(int32 numBits)
{
return numBits/bitsPerBlock_ + 1;
}
public:
bitsetHD(const word& name, int32 numBits)
:
numBlocks_(calculateBlockSize(numBits)),
numBits_(numBits),
blocks_(name, numBlocks_)
{
}
bitsetHD(const bitsetHD&) = default;
bitsetHD(bitsetHD&&) = default;
bitsetHD& operator=(const bitsetHD&) = default;
bitsetHD& operator=(bitsetHD&&) = default;
void set()
{
Kokkos::deep_copy(blocks_, ~0);
}
void reset()
{
Kokkos::deep_copy( blocks_ , static_cast< BlockType >(0));
}
void clear()
{
Kokkos::deep_copy( blocks_ , static_cast< BlockType >(0));
}
INLINE_FUNCTION_HD
void set(int32 pos) const
{
BlockType& block = blocks_[blockIndex(pos)];
block |= blockMask(pos);
}
INLINE_FUNCTION_HD
void unset(int32 pos)const
{
BlockType& block = blocks_[blockIndex(pos)];
block &= (~blockMask(pos));
}
INLINE_FUNCTION_HD
void reset(int32 pos)const
{
unset(pos);
}
INLINE_FUNCTION_HD
void flip(int32 pos)const
{
BlockType& block = blocks_[blockIndex(pos)];
block ^= blockMask(pos);
}
INLINE_FUNCTION_HD
bool isSet(int32 pos)const
{
BlockType& block = blocks_[blockIndex(pos)];
return block & blockMask(pos);
}
INLINE_FUNCTION_HD
bool isUnset(int32 pos)const
{
return !isSet(pos);
}
INLINE_FUNCTION_HD
bool isSetReset(int32 pos)const
{
BlockType& block = blocks_[blockIndex(pos)];
auto mask = blockMask(pos);
bool is_set = block & mask;
block &= (~mask);
return is_set;
}
INLINE_FUNCTION_HD
int32 numBlocks()const
{
return numBlocks_;
}
INLINE_FUNCTION_HD
int32 numBits()const
{
return numBits_;
}
INLINE_FUNCTION_HD
int32 size()const
{
return numBits_;
}
INLINE_FUNCTION_HD
int32 capacity()const
{
return numBlocks_*bitsPerBlock_;
}
INLINE_FUNCTION_H
void realloc(int32 numBits)
{
numBlocks_ = calculateBlockSize(numBits);
numBits_ = numBits;
Kokkos::realloc(blocks_, numBlocks_);
}
};
using bitset32_D = bitsetHD<unsigned>;
using bitset32_H = bitsetHD<unsigned, HostSpace>;
using bitset64_D = bitsetHD<unsigned long>;
using bitset64_H = bitsetHD<unsigned long, HostSpace>;
} // namespace pFlow
#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:64
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: demGeometry.hpp:27
pFlow::bitsetHD::isSet
INLINE_FUNCTION_HD bool isSet(int32 pos) const
Definition: bitsetHD.hpp:153
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
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:57
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:55
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