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