xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-vector.c (revision 126347300c7ca7241e05a0a707bdb6ba247c9e5a)
1 // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed-impl.h>
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 #include <assert.h>
12 #include <math.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 
17 /// @file
18 /// Implementation of public CeedVector interfaces
19 
20 /// @cond DOXYGEN_SKIP
21 static struct CeedVector_private ceed_vector_active;
22 static struct CeedVector_private ceed_vector_none;
23 /// @endcond
24 
25 /// @addtogroup CeedVectorUser
26 /// @{
27 
28 /// Indicate that vector will be provided as an explicit argument to @ref CeedOperatorApply().
29 const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active;
30 
31 /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHT).
32 const CeedVector CEED_VECTOR_NONE = &ceed_vector_none;
33 
34 /// @}
35 
36 /// ----------------------------------------------------------------------------
37 /// CeedVector Backend API
38 /// ----------------------------------------------------------------------------
39 /// @addtogroup CeedVectorBackend
40 /// @{
41 
42 /**
43   @brief Check for valid data in a `CeedVector`
44 
45   @param[in]  vec             `CeedVector` to check validity
46   @param[out] has_valid_array Variable to store validity
47 
48   @return An error code: 0 - success, otherwise - failure
49 
50   @ref Backend
51 **/
52 int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) {
53   CeedSize length;
54 
55   CeedCheck(vec->HasValidArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasValidArray");
56   CeedCall(CeedVectorGetLength(vec, &length));
57   if (length == 0) {
58     *has_valid_array = true;
59     return CEED_ERROR_SUCCESS;
60   }
61   CeedCall(vec->HasValidArray(vec, has_valid_array));
62   return CEED_ERROR_SUCCESS;
63 }
64 
65 /**
66   @brief Check for borrowed array of a specific @ref CeedMemType in a `CeedVector`
67 
68   @param[in]  vec                        `CeedVector` to check
69   @param[in]  mem_type                   Memory type to check
70   @param[out] has_borrowed_array_of_type Variable to store result
71 
72   @return An error code: 0 - success, otherwise - failure
73 
74   @ref Backend
75 **/
76 int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) {
77   CeedCheck(vec->HasBorrowedArrayOfType, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED,
78             "Backend does not support CeedVectorHasBorrowedArrayOfType");
79   CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type));
80   return CEED_ERROR_SUCCESS;
81 }
82 
83 /**
84   @brief Get the state of a `CeedVector`
85 
86   @param[in]  vec    `CeedVector` to retrieve state
87   @param[out] state  Variable to store state
88 
89   @return An error code: 0 - success, otherwise - failure
90 
91   @ref Backend
92 **/
93 int CeedVectorGetState(CeedVector vec, uint64_t *state) {
94   *state = vec->state;
95   return CEED_ERROR_SUCCESS;
96 }
97 
98 /**
99   @brief Get the backend data of a `CeedVector`
100 
101   @param[in]  vec  `CeedVector` to retrieve state
102   @param[out] data Variable to store data
103 
104   @return An error code: 0 - success, otherwise - failure
105 
106   @ref Backend
107 **/
108 int CeedVectorGetData(CeedVector vec, void *data) {
109   *(void **)data = vec->data;
110   return CEED_ERROR_SUCCESS;
111 }
112 
113 /**
114   @brief Set the backend data of a `CeedVector`
115 
116   @param[in,out] vec  `CeedVector` to retrieve state
117   @param[in]     data Data to set
118 
119   @return An error code: 0 - success, otherwise - failure
120 
121   @ref Backend
122 **/
123 int CeedVectorSetData(CeedVector vec, void *data) {
124   vec->data = data;
125   return CEED_ERROR_SUCCESS;
126 }
127 
128 /**
129   @brief Increment the reference counter for a `CeedVector`
130 
131   @param[in,out] vec `CeedVector` to increment the reference counter
132 
133   @return An error code: 0 - success, otherwise - failure
134 
135   @ref Backend
136 **/
137 int CeedVectorReference(CeedVector vec) {
138   vec->ref_count++;
139   return CEED_ERROR_SUCCESS;
140 }
141 
142 /// @}
143 
144 /// ----------------------------------------------------------------------------
145 /// CeedVector Public API
146 /// ----------------------------------------------------------------------------
147 /// @addtogroup CeedVectorUser
148 /// @{
149 
150 /**
151   @brief Create a `CeedVector` of the specified length (does not allocate memory)
152 
153   @param[in]  ceed   `Ceed` object used to create the `CeedVector`
154   @param[in]  length Length of vector
155   @param[out] vec    Address of the variable where the newly created `CeedVector` will be stored
156 
157   @return An error code: 0 - success, otherwise - failure
158 
159   @ref User
160 **/
161 int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) {
162   if (!ceed->VectorCreate) {
163     Ceed delegate;
164 
165     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector"));
166     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement VectorCreate");
167     CeedCall(CeedVectorCreate(delegate, length, vec));
168     CeedCall(CeedDestroy(&delegate));
169     return CEED_ERROR_SUCCESS;
170   }
171 
172   CeedCall(CeedCalloc(1, vec));
173   CeedCall(CeedReferenceCopy(ceed, &(*vec)->ceed));
174   (*vec)->ref_count = 1;
175   (*vec)->length    = length;
176   (*vec)->state     = 0;
177   CeedCall(ceed->VectorCreate(length, *vec));
178   return CEED_ERROR_SUCCESS;
179 }
180 
181 /**
182   @brief Copy the pointer to a `CeedVector`.
183 
184   Both pointers should be destroyed with @ref CeedVectorDestroy().
185 
186   Note: If the value of `*vec_copy` passed to this function is non-`NULL`, then it is assumed that `*vec_copy` is a pointer to a `CeedVector`.
187         This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`.
188 
189   @param[in]     vec      `CeedVector` to copy reference to
190   @param[in,out] vec_copy Variable to store copied reference
191 
192   @return An error code: 0 - success, otherwise - failure
193 
194   @ref User
195 **/
196 int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) {
197   if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec));
198   CeedCall(CeedVectorDestroy(vec_copy));
199   *vec_copy = vec;
200   return CEED_ERROR_SUCCESS;
201 }
202 
203 /**
204   @brief Copy a `CeedVector` into a different `CeedVector`.
205 
206   @param[in]     vec      `CeedVector` to copy
207   @param[in,out] vec_copy `CeedVector` to copy array into
208 
209   @return An error code: 0 - success, otherwise - failure
210 
211   @ref User
212 **/
213 int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) {
214   CeedMemType mem_type, mem_type_copy;
215   CeedScalar *array;
216 
217   // Get the preferred memory types
218   {
219     Ceed ceed;
220 
221     CeedCall(CeedVectorGetCeed(vec, &ceed));
222     CeedCall(CeedGetPreferredMemType(ceed, &mem_type));
223     CeedCall(CeedDestroy(&ceed));
224 
225     CeedCall(CeedVectorGetCeed(vec_copy, &ceed));
226     CeedCall(CeedGetPreferredMemType(ceed, &mem_type_copy));
227     CeedCall(CeedDestroy(&ceed));
228   }
229 
230   // Check that both have same memory type
231   if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST;
232 
233   // Check compatible lengths
234   {
235     CeedSize length_vec, length_copy;
236 
237     CeedCall(CeedVectorGetLength(vec, &length_vec));
238     CeedCall(CeedVectorGetLength(vec_copy, &length_copy));
239     CeedCheck(length_vec == length_copy, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPATIBLE, "CeedVectors must have the same length to copy");
240   }
241 
242   // Copy the values from vec to vec_copy
243   CeedCall(CeedVectorGetArray(vec, mem_type, &array));
244   CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array));
245 
246   CeedCall(CeedVectorRestoreArray(vec, &array));
247   return CEED_ERROR_SUCCESS;
248 }
249 
250 /**
251   @brief Copy a strided portion of `CeedVector` contents into a different `CeedVector`
252 
253   @param[in]     vec      `CeedVector` to copy
254   @param[in]     start    First index to copy
255   @param[in]     step     Stride between indices to copy
256   @param[in,out] vec_copy `CeedVector` to copy values to
257 
258   @return An error code: 0 - success, otherwise - failure
259 
260   @ref User
261 **/
262 int CeedVectorCopyStrided(CeedVector vec, CeedSize start, CeedInt step, CeedVector vec_copy) {
263   CeedSize          length;
264   const CeedScalar *array      = NULL;
265   CeedScalar       *array_copy = NULL;
266 
267   // Backend version
268   if (vec->CopyStrided && vec_copy->CopyStrided) {
269     CeedCall(vec->CopyStrided(vec, start, step, vec_copy));
270     vec_copy->state += 2;
271     return CEED_ERROR_SUCCESS;
272   }
273 
274   // Get length
275   {
276     CeedSize length_vec, length_copy;
277 
278     CeedCall(CeedVectorGetLength(vec, &length_vec));
279     CeedCall(CeedVectorGetLength(vec_copy, &length_copy));
280     if (length_vec <= 0 || length_copy <= 0) return CEED_ERROR_SUCCESS;
281     length = length_vec < length_copy ? length_vec : length_copy;
282   }
283 
284   // Copy
285   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array));
286   CeedCall(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, &array_copy));
287   for (CeedSize i = start; i < length; i += step) array_copy[i] = array[i];
288 
289   // Cleanup
290   CeedCall(CeedVectorRestoreArrayRead(vec, &array));
291   CeedCall(CeedVectorRestoreArray(vec_copy, &array_copy));
292   return CEED_ERROR_SUCCESS;
293 }
294 
295 /**
296   @brief Set the array used by a `CeedVector`, freeing any previously allocated array if applicable.
297 
298   The backend may copy values to a different @ref CeedMemType, such as during @ref CeedOperatorApply().
299   See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
300 
301   @param[in,out] vec       `CeedVector`
302   @param[in]     mem_type  Memory type of the array being passed
303   @param[in]     copy_mode Copy mode for the array
304   @param[in]     array     Array to be used, or `NULL` with @ref CEED_COPY_VALUES to have the library allocate
305 
306   @return An error code: 0 - success, otherwise - failure
307 
308   @ref User
309 **/
310 int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) {
311   CeedSize length;
312 
313   CeedCheck(vec->SetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray");
314   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
315             "Cannot grant CeedVector array access, the access lock is already in use");
316   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
317 
318   CeedCall(CeedVectorGetLength(vec, &length));
319   if (length > 0) CeedCall(vec->SetArray(vec, mem_type, copy_mode, array));
320   vec->state += 2;
321   return CEED_ERROR_SUCCESS;
322 }
323 
324 /**
325   @brief Set the `CeedVector` to a constant value
326 
327   @param[in,out] vec   `CeedVector`
328   @param[in]     value Value to be used
329 
330   @return An error code: 0 - success, otherwise - failure
331 
332   @ref User
333 **/
334 int CeedVectorSetValue(CeedVector vec, CeedScalar value) {
335   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
336             "Cannot grant CeedVector array access, the access lock is already in use");
337   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
338 
339   if (vec->SetValue) {
340     CeedCall(vec->SetValue(vec, value));
341     vec->state += 2;
342   } else {
343     CeedSize    length;
344     CeedScalar *array;
345 
346     CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array));
347     CeedCall(CeedVectorGetLength(vec, &length));
348     for (CeedSize i = 0; i < length; i++) array[i] = value;
349     CeedCall(CeedVectorRestoreArray(vec, &array));
350   }
351   return CEED_ERROR_SUCCESS;
352 }
353 
354 /**
355   @brief Set a portion of a `CeedVector` to a constant value.
356 
357   Note: The `CeedVector` must already have valid data set via @ref CeedVectorSetArray() or similar.
358 
359   @param[in,out] vec   `CeedVector`
360   @param[in]     start First index to set in range `[start, stop)`
361   @param[in]     stop  One past the last element to set in the range, or `-1` for `length`
362   @param[in]     step  Stride between indices to set
363   @param[in]     value Value to be used
364 
365   @return An error code: 0 - success, otherwise - failure
366 
367   @ref User
368 **/
369 int CeedVectorSetValueStrided(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedScalar value) {
370   CeedSize length;
371 
372   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
373             "Cannot grant CeedVector array access, the access lock is already in use");
374   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
375   CeedCall(CeedVectorGetLength(vec, &length));
376   CeedCheck(stop >= -1 && stop <= length, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
377             "Invalid value for stop %" CeedSize_FMT ", must be in the range [-1, length]", stop);
378 
379   if (vec->SetValueStrided) {
380     CeedCall(vec->SetValueStrided(vec, start, stop, step, value));
381     vec->state += 2;
382   } else {
383     CeedScalar *array;
384 
385     if (length <= 0) return CEED_ERROR_SUCCESS;
386     if (stop == -1) stop = length;
387     CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array));
388     for (CeedSize i = start; i < stop; i += step) array[i] = value;
389     CeedCall(CeedVectorRestoreArray(vec, &array));
390   }
391   return CEED_ERROR_SUCCESS;
392 }
393 
394 /**
395   @brief Sync the `CeedVector` to a specified `mem_type`.
396 
397   This function is used to force synchronization of arrays set with @ref CeedVectorSetArray().
398   If the requested `mem_type` is already synchronized, this function results in a no-op.
399 
400   @param[in,out] vec      `CeedVector`
401   @param[in]     mem_type @ref CeedMemType to be synced
402 
403   @return An error code: 0 - success, otherwise - failure
404 
405   @ref User
406 **/
407 int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
408   CeedSize length;
409 
410   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use");
411 
412   // Don't sync empty array
413   CeedCall(CeedVectorGetLength(vec, &length));
414   if (length == 0) return CEED_ERROR_SUCCESS;
415 
416   if (vec->SyncArray) {
417     CeedCall(vec->SyncArray(vec, mem_type));
418   } else {
419     const CeedScalar *array;
420 
421     CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array));
422     CeedCall(CeedVectorRestoreArrayRead(vec, &array));
423   }
424   return CEED_ERROR_SUCCESS;
425 }
426 
427 /**
428   @brief Take ownership of the `CeedVector` array set by @ref CeedVectorSetArray() with @ref CEED_USE_POINTER and remove the array from the `CeedVector`.
429 
430   The caller is responsible for managing and freeing the array.
431   This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type.
432 
433   @param[in,out] vec      `CeedVector`
434   @param[in]     mem_type Memory type on which to take the array.
435                             If the backend uses a different memory type, this will perform a copy.
436   @param[out]    array    Array on memory type `mem_type`, or `NULL` if array pointer is not required
437 
438   @return An error code: 0 - success, otherwise - failure
439 
440   @ref User
441 **/
442 int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
443   CeedSize    length;
444   CeedScalar *temp_array = NULL;
445 
446   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use");
447   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access");
448 
449   CeedCall(CeedVectorGetLength(vec, &length));
450   if (length > 0) {
451     bool has_borrowed_array_of_type = true, has_valid_array = true;
452 
453     CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type));
454     CeedCheck(has_borrowed_array_of_type, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
455               "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]);
456 
457     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
458     CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
459               "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray");
460 
461     CeedCall(vec->TakeArray(vec, mem_type, &temp_array));
462   }
463   if (array) (*array) = temp_array;
464   return CEED_ERROR_SUCCESS;
465 }
466 
467 /**
468   @brief Get read/write access to a `CeedVector` via the specified memory type.
469 
470   Restore access with @ref CeedVectorRestoreArray().
471 
472   @param[in,out] vec      `CeedVector` to access
473   @param[in]     mem_type Memory type on which to access the array.
474                             If the backend uses a different memory type, this will perform a copy.
475   @param[out]    array    Array on memory type `mem_type`
476 
477   @note The @ref CeedVectorGetArray() and @ref CeedVectorRestoreArray() functions provide access to array pointers in the desired memory space.
478         Pairing get/restore allows the `CeedVector` to track access, thus knowing if norms or other operations may need to be recomputed.
479 
480   @return An error code: 0 - success, otherwise - failure
481 
482   @ref User
483 **/
484 int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
485   CeedSize length;
486 
487   CeedCheck(vec->GetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray");
488   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
489             "Cannot grant CeedVector array access, the access lock is already in use");
490   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
491 
492   CeedCall(CeedVectorGetLength(vec, &length));
493   if (length > 0) {
494     bool has_valid_array = true;
495 
496     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
497     CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
498               "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
499 
500     CeedCall(vec->GetArray(vec, mem_type, array));
501   } else {
502     *array = NULL;
503   }
504   vec->state++;
505   return CEED_ERROR_SUCCESS;
506 }
507 
508 /**
509   @brief Get read-only access to a `CeedVector` via the specified memory type.
510 
511   Restore access with @ref CeedVectorRestoreArrayRead().
512 
513   @param[in]  vec      `CeedVector` to access
514   @param[in]  mem_type Memory type on which to access the array.
515                          If the backend uses a different memory type, this will perform a copy (possibly cached).
516   @param[out] array    Array on memory type `mem_type`
517 
518   @return An error code: 0 - success, otherwise - failure
519 
520   @ref User
521 **/
522 int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
523   CeedSize length;
524 
525   CeedCheck(vec->GetArrayRead, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead");
526   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
527             "Cannot grant CeedVector read-only array access, the access lock is already in use");
528 
529   CeedCall(CeedVectorGetLength(vec, &length));
530   if (length > 0) {
531     bool has_valid_array = true;
532 
533     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
534     CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
535               "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
536 
537     CeedCall(vec->GetArrayRead(vec, mem_type, array));
538   } else {
539     *array = NULL;
540   }
541   vec->num_readers++;
542   return CEED_ERROR_SUCCESS;
543 }
544 
545 /**
546   @brief Get write access to a `CeedVector` via the specified memory type.
547 
548   Restore access with @ref CeedVectorRestoreArray().
549   All old values should be assumed to be invalid.
550 
551   @param[in,out] vec      `CeedVector` to access
552   @param[in]     mem_type Memory type on which to access the array.
553   @param[out]    array    Array on memory type `mem_type`
554 
555   @return An error code: 0 - success, otherwise - failure
556 
557   @ref User
558 **/
559 int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
560   CeedSize length;
561 
562   CeedCheck(vec->GetArrayWrite, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite");
563   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
564             "Cannot grant CeedVector array access, the access lock is already in use");
565   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
566 
567   CeedCall(CeedVectorGetLength(vec, &length));
568   if (length > 0) {
569     CeedCall(vec->GetArrayWrite(vec, mem_type, array));
570   } else {
571     *array = NULL;
572   }
573   vec->state++;
574   return CEED_ERROR_SUCCESS;
575 }
576 
577 /**
578   @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite()
579 
580   @param[in,out] vec   `CeedVector` to restore
581   @param[in,out] array Array of vector data
582 
583   @return An error code: 0 - success, otherwise - failure
584 
585   @ref User
586 **/
587 int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
588   CeedSize length;
589 
590   CeedCheck(vec->state % 2 == 1, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted");
591   CeedCall(CeedVectorGetLength(vec, &length));
592   if (length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec));
593   *array = NULL;
594   vec->state++;
595   return CEED_ERROR_SUCCESS;
596 }
597 
598 /**
599   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
600 
601   @param[in]     vec   `CeedVector` to restore
602   @param[in,out] array Array of vector data
603 
604   @return An error code: 0 - success, otherwise - failure
605 
606   @ref User
607 **/
608 int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
609   CeedSize length;
610 
611   CeedCheck(vec->num_readers > 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
612             "Cannot restore CeedVector array read access, access was not granted");
613   vec->num_readers--;
614   CeedCall(CeedVectorGetLength(vec, &length));
615   if (length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec));
616   *array = NULL;
617   return CEED_ERROR_SUCCESS;
618 }
619 
620 /**
621   @brief Get the norm of a `CeedVector`.
622 
623   Note: This operation is local to the `CeedVector`.
624         This function will likely not provide the desired results for the norm of the libCEED portion of a parallel vector or a `CeedVector` with duplicated or hanging nodes.
625 
626   @param[in]  vec       `CeedVector` to retrieve maximum value
627   @param[in]  norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
628   @param[out] norm      Variable to store norm value
629 
630   @return An error code: 0 - success, otherwise - failure
631 
632   @ref User
633 **/
634 int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
635   bool     has_valid_array = true;
636   CeedSize length;
637 
638   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
639   CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
640             "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray");
641 
642   CeedCall(CeedVectorGetLength(vec, &length));
643   if (length == 0) {
644     *norm = 0;
645     return CEED_ERROR_SUCCESS;
646   }
647 
648   // Backend impl for GPU, if added
649   if (vec->Norm) {
650     CeedCall(vec->Norm(vec, norm_type, norm));
651     return CEED_ERROR_SUCCESS;
652   }
653 
654   const CeedScalar *array;
655   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array));
656   assert(array);
657 
658   *norm = 0.;
659   switch (norm_type) {
660     case CEED_NORM_1:
661       for (CeedSize i = 0; i < length; i++) {
662         *norm += fabs(array[i]);
663       }
664       break;
665     case CEED_NORM_2:
666       for (CeedSize i = 0; i < length; i++) {
667         *norm += fabs(array[i]) * fabs(array[i]);
668       }
669       break;
670     case CEED_NORM_MAX:
671       for (CeedSize i = 0; i < length; i++) {
672         const CeedScalar abs_v_i = fabs(array[i]);
673         *norm                    = *norm > abs_v_i ? *norm : abs_v_i;
674       }
675   }
676   if (norm_type == CEED_NORM_2) *norm = sqrt(*norm);
677 
678   CeedCall(CeedVectorRestoreArrayRead(vec, &array));
679   return CEED_ERROR_SUCCESS;
680 }
681 
682 /**
683   @brief Compute `x = alpha x`
684 
685   @param[in,out] x     `CeedVector` for scaling
686   @param[in]     alpha scaling factor
687 
688   @return An error code: 0 - success, otherwise - failure
689 
690   @ref User
691 **/
692 int CeedVectorScale(CeedVector x, CeedScalar alpha) {
693   bool        has_valid_array = true;
694   CeedSize    length;
695   CeedScalar *x_array = NULL;
696 
697   CeedCall(CeedVectorHasValidArray(x, &has_valid_array));
698   CeedCheck(has_valid_array, CeedVectorReturnCeed(x), CEED_ERROR_BACKEND,
699             "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray");
700 
701   // Return early for empty vector
702   CeedCall(CeedVectorGetLength(x, &length));
703   if (length == 0) return CEED_ERROR_SUCCESS;
704 
705   // Backend implementation
706   if (x->Scale) return x->Scale(x, alpha);
707 
708   // Default implementation
709   CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array));
710   assert(x_array);
711   for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha;
712   CeedCall(CeedVectorRestoreArray(x, &x_array));
713   return CEED_ERROR_SUCCESS;
714 }
715 
716 /**
717   @brief Compute `y = alpha x + y`
718 
719   @param[in,out] y     target `CeedVector` for sum
720   @param[in]     alpha scaling factor
721   @param[in]     x     second `CeedVector`, must be different than ``y`
722 
723   @return An error code: 0 - success, otherwise - failure
724 
725   @ref User
726 **/
727 int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
728   bool              has_valid_array_x = true, has_valid_array_y = true;
729   CeedSize          length_x, length_y;
730   CeedScalar       *y_array = NULL;
731   CeedScalar const *x_array = NULL;
732 
733   CeedCall(CeedVectorGetLength(y, &length_y));
734   CeedCall(CeedVectorGetLength(x, &length_x));
735   CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED,
736             "Cannot add vector of different lengths."
737             " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT,
738             length_x, length_y);
739   CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY");
740 
741   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
742   CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND,
743             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
744   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
745   CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND,
746             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
747 
748   {
749     Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y;
750 
751     CeedCall(CeedVectorGetCeed(y, &ceed_y));
752     CeedCall(CeedVectorGetCeed(x, &ceed_x));
753     CeedCall(CeedGetParent(ceed_x, &ceed_parent_x));
754     CeedCall(CeedGetParent(ceed_y, &ceed_parent_y));
755     CeedCall(CeedDestroy(&ceed_x));
756     CeedCall(CeedDestroy(&ceed_y));
757     CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE,
758               "Vectors x and y must be created by the same Ceed context");
759     CeedCall(CeedDestroy(&ceed_parent_x));
760     CeedCall(CeedDestroy(&ceed_parent_y));
761   }
762 
763   // Return early for empty vectors
764   if (length_y == 0) return CEED_ERROR_SUCCESS;
765 
766   // Backend implementation
767   if (y->AXPY) {
768     CeedCall(y->AXPY(y, alpha, x));
769     return CEED_ERROR_SUCCESS;
770   }
771 
772   // Default implementation
773   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
774   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
775 
776   assert(x_array);
777   assert(y_array);
778 
779   for (CeedSize i = 0; i < length_y; i++) y_array[i] += alpha * x_array[i];
780 
781   CeedCall(CeedVectorRestoreArray(y, &y_array));
782   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
783   return CEED_ERROR_SUCCESS;
784 }
785 
786 /**
787   @brief Compute `y = alpha x + beta y`
788 
789   @param[in,out] y     target `CeedVector` for sum
790   @param[in]     alpha first scaling factor
791   @param[in]     beta  second scaling factor
792   @param[in]     x     second `CeedVector`, must be different than `y`
793 
794   @return An error code: 0 - success, otherwise - failure
795 
796   @ref User
797 **/
798 int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
799   bool              has_valid_array_x = true, has_valid_array_y = true;
800   CeedSize          length_x, length_y;
801   CeedScalar       *y_array = NULL;
802   CeedScalar const *x_array = NULL;
803 
804   CeedCall(CeedVectorGetLength(y, &length_y));
805   CeedCall(CeedVectorGetLength(x, &length_x));
806   CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED,
807             "Cannot add vector of different lengths."
808             " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT,
809             length_x, length_y);
810   CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY");
811 
812   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
813   CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND,
814             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
815   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
816   CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND,
817             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
818 
819   {
820     Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y;
821 
822     CeedCall(CeedVectorGetCeed(y, &ceed_y));
823     CeedCall(CeedVectorGetCeed(x, &ceed_x));
824     CeedCall(CeedGetParent(ceed_x, &ceed_parent_x));
825     CeedCall(CeedGetParent(ceed_y, &ceed_parent_y));
826     CeedCall(CeedDestroy(&ceed_x));
827     CeedCall(CeedDestroy(&ceed_y));
828     CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE,
829               "Vectors x and y must be created by the same Ceed context");
830     CeedCall(CeedDestroy(&ceed_parent_x));
831     CeedCall(CeedDestroy(&ceed_parent_y));
832   }
833 
834   // Return early for empty vectors
835   if (length_y == 0) return CEED_ERROR_SUCCESS;
836 
837   // Backend implementation
838   if (y->AXPBY) {
839     CeedCall(y->AXPBY(y, alpha, beta, x));
840     return CEED_ERROR_SUCCESS;
841   }
842 
843   // Default implementation
844   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
845   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
846 
847   assert(x_array);
848   assert(y_array);
849 
850   for (CeedSize i = 0; i < length_y; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i];
851 
852   CeedCall(CeedVectorRestoreArray(y, &y_array));
853   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
854   return CEED_ERROR_SUCCESS;
855 }
856 
857 /**
858   @brief Compute the pointwise multiplication \f$w = x .* y\f$.
859 
860   Any subset of `x`, `y`, and `w` may be the same `CeedVector`.
861 
862   @param[out] w target `CeedVector` for the product
863   @param[in]  x first `CeedVector` for product
864   @param[in]  y second `CeedVector` for the product
865 
866   @return An error code: 0 - success, otherwise - failure
867 
868   @ref User
869 **/
870 int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
871   bool              has_valid_array_x = true, has_valid_array_y = true;
872   CeedScalar       *w_array = NULL;
873   CeedScalar const *x_array = NULL, *y_array = NULL;
874   CeedSize          length_w, length_x, length_y;
875 
876   CeedCall(CeedVectorGetLength(w, &length_w));
877   CeedCall(CeedVectorGetLength(x, &length_x));
878   CeedCall(CeedVectorGetLength(y, &length_y));
879   CeedCheck(length_x >= length_w && length_y >= length_w, CeedVectorReturnCeed(w), CEED_ERROR_UNSUPPORTED,
880             "Cannot pointwise multiply vectors of incompatible lengths."
881             " w length: %" CeedSize_FMT " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT,
882             length_w, length_x, length_y);
883 
884   {
885     Ceed ceed_w, ceed_x, ceed_y, ceed_parent_w, ceed_parent_x, ceed_parent_y;
886 
887     CeedCall(CeedVectorGetCeed(w, &ceed_w));
888     CeedCall(CeedVectorGetCeed(x, &ceed_x));
889     CeedCall(CeedVectorGetCeed(y, &ceed_y));
890     CeedCall(CeedGetParent(ceed_w, &ceed_parent_w));
891     CeedCall(CeedGetParent(ceed_x, &ceed_parent_x));
892     CeedCall(CeedGetParent(ceed_y, &ceed_parent_y));
893     CeedCall(CeedDestroy(&ceed_w));
894     CeedCall(CeedDestroy(&ceed_x));
895     CeedCall(CeedDestroy(&ceed_y));
896     CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, CeedVectorReturnCeed(w), CEED_ERROR_INCOMPATIBLE,
897               "Vectors w, x, and y must be created by the same Ceed context");
898     CeedCall(CeedDestroy(&ceed_parent_w));
899     CeedCall(CeedDestroy(&ceed_parent_x));
900     CeedCall(CeedDestroy(&ceed_parent_y));
901   }
902 
903   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
904   CeedCheck(has_valid_array_x, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND,
905             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
906   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
907   CeedCheck(has_valid_array_y, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND,
908             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
909 
910   // Return early for empty vectors
911   if (length_w == 0) return CEED_ERROR_SUCCESS;
912 
913   // Backend implementation
914   if (w->PointwiseMult) {
915     CeedCall(w->PointwiseMult(w, x, y));
916     return CEED_ERROR_SUCCESS;
917   }
918 
919   // Default implementation
920   if (x == w || y == w) {
921     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
922   } else {
923     CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array));
924   }
925   if (x != w) {
926     CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
927   } else {
928     x_array = w_array;
929   }
930   if (y != w && y != x) {
931     CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array));
932   } else if (y == x) {
933     y_array = x_array;
934   } else if (y == w) {
935     y_array = w_array;
936   }
937 
938   assert(w_array);
939   assert(x_array);
940   assert(y_array);
941 
942   for (CeedSize i = 0; i < length_w; i++) w_array[i] = x_array[i] * y_array[i];
943 
944   if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array));
945   if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
946   CeedCall(CeedVectorRestoreArray(w, &w_array));
947   return CEED_ERROR_SUCCESS;
948 }
949 
950 /**
951   @brief Take the reciprocal of a `CeedVector`.
952 
953   @param[in,out] vec `CeedVector` to take reciprocal
954 
955   @return An error code: 0 - success, otherwise - failure
956 
957   @ref User
958 **/
959 int CeedVectorReciprocal(CeedVector vec) {
960   bool        has_valid_array = true;
961   CeedSize    length;
962   CeedScalar *array;
963 
964   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
965   CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
966             "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray");
967 
968   // Check if vector data set
969   CeedCheck(vec->state > 0, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal");
970 
971   // Return early for empty vector
972   CeedCall(CeedVectorGetLength(vec, &length));
973   if (length == 0) return CEED_ERROR_SUCCESS;
974 
975   // Backend impl for GPU, if added
976   if (vec->Reciprocal) {
977     CeedCall(vec->Reciprocal(vec));
978     return CEED_ERROR_SUCCESS;
979   }
980 
981   CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array));
982   for (CeedSize i = 0; i < length; i++) {
983     if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i];
984   }
985 
986   CeedCall(CeedVectorRestoreArray(vec, &array));
987   return CEED_ERROR_SUCCESS;
988 }
989 
990 /**
991   @brief View a `CeedVector`
992 
993   Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`.
994         Any portion of the provided range that is outside the range of valid indices for the `CeedVector` will be ignored.
995 
996   @param[in] vec    `CeedVector` to view
997   @param[in] start  Index of first `CeedVector` entry to view
998   @param[in] stop   Index of last `CeedVector` entry to view
999   @param[in] step   Step between `CeedVector` entries to view
1000   @param[in] fp_fmt Printing format
1001   @param[in] stream Filestream to write to
1002 
1003   @return An error code: 0 - success, otherwise - failure
1004 
1005   @ref User
1006 **/
1007 int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) {
1008   char              fmt[1024];
1009   CeedSize          length;
1010   const CeedScalar *x;
1011 
1012   CeedCheck(step != 0, CeedVectorReturnCeed(vec), CEED_ERROR_MINOR, "View range 'step' must be nonzero");
1013 
1014   CeedCall(CeedVectorGetLength(vec, &length));
1015   fprintf(stream, "CeedVector length %" CeedSize_FMT "\n", length);
1016   if (start != 0 || stop != length || step != 1) {
1017     fprintf(stream, "  start: %" CeedSize_FMT "\n  stop:  %" CeedSize_FMT "\n  step:  %" CeedInt_FMT "\n", start, stop, step);
1018   }
1019   if (start > length) start = length;
1020   if (stop > length) stop = length;
1021 
1022   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
1023   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x));
1024   for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]);
1025   CeedCall(CeedVectorRestoreArrayRead(vec, &x));
1026   if (stop != length) fprintf(stream, "  ...\n");
1027   return CEED_ERROR_SUCCESS;
1028 }
1029 
1030 /**
1031   @brief View a `CeedVector`
1032 
1033   @param[in] vec    `CeedVector` to view
1034   @param[in] fp_fmt Printing format
1035   @param[in] stream Filestream to write to
1036 
1037   @return An error code: 0 - success, otherwise - failure
1038 
1039   @ref User
1040 **/
1041 int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
1042   CeedSize length;
1043 
1044   CeedCall(CeedVectorGetLength(vec, &length));
1045   CeedCall(CeedVectorViewRange(vec, 0, length, 1, fp_fmt, stream));
1046   return CEED_ERROR_SUCCESS;
1047 }
1048 
1049 /**
1050   @brief Get the `Ceed` associated with a `CeedVector`
1051 
1052   @param[in]  vec  `CeedVector` to retrieve state
1053   @param[out] ceed Variable to store `Ceed`
1054 
1055   @return An error code: 0 - success, otherwise - failure
1056 
1057   @ref Advanced
1058 **/
1059 int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
1060   *ceed = NULL;
1061   CeedCall(CeedReferenceCopy(CeedVectorReturnCeed(vec), ceed));
1062   return CEED_ERROR_SUCCESS;
1063 }
1064 
1065 /**
1066   @brief Return the `Ceed` associated with a `CeedVector`
1067 
1068   @param[in]  vec  `CeedVector` to retrieve state
1069 
1070   @return `Ceed` associated with the `vec`
1071 
1072   @ref Advanced
1073 **/
1074 Ceed CeedVectorReturnCeed(CeedVector vec) { return vec->ceed; }
1075 
1076 /**
1077   @brief Get the length of a `CeedVector`
1078 
1079   @param[in]  vec    `CeedVector` to retrieve length
1080   @param[out] length Variable to store length
1081 
1082   @return An error code: 0 - success, otherwise - failure
1083 
1084   @ref User
1085 **/
1086 int CeedVectorGetLength(CeedVector vec, CeedSize *length) {
1087   *length = vec->length;
1088   return CEED_ERROR_SUCCESS;
1089 }
1090 
1091 /**
1092   @brief Destroy a `CeedVector`
1093 
1094   @param[in,out] vec `CeedVector` to destroy
1095 
1096   @return An error code: 0 - success, otherwise - failure
1097 
1098   @ref User
1099 **/
1100 int CeedVectorDestroy(CeedVector *vec) {
1101   if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) {
1102     *vec = NULL;
1103     return CEED_ERROR_SUCCESS;
1104   }
1105   CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use");
1106   CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access");
1107 
1108   if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec));
1109 
1110   CeedCall(CeedDestroy(&(*vec)->ceed));
1111   CeedCall(CeedFree(vec));
1112   return CEED_ERROR_SUCCESS;
1113 }
1114 
1115 /// @}
1116