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; 23ad70ee2cSJeremy L Thompson CeedQFunctionUser f = NULL; 24*51d50b59SJeremy L Thompson CeedQFunctionField *output_fields; 25ad70ee2cSJeremy L Thompson CeedQFunction_Memcheck *impl; 26ad70ee2cSJeremy L Thompson 27ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); 28ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &impl)); 29ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data)); 30ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f)); 312b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out)); 32*51d50b59SJeremy L Thompson int mem_block_ids[num_out]; 33fc7cf9a0Sjeremylt 34*51d50b59SJeremy L Thompson // Get input/output arrays 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 for (CeedInt i = 0; i < num_out; i++) { 39edc819a1SJeremy L Thompson CeedSize len; 408e6aa226SJed Brown char name[32] = ""; 41edc819a1SJeremy L Thompson 422b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i])); 43edc819a1SJeremy L Thompson 442b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(V[i], &len)); 45fc7cf9a0Sjeremylt VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len); 46edc819a1SJeremy L Thompson 478e6aa226SJed Brown snprintf(name, 32, "'QFunction output %" CeedInt_FMT "'", i); 48edc819a1SJeremy L Thompson mem_block_ids[i] = VALGRIND_CREATE_BLOCK(impl->outputs[i], len, name); 49fc7cf9a0Sjeremylt } 50fc7cf9a0Sjeremylt 51*51d50b59SJeremy L Thompson // Call user function 522b730f8bSJeremy L Thompson CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs)); 53fc7cf9a0Sjeremylt 54*51d50b59SJeremy L Thompson // Restore input arrays 55edc819a1SJeremy L Thompson for (CeedInt i = 0; i < num_in; i++) { 562b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i])); 57fc7cf9a0Sjeremylt } 58*51d50b59SJeremy L Thompson // Check for unset output values 59*51d50b59SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &output_fields)); 60edc819a1SJeremy L Thompson for (CeedInt i = 0; i < num_out; i++) { 61*51d50b59SJeremy L Thompson CeedInt field_size; 628811b53cSJeremy L Thompson 63*51d50b59SJeremy L Thompson // Note: need field size because vector may be longer than needed for output 64*51d50b59SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(output_fields[i], &field_size)); 65*51d50b59SJeremy L Thompson for (CeedSize j = 0; j < Q * field_size; j++) { 668811b53cSJeremy 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, 678811b53cSJeremy L Thompson j); 688811b53cSJeremy L Thompson } 692b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i])); 70edc819a1SJeremy L Thompson VALGRIND_DISCARD(mem_block_ids[i]); 71fc7cf9a0Sjeremylt } 722b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data)); 73e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 74fc7cf9a0Sjeremylt } 75fc7cf9a0Sjeremylt 76f10650afSjeremylt //------------------------------------------------------------------------------ 77f10650afSjeremylt // QFunction Destroy 78f10650afSjeremylt //------------------------------------------------------------------------------ 79fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) { 80fc7cf9a0Sjeremylt CeedQFunction_Memcheck *impl; 81fc7cf9a0Sjeremylt 82ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, (void *)&impl)); 832b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->inputs)); 842b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->outputs)); 852b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 86e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 87fc7cf9a0Sjeremylt } 88fc7cf9a0Sjeremylt 89f10650afSjeremylt //------------------------------------------------------------------------------ 90f10650afSjeremylt // QFunction Create 91f10650afSjeremylt //------------------------------------------------------------------------------ 92fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) { 93fc7cf9a0Sjeremylt Ceed ceed; 94fc7cf9a0Sjeremylt CeedQFunction_Memcheck *impl; 95ad70ee2cSJeremy L Thompson 96ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); 972b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 982b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs)); 992b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs)); 1002b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionSetData(qf, impl)); 1012b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Memcheck)); 1022b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Memcheck)); 103e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 104fc7cf9a0Sjeremylt } 1052a86cc9dSSebastian Grimberg 106f10650afSjeremylt //------------------------------------------------------------------------------ 107