Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getAvailableMemoryOnHost() and getAvailableMemoryOnDevice() #299

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
#include <sys/wait.h>
#endif

#if defined(__APPLE__) && defined(__MACH__)
#include <sys/types.h>
#include <sys/sysctl.h>

#include <mach/host_info.h>
#include <mach/mach_host.h>
#include <mach/task_info.h>
#include <mach/task.h>
#else
#include <unistd.h>
#endif

/**
* @struct UnwindState
* @brief Holds info used in unwindCallback.
Expand Down Expand Up @@ -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
20 changes: 16 additions & 4 deletions src/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* @file system.hpp
* @brief Contains functions that interact with the system or runtime environment.
*/

#pragma once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of LvArray is using #pragma once, did you not have any issues with it before?

#ifndef LVARRAY_SYSTEM_HPP
#define LVARRAY_SYSTEM_HPP

// System includes
#include <string>
Expand Down Expand Up @@ -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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer getAvailableMemory(MemorySpace const space) instead of two separate functions.


/**
* @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.
Expand Down Expand Up @@ -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 */