Skip to content

Commit

Permalink
Simplify LinkedCellList discriminator with SelfNeighborTag option
Browse files Browse the repository at this point in the history
  • Loading branch information
streeve committed Oct 14, 2024
1 parent 26a281c commit a762117
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
32 changes: 30 additions & 2 deletions core/src/Cabana_NeighborList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Cabana
// Neighbor List Interface
//---------------------------------------------------------------------------//
/*!
\brief Tag for full neighbor lists.
\brief Tag for building full neighbor lists.
In this case every particle has its neighbors stored in the list. So, if
particle "i" neighbors particle "j" then "j" will be in the neighbor list
Expand All @@ -38,7 +38,7 @@ class FullNeighborTag

//---------------------------------------------------------------------------//
/*!
\brief Tag for half neighbor lists.
\brief Tag for building half neighbor lists.
In this case only half of the neighbors are stored and the inverse
relationship is implied. So, if particle "i" neighbors particle "j" then "j"
Expand All @@ -49,11 +49,39 @@ class HalfNeighborTag
{
};

//---------------------------------------------------------------------------//
/*!
\brief Tag for neighbor list iteration, ignoring only self neighbors.
*/
class SelfNeighborTag
{
};

//---------------------------------------------------------------------------//
//! Neighborhood discriminator.
template <class Tag>
class NeighborDiscriminator;

//! Self neighbor discriminator specialization.
//! \note This is not sufficient for building both full and half neighbor lists
//! and is intended for neighbor iteration.
template <>
class NeighborDiscriminator<SelfNeighborTag>
{
public:
/*!
\brief Check whether neighbor pair is valid.
This check only considers self neighbors (i.e. the particle index "p" is
not the same as the neighbor index "n").
*/
KOKKOS_INLINE_FUNCTION
static bool isValid( const std::size_t p, const std::size_t n )
{
return ( p != n );
}
};

//! Full list discriminator specialization.
template <>
class NeighborDiscriminator<FullNeighborTag>
Expand Down
17 changes: 6 additions & 11 deletions core/src/Cabana_Parallel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,13 +1134,12 @@ struct LinkedCellParallelFor
LinkedCellType _list;
//! Spatial dimension
static constexpr std::size_t num_space_dim = LinkedCellType::num_space_dim;
Kokkos::Array<double, num_space_dim> empty;

//! beginning index of the loop
index_type _begin;

//! discriminator for whether a particle is a neighbor or not
NeighborDiscriminator<FullNeighborTag> _discriminator;
NeighborDiscriminator<SelfNeighborTag> _discriminator;

//! Constructor
LinkedCellParallelFor( std::string label, Policy exec_policy,
Expand All @@ -1151,9 +1150,6 @@ struct LinkedCellParallelFor
, _list( list )
, _begin( begin )
{
for ( int d = 0; d < num_space_dim; ++d )
empty[d] = 0.0;

if ( label.empty() )
Kokkos::parallel_for( dynamic_cast<const Policy&>( exec_policy ),
*this );
Expand Down Expand Up @@ -1217,7 +1213,7 @@ struct LinkedCellParallelFor
auto j = _list.getParticle( n );

// Avoid self interactions (dummy position args).
if ( _discriminator.isValid( i, empty, j, empty ) )
if ( _discriminator.isValid( i, j ) )
{
Impl::functorTagDispatch<WorkTag>( _functor, i, j );
}
Expand Down Expand Up @@ -1285,7 +1281,7 @@ struct LinkedCellParallelFor
auto j = _list.getParticle( n );

// Avoid self interactions (dummy position args).
if ( _discriminator.isValid( i, empty, j, empty ) )
if ( _discriminator.isValid( i, j ) )
{
Impl::functorTagDispatch<WorkTag>( _functor, i, j );
}
Expand All @@ -1312,13 +1308,12 @@ struct LinkedCellParallelReduce
LinkedCellType _list;
//! Spatial dimension
static constexpr std::size_t num_space_dim = LinkedCellType::num_space_dim;
Kokkos::Array<double, num_space_dim> empty;

//! beginning index of the loop
index_type _begin;

//! discriminator for whether a particle is a neighbor or not
NeighborDiscriminator<FullNeighborTag> _discriminator;
NeighborDiscriminator<SelfNeighborTag> _discriminator;

//! Constructor
LinkedCellParallelReduce( std::string label, Policy exec_policy,
Expand Down Expand Up @@ -1395,7 +1390,7 @@ struct LinkedCellParallelReduce
auto j = _list.getParticle( n );

// Avoid self interactions (dummy position args).
if ( _discriminator.isValid( i, empty, j, empty ) )
if ( _discriminator.isValid( i, j ) )
{
Impl::functorTagDispatch<WorkTag>( _functor, i, j, ival );
}
Expand Down Expand Up @@ -1464,7 +1459,7 @@ struct LinkedCellParallelReduce
auto j = _list.getParticle( n );

// Avoid self interactions (dummy position args).
if ( _discriminator.isValid( i, empty, j, empty ) )
if ( _discriminator.isValid( i, j ) )
{
Impl::functorTagDispatch<WorkTag>( _functor, i, j, ival );
}
Expand Down
16 changes: 7 additions & 9 deletions core/unit_test/tstLinkedCellList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ void checkLinkedCellNeighborInterface( const ListType& nlist,
Kokkos::View<std::size_t*, Kokkos::HostSpace> N2_copy_neighbors(
"num_n2_neighbors", positions.size() );

Cabana::NeighborDiscriminator<Cabana::FullNeighborTag> _discriminator;
Cabana::NeighborDiscriminator<Cabana::SelfNeighborTag> _discriminator;

std::size_t max_n2_neighbors = 0;
std::size_t sum_n2_neighbors = 0;
Expand Down Expand Up @@ -723,8 +723,7 @@ void checkLinkedCellNeighborInterface( const ListType& nlist,
const double dx = positions( pid, d ) - positions( np, d );
r2 += dx * dx;
}
if ( r2 <= c2 &&
_discriminator.isValid( pid, 0, 0, 0, np, 0, 0, 0 ) )
if ( r2 <= c2 && _discriminator.isValid( pid, np ) )
{
if ( nlist.sorted() )
Kokkos::atomic_add(
Expand Down Expand Up @@ -782,8 +781,7 @@ void checkLinkedCellNeighborParallel( const ListType& nlist,
// to the particle (within cutoff) and compare to counts.
auto c2 = cutoff * cutoff;

Cabana::NeighborDiscriminator<Cabana::FullNeighborTag> _discriminator;
Kokkos::Array<double, Dim> empty;
Cabana::NeighborDiscriminator<Cabana::SelfNeighborTag> _discriminator;

auto serial_count_op = KOKKOS_LAMBDA( const int i, const int j )
{
Expand All @@ -793,7 +791,7 @@ void checkLinkedCellNeighborParallel( const ListType& nlist,
const double dx = positions( i, d ) - positions( j, d );
r2 += dx * dx;
}
if ( r2 <= c2 && _discriminator.isValid( i, empty, j, empty ) )
if ( r2 <= c2 && _discriminator.isValid( i, j ) )
{
if ( nlist.sorted() )
{
Expand All @@ -815,7 +813,7 @@ void checkLinkedCellNeighborParallel( const ListType& nlist,
const double dx = positions( i, d ) - positions( j, d );
r2 += dx * dx;
}
if ( r2 <= c2 && _discriminator.isValid( i, 0, 0, 0, j, 0, 0, 0 ) )
if ( r2 <= c2 && _discriminator.isValid( i, j ) )
{
if ( nlist.sorted() )
{
Expand Down Expand Up @@ -858,7 +856,7 @@ void checkLinkedCellNeighborReduce( const ListType& nlist,
// to the particle (within cutoff) and compare to counts.
auto c2 = cutoff * cutoff;

Cabana::NeighborDiscriminator<Cabana::FullNeighborTag> _discriminator;
Cabana::NeighborDiscriminator<Cabana::SelfNeighborTag> _discriminator;

auto sum_op = KOKKOS_LAMBDA( const int i, const int j, double& sum )
{
Expand All @@ -868,7 +866,7 @@ void checkLinkedCellNeighborReduce( const ListType& nlist,
const double dx = position( i, d ) - position( j, d );
r2 += dx * dx;
}
if ( r2 <= c2 && _discriminator.isValid( i, 0, 0, 0, j, 0, 0, 0 ) )
if ( r2 <= c2 && _discriminator.isValid( i, j ) )
{
if ( nlist.sorted() )
{
Expand Down

0 comments on commit a762117

Please sign in to comment.