diff --git a/src/system.cpp b/src/system.cpp index a6532ac5..9874ae89 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -30,6 +30,18 @@ #include #endif +#if defined(__APPLE__) && defined(__MACH__) + #include + #include + + #include + #include + #include + #include +#else + #include +#endif + /** * @struct UnwindState * @brief Holds info used in unwindCallback. @@ -650,5 +662,52 @@ 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; +#if defined(LVARRAY_USE_CUDA) + size_t total; + LVARRAY_ERROR_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..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 @@ -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. @@ -144,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 */