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