xref: /libCEED/rust/libceed-sys/c-src/backends/memcheck/ceed-memcheck-qfunction.c (revision edc819a10d47c4f248d00a144b44345fa45bfe5b)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3fc7cf9a0Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5fc7cf9a0Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7fc7cf9a0Sjeremylt 
8ec3da8bcSJed Brown #include <ceed/ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
10*edc819a1SJeremy L Thompson #include <string.h>
113d576824SJeremy L Thompson #include <valgrind/memcheck.h>
12fc7cf9a0Sjeremylt #include "ceed-memcheck.h"
13fc7cf9a0Sjeremylt 
14f10650afSjeremylt //------------------------------------------------------------------------------
15f10650afSjeremylt // QFunction Apply
16f10650afSjeremylt //------------------------------------------------------------------------------
17fc7cf9a0Sjeremylt static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q,
18fc7cf9a0Sjeremylt                                        CeedVector *U, CeedVector *V) {
19fc7cf9a0Sjeremylt   int ierr;
20fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
21e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr);
22fc7cf9a0Sjeremylt 
23777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
24e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChkBackend(ierr);
25777ff853SJeremy L Thompson   void *ctxData = NULL;
26777ff853SJeremy L Thompson   if (ctx) {
27777ff853SJeremy L Thompson     ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctxData);
28e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
29777ff853SJeremy L Thompson   }
30fc7cf9a0Sjeremylt 
31692c2638Sjeremylt   CeedQFunctionUser f = NULL;
32e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChkBackend(ierr);
33fc7cf9a0Sjeremylt 
34d1d35e2fSjeremylt   CeedInt num_in, num_out;
35d1d35e2fSjeremylt   ierr = CeedQFunctionGetNumArgs(qf, &num_in, &num_out); CeedChkBackend(ierr);
36fc7cf9a0Sjeremylt 
37*edc819a1SJeremy L Thompson   for (CeedInt i = 0; i<num_in; i++) {
38fc7cf9a0Sjeremylt     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
39e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
40fc7cf9a0Sjeremylt   }
41*edc819a1SJeremy L Thompson   int mem_block_ids[num_out];
42*edc819a1SJeremy L Thompson   for (CeedInt i = 0; i<num_out; i++) {
43*edc819a1SJeremy L Thompson     CeedSize len;
44*edc819a1SJeremy L Thompson     char name[30] = "";
45*edc819a1SJeremy L Thompson 
469c774eddSJeremy L Thompson     ierr = CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]);
47e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
48*edc819a1SJeremy L Thompson 
49e15f9bd0SJeremy L Thompson     ierr = CeedVectorGetLength(V[i], &len); CeedChkBackend(ierr);
50fc7cf9a0Sjeremylt     VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len);
51*edc819a1SJeremy L Thompson 
52*edc819a1SJeremy L Thompson     snprintf(name, 30, "'QFunction output %d'", i);
53*edc819a1SJeremy L Thompson     mem_block_ids[i] = VALGRIND_CREATE_BLOCK(impl->outputs[i], len, name);
54fc7cf9a0Sjeremylt   }
55fc7cf9a0Sjeremylt 
56e15f9bd0SJeremy L Thompson   ierr = f(ctxData, Q, impl->inputs, impl->outputs); CeedChkBackend(ierr);
57fc7cf9a0Sjeremylt 
58*edc819a1SJeremy L Thompson   for (CeedInt i = 0; i<num_in; i++) {
59e15f9bd0SJeremy L Thompson     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChkBackend(ierr);
60fc7cf9a0Sjeremylt   }
61*edc819a1SJeremy L Thompson   for (CeedInt i = 0; i<num_out; i++) {
62e15f9bd0SJeremy L Thompson     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChkBackend(ierr);
63*edc819a1SJeremy L Thompson     VALGRIND_DISCARD(mem_block_ids[i]);
64fc7cf9a0Sjeremylt   }
65777ff853SJeremy L Thompson   if (ctx) {
66e15f9bd0SJeremy L Thompson     ierr = CeedQFunctionContextRestoreData(ctx, &ctxData); CeedChkBackend(ierr);
67777ff853SJeremy L Thompson   }
68fc7cf9a0Sjeremylt 
69e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
70fc7cf9a0Sjeremylt }
71fc7cf9a0Sjeremylt 
72f10650afSjeremylt //------------------------------------------------------------------------------
73f10650afSjeremylt // QFunction Destroy
74f10650afSjeremylt //------------------------------------------------------------------------------
75fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) {
76fc7cf9a0Sjeremylt   int ierr;
77fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
78e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChkBackend(ierr);
79fc7cf9a0Sjeremylt 
80e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->inputs); CeedChkBackend(ierr);
81e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->outputs); CeedChkBackend(ierr);
82e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
83fc7cf9a0Sjeremylt 
84e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85fc7cf9a0Sjeremylt }
86fc7cf9a0Sjeremylt 
87f10650afSjeremylt //------------------------------------------------------------------------------
88f10650afSjeremylt // QFunction Create
89f10650afSjeremylt //------------------------------------------------------------------------------
90fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) {
91fc7cf9a0Sjeremylt   int ierr;
92fc7cf9a0Sjeremylt   Ceed ceed;
93e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
94fc7cf9a0Sjeremylt 
95fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
96e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
97bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &impl->inputs); CeedChkBackend(ierr);
98bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &impl->outputs); CeedChkBackend(ierr);
99e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, impl); CeedChkBackend(ierr);
100fc7cf9a0Sjeremylt 
101fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
102e15f9bd0SJeremy L Thompson                                 CeedQFunctionApply_Memcheck); CeedChkBackend(ierr);
103fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
104e15f9bd0SJeremy L Thompson                                 CeedQFunctionDestroy_Memcheck); CeedChkBackend(ierr);
105fc7cf9a0Sjeremylt 
106e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
107fc7cf9a0Sjeremylt }
108f10650afSjeremylt //------------------------------------------------------------------------------
109