diff --git a/examples/exampleArrayOfArrays.cpp b/examples/exampleArrayOfArrays.cpp index 123fe962..cababa40 100644 --- a/examples/exampleArrayOfArrays.cpp +++ b/examples/exampleArrayOfArrays.cpp @@ -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. @@ -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. @@ -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. diff --git a/src/ArrayView.hpp b/src/ArrayView.hpp index 3fbd14d2..b2d66baf 100644 --- a/src/ArrayView.hpp +++ b/src/ArrayView.hpp @@ -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. */ diff --git a/src/ChaiBuffer.hpp b/src/ChaiBuffer.hpp index ac11c875..5a7f5cb1 100644 --- a/src/ChaiBuffer.hpp +++ b/src/ChaiBuffer.hpp @@ -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 ) @@ -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 . diff --git a/src/MallocBuffer.hpp b/src/MallocBuffer.hpp index 8617c1b6..cc924cef 100644 --- a/src/MallocBuffer.hpp +++ b/src/MallocBuffer.hpp @@ -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 . diff --git a/src/StackBuffer.hpp b/src/StackBuffer.hpp index f918bf32..4c01f1ea 100644 --- a/src/StackBuffer.hpp +++ b/src/StackBuffer.hpp @@ -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 . diff --git a/unitTests/CMakeLists.txt b/unitTests/CMakeLists.txt index 3dbf2d77..5572a358 100644 --- a/unitTests/CMakeLists.txt +++ b/unitTests/CMakeLists.txt @@ -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 diff --git a/unitTests/testArrayView.hpp b/unitTests/testArrayView.hpp index 690cef51..695276ac 100644 --- a/unitTests/testArrayView.hpp +++ b/unitTests/testArrayView.hpp @@ -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 > diff --git a/unitTests/testArrayView_getPointerInSpace.cpp b/unitTests/testArrayView_getPointerInSpace.cpp new file mode 100644 index 00000000..7bfdfffe --- /dev/null +++ b/unitTests/testArrayView_getPointerInSpace.cpp @@ -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; +}