# PhasicFlow: src/Interaction/contactLists/sortedPairs.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.

sortedPairs.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 __sortedPairs_hpp__
#define __sortedPairs_hpp__
#include "unsortedPairs.hpp"
#include "KokkosUtilities.hpp"
namespace pFlow
{
template<typename executionSpace, typename idType>
class sortedPairs
:
public unsortedPairs<executionSpace, idType>
{
public:
using UnsortedPairs = unsortedPairs<executionSpace,idType>;
using IdType = typename UnsortedPairs::IdType;
using ExecutionSpace = typename UnsortedPairs::ExecutionSpace;
using memory_space = typename ExecutionSpace::memory_space;
using PairType = typename UnsortedPairs::PairType;
using ContainerType = typename UnsortedPairs::ContainerType;
struct pairAccessor
{
using PairType = typename sortedPairs::PairType;
uint32 size_;
ViewType1D<PairType,ExecutionSpace> sortedParis_;
INLINE_FUNCTION_HD
uint32 size()const { return size_; }
INLINE_FUNCTION_HD
uint32 loopCount()const { return size_; }
INLINE_FUNCTION_HD
bool isValid(uint32 i)const { return i<size_; }
INLINE_FUNCTION_HD
PairType getPair(uint32 i)const { return sortedParis_[i]; }
INLINE_FUNCTION_HD
bool getPair(uint32 i, PairType& pair)const {
if(i<size_) {
pair = sortedParis_[i];
return true;
}
return false;
}
};
class TagFillFlag{};
class TagFillPairs{};
protected:
uint32 size_ = 0;
ViewType1D<uint32,ExecutionSpace> flags_;
ViewType1D<PairType,ExecutionSpace> sortedPairs_;
using rpFillFlag = Kokkos::RangePolicy<
ExecutionSpace,
Kokkos::Schedule<Kokkos::Static>,
Kokkos::IndexType<uint32>,
TagFillFlag >;
using rpFillPairs = Kokkos::RangePolicy<
ExecutionSpace,
Kokkos::Schedule<Kokkos::Static>,
Kokkos::IndexType<uint32>,
TagFillPairs>;
public:
// - type info
TypeInfoNV("sortedPairs");
// constructors
explicit sortedPairs(uint32 initialSize =1)
:
UnsortedPairs(initialSize),
flags_("flags_",UnsortedPairs::capacity()+1),
sortedPairs_("sortedPairs_",UnsortedPairs::capacity())
{}
bool beforeBroadSearch()
{
this->clear();
return true;
}
bool afterBroadSearch()
{
prepareSorted();
return true;
}
// - Device call
// return the pair at index idx
// perform no check for size and existance
INLINE_FUNCTION_HD
PairType getPair(uint32 idx)const
{
return sortedPairs_[idx];
}
// - Device/host call
// return the pair at index idx
INLINE_FUNCTION_HD
bool getPair(uint32 idx, PairType& p)const
{
if(isValid(idx))
{
p = getPair(idx);
return true;
}
else
{
return false;
}
}
INLINE_FUNCTION_HD
bool isValid(uint32 idx)const
{
return idx < size_;
}
//use this when the value of size_ is updated
INLINE_FUNCTION_H
uint32 size()const
{
return size_;
}
uint32 loopCount()const
{
return size_;
}
pairAccessor getPairs()const
{
return {size_, sortedPairs_};
}
INLINE_FUNCTION_H
void clear()
{
UnsortedPairs::clear();
size_ = 0;
}
void prepareSorted()
{
// first check the size of flags_
uint32 capacity = UnsortedPairs::capacity();
if( capacity+1 > flags_.size() )
{
reallocNoInit(flags_, capacity+1);
}
// fill the flags
Kokkos::parallel_for(
"contactPairsSorted::fillFlag",
rpFillFlag(0,capacity+1),
*this);
Kokkos::fence();
// inclusive scan on flags_
exclusiveScan(flags_, 0, capacity+1,flags_,0);
Kokkos::fence(); // Kokkos's scan is blocking I guess. So, this could be removed.
// get the last value of flags_ to obtain the size of sortedPairs_
getNth(size_, flags_, capacity);
if(size_ == 0 )return;
// resize sortedPairs_ if necessary;
if( size_>sortedPairs_.size() )
{
// get more space to prevent reallocations in next iterations
uint32 len = size_*1.1+1;
reallocNoInit(sortedPairs_, len);
}
Kokkos::parallel_for(
"contactPairsSorted::fillPairs",
rpFillPairs(0,this->capacity()),
*this);
Kokkos::fence();
// - sort paris based on the first and second
sort(sortedPairs_, 0, size_ );
}
INLINE_FUNCTION_HD
void operator()(TagFillFlag, uint32 i)const
{
if(this->container_.valid_at(i) )
flags_[i] = 1;
else
flags_[i] = 0;
}
INLINE_FUNCTION_HD
void operator()(TagFillPairs, uint32 i)const
{
auto fi = flags_[i];
if(fi!=flags_[i+1])
sortedPairs_[fi] = this->container_.key_at(i);
}
};
}
#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")