xref: /libCEED/rust/libceed-sys/c-src/backends/memcheck/ceed-memcheck-qfunction.c (revision ad70ee2c7083010521460a681a248527200af770) !
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>
108811b53cSJeremy 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) {
208811b53cSJeremy L Thompson   Ceed                    ceed;
21fb02a165SJeremy L Thompson   void                   *ctx_data = NULL;
22d1d35e2fSjeremylt   CeedInt                 num_in, num_out;
23*ad70ee2cSJeremy L Thompson   CeedQFunctionUser       f = NULL;
24*ad70ee2cSJeremy L Thompson   CeedQFunction_Memcheck *impl;
25*ad70ee2cSJeremy L Thompson 
26*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
27*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
28*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data));
29*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f));
302b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out));
31fc7cf9a0Sjeremylt 
32edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
332b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]));
34fc7cf9a0Sjeremylt   }
35edc819a1SJeremy L Thompson   int mem_block_ids[num_out];
36*ad70ee2cSJeremy L Thompson 
37edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
38edc819a1SJeremy L Thompson     CeedSize len;
398e6aa226SJed Brown     char     name[32] = "";
40edc819a1SJeremy L Thompson 
412b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]));
42edc819a1SJeremy L Thompson 
432b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(V[i], &len));
44fc7cf9a0Sjeremylt     VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len);
45edc819a1SJeremy L Thompson 
468e6aa226SJed Brown     snprintf(name, 32, "'QFunction output %" CeedInt_FMT "'", i);
47edc819a1SJeremy L Thompson     mem_block_ids[i] = VALGRIND_CREATE_BLOCK(impl->outputs[i], len, name);
48fc7cf9a0Sjeremylt   }
49fc7cf9a0Sjeremylt 
502b730f8bSJeremy L Thompson   CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs));
51fc7cf9a0Sjeremylt 
52edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_in; i++) {
532b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]));
54fc7cf9a0Sjeremylt   }
55edc819a1SJeremy L Thompson   for (CeedInt i = 0; i < num_out; i++) {
568811b53cSJeremy L Thompson     CeedSize length;
578811b53cSJeremy L Thompson 
588811b53cSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(V[i], &length));
598811b53cSJeremy L Thompson     for (CeedSize j = 0; j < length; j++) {
608811b53cSJeremy 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,
618811b53cSJeremy L Thompson                 j);
628811b53cSJeremy L Thompson     }
632b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i]));
64edc819a1SJeremy L Thompson     VALGRIND_DISCARD(mem_block_ids[i]);
65fc7cf9a0Sjeremylt   }
662b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data));
67e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
68fc7cf9a0Sjeremylt }
69fc7cf9a0Sjeremylt 
70f10650afSjeremylt //------------------------------------------------------------------------------
71f10650afSjeremylt // QFunction Destroy
72f10650afSjeremylt //------------------------------------------------------------------------------
73fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) {
74fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
75fc7cf9a0Sjeremylt 
76*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, (void *)&impl));
772b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->inputs));
782b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->outputs));
792b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
80e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81fc7cf9a0Sjeremylt }
82fc7cf9a0Sjeremylt 
83f10650afSjeremylt //------------------------------------------------------------------------------
84f10650afSjeremylt // QFunction Create
85f10650afSjeremylt //------------------------------------------------------------------------------
86fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) {
87fc7cf9a0Sjeremylt   Ceed                    ceed;
88fc7cf9a0Sjeremylt   CeedQFunction_Memcheck *impl;
89*ad70ee2cSJeremy L Thompson 
90*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
912b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
922b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs));
932b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs));
942b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionSetData(qf, impl));
952b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Memcheck));
962b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Memcheck));
97e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98fc7cf9a0Sjeremylt }
992a86cc9dSSebastian Grimberg 
100f10650afSjeremylt //------------------------------------------------------------------------------
101