13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 330f4f45fSnbeams // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 530f4f45fSnbeams // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 730f4f45fSnbeams 8*2b730f8bSJeremy L Thompson #include "ceed-hip-compile.h" 9*2b730f8bSJeremy L Thompson 10ec3da8bcSJed Brown #include <ceed/backend.h> 11*2b730f8bSJeremy L Thompson #include <ceed/ceed.h> 12c9c2c079SJeremy L Thompson #include <ceed/jit-tools.h> 13*2b730f8bSJeremy L Thompson #include <hip/hiprtc.h> 1430f4f45fSnbeams #include <stdarg.h> 153d576824SJeremy L Thompson #include <string.h> 16*2b730f8bSJeremy L Thompson 17*2b730f8bSJeremy L Thompson #include <sstream> 18*2b730f8bSJeremy L Thompson 197fcac036SJeremy L Thompson #include "ceed-hip-common.h" 2030f4f45fSnbeams 2130f4f45fSnbeams #define CeedChk_hiprtc(ceed, x) \ 2230f4f45fSnbeams do { \ 2330f4f45fSnbeams hiprtcResult result = static_cast<hiprtcResult>(x); \ 24*2b730f8bSJeremy L Thompson if (result != HIPRTC_SUCCESS) return CeedError((ceed), CEED_ERROR_BACKEND, hiprtcGetErrorString(result)); \ 2530f4f45fSnbeams } while (0) 2630f4f45fSnbeams 27*2b730f8bSJeremy L Thompson #define CeedCallHiprtc(ceed, ...) \ 28*2b730f8bSJeremy L Thompson do { \ 29*2b730f8bSJeremy L Thompson int ierr_q_ = __VA_ARGS__; \ 30*2b730f8bSJeremy L Thompson CeedChk_hiprtc(ceed, ierr_q_); \ 31*2b730f8bSJeremy L Thompson } while (0); 32*2b730f8bSJeremy L Thompson 3330f4f45fSnbeams //------------------------------------------------------------------------------ 3430f4f45fSnbeams // Compile HIP kernel 3530f4f45fSnbeams //------------------------------------------------------------------------------ 36*2b730f8bSJeremy L Thompson int CeedCompileHip(Ceed ceed, const char *source, hipModule_t *module, const CeedInt num_defines, ...) { 3730f4f45fSnbeams hipFree(0); // Make sure a Context exists for hiprtc 3830f4f45fSnbeams hiprtcProgram prog; 3930f4f45fSnbeams 4030f4f45fSnbeams std::ostringstream code; 41c9c2c079SJeremy L Thompson 429faa5937SNatalie Beams // Add hip runtime include statement for generation if runtime < 40400000 439faa5937SNatalie Beams // (implies ROCm < 4.5) 449faa5937SNatalie Beams int runtime_version; 45*2b730f8bSJeremy L Thompson CeedCallHip(ceed, hipRuntimeGetVersion(&runtime_version)); 469faa5937SNatalie Beams if (runtime_version < 40400000) { 4730f4f45fSnbeams code << "\n#include <hip/hip_runtime.h>\n"; 489faa5937SNatalie Beams } 499faa5937SNatalie Beams // With ROCm 4.5, need to include these definitions specifically for hiprtc 509faa5937SNatalie Beams // (but cannot include the runtime header) 519faa5937SNatalie Beams else { 529faa5937SNatalie Beams code << "#include <stddef.h>\n"; 539faa5937SNatalie Beams code << "#define __forceinline__ inline __attribute__((always_inline))\n"; 549faa5937SNatalie Beams code << "#define HIP_DYNAMIC_SHARED(type, var) extern __shared__ type var[];\n"; 559faa5937SNatalie Beams } 5630f4f45fSnbeams 57c9c2c079SJeremy L Thompson // Kernel specific options, such as kernel constants 58c9c2c079SJeremy L Thompson if (num_defines > 0) { 5930f4f45fSnbeams va_list args; 60c9c2c079SJeremy L Thompson va_start(args, num_defines); 6130f4f45fSnbeams char *name; 6230f4f45fSnbeams int val; 63c9c2c079SJeremy L Thompson for (int i = 0; i < num_defines; i++) { 6430f4f45fSnbeams name = va_arg(args, char *); 6530f4f45fSnbeams val = va_arg(args, int); 6630f4f45fSnbeams code << "#define " << name << " " << val << "\n"; 6730f4f45fSnbeams } 6830f4f45fSnbeams va_end(args); 6930f4f45fSnbeams } 7030f4f45fSnbeams 71c9c2c079SJeremy L Thompson // Standard libCEED definitions for HIP backends 72c9c2c079SJeremy L Thompson char *jit_defs_path, *jit_defs_source; 73*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-jit.h", &jit_defs_path)); 74*2b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &jit_defs_source)); 75c9c2c079SJeremy L Thompson code << jit_defs_source; 76c9c2c079SJeremy L Thompson code << "\n\n"; 77*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&jit_defs_path)); 78*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&jit_defs_source)); 7930f4f45fSnbeams 8030f4f45fSnbeams // Non-macro options 81c9c2c079SJeremy L Thompson const int num_opts = 3; 82c9c2c079SJeremy L Thompson const char *opts[num_opts]; 8330f4f45fSnbeams opts[0] = "-default-device"; 8430f4f45fSnbeams struct hipDeviceProp_t prop; 8530f4f45fSnbeams Ceed_Hip *ceed_data; 86*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, (void **)&ceed_data)); 87*2b730f8bSJeremy L Thompson CeedCallHip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id)); 880d0321e0SJeremy L Thompson std::string arch_arg = "--gpu-architecture=" + std::string(prop.gcnArchName); 890d0321e0SJeremy L Thompson opts[1] = arch_arg.c_str(); 90b3c5430cSnbeams opts[2] = "-munsafe-fp-atomics"; 9130f4f45fSnbeams 9230f4f45fSnbeams // Add string source argument provided in call 9330f4f45fSnbeams code << source; 9430f4f45fSnbeams 9530f4f45fSnbeams // Create Program 96*2b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 9730f4f45fSnbeams 9830f4f45fSnbeams // Compile kernel 99c9c2c079SJeremy L Thompson hiprtcResult result = hiprtcCompileProgram(prog, num_opts, opts); 10030f4f45fSnbeams if (result != HIPRTC_SUCCESS) { 1010d0321e0SJeremy L Thompson size_t log_size; 1020d0321e0SJeremy L Thompson CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size)); 10330f4f45fSnbeams char *log; 104*2b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(log_size, &log)); 105*2b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcGetProgramLog(prog, log)); 106*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", hiprtcGetErrorString(result), log); 10730f4f45fSnbeams } 10830f4f45fSnbeams 1090d0321e0SJeremy L Thompson size_t ptx_size; 110*2b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcGetCodeSize(prog, &ptx_size)); 11130f4f45fSnbeams char *ptx; 112*2b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(ptx_size, &ptx)); 113*2b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcGetCode(prog, ptx)); 114*2b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcDestroyProgram(&prog)); 11530f4f45fSnbeams 116*2b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleLoadData(module, ptx)); 117*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&ptx)); 11830f4f45fSnbeams 119e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12030f4f45fSnbeams } 12130f4f45fSnbeams 12230f4f45fSnbeams //------------------------------------------------------------------------------ 12330f4f45fSnbeams // Get HIP kernel 12430f4f45fSnbeams //------------------------------------------------------------------------------ 125*2b730f8bSJeremy L Thompson int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name, hipFunction_t *kernel) { 126*2b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleGetFunction(kernel, module, name)); 127e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12830f4f45fSnbeams } 12930f4f45fSnbeams 13030f4f45fSnbeams //------------------------------------------------------------------------------ 13130f4f45fSnbeams // Run HIP kernel 13230f4f45fSnbeams //------------------------------------------------------------------------------ 133*2b730f8bSJeremy L Thompson int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int grid_size, const int block_size, void **args) { 134*2b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size, 1, 1, 0, NULL, args, NULL)); 135e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13630f4f45fSnbeams } 13730f4f45fSnbeams 13830f4f45fSnbeams //------------------------------------------------------------------------------ 13930f4f45fSnbeams // Run HIP kernel for spatial dimension 14030f4f45fSnbeams //------------------------------------------------------------------------------ 141*2b730f8bSJeremy L Thompson int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int grid_size, const int block_size_x, const int block_size_y, const int block_size_z, 142*2b730f8bSJeremy L Thompson void **args) { 143*2b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size_x, block_size_y, block_size_z, 0, NULL, args, NULL)); 144e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14530f4f45fSnbeams } 14630f4f45fSnbeams 14730f4f45fSnbeams //------------------------------------------------------------------------------ 148e15f9bd0SJeremy L Thompson // Run HIP kernel for spatial dimension with shared memory 14930f4f45fSnbeams //------------------------------------------------------------------------------ 150*2b730f8bSJeremy L Thompson int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int grid_size, const int block_size_x, const int block_size_y, 151*2b730f8bSJeremy L Thompson const int block_size_z, const int shared_mem_size, void **args) { 152*2b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size_x, block_size_y, block_size_z, shared_mem_size, NULL, args, NULL)); 153e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15430f4f45fSnbeams } 155