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 17*ec3da8bcSJed Brown #include <ceed/ceed.h> 18*ec3da8bcSJed Brown #include <ceed/backend.h> 1930f4f45fSnbeams #include <sstream> 2030f4f45fSnbeams #include <stdarg.h> 213d576824SJeremy L Thompson #include <string.h> 2230f4f45fSnbeams #include <hip/hiprtc.h> 2330f4f45fSnbeams #include "ceed-hip.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, 3730f4f45fSnbeams const CeedInt numopts, ...) { 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 4830f4f45fSnbeams const int optslen = 32; 4981a63d6fSnbeams const int optssize = 2; 5081a63d6fSnbeams const char *opts[optssize]; 5130f4f45fSnbeams if (numopts > 0) { 5230f4f45fSnbeams va_list args; 5330f4f45fSnbeams va_start(args, numopts); 5430f4f45fSnbeams char *name; 5530f4f45fSnbeams int val; 5630f4f45fSnbeams for (int i = 0; i < numopts; i++) { 5730f4f45fSnbeams name = va_arg(args, char *); 5830f4f45fSnbeams val = va_arg(args, int); 5930f4f45fSnbeams code << "#define " << name << " " << val << "\n"; 6030f4f45fSnbeams } 6130f4f45fSnbeams va_end(args); 6230f4f45fSnbeams } 6330f4f45fSnbeams 6430f4f45fSnbeams // Standard backend options 65e15f9bd0SJeremy L Thompson code << "#define CeedScalar double\n"; 66e15f9bd0SJeremy L Thompson code << "#define CeedInt int\n"; 67e15f9bd0SJeremy L Thompson code << "#define CEED_ERROR_SUCCESS 0\n\n"; 6830f4f45fSnbeams 6930f4f45fSnbeams // Non-macro options 7030f4f45fSnbeams opts[0] = "-default-device"; 7130f4f45fSnbeams struct hipDeviceProp_t prop; 7230f4f45fSnbeams Ceed_Hip *ceed_data; 73e15f9bd0SJeremy L Thompson ierr = CeedGetData(ceed, (void **)&ceed_data); CeedChkBackend(ierr); 7430f4f45fSnbeams CeedChk_Hip(ceed, hipGetDeviceProperties(&prop, ceed_data->deviceId)); 7530f4f45fSnbeams char buff[optslen]; 7630f4f45fSnbeams std::string gfxName = "gfx" + std::to_string(prop.gcnArch); 7730f4f45fSnbeams std::string archArg = "--gpu-architecture=" + gfxName; 7829b67289Sjeremylt snprintf(buff, optslen, "%s", archArg.c_str()); 7930f4f45fSnbeams opts[1] = buff; 8030f4f45fSnbeams 8130f4f45fSnbeams // Add string source argument provided in call 8230f4f45fSnbeams code << source; 8330f4f45fSnbeams 8430f4f45fSnbeams // Create Program 8530f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL)); 8630f4f45fSnbeams 8730f4f45fSnbeams // Compile kernel 8881a63d6fSnbeams hiprtcResult result = hiprtcCompileProgram(prog, optssize, opts); 8930f4f45fSnbeams if (result != HIPRTC_SUCCESS) { 9030f4f45fSnbeams size_t logsize; 9130f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &logsize)); 9230f4f45fSnbeams char *log; 93e15f9bd0SJeremy L Thompson ierr = CeedMalloc(logsize, &log); CeedChkBackend(ierr); 9430f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetProgramLog(prog, log)); 95e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", hiprtcGetErrorString(result), log); 9630f4f45fSnbeams } 9730f4f45fSnbeams 9830f4f45fSnbeams size_t ptxsize; 9930f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetCodeSize(prog, &ptxsize)); 10030f4f45fSnbeams char *ptx; 101e15f9bd0SJeremy L Thompson ierr = CeedMalloc(ptxsize, &ptx); CeedChkBackend(ierr); 10230f4f45fSnbeams CeedChk_hiprtc(ceed, hiprtcGetCode(prog, ptx)); 10317fed040Snbeams CeedChk_hiprtc(ceed, hiprtcDestroyProgram(&prog)); 10430f4f45fSnbeams 10530f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLoadData(module, ptx)); 106e15f9bd0SJeremy L Thompson ierr = CeedFree(&ptx); CeedChkBackend(ierr); 10730f4f45fSnbeams 108e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10930f4f45fSnbeams } 11030f4f45fSnbeams 11130f4f45fSnbeams //------------------------------------------------------------------------------ 11230f4f45fSnbeams // Get HIP kernel 11330f4f45fSnbeams //------------------------------------------------------------------------------ 11430f4f45fSnbeams int CeedGetKernelHip(Ceed ceed, hipModule_t module, const char *name, 11530f4f45fSnbeams hipFunction_t *kernel) { 11630f4f45fSnbeams 11730f4f45fSnbeams CeedChk_Hip(ceed, hipModuleGetFunction(kernel, module, name)); 118e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11930f4f45fSnbeams } 12030f4f45fSnbeams 12130f4f45fSnbeams //------------------------------------------------------------------------------ 12230f4f45fSnbeams // Run HIP kernel 12330f4f45fSnbeams //------------------------------------------------------------------------------ 12430f4f45fSnbeams int CeedRunKernelHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 12530f4f45fSnbeams const int blockSize, void **args) { 12630f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, blockSize, 1, 12730f4f45fSnbeams 1, 0, NULL, args, NULL)); 128e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12930f4f45fSnbeams } 13030f4f45fSnbeams 13130f4f45fSnbeams //------------------------------------------------------------------------------ 13230f4f45fSnbeams // Run HIP kernel for spatial dimension 13330f4f45fSnbeams //------------------------------------------------------------------------------ 13430f4f45fSnbeams int CeedRunKernelDimHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 13530f4f45fSnbeams const int blockSizeX, const int blockSizeY, 13630f4f45fSnbeams const int blockSizeZ, void **args) { 13730f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, 13830f4f45fSnbeams blockSizeX, blockSizeY, blockSizeZ, 13930f4f45fSnbeams 0, NULL, args, NULL)); 140e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14130f4f45fSnbeams } 14230f4f45fSnbeams 14330f4f45fSnbeams //------------------------------------------------------------------------------ 144e15f9bd0SJeremy L Thompson // Run HIP kernel for spatial dimension with shared memory 14530f4f45fSnbeams //------------------------------------------------------------------------------ 14630f4f45fSnbeams int CeedRunKernelDimSharedHip(Ceed ceed, hipFunction_t kernel, const int gridSize, 14730f4f45fSnbeams const int blockSizeX, const int blockSizeY, 14830f4f45fSnbeams const int blockSizeZ, const int sharedMemSize, 14930f4f45fSnbeams void **args) { 15030f4f45fSnbeams CeedChk_Hip(ceed, hipModuleLaunchKernel(kernel, gridSize, 1, 1, 15130f4f45fSnbeams blockSizeX, blockSizeY, blockSizeZ, 15230f4f45fSnbeams sharedMemSize, NULL, args, NULL)); 153e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15430f4f45fSnbeams } 155