|
| 1 | +# |
| 2 | +# This file contains two functions: |
| 3 | +# chooseCudaCC |
| 4 | +# getFlagsForCudaCCList |
| 5 | +# |
| 6 | +# Motivation: |
| 7 | +# CUDA hardware and SDKs are developing over time, different SDK support different |
| 8 | +# hardware, and supported hardware differs depending on platform even for the same |
| 9 | +# SDK version. This file attempts to provide a function that returns a valid selection |
| 10 | +# of hardware for the current SDK and platform. It will require updates as CUDA develops, |
| 11 | +# and it is currently not complete in terms of existing platforms that support CUDA. |
| 12 | +# |
| 13 | + |
| 14 | +# |
| 15 | +# Return the minimal set of supported Cuda CC |
| 16 | +# |
| 17 | +# Usage: |
| 18 | +# chooseCudaCC(SUPPORTED_CC SUPPORTED_GENCODE_FLAGS |
| 19 | +# [MIN_CUDA_VERSION X.Y] |
| 20 | +# [MIN_CC XX ]) |
| 21 | +# |
| 22 | +# SUPPORTED_CC out variable. Stores the list of supported CC. |
| 23 | +# SUPPORTED_GENCODE_FLAGS out variable. List of gencode flags to append to, e.g., CUDA_NVCC_FLAGS |
| 24 | +# MIN_CUDA_VERSION the minimal supported version of cuda (e.g. 7.5, default 7.0). |
| 25 | +# MIN_CC minimal supported Cuda CC by the project (e.g. 35, default 20) |
| 26 | +# |
| 27 | +# This function does not edit cache entries or variables in the parent scope |
| 28 | +# except for the variables whose names are supplied for SUPPORTED_CC and |
| 29 | +# SUPPORTED_GENCODE_FLAGS |
| 30 | +# |
| 31 | +# You may want to cache SUPPORTED_CC and append SUPPORTED_GENCODE_FLAGS to |
| 32 | +# CUDA_NVCC_FLAGS. |
| 33 | +# Like this: |
| 34 | +# set(MYCC ${MYCC} CACHE STRING "CUDA CC versions to compile") |
| 35 | +# end |
| 36 | +# set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};${MY_GENCODE_FLAGS}") |
| 37 | +# |
| 38 | +function(chooseCudaCC SUPPORTED_CC SUPPORTED_GENCODE_FLAGS) |
| 39 | + set(options "") |
| 40 | + set(oneValueArgs MIN_CUDA_VERSION MIN_CC) |
| 41 | + set(multipleValueArgs "") |
| 42 | + cmake_parse_arguments(CHOOSE_CUDA "${options}" "${oneValueArgs}" "${multipleValueArgs}" ${ARGN}) |
| 43 | + |
| 44 | + if(NOT DEFINED CHOOSE_CUDA_MIN_CC) |
| 45 | + set(CHOOSE_CUDA_MIN_CC 20) |
| 46 | + endif() |
| 47 | + if(NOT DEFINED CHOOSE_CUDA_MIN_CUDA_VERSION) |
| 48 | + set(CHOOSE_CUDA_MIN_CUDA_VERSION 7.0) |
| 49 | + endif() |
| 50 | + |
| 51 | + find_package(CUDA ${CHOOSE_CUDA_MIN_CUDA_VERSION} REQUIRED) |
| 52 | + |
| 53 | + if(NOT CUDA_FOUND) |
| 54 | + message(FATAL_ERROR "Could not find CUDA >= ${CHOOSE_CUDA_MIN_CUDA_VERSION}") |
| 55 | + endif() |
| 56 | + |
| 57 | + # |
| 58 | + # Create a list of possible CCs for each host processor. |
| 59 | + # This may require tuning: CUDA cards exist in AIX machines with POWER CPUs, |
| 60 | + # it is possible that non-Tegra ARM systems exist as well. |
| 61 | + # For now, this is my best guess. |
| 62 | + # |
| 63 | + set(TEGRA_SUPPORTED_PROCESSORS "armv71;arm;aarch64") |
| 64 | + set(OTHER_SUPPORTED_PROCESSORS "i686;x86_64;AMD64") |
| 65 | + |
| 66 | + set(CC_LIST_BY_SYSTEM_PROCESSOR "") |
| 67 | + if(CMAKE_SYSTEM_PROCESSOR IN_LIST OTHER_SUPPORTED_PROCESSORS) |
| 68 | + list(APPEND CC_LIST_BY_SYSTEM_PROCESSOR "20;21;30;35;50;52;60;61;70;75;80;86;89;90") |
| 69 | + endif() |
| 70 | + if(CMAKE_SYSTEM_PROCESSOR IN_LIST TEGRA_SUPPORTED_PROCESSORS) |
| 71 | + list(APPEND CC_LIST_BY_SYSTEM_PROCESSOR "32;53;62;72") |
| 72 | + endif() |
| 73 | + if(NOT CC_LIST_BY_SYSTEM_PROCESSOR) |
| 74 | + message(FATAL_ERROR "Unknown how to build for ${CMAKE_SYSTEM_PROCESSOR}") |
| 75 | + endif() |
| 76 | + |
| 77 | + # |
| 78 | + # Default setting of the CUDA CC versions to compile. |
| 79 | + # Shortening the lists saves a lot of compile time. |
| 80 | + # |
| 81 | + |
| 82 | + # The current version last time this list was updated was CUDA 12.1. |
| 83 | + if(CUDA_VERSION VERSION_GREATER_EQUAL 12) |
| 84 | + set(CUDA_MIN_CC 50) |
| 85 | + set(CUDA_MAX_CC 90) |
| 86 | + elseif(CUDA_VERSION VERSION_GREATER_EQUAL 11.8) |
| 87 | + set(CUDA_MIN_CC 35) |
| 88 | + set(CUDA_MAX_CC 90) |
| 89 | + elseif(CUDA_VERSION VERSION_GREATER_EQUAL 11.1) |
| 90 | + set(CUDA_MIN_CC 35) |
| 91 | + set(CUDA_MAX_CC 86) |
| 92 | + elseif(CUDA_VERSION_MAJOR GREATER_EQUAL 11) |
| 93 | + set(CUDA_MIN_CC 35) |
| 94 | + set(CUDA_MAX_CC 80) |
| 95 | + elseif(CUDA_VERSION_MAJOR GREATER_EQUAL 10) |
| 96 | + set(CUDA_MIN_CC 30) |
| 97 | + set(CUDA_MAX_CC 75) |
| 98 | + elseif(CUDA_VERSION_MAJOR GREATER_EQUAL 9) |
| 99 | + set(CUDA_MIN_CC 30) |
| 100 | + set(CUDA_MAX_CC 72) |
| 101 | + elseif(CUDA_VERSION_MAJOR GREATER_EQUAL 8) |
| 102 | + set(CUDA_MIN_CC 20) |
| 103 | + set(CUDA_MAX_CC 62) |
| 104 | + elseif(CUDA_VERSION_MAJOR GREATER_EQUAL 7) |
| 105 | + set(CUDA_MIN_CC 20) |
| 106 | + set(CUDA_MAX_CC 53) |
| 107 | + else() |
| 108 | + message(FATAL_ERROR "We do not support a CUDA SDK below version 7.0") |
| 109 | + endif() |
| 110 | + if(${CHOOSE_CUDA_MIN_CC} GREATER ${CUDA_MIN_CC}) |
| 111 | + set(CUDA_MIN_CC ${CHOOSE_CUDA_MIN_CC}) |
| 112 | + endif() |
| 113 | + |
| 114 | + set(CC_LIST "") |
| 115 | + foreach(CC ${CC_LIST_BY_SYSTEM_PROCESSOR}) |
| 116 | + if( (${CC} GREATER_EQUAL ${CUDA_MIN_CC}) AND |
| 117 | + (${CC} LESS_EQUAL ${CUDA_MAX_CC}) ) |
| 118 | + list(APPEND CC_LIST ${CC}) |
| 119 | + endif() |
| 120 | + endforeach() |
| 121 | + |
| 122 | + # |
| 123 | + # Add all requested CUDA CCs to the command line for offline compilation |
| 124 | + # |
| 125 | + set(GENCODE_FLAGS "") |
| 126 | + list(SORT CC_LIST) |
| 127 | + foreach(CC_VERSION ${CC_LIST}) |
| 128 | + list(APPEND GENCODE_FLAGS "-gencode;arch=compute_${CC_VERSION},code=sm_${CC_VERSION}") |
| 129 | + endforeach() |
| 130 | + |
| 131 | + # |
| 132 | + # Use the highest request CUDA CC for CUDA JIT compilation |
| 133 | + # |
| 134 | + list(LENGTH CC_LIST CC_LIST_LEN) |
| 135 | + MATH(EXPR CC_LIST_LEN "${CC_LIST_LEN}-1") |
| 136 | + list(GET CC_LIST ${CC_LIST_LEN} CC_LIST_LAST) |
| 137 | + list(APPEND GENCODE_FLAGS "-gencode;arch=compute_${CC_LIST_LAST},code=compute_${CC_LIST_LAST}") |
| 138 | + |
| 139 | + # |
| 140 | + # Two variables are exported to the parent scope. One is passed through the |
| 141 | + # environment (CUDA_NVCC_FLAGS), the other is passed by name (SUPPORTED_CC) |
| 142 | + # |
| 143 | + set(${SUPPORTED_GENCODE_FLAGS} "${GENCODE_FLAGS}" PARENT_SCOPE) |
| 144 | + set(${SUPPORTED_CC} "${CC_LIST}" PARENT_SCOPE) |
| 145 | +endfunction() |
| 146 | + |
| 147 | +# |
| 148 | +# Return the gencode parameters for a given list of CCs. |
| 149 | +# |
| 150 | +# Usage: |
| 151 | +# getFlagsForCudaCCList(INPUT_CC_LIST SUPPORTED_GENCODE_FLAGS) |
| 152 | +# |
| 153 | +# INPUT_CC_LIST in variable. Contains a list of supported CCs. |
| 154 | +# SUPPORTED_GENCODE_FLAGS out variable. List of gencode flags to append to, e.g., CUDA_NVCC_FLAGS |
| 155 | +# |
| 156 | +function(getFlagsForCudaCCList INPUT_CC_LIST SUPPORTED_GENCODE_FLAGS) |
| 157 | + set(CC_LIST "${${INPUT_CC_LIST}}") |
| 158 | + |
| 159 | + # |
| 160 | + # Add all requested CUDA CCs to the command line for offline compilation |
| 161 | + # |
| 162 | + set(GENCODE_FLAGS "") |
| 163 | + list(SORT CC_LIST) |
| 164 | + foreach(CC_VERSION ${CC_LIST}) |
| 165 | + list(APPEND GENCODE_FLAGS "-gencode;arch=compute_${CC_VERSION},code=sm_${CC_VERSION}") |
| 166 | + endforeach() |
| 167 | + |
| 168 | + # |
| 169 | + # Use the highest request CUDA CC for CUDA JIT compilation |
| 170 | + # |
| 171 | + list(LENGTH CC_LIST CC_LIST_LEN) |
| 172 | + MATH(EXPR CC_LIST_LEN "${CC_LIST_LEN}-1") |
| 173 | + list(GET CC_LIST ${CC_LIST_LEN} CC_LIST_LAST) |
| 174 | + list(APPEND GENCODE_FLAGS "-gencode;arch=compute_${CC_LIST_LAST},code=compute_${CC_LIST_LAST}") |
| 175 | + |
| 176 | + message(STATUS "Setting gencode flags: ${GENCODE_FLAGS}") |
| 177 | + |
| 178 | + # |
| 179 | + # Two variables are exported to the parent scope. One is passed through the |
| 180 | + # environment (CUDA_NVCC_FLAGS), the other is passed by name (SUPPORTED_CC) |
| 181 | + # |
| 182 | + set(${SUPPORTED_GENCODE_FLAGS} "${GENCODE_FLAGS}" PARENT_SCOPE) |
| 183 | +endfunction() |
| 184 | + |
0 commit comments