xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-vector.c (revision b1a610ef203ae2e2bf8a9bddcc192eaf0a166984)
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  Last index to set in range `[start, stop)`, 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, "Invalid value for stop, must be in the range [-1, length]");
377 
378   if (vec->SetValueStrided) {
379     CeedCall(vec->SetValueStrided(vec, start, stop, step, value));
380     vec->state += 2;
381   } else {
382     CeedScalar *array;
383 
384     if (length <= 0) return CEED_ERROR_SUCCESS;
385     if (stop == -1) stop = length;
386     CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array));
387     for (CeedSize i = start; i < stop; i += step) array[i] = value;
388     CeedCall(CeedVectorRestoreArray(vec, &array));
389   }
390   return CEED_ERROR_SUCCESS;
391 }
392 
393 /**
394   @brief Sync the `CeedVector` to a specified `mem_type`.
395 
396   This function is used to force synchronization of arrays set with @ref CeedVectorSetArray().
397   If the requested `mem_type` is already synchronized, this function results in a no-op.
398 
399   @param[in,out] vec      `CeedVector`
400   @param[in]     mem_type @ref CeedMemType to be synced
401 
402   @return An error code: 0 - success, otherwise - failure
403 
404   @ref User
405 **/
406 int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
407   CeedSize length;
408 
409   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use");
410 
411   // Don't sync empty array
412   CeedCall(CeedVectorGetLength(vec, &length));
413   if (length == 0) return CEED_ERROR_SUCCESS;
414 
415   if (vec->SyncArray) {
416     CeedCall(vec->SyncArray(vec, mem_type));
417   } else {
418     const CeedScalar *array;
419 
420     CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array));
421     CeedCall(CeedVectorRestoreArrayRead(vec, &array));
422   }
423   return CEED_ERROR_SUCCESS;
424 }
425 
426 /**
427   @brief Take ownership of the `CeedVector` array set by @ref CeedVectorSetArray() with @ref CEED_USE_POINTER and remove the array from the `CeedVector`.
428 
429   The caller is responsible for managing and freeing the array.
430   This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type.
431 
432   @param[in,out] vec      `CeedVector`
433   @param[in]     mem_type Memory type on which to take the array.
434                             If the backend uses a different memory type, this will perform a copy.
435   @param[out]    array    Array on memory type `mem_type`, or `NULL` if array pointer is not required
436 
437   @return An error code: 0 - success, otherwise - failure
438 
439   @ref User
440 **/
441 int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
442   CeedSize    length;
443   CeedScalar *temp_array = NULL;
444 
445   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use");
446   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access");
447 
448   CeedCall(CeedVectorGetLength(vec, &length));
449   if (length > 0) {
450     bool has_borrowed_array_of_type = true, has_valid_array = true;
451 
452     CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type));
453     CeedCheck(has_borrowed_array_of_type, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
454               "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]);
455 
456     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
457     CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
458               "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray");
459 
460     CeedCall(vec->TakeArray(vec, mem_type, &temp_array));
461   }
462   if (array) (*array) = temp_array;
463   return CEED_ERROR_SUCCESS;
464 }
465 
466 /**
467   @brief Get read/write access to a `CeedVector` via the specified memory type.
468 
469   Restore access with @ref CeedVectorRestoreArray().
470 
471   @param[in,out] vec      `CeedVector` to access
472   @param[in]     mem_type Memory type on which to access the array.
473                             If the backend uses a different memory type, this will perform a copy.
474   @param[out]    array    Array on memory type `mem_type`
475 
476   @note The @ref CeedVectorGetArray() and @ref CeedVectorRestoreArray() functions provide access to array pointers in the desired memory space.
477         Pairing get/restore allows the `CeedVector` to track access, thus knowing if norms or other operations may need to be recomputed.
478 
479   @return An error code: 0 - success, otherwise - failure
480 
481   @ref User
482 **/
483 int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
484   CeedSize length;
485 
486   CeedCheck(vec->GetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray");
487   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
488             "Cannot grant CeedVector array access, the access lock is already in use");
489   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
490 
491   CeedCall(CeedVectorGetLength(vec, &length));
492   if (length > 0) {
493     bool has_valid_array = true;
494 
495     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
496     CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
497               "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
498 
499     CeedCall(vec->GetArray(vec, mem_type, array));
500   } else {
501     *array = NULL;
502   }
503   vec->state++;
504   return CEED_ERROR_SUCCESS;
505 }
506 
507 /**
508   @brief Get read-only access to a `CeedVector` via the specified memory type.
509 
510   Restore access with @ref CeedVectorRestoreArrayRead().
511 
512   @param[in]  vec      `CeedVector` to access
513   @param[in]  mem_type Memory type on which to access the array.
514                          If the backend uses a different memory type, this will perform a copy (possibly cached).
515   @param[out] array    Array on memory type `mem_type`
516 
517   @return An error code: 0 - success, otherwise - failure
518 
519   @ref User
520 **/
521 int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
522   CeedSize length;
523 
524   CeedCheck(vec->GetArrayRead, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead");
525   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
526             "Cannot grant CeedVector read-only array access, the access lock is already in use");
527 
528   CeedCall(CeedVectorGetLength(vec, &length));
529   if (length > 0) {
530     bool has_valid_array = true;
531 
532     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
533     CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
534               "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
535 
536     CeedCall(vec->GetArrayRead(vec, mem_type, array));
537   } else {
538     *array = NULL;
539   }
540   vec->num_readers++;
541   return CEED_ERROR_SUCCESS;
542 }
543 
544 /**
545   @brief Get write access to a `CeedVector` via the specified memory type.
546 
547   Restore access with @ref CeedVectorRestoreArray().
548   All old values should be assumed to be invalid.
549 
550   @param[in,out] vec      `CeedVector` to access
551   @param[in]     mem_type Memory type on which to access the array.
552   @param[out]    array    Array on memory type `mem_type`
553 
554   @return An error code: 0 - success, otherwise - failure
555 
556   @ref User
557 **/
558 int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
559   CeedSize length;
560 
561   CeedCheck(vec->GetArrayWrite, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite");
562   CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
563             "Cannot grant CeedVector array access, the access lock is already in use");
564   CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
565 
566   CeedCall(CeedVectorGetLength(vec, &length));
567   if (length > 0) {
568     CeedCall(vec->GetArrayWrite(vec, mem_type, array));
569   } else {
570     *array = NULL;
571   }
572   vec->state++;
573   return CEED_ERROR_SUCCESS;
574 }
575 
576 /**
577   @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite()
578 
579   @param[in,out] vec   `CeedVector` to restore
580   @param[in,out] array Array of vector data
581 
582   @return An error code: 0 - success, otherwise - failure
583 
584   @ref User
585 **/
586 int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
587   CeedSize length;
588 
589   CeedCheck(vec->state % 2 == 1, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted");
590   CeedCall(CeedVectorGetLength(vec, &length));
591   if (length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec));
592   *array = NULL;
593   vec->state++;
594   return CEED_ERROR_SUCCESS;
595 }
596 
597 /**
598   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
599 
600   @param[in]     vec   `CeedVector` to restore
601   @param[in,out] array Array of vector data
602 
603   @return An error code: 0 - success, otherwise - failure
604 
605   @ref User
606 **/
607 int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
608   CeedSize length;
609 
610   CeedCheck(vec->num_readers > 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS,
611             "Cannot restore CeedVector array read access, access was not granted");
612   vec->num_readers--;
613   CeedCall(CeedVectorGetLength(vec, &length));
614   if (length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec));
615   *array = NULL;
616   return CEED_ERROR_SUCCESS;
617 }
618 
619 /**
620   @brief Get the norm of a `CeedVector`.
621 
622   Note: This operation is local to the `CeedVector`.
623         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.
624 
625   @param[in]  vec       `CeedVector` to retrieve maximum value
626   @param[in]  norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
627   @param[out] norm      Variable to store norm value
628 
629   @return An error code: 0 - success, otherwise - failure
630 
631   @ref User
632 **/
633 int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
634   bool     has_valid_array = true;
635   CeedSize length;
636 
637   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
638   CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
639             "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray");
640 
641   CeedCall(CeedVectorGetLength(vec, &length));
642   if (length == 0) {
643     *norm = 0;
644     return CEED_ERROR_SUCCESS;
645   }
646 
647   // Backend impl for GPU, if added
648   if (vec->Norm) {
649     CeedCall(vec->Norm(vec, norm_type, norm));
650     return CEED_ERROR_SUCCESS;
651   }
652 
653   const CeedScalar *array;
654   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array));
655   assert(array);
656 
657   *norm = 0.;
658   switch (norm_type) {
659     case CEED_NORM_1:
660       for (CeedSize i = 0; i < length; i++) {
661         *norm += fabs(array[i]);
662       }
663       break;
664     case CEED_NORM_2:
665       for (CeedSize i = 0; i < length; i++) {
666         *norm += fabs(array[i]) * fabs(array[i]);
667       }
668       break;
669     case CEED_NORM_MAX:
670       for (CeedSize i = 0; i < length; i++) {
671         const CeedScalar abs_v_i = fabs(array[i]);
672         *norm                    = *norm > abs_v_i ? *norm : abs_v_i;
673       }
674   }
675   if (norm_type == CEED_NORM_2) *norm = sqrt(*norm);
676 
677   CeedCall(CeedVectorRestoreArrayRead(vec, &array));
678   return CEED_ERROR_SUCCESS;
679 }
680 
681 /**
682   @brief Compute `x = alpha x`
683 
684   @param[in,out] x     `CeedVector` for scaling
685   @param[in]     alpha scaling factor
686 
687   @return An error code: 0 - success, otherwise - failure
688 
689   @ref User
690 **/
691 int CeedVectorScale(CeedVector x, CeedScalar alpha) {
692   bool        has_valid_array = true;
693   CeedSize    length;
694   CeedScalar *x_array = NULL;
695 
696   CeedCall(CeedVectorHasValidArray(x, &has_valid_array));
697   CeedCheck(has_valid_array, CeedVectorReturnCeed(x), CEED_ERROR_BACKEND,
698             "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray");
699 
700   // Return early for empty vector
701   CeedCall(CeedVectorGetLength(x, &length));
702   if (length == 0) return CEED_ERROR_SUCCESS;
703 
704   // Backend implementation
705   if (x->Scale) return x->Scale(x, alpha);
706 
707   // Default implementation
708   CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array));
709   assert(x_array);
710   for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha;
711   CeedCall(CeedVectorRestoreArray(x, &x_array));
712   return CEED_ERROR_SUCCESS;
713 }
714 
715 /**
716   @brief Compute `y = alpha x + y`
717 
718   @param[in,out] y     target `CeedVector` for sum
719   @param[in]     alpha scaling factor
720   @param[in]     x     second `CeedVector`, must be different than ``y`
721 
722   @return An error code: 0 - success, otherwise - failure
723 
724   @ref User
725 **/
726 int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
727   bool              has_valid_array_x = true, has_valid_array_y = true;
728   CeedSize          length_x, length_y;
729   CeedScalar       *y_array = NULL;
730   CeedScalar const *x_array = NULL;
731 
732   CeedCall(CeedVectorGetLength(y, &length_y));
733   CeedCall(CeedVectorGetLength(x, &length_x));
734   CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED,
735             "Cannot add vector of different lengths."
736             " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT,
737             length_x, length_y);
738   CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY");
739 
740   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
741   CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND,
742             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
743   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
744   CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND,
745             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
746 
747   {
748     Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y;
749 
750     CeedCall(CeedVectorGetCeed(y, &ceed_y));
751     CeedCall(CeedVectorGetCeed(x, &ceed_x));
752     CeedCall(CeedGetParent(ceed_x, &ceed_parent_x));
753     CeedCall(CeedGetParent(ceed_y, &ceed_parent_y));
754     CeedCall(CeedDestroy(&ceed_x));
755     CeedCall(CeedDestroy(&ceed_y));
756     CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE,
757               "Vectors x and y must be created by the same Ceed context");
758     CeedCall(CeedDestroy(&ceed_parent_x));
759     CeedCall(CeedDestroy(&ceed_parent_y));
760   }
761 
762   // Return early for empty vectors
763   if (length_y == 0) return CEED_ERROR_SUCCESS;
764 
765   // Backend implementation
766   if (y->AXPY) {
767     CeedCall(y->AXPY(y, alpha, x));
768     return CEED_ERROR_SUCCESS;
769   }
770 
771   // Default implementation
772   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
773   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
774 
775   assert(x_array);
776   assert(y_array);
777 
778   for (CeedSize i = 0; i < length_y; i++) y_array[i] += alpha * x_array[i];
779 
780   CeedCall(CeedVectorRestoreArray(y, &y_array));
781   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
782   return CEED_ERROR_SUCCESS;
783 }
784 
785 /**
786   @brief Compute `y = alpha x + beta y`
787 
788   @param[in,out] y     target `CeedVector` for sum
789   @param[in]     alpha first scaling factor
790   @param[in]     beta  second scaling factor
791   @param[in]     x     second `CeedVector`, must be different than `y`
792 
793   @return An error code: 0 - success, otherwise - failure
794 
795   @ref User
796 **/
797 int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
798   bool              has_valid_array_x = true, has_valid_array_y = true;
799   CeedSize          length_x, length_y;
800   CeedScalar       *y_array = NULL;
801   CeedScalar const *x_array = NULL;
802 
803   CeedCall(CeedVectorGetLength(y, &length_y));
804   CeedCall(CeedVectorGetLength(x, &length_x));
805   CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED,
806             "Cannot add vector of different lengths."
807             " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT,
808             length_x, length_y);
809   CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY");
810 
811   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
812   CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND,
813             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
814   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
815   CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND,
816             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
817 
818   {
819     Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y;
820 
821     CeedCall(CeedVectorGetCeed(y, &ceed_y));
822     CeedCall(CeedVectorGetCeed(x, &ceed_x));
823     CeedCall(CeedGetParent(ceed_x, &ceed_parent_x));
824     CeedCall(CeedGetParent(ceed_y, &ceed_parent_y));
825     CeedCall(CeedDestroy(&ceed_x));
826     CeedCall(CeedDestroy(&ceed_y));
827     CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE,
828               "Vectors x and y must be created by the same Ceed context");
829     CeedCall(CeedDestroy(&ceed_parent_x));
830     CeedCall(CeedDestroy(&ceed_parent_y));
831   }
832 
833   // Return early for empty vectors
834   if (length_y == 0) return CEED_ERROR_SUCCESS;
835 
836   // Backend implementation
837   if (y->AXPBY) {
838     CeedCall(y->AXPBY(y, alpha, beta, x));
839     return CEED_ERROR_SUCCESS;
840   }
841 
842   // Default implementation
843   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
844   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
845 
846   assert(x_array);
847   assert(y_array);
848 
849   for (CeedSize i = 0; i < length_y; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i];
850 
851   CeedCall(CeedVectorRestoreArray(y, &y_array));
852   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
853   return CEED_ERROR_SUCCESS;
854 }
855 
856 /**
857   @brief Compute the pointwise multiplication \f$w = x .* y\f$.
858 
859   Any subset of `x`, `y`, and `w` may be the same `CeedVector`.
860 
861   @param[out] w target `CeedVector` for the product
862   @param[in]  x first `CeedVector` for product
863   @param[in]  y second `CeedVector` for the product
864 
865   @return An error code: 0 - success, otherwise - failure
866 
867   @ref User
868 **/
869 int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
870   bool              has_valid_array_x = true, has_valid_array_y = true;
871   CeedScalar       *w_array = NULL;
872   CeedScalar const *x_array = NULL, *y_array = NULL;
873   CeedSize          length_w, length_x, length_y;
874 
875   CeedCall(CeedVectorGetLength(w, &length_w));
876   CeedCall(CeedVectorGetLength(x, &length_x));
877   CeedCall(CeedVectorGetLength(y, &length_y));
878   CeedCheck(length_x >= length_w && length_y >= length_w, CeedVectorReturnCeed(w), CEED_ERROR_UNSUPPORTED,
879             "Cannot pointwise multiply vectors of incompatible lengths."
880             " w length: %" CeedSize_FMT " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT,
881             length_w, length_x, length_y);
882 
883   {
884     Ceed ceed_w, ceed_x, ceed_y, ceed_parent_w, ceed_parent_x, ceed_parent_y;
885 
886     CeedCall(CeedVectorGetCeed(w, &ceed_w));
887     CeedCall(CeedVectorGetCeed(x, &ceed_x));
888     CeedCall(CeedVectorGetCeed(y, &ceed_y));
889     CeedCall(CeedGetParent(ceed_w, &ceed_parent_w));
890     CeedCall(CeedGetParent(ceed_x, &ceed_parent_x));
891     CeedCall(CeedGetParent(ceed_y, &ceed_parent_y));
892     CeedCall(CeedDestroy(&ceed_w));
893     CeedCall(CeedDestroy(&ceed_x));
894     CeedCall(CeedDestroy(&ceed_y));
895     CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, CeedVectorReturnCeed(w), CEED_ERROR_INCOMPATIBLE,
896               "Vectors w, x, and y must be created by the same Ceed context");
897     CeedCall(CeedDestroy(&ceed_parent_w));
898     CeedCall(CeedDestroy(&ceed_parent_x));
899     CeedCall(CeedDestroy(&ceed_parent_y));
900   }
901 
902   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
903   CeedCheck(has_valid_array_x, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND,
904             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
905   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
906   CeedCheck(has_valid_array_y, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND,
907             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
908 
909   // Return early for empty vectors
910   if (length_w == 0) return CEED_ERROR_SUCCESS;
911 
912   // Backend implementation
913   if (w->PointwiseMult) {
914     CeedCall(w->PointwiseMult(w, x, y));
915     return CEED_ERROR_SUCCESS;
916   }
917 
918   // Default implementation
919   if (x == w || y == w) {
920     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
921   } else {
922     CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array));
923   }
924   if (x != w) {
925     CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
926   } else {
927     x_array = w_array;
928   }
929   if (y != w && y != x) {
930     CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array));
931   } else if (y == x) {
932     y_array = x_array;
933   } else if (y == w) {
934     y_array = w_array;
935   }
936 
937   assert(w_array);
938   assert(x_array);
939   assert(y_array);
940 
941   for (CeedSize i = 0; i < length_w; i++) w_array[i] = x_array[i] * y_array[i];
942 
943   if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array));
944   if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
945   CeedCall(CeedVectorRestoreArray(w, &w_array));
946   return CEED_ERROR_SUCCESS;
947 }
948 
949 /**
950   @brief Take the reciprocal of a `CeedVector`.
951 
952   @param[in,out] vec `CeedVector` to take reciprocal
953 
954   @return An error code: 0 - success, otherwise - failure
955 
956   @ref User
957 **/
958 int CeedVectorReciprocal(CeedVector vec) {
959   bool        has_valid_array = true;
960   CeedSize    length;
961   CeedScalar *array;
962 
963   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
964   CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND,
965             "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray");
966 
967   // Check if vector data set
968   CeedCheck(vec->state > 0, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal");
969 
970   // Return early for empty vector
971   CeedCall(CeedVectorGetLength(vec, &length));
972   if (length == 0) return CEED_ERROR_SUCCESS;
973 
974   // Backend impl for GPU, if added
975   if (vec->Reciprocal) {
976     CeedCall(vec->Reciprocal(vec));
977     return CEED_ERROR_SUCCESS;
978   }
979 
980   CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array));
981   for (CeedSize i = 0; i < length; i++) {
982     if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i];
983   }
984 
985   CeedCall(CeedVectorRestoreArray(vec, &array));
986   return CEED_ERROR_SUCCESS;
987 }
988 
989 /**
990   @brief View a `CeedVector`
991 
992   Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`.
993         Any portion of the provided range that is outside the range of valid indices for the `CeedVector` will be ignored.
994 
995   @param[in] vec    `CeedVector` to view
996   @param[in] start  Index of first `CeedVector` entry to view
997   @param[in] stop   Index of last `CeedVector` entry to view
998   @param[in] step   Step between `CeedVector` entries to view
999   @param[in] fp_fmt Printing format
1000   @param[in] stream Filestream to write to
1001 
1002   @return An error code: 0 - success, otherwise - failure
1003 
1004   @ref User
1005 **/
1006 int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) {
1007   char              fmt[1024];
1008   CeedSize          length;
1009   const CeedScalar *x;
1010 
1011   CeedCheck(step != 0, CeedVectorReturnCeed(vec), CEED_ERROR_MINOR, "View range 'step' must be nonzero");
1012 
1013   CeedCall(CeedVectorGetLength(vec, &length));
1014   fprintf(stream, "CeedVector length %" CeedSize_FMT "\n", length);
1015   if (start != 0 || stop != length || step != 1) {
1016     fprintf(stream, "  start: %" CeedSize_FMT "\n  stop:  %" CeedSize_FMT "\n  step:  %" CeedInt_FMT "\n", start, stop, step);
1017   }
1018   if (start > length) start = length;
1019   if (stop > length) stop = length;
1020 
1021   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
1022   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x));
1023   for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]);
1024   CeedCall(CeedVectorRestoreArrayRead(vec, &x));
1025   if (stop != length) fprintf(stream, "  ...\n");
1026   return CEED_ERROR_SUCCESS;
1027 }
1028 
1029 /**
1030   @brief View a `CeedVector`
1031 
1032   @param[in] vec    `CeedVector` to view
1033   @param[in] fp_fmt Printing format
1034   @param[in] stream Filestream to write to
1035 
1036   @return An error code: 0 - success, otherwise - failure
1037 
1038   @ref User
1039 **/
1040 int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
1041   CeedSize length;
1042 
1043   CeedCall(CeedVectorGetLength(vec, &length));
1044   CeedCall(CeedVectorViewRange(vec, 0, length, 1, fp_fmt, stream));
1045   return CEED_ERROR_SUCCESS;
1046 }
1047 
1048 /**
1049   @brief Get the `Ceed` associated with a `CeedVector`
1050 
1051   @param[in]  vec  `CeedVector` to retrieve state
1052   @param[out] ceed Variable to store `Ceed`
1053 
1054   @return An error code: 0 - success, otherwise - failure
1055 
1056   @ref Advanced
1057 **/
1058 int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
1059   *ceed = NULL;
1060   CeedCall(CeedReferenceCopy(CeedVectorReturnCeed(vec), ceed));
1061   return CEED_ERROR_SUCCESS;
1062 }
1063 
1064 /**
1065   @brief Return the `Ceed` associated with a `CeedVector`
1066 
1067   @param[in]  vec  `CeedVector` to retrieve state
1068 
1069   @return `Ceed` associated with the `vec`
1070 
1071   @ref Advanced
1072 **/
1073 Ceed CeedVectorReturnCeed(CeedVector vec) { return vec->ceed; }
1074 
1075 /**
1076   @brief Get the length of a `CeedVector`
1077 
1078   @param[in]  vec    `CeedVector` to retrieve length
1079   @param[out] length Variable to store length
1080 
1081   @return An error code: 0 - success, otherwise - failure
1082 
1083   @ref User
1084 **/
1085 int CeedVectorGetLength(CeedVector vec, CeedSize *length) {
1086   *length = vec->length;
1087   return CEED_ERROR_SUCCESS;
1088 }
1089 
1090 /**
1091   @brief Destroy a `CeedVector`
1092 
1093   @param[in,out] vec `CeedVector` to destroy
1094 
1095   @return An error code: 0 - success, otherwise - failure
1096 
1097   @ref User
1098 **/
1099 int CeedVectorDestroy(CeedVector *vec) {
1100   if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) {
1101     *vec = NULL;
1102     return CEED_ERROR_SUCCESS;
1103   }
1104   CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use");
1105   CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access");
1106 
1107   if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec));
1108 
1109   CeedCall(CeedDestroy(&(*vec)->ceed));
1110   CeedCall(CeedFree(vec));
1111   return CEED_ERROR_SUCCESS;
1112 }
1113 
1114 /// @}
1115