1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
|
/* ************************************************************************
* Copyright (C) 2020-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cop-
* ies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM-
* PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE-
* CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* ************************************************************************ */
#pragma once
#include "hipsolver.h"
#ifdef __cplusplus
#include "complex.hpp"
#include "hipsolver_datatype2string.hpp"
#include <cassert>
#include <cmath>
#include <random>
#include <type_traits>
#include <vector>
#endif
#include <stdio.h>
#include <stdlib.h>
/*!\file
* \brief provide data initialization, timing, hipsolver type <-> lapack char conversion utilities.
*/
#ifdef GOOGLE_TEST
#include <gtest/gtest.h>
#define CHECK_HIP_ERROR(error) ASSERT_EQ(error, hipSuccess)
inline void hipsolver_expect_status(hipsolverStatus_t status, hipsolverStatus_t expected)
{
if(status != HIPSOLVER_STATUS_NOT_SUPPORTED)
ASSERT_EQ(status, expected);
}
#define EXPECT_ROCBLAS_STATUS(status, expected) hipsolver_expect_status(status, expected)
#define CHECK_ROCBLAS_ERROR(status) hipsolver_expect_status(status, HIPSOLVER_STATUS_SUCCESS)
#else
#define CHECK_HIP_ERROR(error) \
do \
{ \
if(error != hipSuccess) \
{ \
fprintf(stderr, \
"error: '%s'(%d) at %s:%d\n", \
hipGetErrorString(error), \
error, \
__FILE__, \
__LINE__); \
exit(EXIT_FAILURE); \
} \
} while(0)
inline void hipsolver_expect_status(hipsolverStatus_t status, hipsolverStatus_t expected)
{
if(status != expected && status != HIPSOLVER_STATUS_NOT_SUPPORTED)
{
fprintf(stderr,
"hipSOLVER status error: Expected: %s, Actual: %s\n",
hipsolver2string_status(expected),
hipsolver2string_status(status));
if(expected == HIPSOLVER_STATUS_SUCCESS)
exit(EXIT_FAILURE);
}
}
#define EXPECT_ROCBLAS_STATUS(status, expected) hipsolver_expect_status(status, expected)
#define CHECK_ROCBLAS_ERROR(status) hipsolver_expect_status(status, HIPSOLVER_STATUS_SUCCESS)
// The info provided to EXPECT macros is used in hipsolver-test, but
// in hipsolver-bench, the information is just discarded.
struct hipsolver_info_discarder
{
template <typename T>
hipsolver_info_discarder& operator<<(T&&)
{
return *this;
}
};
#define EXPECT_EQ(v1, v2) hipsolver_info_discarder()
#define EXPECT_NE(v1, v2) hipsolver_info_discarder()
#define EXPECT_LT(v1, v2) hipsolver_info_discarder()
#define EXPECT_LE(v1, v2) hipsolver_info_discarder()
#define EXPECT_GT(v1, v2) hipsolver_info_discarder()
#define EXPECT_GE(v1, v2) hipsolver_info_discarder()
#endif
#ifdef __cplusplus
/* ============================================================================================
*/
/*! \brief local handle which is automatically created and destroyed */
class hipsolver_local_handle
{
hipsolverHandle_t m_handle;
public:
hipsolver_local_handle()
{
if(hipsolverCreate(&m_handle) != HIPSOLVER_STATUS_SUCCESS)
throw std::runtime_error("ERROR: Could not create hipsolverHandle_t");
}
~hipsolver_local_handle()
{
hipsolverDestroy(m_handle);
}
hipsolver_local_handle(const hipsolver_local_handle&) = delete;
hipsolver_local_handle(hipsolver_local_handle&&) = delete;
hipsolver_local_handle& operator=(const hipsolver_local_handle&) = delete;
hipsolver_local_handle& operator=(hipsolver_local_handle&&) = delete;
// Allow hipsolver_local_handle to be used anywhere hipsolverHandle_t is expected
operator hipsolverHandle_t&()
{
return m_handle;
}
operator const hipsolverHandle_t&() const
{
return m_handle;
}
};
class hipsolverRf_local_handle
{
hipsolverRfHandle_t m_handle;
public:
hipsolverRf_local_handle()
{
if(hipsolverRfCreate(&m_handle) != HIPSOLVER_STATUS_SUCCESS)
throw std::runtime_error("ERROR: Could not create hipsolverRfHandle_t");
}
~hipsolverRf_local_handle()
{
hipsolverRfDestroy(m_handle);
}
hipsolverRf_local_handle(const hipsolverRf_local_handle&) = delete;
hipsolverRf_local_handle(hipsolverRf_local_handle&&) = delete;
hipsolverRf_local_handle& operator=(const hipsolverRf_local_handle&) = delete;
hipsolverRf_local_handle& operator=(hipsolverRf_local_handle&&) = delete;
// Allow hipsolverRf_local_handle to be used anywhere hipsolverRfHandle_t is expected
operator hipsolverRfHandle_t&()
{
return m_handle;
}
operator const hipsolverRfHandle_t&() const
{
return m_handle;
}
};
class hipsolverSp_local_handle
{
hipsolverSpHandle_t m_handle;
public:
hipsolverSp_local_handle()
{
if(hipsolverSpCreate(&m_handle) != HIPSOLVER_STATUS_SUCCESS)
throw std::runtime_error("ERROR: Could not create hipsolverSpHandle_t");
}
~hipsolverSp_local_handle()
{
hipsolverSpDestroy(m_handle);
}
hipsolverSp_local_handle(const hipsolverSp_local_handle&) = delete;
hipsolverSp_local_handle(hipsolverSp_local_handle&&) = delete;
hipsolverSp_local_handle& operator=(const hipsolverSp_local_handle&) = delete;
hipsolverSp_local_handle& operator=(hipsolverSp_local_handle&&) = delete;
// Allow hipsolverSp_local_handle to be used anywhere hipsolverSpHandle_t is expected
operator hipsolverSpHandle_t&()
{
return m_handle;
}
operator const hipsolverSpHandle_t&() const
{
return m_handle;
}
};
/* ============================================================================================
*/
/*! \brief local gesvdj params which is automatically created and destroyed */
class hipsolver_local_gesvdj_info
{
hipsolverGesvdjInfo_t m_info;
public:
hipsolver_local_gesvdj_info()
{
if(hipsolverDnCreateGesvdjInfo(&m_info) != HIPSOLVER_STATUS_SUCCESS)
throw std::runtime_error("ERROR: Could not create hipsolverGesvdjInfo_t");
}
~hipsolver_local_gesvdj_info()
{
hipsolverDnDestroyGesvdjInfo(m_info);
}
hipsolver_local_gesvdj_info(const hipsolver_local_gesvdj_info&) = delete;
hipsolver_local_gesvdj_info(hipsolver_local_gesvdj_info&&) = delete;
hipsolver_local_gesvdj_info& operator=(const hipsolver_local_gesvdj_info&) = delete;
hipsolver_local_gesvdj_info& operator=(hipsolver_local_gesvdj_info&&) = delete;
// Allow hipsolver_local_gesvdj_info to be used anywhere hipsolverGesvdjInfo_t is expected
operator hipsolverGesvdjInfo_t&()
{
return m_info;
}
operator const hipsolverGesvdjInfo_t&() const
{
return m_info;
}
};
/* ============================================================================================
*/
/*! \brief local syevj params which is automatically created and destroyed */
class hipsolver_local_syevj_info
{
hipsolverSyevjInfo_t m_info;
public:
hipsolver_local_syevj_info()
{
if(hipsolverDnCreateSyevjInfo(&m_info) != HIPSOLVER_STATUS_SUCCESS)
throw std::runtime_error("ERROR: Could not create hipsolverSyevjInfo_t");
}
~hipsolver_local_syevj_info()
{
hipsolverDnDestroySyevjInfo(m_info);
}
hipsolver_local_syevj_info(const hipsolver_local_syevj_info&) = delete;
hipsolver_local_syevj_info(hipsolver_local_syevj_info&&) = delete;
hipsolver_local_syevj_info& operator=(const hipsolver_local_syevj_info&) = delete;
hipsolver_local_syevj_info& operator=(hipsolver_local_syevj_info&&) = delete;
// Allow hipsolver_local_syevj_info to be used anywhere hipsolverSyevjInfo_t is expected
operator hipsolverSyevjInfo_t&()
{
return m_info;
}
operator const hipsolverSyevjInfo_t&() const
{
return m_info;
}
};
/* ============================================================================================
*/
/*! \brief local params which is automatically created and destroyed */
class hipsolver_local_params
{
hipsolverDnParams_t m_info;
public:
hipsolver_local_params()
{
if(hipsolverDnCreateParams(&m_info) != HIPSOLVER_STATUS_SUCCESS)
throw std::runtime_error("ERROR: Could not create hipsolverDnParams_t");
}
~hipsolver_local_params()
{
hipsolverDnDestroyParams(m_info);
}
hipsolver_local_params(const hipsolver_local_params&) = delete;
hipsolver_local_params(hipsolver_local_params&&) = delete;
hipsolver_local_params& operator=(const hipsolver_local_params&) = delete;
hipsolver_local_params& operator=(hipsolver_local_params&&) = delete;
// Allow hipsolver_local_params to be used anywhere hipsolverDnParams_t is expected
operator hipsolverDnParams_t&()
{
return m_info;
}
operator const hipsolverDnParams_t&() const
{
return m_info;
}
};
/* ============================================================================================
*/
// Return true if value is NaN
template <typename T>
inline bool hipsolver_isnan(T)
{
return false;
}
inline bool hipsolver_isnan(double arg)
{
return std::isnan(arg);
}
inline bool hipsolver_isnan(float arg)
{
return std::isnan(arg);
}
inline bool hipsolver_isnan(hipsolverComplex arg)
{
return std::isnan(arg.real()) || std::isnan(arg.imag());
}
inline bool hipsolver_isnan(hipsolverDoubleComplex arg)
{
return std::isnan(arg.real()) || std::isnan(arg.imag());
}
/* =============================================================================================== */
/* Complex / real helpers. */
/* Workaround for clang bug:
https://bugs.llvm.org/show_bug.cgi?id=35863
*/
#if __clang__
#define HIPSOLVER_CLANG_STATIC static
#else
#define HIPSOLVER_CLANG_STATIC
#endif
template <typename T>
static constexpr bool is_complex = false;
// cppcheck-suppress syntaxError
template <>
HIPSOLVER_CLANG_STATIC inline constexpr bool is_complex<hipsolverComplex> = true;
template <>
HIPSOLVER_CLANG_STATIC inline constexpr bool is_complex<hipsolverDoubleComplex> = true;
// Get base types from complex types.
template <typename T, typename = void>
struct real_t_impl
{
using type = T;
};
template <typename T>
struct real_t_impl<T, std::enable_if_t<is_complex<T>>>
{
using type = decltype(T{}.real());
};
template <typename T>
using real_t = typename real_t_impl<T>::type;
/* ============================================================================================ */
/*! \brief Random number generator which generates NaN values */
using hipsolver_rng_t = std::mt19937;
extern hipsolver_rng_t hipsolver_rng, hipsolver_seed;
// Reset the seed (mainly to ensure repeatability of failures in a given suite)
inline void hipsolver_seedrand()
{
hipsolver_rng = hipsolver_seed;
}
class hipsolver_nan_rng
{
// Generate random NaN values
template <typename T, typename UINT_T, int SIG, int EXP>
static T random_nan_data()
{
static_assert(sizeof(UINT_T) == sizeof(T), "Type sizes do not match");
union u_t
{
u_t() {}
UINT_T u;
T fp;
} x;
do
x.u = std::uniform_int_distribution<UINT_T>{}(hipsolver_rng);
while(!(x.u & (((UINT_T)1 << SIG) - 1))); // Reject Inf (mantissa == 0)
x.u |= (((UINT_T)1 << EXP) - 1) << SIG; // Exponent = all 1's
return x.fp; // NaN with random bits
}
public:
// Random integer
template <typename T, typename std::enable_if<std::is_integral<T>{}, int>::type = 0>
explicit operator T() const
{
return std::uniform_int_distribution<T>{}(hipsolver_rng);
}
// Random NaN float
explicit operator float() const
{
return random_nan_data<float, uint32_t, 23, 8>();
}
// Random NaN double
explicit operator double() const
{
return random_nan_data<double, uint64_t, 52, 11>();
}
// Random NaN Complex
explicit operator hipsolverComplex() const
{
return {float(*this), float(*this)};
}
// Random NaN Double Complex
explicit operator hipsolverDoubleComplex() const
{
return {double(*this), double(*this)};
}
};
/* ============================================================================================ */
/* generate random number :*/
/*! \brief generate a random number in range [1,2,3,4,5,6,7,8,9,10] */
template <typename T>
T random_generator()
{
return std::uniform_int_distribution<int>(1, 10)(hipsolver_rng);
};
// for hipsolverComplex, generate 2 floats
/*! \brief generate two random numbers in range [1,2,3,4,5,6,7,8,9,10] */
template <>
inline hipsolverComplex random_generator<hipsolverComplex>()
{
return hipsolverComplex(float(std::uniform_int_distribution<int>(1, 10)(hipsolver_rng)),
float(std::uniform_int_distribution<int>(1, 10)(hipsolver_rng)));
}
// for hipsolverDoubleComplex, generate 2 doubles
/*! \brief generate two random numbers in range [1,2,3,4,5,6,7,8,9,10] */
template <>
inline hipsolverDoubleComplex random_generator<hipsolverDoubleComplex>()
{
return hipsolverDoubleComplex(double(std::uniform_int_distribution<int>(1, 10)(hipsolver_rng)),
double(std::uniform_int_distribution<int>(1, 10)(hipsolver_rng)));
}
/*! \brief generate a random number in range [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10] */
template <typename T>
inline T random_generator_negative()
{
return std::uniform_int_distribution<int>(-10, -1)(hipsolver_rng);
};
// for complex, generate two values, convert both to negative
/*! \brief generate a random real value in range [-1, -10] and random
* imaginary value in range [-1, -10]
*/
template <>
inline hipsolverComplex random_generator_negative<hipsolverComplex>()
{
return hipsolverComplex(float(std::uniform_int_distribution<int>(-10, -1)(hipsolver_rng)),
float(std::uniform_int_distribution<int>(-10, -1)(hipsolver_rng)));
}
template <>
inline hipsolverDoubleComplex random_generator_negative<hipsolverDoubleComplex>()
{
return hipsolverDoubleComplex(
double(std::uniform_int_distribution<int>(-10, -1)(hipsolver_rng)),
double(std::uniform_int_distribution<int>(-10, -1)(hipsolver_rng)));
}
/* ============================================================================================ */
/* ============================================================================================ */
/*! \brief Packs strided_batched matricies into groups of 4 in N */
template <typename T>
void hipsolver_packInt8(
std::vector<T>& A, size_t M, size_t N, size_t lda, size_t batch_count = 1, size_t stride_a = 0)
{
std::vector<T> temp(A);
for(size_t b = 0; b < batch_count; b++)
for(size_t colBase = 0; colBase < N; colBase += 4)
for(size_t row = 0; row < lda; row++)
for(size_t colOffset = 0; colOffset < 4; colOffset++)
A[(colBase * lda + 4 * row) + colOffset + (stride_a * b)]
= temp[(colBase + colOffset) * lda + row + (stride_a * b)];
}
/* ============================================================================================ */
/* ============================================================================================ */
/*! \brief turn float -> 's', double -> 'd', hipsolverComplex -> 'c', hipsolverDoubleComplex
* -> 'z' */
template <typename T>
char type2char();
/* ============================================================================================ */
/*! \brief turn float -> int, double -> int, hipsolverComplex.real() -> int,
* hipsolverDoubleComplex.real() -> int */
template <typename T>
int type2int(T val);
/* ============================================================================================ */
/*! \brief Debugging purpose, print out CPU and GPU result matrix, not valid in complex number */
template <typename T, std::enable_if_t<!is_complex<T>, int> = 0>
void print_matrix(T* result, int m, int n, int lda)
{
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
printf("matrix col %d, row %d, result=%.8g\n", j, i, double(result[i + j * lda]));
}
/*! \brief Debugging purpose, print out CPU and GPU result matrix, valid for complex number */
template <typename T, std::enable_if_t<+is_complex<T>, int> = 0>
void print_matrix(T* result, int m, int n, int lda)
{
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
printf("matrix col %d, row %d, result=(%.8g,%.8g)\n",
j,
i,
double(result[i + j * lda].real()),
double(result[i + j * lda].imag()));
}
template <typename T, std::enable_if_t<!is_complex<T>, int> = 0>
void print_matrix(T* CPU_result, T* GPU_result, int m, int n, int lda)
{
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
printf("matrix col %d, row %d, CPU result=%.8g, GPU result=%.8g\n",
j,
i,
double(CPU_result[i + j * lda]),
double(GPU_result[i + j * lda]));
}
/*! \brief Debugging purpose, print out CPU and GPU result matrix, valid for complex number */
template <typename T, std::enable_if_t<+is_complex<T>, int> = 0>
void print_matrix(T* CPU_result, T* GPU_result, int m, int n, int lda)
{
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
printf("matrix col %d, row %d, CPU result=(%.8g,%.8g), GPU result=(%.8g,%.8g)\n",
j,
i,
double(CPU_result[i + j * lda].real()),
double(CPU_result[i + j * lda].imag()),
double(GPU_result[i + j * lda].real()),
double(GPU_result[i + j * lda].imag()));
}
/*! \brief Debugging purpose, print out sparse matrix, not valid in complex number */
template <typename T, std::enable_if_t<!is_complex<T>, int> = 0>
void print_sparse_matrix(int m, int n, int* csrRowPtr, int* csrColInd, T* csrVal)
{
printf("%d by %d matrix\n", m, n);
int idx = 0;
for(int i = 0; i < m; i++)
{
printf(" ");
for(int j = 0; j < n; j++)
{
if(idx < csrRowPtr[i + 1] && j == csrColInd[idx])
{
printf("%.8g, ", csrVal[idx]);
idx++;
}
else
printf("0, ");
}
printf("\n");
}
printf("\n");
}
/* ============================================================================================ */
/* read matrix or values from file */
// integers:
inline void
read_matrix(const std::string filenameS, const int m, const int n, int* A, const int lda)
{
const char* filename = filenameS.c_str();
FILE* mat;
mat = fopen(filename, "r");
int v;
if(mat == NULL)
throw std::invalid_argument(std::string("Error: Could not open file ") + filename
+ " with test data...");
for(int j = 0; j < n; ++j)
{
for(int i = 0; i < m; ++i)
{
int read = fscanf(mat, "%d", &v);
if(read != 1)
throw std::out_of_range(std::string("Error: Could not read element from file ")
+ filename);
A[i + j * lda] = v;
}
}
fclose(mat);
}
inline void read_last(const std::string filenameS, int* A)
{
const char* filename = filenameS.c_str();
FILE* mat;
mat = fopen(filename, "r");
int v;
if(mat == NULL)
throw std::invalid_argument(std::string("Error: Could not open file ") + filename
+ " with test data...");
while(fscanf(mat, "%d", &v) == 1)
{
// do nothing
}
*A = v;
}
// singles:
inline void
read_matrix(const std::string filenameS, const int m, const int n, float* A, const int lda)
{
const char* filename = filenameS.c_str();
FILE* mat;
mat = fopen(filename, "r");
float v;
if(mat == NULL)
throw std::invalid_argument(std::string("Error: Could not open file ") + filename
+ " with test data...");
for(int j = 0; j < n; ++j)
{
for(int i = 0; i < m; ++i)
{
int read = fscanf(mat, "%g", &v);
if(read != 1)
throw std::out_of_range(std::string("Error: Could not read element from file ")
+ filename);
A[i + j * lda] = v;
}
}
fclose(mat);
}
// doubles:
inline void
read_matrix(const std::string filenameS, const int m, const int n, double* A, const int lda)
{
const char* filename = filenameS.c_str();
FILE* mat;
mat = fopen(filename, "r");
double v;
if(mat == NULL)
throw std::invalid_argument(std::string("Error: Could not open file ") + filename
+ " with test data...");
for(int j = 0; j < n; ++j)
{
for(int i = 0; i < m; ++i)
{
int read = fscanf(mat, "%lg", &v);
if(read != 1)
throw std::out_of_range(std::string("Error: Could not read element from file ")
+ filename);
A[i + j * lda] = v;
}
}
fclose(mat);
}
// complex float:
inline void read_matrix(
const std::string filenameS, const int m, const int n, hipsolverComplex* A, const int lda)
{
const char* filename = filenameS.c_str();
FILE* mat;
mat = fopen(filename, "r");
float v;
if(mat == NULL)
throw std::invalid_argument(std::string("Error: Could not open file ") + filename
+ " with test data...");
for(int j = 0; j < n; ++j)
{
for(int i = 0; i < m; ++i)
{
int read = fscanf(mat, "%g", &v);
if(read != 1)
throw std::out_of_range(std::string("Error: Could not read element from file ")
+ filename);
A[i + j * lda] = {v, 0};
}
}
fclose(mat);
}
// complex double:
inline void read_matrix(
const std::string filenameS, const int m, const int n, hipsolverDoubleComplex* A, const int lda)
{
const char* filename = filenameS.c_str();
FILE* mat;
mat = fopen(filename, "r");
double v;
if(mat == NULL)
throw std::invalid_argument(std::string("Error: Could not open file ") + filename
+ " with test data...");
for(int j = 0; j < n; ++j)
{
for(int i = 0; i < m; ++i)
{
int read = fscanf(mat, "%lg", &v);
if(read != 1)
throw std::out_of_range(std::string("Error: Could not read element from file ")
+ filename);
A[i + j * lda] = {v, 0};
}
}
fclose(mat);
}
/* =============================================================================================== */
/* ============================================================================================ */
#endif // __cplusplus
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================================ */
/* timing: HIP only provides very limited timers function clock() and not general;
hipsolver sync CPU and device and use more accurate CPU timer*/
/*! \brief CPU Timer(in microsecond): synchronize with the default device and return wall time */
double get_time_us();
/*! \brief CPU Timer(in microsecond): synchronize with given queue/stream and return wall time */
double get_time_us_sync(hipStream_t stream);
/*! \brief CPU Timer(in microsecond): no GPU synchronization and return wall time */
double get_time_us_no_sync();
#ifdef __cplusplus
}
#endif
/* ============================================================================================ */
|