# PhasicFlow: src/Interaction/contactLists/sortedPairs.hpp Source File

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;
int32 size_;
ViewType1D<PairType,ExecutionSpace> sortedParis_;
INLINE_FUNCTION_HD
int32 size()const { return size_; }
INLINE_FUNCTION_HD
int32 loopCount()const { return size_; }
INLINE_FUNCTION_HD
bool isValid(int32 i)const { return i<size_; }
INLINE_FUNCTION_HD
PairType getPair(int i)const { return sortedParis_[i]; }
INLINE_FUNCTION_HD
bool getPair(int32 i, PairType& pair)const {
if(i<size_) {
pair = sortedParis_[i];
return true;
}
return false;
}
};
class TagFillFlag{};
class TagFillPairs{};
protected:
int32 size_ = 0;
ViewType1D<int32,ExecutionSpace> flags_;
ViewType1D<PairType,ExecutionSpace> sortedPairs_;
using rpFillFlag = Kokkos::RangePolicy<
ExecutionSpace,
Kokkos::Schedule<Kokkos::Static>,
Kokkos::IndexType<int32>,
TagFillFlag >;
using rpFillPairs = Kokkos::RangePolicy<
ExecutionSpace,
Kokkos::Schedule<Kokkos::Static>,
Kokkos::IndexType<int32>,
TagFillPairs>;
public:
// - type info
TypeInfoNV("sortedPairs");
// constructors
sortedPairs(int32 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(int32 idx)const
{
return sortedPairs_[idx];
}
// - Device/host call
// return the pair at index idx
INLINE_FUNCTION_HD
bool getPair(int32 idx, PairType& p)const
{
if(isValid(idx))
{
p = getPair(idx);
return true;
}
else
{
return false;
}
}
INLINE_FUNCTION_HD
bool isValid(int32 idx)const
{
return idx < size_;
}
//use this when the value of size_ is updated
INLINE_FUNCTION_H
int32 size()const
{
return size_;
}
int32 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_
int32 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
int32 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, int32 i)const
{
if(this->container_.valid_at(i) )
flags_[i] = 1;
else
flags_[i] = 0;
}
INLINE_FUNCTION_HD
void operator()(TagFillPairs, int32 i)const
{
auto fi = flags_[i];
if(fi!=flags_[i+1])
sortedPairs_[fi] = this->container_.key_at(i);
}
};
}
#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")