xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunctioncontext.c (revision d1d35e2f02dc969aee8debf3fd943dd784aa847a)
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 Get Data
60 //------------------------------------------------------------------------------
61 static int CeedQFunctionContextGetData_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   return CEED_ERROR_SUCCESS;
80 }
81 
82 //------------------------------------------------------------------------------
83 // QFunctionContext Restore Data
84 //------------------------------------------------------------------------------
85 static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
86   return CEED_ERROR_SUCCESS;
87 }
88 
89 //------------------------------------------------------------------------------
90 // QFunctionContext Destroy
91 //------------------------------------------------------------------------------
92 static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
93   int ierr;
94   CeedQFunctionContext_Ref *impl;
95   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChkBackend(ierr);
96 
97   ierr = CeedFree(&impl->data_allocated); CeedChkBackend(ierr);
98   ierr = CeedFree(&impl); CeedChkBackend(ierr);
99   return CEED_ERROR_SUCCESS;
100 }
101 
102 //------------------------------------------------------------------------------
103 // QFunctionContext Create
104 //------------------------------------------------------------------------------
105 int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
106   int ierr;
107   CeedQFunctionContext_Ref *impl;
108   Ceed ceed;
109   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
110 
111   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
112                                 CeedQFunctionContextSetData_Ref); CeedChkBackend(ierr);
113   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
114                                 CeedQFunctionContextGetData_Ref); CeedChkBackend(ierr);
115   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
116                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
117   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
118                                 CeedQFunctionContextDestroy_Ref); CeedChkBackend(ierr);
119   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
120   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChkBackend(ierr);
121   return CEED_ERROR_SUCCESS;
122 }
123 //------------------------------------------------------------------------------
124