Skip to content

Commit

Permalink
auto detect CUDA arch
Browse files Browse the repository at this point in the history
  • Loading branch information
mp3guy committed Apr 30, 2019
1 parent e4e7217 commit 30c3837
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
44 changes: 44 additions & 0 deletions CMakeModules/CudaDetect.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Taken from https://github.com/BVLC/caffe/blob/master/cmake/Cuda.cmake
################################################################################################
# A function for automatic detection of GPUs installed (if autodetection is enabled)
# Usage:
# detect_installed_gpus(out_variable)
function(detect_installed_gpus out_variable)
if(NOT CUDA_gpu_detect_output)
set(__cufile ${PROJECT_BINARY_DIR}/detect_cuda_archs.cu)

file(WRITE ${__cufile} ""
"#include <cstdio>\n"
"int main()\n"
"{\n"
" int count = 0;\n"
" if (cudaSuccess != cudaGetDeviceCount(&count)) return -1;\n"
" if (count == 0) return -1;\n"
" for (int device = 0; device < count; ++device)\n"
" {\n"
" cudaDeviceProp prop;\n"
" if (cudaSuccess == cudaGetDeviceProperties(&prop, device))\n"
" std::printf(\"%d.%d;\", prop.major, prop.minor);\n"
" }\n"
" return 0;\n"
"}\n")

execute_process(COMMAND "${CUDA_NVCC_EXECUTABLE}" "-Wno-deprecated-gpu-targets" "--run" "${__cufile}"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/CMakeFiles/"
RESULT_VARIABLE __nvcc_res OUTPUT_VARIABLE __nvcc_out
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)

if(__nvcc_res EQUAL 0)
string(REGEX REPLACE "\\." "" __nvcc_out "${__nvcc_out}")
string(REGEX MATCHALL "[0-9;]+" __nvcc_out "${__nvcc_out}")
list(REMOVE_DUPLICATES __nvcc_out)
set(CUDA_gpu_detect_output ${__nvcc_out} CACHE INTERNAL "Returned GPU architectures from detect_gpus tool" FORCE)
endif()
endif()

if(NOT CUDA_gpu_detect_output)
message(STATUS "Automatic GPU detection failed. Is CUDA properly installed? .")
else()
set(${out_variable} ${CUDA_gpu_detect_output} PARENT_SCOPE)
endif()
endfunction()
8 changes: 7 additions & 1 deletion Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ file(GLOB shader_srcs Shaders/*.cpp)
file(GLOB cuda Cuda/*.cu)
file(GLOB containers Cuda/containers/*.cpp)

set(CUDA_ARCH_BIN "52" CACHE STRING "Specify 'real' GPU arch to build binaries for, BIN(PTX) format is supported. Example: 1.3 2.1(1.3) or 13 21(13)")
set(CUDA_ARCH_BIN "" CACHE STRING "Specify 'real' GPU arch to build binaries for, BIN(PTX) format is supported. Example: 1.3 2.1(1.3) or 13 21(13)")
set(CUDA_ARCH_PTX "" CACHE STRING "Specify 'virtual' PTX arch to build PTX intermediate code for. Example: 1.0 1.2 or 10 12")

include("${CMAKE_MODULE_PATH}/CudaDetect.cmake")
detect_installed_gpus(CUDA_NVCC_ARCHS)
foreach(NVCC_ARCH IN LISTS CUDA_NVCC_ARCHS)
list(APPEND CUDA_ARCH_BIN "${NVCC_ARCH} ")
endforeach(NVCC_ARCH)

include("${CMAKE_MODULE_PATH}/CudaComputeTargetFlags.cmake")
APPEND_TARGET_ARCH_FLAGS()

Expand Down

0 comments on commit 30c3837

Please sign in to comment.