1 // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 #include "ceed-cuda-compile.h" 9 10 #include <ceed.h> 11 #include <ceed/backend.h> 12 #include <ceed/jit-tools.h> 13 #include <cuda_runtime.h> 14 #include <nvrtc.h> 15 #include <stdarg.h> 16 #include <string.h> 17 18 #include <sstream> 19 20 #include "ceed-cuda-common.h" 21 22 #define CeedChk_Nvrtc(ceed, x) \ 23 do { \ 24 nvrtcResult result = static_cast<nvrtcResult>(x); \ 25 if (result != NVRTC_SUCCESS) return CeedError((ceed), CEED_ERROR_BACKEND, nvrtcGetErrorString(result)); \ 26 } while (0) 27 28 #define CeedCallNvrtc(ceed, ...) \ 29 do { \ 30 int ierr_q_ = __VA_ARGS__; \ 31 CeedChk_Nvrtc(ceed, ierr_q_); \ 32 } while (0) 33 34 //------------------------------------------------------------------------------ 35 // Compile CUDA kernel 36 //------------------------------------------------------------------------------ 37 int CeedCompile_Cuda(Ceed ceed, const char *source, CUmodule *module, const CeedInt num_defines, ...) { 38 size_t ptx_size; 39 char *ptx; 40 const char *jit_defs_path, *jit_defs_source; 41 const int num_opts = 3; 42 CeedInt num_jit_source_dirs = 0; 43 const char **opts; 44 nvrtcProgram prog; 45 struct cudaDeviceProp prop; 46 Ceed_Cuda *ceed_data; 47 48 cudaFree(0); // Make sure a Context exists for nvrtc 49 50 std::ostringstream code; 51 52 // Get kernel specific options, such as kernel constants 53 if (num_defines > 0) { 54 va_list args; 55 va_start(args, num_defines); 56 char *name; 57 int val; 58 59 for (int i = 0; i < num_defines; i++) { 60 name = va_arg(args, char *); 61 val = va_arg(args, int); 62 code << "#define " << name << " " << val << "\n"; 63 } 64 va_end(args); 65 } 66 67 // Standard libCEED definitions for CUDA backends 68 CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-jit.h", &jit_defs_path)); 69 { 70 char *source; 71 72 CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &source)); 73 jit_defs_source = source; 74 } 75 code << jit_defs_source; 76 code << "\n\n"; 77 CeedCallBackend(CeedFree(&jit_defs_path)); 78 CeedCallBackend(CeedFree(&jit_defs_source)); 79 80 // Non-macro options 81 CeedCallBackend(CeedCalloc(num_opts, &opts)); 82 opts[0] = "-default-device"; 83 CeedCallBackend(CeedGetData(ceed, &ceed_data)); 84 CeedCallCuda(ceed, cudaGetDeviceProperties(&prop, ceed_data->device_id)); 85 std::string arch_arg = 86 #if CUDA_VERSION >= 11010 87 // NVRTC used to support only virtual architectures through the option 88 // -arch, since it was only emitting PTX. It will now support actual 89 // architectures as well to emit SASS. 90 // https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html#dynamic-code-generation 91 "-arch=sm_" 92 #else 93 "-arch=compute_" 94 #endif 95 + std::to_string(prop.major) + std::to_string(prop.minor); 96 opts[1] = arch_arg.c_str(); 97 opts[2] = "-Dint32_t=int"; 98 { 99 const char **jit_source_dirs; 100 101 CeedCallBackend(CeedGetJitSourceRoots(ceed, &num_jit_source_dirs, &jit_source_dirs)); 102 CeedCallBackend(CeedRealloc(num_opts + num_jit_source_dirs, &opts)); 103 for (CeedInt i = 0; i < num_jit_source_dirs; i++) { 104 std::ostringstream include_dirs_arg; 105 106 include_dirs_arg << "-I" << jit_source_dirs[i]; 107 CeedCallBackend(CeedStringAllocCopy(include_dirs_arg.str().c_str(), (char **)&opts[num_opts + i])); 108 } 109 CeedCallBackend(CeedRestoreJitSourceRoots(ceed, &jit_source_dirs)); 110 } 111 112 // Add string source argument provided in call 113 code << source; 114 115 // Create Program 116 CeedCallNvrtc(ceed, nvrtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 117 118 // Compile kernel 119 nvrtcResult result = nvrtcCompileProgram(prog, num_opts + num_jit_source_dirs, opts); 120 121 for (CeedInt i = 0; i < num_jit_source_dirs; i++) { 122 CeedCallBackend(CeedFree(&opts[num_opts + i])); 123 } 124 CeedCallBackend(CeedFree(&opts)); 125 if (result != NVRTC_SUCCESS) { 126 char *log; 127 size_t log_size; 128 129 CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n"); 130 CeedDebug(ceed, "Source:\n%s\n", code.str().c_str()); 131 CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n"); 132 CeedCallNvrtc(ceed, nvrtcGetProgramLogSize(prog, &log_size)); 133 CeedCallBackend(CeedMalloc(log_size, &log)); 134 CeedCallNvrtc(ceed, nvrtcGetProgramLog(prog, log)); 135 return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", nvrtcGetErrorString(result), log); 136 } 137 138 #if CUDA_VERSION >= 11010 139 CeedCallNvrtc(ceed, nvrtcGetCUBINSize(prog, &ptx_size)); 140 CeedCallBackend(CeedMalloc(ptx_size, &ptx)); 141 CeedCallNvrtc(ceed, nvrtcGetCUBIN(prog, ptx)); 142 #else 143 CeedCallNvrtc(ceed, nvrtcGetPTXSize(prog, &ptx_size)); 144 CeedCallBackend(CeedMalloc(ptx_size, &ptx)); 145 CeedCallNvrtc(ceed, nvrtcGetPTX(prog, ptx)); 146 #endif 147 CeedCallNvrtc(ceed, nvrtcDestroyProgram(&prog)); 148 149 CeedCallCuda(ceed, cuModuleLoadData(module, ptx)); 150 CeedCallBackend(CeedFree(&ptx)); 151 return CEED_ERROR_SUCCESS; 152 } 153 154 //------------------------------------------------------------------------------ 155 // Get CUDA kernel 156 //------------------------------------------------------------------------------ 157 int CeedGetKernel_Cuda(Ceed ceed, CUmodule module, const char *name, CUfunction *kernel) { 158 CeedCallCuda(ceed, cuModuleGetFunction(kernel, module, name)); 159 return CEED_ERROR_SUCCESS; 160 } 161 162 //------------------------------------------------------------------------------ 163 // Run CUDA kernel with block size selected automatically based on the kernel 164 // (which may use enough registers to require a smaller block size than the 165 // hardware is capable) 166 //------------------------------------------------------------------------------ 167 int CeedRunKernelAutoblockCuda(Ceed ceed, CUfunction kernel, size_t points, void **args) { 168 int min_grid_size, max_block_size; 169 170 CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_block_size, kernel, NULL, 0, 0x10000)); 171 CeedCallBackend(CeedRunKernel_Cuda(ceed, kernel, CeedDivUpInt(points, max_block_size), max_block_size, args)); 172 return CEED_ERROR_SUCCESS; 173 } 174 175 //------------------------------------------------------------------------------ 176 // Run CUDA kernel 177 //------------------------------------------------------------------------------ 178 int CeedRunKernel_Cuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size, void **args) { 179 CeedCallBackend(CeedRunKernelDimShared_Cuda(ceed, kernel, grid_size, block_size, 1, 1, 0, args)); 180 return CEED_ERROR_SUCCESS; 181 } 182 183 //------------------------------------------------------------------------------ 184 // Run CUDA kernel for spatial dimension 185 //------------------------------------------------------------------------------ 186 int CeedRunKernelDim_Cuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size_x, const int block_size_y, const int block_size_z, 187 void **args) { 188 CeedCallBackend(CeedRunKernelDimShared_Cuda(ceed, kernel, grid_size, block_size_x, block_size_y, block_size_z, 0, args)); 189 return CEED_ERROR_SUCCESS; 190 } 191 192 //------------------------------------------------------------------------------ 193 // Run CUDA kernel for spatial dimension with shared memory 194 //------------------------------------------------------------------------------ 195 int CeedRunKernelDimShared_Cuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size_x, const int block_size_y, 196 const int block_size_z, const int shared_mem_size, void **args) { 197 #if CUDA_VERSION >= 9000 198 cuFuncSetAttribute(kernel, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shared_mem_size); 199 #endif 200 CUresult result = cuLaunchKernel(kernel, grid_size, 1, 1, block_size_x, block_size_y, block_size_z, shared_mem_size, NULL, args, NULL); 201 202 if (result == CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES) { 203 int max_threads_per_block, shared_size_bytes, num_regs; 204 205 cuFuncGetAttribute(&max_threads_per_block, CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, kernel); 206 cuFuncGetAttribute(&shared_size_bytes, CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES, kernel); 207 cuFuncGetAttribute(&num_regs, CU_FUNC_ATTRIBUTE_NUM_REGS, kernel); 208 return CeedError(ceed, CEED_ERROR_BACKEND, 209 "CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES: max_threads_per_block %d on block size (%d,%d,%d), shared_size %d, num_regs %d", 210 max_threads_per_block, block_size_x, block_size_y, block_size_z, shared_size_bytes, num_regs); 211 } else CeedChk_Cu(ceed, result); 212 return CEED_ERROR_SUCCESS; 213 } 214 215 //------------------------------------------------------------------------------ 216