xref: /libCEED/rust/libceed-sys/c-src/backends/memcheck/ceed-memcheck-qfunction.c (revision 3d576824e8d990e1f48c6609089904bee9170514)
1fc7cf9a0Sjeremylt // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2fc7cf9a0Sjeremylt // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3fc7cf9a0Sjeremylt // All Rights reserved. See files LICENSE and NOTICE for details.
4fc7cf9a0Sjeremylt //
5fc7cf9a0Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6fc7cf9a0Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7fc7cf9a0Sjeremylt // element discretizations for exascale applications. For more information and
8fc7cf9a0Sjeremylt // source code availability see http://github.com/ceed.
9fc7cf9a0Sjeremylt //
10fc7cf9a0Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11fc7cf9a0Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12fc7cf9a0Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13fc7cf9a0Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14fc7cf9a0Sjeremylt // software, applications, hardware, advanced system engineering and early
15fc7cf9a0Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16fc7cf9a0Sjeremylt 
17*3d576824SJeremy L Thompson #include <ceed.h>
18*3d576824SJeremy L Thompson #include <ceed-backend.h>
19*3d576824SJeremy L Thompson #include <valgrind/memcheck.h>
20fc7cf9a0Sjeremylt #include "ceed-memcheck.h"
21fc7cf9a0Sjeremylt 
22f10650afSjeremylt //------------------------------------------------------------------------------
23f10650afSjeremylt // QFunction Apply
24f10650afSjeremylt //------------------------------------------------------------------------------
25fc7cf9a0Sjeremylt static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q,
26fc7cf9a0Sjeremylt                                        CeedVector *U, CeedVector *V) {
27fc7cf9a0Sjeremylt   int ierr;
28fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
29777ff853SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &impl); CeedChk(ierr);
30fc7cf9a0Sjeremylt 
31777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
32fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
33777ff853SJeremy L Thompson   void *ctxData = NULL;
34777ff853SJeremy L Thompson   if (ctx) {
35777ff853SJeremy L Thompson     ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctxData);
36777ff853SJeremy L Thompson     CeedChk(ierr);
37777ff853SJeremy L Thompson   }
38fc7cf9a0Sjeremylt 
39692c2638Sjeremylt   CeedQFunctionUser f = NULL;
40d4f68153Sjeremylt   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChk(ierr);
41fc7cf9a0Sjeremylt 
42fc7cf9a0Sjeremylt   CeedInt nIn, nOut;
43fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetNumArgs(qf, &nIn, &nOut); CeedChk(ierr);
44fc7cf9a0Sjeremylt 
45fc7cf9a0Sjeremylt   for (int i = 0; i<nIn; i++) {
46fc7cf9a0Sjeremylt     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
47fc7cf9a0Sjeremylt     CeedChk(ierr);
48fc7cf9a0Sjeremylt   }
49fc7cf9a0Sjeremylt   for (int i = 0; i<nOut; i++) {
50fc7cf9a0Sjeremylt     ierr = CeedVectorGetArray(V[i], CEED_MEM_HOST, &impl->outputs[i]);
51fc7cf9a0Sjeremylt     CeedChk(ierr);
52fc7cf9a0Sjeremylt     CeedInt len;
53fc7cf9a0Sjeremylt     ierr = CeedVectorGetLength(V[i], &len); CeedChk(ierr);
54fc7cf9a0Sjeremylt     VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len);
55fc7cf9a0Sjeremylt   }
56fc7cf9a0Sjeremylt 
57777ff853SJeremy L Thompson   ierr = f(ctxData, Q, impl->inputs, impl->outputs); CeedChk(ierr);
58fc7cf9a0Sjeremylt 
59fc7cf9a0Sjeremylt   for (int i = 0; i<nIn; i++) {
60fc7cf9a0Sjeremylt     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChk(ierr);
61fc7cf9a0Sjeremylt   }
62fc7cf9a0Sjeremylt   for (int i = 0; i<nOut; i++) {
63fc7cf9a0Sjeremylt     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChk(ierr);
64fc7cf9a0Sjeremylt   }
65777ff853SJeremy L Thompson   if (ctx) {
66777ff853SJeremy L Thompson     ierr = CeedQFunctionContextRestoreData(ctx, &ctxData); CeedChk(ierr);
67777ff853SJeremy L Thompson   }
68fc7cf9a0Sjeremylt 
69fc7cf9a0Sjeremylt   return 0;
70fc7cf9a0Sjeremylt }
71fc7cf9a0Sjeremylt 
72f10650afSjeremylt //------------------------------------------------------------------------------
73f10650afSjeremylt // QFunction Destroy
74f10650afSjeremylt //------------------------------------------------------------------------------
75fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) {
76fc7cf9a0Sjeremylt   int ierr;
77fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
78fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChk(ierr);
79fc7cf9a0Sjeremylt 
80fc7cf9a0Sjeremylt   ierr = CeedFree(&impl->inputs); CeedChk(ierr);
81fc7cf9a0Sjeremylt   ierr = CeedFree(&impl->outputs); CeedChk(ierr);
82fc7cf9a0Sjeremylt   ierr = CeedFree(&impl); CeedChk(ierr);
83fc7cf9a0Sjeremylt 
84fc7cf9a0Sjeremylt   return 0;
85fc7cf9a0Sjeremylt }
86fc7cf9a0Sjeremylt 
87f10650afSjeremylt //------------------------------------------------------------------------------
88f10650afSjeremylt // QFunction Create
89f10650afSjeremylt //------------------------------------------------------------------------------
90fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) {
91fc7cf9a0Sjeremylt   int ierr;
92fc7cf9a0Sjeremylt   Ceed ceed;
93fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
94fc7cf9a0Sjeremylt 
95fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
96fc7cf9a0Sjeremylt   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
97fc7cf9a0Sjeremylt   ierr = CeedCalloc(16, &impl->inputs); CeedChk(ierr);
98fc7cf9a0Sjeremylt   ierr = CeedCalloc(16, &impl->outputs); CeedChk(ierr);
99777ff853SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, impl); CeedChk(ierr);
100fc7cf9a0Sjeremylt 
101fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
102fc7cf9a0Sjeremylt                                 CeedQFunctionApply_Memcheck); CeedChk(ierr);
103fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
104fc7cf9a0Sjeremylt                                 CeedQFunctionDestroy_Memcheck); CeedChk(ierr);
105fc7cf9a0Sjeremylt 
106fc7cf9a0Sjeremylt   return 0;
107fc7cf9a0Sjeremylt }
108f10650afSjeremylt //------------------------------------------------------------------------------
109