xref: /libCEED/backends/hip/ceed-hip-compile.cpp (revision c9c2c07970382857cc7b4a28d359710237b91a3e) !
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 
8ec3da8bcSJed Brown #include <ceed/ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
10*c9c2c079SJeremy L Thompson #include <ceed/jit-tools.h>
1130f4f45fSnbeams #include <sstream>
1230f4f45fSnbeams #include <stdarg.h>
133d576824SJeremy L Thompson #include <string.h>
1430f4f45fSnbeams #include <hip/hiprtc.h>
157fcac036SJeremy L Thompson #include "ceed-hip-common.h"
1630f4f45fSnbeams #include "ceed-hip-compile.h"
1730f4f45fSnbeams 
1830f4f45fSnbeams #define CeedChk_hiprtc(ceed, x) \
1930f4f45fSnbeams do { \
2030f4f45fSnbeams   hiprtcResult result = static_cast<hiprtcResult>(x); \
2130f4f45fSnbeams   if (result != HIPRTC_SUCCESS) \
22e15f9bd0SJeremy L Thompson     return CeedError((ceed), CEED_ERROR_BACKEND, hiprtcGetErrorString(result)); \
2330f4f45fSnbeams } while (0)
2430f4f45fSnbeams 
2530f4f45fSnbeams //------------------------------------------------------------------------------
2630f4f45fSnbeams // Compile HIP kernel
2730f4f45fSnbeams //------------------------------------------------------------------------------
2830f4f45fSnbeams int CeedCompileHip(Ceed ceed, const char *source, hipModule_t *module,
29*c9c2c079SJeremy L Thompson                     const CeedInt num_defines, ...) {
3030f4f45fSnbeams   int ierr;
3130f4f45fSnbeams   hipFree(0); // Make sure a Context exists for hiprtc
3230f4f45fSnbeams   hiprtcProgram prog;
3330f4f45fSnbeams 
3430f4f45fSnbeams   std::ostringstream code;
35*c9c2c079SJeremy L Thompson 
369faa5937SNatalie Beams   // Add hip runtime include statement for generation if runtime < 40400000
379faa5937SNatalie Beams   // (implies ROCm < 4.5)
389faa5937SNatalie Beams   int runtime_version;
399faa5937SNatalie Beams   CeedChk_Hip(ceed, hipRuntimeGetVersion(&runtime_version));
409faa5937SNatalie Beams   if (runtime_version < 40400000) {
4130f4f45fSnbeams     code << "\n#include <hip/hip_runtime.h>\n";
429faa5937SNatalie Beams   }
439faa5937SNatalie Beams   // With ROCm 4.5, need to include these definitions specifically for hiprtc
449faa5937SNatalie Beams   // (but cannot include the runtime header)
459faa5937SNatalie Beams   else {
469faa5937SNatalie Beams     code << "#include <stddef.h>\n";
479faa5937SNatalie Beams     code << "#define __forceinline__ inline __attribute__((always_inline))\n";
489faa5937SNatalie Beams     code << "#define HIP_DYNAMIC_SHARED(type, var) extern __shared__ type var[];\n";
499faa5937SNatalie Beams   }
5030f4f45fSnbeams 
51*c9c2c079SJeremy L Thompson   // Kernel specific options, such as kernel constants
52*c9c2c079SJeremy L Thompson   if (num_defines > 0) {
5330f4f45fSnbeams     va_list args;
54*c9c2c079SJeremy L Thompson     va_start(args, num_defines);
5530f4f45fSnbeams     char *name;
5630f4f45fSnbeams     int val;
57*c9c2c079SJeremy L Thompson     for (int i = 0; i < num_defines; i++) {
5830f4f45fSnbeams       name = va_arg(args, char *);
5930f4f45fSnbeams       val = va_arg(args, int);
6030f4f45fSnbeams       code << "#define " << name << " " << val << "\n";
6130f4f45fSnbeams     }
6230f4f45fSnbeams     va_end(args);
6330f4f45fSnbeams   }
6430f4f45fSnbeams 
65*c9c2c079SJeremy L Thompson   // Standard libCEED definitions for HIP backends
66*c9c2c079SJeremy L Thompson   char *jit_defs_path, *jit_defs_source;
67*c9c2c079SJeremy L Thompson   ierr = CeedGetJitAbsolutePath(ceed,
68*c9c2c079SJeremy L Thompson                                 "ceed/jit-source/hip/hip-jit.h",
69*c9c2c079SJeremy L Thompson                                 &jit_defs_path); CeedChkBackend(ierr);
70*c9c2c079SJeremy L Thompson   ierr = CeedLoadSourceToBuffer(ceed, jit_defs_path, &jit_defs_source);
71*c9c2c079SJeremy L Thompson   CeedChkBackend(ierr);
72*c9c2c079SJeremy L Thompson   code << jit_defs_source;
73*c9c2c079SJeremy L Thompson   code << "\n\n";
74*c9c2c079SJeremy L Thompson   ierr = CeedFree(&jit_defs_path); CeedChkBackend(ierr);
75*c9c2c079SJeremy L Thompson   ierr = CeedFree(&jit_defs_source); CeedChkBackend(ierr);
7630f4f45fSnbeams 
7730f4f45fSnbeams   // Non-macro options
78*c9c2c079SJeremy L Thompson   const int num_opts = 3;
79*c9c2c079SJeremy L Thompson   const char *opts[num_opts];
8030f4f45fSnbeams   opts[0] = "-default-device";
8130f4f45fSnbeams   struct hipDeviceProp_t prop;
8230f4f45fSnbeams   Ceed_Hip *ceed_data;
83e15f9bd0SJeremy L Thompson   ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChkBackend(ierr);
840d0321e0SJeremy L Thompson   CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id));
850d0321e0SJeremy L Thompson   std::string arch_arg = "--gpu-architecture="  + std::string(prop.gcnArchName);
860d0321e0SJeremy L Thompson   opts[1] = arch_arg.c_str();
87b3c5430cSnbeams   opts[2] = "-munsafe-fp-atomics";
8830f4f45fSnbeams 
8930f4f45fSnbeams   // Add string source argument provided in call
9030f4f45fSnbeams   code << source;
9130f4f45fSnbeams 
9230f4f45fSnbeams   // Create Program
9330f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL));
9430f4f45fSnbeams 
9530f4f45fSnbeams   // Compile kernel
96*c9c2c079SJeremy L Thompson   hiprtcResult result = hiprtcCompileProgram(prog, num_opts, opts);
9730f4f45fSnbeams   if (result != HIPRTC_SUCCESS) {
980d0321e0SJeremy L Thompson     size_t log_size;
990d0321e0SJeremy L Thompson     CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size));
10030f4f45fSnbeams     char *log;
1010d0321e0SJeremy L Thompson     ierr = CeedMalloc(log_size, &log); CeedChkBackend(ierr);
10230f4f45fSnbeams     CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log));
1030d0321e0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s",
1040d0321e0SJeremy L Thompson                      hiprtcGetErrorString(result), log);
10530f4f45fSnbeams   }
10630f4f45fSnbeams 
1070d0321e0SJeremy L Thompson   size_t ptx_size;
1080d0321e0SJeremy L Thompson   CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptx_size));
10930f4f45fSnbeams   char *ptx;
1100d0321e0SJeremy L Thompson   ierr = CeedMalloc(ptx_size, &ptx); CeedChkBackend(ierr);
11130f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx));
11217fed040Snbeams   CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog));
11330f4f45fSnbeams 
11430f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLoadData(module, ptx));
115e15f9bd0SJeremy L Thompson   ierr = CeedFree(&ptx); CeedChkBackend(ierr);
11630f4f45fSnbeams 
117e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11830f4f45fSnbeams }
11930f4f45fSnbeams 
12030f4f45fSnbeams //------------------------------------------------------------------------------
12130f4f45fSnbeams // Get HIP kernel
12230f4f45fSnbeams //------------------------------------------------------------------------------
12330f4f45fSnbeams int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name,
12430f4f45fSnbeams                       hipFunction_t *kernel) {
12530f4f45fSnbeams 
12630f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name));
127e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12830f4f45fSnbeams }
12930f4f45fSnbeams 
13030f4f45fSnbeams //------------------------------------------------------------------------------
13130f4f45fSnbeams // Run HIP kernel
13230f4f45fSnbeams //------------------------------------------------------------------------------
1330d0321e0SJeremy L Thompson int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int grid_size,
1340d0321e0SJeremy L Thompson                       const int block_size, void **args) {
1350d0321e0SJeremy L Thompson   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size, 1,
13630f4f45fSnbeams                                   1, 0, NULL, args, NULL));
137e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13830f4f45fSnbeams }
13930f4f45fSnbeams 
14030f4f45fSnbeams //------------------------------------------------------------------------------
14130f4f45fSnbeams // Run HIP kernel for spatial dimension
14230f4f45fSnbeams //------------------------------------------------------------------------------
1430d0321e0SJeremy L Thompson int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int grid_size,
1440d0321e0SJeremy L Thompson                          const int block_size_x, const int block_size_y,
1450d0321e0SJeremy L Thompson                          const int block_size_z, void **args) {
1460d0321e0SJeremy L Thompson   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1,
1470d0321e0SJeremy L Thompson                                   block_size_x, block_size_y, block_size_z,
14830f4f45fSnbeams                                   0, NULL, args, NULL));
149e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15030f4f45fSnbeams }
15130f4f45fSnbeams 
15230f4f45fSnbeams //------------------------------------------------------------------------------
153e15f9bd0SJeremy L Thompson // Run HIP kernel for spatial dimension with shared memory
15430f4f45fSnbeams //------------------------------------------------------------------------------
1550d0321e0SJeremy L Thompson int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int grid_size,
1560d0321e0SJeremy L Thompson                                const int block_size_x, const int block_size_y,
1570d0321e0SJeremy L Thompson                                const int block_size_z, const int shared_mem_size,
15830f4f45fSnbeams                                void **args) {
1590d0321e0SJeremy L Thompson   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1,
1600d0321e0SJeremy L Thompson                                   block_size_x, block_size_y, block_size_z,
1610d0321e0SJeremy L Thompson                                   shared_mem_size, NULL, args, NULL));
162e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16330f4f45fSnbeams }
164