select_compute_arch.cmake 7.4 KB

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