Skip to content

Commit

Permalink
Merge pull request #654 from streeve/particle_list_mirror
Browse files Browse the repository at this point in the history
Add ParticleList mirror and deep copy
  • Loading branch information
streeve authored Aug 1, 2023
2 parents 438779a + 1132a06 commit 8435dbd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
74 changes: 67 additions & 7 deletions core/src/Cabana_DeepCopy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define CABANA_DEEPCOPY_HPP

#include <Cabana_AoSoA.hpp>
#include <Cabana_ParticleList.hpp>
#include <Cabana_Slice.hpp>
#include <impl/Cabana_TypeTraits.hpp>

Expand Down Expand Up @@ -104,10 +105,9 @@ inline SrcAoSoA create_mirror_view_and_copy(
const Space&, const SrcAoSoA& src,
typename std::enable_if<
( std::is_same<typename SrcAoSoA::memory_space,
typename Space::memory_space>::value )>::type* = 0 )
typename Space::memory_space>::value &&
is_aosoa<SrcAoSoA>::value )>::type* = 0 )
{
static_assert( is_aosoa<SrcAoSoA>::value,
"create_mirror_view_and_copy() requires an AoSoA" );
return src;
}

Expand All @@ -127,11 +127,9 @@ create_mirror_view_and_copy(
const Space& space, const SrcAoSoA& src,
typename std::enable_if<
( !std::is_same<typename SrcAoSoA::memory_space,
typename Space::memory_space>::value )>::type* = 0 )
typename Space::memory_space>::value &&
is_aosoa<SrcAoSoA>::value )>::type* = 0 )
{
static_assert( is_aosoa<SrcAoSoA>::value,
"create_mirror_view_and_copy() requires an AoSoA" );

auto dst = create_mirror( space, src );

Kokkos::deep_copy(
Expand Down Expand Up @@ -232,6 +230,25 @@ deep_copy( DstAoSoA& dst, const SrcAoSoA& src,
}
}

//---------------------------------------------------------------------------//
/*!
\brief Deep copy data between compatible ParticleList objects.
\param dst The destination for the copied data.
\param src The source of the copied data.
s*/
template <class DstMemorySpace, class SrcMemorySpace, class... FieldTags>
inline void deep_copy( ParticleList<DstMemorySpace, FieldTags...>& dst,
const ParticleList<SrcMemorySpace, FieldTags...>& src )
{
// Copy particle data to new memory space.
auto aosoa_src = src.aosoa();
auto& aosoa_dst = dst.aosoa();

// Set the new data.
Cabana::deep_copy( aosoa_dst, aosoa_src );
}

//---------------------------------------------------------------------------//
/*!
\brief Fill an AoSoA with a tuple.
Expand Down Expand Up @@ -390,6 +407,49 @@ inline void deep_copy( Slice_t& slice,
}

//---------------------------------------------------------------------------//
/*!
\brief Create a mirror of the given ParticleList in the given memory space.
\note Memory allocation will only occur if the requested mirror
memory space is different from that of the input AoSoA. If they are the
same, the original ParticleList is returned.
*/
template <class DstMemorySpace, class SrcMemorySpace, class... FieldTags>
auto create_mirror_view_and_copy(
DstMemorySpace, ParticleList<SrcMemorySpace, FieldTags...> plist_src,
typename std::enable_if<
std::is_same<SrcMemorySpace, DstMemorySpace>::value>::type* = 0 )
{
return plist_src;
}

/*!
\brief Create a mirror of the given ParticleList in the given memory space.
\note Memory allocation will only occur if the requested mirror
memory space is different from that of the input AoSoA. If they are the
same, the original ParticleList is returned.
*/
template <class DstMemorySpace, class SrcMemorySpace, class... FieldTags>
auto create_mirror_view_and_copy(
DstMemorySpace, ParticleList<SrcMemorySpace, FieldTags...> plist_src,
typename std::enable_if<
!std::is_same<SrcMemorySpace, DstMemorySpace>::value>::type* = 0 )
{
// Extract the original AoSoA.
auto aosoa_src = plist_src.aosoa();

// Create an AoSoA in the new memory space.
using src_plist_type = ParticleList<SrcMemorySpace, FieldTags...>;
using member_types = typename src_plist_type::member_types;
AoSoA<member_types, DstMemorySpace> aosoa_dst( aosoa_src.label() );

// Copy data to new AoAoA.
deep_copy( aosoa_dst, aosoa_src );

// Create new list with the copied data.
return ParticleList<DstMemorySpace, FieldTags...>( aosoa_dst );
}

} // end namespace Cabana

Expand Down
14 changes: 11 additions & 3 deletions core/src/Cabana_ParticleList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ class ParticleList
public:
//! Kokkos memory space.
using memory_space = MemorySpace;
//! Particle AoSoA member types.
//! AoSoA member field types.
using traits = ParticleTraits<FieldTags...>;
//! AoSoA member types.
using member_types = typename traits::member_types;
//! AoSoA type.
using aosoa_type =
Cabana::AoSoA<typename traits::member_types, memory_space>;
using aosoa_type = Cabana::AoSoA<member_types, memory_space>;
//! Particle tuple type.
using tuple_type = typename aosoa_type::tuple_type;
/*!
Expand All @@ -205,6 +206,13 @@ class ParticleList
{
}

//! Constructor from existing AoSoA.
template <class AoSoAType>
ParticleList( const AoSoAType aosoa )
: _aosoa( aosoa )
{
}

//! Get the number of particles in the list.
std::size_t size() const { return _aosoa.size(); }

Expand Down

0 comments on commit 8435dbd

Please sign in to comment.