15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 2c9c2c079SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3c9c2c079SJeremy L Thompson // 4c9c2c079SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5c9c2c079SJeremy L Thompson // 6c9c2c079SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7c9c2c079SJeremy L Thompson 82b730f8bSJeremy L Thompson #include "ceed-cuda-compile.h" 92b730f8bSJeremy L Thompson 1049aac155SJeremy L Thompson #include <ceed.h> 11c9c2c079SJeremy L Thompson #include <ceed/backend.h> 12c9c2c079SJeremy L Thompson #include <ceed/jit-tools.h> 13c9c2c079SJeremy L Thompson #include <cuda_runtime.h> 14c9c2c079SJeremy L Thompson #include <nvrtc.h> 15c9c2c079SJeremy L Thompson #include <stdarg.h> 16c9c2c079SJeremy L Thompson #include <string.h> 172b730f8bSJeremy L Thompson 182b730f8bSJeremy L Thompson #include <sstream> 192b730f8bSJeremy L Thompson 20c9c2c079SJeremy L Thompson #include "ceed-cuda-common.h" 21c9c2c079SJeremy L Thompson 22c9c2c079SJeremy L Thompson #define CeedChk_Nvrtc(ceed, x) \ 23c9c2c079SJeremy L Thompson do { \ 24c9c2c079SJeremy L Thompson nvrtcResult result = static_cast<nvrtcResult>(x); \ 252b730f8bSJeremy L Thompson if (result != NVRTC_SUCCESS) return CeedError((ceed), CEED_ERROR_BACKEND, nvrtcGetErrorString(result)); \ 26c9c2c079SJeremy L Thompson } while (0) 27c9c2c079SJeremy L Thompson 282b730f8bSJeremy L Thompson #define CeedCallNvrtc(ceed, ...) \ 292b730f8bSJeremy L Thompson do { \ 302b730f8bSJeremy L Thompson int ierr_q_ = __VA_ARGS__; \ 312b730f8bSJeremy L Thompson CeedChk_Nvrtc(ceed, ierr_q_); \ 326574a04fSJeremy L Thompson } while (0) 332b730f8bSJeremy L Thompson 34c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 35c9c2c079SJeremy L Thompson // Compile CUDA kernel 36c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 37eb7e6cafSJeremy L Thompson int CeedCompile_Cuda(Ceed ceed, const char *source, CUmodule *module, const CeedInt num_defines, ...) { 38ca735530SJeremy L Thompson size_t ptx_size; 3922070f95SJeremy L Thompson char *ptx; 4022070f95SJeremy L Thompson const char *jit_defs_path, *jit_defs_source; 41*b13efd58SJeremy L Thompson const int num_opts = 3; 42*b13efd58SJeremy L Thompson CeedInt num_jit_source_dirs = 0; 43*b13efd58SJeremy L Thompson const char **opts; 44c9c2c079SJeremy L Thompson nvrtcProgram prog; 45ca735530SJeremy L Thompson struct cudaDeviceProp prop; 46ca735530SJeremy L Thompson Ceed_Cuda *ceed_data; 47ca735530SJeremy L Thompson 48ca735530SJeremy L Thompson cudaFree(0); // Make sure a Context exists for nvrtc 49c9c2c079SJeremy L Thompson 50c9c2c079SJeremy L Thompson std::ostringstream code; 51c9c2c079SJeremy L Thompson 52c9c2c079SJeremy L Thompson // Get kernel specific options, such as kernel constants 53c9c2c079SJeremy L Thompson if (num_defines > 0) { 54c9c2c079SJeremy L Thompson va_list args; 55c9c2c079SJeremy L Thompson va_start(args, num_defines); 56c9c2c079SJeremy L Thompson char *name; 57c9c2c079SJeremy L Thompson int val; 58ca735530SJeremy L Thompson 59c9c2c079SJeremy L Thompson for (int i = 0; i < num_defines; i++) { 60c9c2c079SJeremy L Thompson name = va_arg(args, char *); 61c9c2c079SJeremy L Thompson val = va_arg(args, int); 62c9c2c079SJeremy L Thompson code << "#define " << name << " " << val << "\n"; 63c9c2c079SJeremy L Thompson } 64c9c2c079SJeremy L Thompson va_end(args); 65c9c2c079SJeremy L Thompson } 66c9c2c079SJeremy L Thompson 67c9c2c079SJeremy L Thompson // Standard libCEED definitions for CUDA backends 682b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-jit.h", &jit_defs_path)); 6922070f95SJeremy L Thompson { 7022070f95SJeremy L Thompson char *source; 7122070f95SJeremy L Thompson 7222070f95SJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &source)); 7322070f95SJeremy L Thompson jit_defs_source = source; 7422070f95SJeremy L Thompson } 75c9c2c079SJeremy L Thompson code << jit_defs_source; 76c9c2c079SJeremy L Thompson code << "\n\n"; 77edaedbd9SJeremy L Thompson CeedCallBackend(CeedFree(&jit_defs_path)); 78edaedbd9SJeremy L Thompson CeedCallBackend(CeedFree(&jit_defs_source)); 79c9c2c079SJeremy L Thompson 80c9c2c079SJeremy L Thompson // Non-macro options 81*b13efd58SJeremy L Thompson CeedCallBackend(CeedCalloc(num_opts, &opts)); 82c9c2c079SJeremy L Thompson opts[0] = "-default-device"; 832b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &ceed_data)); 842b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaGetDeviceProperties(&prop, ceed_data->device_id)); 8529ec485eSJed Brown std::string arch_arg = 8629ec485eSJed Brown #if CUDA_VERSION >= 11010 8729ec485eSJed Brown // NVRTC used to support only virtual architectures through the option 8829ec485eSJed Brown // -arch, since it was only emitting PTX. It will now support actual 8929ec485eSJed Brown // architectures as well to emit SASS. 9029ec485eSJed Brown // https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html#dynamic-code-generation 9129ec485eSJed Brown "-arch=sm_" 9229ec485eSJed Brown #else 9329ec485eSJed Brown "-arch=compute_" 9429ec485eSJed Brown #endif 9529ec485eSJed Brown + std::to_string(prop.major) + std::to_string(prop.minor); 96c9c2c079SJeremy L Thompson opts[1] = arch_arg.c_str(); 97c9c2c079SJeremy L Thompson opts[2] = "-Dint32_t=int"; 98*b13efd58SJeremy L Thompson { 99*b13efd58SJeremy L Thompson const char **jit_source_dirs; 100*b13efd58SJeremy L Thompson 101*b13efd58SJeremy L Thompson CeedCallBackend(CeedGetJitSourceRoots(ceed, &num_jit_source_dirs, &jit_source_dirs)); 102*b13efd58SJeremy L Thompson CeedCallBackend(CeedRealloc(num_opts + num_jit_source_dirs, &opts)); 103*b13efd58SJeremy L Thompson for (CeedInt i = 0; i < num_jit_source_dirs; i++) { 104*b13efd58SJeremy L Thompson std::ostringstream include_dirs_arg; 105*b13efd58SJeremy L Thompson 106*b13efd58SJeremy L Thompson include_dirs_arg << "-I" << jit_source_dirs[i]; 107*b13efd58SJeremy L Thompson CeedCallBackend(CeedStringAllocCopy(include_dirs_arg.str().c_str(), (char **)&opts[num_opts + i])); 108*b13efd58SJeremy L Thompson } 109*b13efd58SJeremy L Thompson CeedCallBackend(CeedRestoreJitSourceRoots(ceed, &jit_source_dirs)); 110*b13efd58SJeremy L Thompson } 111c9c2c079SJeremy L Thompson 112c9c2c079SJeremy L Thompson // Add string source argument provided in call 113c9c2c079SJeremy L Thompson code << source; 114c9c2c079SJeremy L Thompson 115c9c2c079SJeremy L Thompson // Create Program 1162b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 117c9c2c079SJeremy L Thompson 118c9c2c079SJeremy L Thompson // Compile kernel 119*b13efd58SJeremy L Thompson nvrtcResult result = nvrtcCompileProgram(prog, num_opts + num_jit_source_dirs, opts); 120ca735530SJeremy L Thompson 121*b13efd58SJeremy L Thompson for (CeedInt i = 0; i < num_jit_source_dirs; i++) { 122*b13efd58SJeremy L Thompson CeedCallBackend(CeedFree(&opts[num_opts + i])); 123*b13efd58SJeremy L Thompson } 124*b13efd58SJeremy L Thompson CeedCallBackend(CeedFree(&opts)); 125c9c2c079SJeremy L Thompson if (result != NVRTC_SUCCESS) { 126c9c2c079SJeremy L Thompson char *log; 127ca735530SJeremy L Thompson size_t log_size; 128ca735530SJeremy L Thompson 129a4bfdec2SJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n"); 130f5a0ec79SJeremy L Thompson CeedDebug(ceed, "Source:\n%s\n", code.str().c_str()); 131a4bfdec2SJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n"); 132ca735530SJeremy L Thompson CeedCallNvrtc(ceed, nvrtcGetProgramLogSize(prog, &log_size)); 1332b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(log_size, &log)); 1342b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcGetProgramLog(prog, log)); 1352b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", nvrtcGetErrorString(result), log); 136c9c2c079SJeremy L Thompson } 137c9c2c079SJeremy L Thompson 13829ec485eSJed Brown #if CUDA_VERSION >= 11010 13929ec485eSJed Brown CeedCallNvrtc(ceed, nvrtcGetCUBINSize(prog, &ptx_size)); 14029ec485eSJed Brown CeedCallBackend(CeedMalloc(ptx_size, &ptx)); 14129ec485eSJed Brown CeedCallNvrtc(ceed, nvrtcGetCUBIN(prog, ptx)); 14229ec485eSJed Brown #else 1432b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcGetPTXSize(prog, &ptx_size)); 1442b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(ptx_size, &ptx)); 1452b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcGetPTX(prog, ptx)); 14629ec485eSJed Brown #endif 1472b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcDestroyProgram(&prog)); 148c9c2c079SJeremy L Thompson 1492b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleLoadData(module, ptx)); 1502b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&ptx)); 151c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 152c9c2c079SJeremy L Thompson } 153c9c2c079SJeremy L Thompson 154c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 155c9c2c079SJeremy L Thompson // Get CUDA kernel 156c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 157eb7e6cafSJeremy L Thompson int CeedGetKernel_Cuda(Ceed ceed, CUmodule module, const char *name, CUfunction *kernel) { 1582b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleGetFunction(kernel, module, name)); 159c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 160c9c2c079SJeremy L Thompson } 161c9c2c079SJeremy L Thompson 162b2165e7aSSebastian Grimberg //------------------------------------------------------------------------------ 163b2165e7aSSebastian Grimberg // Run CUDA kernel with block size selected automatically based on the kernel 164b2165e7aSSebastian Grimberg // (which may use enough registers to require a smaller block size than the 165b2165e7aSSebastian Grimberg // hardware is capable) 166b2165e7aSSebastian Grimberg //------------------------------------------------------------------------------ 1672b730f8bSJeremy L Thompson int CeedRunKernelAutoblockCuda(Ceed ceed, CUfunction kernel, size_t points, void **args) { 168c9c2c079SJeremy L Thompson int min_grid_size, max_block_size; 169ca735530SJeremy L Thompson 1702b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_block_size, kernel, NULL, 0, 0x10000)); 171eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernel_Cuda(ceed, kernel, CeedDivUpInt(points, max_block_size), max_block_size, args)); 172ca735530SJeremy L Thompson return CEED_ERROR_SUCCESS; 173c9c2c079SJeremy L Thompson } 174c9c2c079SJeremy L Thompson 175c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 176c9c2c079SJeremy L Thompson // Run CUDA kernel 177c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 178eb7e6cafSJeremy L Thompson int CeedRunKernel_Cuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size, void **args) { 179eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernelDimShared_Cuda(ceed, kernel, grid_size, block_size, 1, 1, 0, args)); 180c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 181c9c2c079SJeremy L Thompson } 182c9c2c079SJeremy L Thompson 183c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 184c9c2c079SJeremy L Thompson // Run CUDA kernel for spatial dimension 185c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 186eb7e6cafSJeremy L Thompson 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, 1872b730f8bSJeremy L Thompson void **args) { 188eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernelDimShared_Cuda(ceed, kernel, grid_size, block_size_x, block_size_y, block_size_z, 0, args)); 189c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 190c9c2c079SJeremy L Thompson } 191c9c2c079SJeremy L Thompson 192c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 193ea61e9acSJeremy L Thompson // Run CUDA kernel for spatial dimension with shared memory 194c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 195eb7e6cafSJeremy L Thompson int CeedRunKernelDimShared_Cuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size_x, const int block_size_y, 1962b730f8bSJeremy L Thompson const int block_size_z, const int shared_mem_size, void **args) { 197023b8a51Sabdelfattah83 #if CUDA_VERSION >= 9000 198023b8a51Sabdelfattah83 cuFuncSetAttribute(kernel, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shared_mem_size); 199023b8a51Sabdelfattah83 #endif 2002b730f8bSJeremy L Thompson CUresult result = cuLaunchKernel(kernel, grid_size, 1, 1, block_size_x, block_size_y, block_size_z, shared_mem_size, NULL, args, NULL); 201ca735530SJeremy L Thompson 202c9c2c079SJeremy L Thompson if (result == CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES) { 203c9c2c079SJeremy L Thompson int max_threads_per_block, shared_size_bytes, num_regs; 204ca735530SJeremy L Thompson 2052b730f8bSJeremy L Thompson cuFuncGetAttribute(&max_threads_per_block, CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, kernel); 2062b730f8bSJeremy L Thompson cuFuncGetAttribute(&shared_size_bytes, CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES, kernel); 207c9c2c079SJeremy L Thompson cuFuncGetAttribute(&num_regs, CU_FUNC_ATTRIBUTE_NUM_REGS, kernel); 208c9c2c079SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, 209c9c2c079SJeremy L Thompson "CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES: max_threads_per_block %d on block size (%d,%d,%d), shared_size %d, num_regs %d", 2102b730f8bSJeremy L Thompson max_threads_per_block, block_size_x, block_size_y, block_size_z, shared_size_bytes, num_regs); 211c9c2c079SJeremy L Thompson } else CeedChk_Cu(ceed, result); 212c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 213c9c2c079SJeremy L Thompson } 2142a86cc9dSSebastian Grimberg 2152a86cc9dSSebastian Grimberg //------------------------------------------------------------------------------ 216