xref: /libCEED/backends/ref/ceed-ref-vector.c (revision 6574a04ff2135c3834f1b6ef9a4ec7566c4782db)
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;
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 
61*6574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
629c774eddSJeremy L Thompson 
63d1d35e2fSjeremylt   switch (copy_mode) {
640436c2adSjeremylt     case CEED_COPY_VALUES:
659c774eddSJeremy L Thompson       if (!impl->array_owned) {
662b730f8bSJeremy L Thompson         CeedCallBackend(CeedCalloc(length, &impl->array_owned));
679c774eddSJeremy L Thompson       }
689c774eddSJeremy L Thompson       impl->array_borrowed = NULL;
699c774eddSJeremy L Thompson       impl->array          = impl->array_owned;
702b730f8bSJeremy L Thompson       if (array) memcpy(impl->array, array, length * sizeof(array[0]));
710436c2adSjeremylt       break;
720436c2adSjeremylt     case CEED_OWN_POINTER:
732b730f8bSJeremy L Thompson       CeedCallBackend(CeedFree(&impl->array_owned));
749c774eddSJeremy L Thompson       impl->array_owned    = array;
759c774eddSJeremy L Thompson       impl->array_borrowed = NULL;
760436c2adSjeremylt       impl->array          = array;
770436c2adSjeremylt       break;
780436c2adSjeremylt     case CEED_USE_POINTER:
792b730f8bSJeremy L Thompson       CeedCallBackend(CeedFree(&impl->array_owned));
809c774eddSJeremy L Thompson       impl->array_borrowed = array;
810436c2adSjeremylt       impl->array          = array;
820436c2adSjeremylt   }
83e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
840436c2adSjeremylt }
850436c2adSjeremylt 
86f10650afSjeremylt //------------------------------------------------------------------------------
876a6c615bSJeremy L Thompson // Vector Take Array
886a6c615bSJeremy L Thompson //------------------------------------------------------------------------------
892b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
906a6c615bSJeremy L Thompson   CeedVector_Ref *impl;
912b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
926a6c615bSJeremy L Thompson   Ceed ceed;
932b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
946a6c615bSJeremy L Thompson 
95*6574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend");
969798701eSJeremy L Thompson 
979c774eddSJeremy L Thompson   (*array)             = impl->array_borrowed;
989c774eddSJeremy L Thompson   impl->array_borrowed = NULL;
996a6c615bSJeremy L Thompson   impl->array          = NULL;
1006a6c615bSJeremy L Thompson 
101e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1026a6c615bSJeremy L Thompson }
1036a6c615bSJeremy L Thompson 
1046a6c615bSJeremy L Thompson //------------------------------------------------------------------------------
105f10650afSjeremylt // Vector Get Array
106f10650afSjeremylt //------------------------------------------------------------------------------
1072b730f8bSJeremy L Thompson static int CeedVectorGetArrayCore_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
1080436c2adSjeremylt   CeedVector_Ref *impl;
1092b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1100436c2adSjeremylt   Ceed ceed;
1112b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
1120436c2adSjeremylt 
113*6574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend");
1149c774eddSJeremy L Thompson 
1150436c2adSjeremylt   *array = impl->array;
1169c774eddSJeremy L Thompson 
117e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1180436c2adSjeremylt }
1190436c2adSjeremylt 
120f10650afSjeremylt //------------------------------------------------------------------------------
121f10650afSjeremylt // Vector Get Array Read
122f10650afSjeremylt //------------------------------------------------------------------------------
1232b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
1249c774eddSJeremy L Thompson   return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array);
1259c774eddSJeremy L Thompson }
1269c774eddSJeremy L Thompson 
1279c774eddSJeremy L Thompson //------------------------------------------------------------------------------
1289c774eddSJeremy L Thompson // Vector Get Array
1299c774eddSJeremy L Thompson //------------------------------------------------------------------------------
1302b730f8bSJeremy L Thompson static int CeedVectorGetArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
1319c774eddSJeremy L Thompson   return CeedVectorGetArrayCore_Ref(vec, mem_type, array);
1329c774eddSJeremy L Thompson }
1339c774eddSJeremy L Thompson 
1349c774eddSJeremy L Thompson //------------------------------------------------------------------------------
1359c774eddSJeremy L Thompson // Vector Get Array Write
1369c774eddSJeremy L Thompson //------------------------------------------------------------------------------
1372b730f8bSJeremy L Thompson static int CeedVectorGetArrayWrite_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
1380436c2adSjeremylt   CeedVector_Ref *impl;
1392b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1400436c2adSjeremylt 
1419c774eddSJeremy L Thompson   if (!impl->array) {
1429c774eddSJeremy L Thompson     if (!impl->array_owned && !impl->array_borrowed) {
1439c774eddSJeremy L Thompson       // Allocate if array is not yet allocated
1442b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL));
1459c774eddSJeremy L Thompson     } else {
1469c774eddSJeremy L Thompson       // Select dirty array for GetArrayWrite
1472b730f8bSJeremy L Thompson       if (impl->array_borrowed) impl->array = impl->array_borrowed;
1482b730f8bSJeremy L Thompson       else impl->array = impl->array_owned;
1490436c2adSjeremylt     }
1509c774eddSJeremy L Thompson   }
1519c774eddSJeremy L Thompson 
1529c774eddSJeremy L Thompson   return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array);
1530436c2adSjeremylt }
1540436c2adSjeremylt 
155f10650afSjeremylt //------------------------------------------------------------------------------
156f10650afSjeremylt // Vector Restore Array
157f10650afSjeremylt //------------------------------------------------------------------------------
1582b730f8bSJeremy L Thompson static int CeedVectorRestoreArray_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; }
1590436c2adSjeremylt 
1602b730f8bSJeremy L Thompson static int CeedVectorRestoreArrayRead_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; }
1610436c2adSjeremylt 
162f10650afSjeremylt //------------------------------------------------------------------------------
163f10650afSjeremylt // Vector Destroy
164f10650afSjeremylt //------------------------------------------------------------------------------
1650436c2adSjeremylt static int CeedVectorDestroy_Ref(CeedVector vec) {
1660436c2adSjeremylt   CeedVector_Ref *impl;
1672b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1680436c2adSjeremylt 
1692b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->array_owned));
1702b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
171e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1720436c2adSjeremylt }
1730436c2adSjeremylt 
174f10650afSjeremylt //------------------------------------------------------------------------------
175f10650afSjeremylt // Vector Create
176f10650afSjeremylt //------------------------------------------------------------------------------
1771f9221feSJeremy L Thompson int CeedVectorCreate_Ref(CeedSize n, CeedVector vec) {
1780436c2adSjeremylt   CeedVector_Ref *impl;
1790436c2adSjeremylt   Ceed            ceed;
1802b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
1810436c2adSjeremylt 
1822b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Ref));
1832b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Ref));
1842b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Ref));
1852b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Ref));
1862b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Ref));
1872b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Ref));
1882b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Ref));
1892b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray", CeedVectorRestoreArray_Ref));
1902b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead", CeedVectorRestoreArrayRead_Ref));
1912b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Ref));
1929c774eddSJeremy L Thompson 
1932b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
1942b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetData(vec, impl));
1959c774eddSJeremy L Thompson 
196e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1970436c2adSjeremylt }
198f10650afSjeremylt //------------------------------------------------------------------------------
199