-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathChaiBuffer.hpp
More file actions
609 lines (540 loc) · 20.3 KB
/
ChaiBuffer.hpp
File metadata and controls
609 lines (540 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
* Copyright (c) 2021, Lawrence Livermore National Security, LLC and LvArray contributors.
* All rights reserved.
* See the LICENSE file for details.
* SPDX-License-Identifier: (BSD-3-Clause)
*/
/**
* @file ChaiBuffer.hpp
* @brief Contains the implementation of LvArray::ChaiBuffer.
*/
#pragma once
// Source includes
#include "LvArrayConfig.hpp"
#include "Macros.hpp"
#include "typeManipulation.hpp"
#include "arrayManipulation.hpp"
#include "system.hpp"
#include "bufferManipulation.hpp"
// TPL includes
#include <chai/ArrayManager.hpp>
// System includes
#include <mutex>
namespace LvArray
{
namespace internal
{
/**
* @return The chai::ArrayManager instance.
*/
inline chai::ArrayManager & getArrayManager()
{
static chai::ArrayManager & arrayManager = *chai::ArrayManager::getInstance();
return arrayManager;
}
/// chai is not threadsafe so we use a lock to serialize access.
static std::mutex chaiLock;
/**
* @return The chai::ExecutionSpace corresponding to @p space.
* @param space The MemorySpace to convert.
*/
inline chai::ExecutionSpace toChaiExecutionSpace( MemorySpace const space )
{
if( space == MemorySpace::undefined )
return chai::NONE;
if( space == MemorySpace::host )
return chai::CPU;
#if defined(LVARRAY_USE_CUDA)
if( space == MemorySpace::cuda )
return chai::GPU;
#endif
#if defined(LVARRAY_USE_HIP)
if( space == MemorySpace::hip )
return chai::GPU;
#endif
LVARRAY_ERROR( "Unrecognized memory space " << static_cast< int >( space ) );
return chai::NONE;
}
/**
* @return The MemorySpace corresponding to @p space.
* @param space The chai::ExecutionSpace to convert.
*/
inline MemorySpace toMemorySpace( chai::ExecutionSpace const space )
{
if( space == chai::NONE )
return MemorySpace::undefined;
if( space == chai::CPU )
return MemorySpace::host;
#if defined(LVARRAY_USE_CUDA)
if( space == chai::GPU )
return MemorySpace::cuda;
#endif
#if defined(LVARRAY_USE_HIP)
if( space == chai::GPU )
return MemorySpace::hip;
#endif
LVARRAY_ERROR( "Unrecognized execution space " << static_cast< int >( space ) );
return MemorySpace::undefined;
}
} // namespace internal
/**
* @tparam T type of data that is contained in the buffer.
* @class ChaiBuffer
* @brief Implements the Buffer interface using CHAI.
* @details The ChaiBuffer's allocation can exist in multiple memory spaces. If the chai
* execution space is set the copy constructor will ensure that the newly constructed
* ChaiBuffer's pointer points to memory in that space. If the memory does
* exist it will be allocated and the data copied over. If the memory exists but the data has been
* touched (modified) in the current space it will be copied over. The data is touched in the
* new space if T is non const and is not touched if T is const.
* @note Both the copy constructor and copy assignment constructor perform a shallow copy
* of the source. Similarly the destructor does not free the allocation.
*/
template< typename T >
class ChaiBuffer
{
public:
/// Alias for T used used in the bufferManipulation functions.
using value_type = T;
/// A flag indicating that the ChaiBuffer's copy semantics are shallow.
constexpr static bool hasShallowCopy = true;
/// An alias for the non const version of T.
using T_non_const = std::remove_const_t< T >;
/**
* @brief Default constructor, creates an uninitialized ChaiBuffer.
* @details An uninitialized ChaiBuffer is an undefined state and may only be assigned to.
* An uninitialized ChaiBuffer holds no recources and does not need to be free'd.
*/
LVARRAY_HOST_DEVICE inline constexpr
ChaiBuffer():
m_pointer( nullptr ),
m_capacity( 0 ),
m_pointerRecord( nullptr )
{}
/**
* @brief Constructor for creating an empty Buffer.
* @details An empty buffer may hold resources and needs to be free'd.
* @note The unused boolean parameter is to distinguish this from default constructor.
* @note Although it is marked as a host-device method, this is only valid to call from the host.
*/
LVARRAY_HOST_DEVICE
ChaiBuffer( bool ):
m_pointer( nullptr ),
m_capacity( 0 )
#if !defined(LVARRAY_DEVICE_COMPILE)
, m_pointerRecord( new chai::PointerRecord{} )
#else
, m_pointerRecord( nullptr )
#endif
{
#if defined(LVARRAY_DEVICE_COMPILE)
LVARRAY_ERROR( "Creating a new ChaiBuffer on device is not supported. This is often the result of capturing an array on device instead of a view." );
#else
m_pointerRecord->m_size = 0;
setName( "" );
for( int space = chai::CPU; space < chai::NUM_EXECUTION_SPACES; ++space )
{
m_pointerRecord->m_allocators[ space ] = internal::getArrayManager().getAllocatorId( chai::ExecutionSpace( space ) );
}
#endif
}
/**
* @brief Construct a ChaiBuffer which uses the specific allocator for each space.
* @param spaces The list of spaces.
* @param allocators The allocators, must be the same length as @p spaces.
* @details @code allocator[ i ] @endcode is used for the memory space @code spaces[ i ] @endcode.
* @note Although it is marked as a host-device method, this is only valid to call from the host.
*/
LVARRAY_HOST_DEVICE
ChaiBuffer( std::initializer_list< MemorySpace > const & spaces,
std::initializer_list< umpire::Allocator > const & allocators ):
m_pointer( nullptr ),
m_capacity( 0 )
#if !defined(LVARRAY_DEVICE_COMPILE)
, m_pointerRecord( new chai::PointerRecord{} )
#else
, m_pointerRecord( nullptr )
#endif
{
#if defined(LVARRAY_DEVICE_COMPILE)
LVARRAY_ERROR( "Creating a new ChaiBuffer on device is not supported." );
LVARRAY_UNUSED_VARIABLE( spaces );
LVARRAY_UNUSED_VARIABLE( allocators );
#else
m_pointerRecord->m_size = 0;
setName( "" );
LVARRAY_ERROR_IF_NE( spaces.size(), allocators.size() );
for( int space = chai::CPU; space < chai::NUM_EXECUTION_SPACES; ++space )
{
m_pointerRecord->m_allocators[ space ] = internal::getArrayManager().getAllocatorId( chai::ExecutionSpace( space ) );
}
for( std::size_t i = 0; i < spaces.size(); ++i )
{
m_pointerRecord->m_allocators[ internal::toChaiExecutionSpace( spaces.begin()[ i ] ) ] = allocators.begin()[ i ].getId();
}
#endif
}
/**
* @brief Copy constructor.
* @param src The buffer to copy.
* @details In addition to performing a shallow copy of @p src if the chai execution space
* is set *this will contain a pointer the the allocation in that space.
*/
LVARRAY_HOST_DEVICE inline
ChaiBuffer( ChaiBuffer const & src ):
m_pointer( src.m_pointer ),
m_capacity( src.m_capacity ),
m_pointerRecord( src.m_pointerRecord )
{
#if defined(LVARRAY_USE_DEVICE) && !defined(LVARRAY_DEVICE_COMPILE)
move( internal::toMemorySpace( internal::getArrayManager().getExecutionSpace() ), true );
#endif
}
/**
* @copydoc ChaiBuffer( ChaiBuffer const & )
* @param size The number of values in the allocation.
* @note In addition to performing a shallow copy of @p src if the chai execution space
* is set *this will contain a pointer the the allocation in that space. It will also
* move any nested objects.
*/
LVARRAY_HOST_DEVICE inline
ChaiBuffer( ChaiBuffer const & src, std::ptrdiff_t const size ):
m_pointer( src.m_pointer ),
m_capacity( src.m_capacity ),
m_pointerRecord( src.m_pointerRecord )
{
#if defined(LVARRAY_USE_DEVICE) && !defined(LVARRAY_DEVICE_COMPILE)
moveNested( internal::toMemorySpace( internal::getArrayManager().getExecutionSpace() ), size, true );
#else
LVARRAY_UNUSED_VARIABLE( size );
#endif
}
/**
* @brief Move constructor.
* @param src The ChaiBuffer to be moved from, is uninitialized after this call.
*/
LVARRAY_HOST_DEVICE inline constexpr
ChaiBuffer( ChaiBuffer && src ):
m_pointer( src.m_pointer ),
m_capacity( src.m_capacity ),
m_pointerRecord( src.m_pointerRecord )
{
src.m_capacity = 0;
src.m_pointer = nullptr;
src.m_pointerRecord = nullptr;
}
/**
* @brief Create a shallow copy of @p src but with a different type.
* @tparam U The type to convert from.
* @param src The buffer to copy.
*/
template< typename U >
LVARRAY_HOST_DEVICE inline constexpr
ChaiBuffer( ChaiBuffer< U > const & src ):
m_pointer( reinterpret_cast< T * >( src.data() ) ),
m_capacity( typeManipulation::convertSize< T, U >( src.capacity() ) ),
m_pointerRecord( &src.pointerRecord() )
{}
/**
* @brief Copy assignment operator.
* @param src The ChaiBuffer to be copied.
* @return *this.
*/
LVARRAY_HOST_DEVICE inline LVARRAY_INTEL_CONSTEXPR
ChaiBuffer & operator=( ChaiBuffer const & src )
{
m_capacity = src.m_capacity;
m_pointer = src.m_pointer;
m_pointerRecord = src.m_pointerRecord;
return *this;
}
/**
* @brief Move assignment operator.
* @param src The ChaiBuffer to be moved from, is uninitialized after this call.
* @return *this.
*/
LVARRAY_HOST_DEVICE inline LVARRAY_INTEL_CONSTEXPR
ChaiBuffer & operator=( ChaiBuffer && src )
{
m_capacity = src.m_capacity;
m_pointer = src.m_pointer;
m_pointerRecord = src.m_pointerRecord;
src.m_capacity = 0;
src.m_pointer = nullptr;
src.m_pointerRecord = nullptr;
return *this;
}
/**
* @brief Reallocate the buffer to the new capacity.
* @param size the number of values that are initialized in the buffer. Values between [0, size) are destroyed.
* @param space The space to perform the reallocation in. If space is the CPU then the buffer is reallocated
* only on the CPU and it is free'd in the other spaces. If the space is the GPU the the current size must be zero.
* @param newCapacity the new capacity of the buffer.
* @note Although it is marked as a host-device method, this is only valid to call from the host.
*/
LVARRAY_HOST_DEVICE
void reallocate( std::ptrdiff_t const size,
MemorySpace const space,
std::ptrdiff_t const newCapacity )
{
#if defined(LVARRAY_DEVICE_COMPILE)
LVARRAY_ERROR( "Allocation from device is not supported." );
LVARRAY_UNUSED_VARIABLE( size );
LVARRAY_UNUSED_VARIABLE( space );
LVARRAY_UNUSED_VARIABLE( newCapacity );
#else
move( space, true );
chai::PointerRecord * const newRecord = new chai::PointerRecord{};
newRecord->m_size = newCapacity * sizeof( T );
newRecord->m_user_callback = m_pointerRecord->m_user_callback;
for( int s = chai::CPU; s < chai::NUM_EXECUTION_SPACES; ++s )
{
newRecord->m_allocators[ s ] = m_pointerRecord->m_allocators[ s ];
}
chai::ExecutionSpace const chaiSpace = internal::toChaiExecutionSpace( space );
internal::chaiLock.lock();
internal::getArrayManager().allocate( newRecord, chaiSpace );
internal::chaiLock.unlock();
T * const newPointer = static_cast< T * >( newRecord->m_pointers[ chaiSpace ] );
if( size > 0 )
{
LVARRAY_ERROR_IF_NE_MSG( space, MemorySpace::host, "Calling reallocate with a non-zero current size is not yet supporeted for the GPU." );
std::ptrdiff_t const overlapAmount = std::min( newCapacity, size );
arrayManipulation::uninitializedMove( newPointer, overlapAmount, m_pointer );
arrayManipulation::destroy( m_pointer, size );
}
free();
m_capacity = newCapacity;
m_pointer = newPointer;
m_pointerRecord = newRecord;
registerTouch( space );
#endif
}
/**
* @brief Free the data in the buffer but does not destroy any values.
* @note To destroy the values and free the data call bufferManipulation::free.
* @note Although it is marked as a host-device method, this is only valid to call from the host.
*/
LVARRAY_HOST_DEVICE inline
void free()
{
#if defined(LVARRAY_DEVICE_COMPILE)
LVARRAY_ERROR( "Deallocation from device is not supported." );
#else
std::lock_guard< std::mutex > lock( internal::chaiLock );
internal::getArrayManager().free( m_pointerRecord );
m_capacity = 0;
m_pointer = nullptr;
m_pointerRecord = nullptr;
#endif
}
/**
* @brief Delete the buffer on device (no-op here)
*/
inline
void freeOnDevice() const
{
#if defined(LVARRAY_USE_CUDA) || defined(LVARRAY_USE_HIP )
chai::ExecutionSpace const chaiSpace = chai::CPU;
move( internal::toMemorySpace( chaiSpace ), true );
// if T is const the touch == true is ignored in move(), we force it.
m_pointerRecord->m_touched[ chaiSpace ] = true;
internal::getArrayManager().free( m_pointerRecord, chai::GPU );
#endif
}
/**
* @return Return the capacity of the buffer.
*/
LVARRAY_HOST_DEVICE inline constexpr
std::ptrdiff_t capacity() const
{ return m_capacity; }
/**
* @return Return a pointer to the beginning of the buffer.
*/
LVARRAY_HOST_DEVICE inline constexpr
T * data() const
{ return m_pointer; }
/**
* @brief Return a reference to the associated CHAI PointerRecord.
* @return A reference to the associated CHAI PointerRecord.
*/
LVARRAY_HOST_DEVICE inline constexpr
chai::PointerRecord & pointerRecord() const
{ return *m_pointerRecord; }
/**
* @tparam INDEX_TYPE the type used to index into the values.
* @return The value at position @p i .
* @param i The position of the value to access.
* @note No bounds checks are performed.
*/
template< typename INDEX_TYPE >
LVARRAY_HOST_DEVICE inline constexpr
T & operator[]( INDEX_TYPE const i ) const
{ return m_pointer[ i ]; }
/**
* @brief Move the buffer to the given execution space, optionally touching it.
* @param space The space to move the buffer to.
* @param size The size of the buffer.
* @param touch If the buffer should be touched in the new space or not.
* @note If they type T supports it this will call move( @p space, @p touch ) on each sub object.
*/
inline
void moveNested( MemorySpace const space, std::ptrdiff_t const size, bool const touch ) const
{
#if defined(LVARRAY_USE_CUDA) || defined(LVARRAY_USE_HIP )
chai::ExecutionSpace const chaiSpace = internal::toChaiExecutionSpace( space );
if( m_pointerRecord == nullptr ||
m_capacity == 0 ||
chaiSpace == chai::NONE ) return;
moveNestedImpl( space, size, touch );
#else
LVARRAY_ERROR_IF_NE( space, MemorySpace::host );
LVARRAY_UNUSED_VARIABLE( size );
LVARRAY_UNUSED_VARIABLE( touch );
#endif
}
/**
* @brief Move the buffer to the given execution space, optionally touching it.
* @param space The space to move the buffer to.
* @param touch If the buffer should be touched in the new space or not.
* @note This will not move subobjects.
*/
void move( MemorySpace const space, bool const touch ) const
{
#if defined(LVARRAY_USE_CUDA) || defined(LVARRAY_USE_HIP)
chai::ExecutionSpace const chaiSpace = internal::toChaiExecutionSpace( space );
if( m_pointerRecord == nullptr ||
m_capacity == 0 ||
chaiSpace == chai::NONE ) return;
auto & am = internal::getArrayManager();
const_cast< T * & >( m_pointer ) =
static_cast< T * >( am.move( const_cast< T_non_const * >( m_pointer ), m_pointerRecord, chaiSpace ) );
if( !std::is_const< T >::value && touch ) m_pointerRecord->m_touched[ chaiSpace ] = true;
m_pointerRecord->m_last_space = chaiSpace;
#else
LVARRAY_ERROR_IF_NE( space, MemorySpace::host );
LVARRAY_UNUSED_VARIABLE( touch );
#endif
}
/**
* @return The last space the ChaiBuffer was moved to.
*/
MemorySpace getPreviousSpace() const
{ return internal::toMemorySpace( m_pointerRecord->m_last_space ); }
/**
* @brief Touch the buffer in the given space.
* @param space the space to touch.
*/
inline constexpr
void registerTouch( MemorySpace const space ) const
{
chai::ExecutionSpace const chaiSpace = internal::toChaiExecutionSpace( space );
m_pointerRecord->m_touched[ chaiSpace ] = true;
m_pointerRecord->m_last_space = chaiSpace;
}
/**
* @tparam U The type of the owning class, will be displayed in the callback.
* @brief Set the name associated with this buffer which is used in the chai callback.
* @param name the of the buffer.
*/
template< typename U=ChaiBuffer< T > >
void setName( std::string const & name )
{
std::string const typeString = system::demangleType< U >();
m_pointerRecord->m_user_callback =
[name, typeString]( chai::PointerRecord const * const record, chai::Action const act, chai::ExecutionSpace const s )
{
if( act == chai::ACTION_MOVE )
{
std::string const size = system::calculateSize( record->m_size );
std::string const paddedSize = std::string( 9 - size.size(), ' ' ) + size;
char const * const spaceStr = ( s == chai::CPU ) ? "HOST " : "DEVICE";
LVARRAY_LOG( "Moved " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name );
}
if( act == chai::ACTION_ALLOC )
{
std::string const size = system::calculateSize( record->m_size );
std::string const paddedSize = std::string( 9 - size.size(), ' ' ) + size;
#if defined(LVARRAY_USE_CUDA)
size_t free, total;
cudaMemGetInfo( &free, &total );
std::string const size2 = system::calculateSize( free );
#endif
char const * const spaceStr = ( s == chai::CPU ) ? "HOST " : "DEVICE";
#if defined(LVARRAY_USE_CUDA)
LVARRAY_LOG( "Allocated " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name << " Free memory on device: " << size2 );
#else
LVARRAY_LOG( "Allocated " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name );
#endif
}
if( act == chai::ACTION_FREE )
{
std::string const size = system::calculateSize( record->m_size );
std::string const paddedSize = std::string( 9 - size.size(), ' ' ) + size;
#if defined(LVARRAY_USE_CUDA)
size_t free, total;
cudaMemGetInfo( &free, &total );
std::string const size2 = system::calculateSize( free );
#endif
char const * const spaceStr = ( s == chai::CPU ) ? "HOST " : "DEVICE";
#if defined(LVARRAY_USE_CUDA)
LVARRAY_LOG( "Freed " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name << " Free memory on device: " << size2 );
#else
LVARRAY_LOG( "Freed " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name );
#endif
}
};
}
private:
template< typename U=T_non_const >
std::enable_if_t< bufferManipulation::HasMemberFunction_move< U > >
moveNestedImpl( MemorySpace const space, std::ptrdiff_t const size, bool const touch ) const
{
if( m_pointerRecord->m_last_space != chai::CPU )
{
move( MemorySpace::host, false );
}
moveInnerData( space, size, touch );
move( space, touch );
}
template< typename U=T_non_const >
std::enable_if_t< !bufferManipulation::HasMemberFunction_move< U > >
moveNestedImpl( MemorySpace const space, std::ptrdiff_t const, bool const touch ) const
{ move( space, touch ); }
/**
* @tparam U A dummy parameter to enable SFINAE, do not specify.
* @brief Move inner allocations to the memory space @p space.
* @param space The memory space to move to.
* @param size The number of values to move.
* @param touch If the inner values should be touched or not.
* @return void.
* @note This method is only active when T has a method move( MemorySpace ).
*/
template< typename U=T_non_const >
std::enable_if_t< bufferManipulation::HasMemberFunction_move< U > >
moveInnerData( MemorySpace const space, std::ptrdiff_t const size, bool const touch ) const
{
if( space == MemorySpace::undefined ) return;
for( std::ptrdiff_t i = 0; i < size; ++i )
{
const_cast< T_non_const * >( m_pointer )[ i ].move( space, touch );
}
}
/**
* @tparam U A dummy parameter to enable SFINAE, do not specify.
* @brief Move inner allocations to the memory space @p space.
* @return void.
* @note This method is only active when T does not have a method move( MemorySpace ).
*/
template< typename U=T_non_const >
std::enable_if_t< !bufferManipulation::HasMemberFunction_move< U > >
moveInnerData( MemorySpace const, std::ptrdiff_t const, bool const ) const
{}
/// A pointer to the data.
T * LVARRAY_RESTRICT m_pointer = nullptr;
/// The size of the allocation.
std::ptrdiff_t m_capacity = 0;
/// A pointer to the chai PointerRecord, keeps track of the memory space information.
chai::PointerRecord * m_pointerRecord = nullptr;
};
} /* namespace LvArray */