Skip to content

Commit

Permalink
Add functions to extract data pointer in specific memory spaces from …
Browse files Browse the repository at this point in the history
…ArrayView
  • Loading branch information
klevzoff committed Aug 20, 2020
1 parent 2bf471c commit deb79fb
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 4 deletions.
8 changes: 4 additions & 4 deletions examples/exampleArrayOfArrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ TEST( ArrayOfArrays, resizeFromCapacities )
// Sphinx start after ChaiBuffer
CUDA_TEST( ArrayOfArrays, ChaiBuffer )
{
LvArray::ArrayOfArrays< int, std::ptrdiff_t, LvArray::MallocBuffer > arrayOfArrays( 10, 9 );
LvArray::ArrayOfArrays< int, std::ptrdiff_t, LvArray::ChaiBuffer > arrayOfArrays( 10, 9 );

{
// Create a view.
LvArray::ArrayOfArraysView< int,
std::ptrdiff_t const,
false,
LvArray::MallocBuffer > const view = arrayOfArrays.toView();
LvArray::ChaiBuffer > const view = arrayOfArrays.toView();

// Capture the view on device. This will copy the values, sizes and offsets.
// The values and sizes will be touched.
Expand All @@ -307,7 +307,7 @@ CUDA_TEST( ArrayOfArrays, ChaiBuffer )
LvArray::ArrayOfArraysView< int,
std::ptrdiff_t const,
true,
LvArray::MallocBuffer > const viewConstSizes = arrayOfArrays.toViewConstSizes();
LvArray::ChaiBuffer > const viewConstSizes = arrayOfArrays.toViewConstSizes();

// Capture the view on the host. This will copy back the values and sizes since they were previously touched
// on device. It will only touch the values on host.
Expand All @@ -328,7 +328,7 @@ CUDA_TEST( ArrayOfArrays, ChaiBuffer )
LvArray::ArrayOfArraysView< int const,
std::ptrdiff_t const,
true,
LvArray::MallocBuffer > const viewConst = arrayOfArrays.toViewConst();
LvArray::ChaiBuffer > const viewConst = arrayOfArrays.toViewConst();

// Capture the view on device. Since the values were previously touched on host it will copy them over.
// Both the sizes and offsets are current on device so they are not copied over. Nothing is touched.
Expand Down
8 changes: 8 additions & 0 deletions src/ArrayView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ class ArrayView
T * data() const
{ return m_dataBuffer.data(); }

/**
* @brief @return Return a pointer to the values in a particular memory space.
* @param space The target memory space.
*/
LVARRAY_HOST_DEVICE inline constexpr
T * data( MemorySpace const space ) const
{ return m_dataBuffer.data( space ); }

/**
* @return Return an iterator to the begining of the data.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/ChaiBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static std::mutex chaiLock;
* @return The chai::ExecutionSpace corresponding to @p space.
* @param space The MemorySpace to convert.
*/
LVARRAY_HOST_DEVICE
inline chai::ExecutionSpace toChaiExecutionSpace( MemorySpace const space )
{
if( space == MemorySpace::NONE )
Expand Down Expand Up @@ -294,6 +295,18 @@ class ChaiBuffer
T * data() const
{ return m_pointer; }

/**
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
* @param space The target memory space.
*/
LVARRAY_HOST_DEVICE inline
T * data( MemorySpace const space ) const
{
T * const ptr = static_cast< T * >( m_pointer_record->m_pointers[ internal::toChaiExecutionSpace( space ) ] );
LVARRAY_ERROR_IF( ptr == nullptr, "Buffer not allocated in memory space " << space );
return ptr;
}

/**
* @tparam INDEX_TYPE the type used to index into the values.
* @return The value at position @p i .
Expand Down
11 changes: 11 additions & 0 deletions src/MallocBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ class MallocBuffer : public bufferManipulation::VoidBuffer
T * data() const
{ return m_data; }

/**
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
* @param space The target memory space.
*/
LVARRAY_HOST_DEVICE inline
T * data( MemorySpace const space ) const
{
LVARRAY_ERROR_IF_NE_MSG( space, MemorySpace::CPU, "Buffer does not support target memory space" );
return data();
}

/**
* @tparam INDEX_TYPE the type used to index into the values.
* @return The value at position @p i .
Expand Down
11 changes: 11 additions & 0 deletions src/StackBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class StackBuffer : public bufferManipulation::VoidBuffer
T * data() const
{ return const_cast< T * >( m_data ); }

/**
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
* @param space The target memory space.
*/
LVARRAY_HOST_DEVICE inline
T * data( MemorySpace const space ) const
{
LVARRAY_ERROR_IF_NE_MSG( space, MemorySpace::CPU, "Buffer does not support target memory space" );
return data();
}

/**
* @tparam INDEX_TYPE the type used to index into the values.
* @return The value at position @p i .
Expand Down
1 change: 1 addition & 0 deletions unitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(testSources
testArrayView_copyConstructor.cpp
testArrayView_defaultConstructor.cpp
testArrayView_emptyMove.cpp
testArrayView_getPointerInSpace.cpp
testArrayView_modifyInKernel.cpp
testArrayView_modifyInMultipleKernels.cpp
testArrayView_move.cpp
Expand Down
12 changes: 12 additions & 0 deletions unitTests/testArrayView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@ class ArrayViewPolicyTest : public ArrayViewTest< typename ARRAY_POLICY_PAIR::fi
ParentClass::checkFill( array );
}

static void getPointerInSpace()
{
std::unique_ptr< ARRAY > array = ParentClass::sizedConstructor();
ViewTypeConst const & view = array->toViewConst();

EXPECT_EQ( view.data( MemorySpace::CPU ), view.data() );
view.move( RAJAHelper< POLICY >::space, false );
EXPECT_EQ( view.data( RAJAHelper< POLICY >::space ), view.data() );
view.move( MemorySpace::CPU, false );
EXPECT_EQ( view.data( MemorySpace::CPU ), view.data() );
}

protected:

template< typename ARRAY >
Expand Down
30 changes: 30 additions & 0 deletions unitTests/testArrayView_getPointerInSpace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2020, Lawrence Livermore National Security, LLC and LvArray contributors.
* All rights reserved.
* See the LICENSE file for details.
* SPDX-License-Identifier: (BSD-3-Clause)
*/

// Source includes
#include "testArrayView.hpp"

namespace LvArray
{
namespace testing
{

TYPED_TEST( ArrayViewPolicyTest, getPointerInSpace )
{
this->getPointerInSpace();
}

} // namespace testing
} // namespace LvArray

// This is the default gtest main method. It is included for ease of debugging.
int main( int argc, char * * argv )
{
::testing::InitGoogleTest( &argc, argv );
int const result = RUN_ALL_TESTS();
return result;
}

0 comments on commit deb79fb

Please sign in to comment.