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

[Core] Adding system information inspired from CPPBenchmark #12878

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
7 changes: 5 additions & 2 deletions kratos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ set( KRATOS_VERSION_SOURCES
## Kratos testing engine sources
file(GLOB_RECURSE KRATOS_CORE_TESTING_ENGINE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/testing/*.cpp)

## Kratos I/o sources
## Kratos I/O sources
file(GLOB_RECURSE KRATOS_CORE_INPUT_OUTPUT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/input_output/*.cpp)
file(GLOB KRATOS_EXPRESSION_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/expression/*.cpp)

Expand Down Expand Up @@ -187,7 +187,10 @@ if(CMAKE_UNITY_BUILD MATCHES ON)
set_source_files_properties (${CMAKE_CURRENT_SOURCE_DIR}/utilities/exact_mortar_segmentation_utility.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION TRUE)
set_source_files_properties (${CMAKE_CURRENT_SOURCE_DIR}/testing/distributed_test_case.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION TRUE)

## this cannot be compiled with unity build because calculate_discontinuous_distance_to_skin_process.cpp does implicit instantiation before specialization
## This is giving issues in Windows Unity build
set_source_files_properties (${CMAKE_CURRENT_SOURCE_DIR}/sources/system_information.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION TRUE)

## This cannot be compiled with unity build because calculate_discontinuous_distance_to_skin_process.cpp does implicit instantiation before specialization
set_source_files_properties (${CMAKE_CURRENT_SOURCE_DIR}/processes/find_global_nodal_entity_neighbours_process.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION TRUE)

## The following are conflicting due to KratosComponents
Expand Down
6 changes: 3 additions & 3 deletions kratos/includes/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#include "includes/exception.h"

// Defining the OS
#if defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
#define KRATOS_COMPILED_IN_LINUX
#elif defined(__APPLE__) && defined(__MACH__)
#if defined(__APPLE__) && defined(__MACH__)
#define KRATOS_COMPILED_IN_OS
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) || defined(unix) || defined(__unix) || defined(__unix__)
#define KRATOS_COMPILED_IN_LINUX
#elif defined(_WIN32) || defined(_WIN64)
#define KRATOS_COMPILED_IN_WINDOWS
#endif
Expand Down
5 changes: 5 additions & 0 deletions kratos/includes/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ class KRATOS_API(KRATOS_CORE) Kernel {

void PrintParallelismSupportInfo() const;

/**
* @brief This method prints all the system hardware information
*/
void PrintSystemInfo() const;

///@}
private:
///@name Static Member Variables
Expand Down
167 changes: 167 additions & 0 deletions kratos/includes/system_information.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Vicente Mataix Ferrandiz
// The implementations come from CppBenchmark, with MIT license, see https://github.com/chronoxor/CppBenchmark/
//

#pragma once

// System includes

// External includes

// Project includes
#include "includes/define.h"

namespace Kratos
{
///@addtogroup KratosCore
///@{

///@}
///@name Kratos Classes
///@{

/**
* @class SystemInformation
* @ingroup KratosCore
* @brief This class provides system information
* @details The implementations come from CppBenchmark, with MIT license, see https://github.com/chronoxor/CppBenchmark/
* @author Vicente Mataix Ferrandiz
*/
class KRATOS_API(KRATOS_CORE) SystemInformation
{
public:
///@name Type Definitions
///@{

/// Pointer definition of SystemInformation
KRATOS_CLASS_POINTER_DEFINITION(SystemInformation);

///@}
///@name Life Cycle
///@{

/**
* @brief Default constructor.
*/
SystemInformation() = default;

/**
* @brief Destructor.
*/
virtual ~SystemInformation() = default;

///@}
///@name Operations
///@{

/**
* @brief Get OS version string
* @return The OS version considered
*/
static std::string OSVersion();

/**
* @brief CPU architecture string
* @return The CPU architecture of the current machine
*/
static std::string CPUArchitecture();

/**
* @brief CPU logical cores count
* @return The number of logical CPU cores
*/
static std::size_t CPULogicalCores();

/**
* @brief CPU physical cores count
* @return The number of physical CPU cores
*/
static std::size_t CPUPhysicalCores();

/**
* @brief CPU logical and physical cores count
* @return The number of logical and physical CPU cores
*/
static std::pair<int, int> CPUTotalCores();

/**
* @brief CPU clock speed in Hz
* @return The CPU clock speed
*/
static int64_t CPUClockSpeed();

/**
* @brief Is CPU Hyper-Threading enabled?
* @return If the hyperthreading is enabled
*/
static bool CPUHyperThreading();

/**
* @brief Total RAM in bytes
* @return The total RAM in bytes
*/
static int64_t RamTotal();

/**
* @brief Free RAM in bytes
* @return The free RAM in bytes
*/
static int64_t RamFree();

/**
* @brief Generate clock speed string
* @details Will return a pretty string of Hz, kHz, MHz, GHz based on the given clock speed in hertz.
* @param Hertz Clock speed value in hertz
* @return String with clock speed representation
*/
static std::string GenerateClockSpeed(const int64_t Hertz);

/**
* @brief Generate data size string
* @details Will return a pretty string of bytes, KiB, MiB, GiB, TiB based on the given bytes.
* @param Bytes Data size in bytes
* @return String with data size representation
*/
static std::string GenerateDataSize(const int64_t Bytes);

///@}
///@name Input and output
///@{

/// Turn back information as a string.
std::string Info() const
{
std::stringstream buffer;
buffer << "SystemInformation" ;

return buffer.str();
}

/// Print information about this object.
void PrintInfo(std::ostream& rOStream) const
{
rOStream << "SystemInformation";
}

/// Print object's data.
void PrintData(std::ostream& rOStream) const
{
rOStream << "SystemInformation class";
}

///@}
}; // class SystemInformation
///@}

///@} addtogroup block

} // namespace Kratos
18 changes: 18 additions & 0 deletions kratos/sources/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
#include "includes/kratos_version.h"
#include "includes/data_communicator.h"
#include "includes/parallel_environment.h"
#include "includes/system_information.h"
#include "includes/registry.h"
#include "input_output/logger.h"
#include "utilities/parallel_utilities.h"
#include "utilities/color_utilities.h"

namespace Kratos {

Expand Down Expand Up @@ -55,6 +57,8 @@ void Kernel::PrintInfo() {
<< " Compiled for " << Kernel::OSName() << " and " << Kernel::PythonVersion() << " with " << Kernel::Compiler() << std::endl;

PrintParallelismSupportInfo();

PrintSystemInfo();
}

void Kernel::Initialize() {
Expand Down Expand Up @@ -254,6 +258,20 @@ void Kernel::PrintParallelismSupportInfo() const
}
}

void Kernel::PrintSystemInfo() const
{
const std::string hyper_threading = SystemInformation::CPUHyperThreading() ? "Enabled" : "Disabled";
KRATOS_INFO("") << BOLDFONT(FWHT("System information:")) << RST << "\n"
<< ITAFONT(FWHT("\tOS version:\t\t")) << KCYN << SystemInformation::OSVersion() << RST << "\n"
<< ITAFONT(FWHT("\tCPU architecture:\t")) << KGRN << SystemInformation::CPUArchitecture() << RST << "\n"
<< ITAFONT(FWHT("\tCPU logical cores:\t")) << KGRN << std::to_string(SystemInformation::CPULogicalCores()) << RST << "\n"
<< ITAFONT(FWHT("\tCPU physical cores:\t")) << KGRN << std::to_string(SystemInformation::CPUPhysicalCores()) << RST << "\n"
<< ITAFONT(FWHT("\tCPU clock speed:\t")) << KGRN << SystemInformation::GenerateClockSpeed(SystemInformation::CPUClockSpeed()) << RST << "\n"
<< ITAFONT(FWHT("\tCPU Hyper-Threading:\t")) << KGRN << hyper_threading << RST << "\n"
<< ITAFONT(FWHT("\tRAM total:\t\t")) << KYEL << SystemInformation::GenerateDataSize(SystemInformation::RamTotal()) << RST << "\n"
<< ITAFONT(FWHT("\tRAM free:\t\t")) << KYEL << SystemInformation::GenerateDataSize(SystemInformation::RamFree()) << RST << std::endl;
}

bool Kernel::mIsDistributedRun = false;
std::string Kernel::mPyVersion = std::string("Undefined");

Expand Down
Loading