From 977bc1f2cff9b95ff2c3efc8a7258df97af4740e Mon Sep 17 00:00:00 2001 From: Xavier Lacoste Date: Tue, 25 Jul 2023 16:58:27 +0200 Subject: [PATCH 1/7] add getAvailableMemoryOnHost() and getAvailableMemoryOnDevice() --- src/system.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/system.hpp | 12 +++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/system.cpp b/src/system.cpp index a6532ac5..7b12c329 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -30,6 +30,17 @@ #include #endif +#include +#if defined(__APPLE__) && defined(__MACH__) +#include +#include + +#include +#include +#include +#include +#endif + /** * @struct UnwindState * @brief Holds info used in unwindCallback. @@ -650,5 +661,51 @@ void setFPE() #endif } +/** + * @brief Retieves current available memory on host + * @return the available memory in bytes. + */ +size_t getAvailableMemoryOnHost() +{ +#if defined(__APPLE__) && defined(__MACH__) + int mib[6]; + mib[0] = CTL_HW; + mib[1] = HW_PAGESIZE; + + int pagesize; + size_t length; + length = sizeof( pagesize ); + if( sysctl( mib, 2, &pagesize, &length, NULL, 0 ) < 0 ) + { + fprintf( stderr, "getting page size" ); + } + + mach_msg_type_number_t count = HOST_VM_INFO_COUNT; + + vm_statistics_data_t vmstat; + if( host_statistics( mach_host_self(), HOST_VM_INFO, ( host_info_t ) &vmstat, &count ) != KERN_SUCCESS ) + { + fprintf ( stderr, "Failed to get VM statistics." ); + } + + return vmstat.free_count * pagesize; +#else + return (size_t)sysconf( _SC_AVPHYS_PAGES ) *(size_t) sysconf( _SC_PAGESIZE ); +#endif +} + +/** + * @brief Retieves current available memory on device + * @return the available memory in bytes. + */ +size_t getAvailableMemoryOnDevice() +{ + size_t free=0, total; +#if defined(LVARRA_USE_CUDA) + LVARRAYERROR_IF( cudaSuccess != cudaMemGetInfo( &free, &total ), "Error getting CUDA device available memory" ); +#endif + return free; +} + } // namespace system } // namespace LvArray diff --git a/src/system.hpp b/src/system.hpp index 59a77526..d16f071a 100644 --- a/src/system.hpp +++ b/src/system.hpp @@ -117,6 +117,18 @@ int disableFloatingPointExceptions( int const exceptions = getDefaultFloatingPoi */ void setFPE(); +/** + * @brief Retieves current available memory on host + * @return the available memory in bytes. + */ +size_t getAvailableMemoryOnHost(); + +/** + * @brief Retieves current available memory on device + * @return the available memory in bytes. + */ +size_t getAvailableMemoryOnDevice(); + /** * @class FloatingPointExceptionGuard * @brief Changes the floating point environment and reverts it when destoyed. From c7282c24686257b5bebc3e03506ee5084dc7333c Mon Sep 17 00:00:00 2001 From: Xavier Lacoste Date: Wed, 26 Jul 2023 10:26:25 +0200 Subject: [PATCH 2/7] wronng include --- src/system.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/system.cpp b/src/system.cpp index 7b12c329..987302c9 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -30,7 +30,7 @@ #include #endif -#include +#include #if defined(__APPLE__) && defined(__MACH__) #include #include From 936ac08ae0713e0720c6de7d9712ea066b5caa1f Mon Sep 17 00:00:00 2001 From: Xavier Lacoste Date: Wed, 26 Jul 2023 10:29:45 +0200 Subject: [PATCH 3/7] uncrustify --- src/system.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/system.cpp b/src/system.cpp index 987302c9..2851dd2c 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -700,11 +700,11 @@ size_t getAvailableMemoryOnHost() */ size_t getAvailableMemoryOnDevice() { - size_t free=0, total; + size_t free=0, total; #if defined(LVARRA_USE_CUDA) - LVARRAYERROR_IF( cudaSuccess != cudaMemGetInfo( &free, &total ), "Error getting CUDA device available memory" ); + LVARRAYERROR_IF( cudaSuccess != cudaMemGetInfo( &free, &total ), "Error getting CUDA device available memory" ); #endif - return free; + return free; } } // namespace system From 8fd53ad223f18925504931ba478d1a001be87647 Mon Sep 17 00:00:00 2001 From: Xavier Lacoste Date: Wed, 26 Jul 2023 10:49:06 +0200 Subject: [PATCH 4/7] prgma once not supported by Apple clang version 14.0.3 (clang-1403.0.22.14.1) --- src/system.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/system.hpp b/src/system.hpp index d16f071a..add0ddb6 100644 --- a/src/system.hpp +++ b/src/system.hpp @@ -9,8 +9,8 @@ * @file system.hpp * @brief Contains functions that interact with the system or runtime environment. */ - -#pragma once +#ifndef LVARRAY_SYSTEM_HPP +#define LVARRAY_SYSTEM_HPP // System includes #include @@ -156,11 +156,11 @@ class FloatingPointExceptionGuard }; /** - * @return A string representing @p bytes converted to either - * KB, MB, or GB. + * @return A string representing @p bytes converted to either KB, MB, or GB. * @param bytes The number of bytes. */ std::string calculateSize( size_t const bytes ); } // namespace system } // namespace LvArray +#endif /* !LVARRAY_SYSTEM_HPP */ From c4f22f7ae79457308db4626165ee0b3cd19d03ec Mon Sep 17 00:00:00 2001 From: Xavier Lacoste Date: Wed, 26 Jul 2023 11:08:40 +0200 Subject: [PATCH 5/7] includes --- src/system.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/system.cpp b/src/system.cpp index 2851dd2c..5b199544 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -30,15 +30,16 @@ #include #endif -#include #if defined(__APPLE__) && defined(__MACH__) -#include -#include + #include + #include -#include -#include -#include -#include + #include + #include + #include + #include +#else + #include #endif /** From 67ee02a5478110107e247c051757f28cc18b90c7 Mon Sep 17 00:00:00 2001 From: Xavier Lacoste Date: Wed, 26 Jul 2023 14:48:57 +0200 Subject: [PATCH 6/7] unused variable --- src/system.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/system.cpp b/src/system.cpp index 5b199544..7b5d3671 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -701,8 +701,9 @@ size_t getAvailableMemoryOnHost() */ size_t getAvailableMemoryOnDevice() { - size_t free=0, total; -#if defined(LVARRA_USE_CUDA) + size_t free=0; +#if defined(LVARRAY_USE_CUDA) + size_t total; LVARRAYERROR_IF( cudaSuccess != cudaMemGetInfo( &free, &total ), "Error getting CUDA device available memory" ); #endif return free; From c506d0d307cb1a5ddfa71a43c83b89dbd3caf6d8 Mon Sep 17 00:00:00 2001 From: Xavier Lacoste Date: Wed, 26 Jul 2023 14:55:52 +0200 Subject: [PATCH 7/7] typo --- src/system.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/system.cpp b/src/system.cpp index 7b5d3671..9874ae89 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -704,7 +704,7 @@ size_t getAvailableMemoryOnDevice() size_t free=0; #if defined(LVARRAY_USE_CUDA) size_t total; - LVARRAYERROR_IF( cudaSuccess != cudaMemGetInfo( &free, &total ), "Error getting CUDA device available memory" ); + LVARRAY_ERROR_IF( cudaSuccess != cudaMemGetInfo( &free, &total ), "Error getting CUDA device available memory" ); #endif return free; }