Skip to content
Permalink
Browse files

Merge pull request #12953 from dschwen/fix_memorystuff_12951

Fix memory unit prefixes
  • Loading branch information...
lindsayad committed Feb 22, 2019
2 parents 2ed7285 + 2faafa9 commit 8819d4e7e749ed04b9a5fd70bd2bf6688d3b0b70
@@ -50,8 +50,8 @@ class MemoryUsage : public GeneralPostprocessor, public MemoryUsageReporter
min_process
} _value_type;

/// The units in which to report memory statistics (kilobyte, megabyte, etc).
MemoryUtils::MemUnit _mem_units;
/// The unit prefix for the reported memory statistics (kilobyte, megabyte, etc).
MemoryUtils::MemUnits _mem_units;

/// memory usage metric in bytes
Real _value;
@@ -11,13 +11,11 @@
#define MEMORYUTILS_H

#include "Moose.h"
#include "MooseEnum.h"

namespace MemoryUtils
{

/// get the total amount of physical RAM available
std::size_t getTotalRAM();

/// get the MPI hostname
std::string getMPIProcessorName();

@@ -28,16 +26,28 @@ struct Stats
std::size_t _page_faults;
};

enum class MemUnit
enum class MemUnits
{
Bytes,
Kibibytes,
Mebibytes,
Gibibytes = 3,
Kilobytes,
Megabytes,
Gigabytes,
Gigabytes = 6,
};

/// get the moose enum for the mem_unit_prefix parameter
MooseEnum getMemUnitsEnum();

/// get the total amount of physical RAM available
std::size_t getTotalRAM();

/// get all memory stats for the current process
void getMemoryStats(Stats & stats, MemUnit units = MemUnit::Megabytes);
void getMemoryStats(Stats & stats);

/// convert bytes to selected unit prefix
std::size_t convertBytes(std::size_t bytes, MemUnits unit);

} // namespace MemoryUtils

@@ -35,8 +35,8 @@ class VectorMemoryUsage : public GeneralVectorPostprocessor, public MemoryUsageR
virtual void finalize() override;

protected:
/// The units in which to report memory statistics (kilobyte, megabyte, etc).
MemoryUtils::MemUnit _mem_units;
/// The unit prefix for the reported memory statistics (kilobyte, megabyte, etc).
MemoryUtils::MemUnits _mem_units;

/// hardware id for the physical node the rank is located at
VectorPostprocessorValue & _col_hardware_id;
@@ -25,9 +25,9 @@ validParams<MemoryUsage>()
MooseEnum value_type("total average max_process min_process", "total");
params.addParam<MooseEnum>(
"value_type", value_type, "Aggregation method to apply to the requested memory metric.");
MooseEnum mem_units("bytes kilobytes megabytes gigabytes", "megabytes");
params.addParam<MooseEnum>(
"mem_units", mem_units, "The unit used to report memory usage, default: Megabytes");
params.addParam<MooseEnum>("mem_units",
MemoryUtils::getMemUnitsEnum(),
"The unit prefix used to report memory usage, default: Mebibytes");
params.addParam<bool>("report_peak_value",
true,
"If the postprocessor is executed more than once "
@@ -41,7 +41,7 @@ MemoryUsage::MemoryUsage(const InputParameters & parameters)
MemoryUsageReporter(this),
_mem_type(getParam<MooseEnum>("mem_type").getEnum<MemType>()),
_value_type(getParam<MooseEnum>("value_type").getEnum<ValueType>()),
_mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnit>()),
_mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnits>()),
_value(0.0),
_peak_value(0.0),
_report_peak_value(getParam<bool>("report_peak_value"))
@@ -58,17 +58,17 @@ void
MemoryUsage::execute()
{
MemoryUtils::Stats stats;
MemoryUtils::getMemoryStats(stats, _mem_units);
MemoryUtils::getMemoryStats(stats);

// get the requested per core metric
switch (_mem_type)
{
case MemType::physical_memory:
_value = stats._physical_memory;
_value = MemoryUtils::convertBytes(stats._physical_memory, _mem_units);
break;

case MemType::virtual_memory:
_value = stats._virtual_memory;
_value = MemoryUtils::convertBytes(stats._virtual_memory, _mem_units);
break;

case MemType::page_faults:
@@ -30,6 +30,26 @@
namespace MemoryUtils
{

std::string
getMPIProcessorName()
{
#ifdef LIBMESH_HAVE_MPI
int mpi_namelen;
char mpi_name[MPI_MAX_PROCESSOR_NAME];
MPI_Get_processor_name(mpi_name, &mpi_namelen);
return mpi_name;
#else
return "serial";
#endif
}

MooseEnum
getMemUnitsEnum()
{
return MooseEnum("bytes kibibytes mebibytes gibibytes kilobytes megabytes gigabytes",
"mebibytes");
}

std::size_t
getTotalRAM()
{
@@ -46,21 +66,8 @@ getTotalRAM()
return 0;
}

std::string
getMPIProcessorName()
{
#ifdef LIBMESH_HAVE_MPI
int mpi_namelen;
char mpi_name[MPI_MAX_PROCESSOR_NAME];
MPI_Get_processor_name(mpi_name, &mpi_namelen);
return mpi_name;
#else
return "serial";
#endif
}

void
getMemoryStats(Stats & stats, MemUnit units)
getMemoryStats(Stats & stats)
{
enum StatItem
{
@@ -106,17 +113,37 @@ getMemoryStats(Stats & stats, MemUnit units)
#endif
}

// Create the proper divisor based on the units
const long divisor = 1 << (static_cast<unsigned int>(units) * 10);

// physical mem
stats._physical_memory = val[index_resident_size] / divisor;
stats._physical_memory = val[index_resident_size];

// virtual mem
stats._virtual_memory = val[index_virtual_size] / divisor;
stats._virtual_memory = val[index_virtual_size];

// page faults
stats._page_faults = val[index_page_faults];
}

std::size_t
convertBytes(std::size_t bytes, MemUnits unit)
{
if (unit == MemUnits::Bytes)
return bytes;

unsigned int nunit = static_cast<unsigned int>(unit);

// kibi, mebi, gibi
if (nunit <= 3)
return bytes >> (nunit * 10);

// kilo, mega, giga
if (nunit <= 6)
{
while (nunit-- > 3)
bytes /= 1000;
return bytes;
}

mooseError("Unknown memory unit");
}

} // namespace MemoryUtils
@@ -26,16 +26,16 @@ validParams<VectorMemoryUsage>()
"If the vectorpostprocessor is executed more than once "
"during a time step, report the aggregated peak "
"value.");
MooseEnum mem_units("bytes kilobytes megabytes gigabytes", "megabytes");
params.addParam<MooseEnum>(
"mem_units", mem_units, "The unit used to report memory usage, default: Megabytes");
params.addParam<MooseEnum>("mem_units",
MemoryUtils::getMemUnitsEnum(),
"The unit prefix used to report memory usage, default: Mebibytes");
return params;
}

VectorMemoryUsage::VectorMemoryUsage(const InputParameters & parameters)
: GeneralVectorPostprocessor(parameters),
MemoryUsageReporter(this),
_mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnit>()),
_mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnits>()),
_col_hardware_id(declareVector("hardware_id")),
_col_total_ram(declareVector("total_ram")),
_col_physical_mem(declareVector("physical_mem")),
@@ -57,7 +57,8 @@ VectorMemoryUsage::VectorMemoryUsage(const InputParameters & parameters)
{
std::copy(_hardware_id.begin(), _hardware_id.end(), _col_hardware_id.begin());
for (std::size_t i = 0; i < _nrank; ++i)
_col_total_ram[i] = _hardware_memory_total[_hardware_id[i]];
_col_total_ram[i] =
MemoryUtils::convertBytes(_hardware_memory_total[_hardware_id[i]], _mem_units);
}
}

@@ -73,7 +74,6 @@ VectorMemoryUsage::execute()
{
MemoryUtils::Stats stats;
MemoryUtils::getMemoryStats(stats);
MemoryUtils::getMemoryStats(stats, _mem_units);

if (_report_peak_value)
{
@@ -112,4 +112,11 @@ VectorMemoryUsage::finalize()
_col_node_utilization[i] = physical_per_node[_hardware_id[i]] /
static_cast<Real>(_hardware_memory_total[_hardware_id[i]]);
}

// unit prefix conversion
for (std::size_t i = 0; i < _nrank; ++i)
{
_col_physical_mem[i] = MemoryUtils::convertBytes(_col_physical_mem[i], _mem_units);
_col_virtual_mem[i] = MemoryUtils::convertBytes(_col_virtual_mem[i], _mem_units);
}
}

0 comments on commit 8819d4e

Please sign in to comment.
You can’t perform that action at this time.