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 1730f4f45fSnbeams #include <ceed-backend.h> 1830f4f45fSnbeams #include <string.h> 1930f4f45fSnbeams #include <sstream> 2030f4f45fSnbeams #include <stdarg.h> 2130f4f45fSnbeams #include <hip/hiprtc.h> 2230f4f45fSnbeams #include "ceed-hip.h" 2330f4f45fSnbeams #include "ceed-hip-compile.h" 2430f4f45fSnbeams 2530f4f45fSnbeams #define CeedChk_hiprtc(ceed, x) \ 2630f4f45fSnbeams do { \ 2730f4f45fSnbeams hiprtcResult result = static_cast<hiprtcResult>(x); \ 2830f4f45fSnbeams if (result != HIPRTC_SUCCESS) \ 2930f4f45fSnbeams return CeedError((ceed), x, hiprtcGetErrorString(result)); \ 3030f4f45fSnbeams } while (0) 3130f4f45fSnbeams 3230f4f45fSnbeams //------------------------------------------------------------------------------ 3330f4f45fSnbeams // Compile HIP kernel 3430f4f45fSnbeams //------------------------------------------------------------------------------ 3530f4f45fSnbeams int CeedCompileHip(Ceed ceed, const char *source, hipModule_t *module, 3630f4f45fSnbeams const CeedInt numopts, ...) { 3730f4f45fSnbeams int ierr; 3830f4f45fSnbeams hipFree(0); // Make sure a Context exists for hiprtc 3930f4f45fSnbeams hiprtcProgram prog; 4030f4f45fSnbeams 4130f4f45fSnbeams // Add hip runtime include to string for generation 4230f4f45fSnbeams std::ostringstream code; 4330f4f45fSnbeams code << "\n#include <hip/hip_runtime.h>\n"; 4430f4f45fSnbeams 4530f4f45fSnbeams // Macro definitions 4630f4f45fSnbeams // Get kernel specific options, such as kernel constants 4730f4f45fSnbeams const int optslen = 32; 48*81a63d6fSnbeams const int optssize = 2; 49*81a63d6fSnbeams const char *opts[optssize]; 5030f4f45fSnbeams if (numopts > 0) { 5130f4f45fSnbeams va_list args; 5230f4f45fSnbeams va_start(args, numopts); 5330f4f45fSnbeams char *name; 5430f4f45fSnbeams int val; 5530f4f45fSnbeams for (int i = 0; i < numopts; 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 6430f4f45fSnbeams code << "#define CeedScalar double\n#define CeedInt int\n\n"; 6530f4f45fSnbeams 6630f4f45fSnbeams // Non-macro options 6730f4f45fSnbeams opts[0] = "-default-device"; 6830f4f45fSnbeams struct hipDeviceProp_t prop; 6930f4f45fSnbeams Ceed_Hip *ceed_data; 7030f4f45fSnbeams ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChk(ierr); 7130f4f45fSnbeams CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->deviceId)); 7230f4f45fSnbeams char buff[optslen]; 7330f4f45fSnbeams std::string gfxName = "gfx" + std::to_string(prop.gcnArch); 7430f4f45fSnbeams std::string archArg = "--gpu-architecture=" + gfxName; 7530f4f45fSnbeams snprintf(buff, optslen, archArg.c_str()); 7630f4f45fSnbeams opts[1] = buff; 7730f4f45fSnbeams 7830f4f45fSnbeams // Add string source argument provided in call 7930f4f45fSnbeams code << source; 8030f4f45fSnbeams 8130f4f45fSnbeams // Create Program 8230f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 8330f4f45fSnbeams 8430f4f45fSnbeams // Compile kernel 85*81a63d6fSnbeams hiprtcResult result = hiprtcCompileProgram(prog, optssize, opts); 8630f4f45fSnbeams if (result != HIPRTC_SUCCESS) { 8730f4f45fSnbeams size_t logsize; 8830f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &logsize)); 8930f4f45fSnbeams char *log; 9030f4f45fSnbeams ierr = CeedMalloc(logsize, &log); CeedChk(ierr); 9130f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log)); 9230f4f45fSnbeams return CeedError(ceed, (int)result, "%s\n%s", hiprtcGetErrorString(result), log); 9330f4f45fSnbeams } 9430f4f45fSnbeams 9530f4f45fSnbeams size_t ptxsize; 9630f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptxsize)); 9730f4f45fSnbeams char *ptx; 9830f4f45fSnbeams ierr = CeedMalloc(ptxsize, &ptx); CeedChk(ierr); 9930f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx)); 10030f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog)); 10130f4f45fSnbeams 10230f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLoadData(module, ptx)); 10330f4f45fSnbeams ierr = CeedFree(&ptx); CeedChk(ierr); 10430f4f45fSnbeams 10530f4f45fSnbeams return 0; 10630f4f45fSnbeams } 10730f4f45fSnbeams 10830f4f45fSnbeams //------------------------------------------------------------------------------ 10930f4f45fSnbeams // Get HIP kernel 11030f4f45fSnbeams //------------------------------------------------------------------------------ 11130f4f45fSnbeams int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name, 11230f4f45fSnbeams hipFunction_t *kernel) { 11330f4f45fSnbeams 11430f4f45fSnbeams CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name)); 11530f4f45fSnbeams return 0; 11630f4f45fSnbeams } 11730f4f45fSnbeams 11830f4f45fSnbeams //------------------------------------------------------------------------------ 11930f4f45fSnbeams // Run HIP kernel 12030f4f45fSnbeams //------------------------------------------------------------------------------ 12130f4f45fSnbeams int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 12230f4f45fSnbeams const int blockSize, void **args) { 12330f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, blockSize, 1, 12430f4f45fSnbeams 1, 0, NULL, args, NULL)); 12530f4f45fSnbeams return 0; 12630f4f45fSnbeams } 12730f4f45fSnbeams 12830f4f45fSnbeams //------------------------------------------------------------------------------ 12930f4f45fSnbeams // Run HIP kernel for spatial dimension 13030f4f45fSnbeams //------------------------------------------------------------------------------ 13130f4f45fSnbeams int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 13230f4f45fSnbeams const int blockSizeX, const int blockSizeY, 13330f4f45fSnbeams const int blockSizeZ, void **args) { 13430f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, 13530f4f45fSnbeams blockSizeX, blockSizeY, blockSizeZ, 13630f4f45fSnbeams 0, NULL, args, NULL)); 13730f4f45fSnbeams return 0; 13830f4f45fSnbeams } 13930f4f45fSnbeams 14030f4f45fSnbeams //------------------------------------------------------------------------------ 14130f4f45fSnbeams // Run HIP kernel for spatial dimension with sharde memory 14230f4f45fSnbeams //------------------------------------------------------------------------------ 14330f4f45fSnbeams int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 14430f4f45fSnbeams const int blockSizeX, const int blockSizeY, 14530f4f45fSnbeams const int blockSizeZ, const int sharedMemSize, 14630f4f45fSnbeams void **args) { 14730f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, 14830f4f45fSnbeams blockSizeX, blockSizeY, blockSizeZ, 14930f4f45fSnbeams sharedMemSize, NULL, args, NULL)); 15030f4f45fSnbeams return 0; 15130f4f45fSnbeams } 152