1c9c2c079SJeremy L Thompson // Copyright (c) 2017-2022, 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 8*2b730f8bSJeremy L Thompson #include "ceed-cuda-compile.h" 9*2b730f8bSJeremy L Thompson 10c9c2c079SJeremy L Thompson #include <ceed/backend.h> 11*2b730f8bSJeremy L Thompson #include <ceed/ceed.h> 12c9c2c079SJeremy L Thompson #include <ceed/jit-tools.h> 13c9c2c079SJeremy L Thompson #include <cuda.h> 14c9c2c079SJeremy L Thompson #include <cuda_runtime.h> 15c9c2c079SJeremy L Thompson #include <nvrtc.h> 16c9c2c079SJeremy L Thompson #include <stdarg.h> 17c9c2c079SJeremy L Thompson #include <string.h> 18*2b730f8bSJeremy L Thompson 19*2b730f8bSJeremy L Thompson #include <sstream> 20*2b730f8bSJeremy L Thompson 21c9c2c079SJeremy L Thompson #include "ceed-cuda-common.h" 22c9c2c079SJeremy L Thompson 23c9c2c079SJeremy L Thompson #define CeedChk_Nvrtc(ceed, x) \ 24c9c2c079SJeremy L Thompson do { \ 25c9c2c079SJeremy L Thompson nvrtcResult result = static_cast<nvrtcResult>(x); \ 26*2b730f8bSJeremy L Thompson if (result != NVRTC_SUCCESS) return CeedError((ceed), CEED_ERROR_BACKEND, nvrtcGetErrorString(result)); \ 27c9c2c079SJeremy L Thompson } while (0) 28c9c2c079SJeremy L Thompson 29*2b730f8bSJeremy L Thompson #define CeedCallNvrtc(ceed, ...) \ 30*2b730f8bSJeremy L Thompson do { \ 31*2b730f8bSJeremy L Thompson int ierr_q_ = __VA_ARGS__; \ 32*2b730f8bSJeremy L Thompson CeedChk_Nvrtc(ceed, ierr_q_); \ 33*2b730f8bSJeremy L Thompson } while (0); 34*2b730f8bSJeremy L Thompson 35c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 36c9c2c079SJeremy L Thompson // Compile CUDA kernel 37c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 38*2b730f8bSJeremy L Thompson int CeedCompileCuda(Ceed ceed, const char *source, CUmodule *module, const CeedInt num_defines, ...) { 39c9c2c079SJeremy L Thompson cudaFree(0); // Make sure a Context exists for nvrtc 40c9c2c079SJeremy L Thompson nvrtcProgram prog; 41c9c2c079SJeremy L Thompson 42c9c2c079SJeremy L Thompson std::ostringstream code; 43c9c2c079SJeremy L Thompson 44c9c2c079SJeremy L Thompson // Get kernel specific options, such as kernel constants 45c9c2c079SJeremy L Thompson if (num_defines > 0) { 46c9c2c079SJeremy L Thompson va_list args; 47c9c2c079SJeremy L Thompson va_start(args, num_defines); 48c9c2c079SJeremy L Thompson char *name; 49c9c2c079SJeremy L Thompson int val; 50c9c2c079SJeremy L Thompson for (int i = 0; i < num_defines; i++) { 51c9c2c079SJeremy L Thompson name = va_arg(args, char *); 52c9c2c079SJeremy L Thompson val = va_arg(args, int); 53c9c2c079SJeremy L Thompson code << "#define " << name << " " << val << "\n"; 54c9c2c079SJeremy L Thompson } 55c9c2c079SJeremy L Thompson va_end(args); 56c9c2c079SJeremy L Thompson } 57c9c2c079SJeremy L Thompson 58c9c2c079SJeremy L Thompson // Standard libCEED definitions for CUDA backends 59c9c2c079SJeremy L Thompson char *jit_defs_path, *jit_defs_source; 60*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-jit.h", &jit_defs_path)); 61*2b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &jit_defs_source)); 62c9c2c079SJeremy L Thompson code << jit_defs_source; 63c9c2c079SJeremy L Thompson code << "\n\n"; 64*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&jit_defs_path)); 65*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&jit_defs_source)); 66c9c2c079SJeremy L Thompson 67c9c2c079SJeremy L Thompson // Non-macro options 68c9c2c079SJeremy L Thompson const int num_opts = 3; 69c9c2c079SJeremy L Thompson const char *opts[num_opts]; 70c9c2c079SJeremy L Thompson opts[0] = "-default-device"; 71c9c2c079SJeremy L Thompson struct cudaDeviceProp prop; 72c9c2c079SJeremy L Thompson Ceed_Cuda *ceed_data; 73*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &ceed_data)); 74*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaGetDeviceProperties(&prop, ceed_data->device_id)); 75c9c2c079SJeremy L Thompson std::string arch_arg = "-arch=compute_" + std::to_string(prop.major) + std::to_string(prop.minor); 76c9c2c079SJeremy L Thompson opts[1] = arch_arg.c_str(); 77c9c2c079SJeremy L Thompson opts[2] = "-Dint32_t=int"; 78c9c2c079SJeremy L Thompson 79c9c2c079SJeremy L Thompson // Add string source argument provided in call 80c9c2c079SJeremy L Thompson code << source; 81c9c2c079SJeremy L Thompson 82c9c2c079SJeremy L Thompson // Create Program 83*2b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 84c9c2c079SJeremy L Thompson 85c9c2c079SJeremy L Thompson // Compile kernel 86c9c2c079SJeremy L Thompson nvrtcResult result = nvrtcCompileProgram(prog, num_opts, opts); 87c9c2c079SJeremy L Thompson if (result != NVRTC_SUCCESS) { 88c9c2c079SJeremy L Thompson size_t log_size; 89*2b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcGetProgramLogSize(prog, &log_size)); 90c9c2c079SJeremy L Thompson char *log; 91*2b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(log_size, &log)); 92*2b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcGetProgramLog(prog, log)); 93*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", nvrtcGetErrorString(result), log); 94c9c2c079SJeremy L Thompson } 95c9c2c079SJeremy L Thompson 96c9c2c079SJeremy L Thompson size_t ptx_size; 97*2b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcGetPTXSize(prog, &ptx_size)); 98c9c2c079SJeremy L Thompson char *ptx; 99*2b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(ptx_size, &ptx)); 100*2b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcGetPTX(prog, ptx)); 101*2b730f8bSJeremy L Thompson CeedCallNvrtc(ceed, nvrtcDestroyProgram(&prog)); 102c9c2c079SJeremy L Thompson 103*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleLoadData(module, ptx)); 104*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&ptx)); 105c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 106c9c2c079SJeremy L Thompson } 107c9c2c079SJeremy L Thompson 108c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 109c9c2c079SJeremy L Thompson // Get CUDA kernel 110c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 111*2b730f8bSJeremy L Thompson int CeedGetKernelCuda(Ceed ceed, CUmodule module, const char *name, CUfunction *kernel) { 112*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleGetFunction(kernel, module, name)); 113c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 114c9c2c079SJeremy L Thompson } 115c9c2c079SJeremy L Thompson 116c9c2c079SJeremy L Thompson // Run kernel with block size selected automatically based on the kernel (which 117c9c2c079SJeremy L Thompson // may use enough registers to require a smaller block size than the hardware is 118c9c2c079SJeremy L Thompson // capable). 119*2b730f8bSJeremy L Thompson int CeedRunKernelAutoblockCuda(Ceed ceed, CUfunction kernel, size_t points, void **args) { 120c9c2c079SJeremy L Thompson int min_grid_size, max_block_size; 121*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_block_size, kernel, NULL, 0, 0x10000)); 122*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRunKernelCuda(ceed, kernel, CeedDivUpInt(points, max_block_size), max_block_size, args)); 123c9c2c079SJeremy L Thompson return 0; 124c9c2c079SJeremy L Thompson } 125c9c2c079SJeremy L Thompson 126c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 127c9c2c079SJeremy L Thompson // Run CUDA kernel 128c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 129*2b730f8bSJeremy L Thompson int CeedRunKernelCuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size, void **args) { 130*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRunKernelDimSharedCuda(ceed, kernel, grid_size, block_size, 1, 1, 0, args)); 131c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 132c9c2c079SJeremy L Thompson } 133c9c2c079SJeremy L Thompson 134c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 135c9c2c079SJeremy L Thompson // Run CUDA kernel for spatial dimension 136c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 137*2b730f8bSJeremy L Thompson int CeedRunKernelDimCuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size_x, const int block_size_y, const int block_size_z, 138*2b730f8bSJeremy L Thompson void **args) { 139*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRunKernelDimSharedCuda(ceed, kernel, grid_size, block_size_x, block_size_y, block_size_z, 0, args)); 140c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 141c9c2c079SJeremy L Thompson } 142c9c2c079SJeremy L Thompson 143c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 144c9c2c079SJeremy L Thompson // Run CUDA kernel for spatial dimension with sharde memory 145c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 146*2b730f8bSJeremy L Thompson int CeedRunKernelDimSharedCuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size_x, const int block_size_y, 147*2b730f8bSJeremy L Thompson const int block_size_z, const int shared_mem_size, void **args) { 148*2b730f8bSJeremy 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); 149c9c2c079SJeremy L Thompson if (result == CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES) { 150c9c2c079SJeremy L Thompson int max_threads_per_block, shared_size_bytes, num_regs; 151*2b730f8bSJeremy L Thompson cuFuncGetAttribute(&max_threads_per_block, CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, kernel); 152*2b730f8bSJeremy L Thompson cuFuncGetAttribute(&shared_size_bytes, CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES, kernel); 153c9c2c079SJeremy L Thompson cuFuncGetAttribute(&num_regs, CU_FUNC_ATTRIBUTE_NUM_REGS, kernel); 154c9c2c079SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, 155c9c2c079SJeremy 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", 156*2b730f8bSJeremy L Thompson max_threads_per_block, block_size_x, block_size_y, block_size_z, shared_size_bytes, num_regs); 157c9c2c079SJeremy L Thompson } else CeedChk_Cu(ceed, result); 158c9c2c079SJeremy L Thompson return CEED_ERROR_SUCCESS; 159c9c2c079SJeremy L Thompson } 160c9c2c079SJeremy L Thompson 161c9c2c079SJeremy L Thompson //------------------------------------------------------------------------------ 162