xref: /libCEED/backends/ref/ceed-ref-qfunctioncontext.c (revision ec3da8bcb94d9f0073544b37b5081a06981a86f7)
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*ec3da8bcSJed Brown #include <ceed/ceed.h>
18*ec3da8bcSJed 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,
26777ff853SJeremy L Thompson     CeedMemType mtype,
27777ff853SJeremy L Thompson     CeedCopyMode cmode, CeedScalar *data) {
28777ff853SJeremy L Thompson   int ierr;
29777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
30e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
31e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
32777ff853SJeremy L Thompson   size_t ctxsize;
33e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetContextSize(ctx, &ctxsize); CeedChkBackend(ierr);
34777ff853SJeremy L Thompson   Ceed ceed;
35e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
36777ff853SJeremy L Thompson 
37777ff853SJeremy L Thompson   if (mtype != CEED_MEM_HOST)
38777ff853SJeremy L Thompson     // LCOV_EXCL_START
39e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Only MemType = HOST supported");
40777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
41e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->data_allocated); CeedChkBackend(ierr);
42777ff853SJeremy L Thompson   switch (cmode) {
43777ff853SJeremy L Thompson   case CEED_COPY_VALUES:
44e15f9bd0SJeremy L Thompson     ierr = CeedMallocArray(1, ctxsize, &impl->data_allocated); CeedChkBackend(ierr);
45777ff853SJeremy L Thompson     impl->data = impl->data_allocated;
46777ff853SJeremy L Thompson     memcpy(impl->data, data, ctxsize);
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 //------------------------------------------------------------------------------
59777ff853SJeremy L Thompson // QFunctionContext Get Data
60777ff853SJeremy L Thompson //------------------------------------------------------------------------------
61777ff853SJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx,
62777ff853SJeremy L Thompson     CeedMemType mtype, CeedScalar *data) {
63777ff853SJeremy L Thompson   int ierr;
64777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
65e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
66e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
67777ff853SJeremy L Thompson   Ceed ceed;
68e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
69777ff853SJeremy L Thompson 
70777ff853SJeremy L Thompson   if (mtype != CEED_MEM_HOST)
71777ff853SJeremy L Thompson     // LCOV_EXCL_START
72e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide to HOST memory");
73777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
74777ff853SJeremy L Thompson   if (!impl->data)
75777ff853SJeremy L Thompson     // LCOV_EXCL_START
76e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "No context data set");
77777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
78777ff853SJeremy L Thompson   *(void **)data = impl->data;
79e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
80777ff853SJeremy L Thompson }
81777ff853SJeremy L Thompson 
82777ff853SJeremy L Thompson //------------------------------------------------------------------------------
83777ff853SJeremy L Thompson // QFunctionContext Restore Data
84777ff853SJeremy L Thompson //------------------------------------------------------------------------------
85777ff853SJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
86e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87777ff853SJeremy L Thompson }
88777ff853SJeremy L Thompson 
89777ff853SJeremy L Thompson //------------------------------------------------------------------------------
90777ff853SJeremy L Thompson // QFunctionContext Destroy
91777ff853SJeremy L Thompson //------------------------------------------------------------------------------
92777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
93777ff853SJeremy L Thompson   int ierr;
94777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
95e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChkBackend(ierr);
96777ff853SJeremy L Thompson 
97e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->data_allocated); CeedChkBackend(ierr);
98e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
99e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
100777ff853SJeremy L Thompson }
101777ff853SJeremy L Thompson 
102777ff853SJeremy L Thompson //------------------------------------------------------------------------------
103777ff853SJeremy L Thompson // QFunctionContext Create
104777ff853SJeremy L Thompson //------------------------------------------------------------------------------
105777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
106777ff853SJeremy L Thompson   int ierr;
107777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
108777ff853SJeremy L Thompson   Ceed ceed;
109e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
110777ff853SJeremy L Thompson 
111777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
112e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextSetData_Ref); CeedChkBackend(ierr);
113777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
114e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextGetData_Ref); CeedChkBackend(ierr);
115777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
116e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
117777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
118e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextDestroy_Ref); CeedChkBackend(ierr);
119e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
120e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChkBackend(ierr);
121e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
122777ff853SJeremy L Thompson }
123777ff853SJeremy L Thompson //------------------------------------------------------------------------------
124