xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunctioncontext.c (revision 891038deaa55d3f0c57599b0bbe43569c9f61de7)
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 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy 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,
26d1d35e2fSjeremylt     CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *data) {
27777ff853SJeremy L Thompson   int ierr;
28777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
29e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
30e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
31d1d35e2fSjeremylt   size_t ctx_size;
32d1d35e2fSjeremylt   ierr = CeedQFunctionContextGetContextSize(ctx, &ctx_size); CeedChkBackend(ierr);
33777ff853SJeremy L Thompson   Ceed ceed;
34e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
35777ff853SJeremy L Thompson 
36d1d35e2fSjeremylt   if (mem_type != CEED_MEM_HOST)
37777ff853SJeremy L Thompson     // LCOV_EXCL_START
38e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Only MemType = HOST supported");
39777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
40e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->data_allocated); CeedChkBackend(ierr);
41d1d35e2fSjeremylt   switch (copy_mode) {
42777ff853SJeremy L Thompson   case CEED_COPY_VALUES:
43d1d35e2fSjeremylt     ierr = CeedMallocArray(1, ctx_size, &impl->data_allocated);
44d1d35e2fSjeremylt     CeedChkBackend(ierr);
45777ff853SJeremy L Thompson     impl->data = impl->data_allocated;
46d1d35e2fSjeremylt     memcpy(impl->data, data, ctx_size);
47777ff853SJeremy L Thompson     break;
48777ff853SJeremy L Thompson   case CEED_OWN_POINTER:
49777ff853SJeremy L Thompson     impl->data_allocated = data;
50777ff853SJeremy L Thompson     impl->data = data;
51777ff853SJeremy L Thompson     break;
52777ff853SJeremy L Thompson   case CEED_USE_POINTER:
53777ff853SJeremy L Thompson     impl->data = data;
54777ff853SJeremy L Thompson   }
55e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
56777ff853SJeremy L Thompson }
57777ff853SJeremy L Thompson 
58777ff853SJeremy L Thompson //------------------------------------------------------------------------------
59*891038deSjeremylt // QFunctionContext Take Data
60*891038deSjeremylt //------------------------------------------------------------------------------
61*891038deSjeremylt static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx,
62*891038deSjeremylt     CeedMemType mem_type, CeedScalar *data) {
63*891038deSjeremylt   int ierr;
64*891038deSjeremylt   CeedQFunctionContext_Ref *impl;
65*891038deSjeremylt   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
66*891038deSjeremylt   CeedChkBackend(ierr);
67*891038deSjeremylt   Ceed ceed;
68*891038deSjeremylt   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
69*891038deSjeremylt 
70*891038deSjeremylt   if (mem_type != CEED_MEM_HOST)
71*891038deSjeremylt     // LCOV_EXCL_START
72*891038deSjeremylt     return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide to HOST memory");
73*891038deSjeremylt   // LCOV_EXCL_STOP
74*891038deSjeremylt   if (!impl->data)
75*891038deSjeremylt     // LCOV_EXCL_START
76*891038deSjeremylt     return CeedError(ceed, CEED_ERROR_BACKEND, "No context data set");
77*891038deSjeremylt   // LCOV_EXCL_STOP
78*891038deSjeremylt   *(void **)data = impl->data;
79*891038deSjeremylt   impl->data = NULL;
80*891038deSjeremylt   impl->data_allocated = NULL;
81*891038deSjeremylt   return CEED_ERROR_SUCCESS;
82*891038deSjeremylt }
83*891038deSjeremylt 
84*891038deSjeremylt //------------------------------------------------------------------------------
85777ff853SJeremy L Thompson // QFunctionContext Get Data
86777ff853SJeremy L Thompson //------------------------------------------------------------------------------
87777ff853SJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx,
88d1d35e2fSjeremylt     CeedMemType mem_type, CeedScalar *data) {
89777ff853SJeremy L Thompson   int ierr;
90777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
91e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
92e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
93777ff853SJeremy L Thompson   Ceed ceed;
94e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
95777ff853SJeremy L Thompson 
96d1d35e2fSjeremylt   if (mem_type != CEED_MEM_HOST)
97777ff853SJeremy L Thompson     // LCOV_EXCL_START
98e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide to HOST memory");
99777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
100777ff853SJeremy L Thompson   if (!impl->data)
101777ff853SJeremy L Thompson     // LCOV_EXCL_START
102e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "No context data set");
103777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
104777ff853SJeremy L Thompson   *(void **)data = impl->data;
105e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
106777ff853SJeremy L Thompson }
107777ff853SJeremy L Thompson 
108777ff853SJeremy L Thompson //------------------------------------------------------------------------------
109777ff853SJeremy L Thompson // QFunctionContext Restore Data
110777ff853SJeremy L Thompson //------------------------------------------------------------------------------
111777ff853SJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
112e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
113777ff853SJeremy L Thompson }
114777ff853SJeremy L Thompson 
115777ff853SJeremy L Thompson //------------------------------------------------------------------------------
116777ff853SJeremy L Thompson // QFunctionContext Destroy
117777ff853SJeremy L Thompson //------------------------------------------------------------------------------
118777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
119777ff853SJeremy L Thompson   int ierr;
120777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
121e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChkBackend(ierr);
122777ff853SJeremy L Thompson 
123e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->data_allocated); CeedChkBackend(ierr);
124e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
125e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
126777ff853SJeremy L Thompson }
127777ff853SJeremy L Thompson 
128777ff853SJeremy L Thompson //------------------------------------------------------------------------------
129777ff853SJeremy L Thompson // QFunctionContext Create
130777ff853SJeremy L Thompson //------------------------------------------------------------------------------
131777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
132777ff853SJeremy L Thompson   int ierr;
133777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
134777ff853SJeremy L Thompson   Ceed ceed;
135e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
136777ff853SJeremy L Thompson 
137777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
138e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextSetData_Ref); CeedChkBackend(ierr);
139*891038deSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData",
140*891038deSjeremylt                                 CeedQFunctionContextTakeData_Ref); CeedChkBackend(ierr);
141777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
142e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextGetData_Ref); CeedChkBackend(ierr);
143777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
144e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
145777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
146e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextDestroy_Ref); CeedChkBackend(ierr);
147e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
148e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChkBackend(ierr);
149e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
150777ff853SJeremy L Thompson }
151777ff853SJeremy L Thompson //------------------------------------------------------------------------------
152