xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-qfunction.c (revision b7453713e95c1c6eb59ce174cbcb87227e92884e)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
1049aac155SJeremy L Thompson #include <ceed/jit-source/hip/hip-types.h>
110d0321e0SJeremy L Thompson #include <hip/hip_runtime.h>
122b730f8bSJeremy L Thompson 
1349aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
140d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
152b730f8bSJeremy L Thompson #include "ceed-hip-ref-qfunction-load.h"
162b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
170d0321e0SJeremy L Thompson 
180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
190d0321e0SJeremy L Thompson // Apply QFunction
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
212b730f8bSJeremy L Thompson static int CeedQFunctionApply_Hip(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
220d0321e0SJeremy L Thompson   Ceed               ceed;
23*b7453713SJeremy L Thompson   Ceed_Hip          *ceed_Hip;
24*b7453713SJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
25*b7453713SJeremy L Thompson   CeedQFunction_Hip *data;
26*b7453713SJeremy L Thompson 
272b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
280d0321e0SJeremy L Thompson 
290d0321e0SJeremy L Thompson   // Build and compile kernel, if not done
30eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedQFunctionBuildKernel_Hip_ref(qf));
310d0321e0SJeremy L Thompson 
322b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &data));
332b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &ceed_Hip));
342b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
35b2165e7aSSebastian Grimberg   const int block_size = ceed_Hip->opt_block_size;
360d0321e0SJeremy L Thompson 
370d0321e0SJeremy L Thompson   // Read vectors
38437930d1SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
392b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_DEVICE, &data->fields.inputs[i]));
400d0321e0SJeremy L Thompson   }
41437930d1SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
422b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_DEVICE, &data->fields.outputs[i]));
430d0321e0SJeremy L Thompson   }
440d0321e0SJeremy L Thompson 
450d0321e0SJeremy L Thompson   // Get context data
462b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &data->d_c));
470d0321e0SJeremy L Thompson 
480d0321e0SJeremy L Thompson   // Run kernel
490d0321e0SJeremy L Thompson   void *args[] = {&data->d_c, (void *)&Q, &data->fields};
50*b7453713SJeremy L Thompson 
51b2165e7aSSebastian Grimberg   CeedCallBackend(CeedRunKernel_Hip(ceed, data->QFunction, CeedDivUpInt(Q, block_size), block_size, args));
520d0321e0SJeremy L Thompson 
530d0321e0SJeremy L Thompson   // Restore vectors
54437930d1SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
552b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &data->fields.inputs[i]));
560d0321e0SJeremy L Thompson   }
57437930d1SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
582b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(V[i], &data->fields.outputs[i]));
590d0321e0SJeremy L Thompson   }
600d0321e0SJeremy L Thompson 
610d0321e0SJeremy L Thompson   // Restore context
622b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &data->d_c));
630d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
640d0321e0SJeremy L Thompson }
650d0321e0SJeremy L Thompson 
660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
670d0321e0SJeremy L Thompson // Destroy QFunction
680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
690d0321e0SJeremy L Thompson static int CeedQFunctionDestroy_Hip(CeedQFunction qf) {
700d0321e0SJeremy L Thompson   Ceed               ceed;
71*b7453713SJeremy L Thompson   CeedQFunction_Hip *data;
72*b7453713SJeremy L Thompson 
73*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &data));
742b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
752b730f8bSJeremy L Thompson   if (data->module) CeedCallHip(ceed, hipModuleUnload(data->module));
762b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
770d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
780d0321e0SJeremy L Thompson }
790d0321e0SJeremy L Thompson 
800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
810d0321e0SJeremy L Thompson // Create QFunction
820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
830d0321e0SJeremy L Thompson int CeedQFunctionCreate_Hip(CeedQFunction qf) {
840d0321e0SJeremy L Thompson   Ceed               ceed;
85*b7453713SJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
860d0321e0SJeremy L Thompson   CeedQFunction_Hip *data;
87*b7453713SJeremy L Thompson 
88*b7453713SJeremy L Thompson   CeedQFunctionGetCeed(qf, &ceed);
892b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
902b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionSetData(qf, data));
912b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
920d0321e0SJeremy L Thompson 
930d0321e0SJeremy L Thompson   // Read QFunction source
942b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetKernelName(qf, &data->qfunction_name));
9523d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading QFunction User Source -----\n");
962b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionLoadSourceToBuffer(qf, &data->qfunction_source));
9723d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading QFunction User Source Complete! -----\n");
980d0321e0SJeremy L Thompson 
990d0321e0SJeremy L Thompson   // Register backend functions
1002b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Hip));
1012b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Hip));
1020d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1030d0321e0SJeremy L Thompson }
1042a86cc9dSSebastian Grimberg 
1050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
106