xref: /libCEED/backends/memcheck/ceed-memcheck-qfunction.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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/backend.h>
9*2b730f8bSJeremy L Thompson #include <ceed/ceed.h>
10edc819a1SJeremy L Thompson #include <string.h>
113d576824SJeremy L Thompson #include <valgrind/memcheck.h>
12*2b730f8bSJeremy L Thompson 
13fc7cf9a0Sjeremylt #include "ceed-memcheck.h"
14fc7cf9a0Sjeremylt 
15f10650afSjeremylt //------------------------------------------------------------------------------
16f10650afSjeremylt // QFunction Apply
17f10650afSjeremylt //------------------------------------------------------------------------------
18*2b730f8bSJeremy L Thompson static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
19fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
20*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
21fc7cf9a0Sjeremylt 
22fb02a165SJeremy L Thompson   void *ctx_data = NULL;
23*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data));
24fc7cf9a0Sjeremylt 
25692c2638Sjeremylt   CeedQFunctionUser f = NULL;
26*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f));
27fc7cf9a0Sjeremylt 
28d1d35e2fSjeremylt   CeedInt num_in, num_out;
29*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out));
30fc7cf9a0Sjeremylt 
31edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
32*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]));
33fc7cf9a0Sjeremylt   }
34edc819a1SJeremy L Thompson   int mem_block_ids[num_out];
35edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
36edc819a1SJeremy L Thompson     CeedSize len;
37edc819a1SJeremy L Thompson     char     name[30] = "";
38edc819a1SJeremy L Thompson 
39*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]));
40edc819a1SJeremy L Thompson 
41*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(V[i], &len));
42fc7cf9a0Sjeremylt     VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len);
43edc819a1SJeremy L Thompson 
44990fdeb6SJeremy L Thompson     snprintf(name, 30, "'QFunction output %" CeedInt_FMT "'", i);
45edc819a1SJeremy L Thompson     mem_block_ids[i] = VALGRIND_CREATE_BLOCK(impl->outputs[i], len, name);
46fc7cf9a0Sjeremylt   }
47fc7cf9a0Sjeremylt 
48*2b730f8bSJeremy L Thompson   CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs));
49fc7cf9a0Sjeremylt 
50edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
51*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]));
52fc7cf9a0Sjeremylt   }
53edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
54*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i]));
55edc819a1SJeremy L Thompson     VALGRIND_DISCARD(mem_block_ids[i]);
56fc7cf9a0Sjeremylt   }
57*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data));
58fc7cf9a0Sjeremylt 
59e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
60fc7cf9a0Sjeremylt }
61fc7cf9a0Sjeremylt 
62f10650afSjeremylt //------------------------------------------------------------------------------
63f10650afSjeremylt // QFunction Destroy
64f10650afSjeremylt //------------------------------------------------------------------------------
65fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) {
66fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
67*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, (void *)&impl));
68fc7cf9a0Sjeremylt 
69*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->inputs));
70*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->outputs));
71*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
72fc7cf9a0Sjeremylt 
73e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
74fc7cf9a0Sjeremylt }
75fc7cf9a0Sjeremylt 
76f10650afSjeremylt //------------------------------------------------------------------------------
77f10650afSjeremylt // QFunction Create
78f10650afSjeremylt //------------------------------------------------------------------------------
79fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) {
80fc7cf9a0Sjeremylt   Ceed ceed;
81*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
82fc7cf9a0Sjeremylt 
83fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
84*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
85*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs));
86*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs));
87*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionSetData(qf, impl));
88fc7cf9a0Sjeremylt 
89*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Memcheck));
90*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Memcheck));
91fc7cf9a0Sjeremylt 
92e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93fc7cf9a0Sjeremylt }
94f10650afSjeremylt //------------------------------------------------------------------------------
95