xref: /libCEED/backends/memcheck/ceed-memcheck-qfunction.c (revision fc7cf9a0106c61e423592cfe742ac213c7a4a062)
1*fc7cf9a0Sjeremylt // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2*fc7cf9a0Sjeremylt // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3*fc7cf9a0Sjeremylt // All Rights reserved. See files LICENSE and NOTICE for details.
4*fc7cf9a0Sjeremylt //
5*fc7cf9a0Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6*fc7cf9a0Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7*fc7cf9a0Sjeremylt // element discretizations for exascale applications. For more information and
8*fc7cf9a0Sjeremylt // source code availability see http://github.com/ceed.
9*fc7cf9a0Sjeremylt //
10*fc7cf9a0Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11*fc7cf9a0Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12*fc7cf9a0Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13*fc7cf9a0Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14*fc7cf9a0Sjeremylt // software, applications, hardware, advanced system engineering and early
15*fc7cf9a0Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16*fc7cf9a0Sjeremylt 
17*fc7cf9a0Sjeremylt #include "ceed-memcheck.h"
18*fc7cf9a0Sjeremylt 
19*fc7cf9a0Sjeremylt static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q,
20*fc7cf9a0Sjeremylt                                          CeedVector *U, CeedVector *V) {
21*fc7cf9a0Sjeremylt   int ierr;
22*fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
23*fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChk(ierr);
24*fc7cf9a0Sjeremylt 
25*fc7cf9a0Sjeremylt   void *ctx;
26*fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
27*fc7cf9a0Sjeremylt 
28*fc7cf9a0Sjeremylt   int (*f)() = NULL;
29*fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetUserFunction(qf, (int (* *)())&f); CeedChk(ierr);
30*fc7cf9a0Sjeremylt 
31*fc7cf9a0Sjeremylt   CeedInt nIn, nOut;
32*fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetNumArgs(qf, &nIn, &nOut); CeedChk(ierr);
33*fc7cf9a0Sjeremylt 
34*fc7cf9a0Sjeremylt   for (int i = 0; i<nIn; i++) {
35*fc7cf9a0Sjeremylt     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
36*fc7cf9a0Sjeremylt     CeedChk(ierr);
37*fc7cf9a0Sjeremylt   }
38*fc7cf9a0Sjeremylt   for (int i = 0; i<nOut; i++) {
39*fc7cf9a0Sjeremylt     ierr = CeedVectorGetArray(V[i], CEED_MEM_HOST, &impl->outputs[i]);
40*fc7cf9a0Sjeremylt     CeedChk(ierr);
41*fc7cf9a0Sjeremylt     CeedInt len;
42*fc7cf9a0Sjeremylt     ierr = CeedVectorGetLength(V[i], &len); CeedChk(ierr);
43*fc7cf9a0Sjeremylt     VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len);
44*fc7cf9a0Sjeremylt   }
45*fc7cf9a0Sjeremylt 
46*fc7cf9a0Sjeremylt   ierr = f(ctx, Q, impl->inputs, impl->outputs); CeedChk(ierr);
47*fc7cf9a0Sjeremylt 
48*fc7cf9a0Sjeremylt   for (int i = 0; i<nIn; i++) {
49*fc7cf9a0Sjeremylt     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChk(ierr);
50*fc7cf9a0Sjeremylt   }
51*fc7cf9a0Sjeremylt   for (int i = 0; i<nOut; i++) {
52*fc7cf9a0Sjeremylt     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChk(ierr);
53*fc7cf9a0Sjeremylt   }
54*fc7cf9a0Sjeremylt 
55*fc7cf9a0Sjeremylt   return 0;
56*fc7cf9a0Sjeremylt }
57*fc7cf9a0Sjeremylt 
58*fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) {
59*fc7cf9a0Sjeremylt   int ierr;
60*fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
61*fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChk(ierr);
62*fc7cf9a0Sjeremylt 
63*fc7cf9a0Sjeremylt   ierr = CeedFree(&impl->inputs); CeedChk(ierr);
64*fc7cf9a0Sjeremylt   ierr = CeedFree(&impl->outputs); CeedChk(ierr);
65*fc7cf9a0Sjeremylt   ierr = CeedFree(&impl); CeedChk(ierr);
66*fc7cf9a0Sjeremylt 
67*fc7cf9a0Sjeremylt   return 0;
68*fc7cf9a0Sjeremylt }
69*fc7cf9a0Sjeremylt 
70*fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) {
71*fc7cf9a0Sjeremylt   int ierr;
72*fc7cf9a0Sjeremylt   Ceed ceed;
73*fc7cf9a0Sjeremylt   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
74*fc7cf9a0Sjeremylt 
75*fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
76*fc7cf9a0Sjeremylt   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
77*fc7cf9a0Sjeremylt   ierr = CeedCalloc(16, &impl->inputs); CeedChk(ierr);
78*fc7cf9a0Sjeremylt   ierr = CeedCalloc(16, &impl->outputs); CeedChk(ierr);
79*fc7cf9a0Sjeremylt   ierr = CeedQFunctionSetData(qf, (void *)&impl); CeedChk(ierr);
80*fc7cf9a0Sjeremylt 
81*fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
82*fc7cf9a0Sjeremylt                                 CeedQFunctionApply_Memcheck); CeedChk(ierr);
83*fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
84*fc7cf9a0Sjeremylt                                 CeedQFunctionDestroy_Memcheck); CeedChk(ierr);
85*fc7cf9a0Sjeremylt 
86*fc7cf9a0Sjeremylt   return 0;
87*fc7cf9a0Sjeremylt }
88