Merge pull request #148 from robertmaynard/support_nvml_with_static_builds

Support static builds of nvbench with nvml enabled.
This commit is contained in:
Allison Vacanti
2023-11-15 09:52:35 -05:00
committed by GitHub
5 changed files with 55 additions and 42 deletions

View File

@@ -21,6 +21,18 @@ macro(nvbench_generate_exports)
) )
endif() endif()
if (TARGET nvbench_json)
set(nvbench_json_code_block
[=[
add_library(nvbench_json INTERFACE IMPORTED)
if (TARGET nlohmann_json::nlohmann_json)
target_link_libraries(nvbench_json INTERFACE nlohmann_json::nlohmann_json)
endif()
]=])
string(APPEND nvbench_build_export_code_block ${nvbench_json_code_block})
string(APPEND nvbench_install_export_code_block ${nvbench_json_code_block})
endif()
rapids_export(BUILD NVBench rapids_export(BUILD NVBench
EXPORT_SET nvbench-targets EXPORT_SET nvbench-targets
NAMESPACE "nvbench::" NAMESPACE "nvbench::"

View File

@@ -24,16 +24,14 @@ set(srcs
detail/measure_cold.cu detail/measure_cold.cu
detail/measure_hot.cu detail/measure_hot.cu
detail/state_generator.cxx detail/state_generator.cxx
internal/nvml.cxx
) )
if (NVBench_ENABLE_CUPTI) if (NVBench_ENABLE_CUPTI)
list(APPEND srcs detail/measure_cupti.cu cupti_profiler.cxx) list(APPEND srcs detail/measure_cupti.cu cupti_profiler.cxx)
endif() endif()
if (NVBench_ENABLE_NVML)
list(APPEND srcs internal/nvml.cxx)
endif()
# CUDA 11.0 can't compile json_printer without crashing # CUDA 11.0 can't compile json_printer without crashing
# So for that version fall back to C++ with degraded # So for that version fall back to C++ with degraded
# output ( no PTX version info ) # output ( no PTX version info )

View File

@@ -45,6 +45,9 @@ device_info::device_info(int id)
, m_nvml_device(nullptr) , m_nvml_device(nullptr)
{ {
NVBENCH_CUDA_CALL(cudaGetDeviceProperties(&m_prop, m_id)); NVBENCH_CUDA_CALL(cudaGetDeviceProperties(&m_prop, m_id));
// NVML's lifetime should extend for the entirety of the process, so store in a
// global.
[[maybe_unused]] static auto nvml_lifetime = nvbench::nvml::NVMLLifetimeManager();
#ifdef NVBENCH_HAS_NVML #ifdef NVBENCH_HAS_NVML
// Retrieve the current device's pci_id as a null-terminated string. // Retrieve the current device's pci_id as a null-terminated string.

View File

@@ -32,6 +32,16 @@
namespace nvbench::nvml namespace nvbench::nvml
{ {
// RAII struct that initializes and shuts down NVML
// Needs to be constructed and kept alive while using nvml
struct NVMLLifetimeManager
{
NVMLLifetimeManager();
~NVMLLifetimeManager();
private:
bool m_inited{false};
};
/// Base class for NVML-specific exceptions /// Base class for NVML-specific exceptions
struct error : std::runtime_error struct error : std::runtime_error
{ {

View File

@@ -16,56 +16,46 @@
* limitations under the License. * limitations under the License.
*/ */
#include <nvbench/internal/nvml.cuh>
#include <nvbench/config.cuh> #include <nvbench/config.cuh>
#include <nvbench/internal/nvml.cuh>
#include <fmt/format.h>
#include <nvml.h>
#include <stdexcept> #include <stdexcept>
namespace #include <fmt/format.h>
{ #include <nvml.h>
// RAII struct that initializes and shuts down NVML namespace nvbench::nvml
struct NVMLLifetimeManager
{ {
NVMLLifetimeManager() NVMLLifetimeManager::NVMLLifetimeManager()
{
#ifdef NVBENCH_HAS_NVML
try
{
NVBENCH_NVML_CALL_NO_API(nvmlInit());
m_inited = true;
}
catch (std::exception &e)
{
fmt::print("NVML initialization failed:\n {}", e.what());
}
#endif
}
NVMLLifetimeManager::~NVMLLifetimeManager()
{
#ifdef NVBENCH_HAS_NVML
if (m_inited)
{ {
try try
{ {
NVBENCH_NVML_CALL_NO_API(nvmlInit()); NVBENCH_NVML_CALL_NO_API(nvmlShutdown());
m_inited = true;
} }
catch (std::exception &e) catch (std::exception &e)
{ {
fmt::print("NVML initialization failed:\n {}", e.what()); fmt::print("NVML shutdown failed:\n {}", e.what());
} }
} }
#endif
}
~NVMLLifetimeManager() } // namespace nvbench::nvml
{
if (m_inited)
{
try
{
NVBENCH_NVML_CALL_NO_API(nvmlShutdown());
}
catch (std::exception &e)
{
fmt::print("NVML shutdown failed:\n {}", e.what());
}
}
}
private:
bool m_inited{false};
};
// NVML's lifetime should extend for the entirety of the process, so store in a
// global.
auto nvml_lifetime = NVMLLifetimeManager{};
} // namespace