| 1 | #!/bin/bash |
| 2 | # |
| 3 | # mymcpu: Print the codename the current gpu |
| 4 | # mygpu: Print a gpu value |
| 5 | # myarchgpu: Print arch-gpu value |
| 6 | # |
| 7 | |
| 8 | PROGVERSION=X.Y-Z |
| 9 | |
| 10 | # Copyright (c) 2020-2022 Advanced Micro Devices, Inc. All rights reserved. |
| 11 | # [MITx11 License] |
| 12 | |
| 13 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | # of this software and associated documentation files (the "Software"), to deal |
| 15 | # in the Software without restriction, including without limitation the rights |
| 16 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | # copies of the Software, and to permit persons to whom the Software is |
| 18 | # furnished to do so, subject to the following conditions: |
| 19 | # |
| 20 | # The above copyright notice and this permission notice shall be included in |
| 21 | # all copies or substantial portions of the Software. |
| 22 | # |
| 23 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 29 | # THE SOFTWARE. |
| 30 | |
| 31 | function usage(){ |
| 32 | /bin/cat 2>&1 <<"EOF" |
| 33 | |
| 34 | mymcpu: Print out a codename for the gpu of the current system. |
| 35 | |
| 36 | mygpu: Print out the real gpu name for the current system . |
| 37 | mygpu will only print values accepted by cuda clang in |
| 38 | the clang argument --cuda-gpu-arch |
| 39 | myarchgpu: Print out the real gpu name for the current system |
| 40 | with the arch prefix. e.g. nvptx-sm_70 |
| 41 | |
| 42 | Usage: myarchgpu |
| 43 | |
| 44 | Options without values: |
| 45 | -h Print this help message |
| 46 | -d If GPU is unknown, return this default device |
| 47 | -version Print the version of mymcpu and mygpu |
| 48 | |
| 49 | Copyright (c) 2020-2022 ADVANCED MICRO DEVICES, INC. |
| 50 | |
| 51 | EOF |
| 52 | exit 1 |
| 53 | } |
| 54 | |
| 55 | function version(){ |
| 56 | echo $PROGVERSION |
| 57 | exit 0 |
| 58 | } |
| 59 | |
| 60 | function getdname(){ |
| 61 | local __DIRN=`dirname "$1"` |
| 62 | if [ "$__DIRN" = "." ] ; then |
| 63 | __DIRN=$PWD; |
| 64 | else |
| 65 | if [ ${__DIRN:0:1} != "/" ] ; then |
| 66 | if [ ${__DIRN:0:2} == ".." ] ; then |
| 67 | __DIRN=`dirname $PWD`/${__DIRN:3} |
| 68 | else |
| 69 | if [ ${__DIRN:0:1} = "." ] ; then |
| 70 | __DIRN=$PWD/${__DIRN:2} |
| 71 | else |
| 72 | __DIRN=$PWD/$__DIRN |
| 73 | fi |
| 74 | fi |
| 75 | fi |
| 76 | fi |
| 77 | echo $__DIRN |
| 78 | } |
| 79 | |
| 80 | function getarch(){ |
| 81 | if [ "${1:0:3}" == "sm_" ] ; then |
| 82 | echo "nvptx" |
| 83 | else |
| 84 | echo "amdgcn" |
| 85 | fi |
| 86 | } |
| 87 | |
| 88 | function code2gpu(){ |
| 89 | case "$1" in |
| 90 | "tahiti") gpuname="gfx600" |
| 91 | ;; |
| 92 | "gfx600") gpuname="gfx600" |
| 93 | ;; |
| 94 | "oland") gpuname="gfx601" |
| 95 | ;; |
| 96 | "gfx601") gpuname="gfx601" |
| 97 | ;; |
| 98 | "gfx700") gpuname="gfx700" |
| 99 | ;; |
| 100 | "kaveri") gpuname="gfx700" |
| 101 | ;; |
| 102 | "gfx701") gpuname="gfx701" |
| 103 | ;; |
| 104 | "hawaii") gpuname="gfx701" |
| 105 | ;; |
| 106 | "gfx703") gpuname="gfx703" |
| 107 | ;; |
| 108 | "kabini") gpuname="gfx703" |
| 109 | ;; |
| 110 | "gfx801") gpuname="gfx801" |
| 111 | ;; |
| 112 | "carrizo") gpuname="gfx801" |
| 113 | ;; |
| 114 | "gfx802") gpuname="gfx802" |
| 115 | ;; |
| 116 | "tonga") gpuname="gfx802" |
| 117 | ;; |
| 118 | "gfx803") gpuname="gfx803" |
| 119 | ;; |
| 120 | "fiji") gpuname="gfx803" |
| 121 | ;; |
| 122 | "vega") gpuname="gfx900" |
| 123 | ;; |
| 124 | "RavenRidge") gpuname="gfx902" |
| 125 | ;; |
| 126 | "vega20") gpuname="gfx906" |
| 127 | ;; |
| 128 | "NRAXJ") gpuname="gfx908" |
| 129 | ;; |
| 130 | "NRAXJ2") gpuname="gfx90a" |
| 131 | ;; |
| 132 | "navi12") gpuname="gfx1011" |
| 133 | ;; |
| 134 | "navi21") gpuname="gfx1030" |
| 135 | ;; |
| 136 | "n3a") gpuname="gfx1101" |
| 137 | ;; |
| 138 | "sm_30") gpuname="sm_30" |
| 139 | ;; |
| 140 | "k4000") gpuname="sm_30" |
| 141 | ;; |
| 142 | "k40c") gpuname="sm_35" |
| 143 | ;; |
| 144 | "k4200") gpuname="sm_30" |
| 145 | ;; |
| 146 | "gtx960m") gpuname="sm_50" |
| 147 | ;; |
| 148 | "gtx980") gpuname="sm_35" |
| 149 | ;; |
| 150 | "gtx1050") gpuname="sm_61" |
| 151 | ;; |
| 152 | "gtx1060") gpuname="sm_61" |
| 153 | ;; |
| 154 | "gtx1080") gpuname="sm_61" |
| 155 | ;; |
| 156 | "gt730") gpuname="sm_35" |
| 157 | ;; |
| 158 | "p100") gpuname="sm_60" |
| 159 | ;; |
| 160 | "gv100") gpuname="sm_70" |
| 161 | ;; |
| 162 | "v100") gpuname="sm_70" |
| 163 | ;; |
| 164 | "") gpuname="sm_35" |
| 165 | ;; |
| 166 | *) gpuname=$unknown_value |
| 167 | ;; |
| 168 | esac |
| 169 | echo $gpuname |
| 170 | } |
| 171 | |
| 172 | function getmycodename() |
| 173 | { |
| 174 | thisdir=$(getdname $0) |
| 175 | if [ -L "$thisdir/mymcpu" ] ; then |
| 176 | linkfile=`readlink "$thisdir/mymcpu"` |
| 177 | if [ "${linkfile%%/*}" == ".." ] ; then |
| 178 | linkfile=$thisdir/$linkfile |
| 179 | fi |
| 180 | thisdir=$(getdname $linkfile) |
| 181 | fi |
| 182 | GPUTABLE_FILE=${GPUTABLE_FILE:-$thisdir/gputable.txt} |
| 183 | local _found=0 |
| 184 | if [ -f $GPUTABLE_FILE ] ; then |
| 185 | # Look for amd cards first |
| 186 | for sysfsname in `find -L /sys/bus/pci/devices -maxdepth 2 -name uevent | xargs grep -l -E 'DRIVER=amdgpu'` ; do |
| 187 | gpuid=`cat $sysfsname | grep 'PCI_ID=' | cut -d"=" -f2 | tr '[:upper:]' '[:lower:]'` |
| 188 | if [ $_found == 0 ] ; then |
| 189 | #fixme what happens if multiple gpus, will gpuid be multiple lines? |
| 190 | entry=`grep -m1 "^$gpuid" $GPUTABLE_FILE` |
| 191 | if [ $? == 0 ] ; then |
| 192 | codename=`echo $entry | awk '{print $4}'` |
| 193 | _found=1 |
| 194 | fi |
| 195 | fi |
| 196 | done |
| 197 | if [ $_found == 0 ] ; then |
| 198 | for sysfsname in `find -L /sys/bus/pci/devices -maxdepth 2 -name uevent | xargs grep -l -E 'DRIVER=nvidia'` ; do |
| 199 | gpuid=`cat $sysfsname | grep 'PCI_ID=' | cut -d"=" -f2 | tr '[:upper:]' '[:lower:]'` |
| 200 | if [ $_found == 0 ] ; then |
| 201 | #fixme what happens if multiple gpus, will gpuid be multiple lines? |
| 202 | entry=`grep -m1 "^$gpuid" $GPUTABLE_FILE` |
| 203 | if [ $? == 0 ] ; then |
| 204 | codename=`echo $entry | awk '{print $4}'` |
| 205 | _found=1 |
| 206 | fi |
| 207 | fi |
| 208 | done |
| 209 | fi |
| 210 | fi |
| 211 | if [ $_found == 1 ] ; then |
| 212 | echo $codename |
| 213 | else |
| 214 | echo "unknown" |
| 215 | # for debugging unknown codenames, return gpuid here |
| 216 | # echo "$gpuid" |
| 217 | fi |
| 218 | } |
| 219 | |
| 220 | # =========== Main code for mymcpu and mygpu starts here ========== |
| 221 | TYPERUN=${0##*\/} |
| 222 | MODE= |
| 223 | |
| 224 | unknown_value="unknown" |
| 225 | # process args |
| 226 | while [ $# -gt 0 ] ; do |
| 227 | case "$1" in |
| 228 | -n) MODE=num; shift ;; |
| 229 | -h) usage ;; |
| 230 | -d) unknown_value=$2; shift ;; |
| 231 | -help) usage ;; |
| 232 | --help) usage ;; |
| 233 | -version) version ;; |
| 234 | --version) version ;; |
| 235 | --) shift ; break;; |
| 236 | *) break;echo $1 ignored; |
| 237 | esac |
| 238 | shift |
| 239 | done |
| 240 | |
| 241 | rc=0 |
| 242 | if [ "$TYPERUN" == "mygpu" ] ; then |
| 243 | codename=$(getmycodename) |
| 244 | gpuname=$(code2gpu $codename) |
| 245 | [ "$gpuname" == "unknown" ] && rc=1 |
| 246 | if [ -z "$MODE" ]; then |
| 247 | echo $gpuname |
| 248 | else |
| 249 | echo `echo $gpuname | sed 's/gfx//' | sed 's/sm_//'` |
| 250 | fi |
| 251 | elif [ "$TYPERUN" == "myarchgpu" ] ; then |
| 252 | codename=$(getmycodename) |
| 253 | gpuname=$(code2gpu $codename) |
| 254 | if [ "$gpuname" == "unknown" ] ; then |
| 255 | rc=1 |
| 256 | echo unknown-unknown |
| 257 | else |
| 258 | archname=$(getarch $gpuname) |
| 259 | echo $archname-$gpuname |
| 260 | fi |
| 261 | else |
| 262 | # This is mymcpu so print the codename |
| 263 | codename=$(getmycodename) |
| 264 | [ "$codename" == "unknown" ] && rc=1 |
| 265 | echo $codename |
| 266 | fi |
| 267 | |
| 268 | exit $rc |