xref: /libCEED/backends/hip/ceed-hip-compile.cpp (revision 30f4f45fbb110fe49bb316a924b47463b018a077)
1*30f4f45fSnbeams // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2*30f4f45fSnbeams // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3*30f4f45fSnbeams // All Rights reserved. See files LICENSE and NOTICE for details.
4*30f4f45fSnbeams //
5*30f4f45fSnbeams // This file is part of CEED, a collection of benchmarks, miniapps, software
6*30f4f45fSnbeams // libraries and APIs for efficient high-order finite element and spectral
7*30f4f45fSnbeams // element discretizations for exascale applications. For more information and
8*30f4f45fSnbeams // source code availability see http://github.com/ceed.
9*30f4f45fSnbeams //
10*30f4f45fSnbeams // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11*30f4f45fSnbeams // a collaborative effort of two U.S. Department of Energy organizations (Office
12*30f4f45fSnbeams // of Science and the National Nuclear Security Administration) responsible for
13*30f4f45fSnbeams // the planning and preparation of a capable exascale ecosystem, including
14*30f4f45fSnbeams // software, applications, hardware, advanced system engineering and early
15*30f4f45fSnbeams // testbed platforms, in support of the nation's exascale computing imperative.
16*30f4f45fSnbeams 
17*30f4f45fSnbeams #include <ceed-backend.h>
18*30f4f45fSnbeams #include <string.h>
19*30f4f45fSnbeams #include <sstream>
20*30f4f45fSnbeams #include <stdarg.h>
21*30f4f45fSnbeams #include <hip/hiprtc.h>
22*30f4f45fSnbeams #include "ceed-hip.h"
23*30f4f45fSnbeams #include "ceed-hip-compile.h"
24*30f4f45fSnbeams 
25*30f4f45fSnbeams #define CeedChk_hiprtc(ceed, x) \
26*30f4f45fSnbeams do { \
27*30f4f45fSnbeams   hiprtcResult result = static_cast<hiprtcResult>(x); \
28*30f4f45fSnbeams   if (result != HIPRTC_SUCCESS) \
29*30f4f45fSnbeams     return CeedError((ceed), x, hiprtcGetErrorString(result)); \
30*30f4f45fSnbeams } while (0)
31*30f4f45fSnbeams 
32*30f4f45fSnbeams //------------------------------------------------------------------------------
33*30f4f45fSnbeams // Compile HIP kernel
34*30f4f45fSnbeams //------------------------------------------------------------------------------
35*30f4f45fSnbeams int CeedCompileHip(Ceed ceed, const char *source, hipModule_t *module,
36*30f4f45fSnbeams                     const CeedInt numopts, ...) {
37*30f4f45fSnbeams   int ierr;
38*30f4f45fSnbeams   hipFree(0); // Make sure a Context exists for hiprtc
39*30f4f45fSnbeams   hiprtcProgram prog;
40*30f4f45fSnbeams 
41*30f4f45fSnbeams   // Add hip runtime include to string for generation
42*30f4f45fSnbeams   std::ostringstream code;
43*30f4f45fSnbeams   code << "\n#include <hip/hip_runtime.h>\n";
44*30f4f45fSnbeams 
45*30f4f45fSnbeams   // Macro definitions
46*30f4f45fSnbeams   // Get kernel specific options, such as kernel constants
47*30f4f45fSnbeams   const int optslen = 32;
48*30f4f45fSnbeams   const int optsextra = 2;
49*30f4f45fSnbeams   const char *opts[optsextra];
50*30f4f45fSnbeams   if (numopts > 0) {
51*30f4f45fSnbeams     va_list args;
52*30f4f45fSnbeams     va_start(args, numopts);
53*30f4f45fSnbeams     char *name;
54*30f4f45fSnbeams     int val;
55*30f4f45fSnbeams     for (int i = 0; i < numopts; i++) {
56*30f4f45fSnbeams       name = va_arg(args, char *);
57*30f4f45fSnbeams       val = va_arg(args, int);
58*30f4f45fSnbeams       code << "#define " << name << " " << val << "\n";
59*30f4f45fSnbeams     }
60*30f4f45fSnbeams     va_end(args);
61*30f4f45fSnbeams   }
62*30f4f45fSnbeams 
63*30f4f45fSnbeams   // Standard backend options
64*30f4f45fSnbeams   code << "#define CeedScalar double\n#define CeedInt int\n\n";
65*30f4f45fSnbeams 
66*30f4f45fSnbeams   // Non-macro options
67*30f4f45fSnbeams   opts[0] = "-default-device";
68*30f4f45fSnbeams   struct hipDeviceProp_t prop;
69*30f4f45fSnbeams   Ceed_Hip *ceed_data;
70*30f4f45fSnbeams   ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChk(ierr);
71*30f4f45fSnbeams   CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->deviceId));
72*30f4f45fSnbeams   char buff[optslen];
73*30f4f45fSnbeams   std::string gfxName = "gfx" + std::to_string(prop.gcnArch);
74*30f4f45fSnbeams   std::string archArg = "--gpu-architecture="  + gfxName;
75*30f4f45fSnbeams   snprintf(buff, optslen, archArg.c_str());
76*30f4f45fSnbeams   opts[1] = buff;
77*30f4f45fSnbeams 
78*30f4f45fSnbeams   // Add string source argument provided in call
79*30f4f45fSnbeams   code << source;
80*30f4f45fSnbeams 
81*30f4f45fSnbeams   // Create Program
82*30f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL));
83*30f4f45fSnbeams 
84*30f4f45fSnbeams   // Compile kernel
85*30f4f45fSnbeams   hiprtcResult result = hiprtcCompileProgram(prog, numopts + optsextra, opts);
86*30f4f45fSnbeams   if (result != HIPRTC_SUCCESS) {
87*30f4f45fSnbeams     size_t logsize;
88*30f4f45fSnbeams     CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &logsize));
89*30f4f45fSnbeams     char *log;
90*30f4f45fSnbeams     ierr = CeedMalloc(logsize, &log); CeedChk(ierr);
91*30f4f45fSnbeams     CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log));
92*30f4f45fSnbeams     return CeedError(ceed, (int)result, "%s\n%s", hiprtcGetErrorString(result), log);
93*30f4f45fSnbeams   }
94*30f4f45fSnbeams 
95*30f4f45fSnbeams   size_t ptxsize;
96*30f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptxsize));
97*30f4f45fSnbeams   char *ptx;
98*30f4f45fSnbeams   ierr = CeedMalloc(ptxsize, &ptx); CeedChk(ierr);
99*30f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx));
100*30f4f45fSnbeams   CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog));
101*30f4f45fSnbeams 
102*30f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLoadData(module, ptx));
103*30f4f45fSnbeams   ierr = CeedFree(&ptx); CeedChk(ierr);
104*30f4f45fSnbeams 
105*30f4f45fSnbeams   return 0;
106*30f4f45fSnbeams }
107*30f4f45fSnbeams 
108*30f4f45fSnbeams //------------------------------------------------------------------------------
109*30f4f45fSnbeams // Get HIP kernel
110*30f4f45fSnbeams //------------------------------------------------------------------------------
111*30f4f45fSnbeams int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name,
112*30f4f45fSnbeams                       hipFunction_t *kernel) {
113*30f4f45fSnbeams 
114*30f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name));
115*30f4f45fSnbeams   return 0;
116*30f4f45fSnbeams }
117*30f4f45fSnbeams 
118*30f4f45fSnbeams //------------------------------------------------------------------------------
119*30f4f45fSnbeams // Run HIP kernel
120*30f4f45fSnbeams //------------------------------------------------------------------------------
121*30f4f45fSnbeams int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int gridSize,
122*30f4f45fSnbeams                       const int blockSize, void **args) {
123*30f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, blockSize, 1,
124*30f4f45fSnbeams                                   1, 0, NULL, args, NULL));
125*30f4f45fSnbeams   return 0;
126*30f4f45fSnbeams }
127*30f4f45fSnbeams 
128*30f4f45fSnbeams //------------------------------------------------------------------------------
129*30f4f45fSnbeams // Run HIP kernel for spatial dimension
130*30f4f45fSnbeams //------------------------------------------------------------------------------
131*30f4f45fSnbeams int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int gridSize,
132*30f4f45fSnbeams                          const int blockSizeX, const int blockSizeY,
133*30f4f45fSnbeams                          const int blockSizeZ, void **args) {
134*30f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1,
135*30f4f45fSnbeams                                   blockSizeX, blockSizeY, blockSizeZ,
136*30f4f45fSnbeams                                   0, NULL, args, NULL));
137*30f4f45fSnbeams   return 0;
138*30f4f45fSnbeams }
139*30f4f45fSnbeams 
140*30f4f45fSnbeams //------------------------------------------------------------------------------
141*30f4f45fSnbeams // Run HIP kernel for spatial dimension with sharde memory
142*30f4f45fSnbeams //------------------------------------------------------------------------------
143*30f4f45fSnbeams int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int gridSize,
144*30f4f45fSnbeams                                const int blockSizeX, const int blockSizeY,
145*30f4f45fSnbeams                                const int blockSizeZ, const int sharedMemSize,
146*30f4f45fSnbeams                                void **args) {
147*30f4f45fSnbeams   CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1,
148*30f4f45fSnbeams                                   blockSizeX, blockSizeY, blockSizeZ,
149*30f4f45fSnbeams                                   sharedMemSize, NULL, args, NULL));
150*30f4f45fSnbeams   return 0;
151*30f4f45fSnbeams }
152