xref: /libCEED/backends/ref/ceed-ref-qfunctioncontext.c (revision e6ac5b306d502db4218066c39deae8c617cc3fc2)
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/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 mem_type, CeedCopyMode copy_mode, CeedScalar *data) {
27   int ierr;
28   CeedQFunctionContext_Ref *impl;
29   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
30   CeedChkBackend(ierr);
31   size_t ctx_size;
32   ierr = CeedQFunctionContextGetContextSize(ctx, &ctx_size); CeedChkBackend(ierr);
33   Ceed ceed;
34   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
35 
36   if (mem_type != CEED_MEM_HOST)
37     // LCOV_EXCL_START
38     return CeedError(ceed, CEED_ERROR_BACKEND, "Only MemType = HOST supported");
39   // LCOV_EXCL_STOP
40   ierr = CeedFree(&impl->data_allocated); CeedChkBackend(ierr);
41   switch (copy_mode) {
42   case CEED_COPY_VALUES:
43     ierr = CeedMallocArray(1, ctx_size, &impl->data_allocated);
44     CeedChkBackend(ierr);
45     impl->data = impl->data_allocated;
46     memcpy(impl->data, data, ctx_size);
47     break;
48   case CEED_OWN_POINTER:
49     impl->data_allocated = data;
50     impl->data = data;
51     break;
52   case CEED_USE_POINTER:
53     impl->data = data;
54   }
55   return CEED_ERROR_SUCCESS;
56 }
57 
58 //------------------------------------------------------------------------------
59 // QFunctionContext Take Data
60 //------------------------------------------------------------------------------
61 static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx,
62     CeedMemType mem_type, CeedScalar *data) {
63   int ierr;
64   CeedQFunctionContext_Ref *impl;
65   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
66   CeedChkBackend(ierr);
67   Ceed ceed;
68   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
69 
70   if (mem_type != CEED_MEM_HOST)
71     // LCOV_EXCL_START
72     return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide to HOST memory");
73   // LCOV_EXCL_STOP
74   if (!impl->data)
75     // LCOV_EXCL_START
76     return CeedError(ceed, CEED_ERROR_BACKEND, "No context data set");
77   // LCOV_EXCL_STOP
78   *(void **)data = impl->data;
79   impl->data = NULL;
80   impl->data_allocated = NULL;
81   return CEED_ERROR_SUCCESS;
82 }
83 
84 //------------------------------------------------------------------------------
85 // QFunctionContext Get Data
86 //------------------------------------------------------------------------------
87 static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx,
88     CeedMemType mem_type, CeedScalar *data) {
89   int ierr;
90   CeedQFunctionContext_Ref *impl;
91   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
92   CeedChkBackend(ierr);
93   Ceed ceed;
94   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
95 
96   if (mem_type != CEED_MEM_HOST)
97     // LCOV_EXCL_START
98     return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide to HOST memory");
99   // LCOV_EXCL_STOP
100   if (!impl->data)
101     // LCOV_EXCL_START
102     return CeedError(ceed, CEED_ERROR_BACKEND, "No context data set");
103   // LCOV_EXCL_STOP
104   *(void **)data = impl->data;
105   return CEED_ERROR_SUCCESS;
106 }
107 
108 //------------------------------------------------------------------------------
109 // QFunctionContext Restore Data
110 //------------------------------------------------------------------------------
111 static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
112   return CEED_ERROR_SUCCESS;
113 }
114 
115 //------------------------------------------------------------------------------
116 // QFunctionContext Destroy
117 //------------------------------------------------------------------------------
118 static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
119   int ierr;
120   CeedQFunctionContext_Ref *impl;
121   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChkBackend(ierr);
122 
123   ierr = CeedFree(&impl->data_allocated); CeedChkBackend(ierr);
124   ierr = CeedFree(&impl); CeedChkBackend(ierr);
125   return CEED_ERROR_SUCCESS;
126 }
127 
128 //------------------------------------------------------------------------------
129 // QFunctionContext Create
130 //------------------------------------------------------------------------------
131 int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
132   int ierr;
133   CeedQFunctionContext_Ref *impl;
134   Ceed ceed;
135   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
136 
137   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
138                                 CeedQFunctionContextSetData_Ref); CeedChkBackend(ierr);
139   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData",
140                                 CeedQFunctionContextTakeData_Ref); CeedChkBackend(ierr);
141   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
142                                 CeedQFunctionContextGetData_Ref); CeedChkBackend(ierr);
143   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
144                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
145   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
146                                 CeedQFunctionContextDestroy_Ref); CeedChkBackend(ierr);
147   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
148   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChkBackend(ierr);
149   return CEED_ERROR_SUCCESS;
150 }
151 //------------------------------------------------------------------------------
152