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
|
/*! \file */
/* ************************************************************************
* Copyright (C) 2019-2022 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
* copies 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
* IMPLIED, 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 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ************************************************************************ */
/*! \file
* \brief utility.hpp provides common utilities
*/
#pragma once
#ifndef UTILITY_HPP
#define UTILITY_HPP
#include "rocsparse_matrix.hpp"
#include "rocsparse_test.hpp"
#include <hip/hip_runtime_api.h>
#include <vector>
/* ==================================================================================== */
// Return index type
template <typename I>
inline rocsparse_indextype get_indextype(void);
/* ==================================================================================== */
// Return data type
template <typename T>
inline rocsparse_datatype get_datatype(void);
/*! \brief Return \ref rocsparse_indextype */
template <>
inline rocsparse_indextype get_indextype<uint16_t>(void)
{
return rocsparse_indextype_u16;
}
template <>
inline rocsparse_indextype get_indextype<int32_t>(void)
{
return rocsparse_indextype_i32;
}
template <>
inline rocsparse_indextype get_indextype<int64_t>(void)
{
return rocsparse_indextype_i64;
}
/*! \brief Return \ref rocsparse_datatype */
template <>
inline rocsparse_datatype get_datatype<float>(void)
{
return rocsparse_datatype_f32_r;
}
template <>
inline rocsparse_datatype get_datatype<double>(void)
{
return rocsparse_datatype_f64_r;
}
template <>
inline rocsparse_datatype get_datatype<rocsparse_float_complex>(void)
{
return rocsparse_datatype_f32_c;
}
template <>
inline rocsparse_datatype get_datatype<rocsparse_double_complex>(void)
{
return rocsparse_datatype_f64_c;
}
/* ==================================================================================== */
/*! \brief local handle which is automatically created and destroyed */
class rocsparse_local_handle
{
rocsparse_handle handle{};
public:
rocsparse_local_handle()
{
rocsparse_create_handle(&this->handle);
}
~rocsparse_local_handle()
{
rocsparse_destroy_handle(this->handle);
}
// Allow rocsparse_local_handle to be used anywhere rocsparse_handle is expected
operator rocsparse_handle&()
{
return this->handle;
}
operator const rocsparse_handle&() const
{
return this->handle;
}
};
/* ==================================================================================== */
/*! \brief local matrix descriptor which is automatically created and destroyed */
class rocsparse_local_mat_descr
{
rocsparse_mat_descr descr{};
public:
rocsparse_local_mat_descr()
{
rocsparse_create_mat_descr(&this->descr);
}
~rocsparse_local_mat_descr()
{
rocsparse_destroy_mat_descr(this->descr);
}
// Allow rocsparse_local_mat_descr to be used anywhere rocsparse_mat_descr is expected
operator rocsparse_mat_descr&()
{
return this->descr;
}
operator const rocsparse_mat_descr&() const
{
return this->descr;
}
};
/* ==================================================================================== */
/*! \brief local matrix info which is automatically created and destroyed */
class rocsparse_local_mat_info
{
rocsparse_mat_info info{};
public:
rocsparse_local_mat_info()
{
rocsparse_create_mat_info(&this->info);
}
~rocsparse_local_mat_info()
{
rocsparse_destroy_mat_info(this->info);
}
// Sometimes useful to reset local info
void reset()
{
rocsparse_destroy_mat_info(this->info);
rocsparse_create_mat_info(&this->info);
}
// Allow rocsparse_local_mat_info to be used anywhere rocsparse_mat_info is expected
operator rocsparse_mat_info&()
{
return this->info;
}
operator const rocsparse_mat_info&() const
{
return this->info;
}
};
/* ==================================================================================== */
/*! \brief local color info which is automatically created and destroyed */
class rocsparse_local_color_info
{
rocsparse_color_info info{};
public:
rocsparse_local_color_info()
{
rocsparse_create_color_info(&this->info);
}
~rocsparse_local_color_info()
{
rocsparse_destroy_color_info(this->info);
}
// Sometimes useful to reset local info
void reset()
{
rocsparse_destroy_color_info(this->info);
rocsparse_create_color_info(&this->info);
}
// Allow rocsparse_local_color_info to be used anywhere rocsparse_color_info is expected
operator rocsparse_color_info&()
{
return this->info;
}
operator const rocsparse_color_info&() const
{
return this->info;
}
};
/* ==================================================================================== */
/*! \brief hyb matrix structure helper to access data for tests */
struct test_hyb
{
rocsparse_int m;
rocsparse_int n;
rocsparse_hyb_partition partition;
rocsparse_int ell_nnz;
rocsparse_int ell_width;
rocsparse_int* ell_col_ind;
void* ell_val;
rocsparse_int coo_nnz;
rocsparse_int* coo_row_ind;
rocsparse_int* coo_col_ind;
void* coo_val;
};
/* ==================================================================================== */
/*! \brief local hyb matrix structure which is automatically created and destroyed */
class rocsparse_local_hyb_mat
{
rocsparse_hyb_mat hyb{};
public:
rocsparse_local_hyb_mat()
{
rocsparse_create_hyb_mat(&this->hyb);
}
~rocsparse_local_hyb_mat()
{
rocsparse_destroy_hyb_mat(this->hyb);
}
// Allow rocsparse_local_hyb_mat to be used anywhere rocsparse_hyb_mat is expected
operator rocsparse_hyb_mat&()
{
return this->hyb;
}
operator const rocsparse_hyb_mat&() const
{
return this->hyb;
}
};
/* ==================================================================================== */
/*! \brief local dense vector structure which is automatically created and destroyed */
class rocsparse_local_spvec
{
rocsparse_spvec_descr descr{};
public:
rocsparse_local_spvec(int64_t size,
int64_t nnz,
void* indices,
void* values,
rocsparse_indextype idx_type,
rocsparse_index_base idx_base,
rocsparse_datatype compute_type)
{
rocsparse_create_spvec_descr(
&this->descr, size, nnz, indices, values, idx_type, idx_base, compute_type);
}
~rocsparse_local_spvec()
{
if(this->descr != nullptr)
{
rocsparse_destroy_spvec_descr(this->descr);
}
}
// Allow rocsparse_local_spvec to be used anywhere rocsparse_spvec_descr is expected
operator rocsparse_spvec_descr&()
{
return this->descr;
}
operator const rocsparse_spvec_descr&() const
{
return this->descr;
}
};
/* ==================================================================================== */
/*! \brief local sparse matrix structure which is automatically created and destroyed */
class rocsparse_local_spmat
{
rocsparse_spmat_descr descr{};
public:
rocsparse_local_spmat(int64_t m,
int64_t n,
int64_t nnz,
void* coo_row_ind,
void* coo_col_ind,
void* coo_val,
rocsparse_indextype idx_type,
rocsparse_index_base idx_base,
rocsparse_datatype compute_type)
{
rocsparse_create_coo_descr(&this->descr,
m,
n,
nnz,
coo_row_ind,
coo_col_ind,
coo_val,
idx_type,
idx_base,
compute_type);
}
template <memory_mode::value_t MODE, typename T, typename I = rocsparse_int>
explicit rocsparse_local_spmat(coo_matrix<MODE, T, I>& h)
: rocsparse_local_spmat(h.m,
h.n,
h.nnz,
h.row_ind,
h.col_ind,
h.val,
get_indextype<I>(),
h.base,
get_datatype<T>())
{
}
rocsparse_local_spmat(int64_t m,
int64_t n,
int64_t nnz,
void* coo_ind,
void* coo_val,
rocsparse_indextype idx_type,
rocsparse_index_base idx_base,
rocsparse_datatype compute_type)
{
rocsparse_create_coo_aos_descr(
&this->descr, m, n, nnz, coo_ind, coo_val, idx_type, idx_base, compute_type);
}
template <memory_mode::value_t MODE, typename T, typename I = rocsparse_int>
explicit rocsparse_local_spmat(coo_aos_matrix<MODE, T, I>& h)
: rocsparse_local_spmat(
h.m, h.n, h.nnz, h.ind, h.val, get_indextype<I>(), h.base, get_datatype<T>())
{
}
rocsparse_local_spmat(int64_t m,
int64_t n,
int64_t nnz,
void* row_col_ptr,
void* row_col_ind,
void* val,
rocsparse_indextype row_col_ptr_type,
rocsparse_indextype row_col_ind_type,
rocsparse_index_base idx_base,
rocsparse_datatype compute_type,
rocsparse_format format)
{
if(format == rocsparse_format_csr)
{
rocsparse_create_csr_descr(&this->descr,
m,
n,
nnz,
row_col_ptr,
row_col_ind,
val,
row_col_ptr_type,
row_col_ind_type,
idx_base,
compute_type);
}
else
{
assert(format == rocsparse_format_csc);
rocsparse_create_csc_descr(&this->descr,
m,
n,
nnz,
row_col_ptr,
row_col_ind,
val,
row_col_ptr_type,
row_col_ind_type,
idx_base,
compute_type);
}
}
template <memory_mode::value_t MODE,
rocsparse_direction DIRECTION_,
typename T,
typename I = rocsparse_int,
typename J = rocsparse_int>
explicit rocsparse_local_spmat(csx_matrix<MODE, DIRECTION_, T, I, J>& h)
: rocsparse_local_spmat(h.m,
h.n,
h.nnz,
h.ptr,
h.ind,
h.val,
get_indextype<I>(),
get_indextype<J>(),
h.base,
get_datatype<T>(),
(DIRECTION_ == rocsparse_direction_row) ? rocsparse_format_csr
: rocsparse_format_csc)
{
}
rocsparse_local_spmat(int64_t mb,
int64_t nb,
int64_t nnzb,
rocsparse_direction block_dir,
int64_t block_dim,
void* row_col_ptr,
void* row_col_ind,
void* val,
rocsparse_indextype row_col_ptr_type,
rocsparse_indextype row_col_ind_type,
rocsparse_index_base idx_base,
rocsparse_datatype compute_type,
rocsparse_format format)
{
if(format == rocsparse_format_bsr)
{
rocsparse_create_bsr_descr(&this->descr,
mb,
nb,
nnzb,
block_dir,
block_dim,
row_col_ptr,
row_col_ind,
val,
row_col_ptr_type,
row_col_ind_type,
idx_base,
compute_type);
}
}
template <memory_mode::value_t MODE,
rocsparse_direction DIRECTION_,
typename T,
typename I = rocsparse_int,
typename J = rocsparse_int>
explicit rocsparse_local_spmat(gebsx_matrix<MODE, DIRECTION_, T, I, J>& h)
: rocsparse_local_spmat(h.mb,
h.nb,
h.nnzb,
h.block_direction,
(h.row_block_dim == h.col_block_dim) ? h.row_block_dim : -1,
h.ptr,
h.ind,
h.val,
get_indextype<I>(),
get_indextype<J>(),
h.base,
get_datatype<T>(),
rocsparse_format_bsr)
{
}
rocsparse_local_spmat(int64_t m,
int64_t n,
rocsparse_direction block_dir,
int64_t block_size,
int64_t ell_cols,
void* ell_col_ind,
void* ell_val,
rocsparse_indextype idx_type,
rocsparse_index_base idx_base,
rocsparse_datatype compute_type)
{
rocsparse_create_bell_descr(&this->descr,
m,
n,
block_dir,
block_size,
ell_cols,
ell_col_ind,
ell_val,
idx_type,
idx_base,
compute_type);
}
rocsparse_local_spmat(int64_t m,
int64_t n,
void* ell_col_ind,
void* ell_val,
int64_t ell_width,
rocsparse_indextype idx_type,
rocsparse_index_base idx_base,
rocsparse_datatype compute_type)
{
rocsparse_create_ell_descr(
&this->descr, m, n, ell_col_ind, ell_val, ell_width, idx_type, idx_base, compute_type);
}
template <memory_mode::value_t MODE, typename T, typename I = rocsparse_int>
explicit rocsparse_local_spmat(ell_matrix<MODE, T, I>& h)
: rocsparse_local_spmat(
h.m, h.n, h.ind, h.val, h.width, get_indextype<I>(), h.base, get_datatype<T>())
{
}
~rocsparse_local_spmat()
{
if(this->descr != nullptr)
rocsparse_destroy_spmat_descr(this->descr);
}
// Allow rocsparse_local_spmat to be used anywhere rocsparse_spmat_descr is expected
operator rocsparse_spmat_descr&()
{
return this->descr;
}
operator const rocsparse_spmat_descr&() const
{
return this->descr;
}
};
/* ==================================================================================== */
/*! \brief local dense vector structure which is automatically created and destroyed */
class rocsparse_local_dnvec
{
rocsparse_dnvec_descr descr{};
public:
rocsparse_local_dnvec(int64_t size, void* values, rocsparse_datatype compute_type)
{
rocsparse_create_dnvec_descr(&this->descr, size, values, compute_type);
}
template <memory_mode::value_t MODE, typename T>
explicit rocsparse_local_dnvec(dense_matrix<MODE, T>& h)
: rocsparse_local_dnvec(h.m, (T*)h, get_datatype<T>())
{
}
~rocsparse_local_dnvec()
{
if(this->descr != nullptr)
rocsparse_destroy_dnvec_descr(this->descr);
}
// Allow rocsparse_local_dnvec to be used anywhere rocsparse_dnvec_descr is expected
operator rocsparse_dnvec_descr&()
{
return this->descr;
}
operator const rocsparse_dnvec_descr&() const
{
return this->descr;
}
};
/* ==================================================================================== */
/*! \brief local dense matrix structure which is automatically created and destroyed */
class rocsparse_local_dnmat
{
rocsparse_dnmat_descr descr{};
public:
rocsparse_local_dnmat(int64_t rows,
int64_t cols,
int64_t ld,
void* values,
rocsparse_datatype compute_type,
rocsparse_order order)
{
rocsparse_create_dnmat_descr(&this->descr, rows, cols, ld, values, compute_type, order);
}
template <memory_mode::value_t MODE, typename T>
explicit rocsparse_local_dnmat(dense_matrix<MODE, T>& h)
: rocsparse_local_dnmat(h.m, h.n, h.ld, (T*)h, get_datatype<T>(), h.order)
{
}
~rocsparse_local_dnmat()
{
if(this->descr != nullptr)
rocsparse_destroy_dnmat_descr(this->descr);
}
// Allow rocsparse_local_dnmat to be used anywhere rocsparse_dnmat_descr is expected
operator rocsparse_dnmat_descr&()
{
return this->descr;
}
operator const rocsparse_dnmat_descr&() const
{
return this->descr;
}
};
/* ==================================================================================== */
/* timing: HIP only provides very limited timers function clock() and not general;
rocsparse 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(void);
/*! \brief CPU Timer(in microsecond): synchronize with given queue/stream and return
* wall time
*/
double get_time_us_sync(hipStream_t stream);
/* ==================================================================================== */
// Return path of this executable
std::string rocsparse_exepath();
#endif // UTILITY_HPP
|