# PhasicFlow: src/phasicFlow/algorithms/cudaAlgorithms.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.

cudaAlgorithms.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 __cudaAlgorithms_hpp__
#define __cudaAlgorithms_hpp__
#ifdef __CUDACC__
#include <thrust/fill.h>
#include <thrust/count.h>
#include <thrust/scan.h>
#include <thrust/sort.h>
#include <thrust/extrema.h>
#include <thrust/execution_policy.h>
#include "pFlowMacros.hpp"
#include "algorithmFunctions.hpp"
#include "types.hpp"
namespace pFlow::algorithms::CUDA
{
template<typename Type>
INLINE_FUNCTION_H
int32 count(const Type* first, int32 numElems, const Type& val)
{
return thrust::count_if(
thrust::device,
first, first+numElems,
LAMBDA_HD(const Type& check){ return equal(check,val);} );
}
template<typename Type>
INLINE_FUNCTION_H
void fill(Type* first, int32 numElems, const Type& val)
{
thrust::fill(thrust::device, first, first+numElems, val);
}
template<typename Type>
INLINE_FUNCTION_H
void fillSequence(Type* first, int32 numElems, const Type& firstVal)
{
thrust::for_each_n(
thrust::device,
first,
numElems,
LAMBDA_HD(Type& ref){ ref = firstVal+(&ref - first);});
}
template<typename Type, typename indexType>
INLINE_FUNCTION_H
void fillSelected(Type* first, const indexType* indices, const int32 numElems, const Type val)
{
thrust::for_each_n(
thrust::device,
indices,
numElems,
LAMBDA_HD(indexType i){
first[i] = val;
});
}
template<typename Type>
INLINE_FUNCTION_H
Type max(const Type* first, int32 numElems)
{
Type resH;
auto* resD = thrust::max_element(
thrust::device,
first,
first+numElems,
less<Type>());
cudaMemcpy(&resH, resD, sizeof(Type),cudaMemcpyDeviceToHost);
return resH;
}
template<typename Type>
INLINE_FUNCTION_H
Type min(const Type* first, int32 numElems)
{
Type resH;
auto* resD = thrust::min_element(
thrust::device,
first,
first+numElems,
less<Type>());
cudaMemcpy(&resH, resD, sizeof(Type),cudaMemcpyDeviceToHost);
return resH;
}
template<typename Type>
INLINE_FUNCTION_H
void sort(Type* first, int32 numElems)
{
thrust::sort(
thrust::device,
first,
first+numElems,
less<Type>());
}
template<typename Type, typename CompareFunc>
INLINE_FUNCTION_H
void sort(Type* first, int32 numElems, CompareFunc compare)
{
thrust::sort(
thrust::device,
first,
first+numElems,
compare);
}
template<typename Type, typename PermuteType>
INLINE_FUNCTION_H
void permuteSort(const Type* first, PermuteType* pFirst, int32 numElems)
{
fillSequence(pFirst, numElems, static_cast<PermuteType>(0));
thrust::sort(
thrust::device,
pFirst,
pFirst+numElems,
LAMBDA_HD(const PermuteType& lhs, const PermuteType& rhs){
return first[lhs]<first[rhs]; });
}
template<typename Type, typename DestType>
void exclusiveScan(Type* first, DestType* dFirst, int32 numElems)
{
thrust::exclusive_scan(
thrust::device,
first, first+numElems,
dFirst, 0);
}
template<typename Type, typename DestType>
void inclusiveScan(Type* first, DestType* dFirst, int32 numElems)
{
thrust::inclusive_scan(
thrust::device,
first, first+numElems,
dFirst);
}
} // end of namespace
#endif //__CUDA__
#endif //__cudaAlgorithms_hpp__
pFlow::inclusiveScan
void inclusiveScan(const ViewType1D< Type, properties... > &view, uint32 start, uint32 end, ViewType1D< Type, dProperties... > &dView, uint32 dStart)
Definition: ViewAlgorithms.hpp:467
pFlow::fill
void fill(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:44
types.hpp
algorithmFunctions.hpp
pFlow::equal
INLINE_FUNCTION_HD bool equal(const box &b1, const box &b2, real tol=smallValue)
Definition: box.hpp:135
count_if
auto count_if(const Vector< T, Allocator > &vec, UnaryPredicate p)
pFlow::max
T max(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:79
pFlow::fillSelected
bool fillSelected(ViewType1D< Type, properties... > view, ViewType1D< indexType, indexProperties... > indices, uint32 numElems, Type val)
Definition: ViewAlgorithms.hpp:135
pFlow::permuteSort
void permuteSort(const ViewType1D< Type, properties... > &view, uint32 start, uint32 end, ViewType1D< permType, permProperties... > &permuteView, uint32 permStart)
Definition: ViewAlgorithms.hpp:346
pFlowMacros.hpp
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
fill
void fill(Vector< T, Allocator > &vec, const T &val)
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:57
pFlow::count
auto count(const Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:26
pFlow::min
T min(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:28
sort
void sort(Vector< T, Allocator > &vec)
pFlow::sort
void sort(Vector< T, Allocator > &vec)
Definition: VectorAlgorithm.hpp:62
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:58
pFlow::fillSequence
void fillSequence(internalField< T, MemorySpace > &iField, const T &startVal)
Definition: internalFieldAlgorithms.hpp:198
pFlow::exclusiveScan
void exclusiveScan(const ViewType1D< Type, properties... > &view, uint32 start, uint32 end, ViewType1D< Type, dProperties... > &dView, uint32 dStart)
Definition: ViewAlgorithms.hpp:439