xref: /libCEED/rust/libceed-sys/c-src/backends/memcheck/ceed-memcheck-qfunction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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 
8*49aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
10*49aac155SJeremy L Thompson #include <stdio.h>
113d576824SJeremy L Thompson #include <valgrind/memcheck.h>
122b730f8bSJeremy L Thompson 
13fc7cf9a0Sjeremylt #include "ceed-memcheck.h"
14fc7cf9a0Sjeremylt 
15f10650afSjeremylt //------------------------------------------------------------------------------
16f10650afSjeremylt // QFunction Apply
17f10650afSjeremylt //------------------------------------------------------------------------------
182b730f8bSJeremy L Thompson static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
19fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
202b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
21fc7cf9a0Sjeremylt 
22fb02a165SJeremy L Thompson   void *ctx_data = NULL;
232b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data));
24fc7cf9a0Sjeremylt 
25692c2638Sjeremylt   CeedQFunctionUser f = NULL;
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f));
27fc7cf9a0Sjeremylt 
28d1d35e2fSjeremylt   CeedInt num_in, num_out;
292b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out));
30fc7cf9a0Sjeremylt 
31edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
322b730f8bSJeremy 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 
392b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]));
40edc819a1SJeremy L Thompson 
412b730f8bSJeremy 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 
482b730f8bSJeremy L Thompson   CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs));
49fc7cf9a0Sjeremylt 
50edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
512b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]));
52fc7cf9a0Sjeremylt   }
53edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
542b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i]));
55edc819a1SJeremy L Thompson     VALGRIND_DISCARD(mem_block_ids[i]);
56fc7cf9a0Sjeremylt   }
572b730f8bSJeremy 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;
672b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, (void *)&impl));
68fc7cf9a0Sjeremylt 
692b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->inputs));
702b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->outputs));
712b730f8bSJeremy 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;
812b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
82fc7cf9a0Sjeremylt 
83fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
842b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
852b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs));
862b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs));
872b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionSetData(qf, impl));
88fc7cf9a0Sjeremylt 
892b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Memcheck));
902b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Memcheck));
91fc7cf9a0Sjeremylt 
92e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93fc7cf9a0Sjeremylt }
94f10650afSjeremylt //------------------------------------------------------------------------------
95