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. 30436c2adSjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 50436c2adSjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 70436c2adSjeremylt 8*49aac155SJeremy L Thompson #include <ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 10*49aac155SJeremy L Thompson #include <stdbool.h> 113d576824SJeremy L Thompson #include <string.h> 122b730f8bSJeremy L Thompson 130436c2adSjeremylt #include "ceed-ref.h" 140436c2adSjeremylt 15f10650afSjeremylt //------------------------------------------------------------------------------ 169c774eddSJeremy L Thompson // Has Valid Array 179c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 189c774eddSJeremy L Thompson static int CeedVectorHasValidArray_Ref(CeedVector vec, bool *has_valid_array) { 199c774eddSJeremy L Thompson CeedVector_Ref *impl; 202b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 219c774eddSJeremy L Thompson 229c774eddSJeremy L Thompson *has_valid_array = !!impl->array; 239c774eddSJeremy L Thompson 249c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 259c774eddSJeremy L Thompson } 269c774eddSJeremy L Thompson 279c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 289c774eddSJeremy L Thompson // Check if has borrowed array of given type 299c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 302b730f8bSJeremy L Thompson static inline int CeedVectorHasBorrowedArrayOfType_Ref(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 319c774eddSJeremy L Thompson CeedVector_Ref *impl; 322b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 339c774eddSJeremy L Thompson Ceed ceed; 342b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 359c774eddSJeremy L Thompson 369c774eddSJeremy L Thompson switch (mem_type) { 379c774eddSJeremy L Thompson case CEED_MEM_HOST: 389c774eddSJeremy L Thompson *has_borrowed_array_of_type = !!impl->array_borrowed; 399c774eddSJeremy L Thompson break; 409c774eddSJeremy L Thompson default: 419c774eddSJeremy L Thompson // LCOV_EXCL_START 422b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 439c774eddSJeremy L Thompson // LCOV_EXCL_STOP 449c774eddSJeremy L Thompson break; 459c774eddSJeremy L Thompson } 469c774eddSJeremy L Thompson 479c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 489c774eddSJeremy L Thompson } 499c774eddSJeremy L Thompson 509c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 51f10650afSjeremylt // Vector Set Array 52f10650afSjeremylt //------------------------------------------------------------------------------ 532b730f8bSJeremy L Thompson static int CeedVectorSetArray_Ref(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 540436c2adSjeremylt CeedVector_Ref *impl; 552b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 561f9221feSJeremy L Thompson CeedSize length; 572b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 580436c2adSjeremylt Ceed ceed; 592b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 600436c2adSjeremylt 612b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 620436c2adSjeremylt // LCOV_EXCL_START 632b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 640436c2adSjeremylt // LCOV_EXCL_STOP 652b730f8bSJeremy L Thompson } 669c774eddSJeremy L Thompson 67d1d35e2fSjeremylt switch (copy_mode) { 680436c2adSjeremylt case CEED_COPY_VALUES: 699c774eddSJeremy L Thompson if (!impl->array_owned) { 702b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(length, &impl->array_owned)); 719c774eddSJeremy L Thompson } 729c774eddSJeremy L Thompson impl->array_borrowed = NULL; 739c774eddSJeremy L Thompson impl->array = impl->array_owned; 742b730f8bSJeremy L Thompson if (array) memcpy(impl->array, array, length * sizeof(array[0])); 750436c2adSjeremylt break; 760436c2adSjeremylt case CEED_OWN_POINTER: 772b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 789c774eddSJeremy L Thompson impl->array_owned = array; 799c774eddSJeremy L Thompson impl->array_borrowed = NULL; 800436c2adSjeremylt impl->array = array; 810436c2adSjeremylt break; 820436c2adSjeremylt case CEED_USE_POINTER: 832b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 849c774eddSJeremy L Thompson impl->array_borrowed = array; 850436c2adSjeremylt impl->array = array; 860436c2adSjeremylt } 87e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 880436c2adSjeremylt } 890436c2adSjeremylt 90f10650afSjeremylt //------------------------------------------------------------------------------ 916a6c615bSJeremy L Thompson // Vector Take Array 926a6c615bSJeremy L Thompson //------------------------------------------------------------------------------ 932b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 946a6c615bSJeremy L Thompson CeedVector_Ref *impl; 952b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 966a6c615bSJeremy L Thompson Ceed ceed; 972b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 986a6c615bSJeremy L Thompson 999798701eSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 1009798701eSJeremy L Thompson // LCOV_EXCL_START 1019798701eSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 1029798701eSJeremy L Thompson // LCOV_EXCL_STOP 1039798701eSJeremy L Thompson } 1049798701eSJeremy L Thompson 1059c774eddSJeremy L Thompson (*array) = impl->array_borrowed; 1069c774eddSJeremy L Thompson impl->array_borrowed = NULL; 1076a6c615bSJeremy L Thompson impl->array = NULL; 1086a6c615bSJeremy L Thompson 109e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1106a6c615bSJeremy L Thompson } 1116a6c615bSJeremy L Thompson 1126a6c615bSJeremy L Thompson //------------------------------------------------------------------------------ 113f10650afSjeremylt // Vector Get Array 114f10650afSjeremylt //------------------------------------------------------------------------------ 1152b730f8bSJeremy L Thompson static int CeedVectorGetArrayCore_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 1160436c2adSjeremylt CeedVector_Ref *impl; 1172b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1180436c2adSjeremylt Ceed ceed; 1192b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 1200436c2adSjeremylt 1212b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 1220436c2adSjeremylt // LCOV_EXCL_START 1232b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 1240436c2adSjeremylt // LCOV_EXCL_STOP 1252b730f8bSJeremy L Thompson } 1269c774eddSJeremy L Thompson 1270436c2adSjeremylt *array = impl->array; 1289c774eddSJeremy L Thompson 129e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1300436c2adSjeremylt } 1310436c2adSjeremylt 132f10650afSjeremylt //------------------------------------------------------------------------------ 133f10650afSjeremylt // Vector Get Array Read 134f10650afSjeremylt //------------------------------------------------------------------------------ 1352b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 1369c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array); 1379c774eddSJeremy L Thompson } 1389c774eddSJeremy L Thompson 1399c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1409c774eddSJeremy L Thompson // Vector Get Array 1419c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1422b730f8bSJeremy L Thompson static int CeedVectorGetArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 1439c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, array); 1449c774eddSJeremy L Thompson } 1459c774eddSJeremy L Thompson 1469c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1479c774eddSJeremy L Thompson // Vector Get Array Write 1489c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1492b730f8bSJeremy L Thompson static int CeedVectorGetArrayWrite_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 1500436c2adSjeremylt CeedVector_Ref *impl; 1512b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1520436c2adSjeremylt 1539c774eddSJeremy L Thompson if (!impl->array) { 1549c774eddSJeremy L Thompson if (!impl->array_owned && !impl->array_borrowed) { 1559c774eddSJeremy L Thompson // Allocate if array is not yet allocated 1562b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL)); 1579c774eddSJeremy L Thompson } else { 1589c774eddSJeremy L Thompson // Select dirty array for GetArrayWrite 1592b730f8bSJeremy L Thompson if (impl->array_borrowed) impl->array = impl->array_borrowed; 1602b730f8bSJeremy L Thompson else impl->array = impl->array_owned; 1610436c2adSjeremylt } 1629c774eddSJeremy L Thompson } 1639c774eddSJeremy L Thompson 1649c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array); 1650436c2adSjeremylt } 1660436c2adSjeremylt 167f10650afSjeremylt //------------------------------------------------------------------------------ 168f10650afSjeremylt // Vector Restore Array 169f10650afSjeremylt //------------------------------------------------------------------------------ 1702b730f8bSJeremy L Thompson static int CeedVectorRestoreArray_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; } 1710436c2adSjeremylt 1722b730f8bSJeremy L Thompson static int CeedVectorRestoreArrayRead_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; } 1730436c2adSjeremylt 174f10650afSjeremylt //------------------------------------------------------------------------------ 175f10650afSjeremylt // Vector Destroy 176f10650afSjeremylt //------------------------------------------------------------------------------ 1770436c2adSjeremylt static int CeedVectorDestroy_Ref(CeedVector vec) { 1780436c2adSjeremylt CeedVector_Ref *impl; 1792b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1800436c2adSjeremylt 1812b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 1822b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 183e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1840436c2adSjeremylt } 1850436c2adSjeremylt 186f10650afSjeremylt //------------------------------------------------------------------------------ 187f10650afSjeremylt // Vector Create 188f10650afSjeremylt //------------------------------------------------------------------------------ 1891f9221feSJeremy L Thompson int CeedVectorCreate_Ref(CeedSize n, CeedVector vec) { 1900436c2adSjeremylt CeedVector_Ref *impl; 1910436c2adSjeremylt Ceed ceed; 1922b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 1930436c2adSjeremylt 1942b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Ref)); 1952b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Ref)); 1962b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Ref)); 1972b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Ref)); 1982b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Ref)); 1992b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Ref)); 2002b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Ref)); 2012b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray", CeedVectorRestoreArray_Ref)); 2022b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead", CeedVectorRestoreArrayRead_Ref)); 2032b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Ref)); 2049c774eddSJeremy L Thompson 2052b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 2062b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetData(vec, impl)); 2079c774eddSJeremy L Thompson 208e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2090436c2adSjeremylt } 210f10650afSjeremylt //------------------------------------------------------------------------------ 211