Skip to content

Commit

Permalink
Add 'CUDA' awareness
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSchofield committed Jul 19, 2024
1 parent b5a4880 commit 71186d3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
matrix:
configuration: [ Debug, Release ]
buildPreset: [ windows-msvc-x64, windows-msvc-spectre-x64, windows-msvc-amd64, windows-msvc-x86, windows-msvc-arm64, windows-clang-x64, windows-clang-amd64, windows-clangcl-x64, windows-clangcl-amd64 ]
include:
- buildPreset: windows-msvc-x64
cuda: true
runs-on: windows-latest
steps:
- name: Checkout
Expand All @@ -40,6 +43,17 @@ jobs:
shell: pwsh
run: |
choco install ninja
- name: Install NVIDIA Cuda Toolkit
if: ${{ matrix.cuda }}
shell: pwsh
run: |
choco install cuda
# The installation sets the CUDA_PATH environment variable - use 'Update-SessionEnvironment' to pull it into
# the current process' environment block, then add it to the '$env:GITHUB_ENV' file for subsequent steps.
Import-Module "$env:ChocolateyInstall/helpers/chocolateyInstaller.psm1"
Update-SessionEnvironment
"CUDA_PATH=$env:CUDA_PATH" >> $env:GITHUB_ENV
- name: CMake Configure ${{ matrix.buildPreset }}
shell: pwsh
run: |
Expand Down
15 changes: 15 additions & 0 deletions Windows.MSVC.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
# | MSVC | 1 |
# | MSVC_VERSION | The '<major><minor>' version of the C++ compiler being used. For example, '1929' |
#
# Other configuration:
#
# * If the 'CMAKE_CUDA_COMPILER' is set, and 'CMAKE_CUDA_HOST_COMPILER' is not set, and ENV{CUDAHOSTCXX} not defined
# then 'CMAKE_CUDA_HOST_COMPILER' is set to the value of 'CMAKE_CXX_COMPILER'.
#
# Resources:
# <https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html>
#
Expand Down Expand Up @@ -219,3 +224,13 @@ endif()

# Windows Kits
include("${CMAKE_CURRENT_LIST_DIR}/Windows.Kits.cmake")

# CUDA support
#
# If a CUDA compiler is specified, and a host compiler wasn't specified, set 'CMAKE_CXX_COMPILER'
# as the host compiler.
if(CMAKE_CUDA_COMPILER)
if((NOT CMAKE_CUDA_HOST_COMPILER) AND (NOT DEFINED ENV{CUDAHOSTCXX}))
set(CMAKE_CUDA_HOST_COMPILER "${CMAKE_CXX_COMPILER}")
endif()
endif()
12 changes: 12 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#----------------------------------------------------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.20)

if(DEFINED ENV{CUDA_PATH})
set(CMAKE_CUDA_COMPILER "$ENV{CUDA_PATH}/bin/nvcc.exe")
endif()

project(WindowsToolchainExample)

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
Expand All @@ -20,6 +24,14 @@ add_subdirectory(CommandLineC)
add_subdirectory(SharedLibrary)
add_subdirectory(WindowsApplication)

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
if(DEFINED ENV{CUDA_PATH})
add_subdirectory(CommandLineCuda)
else()
message(WARNING "NVIDIA Cuda Toolkit not found - please make sure that the 'CUDA_PATH' environment variable is set.")
endif()
endif()

if((CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") AND VS_EXPERIMENTAL_MODULE)
if((CMAKE_SYSTEM_PROCESSOR STREQUAL x64) OR (CMAKE_SYSTEM_PROCESSOR STREQUAL AMD64) OR (CMAKE_SYSTEM_PROCESSOR STREQUAL x86))
add_subdirectory(CommandLineModule)
Expand Down
8 changes: 8 additions & 0 deletions example/CommandLineCuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#----------------------------------------------------------------------------------------------------------------------
#
#----------------------------------------------------------------------------------------------------------------------
project(CommandLineCuda LANGUAGES CXX CUDA)

add_executable(CommandLineCuda
main.cu
)
28 changes: 28 additions & 0 deletions example/CommandLineCuda/main.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//---------------------------------------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------------------------------------
#include <cuda.h>
#include <cuda_runtime.h>
#include <exception>
#include <iostream>

int main(int, char**)
{
try
{
int deviceCount = 0;
cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
if (error_id != cudaSuccess)
{
std::cerr << "cudaGetDeviceCount returned " << static_cast<int>(error_id) << ".\n"
<< " -> " << cudaGetErrorString(error_id) << "\n";
exit(EXIT_FAILURE);
}

std::cout << "Detected " << deviceCount << " CUDA capable device(s)\n";
}
catch (const std::exception& ex)
{
std::cerr << "Exception: " << ex.what();
}
}

0 comments on commit 71186d3

Please sign in to comment.