xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-compile.cpp (revision b3c5430c937f651b7adecb6a22cf27440a67bb39)
130f4f45fSnbeams // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
230f4f45fSnbeams // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
330f4f45fSnbeams // All Rights reserved. See files LICENSE and NOTICE for details.
430f4f45fSnbeams //
530f4f45fSnbeams // This file is part of CEED, a collection of benchmarks, miniapps, software
630f4f45fSnbeams // libraries and APIs for efficient high-order finite element and spectral
730f4f45fSnbeams // element discretizations for exascale applications. For more information and
830f4f45fSnbeams // source code availability see http://github.com/ceed.
930f4f45fSnbeams //
1030f4f45fSnbeams // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
1130f4f45fSnbeams // a collaborative effort of two U.S. Department of Energy organizations (Office
1230f4f45fSnbeams // of Science and the National Nuclear Security Administration) responsible for
1330f4f45fSnbeams // the planning and preparation of a capable exascale ecosystem, including
1430f4f45fSnbeams // software, applications, hardware, advanced system engineering and early
1530f4f45fSnbeams // testbed platforms, in support of the nation's exascale computing imperative.
1630f4f45fSnbeams 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
1930f4f45fSnbeams #include <sstream>
2030f4f45fSnbeams #include <stdarg.h>
213d576824SJeremy L Thompson #include <string.h>
2230f4f45fSnbeams #include <hip/hiprtc.h>
237fcac036SJeremy L Thompson #include "ceed-hip-common.h"
2430f4f45fSnbeams #include "ceed-hip-compile.h"
2530f4f45fSnbeams 
2630f4f45fSnbeams #define CeedChk_hiprtc(ceed, x) \
2730f4f45fSnbeams do { \
2830f4f45fSnbeams   hiprtcResult result = static_cast<hiprtcResult>(x); \
2930f4f45fSnbeams   if (result != HIPRTC_SUCCESS) \
30e15f9bd0SJeremy L Thompson     return CeedError((ceed), CEED_ERROR_BACKEND, hiprtcGetErrorString(result)); \
3130f4f45fSnbeams } while (0)
3230f4f45fSnbeams 
3330f4f45fSnbeams //------------------------------------------------------------------------------
3430f4f45fSnbeams // Compile HIP kernel
3530f4f45fSnbeams //------------------------------------------------------------------------------
3630f4f45fSnbeams int CeedCompileHip(Ceed ceed, const char *source, hipModule_t *module,
370d0321e0SJeremy L Thompson                     const CeedInt num_opts, ...) {
3830f4f45fSnbeams   int ierr;
3930f4f45fSnbeams   hipFree(0); // Make sure a Context exists for hiprtc
4030f4f45fSnbeams   hiprtcProgram prog;
4130f4f45fSnbeams 
4230f4f45fSnbeams   std::ostringstream code;
439faa5937SNatalie Beams   // Add hip runtime include statement for generation if runtime < 40400000
449faa5937SNatalie Beams   // (implies ROCm < 4.5)
459faa5937SNatalie Beams   int runtime_version;
469faa5937SNatalie Beams   CeedChk_Hip(ceed, hipRuntimeGetVersion(&runtime_version));
479faa5937SNatalie Beams   if (runtime_version < 40400000) {
4830f4f45fSnbeams     code << "\n#include <hip/hip_runtime.h>\n";
499faa5937SNatalie Beams   }
509faa5937SNatalie Beams   // With ROCm 4.5, need to include these definitions specifically for hiprtc
519faa5937SNatalie Beams   // (but cannot include the runtime header)
529faa5937SNatalie Beams   else {
539faa5937SNatalie Beams     code << "#include <stddef.h>\n";
549faa5937SNatalie Beams     code << "#define __forceinline__ inline __attribute__((always_inline))\n";
559faa5937SNatalie Beams     code << "#define HIP_DYNAMIC_SHARED(type, var) extern __shared__ type var[];\n";
569faa5937SNatalie Beams   }
5730f4f45fSnbeams 
5830f4f45fSnbeams   // Macro definitions
5930f4f45fSnbeams   // Get kernel specific options, such as kernel constants
60*b3c5430cSnbeams   const int opts_size = 3;
610d0321e0SJeremy L Thompson   const char *opts[opts_size];
620d0321e0SJeremy L Thompson   if (num_opts > 0) {
6330f4f45fSnbeams     va_list args;
640d0321e0SJeremy L Thompson     va_start(args, num_opts);
6530f4f45fSnbeams     char *name;
6630f4f45fSnbeams     int val;
670d0321e0SJeremy L Thompson     for (int i = 0; i < num_opts; i++) {
6830f4f45fSnbeams       name = va_arg(args, char *);
6930f4f45fSnbeams       val = va_arg(args, int);
7030f4f45fSnbeams       code << "#define " << name << " " << val << "\n";
7130f4f45fSnbeams     }
7230f4f45fSnbeams     va_end(args);
7330f4f45fSnbeams   }
7430f4f45fSnbeams 
7530f4f45fSnbeams   // Standard backend options
7680a9ef05SNatalie Beams   if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) {
7780a9ef05SNatalie Beams     code << "#define CeedScalar float\n";
7880a9ef05SNatalie Beams   }
7980a9ef05SNatalie Beams   else {
80e15f9bd0SJeremy L Thompson     code << "#define CeedScalar double\n";
8180a9ef05SNatalie Beams   }
82e15f9bd0SJeremy L Thompson   code << "#define CeedInt int\n";
83e15f9bd0SJeremy L Thompson   code << "#define CEED_ERROR_SUCCESS 0\n\n";
8430f4f45fSnbeams 
8530f4f45fSnbeams   // Non-macro options
8630f4f45fSnbeams   opts[0] = "-default-device";
8730f4f45fSnbeams   struct hipDeviceProp_t prop;
8830f4f45fSnbeams   Ceed_Hip *ceed_data;
89e15f9bd0SJeremy L Thompson   ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChkBackend(ierr);
900d0321e0SJeremy L Thompson   CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id));
910d0321e0SJeremy L Thompson   std::string arch_arg = "--gpu-architecture="  + std::string(prop.gcnArchName);
920d0321e0SJeremy L Thompson   opts[1] = arch_arg.c_str();
93*b3c5430cSnbeams   opts[2] = "-munsafe-fp-atomics";
9430f4f45fSnbeams 
9530f4f45fSnbeams   // Add string source argument provided in call
9630f4f45fSnbeams   code << source;
9730f4f45fSnbeams 
9830f4f45fSnbeams   // Create Program
9930f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL));
10030f4f45fSnbeams 
10130f4f45fSnbeams   // Compile kernel
1020d0321e0SJeremy L Thompson   hiprtcResult result = hiprtcCompileProgram(prog, opts_size, opts);
10330f4f45fSnbeams   if (result != HIPRTC_SUCCESS) {
1040d0321e0SJeremy L Thompson     size_t log_size;
1050d0321e0SJeremy L Thompson     CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size));
10630f4f45fSnbeams     char *log;
1070d0321e0SJeremy L Thompson     ierr = CeedMalloc(log_size, &log); CeedChkBackend(ierr);
10830f4f45fSnbeams     CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log));
1090d0321e0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s",
1100d0321e0SJeremy L Thompson                      hiprtcGetErrorString(result), log);
11130f4f45fSnbeams   }
11230f4f45fSnbeams 
1130d0321e0SJeremy L Thompson   size_t ptx_size;
1140d0321e0SJeremy L Thompson   CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptx_size));
11530f4f45fSnbeams   char *ptx;
1160d0321e0SJeremy L Thompson   ierr = CeedMalloc(ptx_size, &ptx); CeedChkBackend(ierr);
11730f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx));
11817fed040Snbeams   CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog));
11930f4f45fSnbeams 
12030f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLoadData(module, ptx));
121e15f9bd0SJeremy L Thompson   ierr = CeedFree(&ptx); CeedChkBackend(ierr);
12230f4f45fSnbeams 
123e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12430f4f45fSnbeams }
12530f4f45fSnbeams 
12630f4f45fSnbeams //------------------------------------------------------------------------------
12730f4f45fSnbeams // Get HIP kernel
12830f4f45fSnbeams //------------------------------------------------------------------------------
12930f4f45fSnbeams int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name,
13030f4f45fSnbeams                       hipFunction_t *kernel) {
13130f4f45fSnbeams 
13230f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name));
133e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13430f4f45fSnbeams }
13530f4f45fSnbeams 
13630f4f45fSnbeams //------------------------------------------------------------------------------
13730f4f45fSnbeams // Run HIP kernel
13830f4f45fSnbeams //------------------------------------------------------------------------------
1390d0321e0SJeremy L Thompson int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int grid_size,
1400d0321e0SJeremy L Thompson                       const int block_size, void **args) {
1410d0321e0SJeremy L Thompson   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size, 1,
14230f4f45fSnbeams                                   1, 0, NULL, args, NULL));
143e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14430f4f45fSnbeams }
14530f4f45fSnbeams 
14630f4f45fSnbeams //------------------------------------------------------------------------------
14730f4f45fSnbeams // Run HIP kernel for spatial dimension
14830f4f45fSnbeams //------------------------------------------------------------------------------
1490d0321e0SJeremy L Thompson int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int grid_size,
1500d0321e0SJeremy L Thompson                          const int block_size_x, const int block_size_y,
1510d0321e0SJeremy L Thompson                          const int block_size_z, void **args) {
1520d0321e0SJeremy L Thompson   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1,
1530d0321e0SJeremy L Thompson                                   block_size_x, block_size_y, block_size_z,
15430f4f45fSnbeams                                   0, NULL, args, NULL));
155e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15630f4f45fSnbeams }
15730f4f45fSnbeams 
15830f4f45fSnbeams //------------------------------------------------------------------------------
159e15f9bd0SJeremy L Thompson // Run HIP kernel for spatial dimension with shared memory
16030f4f45fSnbeams //------------------------------------------------------------------------------
1610d0321e0SJeremy L Thompson int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int grid_size,
1620d0321e0SJeremy L Thompson                                const int block_size_x, const int block_size_y,
1630d0321e0SJeremy L Thompson                                const int block_size_z, const int shared_mem_size,
16430f4f45fSnbeams                                void **args) {
1650d0321e0SJeremy L Thompson   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1,
1660d0321e0SJeremy L Thompson                                   block_size_x, block_size_y, block_size_z,
1670d0321e0SJeremy L Thompson                                   shared_mem_size, NULL, args, NULL));
168e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16930f4f45fSnbeams }
170