Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array layouts #296

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions benchmarks/benchmarkArray1DR2TensorMultiplicationKernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ namespace benchmarking
RAJA_OUTER_LOOP( N, INNER_LOOP( a_ijl, b_ilk, c_ijk ) )


template< typename VALUE_TYPE_CONST, int USD >
template< typename VALUE_TYPE_CONST, typename LAYOUT, std::enable_if_t< LAYOUT::NDIM == 2 >* = nullptr >
inline LVARRAY_HOST_DEVICE constexpr
void R2TensorMultiplyFortran( LvArray::ArraySlice< VALUE_TYPE_CONST, 2, USD, INDEX_TYPE > const a,
LvArray::ArraySlice< VALUE_TYPE_CONST, 2, USD, INDEX_TYPE > const b,
LvArray::ArraySlice< VALUE_TYPE, 2, USD, INDEX_TYPE > const c )
void R2TensorMultiplyFortran( LvArray::ArraySlice< VALUE_TYPE_CONST, LAYOUT > const a,
LvArray::ArraySlice< VALUE_TYPE_CONST, LAYOUT > const b,
LvArray::ArraySlice< VALUE_TYPE, LAYOUT > const c )
{ INNER_LOOP( a( j, l ), b( l, k ), c( j, k ) ) }

template< typename VALUE_TYPE_CONST, int USD >
template< typename VALUE_TYPE_CONST, typename LAYOUT, std::enable_if_t< LAYOUT::NDIM == 2 >* = nullptr >
RAJA_INLINE LVARRAY_HOST_DEVICE constexpr
void R2TensorMultiplySubscript( LvArray::ArraySlice< VALUE_TYPE_CONST, 2, USD, INDEX_TYPE > const a,
LvArray::ArraySlice< VALUE_TYPE_CONST, 2, USD, INDEX_TYPE > const b,
LvArray::ArraySlice< VALUE_TYPE, 2, USD, INDEX_TYPE > const c )
void R2TensorMultiplySubscript( LvArray::ArraySlice< VALUE_TYPE_CONST, LAYOUT > const a,
LvArray::ArraySlice< VALUE_TYPE_CONST, LAYOUT > const b,
LvArray::ArraySlice< VALUE_TYPE, LAYOUT > const c )
{ INNER_LOOP( a[ j ][ l ], b[ l ][ k ], c[ j ][ k ] ) }


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace benchmarking

// Sphinx start after vector
void NaiveNodeToElemMapConstruction::
vector( ArrayView< INDEX_TYPE const, 2, 1, INDEX_TYPE, DEFAULT_BUFFER > const & elementToNodeMap,
vector( ArrayViewT< INDEX_TYPE const, RAJA::PERM_IJ > const & elementToNodeMap,
std::vector< std::vector< INDEX_TYPE > > & nodeToElementMap,
INDEX_TYPE const numNodes )
{
Expand All @@ -33,7 +33,7 @@ void NaiveNodeToElemMapConstruction::

// Sphinx start after naive
void NaiveNodeToElemMapConstruction::
naive( ArrayView< INDEX_TYPE const, 2, 1, INDEX_TYPE, DEFAULT_BUFFER > const & elementToNodeMap,
naive( ArrayViewT< INDEX_TYPE const, RAJA::PERM_IJ > const & elementToNodeMap,
ArrayOfArrays< INDEX_TYPE, INDEX_TYPE, DEFAULT_BUFFER > & nodeToElementMap,
INDEX_TYPE const numNodes )
{
Expand All @@ -52,7 +52,7 @@ void NaiveNodeToElemMapConstruction::
// Sphinx start after overAllocation
template< typename POLICY >
void NodeToElemMapConstruction< POLICY >::
overAllocation( ArrayView< INDEX_TYPE const, 2, 1, INDEX_TYPE, DEFAULT_BUFFER > const & elementToNodeMap,
overAllocation( ArrayViewT< INDEX_TYPE const, RAJA::PERM_IJ > const & elementToNodeMap,
ArrayOfArrays< INDEX_TYPE, INDEX_TYPE, DEFAULT_BUFFER > & nodeToElementMap,
INDEX_TYPE const numNodes,
INDEX_TYPE const maxNodeElements )
Expand Down Expand Up @@ -83,14 +83,14 @@ overAllocation( ArrayView< INDEX_TYPE const, 2, 1, INDEX_TYPE, DEFAULT_BUFFER >
// Sphinx start after resizeFromCapacities
template< typename POLICY >
void NodeToElemMapConstruction< POLICY >::
resizeFromCapacities( ArrayView< INDEX_TYPE const, 2, 1, INDEX_TYPE, DEFAULT_BUFFER > const & elementToNodeMap,
resizeFromCapacities( ArrayViewT< INDEX_TYPE const, RAJA::PERM_IJ > const & elementToNodeMap,
ArrayOfArrays< INDEX_TYPE, INDEX_TYPE, DEFAULT_BUFFER > & nodeToElementMap,
INDEX_TYPE const numNodes )
{
using ATOMIC_POLICY = typename RAJAHelper< POLICY >::AtomicPolicy;

// Create an Array containing the size of each inner array.
Array< INDEX_TYPE, 1, RAJA::PERM_I, INDEX_TYPE, DEFAULT_BUFFER > elementsPerNode( numNodes );
Array< INDEX_TYPE, DynamicExtent< 1, INDEX_TYPE >, RAJA::PERM_I, DEFAULT_BUFFER > elementsPerNode( numNodes );

// Calculate the size of each inner array.
RAJA::forall< POLICY >(
Expand Down
19 changes: 6 additions & 13 deletions benchmarks/benchmarkHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,13 @@ inline std::string typeToString( RAJA::PERM_KJI const & ) { return "RAJA::PERM_K
using INDEX_TYPE = std::ptrdiff_t;

template< typename T, typename PERMUTATION >
using ArrayT = LvArray::Array< T, typeManipulation::getDimension< PERMUTATION >, PERMUTATION, INDEX_TYPE, DEFAULT_BUFFER >;
using ArrayT = LvArray::Array< T, DynamicExtent< typeManipulation::getDimension< PERMUTATION >, INDEX_TYPE >, PERMUTATION, DEFAULT_BUFFER >;

template< typename T, typename PERMUTATION >
using ArrayViewT = LvArray::ArrayView< T,
typeManipulation::getDimension< PERMUTATION >,
typeManipulation::getStrideOneDimension( PERMUTATION {} ),
INDEX_TYPE,
DEFAULT_BUFFER >;
using ArrayViewT = LvArray::ArrayView< T, DynamicLayout< typeManipulation::getDimension< PERMUTATION >, INDEX_TYPE, PERMUTATION >, DEFAULT_BUFFER >;

template< typename T, typename PERMUTATION >
using ArraySliceT = LvArray::ArraySlice< T,
typeManipulation::getDimension< PERMUTATION >,
typeManipulation::getStrideOneDimension( PERMUTATION {} ),
INDEX_TYPE >;
using ArraySliceT = LvArray::ArraySlice< T, DynamicLayout< typeManipulation::getDimension< PERMUTATION >, INDEX_TYPE, PERMUTATION > >;

template< typename T, typename PERMUTATION >
using RajaView = RAJA::View< T,
Expand Down Expand Up @@ -145,8 +138,8 @@ inline std::uint_fast64_t getSeed()
}


template< typename T, int NDIM, int USD >
void initialize( ArraySlice< T, NDIM, USD, INDEX_TYPE > const slice, int & iter )
template< typename T, typename LAYOUT >
void initialize( ArraySlice< T, LAYOUT > const slice, int & iter )
{
++iter;
std::mt19937_64 gen( iter * getSeed() );
Expand All @@ -171,7 +164,7 @@ RajaView< T, PERMUTATION > makeRajaView( ArrayT< T, PERMUTATION > const & array

for( int i = 0; i < NDIM; ++i )
{
sizes[ i ] = array.dims()[ i ];
sizes[ i ] = array.size( i );
}

constexpr std::array< camp::idx_t, NDIM > const permutation = RAJA::as_array< PERMUTATION >::get();
Expand Down
96 changes: 36 additions & 60 deletions examples/exampleArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ TEST( Array, constructors )
{
// Create an empty 2D array of integers.
LvArray::Array< int,
2,
LvArray::DynamicExtent< 2, std::ptrdiff_t >,
camp::idx_seq< 0, 1 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array;
EXPECT_TRUE( array.empty() );
EXPECT_EQ( array.size(), 0 );
Expand All @@ -44,9 +43,8 @@ TEST( Array, constructors )
{
// Create a 3D array of std::string of size 3 x 4 x 5.
LvArray::Array< std::string,
3,
LvArray::DynamicExtent< 3, std::ptrdiff_t >,
camp::idx_seq< 0, 1, 2 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array( 3, 4, 5 );
EXPECT_FALSE( array.empty() );
EXPECT_EQ( array.size(), 3 * 4 * 5 );
Expand All @@ -69,9 +67,8 @@ TEST( Array, accessors )
{
// Create a 2D array of integers.
LvArray::Array< int,
2,
LvArray::DynamicExtent< 2, std::ptrdiff_t >,
camp::idx_seq< 0, 1 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array( 3, 4 );

// Access using operator().
Expand Down Expand Up @@ -100,15 +97,14 @@ TEST( Array, permutations )
{
// Create a 3D array of doubles in the standard layout.
LvArray::Array< int,
3,
LvArray::DynamicExtent< 3, std::ptrdiff_t >,
camp::idx_seq< 0, 1, 2 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array( 3, 4, 5 );

// Index 0 has the largest stride while index 2 has unit stride.
EXPECT_EQ( array.strides()[ 0 ], array.size( 2 ) * array.size( 1 ) );
EXPECT_EQ( array.strides()[ 1 ], array.size( 2 ) );
EXPECT_EQ( array.strides()[ 2 ], 1 );
EXPECT_EQ( array.stride< 0 >(), array.size( 2 ) * array.size( 1 ) );
EXPECT_EQ( array.stride< 1 >(), array.size( 2 ) );
EXPECT_EQ( array.stride< 2 >(), 1 );

int const * const pointer = array.data();
for( std::ptrdiff_t i = 0; i < array.size( 0 ); ++i )
Expand All @@ -128,15 +124,14 @@ TEST( Array, permutations )
{
// Create a 3D array of doubles in a flipped layout.
LvArray::Array< int,
3,
LvArray::DynamicExtent< 3, std::ptrdiff_t >,
camp::idx_seq< 2, 1, 0 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array( 3, 4, 5 );

// Index 0 has the unit stride while index 2 has the largest stride.
EXPECT_EQ( array.strides()[ 0 ], 1 );
EXPECT_EQ( array.strides()[ 1 ], array.size( 0 ) );
EXPECT_EQ( array.strides()[ 2 ], array.size( 0 ) * array.size( 1 ) );
EXPECT_EQ( array.stride< 0 >(), 1 );
EXPECT_EQ( array.stride< 1 >(), array.size( 0 ) );
EXPECT_EQ( array.stride< 2 >(), array.size( 0 ) * array.size( 1 ) );

int const * const pointer = array.data();
for( std::ptrdiff_t i = 0; i < array.size( 0 ); ++i )
Expand All @@ -159,9 +154,8 @@ TEST( Array, permutations )
TEST( Array, resize )
{
LvArray::Array< int,
3,
LvArray::DynamicExtent< 3, std::ptrdiff_t >,
camp::idx_seq< 0, 1, 2 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array;

// Resize using a pointer
Expand Down Expand Up @@ -192,9 +186,8 @@ TEST( Array, resize )
TEST( Array, resizeSingleDimension )
{
LvArray::Array< int,
2,
LvArray::DynamicExtent< 2, std::ptrdiff_t >,
camp::idx_seq< 1, 0 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array( 5, 6 );

for( std::ptrdiff_t i = 0; i < array.size( 0 ); ++i )
Expand Down Expand Up @@ -223,8 +216,7 @@ TEST( Array, resizeSingleDimension )
}

// Shrink the second dimension from 6 to 3;
array.setSingleParameterResizeIndex( 1 );
array.resize( 3 );
array.resizeDimension< 1 >( 3 );
for( std::ptrdiff_t i = 0; i < array.size( 0 ); ++i )
{
for( std::ptrdiff_t j = 0; j < array.size( 1 ); ++j )
Expand All @@ -246,32 +238,25 @@ TEST( Array, resizeSingleDimension )
TEST( Array, arrayView )
{
LvArray::Array< int,
2,
LvArray::DynamicExtent< 2, std::ptrdiff_t >,
camp::idx_seq< 1, 0 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array( 5, 6 );

// Create a view.
LvArray::ArrayView< int,
2,
0,
std::ptrdiff_t,
LvArray::DynamicLayout< 2, std::ptrdiff_t, camp::idx_seq< 1, 0 > >,
LvArray::MallocBuffer > const view = array;
EXPECT_EQ( view.data(), array.data() );

// Create a view with const values.
LvArray::ArrayView< int const,
2,
0,
std::ptrdiff_t,
LvArray::DynamicLayout< 2, std::ptrdiff_t, camp::idx_seq< 1, 0 > >,
LvArray::MallocBuffer > const viewConst = array.toViewConst();
EXPECT_EQ( viewConst.data(), array.data() );

// Copy a view.
LvArray::ArrayView< int,
2,
0,
std::ptrdiff_t,
LvArray::DynamicLayout< 2, std::ptrdiff_t, camp::idx_seq< 1, 0 > >,
LvArray::MallocBuffer > const viewCopy = view;
EXPECT_EQ( viewCopy.data(), array.data() );
}
Expand All @@ -282,17 +267,14 @@ TEST( Array, arraySlice )
{
{
LvArray::Array< int,
2,
LvArray::DynamicExtent< 2, std::ptrdiff_t >,
camp::idx_seq< 0, 1 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array( 5, 6 );

// The unit stride dimension of array is 1 so when we slice off
// the first dimension the unit stride dimension of the slice is 0.
LvArray::ArraySlice< int,
1,
0,
std::ptrdiff_t > const slice = array[ 2 ];
LvArray::DynamicLayout1D< std::ptrdiff_t > > const slice = array[ 2 ];
EXPECT_TRUE( slice.isContiguous() );
EXPECT_EQ( slice.size(), 6 );
EXPECT_EQ( slice.size( 0 ), 6 );
Expand All @@ -301,17 +283,14 @@ TEST( Array, arraySlice )

{
LvArray::Array< int,
3,
LvArray::DynamicExtent< 3, std::ptrdiff_t >,
camp::idx_seq< 2, 1, 0 >,
std::ptrdiff_t,
LvArray::MallocBuffer > array( 3, 5, 6 );

// The unit stride dimension of array is 0 so when we slice off
// the first dimension the unit stride dimension of the slice is -1.
LvArray::ArraySlice< int,
2,
-1,
std::ptrdiff_t > const slice = array[ 2 ];
LvArray::NonContiguousLayout< 2, std::ptrdiff_t > > const slice = array[ 2 ];
EXPECT_FALSE( slice.isContiguous() );
EXPECT_EQ( slice.size(), 5 * 6 );
EXPECT_EQ( slice.size( 0 ), 5 );
Expand All @@ -326,9 +305,8 @@ TEST( Array, arraySlice )
CUDA_TEST( Array, chaiBuffer )
{
LvArray::Array< int,
2,
LvArray::DynamicExtent< 2, std::ptrdiff_t >,
camp::idx_seq< 1, 0 >,
std::ptrdiff_t,
LvArray::ChaiBuffer > array( 5, 6 );

// Move the array to the device.
Expand All @@ -344,9 +322,7 @@ CUDA_TEST( Array, chaiBuffer )
);

LvArray::ArrayView< int,
2,
0,
std::ptrdiff_t,
LvArray::DynamicLayout< 2, std::ptrdiff_t, camp::idx_seq< 1, 0 > >,
LvArray::ChaiBuffer > const & view = array;

// Capture the view in a host kernel which moves the data back to the host.
Expand All @@ -364,9 +340,8 @@ CUDA_TEST( Array, chaiBuffer )
TEST( Array, setName )
{
LvArray::Array< int,
2,
LvArray::DynamicExtent< 2, std::ptrdiff_t >,
camp::idx_seq< 1, 0 >,
std::ptrdiff_t,
LvArray::ChaiBuffer > array( 1024, 1024 );

// Move the array to the device.
Expand All @@ -381,8 +356,8 @@ TEST( Array, setName )
#endif

// Sphinx start after sum int
template< int NDIM, int USD >
int sum( LvArray::ArraySlice< int const, NDIM, USD, std::ptrdiff_t > const slice )
template< typename LAYOUT >
int sum( LvArray::ArraySlice< int const, LAYOUT > const slice )
{
int value = 0;
for( int const val : slice )
Expand All @@ -395,8 +370,8 @@ int sum( LvArray::ArraySlice< int const, NDIM, USD, std::ptrdiff_t > const slice
// Sphinx end before sum int

// Sphinx start after sum double
template< int NDIM, int USD >
double sum( LvArray::ArraySlice< double const, NDIM, USD, std::ptrdiff_t > const slice )
template< typename LAYOUT >
double sum( LvArray::ArraySlice< double const, LAYOUT > const slice )
{
double value = 0;
LvArray::forValuesInSlice( slice, [&value] ( double const val )
Expand All @@ -409,11 +384,12 @@ double sum( LvArray::ArraySlice< double const, NDIM, USD, std::ptrdiff_t > const
// Sphinx end before sum double

// Sphinx start after copy
template< int NDIM, int DST_USD, int SRC_USD >
void copy( LvArray::ArraySlice< int, NDIM, DST_USD, std::ptrdiff_t > const dst,
LvArray::ArraySlice< int const, NDIM, SRC_USD, std::ptrdiff_t > const src )
template< typename SRC_LAYOUT, typename DST_LAYOUT >
void copy( LvArray::ArraySlice< int, DST_LAYOUT > const dst,
LvArray::ArraySlice< int const, SRC_LAYOUT > const src )
{
for( int dim = 0; dim < NDIM; ++dim )
static_assert( DST_LAYOUT::NDIM == SRC_LAYOUT::NDIM, "Layout ranks must match" );
for( int dim = 0; dim < DST_LAYOUT::NDIM; ++dim )
{
LVARRAY_ERROR_IF_NE( dst.size( dim ), src.size( dim ) );
}
Expand All @@ -431,15 +407,15 @@ void copy( LvArray::ArraySlice< int, NDIM, DST_USD, std::ptrdiff_t > const dst,
TEST( Array, boundsCheck )
{
#if defined(ARRAY_USE_BOUNDS_CHECK)
LvArray::Array< int, 3, camp::idx_seq< 0, 1, 2 >, std::ptrdiff_t, LvArray::MallocBuffer > x( 3, 4, 5 );
LvArray::Array< int, LvArray::DynamicExtent< 3, std::ptrdiff_t >, camp::idx_seq< 0, 1, 2 >, LvArray::MallocBuffer > x( 3, 4, 5 );

// Out of bounds access aborts the program.
EXPECT_DEATH_IF_SUPPORTED( x( 2, 3, 4 ), "" );
EXPECT_DEATH_IF_SUPPORTED( x( -1, 4, 6 ), "" );
EXPECT_DEATH_IF_SUPPORTED( x[ 0 ][ 10 ][ 2 ], "" );

// Out of bounds emplace
LvArray::Array< int, 1, camp::idx_seq< 0 >, std::ptrdiff_t, LvArray::MallocBuffer > x( 10 );
LvArray::Array< int, LvArray::DynamicExtent< 1, std::ptrdiff_t >, camp::idx_seq< 0 >, LvArray::MallocBuffer > x( 10 );
EXPECT_DEATH_IF_SUPPORTED( x.emplace( -1, 5 ) );
#endif
}
Expand Down
Loading