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-common.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 num_opts, ...) { 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 opts_size = 2; 49 const char *opts[opts_size]; 50 if (num_opts > 0) { 51 va_list args; 52 va_start(args, num_opts); 53 char *name; 54 int val; 55 for (int i = 0; i < num_opts; 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 if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 65 code << "#define CeedScalar float\n"; 66 } 67 else { 68 code << "#define CeedScalar double\n"; 69 } 70 code << "#define CeedInt int\n"; 71 code << "#define CEED_ERROR_SUCCESS 0\n\n"; 72 73 // Non-macro options 74 opts[0] = "-default-device"; 75 struct hipDeviceProp_t prop; 76 Ceed_Hip *ceed_data; 77 ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChkBackend(ierr); 78 CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id)); 79 std::string arch_arg = "--gpu-architecture=" + std::string(prop.gcnArchName); 80 opts[1] = arch_arg.c_str(); 81 82 // Add string source argument provided in call 83 code << source; 84 85 // Create Program 86 CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 87 88 // Compile kernel 89 hiprtcResult result = hiprtcCompileProgram(prog, opts_size, opts); 90 if (result != HIPRTC_SUCCESS) { 91 size_t log_size; 92 CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size)); 93 char *log; 94 ierr = CeedMalloc(log_size, &log); CeedChkBackend(ierr); 95 CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log)); 96 return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", 97 hiprtcGetErrorString(result), log); 98 } 99 100 size_t ptx_size; 101 CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptx_size)); 102 char *ptx; 103 ierr = CeedMalloc(ptx_size, &ptx); CeedChkBackend(ierr); 104 CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx)); 105 CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog)); 106 107 CeedChk_Hip(ceed, hipModuleLoadData(module, ptx)); 108 ierr = CeedFree(&ptx); CeedChkBackend(ierr); 109 110 return CEED_ERROR_SUCCESS; 111 } 112 113 //------------------------------------------------------------------------------ 114 // Get HIP kernel 115 //------------------------------------------------------------------------------ 116 int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name, 117 hipFunction_t *kernel) { 118 119 CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name)); 120 return CEED_ERROR_SUCCESS; 121 } 122 123 //------------------------------------------------------------------------------ 124 // Run HIP kernel 125 //------------------------------------------------------------------------------ 126 int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int grid_size, 127 const int block_size, void **args) { 128 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size, 1, 129 1, 0, NULL, args, NULL)); 130 return CEED_ERROR_SUCCESS; 131 } 132 133 //------------------------------------------------------------------------------ 134 // Run HIP kernel for spatial dimension 135 //------------------------------------------------------------------------------ 136 int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int grid_size, 137 const int block_size_x, const int block_size_y, 138 const int block_size_z, void **args) { 139 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, 140 block_size_x, block_size_y, block_size_z, 141 0, NULL, args, NULL)); 142 return CEED_ERROR_SUCCESS; 143 } 144 145 //------------------------------------------------------------------------------ 146 // Run HIP kernel for spatial dimension with shared memory 147 //------------------------------------------------------------------------------ 148 int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int grid_size, 149 const int block_size_x, const int block_size_y, 150 const int block_size_z, const int shared_mem_size, 151 void **args) { 152 CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, 153 block_size_x, block_size_y, block_size_z, 154 shared_mem_size, NULL, args, NULL)); 155 return CEED_ERROR_SUCCESS; 156 } 157