Build CUDA C++ Program using CMake

Build CUDA C++ Program using CMake

CMake is a tool which uses a configuration file called CMakeLists.txt for generating standard build files such as makefiles on Unix systems, Visual Studio project files on Windows, etc. When it comes to CUDA C++ programming, CMake can be used to configure and generate build scripts for building CUDA applications. This tutorial provides the steps to set up a CUDA C++ program using CMake.

Prepare environment

Before starting, make sure you have installed CUDA, CMake and C++ compiler (g++ or Visual C++) or your system.

Build CUDA C++ program

  • Step 1: Create a new C++ project

Create a new directory for CUDA C++ project. For example, let's create a directory called test_cuda for a simple project that determines the number of CUDA devices in the system. Once the directory is created, navigate to it.

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

Inside the project directory, create a CMakeLists.txt file. This file will contain the instructions for CMake to configure and generate the build scripts.

test_cuda/CMakeLists.txt

cmake_minimum_required(VERSION 3.25)
project(myapp)

set(CMAKE_CXX_STANDARD 17)

find_package(CUDAToolkit REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} CUDA::cudart)

The find_package command searches the CUDAToolkit package installed on the system. The REQUIRED keyword indicates that the package is mandatory for building the project.

The add_executable command specifies the source files that should be compiled to create an executable.

The target_link_libraries command specifies the libraries that should be linked with the target executable. In this case, we link the CUDA::cudart target, which is provided by the CUDAToolkit package. The target represents the CUDA runtime library.

  • Step 3: Create source file

Inside the project directory, create main.cpp source file.

test_cuda/main.cpp

#include <iostream>
#include <cuda_runtime.h>

int main()
{
    int deviceCount;
    cudaGetDeviceCount(&deviceCount);
    std::cout << deviceCount << std::endl;

    return 0;
}

The code uses the cudaGetDeviceCount function from the CUDA runtime API to retrieve the number of CUDA devices in the system and prints it to the console.

  • Step 4: Configure the project with CMake

Inside the project directory, run the following CMake command to generate the build scripts and configure the project.

cmake -S . -B build

The -S option specifies the source directory for the CMake project. In this case, it is set to the current directory.

The -B option specifies the build directory for the CMake project. In this case, it is set to a directory named build. It will be created automatically if it does not exist.

  • Step 5: Build the project with CMake

Run the following CMake command to build a project.

cmake --build build

The --build option indicates that CMake should perform a build operation in the specified directory. In this case, it is build directory.

  • Step 6: Run the program

Once the project is built successfully, you can run the program as follows:

./build/myapp

The output might look like this:

1

Leave a Comment

Cancel reply

Your email address will not be published.