Cleanup, extra CUDA info

parent 9d050021
Pipeline #19845248 passed with stage
in 2 minutes and 22 seconds
......@@ -22,6 +22,11 @@ Folders can be nested with `/`. You can control how files show up in each folder
source_group("Source Files" REGULAR_EXPRESSION ".*\\.c[ucp]p?")
```
Take a look at [this blog post][sorting] for tips on ways to automatically sort folders to match your source code layout.
## Running with an IDE
To use an IDE, either pass `-G"name of IDE" if CMake can produce that IDE's files (like Xcode, Visual Studio), or open the CMakeLists.txt file from your IDE if that IDE has built in support for CMake (CLion, QtCreator, many others).
[sorting]: http://blog.audio-tk.com/2015/09/01/sorting-source-files-and-projects-in-folders-with-cmake-and-visual-studioxcode/
# CUDA (in progress)
CUDA support is available in two flavors. The new method, introduced in CMake 3.8 (3.9 for Windows), will be what I focus on first. The old method will be covered afterwards, but as you'll see, it's uglier and harder to get right. I'd stick to requiring CMake 3.8 or 3.9 for CUDA.
CUDA support is available in two flavors. The new method, introduced in CMake 3.8 (3.9 for Windows), will be what I focus on first. The old method will be covered afterwards, but as you'll see, it's uglier and harder to get right. I'd stick to requiring CMake 3.8 or 3.9 for CUDA (and CMake 3.11 for IDEs like Xcode and Visual Studio).
A good resource for CUDA and Modern CMake is [this talk](http://on-demand.gputechconf.com/gtc/2017/presentation/S7438-robert-maynard-build-systems-combining-cuda-and-machine-learning.pdf) by CMake developer Robert Maynard at GTC 2017.
......@@ -23,6 +23,15 @@ You'll probably want `CXX` listed here also. And, if CUDA is optional, you'll wa
enable_language(CUDA)
```
To check to see if CUDA is available, use CheckLanuage:
```cmake
include(CheckLanguage)
check_language(CUDA)
```
You can check the version of the NVCC toolkit with `CMAKE_CUDA_COMPILER_VERSION` (for now, only NVCC is supported, but just to be sure, check `CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA"`).
### Variables for CUDA
Many variables with `CXX` in the name have a CUDA version with `CUDA` instead. For example, to set the C++ standard required for CUDA,
......@@ -46,17 +55,17 @@ set_target_properties(mylib PROPERTIES
CUDA_SEPERABLE_COMPILATION ON)
```
You can also direcly make a PTX file with the `CUDA_PTX_COMPILATION` property.
You can also direclty make a PTX file with the `CUDA_PTX_COMPILATION` property.
### Working with targets
Using targets should work similarly to CXX, but there's a problem. If you include a target that includes compiler options (flags), most of the time, the options will not be protected by the correct includes (and the chances of them having the correct CUDA wrapper is even smaller). Here's what a correct compiler options line should look like:
```cmake
"$<$<BUILD_INTERFACE:$<COMPILE_LANGUAGE:CXX>>:-fopenmp>$<$<BUILD_INTERFACE:$<COMPILE_LANGUAGE:CUDA>>:-Xcompiler=-fopenmp>"
```
However, if you using almost any find_package, and using the Modern CMake methods of targets and inheritence, everything will break. I've learned that the hard way.
However, if you using almost any find_package, and using the Modern CMake methods of targets and inheritance, everything will break. I've learned that the hard way.
For now, here's a pretty reasonable solution, _as long as you know the un-aliased target name_. It's a function that will fix a C++ only target by wrapping the flags if using a CUDA compiler:
......@@ -72,11 +81,38 @@ function(CUDA_CONVERT_FLAGS EXISTING_TARGET)
endfunction()
```
### Useful variables
* `CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES`: Place for built-in Thrust, etc
* `CMAKE_CUDA_COMPILER`: NVCC with location
> ### Note that FindCUDA is deprecated, but for now, the following functions require FindCUDA:
>
> * CUDA version checks / picking a version
> * Architecture detection
> * Architecture detection (Note: 3.12 will fix this at least partially)
> * Linking to CUDA libraries from non-.cu files
## Method 2: FindCUDA
If you want to support an older version of CMake, I recommend at least including the FindCUDA from CMake version 3.9 in your cmake folder (see the CLIUtils github organization for a [git repository](https://github.com/CLIUtils/cuda_support)). You'll want two features that were added: `CUDA_LINK_LIBRARIES_KEYWORD` and `cuda_select_nvcc_arch_flags`, along with the newer architectures and CUDA versions.
To use the old CUDA support, you use `find_package`:
```cmake
find_package(CUDA 7.0 REQUIRED)
message(STATUS "Found CUDA ${CUDA_VERSION_STRING} at ${CUDA_TOOLKIT_ROOT_DIR}")
```
You can control the CUDA flags with `CUDA_NVCC_FLAGS` (list append) and you can control separable compilation with `CUDA_SEPARABLE_COMPILATION`. You'll also want to make sure CUDA plays nice and adds keywords to the targets (CMake 3.9+):
```cmake
set(CUDA_LINK_LIBRARIES_KEYWORD PUBLIC)
```
You'll also might want to allow a user to check for the arch flags of their current hardware:
```cmake
cuda_select_nvcc_arch_flags(ARCH_FLAGS) # optional argument for arch to add
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment