15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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 82b730f8bSJeremy L Thompson #include "ceed-hip-compile.h" 92b730f8bSJeremy L Thompson 1049aac155SJeremy L Thompson #include <ceed.h> 11ec3da8bcSJed Brown #include <ceed/backend.h> 12c9c2c079SJeremy L Thompson #include <ceed/jit-tools.h> 1330f4f45fSnbeams #include <stdarg.h> 143d576824SJeremy L Thompson #include <string.h> 15c85e8640SSebastian Grimberg #include <hip/hiprtc.h> 162b730f8bSJeremy L Thompson 172b730f8bSJeremy L Thompson #include <sstream> 182b730f8bSJeremy 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); \ 242b730f8bSJeremy L Thompson if (result != HIPRTC_SUCCESS) return CeedError((ceed), CEED_ERROR_BACKEND, hiprtcGetErrorString(result)); \ 2530f4f45fSnbeams } while (0) 2630f4f45fSnbeams 272b730f8bSJeremy L Thompson #define CeedCallHiprtc(ceed, ...) \ 282b730f8bSJeremy L Thompson do { \ 292b730f8bSJeremy L Thompson int ierr_q_ = __VA_ARGS__; \ 302b730f8bSJeremy L Thompson CeedChk_hiprtc(ceed, ierr_q_); \ 316574a04fSJeremy L Thompson } while (0) 322b730f8bSJeremy L Thompson 3330f4f45fSnbeams //------------------------------------------------------------------------------ 3430f4f45fSnbeams // Compile HIP kernel 3530f4f45fSnbeams //------------------------------------------------------------------------------ 36eb7e6cafSJeremy L Thompson int CeedCompile_Hip(Ceed ceed, const char *source, hipModule_t *module, const CeedInt num_defines, ...) { 37b7453713SJeremy L Thompson size_t ptx_size; 3822070f95SJeremy L Thompson char *jit_defs_source, *ptx; 3922070f95SJeremy L Thompson const char *jit_defs_path; 40*a491a57eSJeremy L Thompson const int num_opts = 4; 41b13efd58SJeremy L Thompson CeedInt num_jit_source_dirs = 0; 42b13efd58SJeremy L Thompson const char **opts; 43b7453713SJeremy L Thompson int runtime_version; 4430f4f45fSnbeams hiprtcProgram prog; 45b7453713SJeremy L Thompson struct hipDeviceProp_t prop; 46b7453713SJeremy L Thompson Ceed_Hip *ceed_data; 47b7453713SJeremy L Thompson 48b7453713SJeremy L Thompson hipFree(0); // Make sure a Context exists for hiprtc 4930f4f45fSnbeams 5030f4f45fSnbeams std::ostringstream code; 51c9c2c079SJeremy L Thompson 52ea61e9acSJeremy L Thompson // Add hip runtime include statement for generation if runtime < 40400000 (implies ROCm < 4.5) 532b730f8bSJeremy L Thompson CeedCallHip(ceed, hipRuntimeGetVersion(&runtime_version)); 549faa5937SNatalie Beams if (runtime_version < 40400000) { 5530f4f45fSnbeams code << "\n#include <hip/hip_runtime.h>\n"; 569faa5937SNatalie Beams } 57ea61e9acSJeremy L Thompson // With ROCm 4.5, need to include these definitions specifically for hiprtc (but cannot include the runtime header) 589faa5937SNatalie Beams else { 599faa5937SNatalie Beams code << "#include <stddef.h>\n"; 609faa5937SNatalie Beams code << "#define __forceinline__ inline __attribute__((always_inline))\n"; 619faa5937SNatalie Beams code << "#define HIP_DYNAMIC_SHARED(type, var) extern __shared__ type var[];\n"; 629faa5937SNatalie Beams } 6330f4f45fSnbeams 64c9c2c079SJeremy L Thompson // Kernel specific options, such as kernel constants 65c9c2c079SJeremy L Thompson if (num_defines > 0) { 6630f4f45fSnbeams va_list args; 67c9c2c079SJeremy L Thompson va_start(args, num_defines); 6830f4f45fSnbeams char *name; 6930f4f45fSnbeams int val; 70b7453713SJeremy L Thompson 71c9c2c079SJeremy L Thompson for (int i = 0; i < num_defines; i++) { 7230f4f45fSnbeams name = va_arg(args, char *); 7330f4f45fSnbeams val = va_arg(args, int); 7430f4f45fSnbeams code << "#define " << name << " " << val << "\n"; 7530f4f45fSnbeams } 7630f4f45fSnbeams va_end(args); 7730f4f45fSnbeams } 7830f4f45fSnbeams 79c9c2c079SJeremy L Thompson // Standard libCEED definitions for HIP backends 802b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-jit.h", &jit_defs_path)); 812b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &jit_defs_source)); 82c9c2c079SJeremy L Thompson code << jit_defs_source; 83c9c2c079SJeremy L Thompson code << "\n\n"; 84edaedbd9SJeremy L Thompson CeedCallBackend(CeedFree(&jit_defs_path)); 85edaedbd9SJeremy L Thompson CeedCallBackend(CeedFree(&jit_defs_source)); 8630f4f45fSnbeams 8730f4f45fSnbeams // Non-macro options 88b13efd58SJeremy L Thompson CeedCallBackend(CeedCalloc(num_opts, &opts)); 8930f4f45fSnbeams opts[0] = "-default-device"; 902b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, (void **)&ceed_data)); 912b730f8bSJeremy L Thompson CeedCallHip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id)); 920d0321e0SJeremy L Thompson std::string arch_arg = "--gpu-architecture=" + std::string(prop.gcnArchName); 930d0321e0SJeremy L Thompson opts[1] = arch_arg.c_str(); 94b3c5430cSnbeams opts[2] = "-munsafe-fp-atomics"; 95*a491a57eSJeremy L Thompson opts[3] = "-DCEED_RUNNING_JIT_PASS=1"; 96b13efd58SJeremy L Thompson { 97b13efd58SJeremy L Thompson const char **jit_source_dirs; 98b13efd58SJeremy L Thompson 99b13efd58SJeremy L Thompson CeedCallBackend(CeedGetJitSourceRoots(ceed, &num_jit_source_dirs, &jit_source_dirs)); 100b13efd58SJeremy L Thompson CeedCallBackend(CeedRealloc(num_opts + num_jit_source_dirs, &opts)); 101b13efd58SJeremy L Thompson for (CeedInt i = 0; i < num_jit_source_dirs; i++) { 102b13efd58SJeremy L Thompson std::ostringstream include_dirs_arg; 103b13efd58SJeremy L Thompson 104b13efd58SJeremy L Thompson include_dirs_arg << "-I" << jit_source_dirs[i]; 105b13efd58SJeremy L Thompson CeedCallBackend(CeedStringAllocCopy(include_dirs_arg.str().c_str(), (char **)&opts[num_opts + i])); 106b13efd58SJeremy L Thompson } 107b13efd58SJeremy L Thompson CeedCallBackend(CeedRestoreJitSourceRoots(ceed, &jit_source_dirs)); 108b13efd58SJeremy L Thompson } 10930f4f45fSnbeams 11030f4f45fSnbeams // Add string source argument provided in call 11130f4f45fSnbeams code << source; 11230f4f45fSnbeams 11330f4f45fSnbeams // Create Program 1142b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 11530f4f45fSnbeams 11630f4f45fSnbeams // Compile kernel 117b13efd58SJeremy L Thompson hiprtcResult result = hiprtcCompileProgram(prog, num_opts + num_jit_source_dirs, opts); 118b7453713SJeremy L Thompson 119b13efd58SJeremy L Thompson for (CeedInt i = 0; i < num_jit_source_dirs; i++) { 120b13efd58SJeremy L Thompson CeedCallBackend(CeedFree(&opts[num_opts + i])); 121b13efd58SJeremy L Thompson } 122b13efd58SJeremy L Thompson CeedCallBackend(CeedFree(&opts)); 12330f4f45fSnbeams if (result != HIPRTC_SUCCESS) { 1240d0321e0SJeremy L Thompson size_t log_size; 12530f4f45fSnbeams char *log; 126b7453713SJeremy L Thompson 127a4bfdec2SJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n"); 128f5a0ec79SJeremy L Thompson CeedDebug(ceed, "Source:\n%s\n", code.str().c_str()); 129a4bfdec2SJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n"); 130b7453713SJeremy L Thompson CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size)); 1312b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(log_size, &log)); 1322b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcGetProgramLog(prog, log)); 1332b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", hiprtcGetErrorString(result), log); 13430f4f45fSnbeams } 13530f4f45fSnbeams 1362b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcGetCodeSize(prog, &ptx_size)); 1372b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(ptx_size, &ptx)); 1382b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcGetCode(prog, ptx)); 1392b730f8bSJeremy L Thompson CeedCallHiprtc(ceed, hiprtcDestroyProgram(&prog)); 14030f4f45fSnbeams 1412b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleLoadData(module, ptx)); 1422b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&ptx)); 143e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14430f4f45fSnbeams } 14530f4f45fSnbeams 14630f4f45fSnbeams //------------------------------------------------------------------------------ 14730f4f45fSnbeams // Get HIP kernel 14830f4f45fSnbeams //------------------------------------------------------------------------------ 149eb7e6cafSJeremy L Thompson int CeedGetKernel_Hip(Ceed ceed, hipModule_t module, const char *name, hipFunction_t *kernel) { 1502b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleGetFunction(kernel, module, name)); 151e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15230f4f45fSnbeams } 15330f4f45fSnbeams 15430f4f45fSnbeams //------------------------------------------------------------------------------ 15530f4f45fSnbeams // Run HIP kernel 15630f4f45fSnbeams //------------------------------------------------------------------------------ 157eb7e6cafSJeremy L Thompson int CeedRunKernel_Hip(Ceed ceed, hipFunction_t kernel, const int grid_size, const int block_size, void **args) { 1582b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size, 1, 1, 0, NULL, args, NULL)); 159e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 16030f4f45fSnbeams } 16130f4f45fSnbeams 16230f4f45fSnbeams //------------------------------------------------------------------------------ 16330f4f45fSnbeams // Run HIP kernel for spatial dimension 16430f4f45fSnbeams //------------------------------------------------------------------------------ 165eb7e6cafSJeremy L Thompson int CeedRunKernelDim_Hip(Ceed ceed, hipFunction_t kernel, const int grid_size, const int block_size_x, const int block_size_y, const int block_size_z, 1662b730f8bSJeremy L Thompson void **args) { 1672b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size_x, block_size_y, block_size_z, 0, NULL, args, NULL)); 168e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 16930f4f45fSnbeams } 17030f4f45fSnbeams 17130f4f45fSnbeams //------------------------------------------------------------------------------ 172e15f9bd0SJeremy L Thompson // Run HIP kernel for spatial dimension with shared memory 17330f4f45fSnbeams //------------------------------------------------------------------------------ 174eb7e6cafSJeremy L Thompson int CeedRunKernelDimShared_Hip(Ceed ceed, hipFunction_t kernel, const int grid_size, const int block_size_x, const int block_size_y, 1752b730f8bSJeremy L Thompson const int block_size_z, const int shared_mem_size, void **args) { 1762b730f8bSJeremy 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)); 177e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 17830f4f45fSnbeams } 1792a86cc9dSSebastian Grimberg 1802a86cc9dSSebastian Grimberg //------------------------------------------------------------------------------ 181