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

Support static builds of nvbench with nvml enabled. #148

Merged
merged 2 commits into from
Nov 15, 2023
Merged
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
12 changes: 12 additions & 0 deletions cmake/NVBenchExports.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ macro(nvbench_generate_exports)
)
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
EXPORT_SET nvbench-targets
NAMESPACE "nvbench::"
Expand Down
6 changes: 2 additions & 4 deletions nvbench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ set(srcs
detail/measure_cold.cu
detail/measure_hot.cu
detail/state_generator.cxx

internal/nvml.cxx
)

if (NVBench_ENABLE_CUPTI)
list(APPEND srcs detail/measure_cupti.cu cupti_profiler.cxx)
endif()

if (NVBench_ENABLE_NVML)
list(APPEND srcs internal/nvml.cxx)
endif()

# CUDA 11.0 can't compile json_printer without crashing
# So for that version fall back to C++ with degraded
# output ( no PTX version info )
Expand Down
3 changes: 3 additions & 0 deletions nvbench/device_info.cu
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ device_info::device_info(int id)
, m_nvml_device(nullptr)
{
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
// Retrieve the current device's pci_id as a null-terminated string.
Expand Down
10 changes: 10 additions & 0 deletions nvbench/internal/nvml.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
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
struct error : std::runtime_error
{
Expand Down
60 changes: 25 additions & 35 deletions nvbench/internal/nvml.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,46 @@
* limitations under the License.
*/

#include <nvbench/config.cuh>
#include <nvbench/internal/nvml.cuh>

#include <nvbench/config.cuh>
#include <stdexcept>

#include <fmt/format.h>

#include <nvml.h>

#include <stdexcept>

namespace
namespace nvbench::nvml
{
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
}

// RAII struct that initializes and shuts down NVML
struct NVMLLifetimeManager
NVMLLifetimeManager::~NVMLLifetimeManager()
{
NVMLLifetimeManager()
#ifdef NVBENCH_HAS_NVML
if (m_inited)
{
try
{
NVBENCH_NVML_CALL_NO_API(nvmlInit());
m_inited = true;
NVBENCH_NVML_CALL_NO_API(nvmlShutdown());
}
catch (std::exception &e)
{
fmt::print("NVML initialization failed:\n {}", e.what());
fmt::print("NVML shutdown failed:\n {}", e.what());
}
}
#endif
}

~NVMLLifetimeManager()
{
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
} // namespace nvbench::nvml