Skip to content

Commit deb79fb

Browse files
committed
Add functions to extract data pointer in specific memory spaces from ArrayView
1 parent 2bf471c commit deb79fb

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
@@ -427,6 +427,14 @@ class ArrayView
427427
T * data() const
428428
{ return m_dataBuffer.data(); }
429429

430+
/**
431+
* @brief @return Return a pointer to the values in a particular memory space.
432+
* @param space The target memory space.
433+
*/
434+
LVARRAY_HOST_DEVICE inline constexpr
435+
T * data( MemorySpace const space ) const
436+
{ return m_dataBuffer.data( space ); }
437+
430438
/**
431439
* @return Return an iterator to the begining of the data.
432440
*/

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 )
@@ -294,6 +295,18 @@ class ChaiBuffer
294295
T * data() const
295296
{ return m_pointer; }
296297

298+
/**
299+
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
300+
* @param space The target memory space.
301+
*/
302+
LVARRAY_HOST_DEVICE inline
303+
T * data( MemorySpace const space ) const
304+
{
305+
T * const ptr = static_cast< T * >( m_pointer_record->m_pointers[ internal::toChaiExecutionSpace( space ) ] );
306+
LVARRAY_ERROR_IF( ptr == nullptr, "Buffer not allocated in memory space " << space );
307+
return ptr;
308+
}
309+
297310
/**
298311
* @tparam INDEX_TYPE the type used to index into the values.
299312
* @return The value at position @p i .

src/MallocBuffer.hpp

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

155+
/**
156+
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
157+
* @param space The target memory space.
158+
*/
159+
LVARRAY_HOST_DEVICE inline
160+
T * data( MemorySpace const space ) const
161+
{
162+
LVARRAY_ERROR_IF_NE_MSG( space, MemorySpace::CPU, "Buffer does not support target memory space" );
163+
return data();
164+
}
165+
155166
/**
156167
* @tparam INDEX_TYPE the type used to index into the values.
157168
* @return The value at position @p i .

src/StackBuffer.hpp

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

102+
/**
103+
* @brief @return Return a pointer to the beginning of the buffer in a particular memory space.
104+
* @param space The target memory space.
105+
*/
106+
LVARRAY_HOST_DEVICE inline
107+
T * data( MemorySpace const space ) const
108+
{
109+
LVARRAY_ERROR_IF_NE_MSG( space, MemorySpace::CPU, "Buffer does not support target memory space" );
110+
return data();
111+
}
112+
102113
/**
103114
* @tparam INDEX_TYPE the type used to index into the values.
104115
* @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
@@ -471,6 +471,18 @@ class ArrayViewPolicyTest : public ArrayViewTest< typename ARRAY_POLICY_PAIR::fi
471471
ParentClass::checkFill( array );
472472
}
473473

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

476488
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)