1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 #include <ceed.h> 9 #include <ceed/backend.h> 10 #include <stdio.h> 11 #include <valgrind/memcheck.h> 12 13 #include "ceed-memcheck.h" 14 15 //------------------------------------------------------------------------------ 16 // QFunction Apply 17 //------------------------------------------------------------------------------ 18 static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) { 19 CeedQFunction_Memcheck *impl; 20 CeedCallBackend(CeedQFunctionGetData(qf, &impl)); 21 22 void *ctx_data = NULL; 23 CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data)); 24 25 CeedQFunctionUser f = NULL; 26 CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f)); 27 28 CeedInt num_in, num_out; 29 CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out)); 30 31 for (CeedInt i = 0; i < num_in; i++) { 32 CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i])); 33 } 34 int mem_block_ids[num_out]; 35 for (CeedInt i = 0; i < num_out; i++) { 36 CeedSize len; 37 char name[30] = ""; 38 39 CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i])); 40 41 CeedCallBackend(CeedVectorGetLength(V[i], &len)); 42 VALGRIND_MAKE_MEM_UNDEFINED(impl->outputs[i], len); 43 44 snprintf(name, 30, "'QFunction output %" CeedInt_FMT "'", i); 45 mem_block_ids[i] = VALGRIND_CREATE_BLOCK(impl->outputs[i], len, name); 46 } 47 48 CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs)); 49 50 for (CeedInt i = 0; i < num_in; i++) { 51 CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i])); 52 } 53 for (CeedInt i = 0; i < num_out; i++) { 54 CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i])); 55 VALGRIND_DISCARD(mem_block_ids[i]); 56 } 57 CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data)); 58 59 return CEED_ERROR_SUCCESS; 60 } 61 62 //------------------------------------------------------------------------------ 63 // QFunction Destroy 64 //------------------------------------------------------------------------------ 65 static int CeedQFunctionDestroy_Memcheck(CeedQFunction qf) { 66 CeedQFunction_Memcheck *impl; 67 CeedCallBackend(CeedQFunctionGetData(qf, (void *)&impl)); 68 69 CeedCallBackend(CeedFree(&impl->inputs)); 70 CeedCallBackend(CeedFree(&impl->outputs)); 71 CeedCallBackend(CeedFree(&impl)); 72 73 return CEED_ERROR_SUCCESS; 74 } 75 76 //------------------------------------------------------------------------------ 77 // QFunction Create 78 //------------------------------------------------------------------------------ 79 int CeedQFunctionCreate_Memcheck(CeedQFunction qf) { 80 Ceed ceed; 81 CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); 82 83 CeedQFunction_Memcheck *impl; 84 CeedCallBackend(CeedCalloc(1, &impl)); 85 CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs)); 86 CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs)); 87 CeedCallBackend(CeedQFunctionSetData(qf, impl)); 88 89 CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Memcheck)); 90 CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Memcheck)); 91 92 return CEED_ERROR_SUCCESS; 93 } 94 //------------------------------------------------------------------------------ 95