xref: /libCEED/backends/hip/ceed-hip-compile.cpp (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include "ceed-hip-compile.h"
9 
10 #include <ceed/backend.h>
11 #include <ceed/ceed.h>
12 #include <ceed/jit-tools.h>
13 #include <hip/hiprtc.h>
14 #include <stdarg.h>
15 #include <string.h>
16 
17 #include <sstream>
18 
19 #include "ceed-hip-common.h"
20 
21 #define CeedChk_hiprtc(ceed, x)                                                                               \
22   do {                                                                                                        \
23     hiprtcResult result = static_cast<hiprtcResult>(x);                                                       \
24     if (result != HIPRTC_SUCCESS) return CeedError((ceed), CEED_ERROR_BACKEND, hiprtcGetErrorString(result)); \
25   } while (0)
26 
27 #define CeedCallHiprtc(ceed, ...)  \
28   do {                             \
29     int ierr_q_ = __VA_ARGS__;     \
30     CeedChk_hiprtc(ceed, ierr_q_); \
31   } while (0);
32 
33 //------------------------------------------------------------------------------
34 // Compile HIP kernel
35 //------------------------------------------------------------------------------
36 int CeedCompileHip(Ceed ceed, const char *source, hipModule_t *module, const CeedInt num_defines, ...) {
37   hipFree(0);  // Make sure a Context exists for hiprtc
38   hiprtcProgram prog;
39 
40   std::ostringstream code;
41 
42   // Add hip runtime include statement for generation if runtime < 40400000
43   // (implies ROCm < 4.5)
44   int runtime_version;
45   CeedCallHip(ceed, hipRuntimeGetVersion(&runtime_version));
46   if (runtime_version < 40400000) {
47     code << "\n#include <hip/hip_runtime.h>\n";
48   }
49   // With ROCm 4.5, need to include these definitions specifically for hiprtc
50   // (but cannot include the runtime header)
51   else {
52     code << "#include <stddef.h>\n";
53     code << "#define __forceinline__ inline __attribute__((always_inline))\n";
54     code << "#define HIP_DYNAMIC_SHARED(type, var) extern __shared__ type var[];\n";
55   }
56 
57   // Kernel specific options, such as kernel constants
58   if (num_defines > 0) {
59     va_list args;
60     va_start(args, num_defines);
61     char *name;
62     int   val;
63     for (int i = 0; i < num_defines; i++) {
64       name = va_arg(args, char *);
65       val  = va_arg(args, int);
66       code << "#define " << name << " " << val << "\n";
67     }
68     va_end(args);
69   }
70 
71   // Standard libCEED definitions for HIP backends
72   char *jit_defs_path, *jit_defs_source;
73   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-jit.h", &jit_defs_path));
74   CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &jit_defs_source));
75   code << jit_defs_source;
76   code << "\n\n";
77   CeedCallBackend(CeedFree(&jit_defs_path));
78   CeedCallBackend(CeedFree(&jit_defs_source));
79 
80   // Non-macro options
81   const int   num_opts = 3;
82   const char *opts[num_opts];
83   opts[0] = "-default-device";
84   struct hipDeviceProp_t prop;
85   Ceed_Hip              *ceed_data;
86   CeedCallBackend(CeedGetData(ceed, (void **)&ceed_data));
87   CeedCallHip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id));
88   std::string arch_arg = "--gpu-architecture=" + std::string(prop.gcnArchName);
89   opts[1]              = arch_arg.c_str();
90   opts[2]              = "-munsafe-fp-atomics";
91 
92   // Add string source argument provided in call
93   code << source;
94 
95   // Create Program
96   CeedCallHiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL));
97 
98   // Compile kernel
99   hiprtcResult result = hiprtcCompileProgram(prog, num_opts, opts);
100   if (result != HIPRTC_SUCCESS) {
101     size_t log_size;
102     CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size));
103     char *log;
104     CeedCallBackend(CeedMalloc(log_size, &log));
105     CeedCallHiprtc(ceed, hiprtcGetProgramLog(prog, log));
106     return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", hiprtcGetErrorString(result), log);
107   }
108 
109   size_t ptx_size;
110   CeedCallHiprtc(ceed, hiprtcGetCodeSize(prog, &ptx_size));
111   char *ptx;
112   CeedCallBackend(CeedMalloc(ptx_size, &ptx));
113   CeedCallHiprtc(ceed, hiprtcGetCode(prog, ptx));
114   CeedCallHiprtc(ceed, hiprtcDestroyProgram(&prog));
115 
116   CeedCallHip(ceed, hipModuleLoadData(module, ptx));
117   CeedCallBackend(CeedFree(&ptx));
118 
119   return CEED_ERROR_SUCCESS;
120 }
121 
122 //------------------------------------------------------------------------------
123 // Get HIP kernel
124 //------------------------------------------------------------------------------
125 int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name, hipFunction_t *kernel) {
126   CeedCallHip(ceed, hipModuleGetFunction(kernel, module, name));
127   return CEED_ERROR_SUCCESS;
128 }
129 
130 //------------------------------------------------------------------------------
131 // Run HIP kernel
132 //------------------------------------------------------------------------------
133 int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int grid_size, const int block_size, void **args) {
134   CeedCallHip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size, 1, 1, 0, NULL, args, NULL));
135   return CEED_ERROR_SUCCESS;
136 }
137 
138 //------------------------------------------------------------------------------
139 // Run HIP kernel for spatial dimension
140 //------------------------------------------------------------------------------
141 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                         void **args) {
143   CeedCallHip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size_x, block_size_y, block_size_z, 0, NULL, args, NULL));
144   return CEED_ERROR_SUCCESS;
145 }
146 
147 //------------------------------------------------------------------------------
148 // Run HIP kernel for spatial dimension with shared memory
149 //------------------------------------------------------------------------------
150 int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int grid_size, const int block_size_x, const int block_size_y,
151                               const int block_size_z, const int shared_mem_size, void **args) {
152   CeedCallHip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size_x, block_size_y, block_size_z, shared_mem_size, NULL, args, NULL));
153   return CEED_ERROR_SUCCESS;
154 }
155