Build NVML C++ Program using CMake

Build NVML C++ Program using CMake

NVIDIA Management Library (NVML) is a powerful tool that can help to gather information about GPU utilization, temperatures, power consumption, and much more. CMake can be used to configure and generate build scripts for building NVML applications. This tutorial provides the steps to set up a NVML C++ program using CMake.

Prepare environment

Before starting, it's essential to ensure that you have CUDA installed on the system. It's worth noting that NVML comes bundled with CUDA. Additionally, make certain that you have CMake and a C++ compiler (g++ or Visual C++) installed on the system. These tools are essential prerequisites for the tasks ahead.

Build NVML C++ program

  • Step 1: Create a new C++ project

Create a new directory for the NVML C++ project. For instance, let's call it test_nvml for a basic project that counts the number of NVIDIA GPU devices in the system. After creating the directory, go to it.

mkdir test_nvml && cd test_nvml
  • Step 2: Create CMakeLists.txt file

Within the project directory, establish a CMakeLists.txt file. This file will house the directives for CMake to configure and produce the necessary build scripts.

test_nvml/CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(myapp)

set(CMAKE_CXX_STANDARD 17)

find_package(CUDAToolkit REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} CUDA::nvml)

The find_package command instructs CMake to find and include the CUDA Toolkit as a required dependency for the project. The REQUIRED keyword ensures that if CUDAToolkit is not found on the system, CMake will terminate the configuration process with an error.

The add_executable command defines the source files that will be compiled to generate an executable program.

The target_link_libraries command instructs CMake to link the executable with the NVML library from the CUDA Toolkit. CUDA::nvml is a CMake target that represents the NVML library, and by linking it, the program gains access to NVML functionality.

  • Step 3: Create source file

In the project directory, create a source file named main.cpp.

test_nvml/main.cpp

#include <iostream>
#include <nvml.h>

int main()
{
    nvmlInit();

    uint32_t deviceCount;
    nvmlDeviceGetCount(&deviceCount);
    std::cout << deviceCount << std::endl;

    nvmlShutdown();

    return 0;
}

The code uses the nvmlDeviceGetCount function to retrieve the number of available NVIDIA GPU devices in the system and prints it to the console.

  • Step 4: Configure the project with CMake

In the project directory, execute the following CMake command to configure the project and generate the necessary build scripts.

cmake -S . -B build

The -S option specifies the source directory where the CMakeLists.txt file is located. The . (dot) indicates the current directory.

The -B option specifies the build directory where CMake will generate build files. In this case, the build directory will be created.

  • Step 5: Build the project with CMake

Execute the following CMake command to start the project's build process.

cmake --build build

The --build option means that CMake is instructed to execute a build operation in the specified directory, which, in this instance, is the build directory.

  • Step 6: Run the program

Upon successful completion of the project build, you can execute the program in the following manner:

./build/myapp

The output might look like this:

1

Leave a Comment

Cancel reply

Your email address will not be published.