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