Skip to content

Commit

Permalink
Merge pull request picassodev#94 from kwitaechong/particle_list
Browse files Browse the repository at this point in the history
Replace duplicated Particle get functions, ParticleList, and createParticles
  • Loading branch information
streeve authored Sep 26, 2023
2 parents dc75aa5 + 32b2d65 commit b021f61
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 581 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
uses: actions/checkout@v2.2.0
with:
repository: ECP-CoPA/Cabana
ref: 0.5.0
ref: 0.6.0
path: cabana
- name: Build Cabana
working-directory: cabana
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ RUN ARBORX_VERSION=v1.0 && \

# Install Cabana
ENV CABANA_DIR=/opt/cabana
RUN CABANA_VERSION=0.5.0 && \
RUN CABANA_VERSION=0.6.0 && \
CABANA_URL=https://github.com/ECP-copa/Cabana/archive/${CABANA_VERSION}.tar.gz && \
CABANA_ARCHIVE=cabana.tar.gz && \
wget --quiet ${CABANA_URL} --output-document=${CABANA_ARCHIVE} && \
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.hipcc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ RUN ARBORX_VERSION=v1.0 && \

# Install Cabana
ENV CABANA_DIR=/opt/cabana
RUN CABANA_VERSION=0.5.0 && \
RUN CABANA_VERSION=0.6.0 && \
CABANA_URL=https://github.com/ECP-copa/Cabana/archive/${CABANA_VERSION}.tar.gz && \
CABANA_ARCHIVE=cabana.tar.gz && \
wget --quiet ${CABANA_URL} --output-document=${CABANA_ARCHIVE} && \
Expand Down
69 changes: 62 additions & 7 deletions src/Picasso_ParticleInit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <Picasso_BatchedLinearAlgebra.hpp>
#include <Picasso_Types.hpp>
#include <Picasso_UniformMesh.hpp>

#include <Cabana_Core.hpp>
#include <Cajita.hpp>
Expand Down Expand Up @@ -113,12 +114,16 @@ void filterEmpties( const ExecutionSpace& exec_space,
\param particle_list The list of particles to populate. This will be filled
with particles and resized to a size equal to the number of particles
created.
\param mesh Picasso UniformMesh
*/
template <class ParticleListType, class InitFunctor, class ExecutionSpace>
template <class ParticleListType, class InitFunctor, class ExecutionSpace,
class MeshType>
void initializeParticles( InitRandom, const ExecutionSpace& exec_space,
const int particles_per_cell,
const InitFunctor& create_functor,
ParticleListType& particle_list,
std::shared_ptr<MeshType>& mesh,
const bool shrink_to_fit = true )
{
Kokkos::Profiling::pushRegion( "Picasso::initializeParticles::Random" );
Expand All @@ -127,7 +132,7 @@ void initializeParticles( InitRandom, const ExecutionSpace& exec_space,
using particle_type = typename ParticleListType::particle_type;

// Get the local grid.
const auto& local_grid = *( particle_list.mesh().localGrid() );
const auto& local_grid = *( mesh->localGrid() );

// Create a local mesh.
auto local_mesh = Cajita::createLocalMesh<ExecutionSpace>( local_grid );
Expand Down Expand Up @@ -237,6 +242,19 @@ void initializeParticles( InitRandom, const ExecutionSpace& exec_space,
Kokkos::Profiling::popRegion();
}

// Wrapper for Picasso::ParticleList
template <class ParticleListType, class InitFunctor, class ExecutionSpace>
void initializeParticles( InitRandom tag, const ExecutionSpace& exec_space,
const int particles_per_cell,
const InitFunctor& create_functor,
ParticleListType& particle_list,
const bool shrink_to_fit = true )
{

initializeParticles( tag, exec_space, particles_per_cell, create_functor,
particle_list, particle_list.mesh(), shrink_to_fit );
}

//---------------------------------------------------------------------------//
/*!
\brief Initialize a uniform number of particles in each cell given an
Expand All @@ -262,12 +280,16 @@ void initializeParticles( InitRandom, const ExecutionSpace& exec_space,
\param particle_list The list of particles to populate. This will be filled
with particles and resized to a size equal to the number of particles
created.
\param mesh Picasso UniformMesh
*/
template <class ParticleListType, class InitFunctor, class ExecutionSpace>
template <class ParticleListType, class InitFunctor, class ExecutionSpace,
class MeshType>
void initializeParticles( InitUniform, const ExecutionSpace& exec_space,
const int particles_per_cell_dim,
const InitFunctor& create_functor,
ParticleListType& particle_list,
std::shared_ptr<MeshType>& mesh,
const bool shrink_to_fit = true )
{
Kokkos::Profiling::pushRegion( "Picasso::initializeParticles::Uniform" );
Expand All @@ -279,7 +301,7 @@ void initializeParticles( InitUniform, const ExecutionSpace& exec_space,
using particle_type = typename ParticleListType::particle_type;

// Get the local grid.
const auto& local_grid = *( particle_list.mesh().localGrid() );
const auto& local_grid = *( mesh->localGrid() );

// Create a local mesh.
auto local_mesh = Cajita::createLocalMesh<ExecutionSpace>( local_grid );
Expand Down Expand Up @@ -393,6 +415,20 @@ void initializeParticles( InitUniform, const ExecutionSpace& exec_space,
Kokkos::Profiling::popRegion();
}

// Wrapper for Picasso::ParticleList
template <class ParticleListType, class InitFunctor, class ExecutionSpace>
void initializeParticles( InitUniform tag, const ExecutionSpace& exec_space,
const int particles_per_cell_dim,
const InitFunctor& create_functor,
ParticleListType& particle_list,
const bool shrink_to_fit = true )
{

initializeParticles( tag, exec_space, particles_per_cell_dim,
create_functor, particle_list, particle_list.mesh(),
shrink_to_fit );
}

//---------------------------------------------------------------------------//
/*!
\brief Initialize a uniform number of particles in each cell given an
Expand Down Expand Up @@ -429,22 +465,25 @@ void initializeParticles( InitUniform, const ExecutionSpace& exec_space,
\param surface_particle_list The list of particles to populate. This will be
filled with particles and resized to a size equal to the number of particles
created.
\param mesh Picasso UniformMesh
*/
template <class ParticleListType, class InitFunc, class FacetGeometry,
class ExecutionSpace>
class ExecutionSpace, class MeshType>
void initializeParticlesSurface( InitRandom, const ExecutionSpace&,
const int particles_per_facet,
const FacetGeometry& surface,
const InitFunc& create_functor,
ParticleListType& surface_particle_list )
ParticleListType& surface_particle_list,
std::shared_ptr<MeshType>& mesh )
{
Kokkos::Profiling::pushRegion( "Picasso::initializeParticles::Surface" );

// Particle type.
using particle_type = typename ParticleListType::particle_type;

// Get the local grid.
const auto& local_grid = *( surface_particle_list.mesh().localGrid() );
const auto& local_grid = *( mesh->localGrid() );

// Create a local mesh.
auto local_mesh = Cajita::createLocalMesh<ExecutionSpace>( local_grid );
Expand Down Expand Up @@ -535,6 +574,22 @@ void initializeParticlesSurface( InitRandom, const ExecutionSpace&,
Kokkos::Profiling::popRegion();
}

// Wrapper for Picasso::ParticleList
template <class ParticleListType, class InitFunctor, class FacetGeometry,
class ExecutionSpace>
void initializeParticlesSurface( InitRandom tag,
const ExecutionSpace& exec_space,
const int particles_per_facet,
const FacetGeometry& surface,
const InitFunctor& create_functor,
ParticleListType& surface_particle_list,
const bool shrink_to_fit = true )
{

initializeParticles( tag, exec_space, particles_per_facet, create_functor,
surface_particle_list, surface_particle_list.mesh() );
}

//---------------------------------------------------------------------------//

} // end namespace Picasso
Expand Down
117 changes: 19 additions & 98 deletions src/Picasso_ParticleList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,98 +25,18 @@

namespace Picasso
{
//---------------------------------------------------------------------------//
// Particle Traits
//---------------------------------------------------------------------------//
template <class... FieldTags>
struct ParticleTraits
{
using member_types = Cabana::MemberTypes<typename FieldTags::data_type...>;
};

//---------------------------------------------------------------------------//
// Particle copy. Wraps a tuple copy of a particle.
//---------------------------------------------------------------------------//
template <class... FieldTags>
struct Particle
{
using traits = ParticleTraits<FieldTags...>;
using tuple_type = Cabana::Tuple<typename traits::member_types>;

static constexpr int vector_length = 1;

// Default constructor.
Particle() = default;

// Tuple wrapper constructor.
KOKKOS_FORCEINLINE_FUNCTION
Particle( const tuple_type& tuple )
: _tuple( tuple )
{
}

// Get the underlying tuple.
KOKKOS_FORCEINLINE_FUNCTION
tuple_type& tuple() { return _tuple; }

KOKKOS_FORCEINLINE_FUNCTION
const tuple_type& tuple() const { return _tuple; }

// The tuple this particle wraps.
tuple_type _tuple;
};

//---------------------------------------------------------------------------//
// Particle view. Wraps a view of the SoA the particle resides in.
//---------------------------------------------------------------------------//
template <int VectorLength, class... FieldTags>
struct ParticleView
{
using traits = ParticleTraits<FieldTags...>;
using soa_type = Cabana::SoA<typename traits::member_types, VectorLength>;

static constexpr int vector_length = VectorLength;

// Default constructor.
ParticleView() = default;

// Tuple wrapper constructor.
KOKKOS_FORCEINLINE_FUNCTION
ParticleView( soa_type& soa, const int vector_index )
: _soa( soa )
, _vector_index( vector_index )
{
}

// Get the underlying SoA.
KOKKOS_FORCEINLINE_FUNCTION
soa_type& soa() { return _soa; }

KOKKOS_FORCEINLINE_FUNCTION
const soa_type& soa() const { return _soa; }

// Get the vector index of the particle in the SoA.
KOKKOS_FORCEINLINE_FUNCTION
int vectorIndex() const { return _vector_index; }

// The soa the particle is in.
soa_type& _soa;

// The local vector index of the particle.
int _vector_index;
};

//---------------------------------------------------------------------------//
// Particle accessor.
//---------------------------------------------------------------------------//
// Particle accessor
template <class FieldTag, class... FieldTags, class... IndexTypes>
KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
sizeof...( IndexTypes ) == FieldTag::rank,
typename Particle<FieldTags...>::tuple_type::
typename Cabana::Particle<FieldTags...>::tuple_type::
template member_const_reference_type<
TypeIndexer<FieldTag, FieldTags...>::index>>::type
get( const Particle<FieldTags...>& particle, FieldTag, IndexTypes... indices )
get( const Cabana::Particle<FieldTags...>& particle, FieldTag,
IndexTypes... indices )
{
return Cabana::get<TypeIndexer<FieldTag, FieldTags...>::index>(
particle.tuple(), indices... );
Expand All @@ -125,9 +45,10 @@ get( const Particle<FieldTags...>& particle, FieldTag, IndexTypes... indices )
template <class FieldTag, class... FieldTags, class... IndexTypes>
KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
sizeof...( IndexTypes ) == FieldTag::rank,
typename Particle<FieldTags...>::tuple_type::template member_reference_type<
TypeIndexer<FieldTag, FieldTags...>::index>>::type
get( Particle<FieldTags...>& particle, FieldTag, IndexTypes... indices )
typename Cabana::Particle<FieldTags...>::tuple_type::
template member_reference_type<
TypeIndexer<FieldTag, FieldTags...>::index>>::type
get( Cabana::Particle<FieldTags...>& particle, FieldTag, IndexTypes... indices )
{
return Cabana::get<TypeIndexer<FieldTag, FieldTags...>::index>(
particle.tuple(), indices... );
Expand All @@ -139,10 +60,10 @@ template <class FieldTag, class... FieldTags, class... IndexTypes,
int VectorLength>
KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
sizeof...( IndexTypes ) == FieldTag::rank,
typename ParticleView<VectorLength, FieldTags...>::soa_type::
typename Cabana::ParticleView<VectorLength, FieldTags...>::soa_type::
template member_const_reference_type<
TypeIndexer<FieldTag, FieldTags...>::index>>::type
get( const ParticleView<VectorLength, FieldTags...>& particle, FieldTag,
get( const Cabana::ParticleView<VectorLength, FieldTags...>& particle, FieldTag,
IndexTypes... indices )
{
return Cabana::get<TypeIndexer<FieldTag, FieldTags...>::index>(
Expand All @@ -153,10 +74,10 @@ template <class FieldTag, class... FieldTags, class... IndexTypes,
int VectorLength>
KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
sizeof...( IndexTypes ) == FieldTag::rank,
typename ParticleView<VectorLength, FieldTags...>::soa_type::
typename Cabana::ParticleView<VectorLength, FieldTags...>::soa_type::
template member_reference_type<
TypeIndexer<FieldTag, FieldTags...>::index>>::type
get( ParticleView<VectorLength, FieldTags...>& particle, FieldTag,
get( Cabana::ParticleView<VectorLength, FieldTags...>& particle, FieldTag,
IndexTypes... indices )
{
return Cabana::get<TypeIndexer<FieldTag, FieldTags...>::index>(
Expand All @@ -173,7 +94,7 @@ KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
get( ParticleType& particle, FieldTag tag )
{
return typename FieldTag::linear_algebra_type(
&( get( particle, tag, 0 ) ), ParticleType::vector_length );
&( Cabana::get( particle, tag, 0 ) ), ParticleType::vector_length );
}

template <class ParticleType, class FieldTag>
Expand All @@ -184,7 +105,7 @@ get( const ParticleType& particle, FieldTag tag )
{
return typename FieldTag::linear_algebra_type(
const_cast<typename FieldTag::value_type*>(
&( get( particle, tag, 0 ) ) ),
&( Cabana::get( particle, tag, 0 ) ) ),
ParticleType::vector_length );
}

Expand All @@ -198,7 +119,7 @@ KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
get( ParticleType& particle, FieldTag tag )
{
return typename FieldTag::linear_algebra_type(
&( get( particle, tag, 0, 0 ) ),
&( Cabana::get( particle, tag, 0, 0 ) ),
ParticleType::vector_length * FieldTag::dim1,
ParticleType::vector_length );
}
Expand All @@ -211,7 +132,7 @@ get( const ParticleType& particle, FieldTag tag )
{
return typename FieldTag::linear_algebra_type(
const_cast<typename FieldTag::value_type*>(
&( get( particle, tag, 0, 0 ) ) ),
&( Cabana::get( particle, tag, 0, 0 ) ) ),
ParticleType::vector_length * FieldTag::dim1,
ParticleType::vector_length );
}
Expand All @@ -227,7 +148,7 @@ class ParticleList

using memory_space = typename Mesh::memory_space;

using traits = ParticleTraits<FieldTags...>;
using traits = Cabana::ParticleTraits<FieldTags...>;

using aosoa_type =
Cabana::AoSoA<typename traits::member_types, memory_space>;
Expand All @@ -237,10 +158,10 @@ class ParticleList
template <std::size_t M>
using slice_type = typename aosoa_type::template member_slice_type<M>;

using particle_type = Particle<FieldTags...>;
using particle_type = Cabana::Particle<FieldTags...>;

using particle_view_type =
ParticleView<aosoa_type::vector_length, FieldTags...>;
Cabana::ParticleView<aosoa_type::vector_length, FieldTags...>;

// Default constructor.
ParticleList( const std::string& label, const std::shared_ptr<Mesh>& mesh )
Expand Down Expand Up @@ -294,7 +215,7 @@ class ParticleList
template <class Mesh, class... FieldTags>
std::shared_ptr<ParticleList<Mesh, FieldTags...>>
createParticleList( const std::string& label, const std::shared_ptr<Mesh>& mesh,
ParticleTraits<FieldTags...> )
Cabana::ParticleTraits<FieldTags...> )
{
return std::make_shared<ParticleList<Mesh, FieldTags...>>( label, mesh );
}
Expand Down
1 change: 0 additions & 1 deletion unit_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ Picasso_add_tests(NAMES
Picasso_add_tests(MPI NAMES
FacetGeometry
ParticleList
ParticleInit
UniformCartesianMeshMapping
BilinearMeshMapping
UniformMesh
Expand Down
Loading

0 comments on commit b021f61

Please sign in to comment.