Skip to content

Commit 9c98e61

Browse files
committed
Add functions to extract data pointer in specific memory spaces from ArrayView
1 parent 160e94e commit 9c98e61

File tree

8 files changed

+90
-4
lines changed

8 files changed

+90
-4
lines changed

examples/exampleArrayOfArrays.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,14 @@ TEST( ArrayOfArrays, resizeFromCapacities )
279279
// Sphinx start after ChaiBuffer
280280
CUDA_TEST( ArrayOfArrays, ChaiBuffer )
281281
{
282-
LvArray::ArrayOfArrays< int, std::ptrdiff_t, LvArray::MallocBuffer > arrayOfArrays( 10, 9 );
282+
LvArray::ArrayOfArrays< int, std::ptrdiff_t, LvArray::ChaiBuffer > arrayOfArrays( 10, 9 );
283283

284284
{
285285
// Create a view.
286286
LvArray::ArrayOfArraysView< int,
287287
std::ptrdiff_t const,
288288
false,
289-
LvArray::MallocBuffer > const view = arrayOfArrays.toView();
289+
LvArray::ChaiBuffer > const view = arrayOfArrays.toView();
290290

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

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

333333
// Capture the view on device. Since the values were previously touched on host it will copy them over.
334334
// Both the sizes and offsets are current on device so they are not copied over. Nothing is touched.

src/ArrayView.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,14 @@ class ArrayView
532532
T * data() const
533533
{ return m_dataBuffer.data(); }
534534

535+
/**
536+
* @brief @return Return a pointer to the values in a particular memory space.
537+
* @param space The target memory space.
538+
*/
539+
LVARRAY_HOST_DEVICE inline constexpr
540+
T * data( MemorySpace const space ) const
541+
{ return m_dataBuffer.data( space ); }
542+
535543
/**
536544
* @return Return an iterator to the begining of the data.
537545
*/

src/ChaiBuffer.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static std::mutex chaiLock;
4949
* @return The chai::ExecutionSpace corresponding to @p space.
5050
* @param space The MemorySpace to convert.
5151
*/
52+
LVARRAY_HOST_DEVICE
5253
inline chai::ExecutionSpace toChaiExecutionSpace( MemorySpace const space )
5354
{
5455
if( space == MemorySpace::NONE )
@@ -315,6 +316,18 @@ class ChaiBuffer
315316
chai::PointerRecord & pointerRecord() const
316317
{ return *m_pointerRecord; }
317318

319+
/**
320+
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
321+
* @param space The target memory space.
322+
*/
323+
LVARRAY_HOST_DEVICE inline
324+
T * data( MemorySpace const space ) const
325+
{
326+
T * const ptr = static_cast< T * >( m_pointer_record->m_pointers[ internal::toChaiExecutionSpace( space ) ] );
327+
LVARRAY_ERROR_IF( ptr == nullptr, "Buffer not allocated in memory space " << space );
328+
return ptr;
329+
}
330+
318331
/**
319332
* @tparam INDEX_TYPE the type used to index into the values.
320333
* @return The value at position @p i .

src/MallocBuffer.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ class MallocBuffer : public bufferManipulation::VoidBuffer
164164
T * data() const
165165
{ return m_data; }
166166

167+
/**
168+
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
169+
* @param space The target memory space.
170+
*/
171+
LVARRAY_HOST_DEVICE inline
172+
T * data( MemorySpace const space ) const
173+
{
174+
LVARRAY_ERROR_IF_NE_MSG( space, MemorySpace::CPU, "Buffer does not support target memory space" );
175+
return data();
176+
}
177+
167178
/**
168179
* @tparam INDEX_TYPE the type used to index into the values.
169180
* @return The value at position @p i .

src/StackBuffer.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ class StackBuffer : public bufferManipulation::VoidBuffer
110110
T * data() const
111111
{ return const_cast< T * >( m_data ); }
112112

113+
/**
114+
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
115+
* @param space The target memory space.
116+
*/
117+
LVARRAY_HOST_DEVICE inline
118+
T * data( MemorySpace const space ) const
119+
{
120+
LVARRAY_ERROR_IF_NE_MSG( space, MemorySpace::CPU, "Buffer does not support target memory space" );
121+
return data();
122+
}
123+
113124
/**
114125
* @tparam INDEX_TYPE the type used to index into the values.
115126
* @return The value at position @p i .

unitTests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(testSources
1212
testArrayView_copyConstructor.cpp
1313
testArrayView_defaultConstructor.cpp
1414
testArrayView_emptyMove.cpp
15+
testArrayView_getPointerInSpace.cpp
1516
testArrayView_modifyInKernel.cpp
1617
testArrayView_modifyInMultipleKernels.cpp
1718
testArrayView_move.cpp

unitTests/testArrayView.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,18 @@ class ArrayViewPolicyTest : public ArrayViewTest< typename ARRAY_POLICY_PAIR::fi
474474
ParentClass::checkFill( array );
475475
}
476476

477+
static void getPointerInSpace()
478+
{
479+
std::unique_ptr< ARRAY > array = ParentClass::sizedConstructor();
480+
ViewTypeConst const & view = array->toViewConst();
481+
482+
EXPECT_EQ( view.data( MemorySpace::CPU ), view.data() );
483+
view.move( RAJAHelper< POLICY >::space, false );
484+
EXPECT_EQ( view.data( RAJAHelper< POLICY >::space ), view.data() );
485+
view.move( MemorySpace::CPU, false );
486+
EXPECT_EQ( view.data( MemorySpace::CPU ), view.data() );
487+
}
488+
477489
protected:
478490

479491
template< typename ARRAY >
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2020, Lawrence Livermore National Security, LLC and LvArray contributors.
3+
* All rights reserved.
4+
* See the LICENSE file for details.
5+
* SPDX-License-Identifier: (BSD-3-Clause)
6+
*/
7+
8+
// Source includes
9+
#include "testArrayView.hpp"
10+
11+
namespace LvArray
12+
{
13+
namespace testing
14+
{
15+
16+
TYPED_TEST( ArrayViewPolicyTest, getPointerInSpace )
17+
{
18+
this->getPointerInSpace();
19+
}
20+
21+
} // namespace testing
22+
} // namespace LvArray
23+
24+
// This is the default gtest main method. It is included for ease of debugging.
25+
int main( int argc, char * * argv )
26+
{
27+
::testing::InitGoogleTest( &argc, argv );
28+
int const result = RUN_ALL_TESTS();
29+
return result;
30+
}

0 commit comments

Comments
 (0)