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 8ec3da8bcSJed Brown #include <ceed/backend.h> 92b730f8bSJeremy L Thompson #include <ceed/ceed.h> 103d576824SJeremy L Thompson #include <string.h> 112b730f8bSJeremy L Thompson 120436c2adSjeremylt #include "ceed-ref.h" 130436c2adSjeremylt 14f10650afSjeremylt //------------------------------------------------------------------------------ 159c774eddSJeremy L Thompson // Has Valid Array 169c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 179c774eddSJeremy L Thompson static int CeedVectorHasValidArray_Ref(CeedVector vec, bool *has_valid_array) { 189c774eddSJeremy L Thompson CeedVector_Ref *impl; 192b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 209c774eddSJeremy L Thompson 219c774eddSJeremy L Thompson *has_valid_array = !!impl->array; 229c774eddSJeremy L Thompson 239c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 249c774eddSJeremy L Thompson } 259c774eddSJeremy L Thompson 269c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 279c774eddSJeremy L Thompson // Check if has borrowed array of given type 289c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 292b730f8bSJeremy L Thompson static inline int CeedVectorHasBorrowedArrayOfType_Ref(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 309c774eddSJeremy L Thompson CeedVector_Ref *impl; 312b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 329c774eddSJeremy L Thompson Ceed ceed; 332b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 349c774eddSJeremy L Thompson 359c774eddSJeremy L Thompson switch (mem_type) { 369c774eddSJeremy L Thompson case CEED_MEM_HOST: 379c774eddSJeremy L Thompson *has_borrowed_array_of_type = !!impl->array_borrowed; 389c774eddSJeremy L Thompson break; 399c774eddSJeremy L Thompson default: 409c774eddSJeremy L Thompson // LCOV_EXCL_START 412b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 429c774eddSJeremy L Thompson // LCOV_EXCL_STOP 439c774eddSJeremy L Thompson break; 449c774eddSJeremy L Thompson } 459c774eddSJeremy L Thompson 469c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 479c774eddSJeremy L Thompson } 489c774eddSJeremy L Thompson 499c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 50f10650afSjeremylt // Vector Set Array 51f10650afSjeremylt //------------------------------------------------------------------------------ 522b730f8bSJeremy L Thompson static int CeedVectorSetArray_Ref(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 530436c2adSjeremylt CeedVector_Ref *impl; 542b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 551f9221feSJeremy L Thompson CeedSize length; 562b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 570436c2adSjeremylt Ceed ceed; 582b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 590436c2adSjeremylt 602b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 610436c2adSjeremylt // LCOV_EXCL_START 622b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 630436c2adSjeremylt // LCOV_EXCL_STOP 642b730f8bSJeremy L Thompson } 659c774eddSJeremy L Thompson 66d1d35e2fSjeremylt switch (copy_mode) { 670436c2adSjeremylt case CEED_COPY_VALUES: 689c774eddSJeremy L Thompson if (!impl->array_owned) { 692b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(length, &impl->array_owned)); 709c774eddSJeremy L Thompson } 719c774eddSJeremy L Thompson impl->array_borrowed = NULL; 729c774eddSJeremy L Thompson impl->array = impl->array_owned; 732b730f8bSJeremy L Thompson if (array) memcpy(impl->array, array, length * sizeof(array[0])); 740436c2adSjeremylt break; 750436c2adSjeremylt case CEED_OWN_POINTER: 762b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 779c774eddSJeremy L Thompson impl->array_owned = array; 789c774eddSJeremy L Thompson impl->array_borrowed = NULL; 790436c2adSjeremylt impl->array = array; 800436c2adSjeremylt break; 810436c2adSjeremylt case CEED_USE_POINTER: 822b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 839c774eddSJeremy L Thompson impl->array_borrowed = array; 840436c2adSjeremylt impl->array = array; 850436c2adSjeremylt } 86e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 870436c2adSjeremylt } 880436c2adSjeremylt 89f10650afSjeremylt //------------------------------------------------------------------------------ 906a6c615bSJeremy L Thompson // Vector Take Array 916a6c615bSJeremy L Thompson //------------------------------------------------------------------------------ 922b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 936a6c615bSJeremy L Thompson CeedVector_Ref *impl; 942b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 956a6c615bSJeremy L Thompson Ceed ceed; 962b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 976a6c615bSJeremy L Thompson 98*9798701eSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 99*9798701eSJeremy L Thompson // LCOV_EXCL_START 100*9798701eSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 101*9798701eSJeremy L Thompson // LCOV_EXCL_STOP 102*9798701eSJeremy L Thompson } 103*9798701eSJeremy L Thompson 1049c774eddSJeremy L Thompson (*array) = impl->array_borrowed; 1059c774eddSJeremy L Thompson impl->array_borrowed = NULL; 1066a6c615bSJeremy L Thompson impl->array = NULL; 1076a6c615bSJeremy L Thompson 108e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1096a6c615bSJeremy L Thompson } 1106a6c615bSJeremy L Thompson 1116a6c615bSJeremy L Thompson //------------------------------------------------------------------------------ 112f10650afSjeremylt // Vector Get Array 113f10650afSjeremylt //------------------------------------------------------------------------------ 1142b730f8bSJeremy L Thompson static int CeedVectorGetArrayCore_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 1150436c2adSjeremylt CeedVector_Ref *impl; 1162b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1170436c2adSjeremylt Ceed ceed; 1182b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 1190436c2adSjeremylt 1202b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 1210436c2adSjeremylt // LCOV_EXCL_START 1222b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 1230436c2adSjeremylt // LCOV_EXCL_STOP 1242b730f8bSJeremy L Thompson } 1259c774eddSJeremy L Thompson 1260436c2adSjeremylt *array = impl->array; 1279c774eddSJeremy L Thompson 128e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1290436c2adSjeremylt } 1300436c2adSjeremylt 131f10650afSjeremylt //------------------------------------------------------------------------------ 132f10650afSjeremylt // Vector Get Array Read 133f10650afSjeremylt //------------------------------------------------------------------------------ 1342b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 1359c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array); 1369c774eddSJeremy L Thompson } 1379c774eddSJeremy L Thompson 1389c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1399c774eddSJeremy L Thompson // Vector Get Array 1409c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1412b730f8bSJeremy L Thompson static int CeedVectorGetArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 1429c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, array); 1439c774eddSJeremy L Thompson } 1449c774eddSJeremy L Thompson 1459c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1469c774eddSJeremy L Thompson // Vector Get Array Write 1479c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 1482b730f8bSJeremy L Thompson static int CeedVectorGetArrayWrite_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 1490436c2adSjeremylt CeedVector_Ref *impl; 1502b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1510436c2adSjeremylt 1529c774eddSJeremy L Thompson if (!impl->array) { 1539c774eddSJeremy L Thompson if (!impl->array_owned && !impl->array_borrowed) { 1549c774eddSJeremy L Thompson // Allocate if array is not yet allocated 1552b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL)); 1569c774eddSJeremy L Thompson } else { 1579c774eddSJeremy L Thompson // Select dirty array for GetArrayWrite 1582b730f8bSJeremy L Thompson if (impl->array_borrowed) impl->array = impl->array_borrowed; 1592b730f8bSJeremy L Thompson else impl->array = impl->array_owned; 1600436c2adSjeremylt } 1619c774eddSJeremy L Thompson } 1629c774eddSJeremy L Thompson 1639c774eddSJeremy L Thompson return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array); 1640436c2adSjeremylt } 1650436c2adSjeremylt 166f10650afSjeremylt //------------------------------------------------------------------------------ 167f10650afSjeremylt // Vector Restore Array 168f10650afSjeremylt //------------------------------------------------------------------------------ 1692b730f8bSJeremy L Thompson static int CeedVectorRestoreArray_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; } 1700436c2adSjeremylt 1712b730f8bSJeremy L Thompson static int CeedVectorRestoreArrayRead_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; } 1720436c2adSjeremylt 173f10650afSjeremylt //------------------------------------------------------------------------------ 174f10650afSjeremylt // Vector Destroy 175f10650afSjeremylt //------------------------------------------------------------------------------ 1760436c2adSjeremylt static int CeedVectorDestroy_Ref(CeedVector vec) { 1770436c2adSjeremylt CeedVector_Ref *impl; 1782b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1790436c2adSjeremylt 1802b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->array_owned)); 1812b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 182e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1830436c2adSjeremylt } 1840436c2adSjeremylt 185f10650afSjeremylt //------------------------------------------------------------------------------ 186f10650afSjeremylt // Vector Create 187f10650afSjeremylt //------------------------------------------------------------------------------ 1881f9221feSJeremy L Thompson int CeedVectorCreate_Ref(CeedSize n, CeedVector vec) { 1890436c2adSjeremylt CeedVector_Ref *impl; 1900436c2adSjeremylt Ceed ceed; 1912b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 1920436c2adSjeremylt 1932b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Ref)); 1942b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Ref)); 1952b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Ref)); 1962b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Ref)); 1972b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Ref)); 1982b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Ref)); 1992b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Ref)); 2002b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray", CeedVectorRestoreArray_Ref)); 2012b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead", CeedVectorRestoreArrayRead_Ref)); 2022b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Ref)); 2039c774eddSJeremy L Thompson 2042b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 2052b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetData(vec, impl)); 2069c774eddSJeremy L Thompson 207e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2080436c2adSjeremylt } 209f10650afSjeremylt //------------------------------------------------------------------------------ 210