├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── run.sh └── src ├── gpu.cu ├── gpu.hpp └── main.cc /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | 3 | # Prerequisites 4 | *.d 5 | 6 | # Compiled Object files 7 | *.slo 8 | *.lo 9 | *.o 10 | *.obj 11 | 12 | # Precompiled Headers 13 | *.gch 14 | *.pch 15 | 16 | # Compiled Dynamic libraries 17 | *.so 18 | *.dylib 19 | *.dll 20 | 21 | # Fortran module files 22 | *.mod 23 | *.smod 24 | 25 | # Compiled Static libraries 26 | *.lai 27 | *.la 28 | *.a 29 | *.lib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | message(STATUS "CMake version: ${CMAKE_VERSION}") 3 | 4 | project(diana) 5 | 6 | option(USE_CUDA "Use CUDA" ON) 7 | 8 | file(GLOB cpu_source_files "${CMAKE_SOURCE_DIR}/src/*.cc") 9 | file(GLOB gpu_source_files "${CMAKE_SOURCE_DIR}/src/*.cu") 10 | 11 | if( ("${CMAKE_VERSION}" VERSION_EQUAL 3.8) OR 12 | ("${CMAKE_VERSION}" VERSION_GREATER 3.8) ) 13 | # Modern CMake 14 | if(USE_CUDA) 15 | enable_language("CUDA") 16 | add_executable(diana ${cpu_source_files} ${gpu_source_files}) 17 | else(USE_CUDA) 18 | add_executable(diana ${cpu_source_files}) 19 | endif() 20 | else() 21 | # Old CMake 22 | add_executable(diana ${cpu_source_files}) 23 | if(USE_CUDA) 24 | find_package(CUDA 8.0) 25 | if(NOT CUDA_FOUND) 26 | message(STATUS "CUDA not found") 27 | set(USE_CUDA OFF) 28 | else() 29 | CUDA_ADD_LIBRARY(diana_gpu ${gpu_source_files}) 30 | target_link_libraries(diana diana_gpu) 31 | endif() 32 | endif() 33 | endif() 34 | 35 | message(STATUS "USE_CUDA: ${USE_CUDA}") 36 | if(USE_CUDA) 37 | add_definitions(-DUSE_CUDA) 38 | if ("${CMAKE_CUDA_COMPILER_TOOLKIT_VERSION}" VERSION_LESS 9.0) 39 | add_definitions(-DOLD_CUDA) 40 | endif() 41 | endif() 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Luke Yeager 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CUDA with CMake 3.8 2 | 3 | With a super-recent build of CMake (https://gitlab.kitware.com/cmake/cmake/merge_requests/949), try this: 4 | ``` 5 | ./run.sh -DCMAKE_VERBOSE_MAKEFILE=On -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CUDA_COMPILER_LAUNCHER=ccache 6 | ``` 7 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | set -x 4 | 5 | cd "$( dirname "${BASH_SOURCE[0]}" )" 6 | 7 | rm -rf build 8 | mkdir build 9 | cd build 10 | cmake .. $@ 11 | make 12 | ./diana 13 | -------------------------------------------------------------------------------- /src/gpu.cu: -------------------------------------------------------------------------------- 1 | #include 2 | #include "gpu.hpp" 3 | 4 | void printCudaVersion() 5 | { 6 | #ifdef OLD_CUDA 7 | std::cout << "CUDA Compiled version: " << __CUDACC_VER__ << std::endl; 8 | #else 9 | std::cout << "CUDA Compiled version: " << __CUDACC_VER_MAJOR__ * 10000 + __CUDACC_VER_MAJOR__ * 100 + __CUDACC_VER_BUILD__ << std::endl; 10 | #endif 11 | 12 | int runtime_ver; 13 | cudaRuntimeGetVersion(&runtime_ver); 14 | std::cout << "CUDA Runtime version: " << runtime_ver << std::endl; 15 | 16 | int driver_ver; 17 | cudaDriverGetVersion(&driver_ver); 18 | std::cout << "CUDA Driver version: " << driver_ver << std::endl; 19 | } 20 | -------------------------------------------------------------------------------- /src/gpu.hpp: -------------------------------------------------------------------------------- 1 | void printCudaVersion(); 2 | -------------------------------------------------------------------------------- /src/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifdef USE_CUDA 4 | #include "gpu.hpp" 5 | #endif 6 | 7 | int main() 8 | { 9 | std::cout << "Hello, world!" << std::endl; 10 | 11 | #ifdef USE_CUDA 12 | std::cout << "CUDA: On" << std::endl; 13 | printCudaVersion(); 14 | #else 15 | std::cout << "CUDA: Off" << std::endl; 16 | #endif 17 | 18 | return 0; 19 | } 20 | --------------------------------------------------------------------------------