CUDA Programming in VS Code with CMake

The development method is to use VS Code on a Windows host to remotely connect to an Ubuntu host. Due to the VS Code extension Nsight Visual Studio Code Edition only being effective on Linux, the GPU is installed on the Ubuntu host. Development and debugging are done by connecting to the remote Ubuntu host via VS Code SSH.

Installation

Install CUDA Toolkit, refer to CUDA Toolkit 12.1 Downloads

wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda_12.1.0_530.30.02_linux.run
sudo sh cuda_12.1.0_530.30.02_linux.run

Install GCC toolchain

sudo apt install build-essential

Install CMake

wget https://cmake.org/files/v3.28/cmake-3.28.0.tar.gz
tar -xvzf cmake-3.28.0.tar.gz
cd cmake-3.28.0
sudo ./configure
sudo make -j8
sudo make install

Install CMake Tools extension in VS Code.

Configure VS Code Workspace

Reference configuration for CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(cuda_test CUDA)

set(CMAKE_CUDA_STANDARD 17)

link_directories(${LIB_DIR})

add_executable(cuda_test main.cu)

set_target_properties(cuda_test PROPERTIES
        CUDA_SEPARABLE_COMPILATION ON)

Set the build configuration in CMake Tools.

Click the button to compile and run directly.

Add launch.json debug configuration under the .vscode folder:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CUDA C++: Launch",
            "type": "cuda-gdb",
            "request": "launch",
            "program": "${workspaceFolder}/build/cuda_test"
        },
        {
            "name": "CUDA C++: Attach",
            "type": "cuda-gdb",
            "request": "attach"
        }
    ]
}

Proceed with debugging: