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-backend.h> 18 #include <string.h> 19 #include <sstream> 20 #include <stdarg.h> 21 #include <hip/hiprtc.h> 22 #include "ceed-hip.h" 23 #include "ceed-hip-compile.h" 24 25 #define CeedChk_hiprtc(ceed, x) \ 26 do { \ 27 hiprtcResult result = static_cast<hiprtcResult>(x); \ 28 if (result != HIPRTC_SUCCESS) \ 29 return CeedError((ceed), x, hiprtcGetErrorString(result)); \ 30 } while (0) 31 32 //------------------------------------------------------------------------------ 33 // Compile HIP kernel 34 //------------------------------------------------------------------------------ 35 int CeedCompileHip(Ceed ceed, const char *source, hipModule_t *module, 36 const CeedInt numopts, ...) { 37 int ierr; 38 hipFree(0); // Make sure a Context exists for hiprtc 39 hiprtcProgram prog; 40 41 // Add hip runtime include to string for generation 42 std::ostringstream code; 43 code << "\n#include <hip/hip_runtime.h>\n"; 44 45 // Macro definitions 46 // Get kernel specific options, such as kernel constants 47 const int optslen = 32; 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#define CeedInt int\n\n"; 65 66 // Non-macro options 67 opts[0] = "-default-device"; 68 struct hipDeviceProp_t prop; 69 Ceed_Hip *ceed_data; 70 ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChk(ierr); 71 CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->deviceId)); 72 char buff[optslen]; 73 std::string gfxName = "gfx" + std::to_string(prop.gcnArch); 74 std::string archArg = "--gpu-architecture=" + gfxName; 75 snprintf(buff, optslen, archArg.c_str()); 76 opts[1] = buff; 77 78 // Add string source argument provided in call 79 code << source; 80 81 // Create Program 82 CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 83 84 // Compile kernel 85 hiprtcResult result = hiprtcCompileProgram(prog, optssize, opts); 86 if (result != HIPRTC_SUCCESS) { 87 size_t logsize; 88 CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &logsize)); 89 char *log; 90 ierr = CeedMalloc(logsize, &log); CeedChk(ierr); 91 CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log)); 92 return CeedError(ceed, (int)result, "%s\n%s", hiprtcGetErrorString(result), log); 93 } 94 95 size_t ptxsize; 96 CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptxsize)); 97 char *ptx; 98 ierr = CeedMalloc(ptxsize, &ptx); CeedChk(ierr); 99 CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx)); 100 // CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog)); 101 102 CeedChk_Hip(ceed, hipModuleLoadData(module, ptx)); 103 ierr = CeedFree(&ptx); CeedChk(ierr); 104 105 return 0; 106 } 107 108 //------------------------------------------------------------------------------ 109 // Get HIP kernel 110 //------------------------------------------------------------------------------ 111 int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name, 112 hipFunction_t *kernel) { 113 114 CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name)); 115 return 0; 116 } 117 118 //------------------------------------------------------------------------------ 119 // Run HIP kernel 120 //------------------------------------------------------------------------------ 121 int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 122 const int blockSize, void **args) { 123 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, blockSize, 1, 124 1, 0, NULL, args, NULL)); 125 return 0; 126 } 127 128 //------------------------------------------------------------------------------ 129 // Run HIP kernel for spatial dimension 130 //------------------------------------------------------------------------------ 131 int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 132 const int blockSizeX, const int blockSizeY, 133 const int blockSizeZ, void **args) { 134 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, 135 blockSizeX, blockSizeY, blockSizeZ, 136 0, NULL, args, NULL)); 137 return 0; 138 } 139 140 //------------------------------------------------------------------------------ 141 // Run HIP kernel for spatial dimension with sharde memory 142 //------------------------------------------------------------------------------ 143 int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 144 const int blockSizeX, const int blockSizeY, 145 const int blockSizeZ, const int sharedMemSize, 146 void **args) { 147 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, 148 blockSizeX, blockSizeY, blockSizeZ, 149 sharedMemSize, NULL, args, NULL)); 150 return 0; 151 } 152