xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunctioncontext.c (revision 3d576824e8d990e1f48c6609089904bee9170514)
1777ff853SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2777ff853SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3777ff853SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details.
4777ff853SJeremy L Thompson //
5777ff853SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
6777ff853SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
7777ff853SJeremy L Thompson // element discretizations for exascale applications. For more information and
8777ff853SJeremy L Thompson // source code availability see http://github.com/ceed.
9777ff853SJeremy L Thompson //
10777ff853SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11777ff853SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
12777ff853SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
13777ff853SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
14777ff853SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
15777ff853SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
16777ff853SJeremy L Thompson 
17*3d576824SJeremy L Thompson #include <ceed.h>
18*3d576824SJeremy L Thompson #include <ceed-backend.h>
19*3d576824SJeremy L Thompson #include <string.h>
20777ff853SJeremy L Thompson #include "ceed-ref.h"
21777ff853SJeremy L Thompson 
22777ff853SJeremy L Thompson //------------------------------------------------------------------------------
23777ff853SJeremy L Thompson // QFunctionContext Set Data
24777ff853SJeremy L Thompson //------------------------------------------------------------------------------
25777ff853SJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx,
26777ff853SJeremy L Thompson     CeedMemType mtype,
27777ff853SJeremy L Thompson     CeedCopyMode cmode, CeedScalar *data) {
28777ff853SJeremy L Thompson   int ierr;
29777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
30777ff853SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl); CeedChk(ierr);
31777ff853SJeremy L Thompson   size_t ctxsize;
32777ff853SJeremy L Thompson   ierr = CeedQFunctionContextGetContextSize(ctx, &ctxsize); CeedChk(ierr);
33777ff853SJeremy L Thompson   Ceed ceed;
34777ff853SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
35777ff853SJeremy L Thompson 
36777ff853SJeremy L Thompson   if (mtype != CEED_MEM_HOST)
37777ff853SJeremy L Thompson     // LCOV_EXCL_START
38777ff853SJeremy L Thompson     return CeedError(ceed, 1, "Only MemType = HOST supported");
39777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
40777ff853SJeremy L Thompson   ierr = CeedFree(&impl->data_allocated); CeedChk(ierr);
41777ff853SJeremy L Thompson   switch (cmode) {
42777ff853SJeremy L Thompson   case CEED_COPY_VALUES:
43777ff853SJeremy L Thompson     ierr = CeedMallocArray(1, ctxsize, &impl->data_allocated); CeedChk(ierr);
44777ff853SJeremy L Thompson     impl->data = impl->data_allocated;
45777ff853SJeremy L Thompson     memcpy(impl->data, data, ctxsize);
46777ff853SJeremy L Thompson     break;
47777ff853SJeremy L Thompson   case CEED_OWN_POINTER:
48777ff853SJeremy L Thompson     impl->data_allocated = data;
49777ff853SJeremy L Thompson     impl->data = data;
50777ff853SJeremy L Thompson     break;
51777ff853SJeremy L Thompson   case CEED_USE_POINTER:
52777ff853SJeremy L Thompson     impl->data = data;
53777ff853SJeremy L Thompson   }
54777ff853SJeremy L Thompson   return 0;
55777ff853SJeremy L Thompson }
56777ff853SJeremy L Thompson 
57777ff853SJeremy L Thompson //------------------------------------------------------------------------------
58777ff853SJeremy L Thompson // QFunctionContext Get Data
59777ff853SJeremy L Thompson //------------------------------------------------------------------------------
60777ff853SJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx,
61777ff853SJeremy L Thompson     CeedMemType mtype, CeedScalar *data) {
62777ff853SJeremy L Thompson   int ierr;
63777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
64777ff853SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl); CeedChk(ierr);
65777ff853SJeremy L Thompson   Ceed ceed;
66777ff853SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
67777ff853SJeremy L Thompson 
68777ff853SJeremy L Thompson   if (mtype != CEED_MEM_HOST)
69777ff853SJeremy L Thompson     // LCOV_EXCL_START
70777ff853SJeremy L Thompson     return CeedError(ceed, 1, "Can only provide to HOST memory");
71777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
72777ff853SJeremy L Thompson   if (!impl->data)
73777ff853SJeremy L Thompson     // LCOV_EXCL_START
74777ff853SJeremy L Thompson     return CeedError(ceed, 1, "No context data set");
75777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
76777ff853SJeremy L Thompson   *(void **)data = impl->data;
77777ff853SJeremy L Thompson   return 0;
78777ff853SJeremy L Thompson }
79777ff853SJeremy L Thompson 
80777ff853SJeremy L Thompson //------------------------------------------------------------------------------
81777ff853SJeremy L Thompson // QFunctionContext Restore Data
82777ff853SJeremy L Thompson //------------------------------------------------------------------------------
83777ff853SJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
84777ff853SJeremy L Thompson   return 0;
85777ff853SJeremy L Thompson }
86777ff853SJeremy L Thompson 
87777ff853SJeremy L Thompson //------------------------------------------------------------------------------
88777ff853SJeremy L Thompson // QFunctionContext Destroy
89777ff853SJeremy L Thompson //------------------------------------------------------------------------------
90777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
91777ff853SJeremy L Thompson   int ierr;
92777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
93777ff853SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChk(ierr);
94777ff853SJeremy L Thompson 
95777ff853SJeremy L Thompson   ierr = CeedFree(&impl->data_allocated); CeedChk(ierr);
96777ff853SJeremy L Thompson   ierr = CeedFree(&impl); CeedChk(ierr);
97777ff853SJeremy L Thompson   return 0;
98777ff853SJeremy L Thompson }
99777ff853SJeremy L Thompson 
100777ff853SJeremy L Thompson //------------------------------------------------------------------------------
101777ff853SJeremy L Thompson // QFunctionContext Create
102777ff853SJeremy L Thompson //------------------------------------------------------------------------------
103777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
104777ff853SJeremy L Thompson   int ierr;
105777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
106777ff853SJeremy L Thompson   Ceed ceed;
107777ff853SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChk(ierr);
108777ff853SJeremy L Thompson 
109777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
110777ff853SJeremy L Thompson                                 CeedQFunctionContextSetData_Ref); CeedChk(ierr);
111777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
112777ff853SJeremy L Thompson                                 CeedQFunctionContextGetData_Ref); CeedChk(ierr);
113777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
114777ff853SJeremy L Thompson                                 CeedQFunctionContextRestoreData_Ref); CeedChk(ierr);
115777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
116777ff853SJeremy L Thompson                                 CeedQFunctionContextDestroy_Ref); CeedChk(ierr);
117777ff853SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
118777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChk(ierr);
119777ff853SJeremy L Thompson   return 0;
120777ff853SJeremy L Thompson }
121777ff853SJeremy L Thompson //------------------------------------------------------------------------------
122