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; 2451d50b59SJeremy 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)); 3251d50b59SJeremy L Thompson int mem_block_ids[num_out]; 33fc7cf9a0Sjeremylt 3451d50b59SJeremy 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 5151d50b59SJeremy L Thompson // Call user function 522b730f8bSJeremy L Thompson CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs)); 53fc7cf9a0Sjeremylt 5451d50b59SJeremy 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 } 5851d50b59SJeremy L Thompson // Check for unset output values 59739ab3b4SJames Wright { 60*34ffed21SJeremy L Thompson const char *kernel_name, *kernel_path; 61739ab3b4SJames Wright 62739ab3b4SJames Wright CeedCallBackend(CeedQFunctionGetSourcePath(qf, &kernel_path)); 63739ab3b4SJames Wright CeedCallBackend(CeedQFunctionGetKernelName(qf, &kernel_name)); 6451d50b59SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &output_fields)); 65edc819a1SJeremy L Thompson for (CeedInt i = 0; i < num_out; i++) { 6651d50b59SJeremy L Thompson CeedInt field_size; 678811b53cSJeremy L Thompson 6851d50b59SJeremy L Thompson // Note: need field size because vector may be longer than needed for output 6951d50b59SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(output_fields[i], &field_size)); 70249f8407SJeremy L Thompson for (CeedSize j = 0; j < field_size * (CeedSize)Q; j++) { 71739ab3b4SJames Wright CeedCheck(!isnan(impl->outputs[i][j]), ceed, CEED_ERROR_BACKEND, 72249f8407SJeremy L Thompson "QFunction output %" CeedInt_FMT " entry %" CeedSize_FMT " is NaN after restoring write-only access: %s:%s ", i, j, kernel_path, 73249f8407SJeremy L Thompson kernel_name); 748811b53cSJeremy L Thompson } 752b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i])); 76edc819a1SJeremy L Thompson VALGRIND_DISCARD(mem_block_ids[i]); 77fc7cf9a0Sjeremylt } 78739ab3b4SJames Wright } 792b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data)); 80e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 81fc7cf9a0Sjeremylt } 82fc7cf9a0Sjeremylt 83f10650afSjeremylt //------------------------------------------------------------------------------ 84f10650afSjeremylt // QFunction Destroy 85f10650afSjeremylt //------------------------------------------------------------------------------ 86fc7cf9a0Sjeremylt static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) { 87fc7cf9a0Sjeremylt CeedQFunction_Memcheck *impl; 88fc7cf9a0Sjeremylt 89ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, (void *)&impl)); 902b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->inputs)); 912b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->outputs)); 922b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 93e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 94fc7cf9a0Sjeremylt } 95fc7cf9a0Sjeremylt 96f10650afSjeremylt //------------------------------------------------------------------------------ 97f10650afSjeremylt // QFunction Create 98f10650afSjeremylt //------------------------------------------------------------------------------ 99fc7cf9a0Sjeremylt int CeedQFunctionCreate_Memcheck(CeedQFunction qf) { 100fc7cf9a0Sjeremylt Ceed ceed; 101fc7cf9a0Sjeremylt CeedQFunction_Memcheck *impl; 102ad70ee2cSJeremy L Thompson 103ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); 1042b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 1052b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs)); 1062b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs)); 1072b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionSetData(qf, impl)); 1082b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Memcheck)); 1092b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Memcheck)); 110e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 111fc7cf9a0Sjeremylt } 1122a86cc9dSSebastian Grimberg 113f10650afSjeremylt //------------------------------------------------------------------------------ 114