xref: /libCEED/rust/libceed-sys/c-src/backends/memcheck/ceed-memcheck-qfunction.c (revision f10650af6497af6d0949b45f2eee824400fc9b71)
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 
17fc7cf9a0Sjeremylt #include "ceed-memcheck.h"
18fc7cf9a0Sjeremylt 
19*f10650afSjeremylt //------------------------------------------------------------------------------
20*f10650afSjeremylt // QFunction Apply
21*f10650afSjeremylt //------------------------------------------------------------------------------
22fc7cf9a0Sjeremylt static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q,
23fc7cf9a0Sjeremylt                                        CeedVector *U, CeedVector *V) {
24fc7cf9a0Sjeremylt   int ierr;
25fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
26fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChk(ierr);
27fc7cf9a0Sjeremylt 
28fc7cf9a0Sjeremylt   void *ctx;
29fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
30fc7cf9a0Sjeremylt 
31692c2638Sjeremylt   CeedQFunctionUser f = NULL;
32d4f68153Sjeremylt   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChk(ierr);
33fc7cf9a0Sjeremylt 
34fc7cf9a0Sjeremylt   CeedInt nIn, nOut;
35fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetNumArgs(qf, &nIn, &nOut); CeedChk(ierr);
36fc7cf9a0Sjeremylt 
37fc7cf9a0Sjeremylt   for (int i = 0; i<nIn; i++) {
38fc7cf9a0Sjeremylt     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
39fc7cf9a0Sjeremylt     CeedChk(ierr);
40fc7cf9a0Sjeremylt   }
41fc7cf9a0Sjeremylt   for (int i = 0; i<nOut; i++) {
42fc7cf9a0Sjeremylt     ierr = CeedVectorGetArray(V[i], CEED_MEM_HOST, &impl->outputs[i]);
43fc7cf9a0Sjeremylt     CeedChk(ierr);
44fc7cf9a0Sjeremylt     CeedInt len;
45fc7cf9a0Sjeremylt     ierr = CeedVectorGetLength(V[i], &len); CeedChk(ierr);
46fc7cf9a0Sjeremylt     VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len);
47fc7cf9a0Sjeremylt   }
48fc7cf9a0Sjeremylt 
49fc7cf9a0Sjeremylt   ierr = f(ctx, Q, impl->inputs, impl->outputs); CeedChk(ierr);
50fc7cf9a0Sjeremylt 
51fc7cf9a0Sjeremylt   for (int i = 0; i<nIn; i++) {
52fc7cf9a0Sjeremylt     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChk(ierr);
53fc7cf9a0Sjeremylt   }
54fc7cf9a0Sjeremylt   for (int i = 0; i<nOut; i++) {
55fc7cf9a0Sjeremylt     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChk(ierr);
56fc7cf9a0Sjeremylt   }
57fc7cf9a0Sjeremylt 
58fc7cf9a0Sjeremylt   return 0;
59fc7cf9a0Sjeremylt }
60fc7cf9a0Sjeremylt 
61*f10650afSjeremylt //------------------------------------------------------------------------------
62*f10650afSjeremylt // QFunction Destroy
63*f10650afSjeremylt //------------------------------------------------------------------------------
64fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) {
65fc7cf9a0Sjeremylt   int ierr;
66fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
67fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChk(ierr);
68fc7cf9a0Sjeremylt 
69fc7cf9a0Sjeremylt   ierr = CeedFree(&impl->inputs); CeedChk(ierr);
70fc7cf9a0Sjeremylt   ierr = CeedFree(&impl->outputs); CeedChk(ierr);
71fc7cf9a0Sjeremylt   ierr = CeedFree(&impl); CeedChk(ierr);
72fc7cf9a0Sjeremylt 
73fc7cf9a0Sjeremylt   return 0;
74fc7cf9a0Sjeremylt }
75fc7cf9a0Sjeremylt 
76*f10650afSjeremylt //------------------------------------------------------------------------------
77*f10650afSjeremylt // QFunction Create
78*f10650afSjeremylt //------------------------------------------------------------------------------
79fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) {
80fc7cf9a0Sjeremylt   int ierr;
81fc7cf9a0Sjeremylt   Ceed ceed;
82fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
83fc7cf9a0Sjeremylt 
84fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
85fc7cf9a0Sjeremylt   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
86fc7cf9a0Sjeremylt   ierr = CeedCalloc(16, &impl->inputs); CeedChk(ierr);
87fc7cf9a0Sjeremylt   ierr = CeedCalloc(16, &impl->outputs); CeedChk(ierr);
88fc7cf9a0Sjeremylt   ierr = CeedQFunctionSetData(qf, (void *)&impl); CeedChk(ierr);
89fc7cf9a0Sjeremylt 
90fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
91fc7cf9a0Sjeremylt                                 CeedQFunctionApply_Memcheck); CeedChk(ierr);
92fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
93fc7cf9a0Sjeremylt                                 CeedQFunctionDestroy_Memcheck); CeedChk(ierr);
94fc7cf9a0Sjeremylt 
95fc7cf9a0Sjeremylt   return 0;
96fc7cf9a0Sjeremylt }
97*f10650afSjeremylt //------------------------------------------------------------------------------
98