130f4f45fSnbeams // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC. 230f4f45fSnbeams // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707. 330f4f45fSnbeams // All Rights reserved. See files LICENSE and NOTICE for details. 430f4f45fSnbeams // 530f4f45fSnbeams // This file is part of CEED, a collection of benchmarks, miniapps, software 630f4f45fSnbeams // libraries and APIs for efficient high-order finite element and spectral 730f4f45fSnbeams // element discretizations for exascale applications. For more information and 830f4f45fSnbeams // source code availability see http://github.com/ceed. 930f4f45fSnbeams // 1030f4f45fSnbeams // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 1130f4f45fSnbeams // a collaborative effort of two U.S. Department of Energy organizations (Office 1230f4f45fSnbeams // of Science and the National Nuclear Security Administration) responsible for 1330f4f45fSnbeams // the planning and preparation of a capable exascale ecosystem, including 1430f4f45fSnbeams // software, applications, hardware, advanced system engineering and early 1530f4f45fSnbeams // testbed platforms, in support of the nation's exascale computing imperative. 1630f4f45fSnbeams 17ec3da8bcSJed Brown #include <ceed/ceed.h> 18ec3da8bcSJed Brown #include <ceed/backend.h> 1930f4f45fSnbeams #include <sstream> 2030f4f45fSnbeams #include <stdarg.h> 213d576824SJeremy L Thompson #include <string.h> 2230f4f45fSnbeams #include <hip/hiprtc.h> 237fcac036SJeremy L Thompson #include "ceed-hip-common.h" 2430f4f45fSnbeams #include "ceed-hip-compile.h" 2530f4f45fSnbeams 2630f4f45fSnbeams #define CeedChk_hiprtc(ceed, x) \ 2730f4f45fSnbeams do { \ 2830f4f45fSnbeams hiprtcResult result = static_cast<hiprtcResult>(x); \ 2930f4f45fSnbeams if (result != HIPRTC_SUCCESS) \ 30e15f9bd0SJeremy L Thompson return CeedError((ceed), CEED_ERROR_BACKEND, hiprtcGetErrorString(result)); \ 3130f4f45fSnbeams } while (0) 3230f4f45fSnbeams 3330f4f45fSnbeams //------------------------------------------------------------------------------ 3430f4f45fSnbeams // Compile HIP kernel 3530f4f45fSnbeams //------------------------------------------------------------------------------ 3630f4f45fSnbeams int CeedCompileHip(Ceed ceed, const char *source, hipModule_t *module, 37*0d0321e0SJeremy L Thompson const CeedInt num_opts, ...) { 3830f4f45fSnbeams int ierr; 3930f4f45fSnbeams hipFree(0); // Make sure a Context exists for hiprtc 4030f4f45fSnbeams hiprtcProgram prog; 4130f4f45fSnbeams 4230f4f45fSnbeams // Add hip runtime include to string for generation 4330f4f45fSnbeams std::ostringstream code; 4430f4f45fSnbeams code << "\n#include <hip/hip_runtime.h>\n"; 4530f4f45fSnbeams 4630f4f45fSnbeams // Macro definitions 4730f4f45fSnbeams // Get kernel specific options, such as kernel constants 48*0d0321e0SJeremy L Thompson const int opts_size = 2; 49*0d0321e0SJeremy L Thompson const char *opts[opts_size]; 50*0d0321e0SJeremy L Thompson if (num_opts > 0) { 5130f4f45fSnbeams va_list args; 52*0d0321e0SJeremy L Thompson va_start(args, num_opts); 5330f4f45fSnbeams char *name; 5430f4f45fSnbeams int val; 55*0d0321e0SJeremy L Thompson for (int i = 0; i < num_opts; i++) { 5630f4f45fSnbeams name = va_arg(args, char *); 5730f4f45fSnbeams val = va_arg(args, int); 5830f4f45fSnbeams code << "#define " << name << " " << val << "\n"; 5930f4f45fSnbeams } 6030f4f45fSnbeams va_end(args); 6130f4f45fSnbeams } 6230f4f45fSnbeams 6330f4f45fSnbeams // Standard backend options 6480a9ef05SNatalie Beams if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 6580a9ef05SNatalie Beams code << "#define CeedScalar float\n"; 6680a9ef05SNatalie Beams } 6780a9ef05SNatalie Beams else { 68e15f9bd0SJeremy L Thompson code << "#define CeedScalar double\n"; 6980a9ef05SNatalie Beams } 70e15f9bd0SJeremy L Thompson code << "#define CeedInt int\n"; 71e15f9bd0SJeremy L Thompson code << "#define CEED_ERROR_SUCCESS 0\n\n"; 7230f4f45fSnbeams 7330f4f45fSnbeams // Non-macro options 7430f4f45fSnbeams opts[0] = "-default-device"; 7530f4f45fSnbeams struct hipDeviceProp_t prop; 7630f4f45fSnbeams Ceed_Hip *ceed_data; 77e15f9bd0SJeremy L Thompson ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChkBackend(ierr); 78*0d0321e0SJeremy L Thompson CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id)); 79*0d0321e0SJeremy L Thompson std::string arch_arg = "--gpu-architecture=" + std::string(prop.gcnArchName); 80*0d0321e0SJeremy L Thompson opts[1] = arch_arg.c_str(); 8130f4f45fSnbeams 8230f4f45fSnbeams // Add string source argument provided in call 8330f4f45fSnbeams code << source; 8430f4f45fSnbeams 8530f4f45fSnbeams // Create Program 8630f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 8730f4f45fSnbeams 8830f4f45fSnbeams // Compile kernel 89*0d0321e0SJeremy L Thompson hiprtcResult result = hiprtcCompileProgram(prog, opts_size, opts); 9030f4f45fSnbeams if (result != HIPRTC_SUCCESS) { 91*0d0321e0SJeremy L Thompson size_t log_size; 92*0d0321e0SJeremy L Thompson CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size)); 9330f4f45fSnbeams char *log; 94*0d0321e0SJeremy L Thompson ierr = CeedMalloc(log_size, &log); CeedChkBackend(ierr); 9530f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log)); 96*0d0321e0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", 97*0d0321e0SJeremy L Thompson hiprtcGetErrorString(result), log); 9830f4f45fSnbeams } 9930f4f45fSnbeams 100*0d0321e0SJeremy L Thompson size_t ptx_size; 101*0d0321e0SJeremy L Thompson CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptx_size)); 10230f4f45fSnbeams char *ptx; 103*0d0321e0SJeremy L Thompson ierr = CeedMalloc(ptx_size, &ptx); CeedChkBackend(ierr); 10430f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx)); 10517fed040Snbeams CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog)); 10630f4f45fSnbeams 10730f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLoadData(module, ptx)); 108e15f9bd0SJeremy L Thompson ierr = CeedFree(&ptx); CeedChkBackend(ierr); 10930f4f45fSnbeams 110e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11130f4f45fSnbeams } 11230f4f45fSnbeams 11330f4f45fSnbeams //------------------------------------------------------------------------------ 11430f4f45fSnbeams // Get HIP kernel 11530f4f45fSnbeams //------------------------------------------------------------------------------ 11630f4f45fSnbeams int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name, 11730f4f45fSnbeams hipFunction_t *kernel) { 11830f4f45fSnbeams 11930f4f45fSnbeams CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name)); 120e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12130f4f45fSnbeams } 12230f4f45fSnbeams 12330f4f45fSnbeams //------------------------------------------------------------------------------ 12430f4f45fSnbeams // Run HIP kernel 12530f4f45fSnbeams //------------------------------------------------------------------------------ 126*0d0321e0SJeremy L Thompson int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int grid_size, 127*0d0321e0SJeremy L Thompson const int block_size, void **args) { 128*0d0321e0SJeremy L Thompson CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, block_size, 1, 12930f4f45fSnbeams 1, 0, NULL, args, NULL)); 130e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13130f4f45fSnbeams } 13230f4f45fSnbeams 13330f4f45fSnbeams //------------------------------------------------------------------------------ 13430f4f45fSnbeams // Run HIP kernel for spatial dimension 13530f4f45fSnbeams //------------------------------------------------------------------------------ 136*0d0321e0SJeremy L Thompson int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int grid_size, 137*0d0321e0SJeremy L Thompson const int block_size_x, const int block_size_y, 138*0d0321e0SJeremy L Thompson const int block_size_z, void **args) { 139*0d0321e0SJeremy L Thompson CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, 140*0d0321e0SJeremy L Thompson block_size_x, block_size_y, block_size_z, 14130f4f45fSnbeams 0, NULL, args, NULL)); 142e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14330f4f45fSnbeams } 14430f4f45fSnbeams 14530f4f45fSnbeams //------------------------------------------------------------------------------ 146e15f9bd0SJeremy L Thompson // Run HIP kernel for spatial dimension with shared memory 14730f4f45fSnbeams //------------------------------------------------------------------------------ 148*0d0321e0SJeremy L Thompson int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int grid_size, 149*0d0321e0SJeremy L Thompson const int block_size_x, const int block_size_y, 150*0d0321e0SJeremy L Thompson const int block_size_z, const int shared_mem_size, 15130f4f45fSnbeams void **args) { 152*0d0321e0SJeremy L Thompson CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, grid_size, 1, 1, 153*0d0321e0SJeremy L Thompson block_size_x, block_size_y, block_size_z, 154*0d0321e0SJeremy L Thompson shared_mem_size, NULL, args, NULL)); 155e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15630f4f45fSnbeams } 157