Skip to content

Commit 8a013cc

Browse files
authored
add a to-string method for memory space (#823)
1 parent 7e290d6 commit 8a013cc

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

include/matx/core/allocator.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ __MATX_INLINE__ MemTracker &GetAllocMap() {
258258
* @param mem Memory space
259259
* @return True is pointer can be printed from the host
260260
*/
261-
inline bool HostPrintable(matxMemorySpace_t mem)
261+
__MATX_INLINE__ bool HostPrintable(matxMemorySpace_t mem)
262262
{
263263
return (mem == MATX_MANAGED_MEMORY || mem == MATX_HOST_MEMORY || mem == MATX_HOST_MALLOC_MEMORY);
264264
}
@@ -271,7 +271,7 @@ inline bool HostPrintable(matxMemorySpace_t mem)
271271
* @param mem Memory space
272272
* @return True is pointer can be printed from the device
273273
*/
274-
inline bool DevicePrintable(matxMemorySpace_t mem)
274+
__MATX_INLINE__ bool DevicePrintable(matxMemorySpace_t mem)
275275
{
276276
return (mem == MATX_MANAGED_MEMORY || mem == MATX_DEVICE_MEMORY ||
277277
mem == MATX_ASYNC_DEVICE_MEMORY);
@@ -284,7 +284,7 @@ inline bool DevicePrintable(matxMemorySpace_t mem)
284284
* @param total Total memory usage
285285
* @param max Maximum memory usage
286286
*/
287-
inline void matxGetMemoryStats(size_t *current, size_t *total, size_t *max)
287+
__MATX_INLINE__ void matxGetMemoryStats(size_t *current, size_t *total, size_t *max)
288288
{
289289
// std::shared_lock lck(memory_mtx);
290290
*current = matxMemoryStats.currentBytesAllocated;
@@ -298,7 +298,7 @@ inline void matxGetMemoryStats(size_t *current, size_t *total, size_t *max)
298298
* @param ptr Pointer
299299
* @return True if allocator
300300
*/
301-
inline bool IsAllocated(void *ptr) {
301+
__MATX_INLINE__ bool IsAllocated(void *ptr) {
302302
return GetAllocMap().is_allocated(ptr);
303303
}
304304

@@ -314,7 +314,7 @@ inline bool IsAllocated(void *ptr) {
314314
*also offset in a positive direction from the base, and generally if you're in
315315
*a specific address range the type of pointer is obvious anyways.
316316
**/
317-
inline matxMemorySpace_t GetPointerKind(void *ptr)
317+
__MATX_INLINE__ matxMemorySpace_t GetPointerKind(void *ptr)
318318
{
319319
return GetAllocMap().get_pointer_kind(ptr);
320320
}
@@ -323,7 +323,7 @@ inline matxMemorySpace_t GetPointerKind(void *ptr)
323323
* @brief Print memory statistics to stdout
324324
*
325325
*/
326-
inline void matxPrintMemoryStatistics()
326+
__MATX_INLINE__ void matxPrintMemoryStatistics()
327327
{
328328
size_t current, total, max;
329329

@@ -345,7 +345,7 @@ inline void matxPrintMemoryStatistics()
345345
* @param space Memory space
346346
* @param stream CUDA stream (for stream allocations)
347347
*/
348-
inline void matxAlloc(void **ptr, size_t bytes,
348+
__MATX_INLINE__ void matxAlloc(void **ptr, size_t bytes,
349349
matxMemorySpace_t space = MATX_MANAGED_MEMORY,
350350
cudaStream_t stream = 0)
351351
{
@@ -355,15 +355,15 @@ inline void matxAlloc(void **ptr, size_t bytes,
355355
}
356356

357357

358-
inline void matxFree(void *ptr)
358+
__MATX_INLINE__ void matxFree(void *ptr)
359359
{
360360
MATX_NVTX_START("", matx::MATX_NVTX_LOG_INTERNAL)
361361

362362
return GetAllocMap().deallocate(ptr);
363363
}
364364

365365

366-
inline void matxFree(void *ptr, cudaStream_t stream)
366+
__MATX_INLINE__ void matxFree(void *ptr, cudaStream_t stream)
367367
{
368368
MATX_NVTX_START("", matx::MATX_NVTX_LOG_INTERNAL)
369369
return GetAllocMap().deallocate(ptr, stream);
@@ -374,7 +374,7 @@ inline void matxFree(void *ptr, cudaStream_t stream)
374374
memory that was allocated in stream A inside of stream B. The caller must ensure that the pointer
375375
and stream being used are valid.
376376
*/
377-
inline void update_stream(void *ptr, cudaStream_t stream)
377+
__MATX_INLINE__ void update_stream(void *ptr, cudaStream_t stream)
378378
{
379379
MATX_NVTX_START("", matx::MATX_NVTX_LOG_INTERNAL)
380380
GetAllocMap().update_stream(ptr, stream);
@@ -394,7 +394,7 @@ struct matx_allocator {
394394
* @param size Size of allocation in bytes
395395
* @return Pointer to allocated memory, or nullptr on error
396396
*/
397-
inline T* allocate(size_t size)
397+
__MATX_INLINE__ T* allocate(size_t size)
398398
{
399399
T *tmp;
400400
matxAlloc(reinterpret_cast<void**>(&tmp), size);
@@ -407,12 +407,21 @@ struct matx_allocator {
407407
* @param ptr Pointer to allocated data
408408
* @param size Size of previously-allocated memory in bytes
409409
*/
410-
inline void deallocate(void *ptr, [[maybe_unused]] size_t size)
410+
__MATX_INLINE__ void deallocate(void *ptr, [[maybe_unused]] size_t size)
411411
{
412412
matxFree(ptr);
413413
}
414414
};
415415

416-
416+
__MATX_INLINE__ std::string SpaceString(matxMemorySpace_t space) {
417+
switch (space) {
418+
case MATX_MANAGED_MEMORY: return "CUDA managed memory";
419+
case MATX_HOST_MEMORY: return "CUDA host-pinned memory";
420+
case MATX_HOST_MALLOC_MEMORY: return "Host memory";
421+
case MATX_DEVICE_MEMORY: return "CUDA device memory";
422+
case MATX_ASYNC_DEVICE_MEMORY: return "CUDA asynchronous device memory";
423+
default: return "Unknown memory";
424+
}
425+
}
417426

418427
} // end namespace matx

0 commit comments

Comments
 (0)