xref: /libCEED/backends/memcheck/ceed-memcheck-qfunction.c (revision fb02a1652bbc3fc0caa8288bcc9fd5bfd4ec4903)
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>
10edc819a1SJeremy 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 
23*fb02a165SJeremy L Thompson   void *ctx_data = NULL;
24*fb02a165SJeremy L Thompson   ierr = CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data);
25e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
26fc7cf9a0Sjeremylt 
27692c2638Sjeremylt   CeedQFunctionUser f = NULL;
28e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChkBackend(ierr);
29fc7cf9a0Sjeremylt 
30d1d35e2fSjeremylt   CeedInt num_in, num_out;
31d1d35e2fSjeremylt   ierr = CeedQFunctionGetNumArgs(qf, &num_in, &num_out); CeedChkBackend(ierr);
32fc7cf9a0Sjeremylt 
33edc819a1SJeremy L Thompson   for (CeedInt i = 0; i<num_in; i++) {
34fc7cf9a0Sjeremylt     ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]);
35e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
36fc7cf9a0Sjeremylt   }
37edc819a1SJeremy L Thompson   int mem_block_ids[num_out];
38edc819a1SJeremy L Thompson   for (CeedInt i = 0; i<num_out; i++) {
39edc819a1SJeremy L Thompson     CeedSize len;
40edc819a1SJeremy L Thompson     char name[30] = "";
41edc819a1SJeremy L Thompson 
429c774eddSJeremy L Thompson     ierr = CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]);
43e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
44edc819a1SJeremy L Thompson 
45e15f9bd0SJeremy L Thompson     ierr = CeedVectorGetLength(V[i], &len); CeedChkBackend(ierr);
46fc7cf9a0Sjeremylt     VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len);
47edc819a1SJeremy L Thompson 
48990fdeb6SJeremy L Thompson     snprintf(name, 30, "'QFunction output %" CeedInt_FMT "'", i);
49edc819a1SJeremy L Thompson     mem_block_ids[i] = VALGRIND_CREATE_BLOCK(impl->outputs[i], len, name);
50fc7cf9a0Sjeremylt   }
51fc7cf9a0Sjeremylt 
52*fb02a165SJeremy L Thompson   ierr = f(ctx_data, Q, impl->inputs, impl->outputs); CeedChkBackend(ierr);
53fc7cf9a0Sjeremylt 
54edc819a1SJeremy L Thompson   for (CeedInt i = 0; i<num_in; i++) {
55e15f9bd0SJeremy L Thompson     ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChkBackend(ierr);
56fc7cf9a0Sjeremylt   }
57edc819a1SJeremy L Thompson   for (CeedInt i = 0; i<num_out; i++) {
58e15f9bd0SJeremy L Thompson     ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChkBackend(ierr);
59edc819a1SJeremy L Thompson     VALGRIND_DISCARD(mem_block_ids[i]);
60fc7cf9a0Sjeremylt   }
61*fb02a165SJeremy L Thompson   ierr = CeedQFunctionRestoreContextData(qf, &ctx_data); CeedChkBackend(ierr);
62fc7cf9a0Sjeremylt 
63e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
64fc7cf9a0Sjeremylt }
65fc7cf9a0Sjeremylt 
66f10650afSjeremylt //------------------------------------------------------------------------------
67f10650afSjeremylt // QFunction Destroy
68f10650afSjeremylt //------------------------------------------------------------------------------
69fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) {
70fc7cf9a0Sjeremylt   int ierr;
71fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
72e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, (void *)&impl); CeedChkBackend(ierr);
73fc7cf9a0Sjeremylt 
74e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->inputs); CeedChkBackend(ierr);
75e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl->outputs); CeedChkBackend(ierr);
76e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
77fc7cf9a0Sjeremylt 
78e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
79fc7cf9a0Sjeremylt }
80fc7cf9a0Sjeremylt 
81f10650afSjeremylt //------------------------------------------------------------------------------
82f10650afSjeremylt // QFunction Create
83f10650afSjeremylt //------------------------------------------------------------------------------
84fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) {
85fc7cf9a0Sjeremylt   int ierr;
86fc7cf9a0Sjeremylt   Ceed ceed;
87e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr);
88fc7cf9a0Sjeremylt 
89fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
90e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
91bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &impl->inputs); CeedChkBackend(ierr);
92bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &impl->outputs); CeedChkBackend(ierr);
93e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, impl); CeedChkBackend(ierr);
94fc7cf9a0Sjeremylt 
95fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
96e15f9bd0SJeremy L Thompson                                 CeedQFunctionApply_Memcheck); CeedChkBackend(ierr);
97fc7cf9a0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
98e15f9bd0SJeremy L Thompson                                 CeedQFunctionDestroy_Memcheck); CeedChkBackend(ierr);
99fc7cf9a0Sjeremylt 
100e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
101fc7cf9a0Sjeremylt }
102f10650afSjeremylt //------------------------------------------------------------------------------
103