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