1 // Copyright (c) 2017-2022, 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 <assert.h> 9 #include <ceed-impl.h> 10 #include <ceed/backend.h> 11 #include <ceed/ceed.h> 12 #include <math.h> 13 #include <stdint.h> 14 #include <stdio.h> 15 16 /// @file 17 /// Implementation of public CeedVector interfaces 18 19 /// @cond DOXYGEN_SKIP 20 static struct CeedVector_private ceed_vector_active; 21 static struct CeedVector_private ceed_vector_none; 22 /// @endcond 23 24 /// @addtogroup CeedVectorUser 25 /// @{ 26 27 /// Indicate that vector will be provided as an explicit argument to CeedOperatorApply(). 28 const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active; 29 30 /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHT). 31 const CeedVector CEED_VECTOR_NONE = &ceed_vector_none; 32 33 /// @} 34 35 /// ---------------------------------------------------------------------------- 36 /// CeedVector Backend API 37 /// ---------------------------------------------------------------------------- 38 /// @addtogroup CeedVectorBackend 39 /// @{ 40 41 /** 42 @brief Check for valid data in a CeedVector 43 44 @param[in] vec CeedVector to check validity 45 @param[out] has_valid_array Variable to store validity 46 47 @return An error code: 0 - success, otherwise - failure 48 49 @ref Backend 50 **/ 51 int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) { 52 if (!vec->HasValidArray) { 53 // LCOV_EXCL_START 54 return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasValidArray"); 55 // LCOV_EXCL_STOP 56 } 57 58 CeedCall(vec->HasValidArray(vec, has_valid_array)); 59 60 return CEED_ERROR_SUCCESS; 61 } 62 63 /** 64 @brief Check for borrowed array of a specific CeedMemType in a CeedVector 65 66 @param[in] vec CeedVector to check 67 @param[in] mem_type Memory type to check 68 @param[out] has_borrowed_array_of_type Variable to store result 69 70 @return An error code: 0 - success, otherwise - failure 71 72 @ref Backend 73 **/ 74 int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 75 if (!vec->HasBorrowedArrayOfType) { 76 // LCOV_EXCL_START 77 return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedArrayOfType"); 78 // LCOV_EXCL_STOP 79 } 80 81 CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type)); 82 83 return CEED_ERROR_SUCCESS; 84 } 85 86 /** 87 @brief Get the state of a CeedVector 88 89 @param[in] vec CeedVector to retrieve state 90 @param[out] state Variable to store state 91 92 @return An error code: 0 - success, otherwise - failure 93 94 @ref Backend 95 **/ 96 int CeedVectorGetState(CeedVector vec, uint64_t *state) { 97 *state = vec->state; 98 return CEED_ERROR_SUCCESS; 99 } 100 101 /** 102 @brief Add a reference to a CeedVector 103 104 @param[in,out] vec CeedVector to increment reference counter 105 106 @return An error code: 0 - success, otherwise - failure 107 108 @ref Backend 109 **/ 110 int CeedVectorAddReference(CeedVector vec) { 111 vec->ref_count++; 112 return CEED_ERROR_SUCCESS; 113 } 114 115 /** 116 @brief Get the backend data of a CeedVector 117 118 @param[in] vec CeedVector to retrieve state 119 @param[out] data Variable to store data 120 121 @return An error code: 0 - success, otherwise - failure 122 123 @ref Backend 124 **/ 125 int CeedVectorGetData(CeedVector vec, void *data) { 126 *(void **)data = vec->data; 127 return CEED_ERROR_SUCCESS; 128 } 129 130 /** 131 @brief Set the backend data of a CeedVector 132 133 @param[in,out] vec CeedVector to retrieve state 134 @param[in] data Data to set 135 136 @return An error code: 0 - success, otherwise - failure 137 138 @ref Backend 139 **/ 140 int CeedVectorSetData(CeedVector vec, void *data) { 141 vec->data = data; 142 return CEED_ERROR_SUCCESS; 143 } 144 145 /** 146 @brief Increment the reference counter for a CeedVector 147 148 @param[in,out] vec CeedVector to increment the reference counter 149 150 @return An error code: 0 - success, otherwise - failure 151 152 @ref Backend 153 **/ 154 int CeedVectorReference(CeedVector vec) { 155 vec->ref_count++; 156 return CEED_ERROR_SUCCESS; 157 } 158 159 /// @} 160 161 /// ---------------------------------------------------------------------------- 162 /// CeedVector Public API 163 /// ---------------------------------------------------------------------------- 164 /// @addtogroup CeedVectorUser 165 /// @{ 166 167 /** 168 @brief Create a CeedVector of the specified length (does not allocate memory) 169 170 @param[in] ceed Ceed object where the CeedVector will be created 171 @param[in] length Length of vector 172 @param[out] vec Address of the variable where the newly created CeedVector will be stored 173 174 @return An error code: 0 - success, otherwise - failure 175 176 @ref User 177 **/ 178 int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { 179 if (!ceed->VectorCreate) { 180 Ceed delegate; 181 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); 182 183 if (!delegate) { 184 // LCOV_EXCL_START 185 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorCreate"); 186 // LCOV_EXCL_STOP 187 } 188 189 CeedCall(CeedVectorCreate(delegate, length, vec)); 190 return CEED_ERROR_SUCCESS; 191 } 192 193 CeedCall(CeedCalloc(1, vec)); 194 (*vec)->ceed = ceed; 195 CeedCall(CeedReference(ceed)); 196 (*vec)->ref_count = 1; 197 (*vec)->length = length; 198 (*vec)->state = 0; 199 CeedCall(ceed->VectorCreate(length, *vec)); 200 return CEED_ERROR_SUCCESS; 201 } 202 203 /** 204 @brief Copy the pointer to a CeedVector. 205 Both pointers should be destroyed with `CeedVectorDestroy()`. 206 Note: If `*vec_copy` is non-NULL, then it is assumed that `*vec_copy` is a pointer to a CeedVector. 207 This CeedVector will be destroyed if `*vec_copy` is the only reference to this CeedVector. 208 209 @param[in] vec CeedVector to copy reference to 210 @param[in,out] vec_copy Variable to store copied reference 211 212 @return An error code: 0 - success, otherwise - failure 213 214 @ref User 215 **/ 216 int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 217 CeedCall(CeedVectorReference(vec)); 218 CeedCall(CeedVectorDestroy(vec_copy)); 219 *vec_copy = vec; 220 return CEED_ERROR_SUCCESS; 221 } 222 223 /** 224 @brief Set the array used by a CeedVector, freeing any previously allocated array if applicable. 225 The backend may copy values to a different memtype, such as during @ref CeedOperatorApply(). 226 See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 227 228 @param[in,out] vec CeedVector 229 @param[in] mem_type Memory type of the array being passed 230 @param[in] copy_mode Copy mode for the array 231 @param[in] array Array to be used, or NULL with @ref CEED_COPY_VALUES to have the library allocate 232 233 @return An error code: 0 - success, otherwise - failure 234 235 @ref User 236 **/ 237 int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 238 if (!vec->SetArray) { 239 // LCOV_EXCL_START 240 return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 241 // LCOV_EXCL_STOP 242 } 243 if (vec->state % 2 == 1) { 244 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 245 } 246 if (vec->num_readers > 0) { 247 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 248 } 249 250 CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 251 vec->state += 2; 252 return CEED_ERROR_SUCCESS; 253 } 254 255 /** 256 @brief Set the CeedVector to a constant value 257 258 @param[in,out] vec CeedVector 259 @param[in] value Value to be used 260 261 @return An error code: 0 - success, otherwise - failure 262 263 @ref User 264 **/ 265 int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 266 if (vec->state % 2 == 1) { 267 // LCOV_EXCL_START 268 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 269 // LCOV_EXCL_STOP 270 } 271 if (vec->num_readers > 0) { 272 // LCOV_EXCL_START 273 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 274 // LCOV_EXCL_STOP 275 } 276 277 if (vec->SetValue) { 278 CeedCall(vec->SetValue(vec, value)); 279 } else { 280 CeedScalar *array; 281 CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 282 for (CeedInt i = 0; i < vec->length; i++) array[i] = value; 283 CeedCall(CeedVectorRestoreArray(vec, &array)); 284 } 285 vec->state += 2; 286 return CEED_ERROR_SUCCESS; 287 } 288 289 /** 290 @brief Sync the CeedVector to a specified memtype. 291 This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 292 If the requested memtype is already synchronized, this function results in a no-op. 293 294 @param[in,out] vec CeedVector 295 @param[in] mem_type Memtype to be synced 296 297 @return An error code: 0 - success, otherwise - failure 298 299 @ref User 300 **/ 301 int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 302 if (vec->state % 2 == 1) { 303 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 304 } 305 306 if (vec->SyncArray) { 307 CeedCall(vec->SyncArray(vec, mem_type)); 308 } else { 309 const CeedScalar *array; 310 CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 311 CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 312 } 313 return CEED_ERROR_SUCCESS; 314 } 315 316 /** 317 @brief Take ownership of the CeedVector array set by @ref CeedVectorSetArray() with @ref CEED_USE_POINTER and remove the array from the CeedVector. 318 The caller is responsible for managing and freeing the array. 319 This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 320 321 @param[in,out] vec CeedVector 322 @param[in] mem_type Memory type on which to take the array. 323 If the backend uses a different memory type, this will perform a copy. 324 @param[out] array Array on memory type mem_type, or NULL if array pointer is not required 325 326 @return An error code: 0 - success, otherwise - failure 327 328 @ref User 329 **/ 330 int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 331 if (vec->state % 2 == 1) { 332 // LCOV_EXCL_START 333 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 334 // LCOV_EXCL_STOP 335 } 336 if (vec->num_readers > 0) { 337 // LCOV_EXCL_START 338 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 339 // LCOV_EXCL_STOP 340 } 341 342 CeedScalar *temp_array = NULL; 343 if (vec->length > 0) { 344 bool has_borrowed_array_of_type = true; 345 CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 346 if (!has_borrowed_array_of_type) { 347 // LCOV_EXCL_START 348 return CeedError(vec->ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", 349 CeedMemTypes[mem_type]); 350 // LCOV_EXCL_STOP 351 } 352 353 bool has_valid_array = true; 354 CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 355 if (!has_valid_array) { 356 // LCOV_EXCL_START 357 return CeedError(vec->ceed, CEED_ERROR_BACKEND, 358 "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 359 // LCOV_EXCL_STOP 360 } 361 362 CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 363 } 364 if (array) (*array) = temp_array; 365 return CEED_ERROR_SUCCESS; 366 } 367 368 /** 369 @brief Get read/write access to a CeedVector via the specified memory type. 370 Restore access with @ref CeedVectorRestoreArray(). 371 372 @param[in,out] vec CeedVector to access 373 @param[in] mem_type Memory type on which to access the array. 374 If the backend uses a different memory type, this will perform a copy. 375 @param[out] array Array on memory type mem_type 376 377 @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space. 378 Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed. 379 380 @return An error code: 0 - success, otherwise - failure 381 382 @ref User 383 **/ 384 int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 385 if (!vec->GetArray) { 386 // LCOV_EXCL_START 387 return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 388 // LCOV_EXCL_STOP 389 } 390 if (vec->state % 2 == 1) { 391 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 392 } 393 if (vec->num_readers > 0) { 394 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 395 } 396 397 bool has_valid_array = true; 398 CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 399 if (!has_valid_array) { 400 // LCOV_EXCL_START 401 return CeedError(vec->ceed, CEED_ERROR_BACKEND, 402 "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 403 // LCOV_EXCL_STOP 404 } 405 406 CeedCall(vec->GetArray(vec, mem_type, array)); 407 vec->state++; 408 return CEED_ERROR_SUCCESS; 409 } 410 411 /** 412 @brief Get read-only access to a CeedVector via the specified memory type. 413 Restore access with @ref CeedVectorRestoreArrayRead(). 414 415 @param[in] vec CeedVector to access 416 @param[in] mem_type Memory type on which to access the array. 417 If the backend uses a different memory type, this will perform a copy (possibly cached). 418 @param[out] array Array on memory type mem_type 419 420 @return An error code: 0 - success, otherwise - failure 421 422 @ref User 423 **/ 424 int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 425 if (!vec->GetArrayRead) { 426 // LCOV_EXCL_START 427 return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 428 // LCOV_EXCL_STOP 429 } 430 if (vec->state % 2 == 1) { 431 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use"); 432 } 433 434 if (vec->length > 0) { 435 bool has_valid_array = true; 436 CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 437 if (!has_valid_array) { 438 // LCOV_EXCL_START 439 return CeedError(vec->ceed, CEED_ERROR_BACKEND, 440 "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 441 // LCOV_EXCL_STOP 442 } 443 444 CeedCall(vec->GetArrayRead(vec, mem_type, array)); 445 } else { 446 *array = NULL; 447 } 448 vec->num_readers++; 449 return CEED_ERROR_SUCCESS; 450 } 451 452 /** 453 @brief Get write access to a CeedVector via the specified memory type. 454 Restore access with @ref CeedVectorRestoreArray(). 455 All old values should be assumed to be invalid. 456 457 @param[in,out] vec CeedVector to access 458 @param[in] mem_type Memory type on which to access the array. 459 @param[out] array Array on memory type mem_type 460 461 @return An error code: 0 - success, otherwise - failure 462 463 @ref User 464 **/ 465 int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 466 if (!vec->GetArrayWrite) { 467 // LCOV_EXCL_START 468 return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite"); 469 // LCOV_EXCL_STOP 470 } 471 if (vec->state % 2 == 1) { 472 // LCOV_EXCL_START 473 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 474 // LCOV_EXCL_STOP 475 } 476 if (vec->num_readers > 0) { 477 // LCOV_EXCL_START 478 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 479 // LCOV_EXCL_STOP 480 } 481 482 CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 483 vec->state++; 484 return CEED_ERROR_SUCCESS; 485 } 486 487 /** 488 @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 489 490 @param[in,out] vec CeedVector to restore 491 @param[in,out] array Array of vector data 492 493 @return An error code: 0 - success, otherwise - failure 494 495 @ref User 496 **/ 497 int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 498 if (vec->state % 2 != 1) { 499 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 500 } 501 if (vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 502 *array = NULL; 503 vec->state++; 504 return CEED_ERROR_SUCCESS; 505 } 506 507 /** 508 @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 509 510 @param[in] vec CeedVector to restore 511 @param[in,out] array Array of vector data 512 513 @return An error code: 0 - success, otherwise - failure 514 515 @ref User 516 **/ 517 int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 518 if (vec->num_readers == 0) { 519 // LCOV_EXCL_START 520 return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted"); 521 // LCOV_EXCL_STOP 522 } 523 524 vec->num_readers--; 525 if (vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 526 *array = NULL; 527 528 return CEED_ERROR_SUCCESS; 529 } 530 531 /** 532 @brief Get the norm of a CeedVector. 533 534 Note: This operation is local to the CeedVector. 535 This function will likely not provide the desired results for the norm of the libCEED portion of a parallel vector or a CeedVector with 536 duplicated or hanging nodes. 537 538 @param[in] vec CeedVector to retrieve maximum value 539 @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 540 @param[out] norm Variable to store norm value 541 542 @return An error code: 0 - success, otherwise - failure 543 544 @ref User 545 **/ 546 int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 547 bool has_valid_array = true; 548 CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 549 if (!has_valid_array) { 550 // LCOV_EXCL_START 551 return CeedError(vec->ceed, CEED_ERROR_BACKEND, 552 "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 553 // LCOV_EXCL_STOP 554 } 555 556 // Backend impl for GPU, if added 557 if (vec->Norm) { 558 CeedCall(vec->Norm(vec, norm_type, norm)); 559 return CEED_ERROR_SUCCESS; 560 } 561 562 const CeedScalar *array; 563 CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 564 565 *norm = 0.; 566 switch (norm_type) { 567 case CEED_NORM_1: 568 for (CeedInt i = 0; i < vec->length; i++) { 569 *norm += fabs(array[i]); 570 } 571 break; 572 case CEED_NORM_2: 573 for (CeedInt i = 0; i < vec->length; i++) { 574 *norm += fabs(array[i]) * fabs(array[i]); 575 } 576 break; 577 case CEED_NORM_MAX: 578 for (CeedInt i = 0; i < vec->length; i++) { 579 const CeedScalar abs_v_i = fabs(array[i]); 580 *norm = *norm > abs_v_i ? *norm : abs_v_i; 581 } 582 } 583 if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 584 585 CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 586 return CEED_ERROR_SUCCESS; 587 } 588 589 /** 590 @brief Compute x = alpha x 591 592 @param[in,out] x vector for scaling 593 @param[in] alpha scaling factor 594 595 @return An error code: 0 - success, otherwise - failure 596 597 @ref User 598 **/ 599 int CeedVectorScale(CeedVector x, CeedScalar alpha) { 600 CeedScalar *x_array = NULL; 601 CeedSize n_x; 602 603 bool has_valid_array = true; 604 CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 605 if (!has_valid_array) { 606 // LCOV_EXCL_START 607 return CeedError(x->ceed, CEED_ERROR_BACKEND, 608 "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 609 // LCOV_EXCL_STOP 610 } 611 612 CeedCall(CeedVectorGetLength(x, &n_x)); 613 614 // Backend implementation 615 if (x->Scale) return x->Scale(x, alpha); 616 617 // Default implementation 618 CeedCall(CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array)); 619 for (CeedInt i = 0; i < n_x; i++) x_array[i] *= alpha; 620 CeedCall(CeedVectorRestoreArray(x, &x_array)); 621 622 return CEED_ERROR_SUCCESS; 623 } 624 625 /** 626 @brief Compute y = alpha x + y 627 628 @param[in,out] y target vector for sum 629 @param[in] alpha scaling factor 630 @param[in] x second vector, must be different than y 631 632 @return An error code: 0 - success, otherwise - failure 633 634 @ref User 635 **/ 636 int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 637 CeedScalar *y_array = NULL; 638 CeedScalar const *x_array = NULL; 639 CeedSize n_x, n_y; 640 641 CeedCall(CeedVectorGetLength(y, &n_y)); 642 CeedCall(CeedVectorGetLength(x, &n_x)); 643 if (n_x != n_y) { 644 // LCOV_EXCL_START 645 return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 646 // LCOV_EXCL_STOP 647 } 648 if (x == y) { 649 // LCOV_EXCL_START 650 return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 651 // LCOV_EXCL_STOP 652 } 653 654 bool has_valid_array_x = true, has_valid_array_y = true; 655 CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 656 if (!has_valid_array_x) { 657 // LCOV_EXCL_START 658 return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 659 // LCOV_EXCL_STOP 660 } 661 CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 662 if (!has_valid_array_y) { 663 // LCOV_EXCL_START 664 return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 665 // LCOV_EXCL_STOP 666 } 667 668 Ceed ceed_parent_x, ceed_parent_y; 669 CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 670 CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 671 if (ceed_parent_x != ceed_parent_y) { 672 // LCOV_EXCL_START 673 return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 674 // LCOV_EXCL_STOP 675 } 676 677 // Backend implementation 678 if (y->AXPY) { 679 CeedCall(y->AXPY(y, alpha, x)); 680 return CEED_ERROR_SUCCESS; 681 } 682 683 // Default implementation 684 CeedCall(CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array)); 685 CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 686 687 assert(x_array); 688 assert(y_array); 689 690 for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i]; 691 692 CeedCall(CeedVectorRestoreArray(y, &y_array)); 693 CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 694 695 return CEED_ERROR_SUCCESS; 696 } 697 698 /** 699 @brief Compute the pointwise multiplication w = x .* y. 700 Any subset of x, y, and w may be the same vector. 701 702 @param[out] w target vector for the product 703 @param[in] x first vector for product 704 @param[in] y second vector for the product 705 706 @return An error code: 0 - success, otherwise - failure 707 708 @ ref User 709 **/ 710 int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 711 CeedScalar *w_array = NULL; 712 CeedScalar const *x_array = NULL, *y_array = NULL; 713 CeedSize n_w, n_x, n_y; 714 715 CeedCall(CeedVectorGetLength(w, &n_w)); 716 CeedCall(CeedVectorGetLength(x, &n_x)); 717 CeedCall(CeedVectorGetLength(y, &n_y)); 718 if (n_w != n_x || n_w != n_y) { 719 // LCOV_EXCL_START 720 return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths"); 721 // LCOV_EXCL_STOP 722 } 723 724 Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y; 725 CeedCall(CeedGetParent(w->ceed, &ceed_parent_w)); 726 CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 727 CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 728 if ((ceed_parent_w != ceed_parent_x) || (ceed_parent_w != ceed_parent_y)) { 729 // LCOV_EXCL_START 730 return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors w, x, and y must be created by the same Ceed context"); 731 // LCOV_EXCL_STOP 732 } 733 734 bool has_valid_array_x = true, has_valid_array_y = true; 735 CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 736 if (!has_valid_array_x) { 737 // LCOV_EXCL_START 738 return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 739 // LCOV_EXCL_STOP 740 } 741 CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 742 if (!has_valid_array_y) { 743 // LCOV_EXCL_START 744 return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 745 // LCOV_EXCL_STOP 746 } 747 748 // Backend implementation 749 if (w->PointwiseMult) { 750 CeedCall(w->PointwiseMult(w, x, y)); 751 return CEED_ERROR_SUCCESS; 752 } 753 754 // Default implementation 755 if (x != w) { 756 CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 757 } else { 758 CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 759 x_array = w_array; 760 } 761 if (y != w && y != x) { 762 CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 763 } else if (y == x) { 764 y_array = x_array; 765 } else { 766 CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 767 y_array = w_array; 768 } 769 if (!w_array) CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 770 771 assert(w_array); 772 assert(x_array); 773 assert(y_array); 774 775 for (CeedInt i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i]; 776 777 if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 778 if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 779 CeedCall(CeedVectorRestoreArray(w, &w_array)); 780 return CEED_ERROR_SUCCESS; 781 } 782 783 /** 784 @brief Take the reciprocal of a CeedVector. 785 786 @param[in,out] vec CeedVector to take reciprocal 787 788 @return An error code: 0 - success, otherwise - failure 789 790 @ref User 791 **/ 792 int CeedVectorReciprocal(CeedVector vec) { 793 bool has_valid_array = true; 794 CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 795 796 if (!has_valid_array) { 797 // LCOV_EXCL_START 798 return CeedError(vec->ceed, CEED_ERROR_BACKEND, 799 "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 800 // LCOV_EXCL_STOP 801 } 802 803 // Check if vector data set 804 if (!vec->state) { 805 // LCOV_EXCL_START 806 return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 807 // LCOV_EXCL_STOP 808 } 809 810 // Backend impl for GPU, if added 811 if (vec->Reciprocal) { 812 CeedCall(vec->Reciprocal(vec)); 813 return CEED_ERROR_SUCCESS; 814 } 815 816 CeedSize len; 817 CeedCall(CeedVectorGetLength(vec, &len)); 818 CeedScalar *array; 819 CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 820 for (CeedInt i = 0; i < len; i++) { 821 if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 822 } 823 824 CeedCall(CeedVectorRestoreArray(vec, &array)); 825 return CEED_ERROR_SUCCESS; 826 } 827 828 /** 829 @brief View a CeedVector 830 831 @param[in] vec CeedVector to view 832 @param[in] fp_fmt Printing format 833 @param[in] stream Filestream to write to 834 835 @return An error code: 0 - success, otherwise - failure 836 837 @ref User 838 **/ 839 int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 840 const CeedScalar *x; 841 CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 842 843 char fmt[1024]; 844 fprintf(stream, "CeedVector length %ld\n", (long)vec->length); 845 snprintf(fmt, sizeof fmt, " %s\n", fp_fmt ? fp_fmt : "%g"); 846 for (CeedInt i = 0; i < vec->length; i++) fprintf(stream, fmt, x[i]); 847 848 CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 849 return CEED_ERROR_SUCCESS; 850 } 851 852 /** 853 @brief Get the Ceed associated with a CeedVector 854 855 @param[in] vec CeedVector to retrieve state 856 @param[out] ceed Variable to store ceed 857 858 @return An error code: 0 - success, otherwise - failure 859 860 @ref Advanced 861 **/ 862 int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 863 *ceed = vec->ceed; 864 return CEED_ERROR_SUCCESS; 865 } 866 867 /** 868 @brief Get the length of a CeedVector 869 870 @param[in] vec CeedVector to retrieve length 871 @param[out] length Variable to store length 872 873 @return An error code: 0 - success, otherwise - failure 874 875 @ref User 876 **/ 877 int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 878 *length = vec->length; 879 return CEED_ERROR_SUCCESS; 880 } 881 882 /** 883 @brief Destroy a CeedVector 884 885 @param[in,out] vec CeedVector to destroy 886 887 @return An error code: 0 - success, otherwise - failure 888 889 @ref User 890 **/ 891 int CeedVectorDestroy(CeedVector *vec) { 892 if (!*vec || --(*vec)->ref_count > 0) return CEED_ERROR_SUCCESS; 893 894 if (((*vec)->state % 2) == 1) { 895 return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 896 } 897 if ((*vec)->num_readers > 0) { 898 // LCOV_EXCL_START 899 return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 900 // LCOV_EXCL_STOP 901 } 902 903 if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 904 905 CeedCall(CeedDestroy(&(*vec)->ceed)); 906 CeedCall(CeedFree(vec)); 907 return CEED_ERROR_SUCCESS; 908 } 909 910 /// @} 911