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.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), x, 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 optslen = 32; 49 const int optssize = 2; 50 const char *opts[optssize]; 51 if (numopts > 0) { 52 va_list args; 53 va_start(args, numopts); 54 char *name; 55 int val; 56 for (int i = 0; i < numopts; i++) { 57 name = va_arg(args, char *); 58 val = va_arg(args, int); 59 code << "#define " << name << " " << val << "\n"; 60 } 61 va_end(args); 62 } 63 64 // Standard backend options 65 code << "#define CeedScalar double\n#define CeedInt int\n\n"; 66 67 // Non-macro options 68 opts[0] = "-default-device"; 69 struct hipDeviceProp_t prop; 70 Ceed_Hip *ceed_data; 71 ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChk(ierr); 72 CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->deviceId)); 73 char buff[optslen]; 74 std::string gfxName = "gfx" + std::to_string(prop.gcnArch); 75 std::string archArg = "--gpu-architecture=" + gfxName; 76 snprintf(buff, optslen, "%s", archArg.c_str()); 77 opts[1] = buff; 78 79 // Add string source argument provided in call 80 code << source; 81 82 // Create Program 83 CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 84 85 // Compile kernel 86 hiprtcResult result = hiprtcCompileProgram(prog, optssize, opts); 87 if (result != HIPRTC_SUCCESS) { 88 size_t logsize; 89 CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &logsize)); 90 char *log; 91 ierr = CeedMalloc(logsize, &log); CeedChk(ierr); 92 CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log)); 93 return CeedError(ceed, (int)result, "%s\n%s", hiprtcGetErrorString(result), log); 94 } 95 96 size_t ptxsize; 97 CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptxsize)); 98 char *ptx; 99 ierr = CeedMalloc(ptxsize, &ptx); CeedChk(ierr); 100 CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx)); 101 CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog)); 102 103 CeedChk_Hip(ceed, hipModuleLoadData(module, ptx)); 104 ierr = CeedFree(&ptx); CeedChk(ierr); 105 106 return 0; 107 } 108 109 //------------------------------------------------------------------------------ 110 // Get HIP kernel 111 //------------------------------------------------------------------------------ 112 int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name, 113 hipFunction_t *kernel) { 114 115 CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name)); 116 return 0; 117 } 118 119 //------------------------------------------------------------------------------ 120 // Run HIP kernel 121 //------------------------------------------------------------------------------ 122 int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 123 const int blockSize, void **args) { 124 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, blockSize, 1, 125 1, 0, NULL, args, NULL)); 126 return 0; 127 } 128 129 //------------------------------------------------------------------------------ 130 // Run HIP kernel for spatial dimension 131 //------------------------------------------------------------------------------ 132 int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 133 const int blockSizeX, const int blockSizeY, 134 const int blockSizeZ, void **args) { 135 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, 136 blockSizeX, blockSizeY, blockSizeZ, 137 0, NULL, args, NULL)); 138 return 0; 139 } 140 141 //------------------------------------------------------------------------------ 142 // Run HIP kernel for spatial dimension with sharde memory 143 //------------------------------------------------------------------------------ 144 int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 145 const int blockSizeX, const int blockSizeY, 146 const int blockSizeZ, const int sharedMemSize, 147 void **args) { 148 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, 149 blockSizeX, blockSizeY, blockSizeZ, 150 sharedMemSize, NULL, args, NULL)); 151 return 0; 152 } 153