select_compute_arch.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. # This list will be used for CUDA_ARCH_NAME = All option
  20. set(CUDA_KNOWN_GPU_ARCHITECTURES "Fermi" "Kepler" "Maxwell")
  21. # This list will be used for CUDA_ARCH_NAME = Common option (enabled by default)
  22. set(CUDA_COMMON_GPU_ARCHITECTURES "3.0" "3.5" "5.0")
  23. if (CUDA_VERSION VERSION_GREATER "6.5")
  24. list(APPEND CUDA_KNOWN_GPU_ARCHITECTURES "Kepler+Tegra" "Kepler+Tesla" "Maxwell+Tegra")
  25. list(APPEND CUDA_COMMON_GPU_ARCHITECTURES "5.2")
  26. endif ()
  27. if (CUDA_VERSION VERSION_GREATER "7.5")
  28. list(APPEND CUDA_KNOWN_GPU_ARCHITECTURES "Pascal")
  29. list(APPEND CUDA_COMMON_GPU_ARCHITECTURES "6.0" "6.1")
  30. else()
  31. list(APPEND CUDA_COMMON_GPU_ARCHITECTURES "5.2+PTX")
  32. endif ()
  33. if (CUDA_VERSION VERSION_GREATER "8.5")
  34. list(APPEND CUDA_KNOWN_GPU_ARCHITECTURES "Volta")
  35. list(APPEND CUDA_COMMON_GPU_ARCHITECTURES "7.0" "7.0+PTX")
  36. else()
  37. list(APPEND CUDA_COMMON_GPU_ARCHITECTURES "6.1+PTX")
  38. endif()
  39. ################################################################################################
  40. # A function for automatic detection of GPUs installed (if autodetection is enabled)
  41. # Usage:
  42. # CUDA_DETECT_INSTALLED_GPUS(OUT_VARIABLE)
  43. #
  44. function(CUDA_DETECT_INSTALLED_GPUS OUT_VARIABLE)
  45. if(NOT CUDA_GPU_DETECT_OUTPUT)
  46. set(file ${PROJECT_BINARY_DIR}/detect_cuda_compute_capabilities.cpp)
  47. file(WRITE ${file} ""
  48. "#include <cuda_runtime.h>\n"
  49. "#include <cstdio>\n"
  50. "int main()\n"
  51. "{\n"
  52. " int count = 0;\n"
  53. " if (cudaSuccess != cudaGetDeviceCount(&count)) return -1;\n"
  54. " if (count == 0) return -1;\n"
  55. " for (int device = 0; device < count; ++device)\n"
  56. " {\n"
  57. " cudaDeviceProp prop;\n"
  58. " if (cudaSuccess == cudaGetDeviceProperties(&prop, device))\n"
  59. " std::printf(\"%d.%d \", prop.major, prop.minor);\n"
  60. " }\n"
  61. " return 0;\n"
  62. "}\n")
  63. try_run(run_result compile_result ${PROJECT_BINARY_DIR} ${file}
  64. CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${CUDA_INCLUDE_DIRS}"
  65. LINK_LIBRARIES ${CUDA_LIBRARIES}
  66. RUN_OUTPUT_VARIABLE compute_capabilities)
  67. if(run_result EQUAL 0)
  68. string(REPLACE "2.1" "2.1(2.0)" compute_capabilities "${compute_capabilities}")
  69. set(CUDA_GPU_DETECT_OUTPUT ${compute_capabilities}
  70. CACHE INTERNAL "Returned GPU architectures from detect_gpus tool" FORCE)
  71. endif()
  72. endif()
  73. if(NOT CUDA_GPU_DETECT_OUTPUT)
  74. message(STATUS "Automatic GPU detection failed. Building for common architectures.")
  75. set(${OUT_VARIABLE} ${CUDA_COMMON_GPU_ARCHITECTURES} PARENT_SCOPE)
  76. else()
  77. set(${OUT_VARIABLE} ${CUDA_GPU_DETECT_OUTPUT} PARENT_SCOPE)
  78. endif()
  79. endfunction()
  80. ################################################################################################
  81. # Function for selecting GPU arch flags for nvcc based on CUDA architectures from parameter list
  82. # Usage:
  83. # SELECT_NVCC_ARCH_FLAGS(out_variable [list of CUDA compute archs])
  84. function(CUDA_SELECT_NVCC_ARCH_FLAGS out_variable)
  85. set(CUDA_ARCH_LIST "${ARGN}")
  86. if("X${CUDA_ARCH_LIST}" STREQUAL "X" )
  87. set(CUDA_ARCH_LIST "Auto")
  88. endif()
  89. set(cuda_arch_bin)
  90. set(cuda_arch_ptx)
  91. if("${CUDA_ARCH_LIST}" STREQUAL "All")
  92. set(CUDA_ARCH_LIST ${CUDA_KNOWN_GPU_ARCHITECTURES})
  93. elseif("${CUDA_ARCH_LIST}" STREQUAL "Common")
  94. set(CUDA_ARCH_LIST ${CUDA_COMMON_GPU_ARCHITECTURES})
  95. elseif("${CUDA_ARCH_LIST}" STREQUAL "Auto")
  96. CUDA_DETECT_INSTALLED_GPUS(CUDA_ARCH_LIST)
  97. message(STATUS "Autodetected CUDA architecture(s): ${CUDA_ARCH_LIST}")
  98. endif()
  99. # Now process the list and look for names
  100. string(REGEX REPLACE "[ \t]+" ";" CUDA_ARCH_LIST "${CUDA_ARCH_LIST}")
  101. list(REMOVE_DUPLICATES CUDA_ARCH_LIST)
  102. foreach(arch_name ${CUDA_ARCH_LIST})
  103. set(arch_bin)
  104. set(arch_ptx)
  105. set(add_ptx FALSE)
  106. # Check to see if we are compiling PTX
  107. if(arch_name MATCHES "(.*)\\+PTX$")
  108. set(add_ptx TRUE)
  109. set(arch_name ${CMAKE_MATCH_1})
  110. endif()
  111. if(arch_name MATCHES "^([0-9]\\.[0-9](\\([0-9]\\.[0-9]\\))?)$")
  112. set(arch_bin ${CMAKE_MATCH_1})
  113. set(arch_ptx ${arch_bin})
  114. else()
  115. # Look for it in our list of known architectures
  116. if(${arch_name} STREQUAL "Fermi")
  117. set(arch_bin 2.0 "2.1(2.0)")
  118. elseif(${arch_name} STREQUAL "Kepler+Tegra")
  119. set(arch_bin 3.2)
  120. elseif(${arch_name} STREQUAL "Kepler+Tesla")
  121. set(arch_bin 3.7)
  122. elseif(${arch_name} STREQUAL "Kepler")
  123. set(arch_bin 3.0 3.5)
  124. set(arch_ptx 3.5)
  125. elseif(${arch_name} STREQUAL "Maxwell+Tegra")
  126. set(arch_bin 5.3)
  127. elseif(${arch_name} STREQUAL "Maxwell")
  128. set(arch_bin 5.0 5.2)
  129. set(arch_ptx 5.2)
  130. elseif(${arch_name} STREQUAL "Pascal")
  131. set(arch_bin 6.0 6.1)
  132. set(arch_ptx 6.1)
  133. elseif(${arch_name} STREQUAL "Volta")
  134. set(arch_bin 7.0 7.0)
  135. set(arch_ptx 7.0)
  136. else()
  137. message(SEND_ERROR "Unknown CUDA Architecture Name ${arch_name} in CUDA_SELECT_NVCC_ARCH_FLAGS")
  138. endif()
  139. endif()
  140. if(NOT arch_bin)
  141. message(SEND_ERROR "arch_bin wasn't set for some reason")
  142. endif()
  143. list(APPEND cuda_arch_bin ${arch_bin})
  144. if(add_ptx)
  145. if (NOT arch_ptx)
  146. set(arch_ptx ${arch_bin})
  147. endif()
  148. list(APPEND cuda_arch_ptx ${arch_ptx})
  149. endif()
  150. endforeach()
  151. # remove dots and convert to lists
  152. string(REGEX REPLACE "\\." "" cuda_arch_bin "${cuda_arch_bin}")
  153. string(REGEX REPLACE "\\." "" cuda_arch_ptx "${cuda_arch_ptx}")
  154. string(REGEX MATCHALL "[0-9()]+" cuda_arch_bin "${cuda_arch_bin}")
  155. string(REGEX MATCHALL "[0-9]+" cuda_arch_ptx "${cuda_arch_ptx}")
  156. if(cuda_arch_bin)
  157. list(REMOVE_DUPLICATES cuda_arch_bin)
  158. endif()
  159. if(cuda_arch_ptx)
  160. list(REMOVE_DUPLICATES cuda_arch_ptx)
  161. endif()
  162. set(nvcc_flags "")
  163. set(nvcc_archs_readable "")
  164. # Tell NVCC to add binaries for the specified GPUs
  165. foreach(arch ${cuda_arch_bin})
  166. if(arch MATCHES "([0-9]+)\\(([0-9]+)\\)")
  167. # User explicitly specified ARCH for the concrete CODE
  168. list(APPEND nvcc_flags -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1})
  169. list(APPEND nvcc_archs_readable sm_${CMAKE_MATCH_1})
  170. else()
  171. # User didn't explicitly specify ARCH for the concrete CODE, we assume ARCH=CODE
  172. list(APPEND nvcc_flags -gencode arch=compute_${arch},code=sm_${arch})
  173. list(APPEND nvcc_archs_readable sm_${arch})
  174. endif()
  175. endforeach()
  176. # Tell NVCC to add PTX intermediate code for the specified architectures
  177. foreach(arch ${cuda_arch_ptx})
  178. list(APPEND nvcc_flags -gencode arch=compute_${arch},code=compute_${arch})
  179. list(APPEND nvcc_archs_readable compute_${arch})
  180. endforeach()
  181. string(REPLACE ";" " " nvcc_archs_readable "${nvcc_archs_readable}")
  182. set(${out_variable} ${nvcc_flags} PARENT_SCOPE)
  183. set(${out_variable}_readable ${nvcc_archs_readable} PARENT_SCOPE)
  184. endfunction()