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 849aac155SJeremy L Thompson #include <ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 1049aac155SJeremy 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; 20ad70ee2cSJeremy L Thompson 212b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 229c774eddSJeremy L Thompson 231c66c397SJeremy L Thompson *has_valid_array = impl->array; 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 Ceed ceed; 32ad70ee2cSJeremy L Thompson CeedVector_Ref *impl; 33ad70ee2cSJeremy L Thompson 34ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 352b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 369c774eddSJeremy L Thompson 379c774eddSJeremy L Thompson switch (mem_type) { 389c774eddSJeremy L Thompson case CEED_MEM_HOST: 391c66c397SJeremy L Thompson *has_borrowed_array_of_type = impl->array_borrowed; 409c774eddSJeremy L Thompson break; 419c774eddSJeremy L Thompson // LCOV_EXCL_START 42b31f666eSJeremy L Thompson default: 432b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 449c774eddSJeremy L Thompson // LCOV_EXCL_STOP 459c774eddSJeremy L Thompson break; 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 Ceed ceed; 55ad70ee2cSJeremy L Thompson CeedSize length; 56ad70ee2cSJeremy L Thompson CeedVector_Ref *impl; 57ad70ee2cSJeremy L Thompson 58ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 59ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 602b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 610436c2adSjeremylt 626574a04fSJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 639c774eddSJeremy L Thompson 64d1d35e2fSjeremylt switch (copy_mode) { 650436c2adSjeremylt case CEED_COPY_VALUES: 669c774eddSJeremy L Thompson if (!impl->array_owned) { 672b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(length, &impl->array_owned)); 689c774eddSJeremy L Thompson } 699c774eddSJeremy L Thompson impl->array_borrowed = NULL; 709c774eddSJeremy L Thompson impl->array = impl->array_owned; 712b730f8bSJeremy L Thompson if (array) memcpy(impl->array, array, length * sizeof(array[0])); 720436c2adSjeremylt break; 730436c2adSjeremylt case CEED_OWN_POINTER: 742b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 759c774eddSJeremy L Thompson impl->array_owned = array; 769c774eddSJeremy L Thompson impl->array_borrowed = NULL; 770436c2adSjeremylt impl->array = array; 780436c2adSjeremylt break; 790436c2adSjeremylt case CEED_USE_POINTER: 802b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 819c774eddSJeremy L Thompson impl->array_borrowed = array; 820436c2adSjeremylt impl->array = array; 830436c2adSjeremylt } 84e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 850436c2adSjeremylt } 860436c2adSjeremylt 87f10650afSjeremylt //------------------------------------------------------------------------------ 886a6c615bSJeremy L Thompson // Vector Take Array 896a6c615bSJeremy L Thompson //------------------------------------------------------------------------------ 902b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 916a6c615bSJeremy L Thompson Ceed ceed; 92ad70ee2cSJeremy L Thompson CeedVector_Ref *impl; 93ad70ee2cSJeremy L Thompson 94ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 952b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 966a6c615bSJeremy L Thompson 976574a04fSJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 989798701eSJeremy L Thompson 999c774eddSJeremy L Thompson (*array) = impl->array_borrowed; 1009c774eddSJeremy L Thompson impl->array_borrowed = NULL; 1016a6c615bSJeremy L Thompson impl->array = NULL; 102e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1036a6c615bSJeremy L Thompson } 1046a6c615bSJeremy L Thompson 1056a6c615bSJeremy L Thompson //------------------------------------------------------------------------------ 106f10650afSjeremylt // Vector Get Array 107f10650afSjeremylt //------------------------------------------------------------------------------ 1082b730f8bSJeremy L Thompson static int CeedVectorGetArrayCore_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 1090436c2adSjeremylt Ceed ceed; 110ad70ee2cSJeremy L Thompson CeedVector_Ref *impl; 111ad70ee2cSJeremy L Thompson 112ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1132b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 1140436c2adSjeremylt 1156574a04fSJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 1169c774eddSJeremy L Thompson 1170436c2adSjeremylt *array = impl->array; 118e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1190436c2adSjeremylt } 1200436c2adSjeremylt 121f10650afSjeremylt //------------------------------------------------------------------------------ 122f10650afSjeremylt // Vector Get Array Read 123f10650afSjeremylt //------------------------------------------------------------------------------ 1242b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 1259c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array); 1269c774eddSJeremy L Thompson } 1279c774eddSJeremy L Thompson 1289c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1299c774eddSJeremy L Thompson // Vector Get Array 1309c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1312b730f8bSJeremy L Thompson static int CeedVectorGetArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 1329c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, array); 1339c774eddSJeremy L Thompson } 1349c774eddSJeremy L Thompson 1359c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1369c774eddSJeremy L Thompson // Vector Get Array Write 1379c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 138*0836ce8dSSebastian Grimberg static int CeedVectorGetArrayWrite_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 1390436c2adSjeremylt CeedVector_Ref *impl; 140ad70ee2cSJeremy L Thompson 1412b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1420436c2adSjeremylt 1439c774eddSJeremy L Thompson if (!impl->array) { 1449c774eddSJeremy L Thompson if (!impl->array_owned && !impl->array_borrowed) { 1459c774eddSJeremy L Thompson // Allocate if array is not yet allocated 1462b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL)); 1479c774eddSJeremy L Thompson } else { 1489c774eddSJeremy L Thompson // Select dirty array for GetArrayWrite 1492b730f8bSJeremy L Thompson if (impl->array_borrowed) impl->array = impl->array_borrowed; 1502b730f8bSJeremy L Thompson else impl->array = impl->array_owned; 1510436c2adSjeremylt } 1529c774eddSJeremy L Thompson } 1539c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array); 1540436c2adSjeremylt } 1550436c2adSjeremylt 156f10650afSjeremylt //------------------------------------------------------------------------------ 157f10650afSjeremylt // Vector Restore Array 158f10650afSjeremylt //------------------------------------------------------------------------------ 1592b730f8bSJeremy L Thompson static int CeedVectorRestoreArray_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; } 1600436c2adSjeremylt 161b2165e7aSSebastian Grimberg //------------------------------------------------------------------------------ 162b2165e7aSSebastian Grimberg // Vector Restore Array Read 163b2165e7aSSebastian Grimberg //------------------------------------------------------------------------------ 1642b730f8bSJeremy L Thompson static int CeedVectorRestoreArrayRead_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; } 1650436c2adSjeremylt 166f10650afSjeremylt //------------------------------------------------------------------------------ 167f10650afSjeremylt // Vector Destroy 168f10650afSjeremylt //------------------------------------------------------------------------------ 1690436c2adSjeremylt static int CeedVectorDestroy_Ref(CeedVector vec) { 1700436c2adSjeremylt CeedVector_Ref *impl; 1710436c2adSjeremylt 172ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1732b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 1742b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 175e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1760436c2adSjeremylt } 1770436c2adSjeremylt 178f10650afSjeremylt //------------------------------------------------------------------------------ 179f10650afSjeremylt // Vector Create 180f10650afSjeremylt //------------------------------------------------------------------------------ 1811f9221feSJeremy L Thompson int CeedVectorCreate_Ref(CeedSize n, CeedVector vec) { 1820436c2adSjeremylt Ceed ceed; 183ad70ee2cSJeremy L Thompson CeedVector_Ref *impl; 1840436c2adSjeremylt 185ad70ee2cSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 1862b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Ref)); 1872b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Ref)); 1882b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Ref)); 1892b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Ref)); 1902b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Ref)); 1912b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Ref)); 1922b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Ref)); 1932b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray", CeedVectorRestoreArray_Ref)); 1942b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead", CeedVectorRestoreArrayRead_Ref)); 1952b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Ref)); 1962b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 1972b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetData(vec, impl)); 198e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1990436c2adSjeremylt } 2002a86cc9dSSebastian Grimberg 201f10650afSjeremylt //------------------------------------------------------------------------------ 202