1 // Copyright (c) 2017-2024, 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.h> 9 #include <ceed/backend.h> 10 #include <math.h> 11 #include <stdbool.h> 12 #include <string.h> 13 #include <hip/hip_runtime.h> 14 15 #include "../hip/ceed-hip-common.h" 16 #include "ceed-hip-ref.h" 17 18 //------------------------------------------------------------------------------ 19 // Check if host/device sync is needed 20 //------------------------------------------------------------------------------ 21 static inline int CeedVectorNeedSync_Hip(const CeedVector vec, CeedMemType mem_type, bool *need_sync) { 22 CeedVector_Hip *impl; 23 bool has_valid_array = false; 24 25 CeedCallBackend(CeedVectorGetData(vec, &impl)); 26 CeedCallBackend(CeedVectorHasValidArray(vec, &has_valid_array)); 27 switch (mem_type) { 28 case CEED_MEM_HOST: 29 *need_sync = has_valid_array && !impl->h_array; 30 break; 31 case CEED_MEM_DEVICE: 32 *need_sync = has_valid_array && !impl->d_array; 33 break; 34 } 35 return CEED_ERROR_SUCCESS; 36 } 37 38 //------------------------------------------------------------------------------ 39 // Sync host to device 40 //------------------------------------------------------------------------------ 41 static inline int CeedVectorSyncH2D_Hip(const CeedVector vec) { 42 CeedSize length; 43 size_t bytes; 44 CeedVector_Hip *impl; 45 46 CeedCallBackend(CeedVectorGetData(vec, &impl)); 47 48 CeedCheck(impl->h_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid host data to sync to device"); 49 50 CeedCallBackend(CeedVectorGetLength(vec, &length)); 51 bytes = length * sizeof(CeedScalar); 52 if (impl->d_array_borrowed) { 53 impl->d_array = impl->d_array_borrowed; 54 } else if (impl->d_array_owned) { 55 impl->d_array = impl->d_array_owned; 56 } else { 57 CeedCallHip(CeedVectorReturnCeed(vec), hipMalloc((void **)&impl->d_array_owned, bytes)); 58 impl->d_array = impl->d_array_owned; 59 } 60 CeedCallHip(CeedVectorReturnCeed(vec), hipMemcpy(impl->d_array, impl->h_array, bytes, hipMemcpyHostToDevice)); 61 return CEED_ERROR_SUCCESS; 62 } 63 64 //------------------------------------------------------------------------------ 65 // Sync device to host 66 //------------------------------------------------------------------------------ 67 static inline int CeedVectorSyncD2H_Hip(const CeedVector vec) { 68 CeedSize length; 69 size_t bytes; 70 CeedVector_Hip *impl; 71 72 CeedCallBackend(CeedVectorGetData(vec, &impl)); 73 74 CeedCheck(impl->d_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid device data to sync to host"); 75 76 if (impl->h_array_borrowed) { 77 impl->h_array = impl->h_array_borrowed; 78 } else if (impl->h_array_owned) { 79 impl->h_array = impl->h_array_owned; 80 } else { 81 CeedSize length; 82 83 CeedCallBackend(CeedVectorGetLength(vec, &length)); 84 CeedCallBackend(CeedCalloc(length, &impl->h_array_owned)); 85 impl->h_array = impl->h_array_owned; 86 } 87 88 CeedCallBackend(CeedVectorGetLength(vec, &length)); 89 bytes = length * sizeof(CeedScalar); 90 CeedCallHip(CeedVectorReturnCeed(vec), hipMemcpy(impl->h_array, impl->d_array, bytes, hipMemcpyDeviceToHost)); 91 return CEED_ERROR_SUCCESS; 92 } 93 94 //------------------------------------------------------------------------------ 95 // Sync arrays 96 //------------------------------------------------------------------------------ 97 static int CeedVectorSyncArray_Hip(const CeedVector vec, CeedMemType mem_type) { 98 bool need_sync = false; 99 100 // Check whether device/host sync is needed 101 CeedCallBackend(CeedVectorNeedSync_Hip(vec, mem_type, &need_sync)); 102 if (!need_sync) return CEED_ERROR_SUCCESS; 103 104 switch (mem_type) { 105 case CEED_MEM_HOST: 106 return CeedVectorSyncD2H_Hip(vec); 107 case CEED_MEM_DEVICE: 108 return CeedVectorSyncH2D_Hip(vec); 109 } 110 return CEED_ERROR_UNSUPPORTED; 111 } 112 113 //------------------------------------------------------------------------------ 114 // Set all pointers as invalid 115 //------------------------------------------------------------------------------ 116 static inline int CeedVectorSetAllInvalid_Hip(const CeedVector vec) { 117 CeedVector_Hip *impl; 118 119 CeedCallBackend(CeedVectorGetData(vec, &impl)); 120 impl->h_array = NULL; 121 impl->d_array = NULL; 122 return CEED_ERROR_SUCCESS; 123 } 124 125 //------------------------------------------------------------------------------ 126 // Check if CeedVector has any valid pointer 127 //------------------------------------------------------------------------------ 128 static inline int CeedVectorHasValidArray_Hip(const CeedVector vec, bool *has_valid_array) { 129 CeedVector_Hip *impl; 130 131 CeedCallBackend(CeedVectorGetData(vec, &impl)); 132 *has_valid_array = impl->h_array || impl->d_array; 133 return CEED_ERROR_SUCCESS; 134 } 135 136 //------------------------------------------------------------------------------ 137 // Check if has array of given type 138 //------------------------------------------------------------------------------ 139 static inline int CeedVectorHasArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) { 140 CeedVector_Hip *impl; 141 142 CeedCallBackend(CeedVectorGetData(vec, &impl)); 143 switch (mem_type) { 144 case CEED_MEM_HOST: 145 *has_array_of_type = impl->h_array_borrowed || impl->h_array_owned; 146 break; 147 case CEED_MEM_DEVICE: 148 *has_array_of_type = impl->d_array_borrowed || impl->d_array_owned; 149 break; 150 } 151 return CEED_ERROR_SUCCESS; 152 } 153 154 //------------------------------------------------------------------------------ 155 // Check if has borrowed array of given type 156 //------------------------------------------------------------------------------ 157 static inline int CeedVectorHasBorrowedArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 158 CeedVector_Hip *impl; 159 160 CeedCallBackend(CeedVectorGetData(vec, &impl)); 161 switch (mem_type) { 162 case CEED_MEM_HOST: 163 *has_borrowed_array_of_type = impl->h_array_borrowed; 164 break; 165 case CEED_MEM_DEVICE: 166 *has_borrowed_array_of_type = impl->d_array_borrowed; 167 break; 168 } 169 return CEED_ERROR_SUCCESS; 170 } 171 172 //------------------------------------------------------------------------------ 173 // Set array from host 174 //------------------------------------------------------------------------------ 175 static int CeedVectorSetArrayHost_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 176 CeedSize length; 177 CeedVector_Hip *impl; 178 179 CeedCallBackend(CeedVectorGetData(vec, &impl)); 180 CeedCallBackend(CeedVectorGetLength(vec, &length)); 181 182 CeedCallBackend(CeedSetHostCeedScalarArray(array, copy_mode, length, (const CeedScalar **)&impl->h_array_owned, 183 (const CeedScalar **)&impl->h_array_borrowed, (const CeedScalar **)&impl->h_array)); 184 return CEED_ERROR_SUCCESS; 185 } 186 187 //------------------------------------------------------------------------------ 188 // Set array from device 189 //------------------------------------------------------------------------------ 190 static int CeedVectorSetArrayDevice_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 191 CeedSize length; 192 Ceed ceed; 193 CeedVector_Hip *impl; 194 195 CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 196 CeedCallBackend(CeedVectorGetData(vec, &impl)); 197 CeedCallBackend(CeedVectorGetLength(vec, &length)); 198 199 CeedCallBackend(CeedSetDeviceCeedScalarArray_Hip(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned, 200 (const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array)); 201 CeedCallBackend(CeedDestroy(&ceed)); 202 return CEED_ERROR_SUCCESS; 203 } 204 205 //------------------------------------------------------------------------------ 206 // Set the array used by a vector, 207 // freeing any previously allocated array if applicable 208 //------------------------------------------------------------------------------ 209 static int CeedVectorSetArray_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) { 210 CeedVector_Hip *impl; 211 212 CeedCallBackend(CeedVectorGetData(vec, &impl)); 213 CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec)); 214 switch (mem_type) { 215 case CEED_MEM_HOST: 216 return CeedVectorSetArrayHost_Hip(vec, copy_mode, array); 217 case CEED_MEM_DEVICE: 218 return CeedVectorSetArrayDevice_Hip(vec, copy_mode, array); 219 } 220 return CEED_ERROR_UNSUPPORTED; 221 } 222 223 //------------------------------------------------------------------------------ 224 // Copy host array to value strided 225 //------------------------------------------------------------------------------ 226 static int CeedHostCopyStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *h_copy_array) { 227 for (CeedSize i = start; i < length; i += step) h_copy_array[i] = h_array[i]; 228 return CEED_ERROR_SUCCESS; 229 } 230 231 //------------------------------------------------------------------------------ 232 // Copy device array to value strided (impl in .hip.cpp file) 233 //------------------------------------------------------------------------------ 234 int CeedDeviceCopyStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *d_copy_array); 235 236 //------------------------------------------------------------------------------ 237 // Copy a vector to a value strided 238 //------------------------------------------------------------------------------ 239 static int CeedVectorCopyStrided_Hip(CeedVector vec, CeedSize start, CeedSize step, CeedVector vec_copy) { 240 CeedSize length; 241 CeedVector_Hip *impl; 242 243 CeedCallBackend(CeedVectorGetData(vec, &impl)); 244 { 245 CeedSize length_vec, length_copy; 246 247 CeedCallBackend(CeedVectorGetLength(vec, &length_vec)); 248 CeedCallBackend(CeedVectorGetLength(vec_copy, &length_copy)); 249 length = length_vec < length_copy ? length_vec : length_copy; 250 } 251 // Set value for synced device/host array 252 if (impl->d_array) { 253 CeedScalar *copy_array; 254 255 CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_DEVICE, ©_array)); 256 CeedCallBackend(CeedDeviceCopyStrided_Hip(impl->d_array, start, step, length, copy_array)); 257 CeedCallBackend(CeedVectorRestoreArray(vec_copy, ©_array)); 258 } else if (impl->h_array) { 259 CeedScalar *copy_array; 260 261 CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, ©_array)); 262 CeedCallBackend(CeedHostCopyStrided_Hip(impl->h_array, start, step, length, copy_array)); 263 CeedCallBackend(CeedVectorRestoreArray(vec_copy, ©_array)); 264 } else { 265 return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set"); 266 } 267 return CEED_ERROR_SUCCESS; 268 } 269 270 //------------------------------------------------------------------------------ 271 // Set host array to value 272 //------------------------------------------------------------------------------ 273 static int CeedHostSetValue_Hip(CeedScalar *h_array, CeedSize length, CeedScalar val) { 274 for (CeedSize i = 0; i < length; i++) h_array[i] = val; 275 return CEED_ERROR_SUCCESS; 276 } 277 278 //------------------------------------------------------------------------------ 279 // Set device array to value (impl in .hip file) 280 //------------------------------------------------------------------------------ 281 int CeedDeviceSetValue_Hip(CeedScalar *d_array, CeedSize length, CeedScalar val); 282 283 //------------------------------------------------------------------------------ 284 // Set a vector to a value 285 //------------------------------------------------------------------------------ 286 static int CeedVectorSetValue_Hip(CeedVector vec, CeedScalar val) { 287 CeedSize length; 288 CeedVector_Hip *impl; 289 290 CeedCallBackend(CeedVectorGetData(vec, &impl)); 291 CeedCallBackend(CeedVectorGetLength(vec, &length)); 292 // Set value for synced device/host array 293 if (!impl->d_array && !impl->h_array) { 294 if (impl->d_array_borrowed) { 295 impl->d_array = impl->d_array_borrowed; 296 } else if (impl->h_array_borrowed) { 297 impl->h_array = impl->h_array_borrowed; 298 } else if (impl->d_array_owned) { 299 impl->d_array = impl->d_array_owned; 300 } else if (impl->h_array_owned) { 301 impl->h_array = impl->h_array_owned; 302 } else { 303 CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL)); 304 } 305 } 306 if (impl->d_array) { 307 if (val == 0) { 308 CeedCallHip(CeedVectorReturnCeed(vec), hipMemset(impl->d_array, 0, length * sizeof(CeedScalar))); 309 } else { 310 CeedCallBackend(CeedDeviceSetValue_Hip(impl->d_array, length, val)); 311 } 312 impl->h_array = NULL; 313 } else if (impl->h_array) { 314 CeedCallBackend(CeedHostSetValue_Hip(impl->h_array, length, val)); 315 impl->d_array = NULL; 316 } 317 return CEED_ERROR_SUCCESS; 318 } 319 320 //------------------------------------------------------------------------------ 321 // Set host array to value strided 322 //------------------------------------------------------------------------------ 323 static int CeedHostSetValueStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val) { 324 for (CeedSize i = start; i < length; i += step) h_array[i] = val; 325 return CEED_ERROR_SUCCESS; 326 } 327 328 //------------------------------------------------------------------------------ 329 // Set device array to value strided (impl in .hip.cpp file) 330 //------------------------------------------------------------------------------ 331 int CeedDeviceSetValueStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val); 332 333 //------------------------------------------------------------------------------ 334 // Set a vector to a value strided 335 //------------------------------------------------------------------------------ 336 static int CeedVectorSetValueStrided_Hip(CeedVector vec, CeedSize start, CeedSize step, CeedScalar val) { 337 CeedSize length; 338 CeedVector_Hip *impl; 339 340 CeedCallBackend(CeedVectorGetData(vec, &impl)); 341 CeedCallBackend(CeedVectorGetLength(vec, &length)); 342 // Set value for synced device/host array 343 if (impl->d_array) { 344 CeedCallBackend(CeedDeviceSetValueStrided_Hip(impl->d_array, start, step, length, val)); 345 impl->h_array = NULL; 346 } else if (impl->h_array) { 347 CeedCallBackend(CeedHostSetValueStrided_Hip(impl->h_array, start, step, length, val)); 348 impl->d_array = NULL; 349 } else { 350 return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set"); 351 } 352 return CEED_ERROR_SUCCESS; 353 } 354 355 //------------------------------------------------------------------------------ 356 // Vector Take Array 357 //------------------------------------------------------------------------------ 358 static int CeedVectorTakeArray_Hip(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 359 CeedVector_Hip *impl; 360 361 CeedCallBackend(CeedVectorGetData(vec, &impl)); 362 363 // Sync array to requested mem_type 364 CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 365 366 // Update pointer 367 switch (mem_type) { 368 case CEED_MEM_HOST: 369 (*array) = impl->h_array_borrowed; 370 impl->h_array_borrowed = NULL; 371 impl->h_array = NULL; 372 break; 373 case CEED_MEM_DEVICE: 374 (*array) = impl->d_array_borrowed; 375 impl->d_array_borrowed = NULL; 376 impl->d_array = NULL; 377 break; 378 } 379 return CEED_ERROR_SUCCESS; 380 } 381 382 //------------------------------------------------------------------------------ 383 // Core logic for array syncronization for GetArray. 384 // If a different memory type is most up to date, this will perform a copy 385 //------------------------------------------------------------------------------ 386 static int CeedVectorGetArrayCore_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 387 CeedVector_Hip *impl; 388 389 CeedCallBackend(CeedVectorGetData(vec, &impl)); 390 391 // Sync array to requested mem_type 392 CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 393 394 // Update pointer 395 switch (mem_type) { 396 case CEED_MEM_HOST: 397 *array = impl->h_array; 398 break; 399 case CEED_MEM_DEVICE: 400 *array = impl->d_array; 401 break; 402 } 403 return CEED_ERROR_SUCCESS; 404 } 405 406 //------------------------------------------------------------------------------ 407 // Get read-only access to a vector via the specified mem_type 408 //------------------------------------------------------------------------------ 409 static int CeedVectorGetArrayRead_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) { 410 return CeedVectorGetArrayCore_Hip(vec, mem_type, (CeedScalar **)array); 411 } 412 413 //------------------------------------------------------------------------------ 414 // Get read/write access to a vector via the specified mem_type 415 //------------------------------------------------------------------------------ 416 static int CeedVectorGetArray_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 417 CeedVector_Hip *impl; 418 419 CeedCallBackend(CeedVectorGetData(vec, &impl)); 420 CeedCallBackend(CeedVectorGetArrayCore_Hip(vec, mem_type, array)); 421 CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec)); 422 switch (mem_type) { 423 case CEED_MEM_HOST: 424 impl->h_array = *array; 425 break; 426 case CEED_MEM_DEVICE: 427 impl->d_array = *array; 428 break; 429 } 430 return CEED_ERROR_SUCCESS; 431 } 432 433 //------------------------------------------------------------------------------ 434 // Get write access to a vector via the specified mem_type 435 //------------------------------------------------------------------------------ 436 static int CeedVectorGetArrayWrite_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 437 bool has_array_of_type = true; 438 CeedVector_Hip *impl; 439 440 CeedCallBackend(CeedVectorGetData(vec, &impl)); 441 CeedCallBackend(CeedVectorHasArrayOfType_Hip(vec, mem_type, &has_array_of_type)); 442 if (!has_array_of_type) { 443 // Allocate if array is not yet allocated 444 CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL)); 445 } else { 446 // Select dirty array 447 switch (mem_type) { 448 case CEED_MEM_HOST: 449 if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed; 450 else impl->h_array = impl->h_array_owned; 451 break; 452 case CEED_MEM_DEVICE: 453 if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed; 454 else impl->d_array = impl->d_array_owned; 455 } 456 } 457 return CeedVectorGetArray_Hip(vec, mem_type, array); 458 } 459 460 //------------------------------------------------------------------------------ 461 // Get the norm of a CeedVector 462 //------------------------------------------------------------------------------ 463 static int CeedVectorNorm_Hip(CeedVector vec, CeedNormType type, CeedScalar *norm) { 464 Ceed ceed; 465 CeedSize length, num_calls; 466 const CeedScalar *d_array; 467 CeedVector_Hip *impl; 468 hipblasHandle_t handle; 469 470 CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 471 CeedCallBackend(CeedVectorGetData(vec, &impl)); 472 CeedCallBackend(CeedVectorGetLength(vec, &length)); 473 CeedCallBackend(CeedGetHipblasHandle_Hip(ceed, &handle)); 474 475 // Is the vector too long to handle with int32? If so, we will divide 476 // it up into "int32-sized" subsections and make repeated BLAS calls. 477 num_calls = length / INT_MAX; 478 if (length % INT_MAX > 0) num_calls += 1; 479 480 // Compute norm 481 CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array)); 482 switch (type) { 483 case CEED_NORM_1: { 484 *norm = 0.0; 485 if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 486 float sub_norm = 0.0; 487 float *d_array_start; 488 489 for (CeedInt i = 0; i < num_calls; i++) { 490 d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 491 CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 492 CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 493 494 CeedCallHipblas(ceed, hipblasSasum(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm)); 495 *norm += sub_norm; 496 } 497 } else { 498 double sub_norm = 0.0; 499 double *d_array_start; 500 501 for (CeedInt i = 0; i < num_calls; i++) { 502 d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 503 CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 504 CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 505 506 CeedCallHipblas(ceed, hipblasDasum(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm)); 507 *norm += sub_norm; 508 } 509 } 510 break; 511 } 512 case CEED_NORM_2: { 513 if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 514 float sub_norm = 0.0, norm_sum = 0.0; 515 float *d_array_start; 516 517 for (CeedInt i = 0; i < num_calls; i++) { 518 d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 519 CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 520 CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 521 522 CeedCallHipblas(ceed, hipblasSnrm2(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm)); 523 norm_sum += sub_norm * sub_norm; 524 } 525 *norm = sqrt(norm_sum); 526 } else { 527 double sub_norm = 0.0, norm_sum = 0.0; 528 double *d_array_start; 529 530 for (CeedInt i = 0; i < num_calls; i++) { 531 d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 532 CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 533 CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 534 535 CeedCallHipblas(ceed, hipblasDnrm2(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm)); 536 norm_sum += sub_norm * sub_norm; 537 } 538 *norm = sqrt(norm_sum); 539 } 540 break; 541 } 542 case CEED_NORM_MAX: { 543 CeedInt index; 544 545 if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 546 float sub_max = 0.0, current_max = 0.0; 547 float *d_array_start; 548 for (CeedInt i = 0; i < num_calls; i++) { 549 d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 550 CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 551 CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 552 553 CeedCallHipblas(ceed, hipblasIsamax(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &index)); 554 CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost)); 555 if (fabs(sub_max) > current_max) current_max = fabs(sub_max); 556 } 557 *norm = current_max; 558 } else { 559 double sub_max = 0.0, current_max = 0.0; 560 double *d_array_start; 561 562 for (CeedInt i = 0; i < num_calls; i++) { 563 d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 564 CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 565 CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 566 567 CeedCallHipblas(ceed, hipblasIdamax(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &index)); 568 CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost)); 569 if (fabs(sub_max) > current_max) current_max = fabs(sub_max); 570 } 571 *norm = current_max; 572 } 573 break; 574 } 575 } 576 CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array)); 577 CeedCallBackend(CeedDestroy(&ceed)); 578 return CEED_ERROR_SUCCESS; 579 } 580 581 //------------------------------------------------------------------------------ 582 // Take reciprocal of a vector on host 583 //------------------------------------------------------------------------------ 584 static int CeedHostReciprocal_Hip(CeedScalar *h_array, CeedSize length) { 585 for (CeedSize i = 0; i < length; i++) { 586 if (fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i]; 587 } 588 return CEED_ERROR_SUCCESS; 589 } 590 591 //------------------------------------------------------------------------------ 592 // Take reciprocal of a vector on device (impl in .hip.cpp file) 593 //------------------------------------------------------------------------------ 594 int CeedDeviceReciprocal_Hip(CeedScalar *d_array, CeedSize length); 595 596 //------------------------------------------------------------------------------ 597 // Take reciprocal of a vector 598 //------------------------------------------------------------------------------ 599 static int CeedVectorReciprocal_Hip(CeedVector vec) { 600 CeedSize length; 601 CeedVector_Hip *impl; 602 603 CeedCallBackend(CeedVectorGetData(vec, &impl)); 604 CeedCallBackend(CeedVectorGetLength(vec, &length)); 605 // Set value for synced device/host array 606 if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Hip(impl->d_array, length)); 607 if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Hip(impl->h_array, length)); 608 return CEED_ERROR_SUCCESS; 609 } 610 611 //------------------------------------------------------------------------------ 612 // Compute x = alpha x on the host 613 //------------------------------------------------------------------------------ 614 static int CeedHostScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length) { 615 for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha; 616 return CEED_ERROR_SUCCESS; 617 } 618 619 //------------------------------------------------------------------------------ 620 // Compute x = alpha x on device (impl in .hip.cpp file) 621 //------------------------------------------------------------------------------ 622 int CeedDeviceScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length); 623 624 //------------------------------------------------------------------------------ 625 // Compute x = alpha x 626 //------------------------------------------------------------------------------ 627 static int CeedVectorScale_Hip(CeedVector x, CeedScalar alpha) { 628 CeedSize length; 629 CeedVector_Hip *x_impl; 630 631 CeedCallBackend(CeedVectorGetData(x, &x_impl)); 632 CeedCallBackend(CeedVectorGetLength(x, &length)); 633 // Set value for synced device/host array 634 if (x_impl->d_array) CeedCallBackend(CeedDeviceScale_Hip(x_impl->d_array, alpha, length)); 635 if (x_impl->h_array) CeedCallBackend(CeedHostScale_Hip(x_impl->h_array, alpha, length)); 636 return CEED_ERROR_SUCCESS; 637 } 638 639 //------------------------------------------------------------------------------ 640 // Compute y = alpha x + y on the host 641 //------------------------------------------------------------------------------ 642 static int CeedHostAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length) { 643 for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i]; 644 return CEED_ERROR_SUCCESS; 645 } 646 647 //------------------------------------------------------------------------------ 648 // Compute y = alpha x + y on device (impl in .hip.cpp file) 649 //------------------------------------------------------------------------------ 650 int CeedDeviceAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length); 651 652 //------------------------------------------------------------------------------ 653 // Compute y = alpha x + y 654 //------------------------------------------------------------------------------ 655 static int CeedVectorAXPY_Hip(CeedVector y, CeedScalar alpha, CeedVector x) { 656 CeedSize length; 657 CeedVector_Hip *y_impl, *x_impl; 658 659 CeedCallBackend(CeedVectorGetData(y, &y_impl)); 660 CeedCallBackend(CeedVectorGetData(x, &x_impl)); 661 CeedCallBackend(CeedVectorGetLength(y, &length)); 662 // Set value for synced device/host array 663 if (y_impl->d_array) { 664 CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 665 CeedCallBackend(CeedDeviceAXPY_Hip(y_impl->d_array, alpha, x_impl->d_array, length)); 666 } 667 if (y_impl->h_array) { 668 CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 669 CeedCallBackend(CeedHostAXPY_Hip(y_impl->h_array, alpha, x_impl->h_array, length)); 670 } 671 return CEED_ERROR_SUCCESS; 672 } 673 674 //------------------------------------------------------------------------------ 675 // Compute y = alpha x + beta y on the host 676 //------------------------------------------------------------------------------ 677 static int CeedHostAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length) { 678 for (CeedSize i = 0; i < length; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i]; 679 return CEED_ERROR_SUCCESS; 680 } 681 682 //------------------------------------------------------------------------------ 683 // Compute y = alpha x + beta y on device (impl in .hip.cpp file) 684 //------------------------------------------------------------------------------ 685 int CeedDeviceAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length); 686 687 //------------------------------------------------------------------------------ 688 // Compute y = alpha x + beta y 689 //------------------------------------------------------------------------------ 690 static int CeedVectorAXPBY_Hip(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 691 CeedSize length; 692 CeedVector_Hip *y_impl, *x_impl; 693 694 CeedCallBackend(CeedVectorGetData(y, &y_impl)); 695 CeedCallBackend(CeedVectorGetData(x, &x_impl)); 696 CeedCallBackend(CeedVectorGetLength(y, &length)); 697 // Set value for synced device/host array 698 if (y_impl->d_array) { 699 CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 700 CeedCallBackend(CeedDeviceAXPBY_Hip(y_impl->d_array, alpha, beta, x_impl->d_array, length)); 701 } 702 if (y_impl->h_array) { 703 CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 704 CeedCallBackend(CeedHostAXPBY_Hip(y_impl->h_array, alpha, beta, x_impl->h_array, length)); 705 } 706 return CEED_ERROR_SUCCESS; 707 } 708 709 //------------------------------------------------------------------------------ 710 // Compute the pointwise multiplication w = x .* y on the host 711 //------------------------------------------------------------------------------ 712 static int CeedHostPointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length) { 713 for (CeedSize i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i]; 714 return CEED_ERROR_SUCCESS; 715 } 716 717 //------------------------------------------------------------------------------ 718 // Compute the pointwise multiplication w = x .* y on device (impl in .hip.cpp file) 719 //------------------------------------------------------------------------------ 720 int CeedDevicePointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length); 721 722 //------------------------------------------------------------------------------ 723 // Compute the pointwise multiplication w = x .* y 724 //------------------------------------------------------------------------------ 725 static int CeedVectorPointwiseMult_Hip(CeedVector w, CeedVector x, CeedVector y) { 726 CeedSize length; 727 CeedVector_Hip *w_impl, *x_impl, *y_impl; 728 729 CeedCallBackend(CeedVectorGetData(w, &w_impl)); 730 CeedCallBackend(CeedVectorGetData(x, &x_impl)); 731 CeedCallBackend(CeedVectorGetData(y, &y_impl)); 732 CeedCallBackend(CeedVectorGetLength(w, &length)); 733 734 // Set value for synced device/host array 735 if (!w_impl->d_array && !w_impl->h_array) { 736 CeedCallBackend(CeedVectorSetValue(w, 0.0)); 737 } 738 if (w_impl->d_array) { 739 CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 740 CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE)); 741 CeedCallBackend(CeedDevicePointwiseMult_Hip(w_impl->d_array, x_impl->d_array, y_impl->d_array, length)); 742 } 743 if (w_impl->h_array) { 744 CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 745 CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST)); 746 CeedCallBackend(CeedHostPointwiseMult_Hip(w_impl->h_array, x_impl->h_array, y_impl->h_array, length)); 747 } 748 return CEED_ERROR_SUCCESS; 749 } 750 751 //------------------------------------------------------------------------------ 752 // Destroy the vector 753 //------------------------------------------------------------------------------ 754 static int CeedVectorDestroy_Hip(const CeedVector vec) { 755 CeedVector_Hip *impl; 756 757 CeedCallBackend(CeedVectorGetData(vec, &impl)); 758 CeedCallHip(CeedVectorReturnCeed(vec), hipFree(impl->d_array_owned)); 759 CeedCallBackend(CeedFree(&impl->h_array_owned)); 760 CeedCallBackend(CeedFree(&impl)); 761 return CEED_ERROR_SUCCESS; 762 } 763 764 //------------------------------------------------------------------------------ 765 // Create a vector of the specified length (does not allocate memory) 766 //------------------------------------------------------------------------------ 767 int CeedVectorCreate_Hip(CeedSize n, CeedVector vec) { 768 CeedVector_Hip *impl; 769 Ceed ceed; 770 771 CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 772 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Hip)); 773 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Hip)); 774 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Hip)); 775 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Hip)); 776 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "CopyStrided", CeedVectorCopyStrided_Hip)); 777 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", CeedVectorSetValue_Hip)); 778 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValueStrided", CeedVectorSetValueStrided_Hip)); 779 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Hip)); 780 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Hip)); 781 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Hip)); 782 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Hip)); 783 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Norm", CeedVectorNorm_Hip)); 784 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Hip)); 785 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Scale", CeedVectorScale_Hip)); 786 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", CeedVectorAXPY_Hip)); 787 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", CeedVectorAXPBY_Hip)); 788 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Hip)); 789 CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Hip)); 790 CeedCallBackend(CeedDestroy(&ceed)); 791 CeedCallBackend(CeedCalloc(1, &impl)); 792 CeedCallBackend(CeedVectorSetData(vec, impl)); 793 return CEED_ERROR_SUCCESS; 794 } 795 796 //------------------------------------------------------------------------------ 797