xref: /libCEED/backends/hip/ceed-hip-compile.cpp (revision 80a9ef0545a39c00cdcaab1ca26f8053604f3120)
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>
2330f4f45fSnbeams #include "ceed-hip.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,
3730f4f45fSnbeams                     const CeedInt numopts, ...) {
3830f4f45fSnbeams   int ierr;
3930f4f45fSnbeams   hipFree(0); // Make sure a Context exists for hiprtc
4030f4f45fSnbeams   hiprtcProgram prog;
4130f4f45fSnbeams 
4230f4f45fSnbeams   // Add hip runtime include to string for generation
4330f4f45fSnbeams   std::ostringstream code;
4430f4f45fSnbeams   code << "\n#include <hip/hip_runtime.h>\n";
4530f4f45fSnbeams 
4630f4f45fSnbeams   // Macro definitions
4730f4f45fSnbeams   // Get kernel specific options, such as kernel constants
4881a63d6fSnbeams   const int optssize = 2;
4981a63d6fSnbeams   const char *opts[optssize];
5030f4f45fSnbeams   if (numopts > 0) {
5130f4f45fSnbeams     va_list args;
5230f4f45fSnbeams     va_start(args, numopts);
5330f4f45fSnbeams     char *name;
5430f4f45fSnbeams     int val;
5530f4f45fSnbeams     for (int i = 0; i < numopts; i++) {
5630f4f45fSnbeams       name = va_arg(args, char *);
5730f4f45fSnbeams       val = va_arg(args, int);
5830f4f45fSnbeams       code << "#define " << name << " " << val << "\n";
5930f4f45fSnbeams     }
6030f4f45fSnbeams     va_end(args);
6130f4f45fSnbeams   }
6230f4f45fSnbeams 
6330f4f45fSnbeams   // Standard backend options
64*80a9ef05SNatalie Beams   if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) {
65*80a9ef05SNatalie Beams     code << "#define CeedScalar float\n";
66*80a9ef05SNatalie Beams   }
67*80a9ef05SNatalie Beams   else {
68e15f9bd0SJeremy L Thompson     code << "#define CeedScalar double\n";
69*80a9ef05SNatalie Beams   }
70e15f9bd0SJeremy L Thompson   code << "#define CeedInt int\n";
71e15f9bd0SJeremy L Thompson   code << "#define CEED_ERROR_SUCCESS 0\n\n";
7230f4f45fSnbeams 
7330f4f45fSnbeams   // Non-macro options
7430f4f45fSnbeams   opts[0] = "-default-device";
7530f4f45fSnbeams   struct hipDeviceProp_t prop;
7630f4f45fSnbeams   Ceed_Hip *ceed_data;
77e15f9bd0SJeremy L Thompson   ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChkBackend(ierr);
7830f4f45fSnbeams   CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->deviceId));
790ea97b06Snbeams   std::string archArg = "--gpu-architecture="  + std::string(prop.gcnArchName);
80972b3d9dSNatalie Beams   opts[1] = archArg.c_str();
8130f4f45fSnbeams 
8230f4f45fSnbeams   // Add string source argument provided in call
8330f4f45fSnbeams   code << source;
8430f4f45fSnbeams 
8530f4f45fSnbeams   // Create Program
8630f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL));
8730f4f45fSnbeams 
8830f4f45fSnbeams   // Compile kernel
8981a63d6fSnbeams   hiprtcResult result = hiprtcCompileProgram(prog, optssize, opts);
9030f4f45fSnbeams   if (result != HIPRTC_SUCCESS) {
9130f4f45fSnbeams     size_t logsize;
9230f4f45fSnbeams     CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &logsize));
9330f4f45fSnbeams     char *log;
94e15f9bd0SJeremy L Thompson     ierr = CeedMalloc(logsize, &log); CeedChkBackend(ierr);
9530f4f45fSnbeams     CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log));
96e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", hiprtcGetErrorString(result), log);
9730f4f45fSnbeams   }
9830f4f45fSnbeams 
9930f4f45fSnbeams   size_t ptxsize;
10030f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptxsize));
10130f4f45fSnbeams   char *ptx;
102e15f9bd0SJeremy L Thompson   ierr = CeedMalloc(ptxsize, &ptx); CeedChkBackend(ierr);
10330f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx));
10417fed040Snbeams   CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog));
10530f4f45fSnbeams 
10630f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLoadData(module, ptx));
107e15f9bd0SJeremy L Thompson   ierr = CeedFree(&ptx); CeedChkBackend(ierr);
10830f4f45fSnbeams 
109e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11030f4f45fSnbeams }
11130f4f45fSnbeams 
11230f4f45fSnbeams //------------------------------------------------------------------------------
11330f4f45fSnbeams // Get HIP kernel
11430f4f45fSnbeams //------------------------------------------------------------------------------
11530f4f45fSnbeams int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name,
11630f4f45fSnbeams                       hipFunction_t *kernel) {
11730f4f45fSnbeams 
11830f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name));
119e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12030f4f45fSnbeams }
12130f4f45fSnbeams 
12230f4f45fSnbeams //------------------------------------------------------------------------------
12330f4f45fSnbeams // Run HIP kernel
12430f4f45fSnbeams //------------------------------------------------------------------------------
12530f4f45fSnbeams int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int gridSize,
12630f4f45fSnbeams                       const int blockSize, void **args) {
12730f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, blockSize, 1,
12830f4f45fSnbeams                                   1, 0, NULL, args, NULL));
129e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13030f4f45fSnbeams }
13130f4f45fSnbeams 
13230f4f45fSnbeams //------------------------------------------------------------------------------
13330f4f45fSnbeams // Run HIP kernel for spatial dimension
13430f4f45fSnbeams //------------------------------------------------------------------------------
13530f4f45fSnbeams int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int gridSize,
13630f4f45fSnbeams                          const int blockSizeX, const int blockSizeY,
13730f4f45fSnbeams                          const int blockSizeZ, void **args) {
13830f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1,
13930f4f45fSnbeams                                   blockSizeX, blockSizeY, blockSizeZ,
14030f4f45fSnbeams                                   0, NULL, args, NULL));
141e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14230f4f45fSnbeams }
14330f4f45fSnbeams 
14430f4f45fSnbeams //------------------------------------------------------------------------------
145e15f9bd0SJeremy L Thompson // Run HIP kernel for spatial dimension with shared memory
14630f4f45fSnbeams //------------------------------------------------------------------------------
14730f4f45fSnbeams int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int gridSize,
14830f4f45fSnbeams                                const int blockSizeX, const int blockSizeY,
14930f4f45fSnbeams                                const int blockSizeZ, const int sharedMemSize,
15030f4f45fSnbeams                                void **args) {
15130f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1,
15230f4f45fSnbeams                                   blockSizeX, blockSizeY, blockSizeZ,
15330f4f45fSnbeams                                   sharedMemSize, NULL, args, NULL));
154e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15530f4f45fSnbeams }
156