xref: /libCEED/backends/memcheck/ceed-memcheck-qfunction.c (revision 8811b53cd473ecf5ce58148a98eca973009d33b8)
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 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
10*8811b53cSJeremy L Thompson #include <math.h>
1149aac155SJeremy L Thompson #include <stdio.h>
123d576824SJeremy L Thompson #include <valgrind/memcheck.h>
132b730f8bSJeremy L Thompson 
14fc7cf9a0Sjeremylt #include "ceed-memcheck.h"
15fc7cf9a0Sjeremylt 
16f10650afSjeremylt //------------------------------------------------------------------------------
17f10650afSjeremylt // QFunction Apply
18f10650afSjeremylt //------------------------------------------------------------------------------
192b730f8bSJeremy L Thompson static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
20*8811b53cSJeremy L Thompson   Ceed ceed;
21*8811b53cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
22*8811b53cSJeremy L Thompson 
23fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
242b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
25fc7cf9a0Sjeremylt 
26fb02a165SJeremy L Thompson   void *ctx_data = NULL;
272b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data));
28fc7cf9a0Sjeremylt 
29692c2638Sjeremylt   CeedQFunctionUser f = NULL;
302b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f));
31fc7cf9a0Sjeremylt 
32d1d35e2fSjeremylt   CeedInt num_in, num_out;
332b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out));
34fc7cf9a0Sjeremylt 
35edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
362b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]));
37fc7cf9a0Sjeremylt   }
38edc819a1SJeremy L Thompson   int mem_block_ids[num_out];
39edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
40edc819a1SJeremy L Thompson     CeedSize len;
418e6aa226SJed Brown     char     name[32] = "";
42edc819a1SJeremy L Thompson 
432b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]));
44edc819a1SJeremy L Thompson 
452b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(V[i], &len));
46fc7cf9a0Sjeremylt     VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len);
47edc819a1SJeremy L Thompson 
488e6aa226SJed Brown     snprintf(name, 32, "'QFunction output %" CeedInt_FMT "'", i);
49edc819a1SJeremy L Thompson     mem_block_ids[i] = VALGRIND_CREATE_BLOCK(impl->outputs[i], len, name);
50fc7cf9a0Sjeremylt   }
51fc7cf9a0Sjeremylt 
522b730f8bSJeremy L Thompson   CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs));
53fc7cf9a0Sjeremylt 
54edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
552b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]));
56fc7cf9a0Sjeremylt   }
57edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
58*8811b53cSJeremy L Thompson     CeedSize length;
59*8811b53cSJeremy L Thompson 
60*8811b53cSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(V[i], &length));
61*8811b53cSJeremy L Thompson     for (CeedSize j = 0; j < length; j++) {
62*8811b53cSJeremy L Thompson       CeedCheck(!isnan(impl->outputs[i][j]), ceed, CEED_ERROR_BACKEND, "QFunction output %d entry %ld is NaN after restoring write-only access", i,
63*8811b53cSJeremy L Thompson                 j);
64*8811b53cSJeremy L Thompson     }
652b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i]));
66edc819a1SJeremy L Thompson     VALGRIND_DISCARD(mem_block_ids[i]);
67fc7cf9a0Sjeremylt   }
682b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data));
69fc7cf9a0Sjeremylt 
70e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
71fc7cf9a0Sjeremylt }
72fc7cf9a0Sjeremylt 
73f10650afSjeremylt //------------------------------------------------------------------------------
74f10650afSjeremylt // QFunction Destroy
75f10650afSjeremylt //------------------------------------------------------------------------------
76fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) {
77fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
782b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, (void *)&impl));
79fc7cf9a0Sjeremylt 
802b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->inputs));
812b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->outputs));
822b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
83fc7cf9a0Sjeremylt 
84e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85fc7cf9a0Sjeremylt }
86fc7cf9a0Sjeremylt 
87f10650afSjeremylt //------------------------------------------------------------------------------
88f10650afSjeremylt // QFunction Create
89f10650afSjeremylt //------------------------------------------------------------------------------
90fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) {
91fc7cf9a0Sjeremylt   Ceed ceed;
922b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
93fc7cf9a0Sjeremylt 
94fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
952b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
962b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs));
972b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs));
982b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionSetData(qf, impl));
99fc7cf9a0Sjeremylt 
1002b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Memcheck));
1012b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Memcheck));
102fc7cf9a0Sjeremylt 
103e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
104fc7cf9a0Sjeremylt }
1052a86cc9dSSebastian Grimberg 
106f10650afSjeremylt //------------------------------------------------------------------------------
107