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