xref: /libCEED/backends/hip-gen/ceed-hip-gen-qfunction.c (revision 8ec64e9ae9d5df169dba8c8ee61d8ec8907b8f80)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed/backend.h>
9 #include <ceed/ceed.h>
10 #include <hip/hip_runtime.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include "ceed-hip-gen.h"
15 
16 //------------------------------------------------------------------------------
17 // Apply QFunction
18 //------------------------------------------------------------------------------
19 static int CeedQFunctionApply_Hip_gen(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
20   Ceed ceed;
21   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
22   return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement QFunctionApply");
23 }
24 
25 //------------------------------------------------------------------------------
26 // Destroy QFunction
27 //------------------------------------------------------------------------------
28 static int CeedQFunctionDestroy_Hip_gen(CeedQFunction qf) {
29   CeedQFunction_Hip_gen *data;
30   CeedCallBackend(CeedQFunctionGetData(qf, &data));
31   Ceed ceed;
32   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
33   CeedCallHip(ceed, hipFree(data->d_c));
34   CeedCallBackend(CeedFree(&data->q_function_source));
35   CeedCallBackend(CeedFree(&data));
36   return CEED_ERROR_SUCCESS;
37 }
38 
39 //------------------------------------------------------------------------------
40 // Create QFunction
41 //------------------------------------------------------------------------------
42 int CeedQFunctionCreate_Hip_gen(CeedQFunction qf) {
43   Ceed ceed;
44   CeedQFunctionGetCeed(qf, &ceed);
45   CeedQFunction_Hip_gen *data;
46   CeedCallBackend(CeedCalloc(1, &data));
47   CeedCallBackend(CeedQFunctionSetData(qf, data));
48 
49   // Read QFunction source
50   CeedCallBackend(CeedQFunctionGetKernelName(qf, &data->q_function_name));
51   CeedDebug256(ceed, 2, "----- Loading QFunction User Source -----\n");
52   CeedCallBackend(CeedQFunctionLoadSourceToBuffer(qf, &data->q_function_source));
53   CeedDebug256(ceed, 2, "----- Loading QFunction User Source Complete! -----\n");
54   if (!data->q_function_source) {
55     // LCOV_EXCL_START
56     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "/gpu/hip/gen backend requires QFunction source code file");
57     // LCOV_EXCL_STOP
58   }
59 
60   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Hip_gen));
61   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Hip_gen));
62   return CEED_ERROR_SUCCESS;
63 }
64 //------------------------------------------------------------------------------
65