]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - cmake/FindCUDA/select_compute_arch.cmake
Make KCF member variables constexpr where possible
[hercules2020/kcf.git] / cmake / FindCUDA / select_compute_arch.cmake
1 # Synopsis:
2 #   CUDA_SELECT_NVCC_ARCH_FLAGS(out_variable [target_CUDA_architectures])
3 #   -- Selects GPU arch flags for nvcc based on target_CUDA_architectures
4 #      target_CUDA_architectures : Auto | Common | All | LIST(ARCH_AND_PTX ...)
5 #       - "Auto" detects local machine GPU compute arch at runtime.
6 #       - "Common" and "All" cover common and entire subsets of architectures
7 #      ARCH_AND_PTX : NAME | NUM.NUM | NUM.NUM(NUM.NUM) | NUM.NUM+PTX
8 #      NAME: Fermi Kepler Maxwell Kepler+Tegra Kepler+Tesla Maxwell+Tegra Pascal
9 #      NUM: Any number. Only those pairs are currently accepted by NVCC though:
10 #            2.0 2.1 3.0 3.2 3.5 3.7 5.0 5.2 5.3 6.0 6.2
11 #      Returns LIST of flags to be added to CUDA_NVCC_FLAGS in ${out_variable}
12 #      Additionally, sets ${out_variable}_readable to the resulting numeric list
13 #      Example:
14 #       CUDA_SELECT_NVCC_ARCH_FLAGS(ARCH_FLAGS 3.0 3.5+PTX 5.2(5.0) Maxwell)
15 #        LIST(APPEND CUDA_NVCC_FLAGS ${ARCH_FLAGS})
16 #
17 #      More info on CUDA architectures: https://en.wikipedia.org/wiki/CUDA
18 #
19
20 # This list will be used for CUDA_ARCH_NAME = All option
21 set(CUDA_KNOWN_GPU_ARCHITECTURES  "Fermi" "Kepler" "Maxwell")
22
23 # This list will be used for CUDA_ARCH_NAME = Common option (enabled by default)
24 set(CUDA_COMMON_GPU_ARCHITECTURES "3.0" "3.5" "5.0")
25
26 if (CUDA_VERSION VERSION_GREATER "6.5")
27   list(APPEND CUDA_KNOWN_GPU_ARCHITECTURES "Kepler+Tegra" "Kepler+Tesla" "Maxwell+Tegra")
28   list(APPEND CUDA_COMMON_GPU_ARCHITECTURES "5.2")
29 endif ()
30
31 if (CUDA_VERSION VERSION_GREATER "7.5")
32   list(APPEND CUDA_KNOWN_GPU_ARCHITECTURES "Pascal")
33   list(APPEND CUDA_COMMON_GPU_ARCHITECTURES "6.0" "6.1" "6.1+PTX")
34 else()
35   list(APPEND CUDA_COMMON_GPU_ARCHITECTURES "5.2+PTX")
36 endif ()
37
38
39
40 ################################################################################################
41 # A function for automatic detection of GPUs installed  (if autodetection is enabled)
42 # Usage:
43 #   CUDA_DETECT_INSTALLED_GPUS(OUT_VARIABLE)
44 #
45 function(CUDA_DETECT_INSTALLED_GPUS OUT_VARIABLE)
46   if(NOT CUDA_GPU_DETECT_OUTPUT)
47     set(cufile ${PROJECT_BINARY_DIR}/detect_cuda_archs.cu)
48
49     file(WRITE ${cufile} ""
50       "#include <cstdio>\n"
51       "int main()\n"
52       "{\n"
53       "  int count = 0;\n"
54       "  if (cudaSuccess != cudaGetDeviceCount(&count)) return -1;\n"
55       "  if (count == 0) return -1;\n"
56       "  for (int device = 0; device < count; ++device)\n"
57       "  {\n"
58       "    cudaDeviceProp prop;\n"
59       "    if (cudaSuccess == cudaGetDeviceProperties(&prop, device))\n"
60       "      std::printf(\"%d.%d \", prop.major, prop.minor);\n"
61       "  }\n"
62       "  return 0;\n"
63       "}\n")
64
65     execute_process(COMMAND "${CUDA_NVCC_EXECUTABLE}" "--run" "${cufile}"
66                     WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/CMakeFiles/"
67                     RESULT_VARIABLE nvcc_res OUTPUT_VARIABLE nvcc_out
68                     ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
69
70     if(nvcc_res EQUAL 0)
71       string(REPLACE "2.1" "2.1(2.0)" nvcc_out "${nvcc_out}")
72       set(CUDA_GPU_DETECT_OUTPUT ${nvcc_out} CACHE INTERNAL "Returned GPU architetures from detect_gpus tool" FORCE)
73     endif()
74   endif()
75
76   if(NOT CUDA_GPU_DETECT_OUTPUT)
77     message(STATUS "Automatic GPU detection failed. Building for common architectures.")
78     set(${OUT_VARIABLE} ${CUDA_COMMON_GPU_ARCHITECTURES} PARENT_SCOPE)
79   else()
80     set(${OUT_VARIABLE} ${CUDA_GPU_DETECT_OUTPUT} PARENT_SCOPE)
81   endif()
82 endfunction()
83
84
85 ################################################################################################
86 # Function for selecting GPU arch flags for nvcc based on CUDA architectures from parameter list
87 # Usage:
88 #   SELECT_NVCC_ARCH_FLAGS(out_variable [list of CUDA compute archs])
89 function(CUDA_SELECT_NVCC_ARCH_FLAGS out_variable)
90   set(CUDA_ARCH_LIST "${ARGN}")
91
92   if("X${CUDA_ARCH_LIST}" STREQUAL "X" )
93     set(CUDA_ARCH_LIST "Auto")
94   endif()
95
96   set(cuda_arch_bin)
97   set(cuda_arch_ptx)
98
99   if("${CUDA_ARCH_LIST}" STREQUAL "All")
100     set(CUDA_ARCH_LIST ${CUDA_KNOWN_GPU_ARCHITECTURES})
101   elseif("${CUDA_ARCH_LIST}" STREQUAL "Common")
102     set(CUDA_ARCH_LIST ${CUDA_COMMON_GPU_ARCHITECTURES})
103   elseif("${CUDA_ARCH_LIST}" STREQUAL "Auto")
104     CUDA_DETECT_INSTALLED_GPUS(CUDA_ARCH_LIST)
105     message(STATUS "Autodetected CUDA architecture(s): ${CUDA_ARCH_LIST}")
106   endif()
107
108   # Now process the list and look for names
109   string(REGEX REPLACE "[ \t]+" ";" CUDA_ARCH_LIST "${CUDA_ARCH_LIST}")
110   list(REMOVE_DUPLICATES CUDA_ARCH_LIST)
111   foreach(arch_name ${CUDA_ARCH_LIST})
112     set(arch_bin)
113     set(add_ptx FALSE)
114     # Check to see if we are compiling PTX
115     if(arch_name MATCHES "(.*)\\+PTX$")
116       set(add_ptx TRUE)
117       set(arch_name ${CMAKE_MATCH_1})
118     endif()
119     if(arch_name MATCHES "^([0-9]\\.[0-9](\\([0-9]\\.[0-9]\\))?)$")
120       set(arch_bin ${CMAKE_MATCH_1})
121       set(arch_ptx ${arch_bin})
122     else()
123       # Look for it in our list of known architectures
124       if(${arch_name} STREQUAL "Fermi")
125         set(arch_bin 2.0 "2.1(2.0)")
126       elseif(${arch_name} STREQUAL "Kepler+Tegra")
127         set(arch_bin 3.2)
128       elseif(${arch_name} STREQUAL "Kepler+Tesla")
129         set(arch_bin 3.7)
130       elseif(${arch_name} STREQUAL "Kepler")
131         set(arch_bin 3.0 3.5)
132         set(arch_ptx 3.5)
133       elseif(${arch_name} STREQUAL "Maxwell+Tegra")
134         set(arch_bin 5.3)
135       elseif(${arch_name} STREQUAL "Maxwell")
136         set(arch_bin 5.0 5.2)
137         set(arch_ptx 5.2)
138       elseif(${arch_name} STREQUAL "Pascal")
139         set(arch_bin 6.0 6.1)
140         set(arch_ptx 6.1)
141       else()
142         message(SEND_ERROR "Unknown CUDA Architecture Name ${arch_name} in CUDA_SELECT_NVCC_ARCH_FLAGS")
143       endif()
144     endif()
145     if(NOT arch_bin)
146       message(SEND_ERROR "arch_bin wasn't set for some reason")
147     endif()
148     list(APPEND cuda_arch_bin ${arch_bin})
149     if(add_ptx)
150       if (NOT arch_ptx)
151         set(arch_ptx ${arch_bin})
152       endif()
153       list(APPEND cuda_arch_ptx ${arch_ptx})
154     endif()
155   endforeach()
156
157   # remove dots and convert to lists
158   string(REGEX REPLACE "\\." "" cuda_arch_bin "${cuda_arch_bin}")
159   string(REGEX REPLACE "\\." "" cuda_arch_ptx "${cuda_arch_ptx}")
160   string(REGEX MATCHALL "[0-9()]+" cuda_arch_bin "${cuda_arch_bin}")
161   string(REGEX MATCHALL "[0-9]+"   cuda_arch_ptx "${cuda_arch_ptx}")
162
163   if(cuda_arch_bin)
164     list(REMOVE_DUPLICATES cuda_arch_bin)
165   endif()
166   if(cuda_arch_ptx)
167     list(REMOVE_DUPLICATES cuda_arch_ptx)
168   endif()
169
170   set(nvcc_flags "")
171   set(nvcc_archs_readable "")
172
173   # Tell NVCC to add binaries for the specified GPUs
174   foreach(arch ${cuda_arch_bin})
175     if(arch MATCHES "([0-9]+)\\(([0-9]+)\\)")
176       # User explicitly specified ARCH for the concrete CODE
177       list(APPEND nvcc_flags -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1})
178       list(APPEND nvcc_archs_readable sm_${CMAKE_MATCH_1})
179     else()
180       # User didn't explicitly specify ARCH for the concrete CODE, we assume ARCH=CODE
181       list(APPEND nvcc_flags -gencode arch=compute_${arch},code=sm_${arch})
182       list(APPEND nvcc_archs_readable sm_${arch})
183     endif()
184   endforeach()
185
186   # Tell NVCC to add PTX intermediate code for the specified architectures
187   foreach(arch ${cuda_arch_ptx})
188     list(APPEND nvcc_flags -gencode arch=compute_${arch},code=compute_${arch})
189     list(APPEND nvcc_archs_readable compute_${arch})
190   endforeach()
191
192   string(REPLACE ";" " " nvcc_archs_readable "${nvcc_archs_readable}")
193   set(${out_variable}          ${nvcc_flags}          PARENT_SCOPE)
194   set(${out_variable}_readable ${nvcc_archs_readable} PARENT_SCOPE)
195 endfunction()