util_cuda.h (5926B)
1 /* 2 Copyright 2010-2011, D. E. Shaw Research. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are 7 met: 8 9 * Redistributions of source code must retain the above copyright 10 notice, this list of conditions, and the following disclaimer. 11 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions, and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 * Neither the name of D. E. Shaw Research nor the names of its 17 contributors may be used to endorse or promote products derived from 18 this software without specific prior written permission. 19 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 #ifndef UTIL_CUDA_H__ 33 #define UTIL_CUDA_H__ 34 35 #include "util.h" 36 37 #include <cuda.h> 38 #include <cuda_runtime_api.h> 39 40 // utility macros to check return codes and complain/exit on failure 41 #define CHECKLAST(MSG) do { cudaError_t e = cudaGetLastError(); if (e != cudaSuccess) {fprintf(stderr, "%s:%d: CUDA Error: %s: %s\n", __FILE__, __LINE__, (MSG), cudaGetErrorString(e)); exit(1); }} while(0) 42 #define CHECKCALL(RET) do { cudaError_t e = (RET); if (e != cudaSuccess) { fprintf(stderr, "%s:%d: CUDA Error: %s\n", __FILE__, __LINE__, cudaGetErrorString(e)); exit(1); } } while(0) 43 44 typedef struct cuda_info { 45 int devnum, cores, blocks_per_grid, threads_per_block; 46 double cycles; 47 struct cudaDeviceProp dev; 48 } CUDAInfo; 49 50 // If devstr is none, chooses device with most cores. 51 static CUDAInfo *cuda_init(const char *devstr) 52 { 53 CUDAInfo *tp; 54 int i, ndev, cores, devcores, devdev; 55 double cycles; 56 CHECKNOTZERO(tp = (CUDAInfo *) malloc(sizeof(CUDAInfo))); 57 CHECKCALL( cudaGetDeviceCount(&ndev) ); 58 devcores = 0; 59 devdev = -1; 60 if (devstr == NULL) { 61 devstr = getenv("R123EXAMPLE_ENVCONF_CUDA_DEVICE"); 62 if (devstr && devstr[0] >= '0' && devstr[0] <= '9' && devstr[1] == '\0') { 63 devdev = devstr[0]-'0'; 64 } 65 } 66 for (i = 0; i < ndev; i++) { 67 struct cudaDeviceProp cu; 68 CHECKCALL( cudaGetDeviceProperties (&cu, i) ); 69 // Number of cores is not available from a query, have to hardwire 70 // some knowledge here, from web articles about the various generations 71 // SM or SMX, might also find this info in 72 // CUDA SDK $CUDA_SAMPLES_DIR/common/inc/helper_cuda.h 73 // or https://github.com/NVIDIA/nvidia-docker/blob/master/tools/src/cuda/cuda.go 74 cores = cu.multiProcessorCount; 75 if (cu.major == 1 && cu.minor >= 0 && cu.minor <= 3) { 76 // 1.0 (G80, G92, aka GTX880, Tesla [CSD]870) to 1.3 (GT200, aka GTX280, Tesla [CS]10xx) have 8 cores per MP 77 cores *= 8; 78 } else if (cu.major == 2 && cu.minor == 0) { 79 // 2.0 (G100, aka GTX480, Tesla/Fermi [CSM]20[567]0, and GF110, aka GTX580, M2090) 80 cores *= 32; 81 } else if (cu.major == 2 && cu.minor == 1) { 82 // 2.1 (GF104, GF114, GF116 aka GTX [45][56]0) 83 cores *= 48; 84 } else if (cu.major == 3) { 85 // 3.0 (Kepler GK104 aka GTX 680), 3.2 (TK1), 3.5 (GK11x, GK20x), 3.7 (GK21x) 86 cores *= 192; 87 } else if (cu.major == 5) { 88 // 5.0 (Maxwell GM10x), 5.2 (GM20x), 5.3 (TX1) 89 cores *= 128; 90 } else if (cu.major == 6 && cu.minor == 0) { 91 // 6.0 (Pascal P100) 92 cores *= 64; 93 } else if (cu.major == 6) { 94 // 6.1 (Pascal 10xx, Titan Xp, P40), 6.2 (Drive PX2 and Tegra) 95 cores *= 128; 96 } else if (cu.major == 7) { 97 // 7.[05] (Volta and Turing RTX 20[678]0, Titan RTX, Quadro RTX), 7.2 (Xavier Jetson) 98 cores *= 64; 99 } else if (cu.major == 8 && cu.minor == 0) { 100 // 8.0 (Ampere A100) 101 cores *= 64; 102 } else if (cu.major == 8 && cu.minor == 6) { 103 // 8.6 (Ampere RTX 30[56789]0, A[23456]000, A45000) 104 cores *= 128; 105 } else { 106 int coremultguess = 384; 107 cores *= coremultguess; 108 fprintf(stderr, "WARNING: Unknown number of cores per MP for this device: assuming %d, so cpb calculation will be wrong and choice of blocks/grid might be suboptimal\n", coremultguess); 109 } 110 /* clockrate is in KHz */ 111 cycles = 1e3 * cu.clockRate * cores; 112 printf(" %d: maj %d min %d %s%s ( %d units @ %g MHz ECC=%d %d cores %g Gcycles/s)\n", 113 i, cu.major, cu.minor, cu.name, cu.integrated ? " integrated" : "", 114 cu.multiProcessorCount, cu.clockRate*1e-3, cu.ECCEnabled, cores, cycles*1e-9); 115 if ((devstr && strstr(cu.name, devstr) == NULL )|| 116 devdev >= 0 && i != devdev) { 117 dprintf(("skipping device %s\n", cu.name)); 118 continue; 119 } 120 if (cores > devcores) { 121 devcores = cores; 122 tp->devnum = i; 123 tp->cores = cores; 124 tp->cycles = cycles; 125 tp->dev = cu; 126 } 127 } 128 if (devcores == 0) { 129 fprintf(stderr, "could not find specified device\n"); 130 exit(1); 131 } 132 tp->blocks_per_grid = tp->cores; /* seems like a good guess */ 133 tp->threads_per_block = tp->dev.warpSize * 2; 134 printf("Using CUDA device %d, %d cores, %g cycles, will try %d blocks/grid %d threads/block\n", 135 tp->devnum, tp->cores, tp->cycles, tp->blocks_per_grid, tp->threads_per_block); 136 CHECKCALL(cudaSetDevice(tp->devnum)); 137 dprintf(("cuda_init done\n")); 138 return tp; 139 } 140 141 static void cuda_done(CUDAInfo *tp) 142 { 143 dprintf(("cuda_done\n")); 144 free(tp); 145 } 146 147 #endif /* UTIL_CUDA_H__ */