Skip to content

Commit 7aace42

Browse files
XL64Xavier LacosteacitrainJie Meng
authored
Feature/add freeOnDevice() method (#298)
* Add freeOnDevice method to free device buffer * Add logging for ACTION_ALLOC/ACTION_FREE when user callback activated * add freeOnDevice for arrays of arrays --------- Co-authored-by: Xavier Lacoste <[email protected]> Co-authored-by: Aurelien <[email protected]> Co-authored-by: Jie Meng <[email protected]>
1 parent fb5200d commit 7aace42

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/ArrayOfArraysView.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,16 @@ class ArrayOfArraysView
596596
}
597597

598598

599+
/**
600+
* Move data to host if not valid and register host as the last valid space and free data on device.
601+
*/
602+
void freeOnDevice() const
603+
{
604+
m_values.freeOnDevice();
605+
m_sizes.freeOnDevice();
606+
m_offsets.freeOnDevice();
607+
}
608+
599609
///@}
600610

601611
protected:

src/ArrayView.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,14 @@ class ArrayView
675675
void move( MemorySpace const space, bool const touch=true ) const
676676
{ m_dataBuffer.moveNested( space, size(), touch ); }
677677

678+
/**
679+
* Move data to host if not valid and register host as the last valid space and free data on device.
680+
*/
681+
void freeOnDevice() const
682+
{
683+
m_dataBuffer.freeOnDevice();
684+
}
685+
678686
///@}
679687

680688
#if defined(LVARRAY_USE_TOTALVIEW_OUTPUT) && !defined(__CUDA_ARCH__)

src/ChaiBuffer.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,21 @@ class ChaiBuffer
373373
#endif
374374
}
375375

376+
/**
377+
* @brief Delete the buffer on device (no-op here)
378+
*/
379+
inline
380+
void freeOnDevice() const
381+
{
382+
#if defined(LVARRAY_USE_CUDA) || defined(LVARRAY_USE_HIP )
383+
chai::ExecutionSpace const chaiSpace = chai::CPU;
384+
move( internal::toMemorySpace( chaiSpace ), true );
385+
// if T is const the touch == true is ignored in move(), we force it.
386+
m_pointerRecord->m_touched[ chaiSpace ] = true;
387+
internal::getArrayManager().free( m_pointerRecord, chai::GPU );
388+
#endif
389+
}
390+
376391
/**
377392
* @return Return the capacity of the buffer.
378393
*/
@@ -499,6 +514,40 @@ class ChaiBuffer
499514
char const * const spaceStr = ( s == chai::CPU ) ? "HOST " : "DEVICE";
500515
LVARRAY_LOG( "Moved " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name );
501516
}
517+
518+
if( act == chai::ACTION_ALLOC )
519+
{
520+
std::string const size = system::calculateSize( record->m_size );
521+
std::string const paddedSize = std::string( 9 - size.size(), ' ' ) + size;
522+
#if defined(LVARRAY_USE_CUDA)
523+
size_t free, total;
524+
cudaMemGetInfo( &free, &total );
525+
std::string const size2 = system::calculateSize( free );
526+
#endif
527+
char const * const spaceStr = ( s == chai::CPU ) ? "HOST " : "DEVICE";
528+
#if defined(LVARRAY_USE_CUDA)
529+
LVARRAY_LOG( "Allocated " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name << " Free memory on device: " << size2 );
530+
#else
531+
LVARRAY_LOG( "Allocated " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name );
532+
#endif
533+
}
534+
535+
if( act == chai::ACTION_FREE )
536+
{
537+
std::string const size = system::calculateSize( record->m_size );
538+
std::string const paddedSize = std::string( 9 - size.size(), ' ' ) + size;
539+
#if defined(LVARRAY_USE_CUDA)
540+
size_t free, total;
541+
cudaMemGetInfo( &free, &total );
542+
std::string const size2 = system::calculateSize( free );
543+
#endif
544+
char const * const spaceStr = ( s == chai::CPU ) ? "HOST " : "DEVICE";
545+
#if defined(LVARRAY_USE_CUDA)
546+
LVARRAY_LOG( "Freed " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name << " Free memory on device: " << size2 );
547+
#else
548+
LVARRAY_LOG( "Freed " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name );
549+
#endif
550+
}
502551
};
503552
}
504553

src/MallocBuffer.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ class MallocBuffer : public bufferManipulation::VoidBuffer
163163
m_data = nullptr;
164164
}
165165

166+
/**
167+
* @brief Delete the buffer on device (no-op here)
168+
*/
169+
LVARRAY_HOST_DEVICE inline
170+
void freeOnDevice() const {}
171+
166172
/**
167173
* @return Return the capacity of the buffer.
168174
*/

0 commit comments

Comments
 (0)