19798701eSJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 29798701eSJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 39798701eSJeremy L Thompson // 49798701eSJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 59798701eSJeremy L Thompson // 69798701eSJeremy L Thompson // This file is part of CEED: http://github.com/ceed 79798701eSJeremy L Thompson 849aac155SJeremy L Thompson #include <ceed.h> 99798701eSJeremy L Thompson #include <ceed/backend.h> 109798701eSJeremy L Thompson #include <math.h> 1149aac155SJeremy L Thompson #include <stdbool.h> 129798701eSJeremy L Thompson #include <string.h> 139798701eSJeremy L Thompson #include <valgrind/memcheck.h> 149798701eSJeremy L Thompson 159798701eSJeremy L Thompson #include "ceed-memcheck.h" 169798701eSJeremy L Thompson 179798701eSJeremy L Thompson //------------------------------------------------------------------------------ 189798701eSJeremy L Thompson // Has Valid Array 199798701eSJeremy L Thompson //------------------------------------------------------------------------------ 209798701eSJeremy L Thompson static int CeedVectorHasValidArray_Memcheck(CeedVector vec, bool *has_valid_array) { 219798701eSJeremy L Thompson CeedVector_Memcheck *impl; 22ad70ee2cSJeremy L Thompson 239798701eSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 241c66c397SJeremy L Thompson *has_valid_array = impl->array; 259798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 269798701eSJeremy L Thompson } 279798701eSJeremy L Thompson 289798701eSJeremy L Thompson //------------------------------------------------------------------------------ 299798701eSJeremy L Thompson // Check if has borrowed array of given type 309798701eSJeremy L Thompson //------------------------------------------------------------------------------ 319798701eSJeremy L Thompson static inline int CeedVectorHasBorrowedArrayOfType_Memcheck(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 32ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 33ad70ee2cSJeremy L Thompson 34ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 35*6e536b99SJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 361c66c397SJeremy L Thompson *has_borrowed_array_of_type = impl->array_borrowed; 379798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 389798701eSJeremy L Thompson } 399798701eSJeremy L Thompson 409798701eSJeremy L Thompson //------------------------------------------------------------------------------ 419798701eSJeremy L Thompson // Vector Set Array 429798701eSJeremy L Thompson //------------------------------------------------------------------------------ 439798701eSJeremy L Thompson static int CeedVectorSetArray_Memcheck(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 44ad70ee2cSJeremy L Thompson CeedSize length; 45ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 46ad70ee2cSJeremy L Thompson 47ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 48ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 499798701eSJeremy L Thompson 50*6e536b99SJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 519798701eSJeremy L Thompson 529798701eSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_allocated)); 539798701eSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 549798701eSJeremy L Thompson switch (copy_mode) { 559798701eSJeremy L Thompson case CEED_COPY_VALUES: 569798701eSJeremy L Thompson CeedCallBackend(CeedCalloc(length, &impl->array_owned)); 579798701eSJeremy L Thompson impl->array_borrowed = NULL; 589798701eSJeremy L Thompson impl->array = impl->array_owned; 599798701eSJeremy L Thompson if (array) { 609798701eSJeremy L Thompson memcpy(impl->array, array, length * sizeof(array[0])); 619798701eSJeremy L Thompson } else { 629798701eSJeremy L Thompson for (CeedInt i = 0; i < length; i++) impl->array[i] = NAN; 639798701eSJeremy L Thompson } 649798701eSJeremy L Thompson break; 659798701eSJeremy L Thompson case CEED_OWN_POINTER: 669798701eSJeremy L Thompson impl->array_owned = array; 679798701eSJeremy L Thompson impl->array_borrowed = NULL; 689798701eSJeremy L Thompson impl->array = array; 699798701eSJeremy L Thompson break; 709798701eSJeremy L Thompson case CEED_USE_POINTER: 719798701eSJeremy L Thompson impl->array_borrowed = array; 729798701eSJeremy L Thompson impl->array = array; 739798701eSJeremy L Thompson } 749798701eSJeremy L Thompson // Copy data to check access 759798701eSJeremy L Thompson CeedCallBackend(CeedCalloc(length, &impl->array_allocated)); 769798701eSJeremy L Thompson memcpy(impl->array_allocated, impl->array, length * sizeof(array[0])); 779798701eSJeremy L Thompson impl->array = impl->array_allocated; 789798701eSJeremy L Thompson VALGRIND_DISCARD(impl->mem_block_id); 799798701eSJeremy L Thompson impl->mem_block_id = VALGRIND_CREATE_BLOCK(impl->array, length * sizeof(array[0]), "'Vector backend array data copy'"); 809798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 819798701eSJeremy L Thompson } 829798701eSJeremy L Thompson 839798701eSJeremy L Thompson //------------------------------------------------------------------------------ 849798701eSJeremy L Thompson // Vector Take Array 859798701eSJeremy L Thompson //------------------------------------------------------------------------------ 869798701eSJeremy L Thompson static int CeedVectorTakeArray_Memcheck(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 87ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 88ad70ee2cSJeremy L Thompson 89ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 909798701eSJeremy L Thompson 91*6e536b99SJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 929798701eSJeremy L Thompson 939798701eSJeremy L Thompson (*array) = impl->array_borrowed; 949798701eSJeremy L Thompson impl->array_borrowed = NULL; 959798701eSJeremy L Thompson impl->array = NULL; 969798701eSJeremy L Thompson VALGRIND_DISCARD(impl->mem_block_id); 979798701eSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_allocated)); 989798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 999798701eSJeremy L Thompson } 1009798701eSJeremy L Thompson 1019798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1029798701eSJeremy L Thompson // Vector Get Array 1039798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1049798701eSJeremy L Thompson static int CeedVectorGetArray_Memcheck(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 105ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 106ad70ee2cSJeremy L Thompson 107ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1089798701eSJeremy L Thompson 109*6e536b99SJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 1109798701eSJeremy L Thompson 1119798701eSJeremy L Thompson *array = impl->array; 1129798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 1139798701eSJeremy L Thompson } 1149798701eSJeremy L Thompson 1159798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1169798701eSJeremy L Thompson // Vector Get Array Read 1179798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1189798701eSJeremy L Thompson static int CeedVectorGetArrayRead_Memcheck(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 119ad70ee2cSJeremy L Thompson CeedSize length; 120ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 121ad70ee2cSJeremy L Thompson 122ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 123ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 1249798701eSJeremy L Thompson 1259798701eSJeremy L Thompson CeedCallBackend(CeedVectorGetArray_Memcheck(vec, mem_type, (CeedScalar **)array)); 1269798701eSJeremy L Thompson 1279798701eSJeremy L Thompson // Make copy to verify no write occurred 1285b5dc0c8SJeremy L Thompson if (!impl->array_read_only_copy) { 1299798701eSJeremy L Thompson CeedCallBackend(CeedCalloc(length, &impl->array_read_only_copy)); 1309798701eSJeremy L Thompson memcpy(impl->array_read_only_copy, *array, length * sizeof((*array)[0])); 1315b5dc0c8SJeremy L Thompson } 1329798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 1339798701eSJeremy L Thompson } 1349798701eSJeremy L Thompson 1359798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1369798701eSJeremy L Thompson // Vector Get Array Write 1379798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1389798701eSJeremy L Thompson static int CeedVectorGetArrayWrite_Memcheck(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 139ad70ee2cSJeremy L Thompson CeedSize length; 140ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 141ad70ee2cSJeremy L Thompson 142ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 143ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 1449798701eSJeremy L Thompson 1459798701eSJeremy L Thompson // Invalidate data to make sure no read occurs 1469798701eSJeremy L Thompson if (!impl->array) CeedCallBackend(CeedVectorSetArray_Memcheck(vec, mem_type, CEED_COPY_VALUES, NULL)); 1479798701eSJeremy L Thompson CeedCallBackend(CeedVectorGetArray_Memcheck(vec, mem_type, array)); 148dd39767dSJeremy L Thompson for (CeedSize i = 0; i < length; i++) (*array)[i] = NAN; 149df852985SJeremy L Thompson impl->is_write_only_access = true; 1509798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 1519798701eSJeremy L Thompson } 1529798701eSJeremy L Thompson 1539798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1549798701eSJeremy L Thompson // Vector Restore Array 1559798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1569798701eSJeremy L Thompson static int CeedVectorRestoreArray_Memcheck(CeedVector vec) { 1579798701eSJeremy L Thompson Ceed ceed; 158ad70ee2cSJeremy L Thompson CeedSize length; 159ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 160ad70ee2cSJeremy L Thompson 161ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 162ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 1639798701eSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 1649798701eSJeremy L Thompson 165df852985SJeremy L Thompson if (impl->is_write_only_access) { 166dd39767dSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 167249f8407SJeremy L Thompson if (isnan(impl->array[i])) 168249f8407SJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_WARNING, "WARNING: Vec entry %" CeedSize_FMT " is NaN after restoring write-only access", i); 169df852985SJeremy L Thompson } 170df852985SJeremy L Thompson impl->is_write_only_access = false; 171df852985SJeremy L Thompson } 1729798701eSJeremy L Thompson if (impl->array_borrowed) { 1739798701eSJeremy L Thompson memcpy(impl->array_borrowed, impl->array, length * sizeof(impl->array[0])); 1749798701eSJeremy L Thompson } 1759798701eSJeremy L Thompson if (impl->array_owned) { 1769798701eSJeremy L Thompson memcpy(impl->array_owned, impl->array, length * sizeof(impl->array[0])); 1779798701eSJeremy L Thompson } 1789798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 1799798701eSJeremy L Thompson } 1809798701eSJeremy L Thompson 1819798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1829798701eSJeremy L Thompson // Vector Restore Array Read-Only 1839798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1849798701eSJeremy L Thompson static int CeedVectorRestoreArrayRead_Memcheck(CeedVector vec) { 185ad70ee2cSJeremy L Thompson CeedSize length; 186ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 187ad70ee2cSJeremy L Thompson 188ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 189ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 1909798701eSJeremy L Thompson 191*6e536b99SJeremy L Thompson CeedCheck(!memcmp(impl->array, impl->array_read_only_copy, length * sizeof(impl->array[0])), CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 1926574a04fSJeremy L Thompson "Array data changed while accessed in read-only mode"); 1939798701eSJeremy L Thompson 1949798701eSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_read_only_copy)); 1959798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 1969798701eSJeremy L Thompson } 1979798701eSJeremy L Thompson 1989798701eSJeremy L Thompson //------------------------------------------------------------------------------ 1999798701eSJeremy L Thompson // Vector Destroy 2009798701eSJeremy L Thompson //------------------------------------------------------------------------------ 2019798701eSJeremy L Thompson static int CeedVectorDestroy_Memcheck(CeedVector vec) { 2029798701eSJeremy L Thompson CeedVector_Memcheck *impl; 2039798701eSJeremy L Thompson 204ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 2059798701eSJeremy L Thompson VALGRIND_DISCARD(impl->mem_block_id); 2069798701eSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_allocated)); 2079798701eSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 2089798701eSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 2099798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 2109798701eSJeremy L Thompson } 2119798701eSJeremy L Thompson 2129798701eSJeremy L Thompson //------------------------------------------------------------------------------ 2139798701eSJeremy L Thompson // Vector Create 2149798701eSJeremy L Thompson //------------------------------------------------------------------------------ 2159798701eSJeremy L Thompson int CeedVectorCreate_Memcheck(CeedSize n, CeedVector vec) { 2169798701eSJeremy L Thompson Ceed ceed; 217ad70ee2cSJeremy L Thompson CeedVector_Memcheck *impl; 2189798701eSJeremy L Thompson 2199e82028bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 2209e82028bSJeremy L Thompson CeedCallBackend(CeedVectorSetData(vec, impl)); 2219e82028bSJeremy L Thompson 222ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 2239798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Memcheck)); 2249798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Memcheck)); 2259798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Memcheck)); 2269798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Memcheck)); 2279798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Memcheck)); 2289798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Memcheck)); 2299798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Memcheck)); 2309798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray", CeedVectorRestoreArray_Memcheck)); 2319798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead", CeedVectorRestoreArrayRead_Memcheck)); 2329798701eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Memcheck)); 2339798701eSJeremy L Thompson return CEED_ERROR_SUCCESS; 2349798701eSJeremy L Thompson } 2352a86cc9dSSebastian Grimberg 2369798701eSJeremy L Thompson //------------------------------------------------------------------------------ 237