xref: /libCEED/backends/hip-ref/ceed-hip-ref-qfunction.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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 
80d0321e0SJeremy L Thompson #include <ceed/backend.h>
9*2b730f8bSJeremy L Thompson #include <ceed/ceed.h>
100d0321e0SJeremy L Thompson #include <hip/hip_runtime.h>
110d0321e0SJeremy L Thompson #include <stdio.h>
120d0321e0SJeremy L Thompson #include <string.h>
13*2b730f8bSJeremy L Thompson 
140d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
15*2b730f8bSJeremy L Thompson #include "ceed-hip-ref-qfunction-load.h"
16*2b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
170d0321e0SJeremy L Thompson 
180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
190d0321e0SJeremy L Thompson // Apply QFunction
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
21*2b730f8bSJeremy L Thompson static int CeedQFunctionApply_Hip(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
220d0321e0SJeremy L Thompson   Ceed ceed;
23*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
240d0321e0SJeremy L Thompson 
250d0321e0SJeremy L Thompson   // Build and compile kernel, if not done
26*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedHipBuildQFunction(qf));
270d0321e0SJeremy L Thompson 
280d0321e0SJeremy L Thompson   CeedQFunction_Hip *data;
29*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &data));
300d0321e0SJeremy L Thompson   Ceed_Hip *ceed_Hip;
31*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &ceed_Hip));
32437930d1SJeremy L Thompson   CeedInt num_input_fields, num_output_fields;
33*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
340d0321e0SJeremy L Thompson   const int blocksize = ceed_Hip->opt_block_size;
350d0321e0SJeremy L Thompson 
360d0321e0SJeremy L Thompson   // Read vectors
37437930d1SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
38*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_DEVICE, &data->fields.inputs[i]));
390d0321e0SJeremy L Thompson   }
40437930d1SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
41*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_DEVICE, &data->fields.outputs[i]));
420d0321e0SJeremy L Thompson   }
430d0321e0SJeremy L Thompson 
440d0321e0SJeremy L Thompson   // Get context data
45*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &data->d_c));
460d0321e0SJeremy L Thompson 
470d0321e0SJeremy L Thompson   // Run kernel
480d0321e0SJeremy L Thompson   void *args[] = {&data->d_c, (void *)&Q, &data->fields};
49*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedRunKernelHip(ceed, data->QFunction, CeedDivUpInt(Q, blocksize), blocksize, args));
500d0321e0SJeremy L Thompson 
510d0321e0SJeremy L Thompson   // Restore vectors
52437930d1SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
53*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &data->fields.inputs[i]));
540d0321e0SJeremy L Thompson   }
55437930d1SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
56*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(V[i], &data->fields.outputs[i]));
570d0321e0SJeremy L Thompson   }
580d0321e0SJeremy L Thompson 
590d0321e0SJeremy L Thompson   // Restore context
60*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &data->d_c));
61441428dfSJeremy L Thompson 
620d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
630d0321e0SJeremy L Thompson }
640d0321e0SJeremy L Thompson 
650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
660d0321e0SJeremy L Thompson // Destroy QFunction
670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
680d0321e0SJeremy L Thompson static int CeedQFunctionDestroy_Hip(CeedQFunction qf) {
690d0321e0SJeremy L Thompson   CeedQFunction_Hip *data;
70*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &data));
710d0321e0SJeremy L Thompson   Ceed ceed;
72*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
73*2b730f8bSJeremy L Thompson   if (data->module) CeedCallHip(ceed, hipModuleUnload(data->module));
74*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
75437930d1SJeremy L Thompson 
760d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
770d0321e0SJeremy L Thompson }
780d0321e0SJeremy L Thompson 
790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
800d0321e0SJeremy L Thompson // Create QFunction
810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
820d0321e0SJeremy L Thompson int CeedQFunctionCreate_Hip(CeedQFunction qf) {
830d0321e0SJeremy L Thompson   Ceed ceed;
840d0321e0SJeremy L Thompson   CeedQFunctionGetCeed(qf, &ceed);
850d0321e0SJeremy L Thompson   CeedQFunction_Hip *data;
86*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
87*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionSetData(qf, data));
88437930d1SJeremy L Thompson   CeedInt num_input_fields, num_output_fields;
89*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
900d0321e0SJeremy L Thompson 
910d0321e0SJeremy L Thompson   // Read QFunction source
92*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetKernelName(qf, &data->qfunction_name));
9346dc0734SJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading QFunction User Source -----\n");
94*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionLoadSourceToBuffer(qf, &data->qfunction_source));
9546dc0734SJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading QFunction User Source Complete! -----\n");
960d0321e0SJeremy L Thompson 
970d0321e0SJeremy L Thompson   // Register backend functions
98*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Hip));
99*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Hip));
1000d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1010d0321e0SJeremy L Thompson }
1020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
103