10d0321e0SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC. 20d0321e0SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707. 30d0321e0SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details. 40d0321e0SJeremy L Thompson // 50d0321e0SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software 60d0321e0SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral 70d0321e0SJeremy L Thompson // element discretizations for exascale applications. For more information and 80d0321e0SJeremy L Thompson // source code availability see http://github.com/ceed. 90d0321e0SJeremy L Thompson // 100d0321e0SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 110d0321e0SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office 120d0321e0SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for 130d0321e0SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including 140d0321e0SJeremy L Thompson // software, applications, hardware, advanced system engineering and early 150d0321e0SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative. 160d0321e0SJeremy L Thompson 170d0321e0SJeremy L Thompson #include <ceed/ceed.h> 180d0321e0SJeremy L Thompson #include <ceed/backend.h> 190d0321e0SJeremy L Thompson #include <hip/hip_runtime.h> 200d0321e0SJeremy L Thompson #include <stdio.h> 210d0321e0SJeremy L Thompson #include <string.h> 220d0321e0SJeremy L Thompson #include "ceed-hip-ref.h" 230d0321e0SJeremy L Thompson #include "ceed-hip-ref-qfunction-load.h" 240d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h" 250d0321e0SJeremy L Thompson 260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 270d0321e0SJeremy L Thompson // Apply QFunction 280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 290d0321e0SJeremy L Thompson static int CeedQFunctionApply_Hip(CeedQFunction qf, CeedInt Q, 300d0321e0SJeremy L Thompson CeedVector *U, CeedVector *V) { 310d0321e0SJeremy L Thompson int ierr; 320d0321e0SJeremy L Thompson Ceed ceed; 330d0321e0SJeremy L Thompson ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr); 340d0321e0SJeremy L Thompson 350d0321e0SJeremy L Thompson // Build and compile kernel, if not done 360d0321e0SJeremy L Thompson ierr = CeedHipBuildQFunction(qf); CeedChkBackend(ierr); 370d0321e0SJeremy L Thompson 380d0321e0SJeremy L Thompson CeedQFunction_Hip *data; 390d0321e0SJeremy L Thompson ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr); 400d0321e0SJeremy L Thompson Ceed_Hip *ceed_Hip; 410d0321e0SJeremy L Thompson ierr = CeedGetData(ceed, &ceed_Hip); CeedChkBackend(ierr); 42*437930d1SJeremy L Thompson CeedInt num_input_fields, num_output_fields; 43*437930d1SJeremy L Thompson ierr = CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields); 440d0321e0SJeremy L Thompson CeedChkBackend(ierr); 450d0321e0SJeremy L Thompson const int blocksize = ceed_Hip->opt_block_size; 460d0321e0SJeremy L Thompson 470d0321e0SJeremy L Thompson // Read vectors 48*437930d1SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 490d0321e0SJeremy L Thompson ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_DEVICE, &data->fields.inputs[i]); 500d0321e0SJeremy L Thompson CeedChkBackend(ierr); 510d0321e0SJeremy L Thompson } 52*437930d1SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 530d0321e0SJeremy L Thompson ierr = CeedVectorGetArrayWrite(V[i], CEED_MEM_DEVICE, &data->fields.outputs[i]); 540d0321e0SJeremy L Thompson CeedChkBackend(ierr); 550d0321e0SJeremy L Thompson } 560d0321e0SJeremy L Thompson 570d0321e0SJeremy L Thompson // Get context data 580d0321e0SJeremy L Thompson CeedQFunctionContext ctx; 590d0321e0SJeremy L Thompson ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChkBackend(ierr); 600d0321e0SJeremy L Thompson if (ctx) { 610d0321e0SJeremy L Thompson ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_DEVICE, &data->d_c); 620d0321e0SJeremy L Thompson CeedChkBackend(ierr); 630d0321e0SJeremy L Thompson } 640d0321e0SJeremy L Thompson 650d0321e0SJeremy L Thompson // Run kernel 660d0321e0SJeremy L Thompson void *args[] = {&data->d_c, (void *) &Q, &data->fields}; 67*437930d1SJeremy L Thompson ierr = CeedRunKernelHip(ceed, data->QFunction, CeedDivUpInt(Q, blocksize), 680d0321e0SJeremy L Thompson blocksize, args); CeedChkBackend(ierr); 690d0321e0SJeremy L Thompson 700d0321e0SJeremy L Thompson // Restore vectors 71*437930d1SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 720d0321e0SJeremy L Thompson ierr = CeedVectorRestoreArrayRead(U[i], &data->fields.inputs[i]); 730d0321e0SJeremy L Thompson CeedChkBackend(ierr); 740d0321e0SJeremy L Thompson } 75*437930d1SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 760d0321e0SJeremy L Thompson ierr = CeedVectorRestoreArray(V[i], &data->fields.outputs[i]); 770d0321e0SJeremy L Thompson CeedChkBackend(ierr); 780d0321e0SJeremy L Thompson } 790d0321e0SJeremy L Thompson 800d0321e0SJeremy L Thompson // Restore context 810d0321e0SJeremy L Thompson if (ctx) { 820d0321e0SJeremy L Thompson ierr = CeedQFunctionContextRestoreData(ctx, &data->d_c); 830d0321e0SJeremy L Thompson CeedChkBackend(ierr); 840d0321e0SJeremy L Thompson } 850d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 860d0321e0SJeremy L Thompson } 870d0321e0SJeremy L Thompson 880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 890d0321e0SJeremy L Thompson // Destroy QFunction 900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 910d0321e0SJeremy L Thompson static int CeedQFunctionDestroy_Hip(CeedQFunction qf) { 920d0321e0SJeremy L Thompson int ierr; 930d0321e0SJeremy L Thompson CeedQFunction_Hip *data; 940d0321e0SJeremy L Thompson ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr); 950d0321e0SJeremy L Thompson Ceed ceed; 960d0321e0SJeremy L Thompson ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr); 970d0321e0SJeremy L Thompson if (data->module) 980d0321e0SJeremy L Thompson CeedChk_Hip(ceed, hipModuleUnload(data->module)); 990d0321e0SJeremy L Thompson ierr = CeedFree(&data); CeedChkBackend(ierr); 100*437930d1SJeremy L Thompson 1010d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1020d0321e0SJeremy L Thompson } 1030d0321e0SJeremy L Thompson 1040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1050d0321e0SJeremy L Thompson // Create QFunction 1060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1070d0321e0SJeremy L Thompson int CeedQFunctionCreate_Hip(CeedQFunction qf) { 1080d0321e0SJeremy L Thompson int ierr; 1090d0321e0SJeremy L Thompson Ceed ceed; 1100d0321e0SJeremy L Thompson CeedQFunctionGetCeed(qf, &ceed); 1110d0321e0SJeremy L Thompson CeedQFunction_Hip *data; 1120d0321e0SJeremy L Thompson ierr = CeedCalloc(1,&data); CeedChkBackend(ierr); 1130d0321e0SJeremy L Thompson ierr = CeedQFunctionSetData(qf, data); CeedChkBackend(ierr); 114*437930d1SJeremy L Thompson CeedInt num_input_fields, num_output_fields; 115*437930d1SJeremy L Thompson ierr = CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields); 1160d0321e0SJeremy L Thompson CeedChkBackend(ierr); 1170d0321e0SJeremy L Thompson 1180d0321e0SJeremy L Thompson // Read QFunction source 119*437930d1SJeremy L Thompson ierr = CeedQFunctionGetKernelName(qf, &data->qfunction_name); 1200d0321e0SJeremy L Thompson CeedChkBackend(ierr); 121*437930d1SJeremy L Thompson ierr = CeedQFunctionLoadSourceToBuffer(qf, &data->qfunction_source); 1220d0321e0SJeremy L Thompson CeedChkBackend(ierr); 1230d0321e0SJeremy L Thompson 1240d0321e0SJeremy L Thompson // Register backend functions 1250d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", 1260d0321e0SJeremy L Thompson CeedQFunctionApply_Hip); CeedChkBackend(ierr); 1270d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", 1280d0321e0SJeremy L Thompson CeedQFunctionDestroy_Hip); CeedChkBackend(ierr); 1290d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1300d0321e0SJeremy L Thompson } 1310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 132