Skip to content

Commit

Permalink
Improves index handling between permutation vector and lcl with speci…
Browse files Browse the repository at this point in the history
…fied begin. Also fixes logic of checking against the correct partial list in unit tests
  • Loading branch information
lebuller committed Mar 27, 2024
1 parent 3ba783a commit ce06eca
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
13 changes: 7 additions & 6 deletions core/src/Cabana_Parallel.hpp
Expand Up @@ -1195,7 +1195,7 @@ struct LinkedCellParallelFor
if ( !_list.sorted() )
jj = _list.permutation( j );
else
jj = j;
jj = j + _begin;
// Avoid self interactions (dummy position args).
if ( _discriminator.isValid( i, 0, 0, 0, jj, 0, 0, 0 ) )
{
Expand Down Expand Up @@ -1233,7 +1233,7 @@ struct LinkedCellParallelFor
if ( !_list.sorted() )
jj = _list.permutation( j );
else
jj = j;
jj = j + _begin;
// Avoid self interactions (dummy position args).
if ( _discriminator.isValid( i, 0, 0, 0, jj, 0, 0,
0 ) )
Expand Down Expand Up @@ -1313,7 +1313,7 @@ struct LinkedCellParallelReduce
if ( !_list.sorted() )
jj = _list.permutation( j );
else
jj = j;
jj = j + _begin;
// Avoid self interactions (dummy position args).
if ( _discriminator.isValid( i, 0, 0, 0, jj, 0, 0, 0 ) )
{
Expand Down Expand Up @@ -1352,7 +1352,7 @@ struct LinkedCellParallelReduce
if ( !_list.sorted() )
jj = _list.permutation( j );
else
jj = j;
jj = j + _begin;
// Avoid self interactions (dummy position args).
if ( _discriminator.isValid( i, 0, 0, 0, jj, 0, 0,
0 ) )
Expand Down Expand Up @@ -1435,7 +1435,7 @@ inline void neighbor_parallel_for(
Impl::LinkedCellParallelFor<work_tag, FunctorType, linear_policy_type,
LinkedCellType,
typename LinkedCellType::CountView>
lcl_par( str, linear_exec_policy, functor, list );
lcl_par( str, linear_exec_policy, functor, list, exec_policy.begin() );
}

//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -1562,7 +1562,8 @@ inline void neighbor_parallel_reduce(
Impl::LinkedCellParallelReduce<
work_tag, FunctorType, linear_policy_type, LinkedCellType,
typename LinkedCellType::CountView, ReduceType>
lcl_par( str, linear_exec_policy, functor, list, reduce_val );
lcl_par( str, linear_exec_policy, functor, list, reduce_val,
exec_policy.begin() );
}

//---------------------------------------------------------------------------//
Expand Down
15 changes: 11 additions & 4 deletions core/unit_test/neighbor_unit_test.hpp
Expand Up @@ -298,9 +298,13 @@ void checkFirstNeighborParallelFor( const TestListType& N2_list_copy,
num_particle );

// Use a full N^2 neighbor list to check against.
for ( std::size_t p = 0; p < num_particle; ++p )
for ( std::size_t p = begin; p < end; ++p )
for ( int n = 0; n < N2_list_copy.counts( p ); ++n )
N2_result( p ) += N2_list_copy.neighbors( p, n );
{
if ( N2_list_copy.neighbors( p, n ) >= begin &&
N2_list_copy.neighbors( p, n ) < end )
N2_result( p ) += N2_list_copy.neighbors( p, n );
}

// Check the result.
auto serial_mirror = Kokkos::create_mirror_view_and_copy(
Expand Down Expand Up @@ -369,8 +373,11 @@ void checkFirstNeighborParallelReduce(
for ( int n = 0; n < N2_list_copy.counts( p ); ++n )
if ( p >= begin && p < end )
{
N2_sum += positions_mirror( p, 0 ) +
positions_mirror( N2_list_copy.neighbors( p, n ), 0 );
if ( N2_list_copy.neighbors( p, n ) >= begin &&
N2_list_copy.neighbors( p, n ) < end )
N2_sum +=
positions_mirror( p, 0 ) +
positions_mirror( N2_list_copy.neighbors( p, n ), 0 );
}

// Check the result.
Expand Down
53 changes: 42 additions & 11 deletions core/unit_test/tstLinkedCellList.hpp
Expand Up @@ -420,12 +420,32 @@ void checkLinkedCellNeighborInterface( const ListType& nlist,
// Purposely using zero-init.
Kokkos::View<std::size_t*, memory_space> num_n2_neighbors(
"num_n2_neighbors", positions.size() );
Kokkos::View<std::size_t*> N2_copy_neighbors( "num_n2_neighbors",
positions.size() );

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

std::size_t max_n2_neighbors = 0;
std::size_t sum_n2_neighbors = 0;

std::size_t N2_copy_max = 0;
std::size_t N2_copy_sum = 0;

for ( int p = begin; p < end; ++p )
{
for ( int n = 0; n < N2_list_copy.counts( p ); ++n )
{
if ( N2_list_copy.neighbors( p, n ) >= begin &&
N2_list_copy.neighbors( p, n ) < end )
{
N2_copy_neighbors( p ) += 1;
}
}
if ( N2_copy_neighbors( p ) > N2_copy_max )
N2_copy_max = N2_copy_neighbors( p );
N2_copy_sum += N2_copy_neighbors( p );
}

double c2 = cutoff * cutoff;

Kokkos::RangePolicy<TEST_EXECSPACE> policy( begin, end );
Expand All @@ -440,16 +460,22 @@ void checkLinkedCellNeighborInterface( const ListType& nlist,
int np = Cabana::NeighborList<ListType>::getNeighbor( nlist,
pid, i );

if ( nlist.sorted() )
np += begin;

const double dx = positions( pid, 0 ) - positions( np, 0 );
const double dy = positions( pid, 1 ) - positions( np, 1 );
const double dz = positions( pid, 2 ) - positions( np, 2 );
const double r2 = dx * dx + dy * dy + dz * dz;

if ( r2 <= c2 &&
_discriminator.isValid( pid, 0, 0, 0, np, 0, 0, 0 ) )
{
if ( nlist.sorted() )
Kokkos::atomic_add(
&num_n2_neighbors( nlist.permutation( pid ) ), 1 );
&num_n2_neighbors(
nlist.permutation( pid - begin ) ),
1 );
else
Kokkos::atomic_add( &num_n2_neighbors( pid ), 1 );
}
Expand All @@ -462,18 +488,21 @@ void checkLinkedCellNeighborInterface( const ListType& nlist,
for ( std::size_t pid = 0; pid < positions.size(); ++pid )
{
if ( pid >= begin && pid < end )
EXPECT_EQ( num_n2_neighbors_host( pid ),
N2_list_copy.counts( pid ) );
{
EXPECT_EQ( num_n2_neighbors_host( pid ), N2_copy_neighbors( pid ) );
}
else
{
EXPECT_EQ( num_n2_neighbors_host( pid ), 0 );
}

sum_n2_neighbors += num_n2_neighbors_host( pid );
if ( num_n2_neighbors_host( pid ) > max_n2_neighbors )
max_n2_neighbors = num_n2_neighbors_host( pid );
}

EXPECT_EQ( max_n2_neighbors, N2_list_copy.max );
EXPECT_EQ( sum_n2_neighbors, N2_list_copy.total );
EXPECT_EQ( max_n2_neighbors, N2_copy_max );
EXPECT_EQ( sum_n2_neighbors, N2_copy_sum );
}

//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -509,8 +538,9 @@ void checkLinkedCellNeighborParallel( const ListType& nlist,
{
if ( nlist.sorted() )
{
Kokkos::atomic_add( &serial_result( nlist.permutation( i ) ),
nlist.permutation( j ) );
Kokkos::atomic_add(
&serial_result( nlist.permutation( i - begin ) ),
nlist.permutation( j - begin ) );
}
else
{
Expand All @@ -528,8 +558,9 @@ void checkLinkedCellNeighborParallel( const ListType& nlist,
{
if ( nlist.sorted() )
{
Kokkos::atomic_add( &team_result( nlist.permutation( i ) ),
nlist.permutation( j ) );
Kokkos::atomic_add(
&team_result( nlist.permutation( i - begin ) ),
nlist.permutation( j - begin ) );
}
else
{
Expand Down Expand Up @@ -577,8 +608,8 @@ void checkLinkedCellNeighborReduce( const ListType& nlist,
{
if ( nlist.sorted() )
{
sum += position( nlist.permutation( i ), 0 ) +
position( nlist.permutation( j ), 0 );
sum += position( nlist.permutation( i - begin ), 0 ) +
position( nlist.permutation( j - begin ), 0 );
}
else
{
Expand Down

0 comments on commit ce06eca

Please sign in to comment.