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

Device Packing Optimization #1382

Merged
merged 4 commits into from
Apr 21, 2021
Merged
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
68 changes: 44 additions & 24 deletions src/coreComponents/dataRepository/BufferOpsDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,25 @@ PackDataByIndexDevice ( buffer_unit_type * & buffer,
parallelDeviceEvents & events )
{
localIndex const numIndices = indices.size();
localIndex const unitSize = var.size() / var.size( 0 ) * sizeof( T );

buffer_unit_type * const devBuffer = buffer;
localIndex const sliceSize = var.size() / var.size( 0 );
localIndex packedSize = numIndices * sliceSize * sizeof( T );
if( DO_PACKING )
{
T * devBuffer = reinterpret_cast< T * >( buffer );
parallelDeviceStream stream;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clean up this stream and events business later. We should not be creating a new stream for every field that we pack and we should probably do stream based synchronization instead of event based.

events.emplace_back( forAll< parallelDeviceAsyncPolicy<> >( stream, numIndices, [=] GEOSX_DEVICE ( localIndex const ii )
events.emplace_back( forAll< parallelDevicePolicy< > >( stream, numIndices, [=] GEOSX_DEVICE ( localIndex const ii )
{
buffer_unit_type * threadBuffer = devBuffer + ii * unitSize;
LvArray::forValuesInSlice( var[ indices[ ii ] ], [&threadBuffer] GEOSX_DEVICE ( T const & value )
T * threadBuffer = &devBuffer[ ii * sliceSize ];
LvArray::forValuesInSlice( var[ indices[ ii ] ], [&] GEOSX_DEVICE ( T const & value )
{
memcpy( threadBuffer, &value, sizeof( T ) );
threadBuffer += sizeof( T );
*threadBuffer = value;
++threadBuffer;
} );
} ) );

buffer += numIndices * unitSize;
buffer += packedSize;
}
localIndex packedSize = numIndices * unitSize;

return packedSize;
}

Expand All @@ -175,6 +175,18 @@ PackByIndexDevice ( buffer_unit_type * & buffer,
parallelDeviceEvents & events )
{
localIndex packedSize = PackPointerDevice< DO_PACKING >( buffer, var.strides(), NDIM );
size_t typeSize = sizeof( T );
uintptr_t misalignment = 0;
if( DO_PACKING )
{
uintptr_t address = reinterpret_cast< uintptr_t >( buffer );
misalignment = address % typeSize;
if( misalignment != 0 )
{
buffer += typeSize - misalignment;
}
}
packedSize += typeSize - 1;
packedSize += PackDataByIndexDevice< DO_PACKING >( buffer, var, indices, events );
return packedSize;
}
Expand All @@ -187,22 +199,22 @@ UnpackDataByIndexDevice ( buffer_unit_type const * & buffer,
parallelDeviceEvents & events )
{
localIndex numIndices = indices.size();
buffer_unit_type const * devBuffer = buffer;
localIndex unitSize = var.size() / var.size( 0 ) * sizeof(T);
localIndex sliceSize = var.size() / var.size( 0 );
localIndex unpackSize = numIndices * sliceSize * sizeof( T );
T const * devBuffer = reinterpret_cast< T const * >( buffer );
parallelDeviceStream stream;
events.emplace_back( forAll< parallelDeviceAsyncPolicy<> >( stream, numIndices, [=] GEOSX_DEVICE ( localIndex const i )
events.emplace_back( forAll< parallelDeviceAsyncPolicy<> >( stream, numIndices, [=] GEOSX_DEVICE ( localIndex const ii )
{
buffer_unit_type const * threadBuffer = devBuffer + i * unitSize;
LvArray::forValuesInSlice( var[ indices[ i ] ], [&threadBuffer] GEOSX_DEVICE ( T & value )
T const * threadBuffer = &devBuffer[ ii * sliceSize ];
LvArray::forValuesInSlice( var[ indices[ ii ] ], [&threadBuffer] GEOSX_DEVICE ( T & value )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it later and you can only get rid of forValuesInSlice if the slice is contiguous. If we don't care the order that it's packed in, as long as it's unpacked in a consistent manner then we can probably do much better but it would take some thinking as to how best to pack things and it would likely involve a single thread packing parts of multiple objects in some cases.

{
memcpy( &value, threadBuffer, sizeof( T ) );
threadBuffer += sizeof( T );
value = *threadBuffer;
++threadBuffer;
} );
} ) );
localIndex avSize = numIndices * unitSize;
localIndex sizeOfPackedChars = avSize;
buffer += avSize;
return sizeOfPackedChars;

buffer += unpackSize;
return unpackSize;
}

template< typename T, int NDIM, int USD, typename T_INDICES >
Expand All @@ -212,10 +224,18 @@ UnpackByIndexDevice ( buffer_unit_type const * & buffer,
T_INDICES const & indices,
parallelDeviceEvents & events )
{
size_t typeSize = sizeof( T );
localIndex strides[NDIM];
localIndex sizeOfPackedChars = UnpackPointerDevice( buffer, strides, NDIM );
sizeOfPackedChars += UnpackDataByIndexDevice( buffer, var, indices, events );
return sizeOfPackedChars;
localIndex unpackSize = UnpackPointerDevice( buffer, strides, NDIM );
uintptr_t address = reinterpret_cast< uintptr_t >( buffer );
uintptr_t misalignment = address % typeSize;
if( misalignment != 0 )
{
buffer += typeSize - misalignment;
}
unpackSize += typeSize - 1;
unpackSize += UnpackDataByIndexDevice( buffer, var, indices, events );
return unpackSize;
}

#define DECLARE_PACK_UNPACK( TYPE, NDIM, USD ) \
Expand Down
8 changes: 4 additions & 4 deletions src/coreComponents/dataRepository/BufferOpsDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ PackDataDevice( buffer_unit_type * & GEOSX_UNUSED_PARAM( buffer ),
//------------------------------------------------------------------------------
template< bool DO_PACKING, typename T, int NDIM, int USD, typename T_INDICES >
typename std::enable_if< can_memcpy< T >, localIndex >::type
PackDataByIndexDevice( buffer_unit_type * & buffer,
ArrayView< T const, NDIM, USD > const & var,
T_INDICES const & indices,
parallelDeviceEvents & events );
PackDataByIndexDevice ( buffer_unit_type * & buffer,
ArrayView< T const, NDIM, USD > const & var,
T_INDICES const & indices,
parallelDeviceEvents & events );

//------------------------------------------------------------------------------
template< bool DO_PACKING, typename T, typename T_INDICES >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ TEST( testPacking, testPackByIndexDevice )
{
std::srand( std::time( nullptr ));
constexpr localIndex size = 10000;
localIndex pack_count = std::rand() % size;
localIndex pack_count = 5000;
array1d< R1Tensor > veloc( size );
array1d< localIndex > indices( pack_count );
array1d< R1Tensor > unpacked( size );
Expand All @@ -219,7 +219,7 @@ TEST( testPacking, testPackByIndexDevice )
for( localIndex ii = 0; ii < pack_count; ++ii )
indices[ii] = std::rand() % size;

// std::sort(indices.begin(),indices.end())
std::sort( indices.begin(), indices.end());

buffer_unit_type * null_buf = NULL;
// [ num_dim, stride_i.. , tensor_0, tensor_1, ..., tensor_n ]
Expand Down