xref: /libCEED/backends/ref/ceed-ref-qfunctioncontext.c (revision 1d0137909cd290d677dbca28a6953e6505c9d054)
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-ref.h"
18 
19 //------------------------------------------------------------------------------
20 // QFunctionContext Set Data
21 //------------------------------------------------------------------------------
22 static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx,
23     CeedMemType mtype,
24     CeedCopyMode cmode, CeedScalar *data) {
25   int ierr;
26   CeedQFunctionContext_Ref *impl;
27   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl); CeedChk(ierr);
28   size_t ctxsize;
29   ierr = CeedQFunctionContextGetContextSize(ctx, &ctxsize); CeedChk(ierr);
30   Ceed ceed;
31   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
32 
33   if (mtype != CEED_MEM_HOST)
34     // LCOV_EXCL_START
35     return CeedError(ceed, 1, "Only MemType = HOST supported");
36   // LCOV_EXCL_STOP
37   ierr = CeedFree(&impl->data_allocated); CeedChk(ierr);
38   switch (cmode) {
39   case CEED_COPY_VALUES:
40     ierr = CeedMallocArray(1, ctxsize, &impl->data_allocated); CeedChk(ierr);
41     impl->data = impl->data_allocated;
42     memcpy(impl->data, data, ctxsize);
43     break;
44   case CEED_OWN_POINTER:
45     impl->data_allocated = data;
46     impl->data = data;
47     break;
48   case CEED_USE_POINTER:
49     impl->data = data;
50   }
51   return 0;
52 }
53 
54 //------------------------------------------------------------------------------
55 // QFunctionContext Get Data
56 //------------------------------------------------------------------------------
57 static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx,
58     CeedMemType mtype, CeedScalar *data) {
59   int ierr;
60   CeedQFunctionContext_Ref *impl;
61   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl); CeedChk(ierr);
62   Ceed ceed;
63   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
64 
65   if (mtype != CEED_MEM_HOST)
66     // LCOV_EXCL_START
67     return CeedError(ceed, 1, "Can only provide to HOST memory");
68   // LCOV_EXCL_STOP
69   if (!impl->data)
70     // LCOV_EXCL_START
71     return CeedError(ceed, 1, "No context data set");
72   // LCOV_EXCL_STOP
73   *(void **)data = impl->data;
74   return 0;
75 }
76 
77 //------------------------------------------------------------------------------
78 // QFunctionContext Restore Data
79 //------------------------------------------------------------------------------
80 static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
81   return 0;
82 }
83 
84 //------------------------------------------------------------------------------
85 // QFunctionContext Destroy
86 //------------------------------------------------------------------------------
87 static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
88   int ierr;
89   CeedQFunctionContext_Ref *impl;
90   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChk(ierr);
91 
92   ierr = CeedFree(&impl->data_allocated); CeedChk(ierr);
93   ierr = CeedFree(&impl); CeedChk(ierr);
94   return 0;
95 }
96 
97 //------------------------------------------------------------------------------
98 // QFunctionContext Create
99 //------------------------------------------------------------------------------
100 int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
101   int ierr;
102   CeedQFunctionContext_Ref *impl;
103   Ceed ceed;
104   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
105 
106   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
107                                 CeedQFunctionContextSetData_Ref); CeedChk(ierr);
108   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
109                                 CeedQFunctionContextGetData_Ref); CeedChk(ierr);
110   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
111                                 CeedQFunctionContextRestoreData_Ref); CeedChk(ierr);
112   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
113                                 CeedQFunctionContextDestroy_Ref); CeedChk(ierr);
114   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
115   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChk(ierr);
116   return 0;
117 }
118 //------------------------------------------------------------------------------
119