xref: /libCEED/backends/ref/ceed-ref-vector.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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>
9*2b730f8bSJeremy L Thompson #include <ceed/ceed.h>
103d576824SJeremy L Thompson #include <string.h>
11*2b730f8bSJeremy 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;
19*2b730f8bSJeremy 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 //------------------------------------------------------------------------------
29*2b730f8bSJeremy 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;
31*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
329c774eddSJeremy L Thompson   Ceed ceed;
33*2b730f8bSJeremy 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
41*2b730f8bSJeremy 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 //------------------------------------------------------------------------------
52*2b730f8bSJeremy L Thompson static int CeedVectorSetArray_Ref(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) {
530436c2adSjeremylt   CeedVector_Ref *impl;
54*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
551f9221feSJeremy L Thompson   CeedSize length;
56*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
570436c2adSjeremylt   Ceed ceed;
58*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
590436c2adSjeremylt 
60*2b730f8bSJeremy L Thompson   if (mem_type != CEED_MEM_HOST) {
610436c2adSjeremylt     // LCOV_EXCL_START
62*2b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
630436c2adSjeremylt     // LCOV_EXCL_STOP
64*2b730f8bSJeremy L Thompson   }
659c774eddSJeremy L Thompson 
66d1d35e2fSjeremylt   switch (copy_mode) {
670436c2adSjeremylt     case CEED_COPY_VALUES:
689c774eddSJeremy L Thompson       if (!impl->array_owned) {
69*2b730f8bSJeremy 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;
73*2b730f8bSJeremy L Thompson       if (array) memcpy(impl->array, array, length * sizeof(array[0]));
740436c2adSjeremylt       break;
750436c2adSjeremylt     case CEED_OWN_POINTER:
76*2b730f8bSJeremy 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:
82*2b730f8bSJeremy 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 //------------------------------------------------------------------------------
92*2b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
936a6c615bSJeremy L Thompson   CeedVector_Ref *impl;
94*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
956a6c615bSJeremy L Thompson   Ceed ceed;
96*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
976a6c615bSJeremy L Thompson 
989c774eddSJeremy L Thompson   (*array)             = impl->array_borrowed;
999c774eddSJeremy L Thompson   impl->array_borrowed = NULL;
1006a6c615bSJeremy L Thompson   impl->array          = NULL;
1016a6c615bSJeremy L Thompson 
102e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1036a6c615bSJeremy L Thompson }
1046a6c615bSJeremy L Thompson 
1056a6c615bSJeremy L Thompson //------------------------------------------------------------------------------
106f10650afSjeremylt // Vector Get Array
107f10650afSjeremylt //------------------------------------------------------------------------------
108*2b730f8bSJeremy L Thompson static int CeedVectorGetArrayCore_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
1090436c2adSjeremylt   CeedVector_Ref *impl;
110*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1110436c2adSjeremylt   Ceed ceed;
112*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
1130436c2adSjeremylt 
114*2b730f8bSJeremy L Thompson   if (mem_type != CEED_MEM_HOST) {
1150436c2adSjeremylt     // LCOV_EXCL_START
116*2b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend");
1170436c2adSjeremylt     // LCOV_EXCL_STOP
118*2b730f8bSJeremy L Thompson   }
1199c774eddSJeremy L Thompson 
1200436c2adSjeremylt   *array = impl->array;
1219c774eddSJeremy L Thompson 
122e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1230436c2adSjeremylt }
1240436c2adSjeremylt 
125f10650afSjeremylt //------------------------------------------------------------------------------
126f10650afSjeremylt // Vector Get Array Read
127f10650afSjeremylt //------------------------------------------------------------------------------
128*2b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
1299c774eddSJeremy L Thompson   return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array);
1309c774eddSJeremy L Thompson }
1319c774eddSJeremy L Thompson 
1329c774eddSJeremy L Thompson //------------------------------------------------------------------------------
1339c774eddSJeremy L Thompson // Vector Get Array
1349c774eddSJeremy L Thompson //------------------------------------------------------------------------------
135*2b730f8bSJeremy L Thompson static int CeedVectorGetArray_Ref(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
1369c774eddSJeremy L Thompson   return CeedVectorGetArrayCore_Ref(vec, mem_type, array);
1379c774eddSJeremy L Thompson }
1389c774eddSJeremy L Thompson 
1399c774eddSJeremy L Thompson //------------------------------------------------------------------------------
1409c774eddSJeremy L Thompson // Vector Get Array Write
1419c774eddSJeremy L Thompson //------------------------------------------------------------------------------
142*2b730f8bSJeremy L Thompson static int CeedVectorGetArrayWrite_Ref(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
1430436c2adSjeremylt   CeedVector_Ref *impl;
144*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1450436c2adSjeremylt 
1469c774eddSJeremy L Thompson   if (!impl->array) {
1479c774eddSJeremy L Thompson     if (!impl->array_owned && !impl->array_borrowed) {
1489c774eddSJeremy L Thompson       // Allocate if array is not yet allocated
149*2b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL));
1509c774eddSJeremy L Thompson     } else {
1519c774eddSJeremy L Thompson       // Select dirty array for GetArrayWrite
152*2b730f8bSJeremy L Thompson       if (impl->array_borrowed) impl->array = impl->array_borrowed;
153*2b730f8bSJeremy L Thompson       else impl->array = impl->array_owned;
1540436c2adSjeremylt     }
1559c774eddSJeremy L Thompson   }
1569c774eddSJeremy L Thompson 
1579c774eddSJeremy L Thompson   return CeedVectorGetArrayCore_Ref(vec, mem_type, (CeedScalar **)array);
1580436c2adSjeremylt }
1590436c2adSjeremylt 
160f10650afSjeremylt //------------------------------------------------------------------------------
161f10650afSjeremylt // Vector Restore Array
162f10650afSjeremylt //------------------------------------------------------------------------------
163*2b730f8bSJeremy L Thompson static int CeedVectorRestoreArray_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; }
1640436c2adSjeremylt 
165*2b730f8bSJeremy L Thompson static int CeedVectorRestoreArrayRead_Ref(CeedVector vec) { return CEED_ERROR_SUCCESS; }
1660436c2adSjeremylt 
167f10650afSjeremylt //------------------------------------------------------------------------------
168f10650afSjeremylt // Vector Destroy
169f10650afSjeremylt //------------------------------------------------------------------------------
1700436c2adSjeremylt static int CeedVectorDestroy_Ref(CeedVector vec) {
1710436c2adSjeremylt   CeedVector_Ref *impl;
172*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1730436c2adSjeremylt 
174*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->array_owned));
175*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
176e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1770436c2adSjeremylt }
1780436c2adSjeremylt 
179f10650afSjeremylt //------------------------------------------------------------------------------
180f10650afSjeremylt // Vector Create
181f10650afSjeremylt //------------------------------------------------------------------------------
1821f9221feSJeremy L Thompson int CeedVectorCreate_Ref(CeedSize n, CeedVector vec) {
1830436c2adSjeremylt   CeedVector_Ref *impl;
1840436c2adSjeremylt   Ceed            ceed;
185*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
1860436c2adSjeremylt 
187*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Ref));
188*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Ref));
189*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Ref));
190*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Ref));
191*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Ref));
192*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Ref));
193*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Ref));
194*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray", CeedVectorRestoreArray_Ref));
195*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead", CeedVectorRestoreArrayRead_Ref));
196*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Ref));
1979c774eddSJeremy L Thompson 
198*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
199*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetData(vec, impl));
2009c774eddSJeremy L Thompson 
201e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2020436c2adSjeremylt }
203f10650afSjeremylt //------------------------------------------------------------------------------
204