xref: /libCEED/backends/ref/ceed-ref-qfunctioncontext.c (revision bd4df46207a63b48a485389a63848eb7a2477f6b)
1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3 // All Rights reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include <ceed.h>
18 #include <ceed-backend.h>
19 #include <string.h>
20 #include "ceed-ref.h"
21 
22 //------------------------------------------------------------------------------
23 // QFunctionContext Set Data
24 //------------------------------------------------------------------------------
25 static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx,
26     CeedMemType mtype,
27     CeedCopyMode cmode, CeedScalar *data) {
28   int ierr;
29   CeedQFunctionContext_Ref *impl;
30   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl); CeedChk(ierr);
31   size_t ctxsize;
32   ierr = CeedQFunctionContextGetContextSize(ctx, &ctxsize); CeedChk(ierr);
33   Ceed ceed;
34   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
35 
36   if (mtype != CEED_MEM_HOST)
37     // LCOV_EXCL_START
38     return CeedError(ceed, 1, "Only MemType = HOST supported");
39   // LCOV_EXCL_STOP
40   ierr = CeedFree(&impl->data_allocated); CeedChk(ierr);
41   switch (cmode) {
42   case CEED_COPY_VALUES:
43     ierr = CeedMallocArray(1, ctxsize, &impl->data_allocated); CeedChk(ierr);
44     impl->data = impl->data_allocated;
45     memcpy(impl->data, data, ctxsize);
46     break;
47   case CEED_OWN_POINTER:
48     impl->data_allocated = data;
49     impl->data = data;
50     break;
51   case CEED_USE_POINTER:
52     impl->data = data;
53   }
54   return 0;
55 }
56 
57 //------------------------------------------------------------------------------
58 // QFunctionContext Get Data
59 //------------------------------------------------------------------------------
60 static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx,
61     CeedMemType mtype, CeedScalar *data) {
62   int ierr;
63   CeedQFunctionContext_Ref *impl;
64   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl); CeedChk(ierr);
65   Ceed ceed;
66   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
67 
68   if (mtype != CEED_MEM_HOST)
69     // LCOV_EXCL_START
70     return CeedError(ceed, 1, "Can only provide to HOST memory");
71   // LCOV_EXCL_STOP
72   if (!impl->data)
73     // LCOV_EXCL_START
74     return CeedError(ceed, 1, "No context data set");
75   // LCOV_EXCL_STOP
76   *(void **)data = impl->data;
77   return 0;
78 }
79 
80 //------------------------------------------------------------------------------
81 // QFunctionContext Restore Data
82 //------------------------------------------------------------------------------
83 static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
84   return 0;
85 }
86 
87 //------------------------------------------------------------------------------
88 // QFunctionContext Destroy
89 //------------------------------------------------------------------------------
90 static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
91   int ierr;
92   CeedQFunctionContext_Ref *impl;
93   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChk(ierr);
94 
95   ierr = CeedFree(&impl->data_allocated); CeedChk(ierr);
96   ierr = CeedFree(&impl); CeedChk(ierr);
97   return 0;
98 }
99 
100 //------------------------------------------------------------------------------
101 // QFunctionContext Create
102 //------------------------------------------------------------------------------
103 int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
104   int ierr;
105   CeedQFunctionContext_Ref *impl;
106   Ceed ceed;
107   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
108 
109   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
110                                 CeedQFunctionContextSetData_Ref); CeedChk(ierr);
111   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
112                                 CeedQFunctionContextGetData_Ref); CeedChk(ierr);
113   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
114                                 CeedQFunctionContextRestoreData_Ref); CeedChk(ierr);
115   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
116                                 CeedQFunctionContextDestroy_Ref); CeedChk(ierr);
117   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
118   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChk(ierr);
119   return 0;
120 }
121 //------------------------------------------------------------------------------
122