okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- sparse_utils.hpp -------------------------*- C++ -*----------------==// 2// 3// Copyright (C) Intel Corporation 4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5// See https://llvm.org/LICENSE.txt for license information. 6// 7//===----------------------------------------------------------------------===// 8 9#ifndef __DPCT_SPARSE_UTILS_HPP__ 10#define __DPCT_SPARSE_UTILS_HPP__ 11 12#include "compat_service.hpp" 13#include "lib_common_utils.hpp" 14 15namespace dpct::sparse { 16class matrix_info { 17public : 18/// Matrix types are: 19/// ge: General matrix 20/// sy: Symmetric matrix 21/// he: Hermitian matrix 22/// tr: Triangular matrix 23enum class matrix_type :int {ge = 0 ,sy ,he ,tr }; 24 25auto get_matrix_type ()const {return _matrix_type ; } 26auto get_diag ()const {return _diag ; } 27auto get_uplo ()const {return _uplo ; } 28auto get_index_base ()const {return _index_base ; } 29void set_matrix_type (matrix_type mt ) {_matrix_type = mt ; } 30void set_diag (oneapi::mkl::diag d ) {_diag = d ; } 31void set_uplo (oneapi::mkl::uplo u ) {_uplo = u ; } 32void set_index_base (oneapi::mkl::index_base ib ) {_index_base = ib ; } 33 34private : 35matrix_type _matrix_type = matrix_type::ge ; 36 oneapi::mkl::diag _diag = oneapi::mkl::diag::nonunit ; 37 oneapi::mkl::uplo _uplo = oneapi::mkl::uplo::upper ; 38 oneapi::mkl::index_base _index_base = oneapi::mkl::index_base::zero ; 39}; 40 41/// Sparse matrix data format 42enum matrix_format :int {csr = 1 ,coo = 2 }; 43 44/// Sparse matrix attribute 45enum matrix_attribute :int {uplo = 0 ,diag }; 46 47enum class conversion_scope :int {index = 0 ,index_and_value }; 48 49#ifdef __INTEL_MKL__ // The oneMKL Interfaces Project does not support this. 50// Forward declaration 51namespace detail { 52template < typename T > struct optimize_csrsv_impl ; 53template < typename T > struct optimize_csrsm_impl ; 54} 55 56/// Saving the optimization information for solving a system of linear 57/// equations. 58class optimize_info { 59public : 60/// Constructor 61optimize_info () { oneapi::mkl::sparse::init_matrix_handle (& _matrix_handle ); } 62/// Destructor 63 ~optimize_info () { 64 oneapi::mkl::sparse::release_matrix_handle (::dpct::cs::get_default_queue (), 65& _matrix_handle ,_deps ) 66 .wait (); 67 } 68/// Add dependency for the destructor. 69/// \param [in] e The event which the destructor depends on. 70void add_dependency (sycl::event e ) {_deps .push_back (e ); } 71/// Get the internal saved matrix handle. 72/// \return Returns the matrix handle. 73 oneapi::mkl::sparse::matrix_handle_t get_matrix_handle ()const noexcept { 74return _matrix_handle ; 75 } 76#ifdef DPCT_USM_LEVEL_NONE 77template < typename T > friend struct detail::optimize_csrsv_impl ; 78template < typename T > friend struct detail::optimize_csrsm_impl ; 79#endif 80 81private : 82 oneapi::mkl::sparse::matrix_handle_t _matrix_handle = nullptr ; 83 std::vector < sycl::event > _deps ; 84#ifdef DPCT_USM_LEVEL_NONE 85static constexpr size_t _max_data_variable_size = 86 (std::max )({sizeof (sycl::buffer < float > ),sizeof (sycl::buffer < double > ), 87sizeof (sycl::buffer < std::complex < float >> ), 88sizeof (sycl::buffer < std::complex < double >> )}); 89using value_buf_t = 90 std::variant < std::array < std::byte ,_max_data_variable_size > , 91 sycl::buffer < float > , sycl::buffer < double > , 92 sycl::buffer < std::complex < float >> , 93 sycl::buffer < std::complex < double >>> ; 94 sycl::buffer < int > _row_ptr_buf = sycl::buffer < int > (0 ); 95 sycl::buffer < int > _col_ind_buf = sycl::buffer < int > (0 ); 96value_buf_t _val_buf ; 97#endif 98}; 99 100/// Structure for describe a sparse matrix 101class sparse_matrix_desc { 102public : 103/// Constructor 104/// \param [out] desc The descriptor to be created 105/// \param [in] row_num Number of rows of the sparse matrix. 106/// \param [in] col_num Number of colums of the sparse matrix. 107/// \param [in] nnz Non-zero elements in the sparse matrix. 108/// \param [in] row_ptr An array of length \p row_num + 1. If the \p row_ptr is 109/// NULL, the sparse_matrix_desc will allocate internal memory for it. This 110/// internal memory can be gotten from get_shadow_row_ptr(). 111/// \param [in] col_ind An array containing the column indices in index-based 112/// numbering. 113/// \param [in] value An array containing the non-zero elements of the sparse matrix. 114/// \param [in] row_ptr_type Data type of the \p row_ptr . 115/// \param [in] col_ind_type Data type of the \p col_ind . 116/// \param [in] base Indicates how input arrays are indexed. 117/// \param [in] value_type Data type of the \p value . 118/// \param [in] data_format The matrix data format. 119sparse_matrix_desc (std::int64_t row_num , std::int64_t col_num , 120 std::int64_t nnz ,void * row_ptr ,void * col_ind , 121void * value ,library_data_t row_ptr_type , 122library_data_t col_ind_type , oneapi::mkl::index_base base , 123library_data_t value_type ,matrix_format data_format ) 124 :_row_num (row_num ),_col_num (col_num ),_nnz (nnz ),_row_ptr (row_ptr ), 125_col_ind (col_ind ),_value (value ),_row_ptr_type (row_ptr_type ), 126_col_ind_type (col_ind_type ),_base (base ),_value_type (value_type ), 127_data_format (data_format ) { 128if (_data_format != matrix_format::csr && 129_data_format != matrix_format::coo ) { 130throw std::runtime_error ("the sparse matrix data format is unsupported" ); 131 } 132 oneapi::mkl::sparse::init_matrix_handle (& _matrix_handle ); 133set_data (); 134 } 135/// Destructor 136 ~sparse_matrix_desc () { 137 oneapi::mkl::sparse::release_matrix_handle (::dpct::cs::get_default_queue (), 138& _matrix_handle ,_deps ) 139 .wait (); 140 } 141 142/// Add dependency for the destroy method. 143/// \param [in] e The event which the destroy method depends on. 144void add_dependency (sycl::event e ) {_deps .push_back (e ); } 145/// Get the internal saved matrix handle. 146/// \return Returns the matrix handle. 147 oneapi::mkl::sparse::matrix_handle_t get_matrix_handle ()const noexcept { 148return _matrix_handle ; 149 } 150/// Get the values saved in the descriptor 151/// \param [out] row_num Number of rows of the sparse matrix. 152/// \param [out] col_num Number of colums of the sparse matrix. 153/// \param [out] nnz Non-zero elements in the sparse matrix. 154/// \param [out] row_ptr An array of length \p row_num + 1. 155/// \param [out] col_ind An array containing the column indices in index-based 156/// numbering. 157/// \param [out] value An array containing the non-zero elements of the sparse matrix. 158/// \param [out] row_ptr_type Data type of the \p row_ptr . 159/// \param [out] col_ind_type Data type of the \p col_ind . 160/// \param [out] base Indicates how input arrays are indexed. 161/// \param [out] value_type Data type of the \p value . 162void get_desc (int64_t * row_num ,int64_t * col_num ,int64_t * nnz , 163void ** row_ptr ,void ** col_ind ,void ** value , 164library_data_t * row_ptr_type ,library_data_t * col_ind_type , 165 oneapi::mkl::index_base * base , 166library_data_t * value_type )const noexcept { 167* row_num = _row_num ; 168* col_num = _col_num ; 169* nnz = _nnz ; 170* row_ptr = _row_ptr ; 171* col_ind = _col_ind ; 172* value = _value ; 173* row_ptr_type = _row_ptr_type ; 174* col_ind_type = _col_ind_type ; 175* base = _base ; 176* value_type = _value_type ; 177 } 178/// Get the sparse matrix data format of this descriptor 179/// \param [out] format The matrix data format result 180void get_format (matrix_format * data_format )const noexcept { 181* data_format = _data_format ; 182 } 183/// Get the index base of this descriptor 184/// \param [out] base The index base result 185void get_base (oneapi::mkl::index_base * base )const noexcept {* base = _base ; } 186/// Get the value pointer of this descriptor 187/// \param [out] value The value pointer result 188void get_value (void ** value )const noexcept {* value = _value ; } 189/// Set the value pointer of this descriptor 190/// \param [in] value The input value pointer 191void set_value (void * value ) { 192if (!value ) { 193throw std::runtime_error ( 194"dpct::sparse::sparse_matrix_desc::set_value(): The value " 195"pointer is NULL." ); 196 } 197if (_value && (_value != value )) { 198throw std::runtime_error ( 199"dpct::sparse::sparse_matrix_desc::set_value(): " 200"The _value pointer is not NULL. It cannot be reset." ); 201 } 202_value = value ; 203set_data (); 204 } 205/// Get the size of the sparse matrix 206/// \param [out] row_num Number of rows of the sparse matrix. 207/// \param [out] col_num Number of colums of the sparse matrix. 208/// \param [out] nnz Non-zero elements in the sparse matrix. 209void get_size (int64_t * row_num ,int64_t * col_num , 210int64_t * nnz )const noexcept { 211* row_num = _row_num ; 212* col_num = _col_num ; 213* nnz = _nnz ; 214 } 215/// Set the sparse matrix attribute 216/// \param [in] attribute The attribute type 217/// \param [in] data The attribute value 218/// \param [in] data_size The data size of the attribute value 219void set_attribute (matrix_attribute attribute ,const void * data , 220size_t data_size ) { 221if (attribute == matrix_attribute::diag ) { 222const oneapi::mkl::diag * diag_ptr = 223reinterpret_cast < const oneapi::mkl::diag *> (data ); 224if (* diag_ptr == oneapi::mkl::diag::unit ) { 225_diag = oneapi::mkl::diag::unit ; 226 }else if (* diag_ptr == oneapi::mkl::diag::nonunit ) { 227_diag = oneapi::mkl::diag::nonunit ; 228 }else { 229throw std::runtime_error ("unsupported diag value" ); 230 } 231 }else if (attribute == matrix_attribute::uplo ) { 232const oneapi::mkl::uplo * uplo_ptr = 233reinterpret_cast < const oneapi::mkl::uplo *> (data ); 234if (* uplo_ptr == oneapi::mkl::uplo::upper ) { 235_uplo = oneapi::mkl::uplo::upper ; 236 }else if (* uplo_ptr == oneapi::mkl::uplo::lower ) { 237_uplo = oneapi::mkl::uplo::lower ; 238 }else { 239throw std::runtime_error ("unsupported uplo value" ); 240 } 241 }else { 242throw std::runtime_error ("unsupported attribute" ); 243 } 244 } 245/// Get the sparse matrix attribute 246/// \param [out] attribute The attribute type 247/// \param [out] data The attribute value 248/// \param [out] data_size The data size of the attribute value 249void get_attribute (matrix_attribute attribute ,void * data , 250size_t data_size )const { 251if (attribute == matrix_attribute::diag ) { 252 oneapi::mkl::diag * diag_ptr = reinterpret_cast < oneapi::mkl::diag *> (data ); 253if (_diag .has_value ()) { 254* diag_ptr = _diag .value (); 255 }else { 256* diag_ptr = oneapi::mkl::diag::nonunit ; 257 } 258 }else if (attribute == matrix_attribute::uplo ) { 259 oneapi::mkl::uplo * uplo_ptr = reinterpret_cast < oneapi::mkl::uplo *> (data ); 260if (_uplo .has_value ()) { 261* uplo_ptr = _uplo .value (); 262 }else { 263* uplo_ptr = oneapi::mkl::uplo::lower ; 264 } 265 }else { 266throw std::runtime_error ("unsupported attribute" ); 267 } 268 } 269/// Set the pointers for describing the sparse matrix 270/// \param [in] row_ptr An array of length \p row_num + 1. 271/// \param [in] col_ind An array containing the column indices in index-based 272/// numbering. 273/// \param [in] value An array containing the non-zero elements of the sparse matrix. 274void set_pointers (void * row_ptr ,void * col_ind ,void * value ) { 275if (!row_ptr ) { 276throw std::runtime_error ( 277"dpct::sparse::sparse_matrix_desc::set_pointers(): The " 278"row_ptr pointer is NULL." ); 279 } 280if (!col_ind ) { 281throw std::runtime_error ( 282"dpct::sparse::sparse_matrix_desc::set_pointers(): The " 283"col_ind pointer is NULL." ); 284 } 285if (_row_ptr && (_row_ptr != row_ptr )) { 286throw std::runtime_error ("dpct::sparse::sparse_matrix_desc::set_pointers(" 287"): The _row_ptr pointer is " 288"not NULL. It cannot be reset." ); 289 } 290if (_col_ind && (_col_ind != col_ind )) { 291throw std::runtime_error ("dpct::sparse::sparse_matrix_desc::set_pointers(" 292"): The _col_ind pointer is " 293"not NULL. It cannot be reset." ); 294 } 295_row_ptr = row_ptr ; 296_col_ind = col_ind ; 297 298// The descriptor will be updated in the set_value function 299set_value (value ); 300 } 301 302/// Get the diag attribute 303/// \return diag value 304 std::optional < oneapi::mkl::diag > get_diag ()const noexcept {return _diag ; } 305/// Get the uplo attribute 306/// \return uplo value 307 std::optional < oneapi::mkl::uplo > get_uplo ()const noexcept {return _uplo ; } 308/// Set the number of non-zero elements 309/// \param nnz [in] The number of non-zero elements. 310void set_nnz (std::int64_t nnz )noexcept {_nnz = nnz ; } 311/// Get the type of the value pointer. 312/// \return The type of the value pointer. 313library_data_t get_value_type ()const noexcept {return _value_type ; } 314/// Get the row_ptr. 315/// \return The row_ptr. 316void * get_row_ptr ()const noexcept {return _row_ptr ; } 317/// If the internal _row_ptr is NULL, the sparse_matrix_desc will allocate 318/// internal memory for it in the constructor. The internal memory can be gotten 319/// from this interface. 320/// \return The shadow row_ptr. 321void * get_shadow_row_ptr ()const noexcept {return _shadow_row_ptr .get (); } 322/// Get the type of the col_ind pointer. 323/// \return The type of the col_ind pointer. 324library_data_t get_col_ind_type ()const noexcept {return _col_ind_type ; } 325/// Get the row_num. 326/// \return The row_num. 327 std::int64_t get_row_num ()const noexcept {return _row_num ; } 328 329private : 330inline static const std::function < void (void * )> _shadow_row_ptr_deleter = 331 [](void * ptr ) { ::dpct::cs::free (ptr , ::dpct::cs::get_default_queue ()); }; 332template < typename index_t ,typename value_t > void set_data () { 333void * row_ptr = nullptr ; 334if (_shadow_row_ptr ) { 335row_ptr = _shadow_row_ptr .get (); 336 }else if (_row_ptr ) { 337row_ptr = _row_ptr ; 338 }else { 339row_ptr = ::dpct::cs::malloc (sizeof (index_t )* (_row_num + 1 ), 340 ::dpct::cs::get_default_queue ()); 341_shadow_row_ptr .reset (row_ptr ); 342 } 343#ifdef DPCT_USM_LEVEL_NONE 344using data_index_t = sycl::buffer < index_t > ; 345using data_value_t = sycl::buffer < value_t > ; 346#else 347using data_index_t = index_t * ; 348using data_value_t = value_t * ; 349#endif 350_data_row_ptr = dpct::detail::get_memory < index_t > (row_ptr ); 351_data_col_ind = dpct::detail::get_memory < index_t > (_col_ind ); 352_data_value = dpct::detail::get_memory < value_t > (_value ); 353if (_data_format == matrix_format::csr ) 354 oneapi::mkl::sparse::set_csr_data ( 355 ::dpct::cs::get_default_queue (),_matrix_handle ,_row_num ,_col_num , 356_base , std::get < data_index_t > (_data_row_ptr ), 357 std::get < data_index_t > (_data_col_ind ), 358 std::get < data_value_t > (_data_value )); 359else 360 oneapi::mkl::sparse::set_coo_data ( 361 ::dpct::cs::get_default_queue (),_matrix_handle ,_row_num ,_col_num , 362_nnz ,_base , std::get < data_index_t > (_data_row_ptr ), 363 std::get < data_index_t > (_data_col_ind ), 364 std::get < data_value_t > (_data_value )); 365 ::dpct::cs::get_default_queue ().wait (); 366 } 367 368void set_data () { 369 std::uint64_t key = dpct::detail::get_type_combination_id ( 370_row_ptr_type ,_col_ind_type ,_value_type ); 371switch (key ) { 372case dpct::detail::get_type_combination_id (library_data_t::real_int32 , 373 library_data_t::real_int32 , 374 library_data_t::real_float ): { 375set_data < std::int32_t ,float > (); 376break ; 377 } 378case dpct::detail::get_type_combination_id (library_data_t::real_int32 , 379 library_data_t::real_int32 , 380 library_data_t::real_double ): { 381set_data < std::int32_t ,double > (); 382break ; 383 } 384case dpct::detail::get_type_combination_id (library_data_t::real_int32 , 385 library_data_t::real_int32 , 386 library_data_t::complex_float ): { 387set_data < std::int32_t , std::complex < float >> (); 388break ; 389 } 390case dpct::detail::get_type_combination_id ( 391 library_data_t::real_int32 , library_data_t::real_int32 , 392 library_data_t::complex_double ): { 393set_data < std::int32_t , std::complex < double >> (); 394break ; 395 } 396case dpct::detail::get_type_combination_id (library_data_t::real_int64 , 397 library_data_t::real_int64 , 398 library_data_t::real_float ): { 399set_data < std::int64_t ,float > (); 400break ; 401 } 402case dpct::detail::get_type_combination_id (library_data_t::real_int64 , 403 library_data_t::real_int64 , 404 library_data_t::real_double ): { 405set_data < std::int64_t ,double > (); 406break ; 407 } 408case dpct::detail::get_type_combination_id (library_data_t::real_int64 , 409 library_data_t::real_int64 , 410 library_data_t::complex_float ): { 411set_data < std::int64_t , std::complex < float >> (); 412break ; 413 } 414case dpct::detail::get_type_combination_id ( 415 library_data_t::real_int64 , library_data_t::real_int64 , 416 library_data_t::complex_double ): { 417set_data < std::int64_t , std::complex < double >> (); 418break ; 419 } 420default : 421throw std::runtime_error ("the combination of data type is unsupported" ); 422 } 423 } 424 425 std::int64_t _row_num ; 426 std::int64_t _col_num ; 427 std::int64_t _nnz ; 428void * _row_ptr ; 429void * _col_ind ; 430void * _value ; 431library_data_t _row_ptr_type ; 432library_data_t _col_ind_type ; 433 oneapi::mkl::index_base _base ; 434library_data_t _value_type ; 435 oneapi::mkl::sparse::matrix_handle_t _matrix_handle = nullptr ; 436 std::vector < sycl::event > _deps ; 437matrix_format _data_format ; 438 std::optional < oneapi::mkl::uplo > _uplo ; 439 std::optional < oneapi::mkl::diag > _diag ; 440 std::unique_ptr < void , std::function < void (void * )>> _shadow_row_ptr = 441 std::unique_ptr < void , std::function < void (void * )>> ( 442nullptr ,_shadow_row_ptr_deleter ); 443 444static constexpr size_t _max_data_variable_size = (std::max )( 445 {sizeof (sycl::buffer < std::int32_t > ),sizeof (sycl::buffer < std::int64_t > ), 446sizeof (sycl::buffer < float > ),sizeof (sycl::buffer < double > ), 447sizeof (sycl::buffer < std::complex < float >> ), 448sizeof (sycl::buffer < std::complex < double >> ),sizeof (void * )}); 449using index_variant_t = 450 std::variant < std::array < std::byte ,_max_data_variable_size > , 451 sycl::buffer < std::int32_t > , sycl::buffer < std::int64_t > , 452 std::int32_t * , std::int64_t *> ; 453using value_variant_t = 454 std::variant < std::array < std::byte ,_max_data_variable_size > , 455 sycl::buffer < float > , sycl::buffer < double > , 456 sycl::buffer < std::complex < float >> , 457 sycl::buffer < std::complex < double >> ,float * ,double * , 458 std::complex < float > * , std::complex < double > *> ; 459index_variant_t _data_row_ptr ; 460index_variant_t _data_col_ind ; 461value_variant_t _data_value ; 462}; 463#endif 464 465class sparse_matrix_desc ; 466using sparse_matrix_desc_t = std::shared_ptr < sparse_matrix_desc > ; 467 468/// Structure for describe a dense vector 469class dense_vector_desc { 470public : 471dense_vector_desc (std::int64_t ele_num ,void * value , 472library_data_t value_type ) 473 :_ele_num (ele_num ),_value (value ),_value_type (value_type ) {} 474void get_desc (std::int64_t * ele_num ,const void ** value , 475library_data_t * value_type )const noexcept { 476* ele_num = _ele_num ; 477* value = _value ; 478* value_type = _value_type ; 479 } 480void get_desc (std::int64_t * ele_num ,void ** value , 481library_data_t * value_type )const noexcept { 482get_desc (ele_num ,const_cast < const void **> (value ),value_type ); 483 } 484void * get_value ()const noexcept {return _value ; } 485void set_value (void * value ) {_value = value ; } 486library_data_t get_value_type ()const noexcept {return _value_type ; } 487 std::int64_t get_ele_num ()const noexcept {return _ele_num ; } 488 489private : 490 std::int64_t _ele_num ; 491void * _value ; 492library_data_t _value_type ; 493}; 494 495/// Structure for describe a dense matrix 496class dense_matrix_desc { 497public : 498dense_matrix_desc (std::int64_t row_num , std::int64_t col_num , 499 std::int64_t leading_dim ,void * value , 500library_data_t value_type , oneapi::mkl::layout layout ) 501 :_row_num (row_num ),_col_num (col_num ),_leading_dim (leading_dim ), 502_value (value ),_value_type (value_type ),_layout (layout ) {} 503void get_desc (std::int64_t * row_num , std::int64_t * col_num , 504 std::int64_t * leading_dim ,void ** value , 505library_data_t * value_type , 506 oneapi::mkl::layout * layout )const noexcept { 507* row_num = _row_num ; 508* col_num = _col_num ; 509* leading_dim = _leading_dim ; 510* value = _value ; 511* value_type = _value_type ; 512* layout = _layout ; 513 } 514void * get_value ()const noexcept {return _value ; } 515void set_value (void * value ) {_value = value ; } 516 std::int64_t get_col_num ()const noexcept {return _col_num ; } 517 std::int64_t get_leading_dim ()const noexcept {return _leading_dim ; } 518 oneapi::mkl::layout get_layout ()const noexcept {return _layout ; } 519 520private : 521 std::int64_t _row_num ; 522 std::int64_t _col_num ; 523 std::int64_t _leading_dim ; 524void * _value ; 525library_data_t _value_type ; 526 oneapi::mkl::layout _layout ; 527}; 528}// namespace dpct::sparse 529 530#include "detail/sparse_utils_detail.hpp" 531 532namespace dpct::sparse { 533class descriptor { 534public : 535descriptor () {} 536void set_queue (::dpct::cs::queue_ptr q_ptr )noexcept {_queue_ptr = q_ptr ; } 537 sycl::queue & get_queue ()const noexcept {return * _queue_ptr ; } 538 539private : 540#ifdef __INTEL_MKL__ 541struct matmat_info { 542 detail::matrix_handle_manager matrix_handle_a ; 543 detail::matrix_handle_manager matrix_handle_b ; 544 detail::matrix_handle_manager matrix_handle_c ; 545 detail::handle_manager < oneapi::mkl::sparse::matmat_descr_t > matmat_desc ; 546bool is_empty () {return matmat_desc .is_empty (); } 547void init (sycl::queue * q_ptr ) { 548matmat_desc .init (q_ptr ); 549matrix_handle_a .init (q_ptr ); 550matrix_handle_b .init (q_ptr ); 551matrix_handle_c .init (q_ptr ); 552 } 553 }; 554 std::unordered_map < detail::csrgemm_args_info ,matmat_info , 555 detail::csrgemm_args_info_hash > & 556get_csrgemm_info_map () { 557return _csrgemm_info_map ; 558 } 559 560 std::unordered_map < detail::csrgemm_args_info ,matmat_info , 561 detail::csrgemm_args_info_hash > 562_csrgemm_info_map ; 563 564template < typename T > 565friend void 566csrgemm_nnz (descriptor * desc , oneapi::mkl::transpose trans_a , 567 oneapi::mkl::transpose trans_b ,int m ,int n ,int k , 568const std::shared_ptr < matrix_info > info_a ,int nnz_a , 569const T * val_a ,const int * row_ptr_a ,const int * col_ind_a , 570const std::shared_ptr < matrix_info > info_b ,int nnz_b , 571const T * val_b ,const int * row_ptr_b ,const int * col_ind_b , 572const std::shared_ptr < matrix_info > info_c ,int * row_ptr_c , 573int * nnz_host_ptr ); 574template < typename T > 575friend void csrgemm (descriptor * desc , oneapi::mkl::transpose trans_a , 576 oneapi::mkl::transpose trans_b ,int m ,int n ,int k , 577const std::shared_ptr < matrix_info > info_a ,const T * val_a , 578const int * row_ptr_a ,const int * col_ind_a , 579const std::shared_ptr < matrix_info > info_b ,const T * val_b , 580const int * row_ptr_b ,const int * col_ind_b , 581const std::shared_ptr < matrix_info > info_c ,T * val_c , 582const int * row_ptr_c ,int * col_ind_c ); 583#endif 584 ::dpct::cs::queue_ptr _queue_ptr = & ::dpct::cs::get_default_queue (); 585}; 586 587using descriptor_ptr = descriptor * ; 588 589/// Computes a CSR format sparse matrix-dense vector product. 590/// y = alpha * op(A) * x + beta * y 591/// \param [in] queue The queue where the routine should be executed. It must 592/// have the in_order property when using the USM mode. 593/// \param [in] trans The operation applied to the matrix A. 594/// \param [in] num_rows Number of rows of the matrix A. 595/// \param [in] num_cols Number of columns of the matrix A. 596/// \param [in] alpha Scaling factor for the matrix A. 597/// \param [in] info Matrix info of the matrix A. 598/// \param [in] val An array containing the non-zero elements of the matrix A. 599/// \param [in] row_ptr An array of length \p num_rows + 1. 600/// \param [in] col_ind An array containing the column indices in index-based 601/// numbering. 602/// \param [in] x Data of the vector x. 603/// \param [in] beta Scaling factor for the vector x. 604/// \param [in, out] y Data of the vector y. 605template < typename T > 606void csrmv (sycl::queue & queue , oneapi::mkl::transpose trans ,int num_rows , 607int num_cols ,const T * alpha , 608const std::shared_ptr < matrix_info > info ,const T * val , 609const int * row_ptr ,const int * col_ind ,const T * x ,const T * beta , 610T * y ) { 611 detail::csrmv_impl < T > ()(queue ,trans ,num_rows ,num_cols ,alpha ,info ,val , 612row_ptr ,col_ind ,x ,beta ,y ); 613} 614 615/// Computes a CSR format sparse matrix-dense vector product. y = A * x 616/// 617/// \param [in] queue The queue where the routine should be executed. It must 618/// have the in_order property when using the USM mode. 619/// \param [in] values An array containing the non-zero elements of the matrix. 620/// \param [in] row_offsets An array of length \p num_rows + 1. 621/// \param [in] column_indices An array containing the column indices in 622/// index-based numbering. 623/// \param [in] vector_x Data of the vector x. 624/// \param [in, out] vector_y Data of the vector y. 625/// \param [in] num_rows Number of rows of the matrix A. 626/// \param [in] num_cols Number of columns of the matrix A. 627template < typename T > 628void csrmv (sycl::queue & queue ,const T * values ,const int * row_offsets , 629const int * column_indices ,const T * vector_x ,T * vector_y , 630int num_rows ,int num_cols ) { 631T alpha {1 },beta {0 }; 632auto matrix_info = std::make_shared < dpct::sparse::matrix_info > (); 633matrix_info -> set_index_base (oneapi::mkl::index_base::zero ); 634matrix_info -> set_matrix_type (dpct::sparse::matrix_info::matrix_type::ge ); 635 detail::csrmv_impl < T > ()(queue , oneapi::mkl::transpose::nontrans ,num_rows , 636num_cols ,& alpha ,matrix_info ,values ,row_offsets , 637column_indices ,vector_x ,& beta ,vector_y ); 638} 639 640/// Computes a CSR format sparse matrix-dense vector product. 641/// y = alpha * op(A) * x + beta * y 642/// \param [in] queue The queue where the routine should be executed. It must 643/// have the in_order property when using the USM mode. 644/// \param [in] trans The operation applied to the matrix A. 645/// \param [in] num_rows Number of rows of the matrix A. 646/// \param [in] num_cols Number of columns of the matrix A. 647/// \param [in] alpha Scaling factor for the matrix A. 648/// \param [in] alpha_type Data type of \p alpha . 649/// \param [in] info Matrix info of the matrix A. 650/// \param [in] val An array containing the non-zero elements of the matrix A. 651/// \param [in] val_type Data type of \p val . 652/// \param [in] row_ptr An array of length \p num_rows + 1. 653/// \param [in] col_ind An array containing the column indices in index-based 654/// numbering. 655/// \param [in] x Data of the vector x. 656/// \param [in] x_type Data type of \p x . 657/// \param [in] beta Scaling factor for the vector x. 658/// \param [in] beta_type Data type of \p beta . 659/// \param [in, out] y Data of the vector y. 660/// \param [in] y_type Data type of \p y . 661inline void csrmv (sycl::queue & queue , oneapi::mkl::transpose trans , 662int num_rows ,int num_cols ,const void * alpha , 663library_data_t alpha_type , 664const std::shared_ptr < matrix_info > info ,const void * val , 665library_data_t val_type ,const int * row_ptr , 666const int * col_ind ,const void * x ,library_data_t x_type , 667const void * beta ,library_data_t beta_type ,void * y , 668library_data_t y_type ) { 669 detail::spblas_shim < detail::csrmv_impl > (val_type ,queue ,trans ,num_rows , 670num_cols ,alpha ,info ,val ,row_ptr , 671col_ind ,x ,beta ,y ); 672} 673 674/// Computes a CSR format sparse matrix-dense matrix product. 675/// C = alpha * op(A) * op(B) + beta * C 676/// \param [in] queue The queue where the routine should be executed. It must 677/// have the in_order property when using the USM mode. 678/// \param [in] trans_a The operation applied to the matrix A. 679/// \param [in] trans_b The operation applied to the matrix B. 680/// \param [in] sparse_rows Number of rows of the matrix A. 681/// \param [in] dense_cols Number of columns of the matrix B or C. 682/// \param [in] sparse_cols Number of columns of the matrix A. 683/// \param [in] alpha Scaling factor for the matrix A. 684/// \param [in] info Matrix info of the matrix A. 685/// \param [in] val An array containing the non-zero elements of the matrix A. 686/// \param [in] row_ptr An array of length \p num_rows + 1. 687/// \param [in] col_ind An array containing the column indices in index-based 688/// numbering. 689/// \param [in] b Data of the matrix B. 690/// \param [in] ldb Leading dimension of the matrix B. 691/// \param [in] beta Scaling factor for the matrix B. 692/// \param [in, out] c Data of the matrix C. 693/// \param [in] ldc Leading dimension of the matrix C. 694template < typename T > 695void csrmm (sycl::queue & queue , oneapi::mkl::transpose trans_a , 696 oneapi::mkl::transpose trans_b ,int sparse_rows ,int dense_cols , 697int sparse_cols ,const T * alpha , 698const std::shared_ptr < matrix_info > info ,const T * val , 699const int * row_ptr ,const int * col_ind ,const T * b ,int ldb , 700const T * beta ,T * c ,int ldc ) { 701#ifndef __INTEL_MKL__ 702throw std::runtime_error ("The oneAPI Math Kernel Library (oneMKL) Interfaces " 703"Project does not support this API." ); 704#else 705using Ty = typename ::dpct::detail::lib_data_traits_t < T > ; 706auto alpha_value = 707 dpct::detail::get_value (reinterpret_cast < const Ty *> (alpha ),queue ); 708auto beta_value = 709 dpct::detail::get_value (reinterpret_cast < const Ty *> (beta ),queue ); 710 711 oneapi::mkl::sparse::matrix_handle_t * sparse_matrix_handle = 712new oneapi::mkl::sparse::matrix_handle_t ; 713 oneapi::mkl::sparse::init_matrix_handle (sparse_matrix_handle ); 714auto data_row_ptr = dpct::detail::get_memory < int > (row_ptr ); 715auto data_col_ind = dpct::detail::get_memory < int > (col_ind ); 716auto data_val = dpct::detail::get_memory < Ty > (val ); 717 oneapi::mkl::sparse::set_csr_data (queue ,* sparse_matrix_handle ,sparse_rows , 718sparse_cols ,info -> get_index_base (), 719data_row_ptr ,data_col_ind ,data_val ); 720 721auto data_b = dpct::detail::get_memory < Ty > (b ); 722auto data_c = dpct::detail::get_memory < Ty > (c ); 723 sycl::event gemm_event ; 724switch (info -> get_matrix_type ()) { 725case matrix_info::matrix_type::ge : { 726#ifndef DPCT_USM_LEVEL_NONE 727gemm_event = 728#endif 729 oneapi::mkl::sparse::gemm (queue , oneapi::mkl::layout::col_major , 730trans_a ,trans_b ,alpha_value , 731* sparse_matrix_handle ,data_b ,dense_cols , 732ldb ,beta_value ,data_c ,ldc ); 733break ; 734} 735default: 736throw std:: runtime_error ( 737"the csrmm does not support matrix_info::matrix_type::sy, " 738"matrix_info::matrix_type::tr and matrix_info::matrix_type::he" ); 739} 740#ifdef DPCT_USM_LEVEL_NONE 741queue . wait (); 742#endif 743sycl:: event e = oneapi::mkl::sparse:: release_matrix_handle ( 744queue , sparse_matrix_handle , { gemm_event }); 745queue . submit ([ & ](sycl:: handler & cgh ) { 746cgh . depends_on ( e ); 747cgh . host_task ([ = ] { delete sparse_matrix_handle ; }); 748}); 749#endif 750} 751 752/// Computes a CSR format sparse matrix-dense matrix product. 753/// C = alpha * op(A) * B + beta * C 754/// \param [in] queue The queue where the routine should be executed. It must 755/// have the in_order property when using the USM mode. 756/// \param [in] trans The operation applied to the matrix A. 757/// \param [in] sparse_rows Number of rows of the matrix A. 758/// \param [in] dense_cols Number of columns of the matrix op(B) or C. 759/// \param [in] sparse_cols Number of columns of the matrix A. 760/// \param [in] alpha Scaling factor for the matrix A. 761/// \param [in] info Matrix info of the matrix A. 762/// \param [in] val An array containing the non-zero elements of the matrix A. 763/// \param [in] row_ptr An array of length \p num_rows + 1. 764/// \param [in] col_ind An array containing the column indices in index-based 765/// numbering. 766/// \param [in] b Data of the matrix B. 767/// \param [in] ldb Leading dimension of the matrix B. 768/// \param [in] beta Scaling factor for the matrix B. 769/// \param [in, out] c Data of the matrix C. 770/// \param [in] ldc Leading dimension of the matrix C. 771template < typename T > 772void csrmm (sycl:: queue & queue , oneapi::mkl:: transpose trans , int sparse_rows , 773int dense_cols , int sparse_cols , const T * alpha , 774const std:: shared_ptr < matrix_info > info , const T * val , 775const int * row_ptr , const int * col_ind , const T * b , int ldb , 776const T * beta , T * c , int ldc ) { 777csrmm < T > ( queue , trans , oneapi::mkl::transpose:: nontrans , sparse_rows , 778dense_cols , sparse_cols , alpha , info , val , row_ptr , col_ind , b , ldb , 779beta , c , ldc ); 780} 781 782#ifdef __INTEL_MKL__ // The oneMKL Interfaces Project does not support this. 783/// Performs internal optimizations for solving a system of linear equations for 784/// a CSR format sparse matrix. 785/// \param [in] queue The queue where the routine should be executed. It must 786/// have the in_order property when using the USM mode. 787/// \param [in] trans The operation applied to the sparse matrix. 788/// \param [in] row_col Number of rows of the sparse matrix. 789/// \param [in] info Matrix info of the sparse matrix. 790/// \param [in] val An array containing the non-zero elements of the sparse matrix. 791/// \param [in] row_ptr An array of length \p num_rows + 1. 792/// \param [in] col_ind An array containing the column indices in index-based 793/// numbering. 794/// \param [out] optimize_info The result of the optimizations. 795template < typename T > 796void optimize_csrsv (sycl:: queue & queue , oneapi::mkl:: transpose trans , 797int row_col , const std:: shared_ptr < matrix_info > info , 798const T * val , const int * row_ptr , const int * col_ind , 799std:: shared_ptr < optimize_info > optimize_info ) { 800detail:: optimize_csrsv_impl < T > ()( queue , trans , row_col , info , val , row_ptr , 801col_ind , optimize_info ); 802} 803 804inline void optimize_csrsv (sycl:: queue & queue , oneapi::mkl:: transpose trans , 805int row_col , const std:: shared_ptr < matrix_info > info , 806const void * val , library_data_t val_type , 807const int * row_ptr , const int * col_ind , 808std:: shared_ptr < optimize_info > optimize_info ) { 809detail:: spblas_shim < detail:: optimize_csrsv_impl > ( 810val_type , queue , trans , row_col , info , val , row_ptr , col_ind , 811optimize_info ); 812} 813 814template < typename T > 815void csrsv (sycl:: queue & queue , oneapi::mkl:: transpose trans , int row_col , 816const T * alpha , const std:: shared_ptr < matrix_info > info , 817const T * val , const int * row_ptr , const int * col_ind , 818std:: shared_ptr < optimize_info > optimize_info , const T * x , T * y ) { 819detail:: csrsv_impl < T > ()( queue , trans , row_col , alpha , info , val , row_ptr , 820col_ind , optimize_info , x , y ); 821} 822 823inline void csrsv (sycl:: queue & queue , oneapi::mkl:: transpose trans , int row_col , 824const void * alpha , library_data_t alpha_type , 825const std:: shared_ptr < matrix_info > info , const void * val , 826library_data_t val_type , const int * row_ptr , 827const int * col_ind , 828std:: shared_ptr < optimize_info > optimize_info , const void * x , 829library_data_t x_type , void * y , library_data_t y_type ) { 830detail:: spblas_shim < detail:: csrsv_impl > ( val_type , queue , trans , row_col , 831alpha , info , val , row_ptr , col_ind , 832optimize_info , x , y ); 833} 834 835/// Performs internal optimizations for dpct::sparse::csrsm by analyzing 836/// the provided matrix structure and operation parameters. The matrix A must be 837/// a triangular sparse matrix with the CSR format. 838/// \param [in] queue The queue where the routine should be executed. It must 839/// have the in_order property when using the USM mode. 840/// \param [in] transa The operation applied to A. 841/// \param [in] transb The operation applied to B and X. 842/// \param [in] row_col Number of rows and columns of A. 843/// \param [in] nrhs Number of columns op_b(B). 844/// \param [in] info Matrix info of A. 845/// \param [in] val An array containing the non-zero elements of A. 846/// \param [in] row_ptr An array of length \p num_rows + 1. 847/// \param [in] col_ind An array containing the column indices in index-based 848/// numbering. 849/// \param [out] optimize_info The result of the optimizations. 850template < typename T > 851void optimize_csrsm (sycl:: queue & queue , oneapi::mkl:: transpose transa , 852oneapi::mkl:: transpose transb , int row_col , int nrhs , 853const std:: shared_ptr < matrix_info > info , const T * val , 854const int * row_ptr , const int * col_ind , 855std:: shared_ptr < optimize_info > optimize_info ) { 856detail:: optimize_csrsm_impl < T > ()( queue , transa , transb , row_col , nrhs , info , 857val , row_ptr , col_ind , optimize_info ); 858} 859 860/// Solves the sparse triangular system op_a(A) * op_b(X) = alpha * op_b(B). 861/// A is a sparse triangular matrix with the CSR format of size \p row_col 862/// by \p row_col . 863/// B is a dense matrix of size \p row_col by \p nrhs ( \p transb is nontrans) 864/// or \p nrhs by \p row_col ( \p transb isn't nontrans). 865/// X is the solution dense matrix. 866/// \param [in] queue The queue where the routine should be executed. It must 867/// have the in_order property when using the USM mode. 868/// \param [in] transa The operation applied to A. 869/// \param [in] transb The operation applied to B and X. 870/// \param [in] row_col Number of rows and columns of A. 871/// \param [in] nrhs Number of columns op_b(B). 872/// \param [in] alpha Specifies the scalar. 873/// \param [in] info Matrix info of A. 874/// \param [in] val An array containing the non-zero elements of A. 875/// \param [in] row_ptr An array of length \p num_rows + 1. 876/// \param [in] col_ind An array containing the column indices in index-based 877/// numbering. 878/// \param [in, out] b The RHS matrix. It will be overwritten by the X. 879/// \param [in] ldb The leading dimension of B and X. 880/// \param [in] optimize_info The result of the optimizations. 881template < typename T > 882void csrsm (sycl:: queue & queue , oneapi::mkl:: transpose transa , 883oneapi::mkl:: transpose transb , int row_col , int nrhs , const T * alpha , 884const std:: shared_ptr < matrix_info > info , const T * val , 885const int * row_ptr , const int * col_ind , T * b , int ldb , 886std:: shared_ptr < optimize_info > optimize_info ) { 887detail:: csrsm_impl < T > ()( queue , transa , transb , row_col , nrhs , alpha , info , 888val , row_ptr , col_ind , b , ldb , optimize_info ); 889} 890 891/// Computes a sparse matrix-dense vector product: y = alpha * op(a) * x + beta * y. 892/// \param [in] queue The queue where the routine should be executed. It must 893/// have the in_order property when using the USM mode. 894/// \param [in] trans Specifies operation on input matrix. 895/// \param [in] alpha Specifies the scalar alpha. 896/// \param [in] a Specifies the sparse matrix a. 897/// \param [in] x Specifies the dense vector x. 898/// \param [in] beta Specifies the scalar beta. 899/// \param [in, out] y Specifies the dense vector y. 900/// \param [in] data_type Specifies the data type of \param a, \param x and \param y . 901inline void spmv (sycl:: queue queue , oneapi::mkl:: transpose trans , 902const void * alpha , sparse_matrix_desc_t a , 903std:: shared_ptr < dense_vector_desc > x , const void * beta , 904std:: shared_ptr < dense_vector_desc > y , 905library_data_t data_type ) { 906detail:: spblas_shim < detail:: spmv_impl > ( data_type , queue , trans , alpha , a , x , 907beta , y ); 908} 909 910/// Computes a sparse matrix-dense matrix product: c = alpha * op(a) * op(b) + beta * c. 911/// \param [in] queue The queue where the routine should be executed. It must 912/// have the in_order property when using the USM mode. 913/// \param [in] trans_a Specifies operation on input matrix a. 914/// \param [in] trans_b Specifies operation on input matrix b. 915/// \param [in] alpha Specifies the scalar alpha. 916/// \param [in] a Specifies the sparse matrix a. 917/// \param [in] b Specifies the dense matrix b. 918/// \param [in] beta Specifies the scalar beta. 919/// \param [in, out] c Specifies the dense matrix c. 920/// \param [in] data_type Specifies the data type of \param a, \param b and \param c . 921inline void spmm (sycl:: queue queue , oneapi::mkl:: transpose trans_a , 922oneapi::mkl:: transpose trans_b , const void * alpha , 923sparse_matrix_desc_t a , std:: shared_ptr < dense_matrix_desc > b , 924const void * beta , std:: shared_ptr < dense_matrix_desc > c , 925library_data_t data_type ) { 926if ( b -> get_layout () != c -> get_layout ()) 927throw std:: runtime_error ( "the layout of b and c are different" ); 928detail:: spblas_shim < detail:: spmm_impl > ( data_type , queue , trans_a , trans_b , 929alpha , a , b , beta , c ); 930} 931 932/// Do initial estimation of work and load balancing of computing a sparse 933/// matrix-sparse matrix product. 934/// \param [in] queue The queue where the routine should be executed. It must 935/// have the in_order property when using the USM mode. 936/// \param [in] trans_a Specifies operation on input matrix a. 937/// \param [in] trans_b Specifies operation on input matrix b. 938/// \param [in] alpha Specifies the scalar alpha. 939/// \param [in] a Specifies the sparse matrix a. 940/// \param [in] b Specifies the sparse matrix b. 941/// \param [in] beta Specifies the scalar beta. 942/// \param [in, out] c Specifies the sparse matrix c. 943/// \param [in] matmat_descr Describes the sparse matrix-sparse matrix operation 944/// to be executed. 945/// \param [in, out] size_temp_buffer Specifies the size of workspace. 946/// \param [in] temp_buffer Specifies the memory of the workspace. 947inline void 948spgemm_work_estimation (sycl:: queue queue , oneapi::mkl:: transpose trans_a , 949oneapi::mkl:: transpose trans_b , const void * alpha , 950sparse_matrix_desc_t a , sparse_matrix_desc_t b , 951const void * beta , sparse_matrix_desc_t c , 952oneapi::mkl::sparse:: matmat_descr_t matmat_descr , 953size_t * size_temp_buffer , void * temp_buffer ) { 954if ( temp_buffer ) { 955detail:: temp_memory < std:: int64_t , true, size_t > size_memory ( 956queue , size_temp_buffer ); 957detail:: temp_memory < std:: uint8_t , false > work_memory ( queue , temp_buffer ); 958oneapi::mkl::sparse:: matmat ( 959queue , a -> get_matrix_handle (), b -> get_matrix_handle (), 960c -> get_matrix_handle (), 961oneapi::mkl::sparse::matmat_request:: work_estimation , matmat_descr , 962size_memory . get_memory_ptr (), work_memory . get_memory_ptr () 963#ifndef DPCT_USM_LEVEL_NONE 964, {} 965#endif 966); 967} else { 968oneapi::mkl::sparse:: set_matmat_data ( 969matmat_descr , oneapi::mkl::sparse::matrix_view_descr:: general , trans_a , 970oneapi::mkl::sparse::matrix_view_descr:: general , trans_b , 971oneapi::mkl::sparse::matrix_view_descr:: general ); 972detail:: temp_memory < std:: int64_t , true, size_t > size_memory ( 973queue , size_temp_buffer ); 974oneapi::mkl::sparse:: matmat ( 975queue , a -> get_matrix_handle (), b -> get_matrix_handle (), 976c -> get_matrix_handle (), 977oneapi::mkl::sparse::matmat_request:: get_work_estimation_buf_size , 978matmat_descr , size_memory . get_memory_ptr (), nullptr 979#ifndef DPCT_USM_LEVEL_NONE 980, {} 981#endif 982); 983} 984} 985 986/// Do internal products for computing the C matrix of computing a sparse 987/// matrix-sparse matrix product. 988/// \param [in] queue The queue where the routine should be executed. It must 989/// have the in_order property when using the USM mode. 990/// \param [in] trans_a Specifies operation on input matrix a. 991/// \param [in] trans_b Specifies operation on input matrix b. 992/// \param [in] alpha Specifies the scalar alpha. 993/// \param [in] a Specifies the sparse matrix a. 994/// \param [in] b Specifies the sparse matrix b. 995/// \param [in] beta Specifies the scalar beta. 996/// \param [in, out] c Specifies the sparse matrix c. 997/// \param [in] matmat_descr Describes the sparse matrix-sparse matrix operation 998/// to be executed. 999/// \param [in, out] size_temp_buffer Specifies the size of workspace. 1000/// \param [in] temp_buffer Specifies the memory of the workspace. 1001inline void spgemm_compute (sycl:: queue queue , oneapi::mkl:: transpose trans_a , 1002oneapi::mkl:: transpose trans_b , const void * alpha , 1003sparse_matrix_desc_t a , sparse_matrix_desc_t b , 1004const void * beta , sparse_matrix_desc_t c , 1005oneapi::mkl::sparse:: matmat_descr_t matmat_descr , 1006size_t * size_temp_buffer , void * temp_buffer ) { 1007if ( temp_buffer ) { 1008std:: int64_t nnz_value = 0 ; 1009{ 1010detail:: temp_memory < std:: int64_t , true, size_t > size_memory ( 1011queue , size_temp_buffer ); 1012detail:: temp_memory < std:: uint8_t , false > work_memory ( queue , temp_buffer ); 1013detail:: temp_memory < std:: int64_t , true, std:: int64_t > nnz_memory ( 1014queue , & nnz_value ); 1015oneapi::mkl::sparse:: matmat ( 1016queue , a -> get_matrix_handle (), b -> get_matrix_handle (), 1017c -> get_matrix_handle (), oneapi::mkl::sparse::matmat_request:: compute , 1018matmat_descr , size_memory . get_memory_ptr (), 1019work_memory . get_memory_ptr () 1020#ifndef DPCT_USM_LEVEL_NONE 1021, {} 1022#endif 1023); 1024oneapi::mkl::sparse:: matmat ( 1025queue , a -> get_matrix_handle (), b -> get_matrix_handle (), 1026c -> get_matrix_handle (), oneapi::mkl::sparse::matmat_request:: get_nnz , 1027matmat_descr , nnz_memory . get_memory_ptr (), nullptr 1028#ifndef DPCT_USM_LEVEL_NONE 1029, {} 1030#endif 1031); 1032} 1033c -> set_nnz ( nnz_value ); 1034} else { 1035detail:: temp_memory < std:: int64_t , true, size_t > size_memory ( 1036queue , size_temp_buffer ); 1037oneapi::mkl::sparse:: matmat ( 1038queue , a -> get_matrix_handle (), b -> get_matrix_handle (), 1039c -> get_matrix_handle (), 1040oneapi::mkl::sparse::matmat_request:: get_compute_buf_size , matmat_descr , 1041size_memory . get_memory_ptr (), nullptr 1042#ifndef DPCT_USM_LEVEL_NONE 1043, {} 1044#endif 1045); 1046} 1047} 1048 1049/// Do any remaining internal products and accumulation and transfer into final 1050/// C matrix arrays of computing a sparse matrix-sparse matrix product. 1051/// \param [in] queue The queue where the routine should be executed. It must 1052/// have the in_order property when using the USM mode. 1053/// \param [in] trans_a Specifies operation on input matrix a. 1054/// \param [in] trans_b Specifies operation on input matrix b. 1055/// \param [in] alpha Specifies the scalar alpha. 1056/// \param [in] a Specifies the sparse matrix a. 1057/// \param [in] b Specifies the sparse matrix b. 1058/// \param [in] beta Specifies the scalar beta. 1059/// \param [in, out] c Specifies the sparse matrix c. 1060/// \param [in] matmat_descr Describes the sparse matrix-sparse matrix operation 1061/// to be executed. 1062inline void spgemm_finalize (sycl:: queue queue , oneapi::mkl:: transpose trans_a , 1063oneapi::mkl:: transpose trans_b , const void * alpha , 1064sparse_matrix_desc_t a , sparse_matrix_desc_t b , 1065const void * beta , sparse_matrix_desc_t c , 1066oneapi::mkl::sparse:: matmat_descr_t matmat_descr ) { 1067oneapi::mkl::sparse:: matmat ( queue , a -> get_matrix_handle (), 1068b -> get_matrix_handle (), c -> get_matrix_handle (), 1069oneapi::mkl::sparse::matmat_request:: finalize , 1070matmat_descr , nullptr , nullptr 1071#ifdef DPCT_USM_LEVEL_NONE 1072); 1073#else 1074, {}). wait (); 1075#endif 1076if ( c -> get_shadow_row_ptr ()) { 1077switch ( c -> get_col_ind_type ()) { 1078case library_data_t:: real_int32 : { 1079::dpct::cs:: memcpy (::dpct::cs:: get_default_queue (), c -> get_row_ptr (), 1080c -> get_shadow_row_ptr (), 1081sizeof(std:: int32_t ) * ( c -> get_row_num () + 1 )) 1082. wait (); 1083break; 1084} 1085case library_data_t:: real_int64 : { 1086::dpct::cs:: memcpy (::dpct::cs:: get_default_queue (), c -> get_row_ptr (), 1087c -> get_shadow_row_ptr (), 1088sizeof(std:: int64_t ) * ( c -> get_row_num () + 1 )) 1089. wait (); 1090break; 1091} 1092default: 1093throw std:: runtime_error ( "dpct::sparse::spgemm_finalize(): The data type " 1094"of the col_ind in matrix c is unsupported." ); 1095} 1096} 1097} 1098 1099/// Performs internal optimizations for spsv by analyzing the provided matrix 1100/// structure and operation parameters. 1101/// \param [in] queue The queue where the routine should be executed. It must 1102/// have the in_order property when using the USM mode. 1103/// \param [in] trans_a Specifies operation on input matrix a. 1104/// \param [in] a Specifies the sparse matrix a. 1105inline void spsv_optimize (sycl:: queue queue , oneapi::mkl:: transpose trans_a , 1106sparse_matrix_desc_t a ) { 1107if (! a -> get_uplo () || ! a -> get_diag ()) { 1108throw std:: runtime_error ( 1109"dpct::sparse::spsv_optimize(): oneapi::mkl::sparse::optimize_trsv " 1110"needs uplo and diag attributes to be specified." ); 1111} 1112oneapi::mkl::sparse:: optimize_trsv ( 1113queue , a -> get_uplo (). value (), oneapi::mkl::transpose:: nontrans , 1114a -> get_diag (). value (), a -> get_matrix_handle ()); 1115} 1116 1117/// Solves a system of linear equations for a sparse matrix. 1118/// \param [in] queue The queue where the routine should be executed. It must 1119/// have the in_order property when using the USM mode. 1120/// \param [in] trans_a Specifies operation on input matrix a. 1121/// \param [in] alpha Specifies the scalar alpha. 1122/// \param [in] a Specifies the sparse matrix a. 1123/// \param [in] x Specifies the dense vector x. 1124/// \param [in, out] y Specifies the dense vector y. 1125/// \param [in] data_type Specifies the data type of \param a, \param x and 1126/// \param y . 1127inline void spsv (sycl:: queue queue , oneapi::mkl:: transpose trans_a , 1128const void * alpha , sparse_matrix_desc_t a , 1129std:: shared_ptr < dense_vector_desc > x , 1130std:: shared_ptr < dense_vector_desc > y , 1131library_data_t data_type ) { 1132if (! a -> get_uplo () || ! a -> get_diag ()) { 1133throw std:: runtime_error ( 1134"dpct::sparse::spsv(): oneapi::mkl::sparse::trsv needs uplo and diag " 1135"attributes to be specified." ); 1136} 1137oneapi::mkl:: uplo uplo = a -> get_uplo (). value (); 1138oneapi::mkl:: diag diag = a -> get_diag (). value (); 1139detail:: spblas_shim < detail:: spsv_impl > ( a -> get_value_type (), queue , uplo , diag , 1140trans_a , alpha , a , x , y ); 1141} 1142 1143/// Performs internal optimizations for spsm by analyzing the provided matrix 1144/// structure and operation parameters. 1145/// \param [in] queue The queue where the routine should be executed. It must 1146/// have the in_order property when using the USM mode. 1147/// \param [in] trans_a Specifies operation on input matrix a. 1148/// \param [in] a Specifies the sparse matrix a. 1149/// \param [in] a Specifies the dense matrix b. 1150/// \param [in] a Specifies the dense matrix c. 1151inline void spsm_optimize (sycl:: queue queue , oneapi::mkl:: transpose trans_a , 1152sparse_matrix_desc_t a , 1153std:: shared_ptr < dense_matrix_desc > b , 1154std:: shared_ptr < dense_matrix_desc > c ) { 1155if (! a -> get_uplo () || ! a -> get_diag ()) { 1156throw std:: runtime_error ( 1157"dpct::sparse::spsm_optimize(): oneapi::mkl::sparse::optimize_trsm " 1158"needs uplo and diag attributes to be specified." ); 1159} 1160oneapi::mkl::sparse:: optimize_trsm ( 1161queue , b -> get_layout (), a -> get_uplo (). value (), trans_a , 1162a -> get_diag (). value (), a -> get_matrix_handle (), c -> get_col_num ()); 1163} 1164 1165/// Solves a system of linear equations with multiple right-hand sides (RHS) 1166/// for a triangular sparse matrix. 1167/// \param [in] queue The queue where the routine should be executed. It must 1168/// have the in_order property when using the USM mode. 1169/// \param [in] trans_a Specifies operation on input matrix a. 1170/// \param [in] trans_b Specifies operation on input matrix b. 1171/// \param [in] alpha Specifies the scalar alpha. 1172/// \param [in] a Specifies the sparse matrix a. 1173/// \param [in] b Specifies the dense matrix b. 1174/// \param [in, out] c Specifies the dense matrix c. 1175/// \param [in] data_type Specifies the data type of \param a, \param b and 1176/// \param c . 1177inline void spsm (sycl:: queue queue , oneapi::mkl:: transpose trans_a , 1178oneapi::mkl:: transpose trans_b , const void * alpha , 1179sparse_matrix_desc_t a , std:: shared_ptr < dense_matrix_desc > b , 1180std:: shared_ptr < dense_matrix_desc > c , 1181library_data_t data_type ) { 1182if (! a -> get_uplo () || ! a -> get_diag ()) { 1183throw std:: runtime_error ( 1184"dpct::sparse::spsm(): oneapi::mkl::sparse::trsm needs uplo and diag " 1185"attributes to be specified." ); 1186} 1187oneapi::mkl:: uplo uplo = a -> get_uplo (). value (); 1188oneapi::mkl:: diag diag = a -> get_diag (). value (); 1189detail:: spblas_shim < detail:: spsm_impl > ( data_type , queue , trans_a , trans_b , 1190uplo , diag , alpha , a , b , c ); 1191} 1192 1193/// Convert a CSR sparse matrix to a CSC sparse matrix. 1194/// \param [in] queue The queue where the routine should be executed. It must 1195/// have the in_order property when using the USM mode. 1196/// \param [in] m Number of rows of the matrix. 1197/// \param [in] n Number of columns of the matrix. 1198/// \param [in] nnz Number of non-zero elements. 1199/// \param [in] from_val An array containing the non-zero elements of the input 1200/// matrix. 1201/// \param [in] from_row_ptr An array of length \p m + 1. 1202/// \param [in] from_col_ind An array containing the column indices in 1203/// index-based numbering. 1204/// \param [out] to_val An array containing the non-zero elements of the output 1205/// matrix. 1206/// \param [out] to_col_ptr An array of length \p n + 1. 1207/// \param [out] to_row_ind An array containing the row indices in index-based 1208/// numbering. 1209/// \param [in] range Specifies the conversion scope. 1210/// \param [in] base Specifies the index base. 1211template < typename T > 1212inline void csr2csc (sycl:: queue queue , int m , int n , int nnz , const T * from_val , 1213const int * from_row_ptr , const int * from_col_ind , T * to_val , 1214int * to_col_ptr , int * to_row_ind , conversion_scope range , 1215oneapi::mkl:: index_base base ) { 1216detail:: csr2csc_impl < T > ()( queue , m , n , nnz , from_val , from_row_ptr , 1217from_col_ind , to_val , to_col_ptr , to_row_ind , range , 1218base ); 1219} 1220 1221/// Convert a CSR sparse matrix to a CSC sparse matrix. 1222/// \param [in] queue The queue where the routine should be executed. It must 1223/// have the in_order property when using the USM mode. 1224/// \param [in] m Number of rows of the matrix. 1225/// \param [in] n Number of columns of the matrix. 1226/// \param [in] nnz Number of non-zero elements. 1227/// \param [in] from_val An array containing the non-zero elements of the input 1228/// matrix. 1229/// \param [in] from_row_ptr An array of length \p m + 1. 1230/// \param [in] from_col_ind An array containing the column indices in 1231/// index-based numbering. 1232/// \param [out] to_val An array containing the non-zero elements of the output 1233/// matrix. 1234/// \param [out] to_col_ptr An array of length \p n + 1. 1235/// \param [out] to_row_ind An array containing the row indices in index-based 1236/// numbering. 1237/// \param [in] value_type Data type of \p from_val and \p to_val . 1238/// \param [in] range Specifies the conversion scope. 1239/// \param [in] base Specifies the index base. 1240inline void csr2csc (sycl:: queue queue , int m , int n , int nnz , 1241const void * from_val , const int * from_row_ptr , 1242const int * from_col_ind , void * to_val , int * to_col_ptr , 1243int * to_row_ind , library_data_t value_type , 1244conversion_scope range , oneapi::mkl:: index_base base ) { 1245detail:: spblas_shim < detail:: csr2csc_impl > ( 1246value_type , queue , m , n , nnz , from_val , from_row_ptr , from_col_ind , 1247to_val , to_col_ptr , to_row_ind , range , base ); 1248} 1249 1250/// Calculate the non-zero elements number of the result of a 1251/// sparse matrix (CSR format)-sparse matrix (CSR format) product: 1252/// C = op(A) * op(B) 1253/// \param [in] desc The descriptor of this calculation. 1254/// \param [in] trans_a The operation applied to the matrix A. 1255/// \param [in] trans_b The operation applied to the matrix B. 1256/// \param [in] m The rows number of op(A) and C. 1257/// \param [in] n The columns number of op(B) and C. 1258/// \param [in] k The columns number of op(A) and rows number of op(B). 1259/// \param [in] info_a Matrix info of the matrix A. 1260/// \param [in] nnz_a Non-zero elements number of matrix A. 1261/// \param [in] val_a An array containing the non-zero elements of the matrix A. 1262/// \param [in] row_ptr_a An array of length row number + 1. 1263/// \param [in] col_ind_a An array containing the column indices in index-based 1264/// numbering. 1265/// \param [in] info_b Matrix info of the matrix B. 1266/// \param [in] nnz_b Non-zero elements number of matrix B. 1267/// \param [in] val_b An array containing the non-zero elements of the matrix B. 1268/// \param [in] row_ptr_b An array of length row number + 1. 1269/// \param [in] col_ind_b An array containing the column indices in index-based 1270/// numbering. 1271/// \param [in] info_c Matrix info of the matrix C. 1272/// \param [in] row_ptr_c An array of length row number + 1. 1273/// \param [out] nnz_host_ptr Non-zero elements number of matrix C. 1274template < typename T > 1275void csrgemm_nnz ( descriptor_ptr desc , oneapi::mkl:: transpose trans_a , 1276oneapi::mkl:: transpose trans_b , int m , int n , int k , 1277const std:: shared_ptr < matrix_info > info_a , int nnz_a , 1278const T * val_a , const int * row_ptr_a , const int * col_ind_a , 1279const std:: shared_ptr < matrix_info > info_b , int nnz_b , 1280const T * val_b , const int * row_ptr_b , const int * col_ind_b , 1281const std:: shared_ptr < matrix_info > info_c , int * row_ptr_c , 1282int * nnz_host_ptr ) { 1283using Ty = typename ::dpct::detail:: lib_data_traits_t < T > ; 1284sycl:: queue & queue = desc -> get_queue (); 1285detail:: csrgemm_args_info args ( trans_a , trans_b , m , n , k , info_a , val_a , 1286row_ptr_a , col_ind_a , info_b , val_b , row_ptr_b , 1287col_ind_b , info_c , row_ptr_c ); 1288auto & info = desc -> get_csrgemm_info_map ()[ args ]; 1289 1290if ( info . is_empty ()) { 1291info . init ( & queue ); 1292 1293int rows_c = m ; 1294int cols_c = n ; 1295int rows_a = ( trans_a == oneapi::mkl::transpose:: nontrans ) ? m : k ; 1296int cols_a = ( trans_a == oneapi::mkl::transpose:: nontrans ) ? k : m ; 1297int rows_b = ( trans_b == oneapi::mkl::transpose:: nontrans ) ? k : n ; 1298int cols_b = ( trans_b == oneapi::mkl::transpose:: nontrans ) ? n : k ; 1299 1300info . matrix_handle_a . set_matrix_data < Ty > ( 1301rows_a , cols_a , info_a -> get_index_base (), row_ptr_a , col_ind_a , val_a ); 1302info . matrix_handle_b . set_matrix_data < Ty > ( 1303rows_b , cols_b , info_b -> get_index_base (), row_ptr_b , col_ind_b , val_b ); 1304// In the future, oneMKL will allow nullptr to be passed in for row_ptr_c in 1305// the initial calls before matmat. But currently, it needs an array of 1306// length row_number + 1. 1307info . matrix_handle_c . set_matrix_data < Ty > ( 1308rows_c , cols_c , info_c -> get_index_base (), row_ptr_c , nullptr , nullptr ); 1309 1310oneapi::mkl::sparse:: set_matmat_data ( 1311info . matmat_desc . get_handle (), 1312oneapi::mkl::sparse::matrix_view_descr:: general , trans_a , 1313oneapi::mkl::sparse::matrix_view_descr:: general , trans_b , 1314oneapi::mkl::sparse::matrix_view_descr:: general ); 1315} 1316 1317#ifdef DPCT_USM_LEVEL_NONE 1318#define __MATMAT ( STEP , NNZ_C ) \ 1319oneapi::mkl::sparse::matmat(queue, info.matrix_handle_a.get_handle(), \ 1320info.matrix_handle_b.get_handle(), \ 1321info.matrix_handle_c.get_handle(), STEP, \ 1322info.matmat_desc.get_handle(), NNZ_C, nullptr) 1323#else 1324#define __MATMAT ( STEP , NNZ_C ) \ 1325oneapi::mkl::sparse::matmat( \ 1326queue, info.matrix_handle_a.get_handle(), \ 1327info.matrix_handle_b.get_handle(), info.matrix_handle_c.get_handle(), \ 1328STEP, info.matmat_desc.get_handle(), NNZ_C, nullptr, {}) 1329#endif 1330 1331__MATMAT (oneapi::mkl::sparse::matmat_request:: work_estimation , nullptr ); 1332queue . wait (); 1333 1334__MATMAT (oneapi::mkl::sparse::matmat_request:: compute , nullptr ); 1335 1336int nnz_c_int = 0 ; 1337#ifdef DPCT_USM_LEVEL_NONE 1338sycl:: buffer < std:: int64_t , 1 > nnz_buf_c ( 1 ); 1339__MATMAT (oneapi::mkl::sparse::matmat_request:: get_nnz , & nnz_buf_c ); 1340nnz_c_int = nnz_buf_c . get_host_access (sycl:: read_only )[ 0 ]; 1341#else 1342std:: int64_t * nnz_c = sycl:: malloc_host < std:: int64_t > ( 1 , queue ); 1343__MATMAT (oneapi::mkl::sparse::matmat_request:: get_nnz , nnz_c ); 1344queue . wait (); 1345nnz_c_int = * nnz_c ; 1346#endif 1347 1348if ( nnz_host_ptr ) { 1349* nnz_host_ptr = nnz_c_int ; 1350} 1351} 1352 1353/// Computes a sparse matrix (CSR format)-sparse matrix (CSR format) product: 1354/// C = op(A) * op(B) 1355/// \param [in] desc The descriptor of this calculation. 1356/// \param [in] trans_a The operation applied to the matrix A. 1357/// \param [in] trans_b The operation applied to the matrix B. 1358/// \param [in] m The rows number of op(A) and C. 1359/// \param [in] n The columns number of op(B) and C. 1360/// \param [in] k The columns number of op(A) and rows number of op(B). 1361/// \param [in] info_a Matrix info of the matrix A. 1362/// \param [in] val_a An array containing the non-zero elements of the matrix A. 1363/// \param [in] row_ptr_a An array of length row number + 1. 1364/// \param [in] col_ind_a An array containing the column indices in index-based 1365/// numbering. 1366/// \param [in] info_b Matrix info of the matrix B. 1367/// \param [in] val_b An array containing the non-zero elements of the matrix B. 1368/// \param [in] row_ptr_b An array of length row number + 1. 1369/// \param [in] col_ind_b An array containing the column indices in index-based 1370/// numbering. 1371/// \param [in] info_c Matrix info of the matrix C. 1372/// \param [out] val_c An array containing the non-zero elements of the matrix 1373/// C. 1374/// \param [in] row_ptr_c An array of length row number + 1. 1375/// \param [out] col_ind_c An array containing the column indices in index-based 1376/// numbering. 1377template < typename T > 1378void csrgemm ( descriptor_ptr desc , oneapi::mkl:: transpose trans_a , 1379oneapi::mkl:: transpose trans_b , int m , int n , int k , 1380const std:: shared_ptr < matrix_info > info_a , const T * val_a , 1381const int * row_ptr_a , const int * col_ind_a , 1382const std:: shared_ptr < matrix_info > info_b , const T * val_b , 1383const int * row_ptr_b , const int * col_ind_b , 1384const std:: shared_ptr < matrix_info > info_c , T * val_c , 1385const int * row_ptr_c , int * col_ind_c ) { 1386using Ty = typename ::dpct::detail:: lib_data_traits_t < T > ; 1387sycl:: queue & queue = desc -> get_queue (); 1388detail:: csrgemm_args_info args ( trans_a , trans_b , m , n , k , info_a , val_a , 1389row_ptr_a , col_ind_a , info_b , val_b , row_ptr_b , 1390col_ind_b , info_c , row_ptr_c ); 1391auto & info = desc -> get_csrgemm_info_map ()[ args ]; 1392if ( info . is_empty ()) { 1393throw std:: runtime_error ( "csrgemm_nnz is not invoked previously." ); 1394} 1395 1396int rows_c = m ; 1397int cols_c = n ; 1398info . matrix_handle_c . set_matrix_data < Ty > ( 1399rows_c , cols_c , info_c -> get_index_base (), row_ptr_c , col_ind_c , val_c ); 1400sycl:: event e ; 1401#ifndef DPCT_USM_LEVEL_NONE 1402e = 1403#endif 1404__MATMAT (oneapi::mkl::sparse::matmat_request:: finalize , nullptr ); 1405#undef __MATMAT 1406 1407info . matrix_handle_a . add_dependency ( e ); 1408info . matrix_handle_b . add_dependency ( e ); 1409info . matrix_handle_c . add_dependency ( e ); 1410info . matmat_desc . add_dependency ( e ); 1411desc -> get_csrgemm_info_map (). erase ( args ); 1412} 1413 1414/// Contains internal information for csrgemm2 by analyzing the provided 1415/// matrix structure and operation parameters. 1416class csrgemm2_info { 1417detail:: matrix_handle_manager matrix_handle_a ; 1418detail:: matrix_handle_manager matrix_handle_b ; 1419detail:: matrix_handle_manager matrix_handle_axb ; 1420detail:: matrix_handle_manager matrix_handle_d ; 1421detail:: matrix_handle_manager matrix_handle_c ; 1422detail:: handle_manager < oneapi::mkl::sparse:: matmat_descr_t > matmat_desc ; 1423detail:: handle_manager < oneapi::mkl::sparse:: omatadd_descr_t > omatadd_desc ; 1424void * row_ptr_axb = nullptr ; 1425void * col_ind_axb = nullptr ; 1426void * val_axb = nullptr ; 1427#ifdef DPCT_USM_LEVEL_NONE 1428sycl:: buffer < std:: int64_t , 1 > * temp_buffer_1_size ; 1429sycl:: buffer < std:: int64_t , 1 > * temp_buffer_2_size ; 1430sycl:: buffer < std:: uint8_t , 1 > * temp_buffer_1 ; 1431sycl:: buffer < std:: uint8_t , 1 > * temp_buffer_2 ; 1432#else 1433std:: int64_t * temp_buffer_1_size ; 1434std:: int64_t * temp_buffer_2_size ; 1435std:: uint8_t * temp_buffer_1 ; 1436std:: uint8_t * temp_buffer_2 ; 1437#endif 1438enum matrix_c_datatype_t { 1439mcd_float , 1440mcd_double , 1441mcd_float2 , 1442mcd_double2 , 1443}; 1444template < typename T > void set_matrix_c_datatype () { 1445if constexpr (std:: is_same_v < T , float > ) { 1446matrix_c_datatype = matrix_c_datatype_t:: mcd_float ; 1447} else if constexpr (std:: is_same_v < T , double > ) { 1448matrix_c_datatype = matrix_c_datatype_t:: mcd_double ; 1449} else if constexpr (std:: is_same_v < T , std:: complex < float >> ) { 1450matrix_c_datatype = matrix_c_datatype_t:: mcd_float2 ; 1451} else { 1452matrix_c_datatype = matrix_c_datatype_t:: mcd_double2 ; 1453} 1454} 1455matrix_c_datatype_t matrix_c_datatype ; 1456void init (sycl:: queue * q_ptr ) { 1457matrix_handle_a . init ( q_ptr ); 1458matrix_handle_b . init ( q_ptr ); 1459matrix_handle_axb . init ( q_ptr ); 1460matrix_handle_d . init ( q_ptr ); 1461matrix_handle_c . init ( q_ptr ); 1462matmat_desc . init ( q_ptr ); 1463omatadd_desc . init ( q_ptr ); 1464} 1465sycl:: event release (sycl:: queue q , sycl:: event dep ) { 1466matrix_handle_a . add_dependency ( dep ); 1467matrix_handle_b . add_dependency ( dep ); 1468matrix_handle_axb . add_dependency ( dep ); 1469matrix_handle_d . add_dependency ( dep ); 1470matrix_handle_c . add_dependency ( dep ); 1471matmat_desc . add_dependency ( dep ); 1472omatadd_desc . add_dependency ( dep ); 1473std:: vector < sycl:: event > events ; 1474events . push_back ( matrix_handle_a . release ()); 1475events . push_back ( matrix_handle_b . release ()); 1476events . push_back ( matrix_handle_axb . release ()); 1477events . push_back ( matrix_handle_d . release ()); 1478events . push_back ( matrix_handle_c . release ()); 1479events . push_back ( matmat_desc . release ()); 1480events . push_back ( omatadd_desc . release ()); 1481return q . single_task ( events , [] {}); 1482} 1483template < typename T > 1484friend void csrgemm2_get_buffer_size ( 1485descriptor_ptr desc , int m , int n , int k , const T * alpha , 1486const std:: shared_ptr < matrix_info > info_a , int nnz_a , 1487const int * row_ptr_a , const int * col_ind_a , 1488const std:: shared_ptr < matrix_info > info_b , int nnz_b , 1489const int * row_ptr_b , const int * col_ind_b , const T * beta , 1490const std:: shared_ptr < matrix_info > info_d , int nnz_d , 1491const int * row_ptr_d , const int * col_ind_d , 1492std:: shared_ptr < csrgemm2_info > info , size_t * buffer_size_in_bytes ); 1493friend void csrgemm2_nnz ( descriptor_ptr desc , int m , int n , int k , 1494const std:: shared_ptr < matrix_info > info_a , int nnz_a , 1495const int * row_ptr_a , const int * col_ind_a , 1496const std:: shared_ptr < matrix_info > info_b , int nnz_b , 1497const int * row_ptr_b , const int * col_ind_b , 1498const std:: shared_ptr < matrix_info > info_d , int nnz_d , 1499const int * row_ptr_d , const int * col_ind_d , 1500const std:: shared_ptr < matrix_info > info_c , 1501int * row_ptr_c , int * nnz_ptr , 1502std:: shared_ptr < csrgemm2_info > info , void * buffer ); 1503template < typename T > 1504friend void 1505csrgemm2 ( descriptor_ptr desc , int m , int n , int k , const T * alpha , 1506const std:: shared_ptr < matrix_info > info_a , int nnz_a , const T * val_a , 1507const int * row_ptr_a , const int * col_ind_a , 1508const std:: shared_ptr < matrix_info > info_b , int nnz_b , const T * val_b , 1509const int * row_ptr_b , const int * col_ind_b , const T * beta , 1510const std:: shared_ptr < matrix_info > info_d , int nnz_d , const T * val_d , 1511const int * row_ptr_d , const int * col_ind_d , 1512const std:: shared_ptr < matrix_info > info_c , T * val_c , 1513const int * row_ptr_c , int * col_ind_c , 1514std:: shared_ptr < csrgemm2_info > info , void * buffer ); 1515}; 1516 1517/// Calculate the required workspace size of the following operation: 1518/// C = alpha * A * B + beta * D 1519/// \param [in] desc The descriptor of this calculation. 1520/// \param [in] m The rows number of A, D and C. 1521/// \param [in] n The columns number of B, D and C. 1522/// \param [in] k The columns number of A and rows number of B. 1523/// \param [in] alpha Scaling factor. 1524/// \param [in] info_a Matrix info of the matrix A. 1525/// \param [in] nnz_a Non-zero elements number of matrix A. 1526/// \param [in] row_ptr_a An array of length \p m + 1. 1527/// \param [in] col_ind_a An array containing the column indices in index-based 1528/// numbering. 1529/// \param [in] info_b Matrix info of the matrix B. 1530/// \param [in] nnz_b Non-zero elements number of matrix B. 1531/// \param [in] row_ptr_b An array of length \p k + 1. 1532/// \param [in] col_ind_b An array containing the column indices in index-based 1533/// numbering. 1534/// \param [in] beta Scaling factor. 1535/// \param [in] info_d Matrix info of the matrix D. 1536/// \param [in] nnz_d Non-zero elements number of matrix D. 1537/// \param [in] row_ptr_d An array of length \p m + 1. 1538/// \param [in] col_ind_d An array containing the column indices in index-based 1539/// numbering. 1540/// \param [in, out] info The information of csrgemm2 operation. 1541/// \param [out] buffer_size_in_bytes Workspace memory size in bytes. 1542template < typename T > 1543void csrgemm2_get_buffer_size ( 1544descriptor_ptr desc , int m , int n , int k , const T * alpha , 1545const std:: shared_ptr < matrix_info > info_a , int nnz_a , const int * row_ptr_a , 1546const int * col_ind_a , const std:: shared_ptr < matrix_info > info_b , int nnz_b , 1547const int * row_ptr_b , const int * col_ind_b , const T * beta , 1548const std:: shared_ptr < matrix_info > info_d , int nnz_d , const int * row_ptr_d , 1549const int * col_ind_d , std:: shared_ptr < csrgemm2_info > info , 1550size_t * buffer_size_in_bytes ) { 1551using Ty = typename ::dpct::detail:: lib_data_traits_t < T > ; 1552sycl:: queue & queue = desc -> get_queue (); 1553info -> set_matrix_c_datatype < Ty > (); 1554 1555info -> row_ptr_axb = ( int * )::dpct::cs:: malloc (( m + 1 ) * sizeof( int ), queue ); 1556info -> init ( & queue ); 1557 1558info -> matrix_handle_a . set_matrix_data < Ty > ( m , k , info_a -> get_index_base (), 1559row_ptr_a , col_ind_a , nullptr ); 1560info -> matrix_handle_b . set_matrix_data < Ty > ( k , n , info_b -> get_index_base (), 1561row_ptr_b , col_ind_b , nullptr ); 1562info -> matrix_handle_axb . set_matrix_data < Ty > ( 1563m , n , oneapi::mkl::index_base:: zero , info -> row_ptr_axb , nullptr , nullptr ); 1564info -> matrix_handle_d . set_matrix_data < Ty > ( m , n , info_d -> get_index_base (), 1565row_ptr_d , col_ind_d , nullptr ); 1566info -> matrix_handle_c . set_matrix_data < Ty > ( m , n , oneapi::mkl::index_base:: zero , 1567nullptr , nullptr , nullptr ); 1568 1569oneapi::mkl::sparse:: set_matmat_data ( 1570info -> matmat_desc . get_handle (), 1571oneapi::mkl::sparse::matrix_view_descr:: general , 1572oneapi::mkl::transpose:: nontrans , 1573oneapi::mkl::sparse::matrix_view_descr:: general , 1574oneapi::mkl::transpose:: nontrans , 1575oneapi::mkl::sparse::matrix_view_descr:: general ); 1576 1577#ifdef DPCT_USM_LEVEL_NONE 1578#define __MATMAT ( STEP , PTR1 , PTR2 ) \ 1579oneapi::mkl::sparse::matmat(queue, info->matrix_handle_a.get_handle(), \ 1580info->matrix_handle_b.get_handle(), \ 1581info->matrix_handle_axb.get_handle(), \ 1582oneapi::mkl::sparse::matmat_request::STEP, \ 1583info->matmat_desc.get_handle(), PTR1, PTR2) 1584#else 1585#define __MATMAT ( STEP , PTR1 , PTR2 ) \ 1586oneapi::mkl::sparse::matmat(queue, info->matrix_handle_a.get_handle(), \ 1587info->matrix_handle_b.get_handle(), \ 1588info->matrix_handle_axb.get_handle(), \ 1589oneapi::mkl::sparse::matmat_request::STEP, \ 1590info->matmat_desc.get_handle(), PTR1, PTR2, {}) 1591#endif 1592 1593std:: int64_t nnz_axb = 0 ; 1594#ifdef DPCT_USM_LEVEL_NONE 1595info -> temp_buffer_1_size = new sycl:: buffer < std:: int64_t , 1 > ( 1 ); 1596__MATMAT ( get_work_estimation_buf_size , info -> temp_buffer_1_size , nullptr ); 1597info -> temp_buffer_1 = new sycl:: buffer < std:: uint8_t , 1 > ( 1598info -> temp_buffer_1_size -> get_host_access (sycl:: read_only )[ 0 ]); 1599__MATMAT ( work_estimation , info -> temp_buffer_1_size , info -> temp_buffer_1 ); 1600info -> temp_buffer_2_size = new sycl:: buffer < std:: int64_t , 1 > ( 1 ); 1601__MATMAT ( get_compute_structure_buf_size , info -> temp_buffer_2_size , nullptr ); 1602info -> temp_buffer_2 = new sycl:: buffer < std:: uint8_t , 1 > ( 1603info -> temp_buffer_2_size -> get_host_access (sycl:: read_only )[ 0 ]); 1604__MATMAT ( compute_structure , info -> temp_buffer_2_size , info -> temp_buffer_2 ); 1605sycl:: buffer < std:: int64_t , 1 > nnz_axb_buf ( 1 ); 1606__MATMAT ( get_nnz , & nnz_axb_buf , nullptr ); 1607nnz_axb = nnz_axb_buf . get_host_access (sycl:: read_only )[ 0 ]; 1608#else 1609info -> temp_buffer_1_size = sycl:: malloc_host < std:: int64_t > ( 1 , queue ); 1610__MATMAT ( get_work_estimation_buf_size , info -> temp_buffer_1_size , nullptr ); 1611queue . wait (); 1612info -> temp_buffer_1 = 1613sycl:: malloc_device < std:: uint8_t > ( info -> temp_buffer_1_size [ 0 ], queue ); 1614__MATMAT ( work_estimation , info -> temp_buffer_1_size , info -> temp_buffer_1 ); 1615info -> temp_buffer_2_size = sycl:: malloc_host < std:: int64_t > ( 1 , queue ); 1616__MATMAT ( get_compute_structure_buf_size , info -> temp_buffer_2_size , nullptr ); 1617queue . wait (); 1618info -> temp_buffer_2 = 1619sycl:: malloc_device < std:: uint8_t > ( info -> temp_buffer_2_size [ 0 ], queue ); 1620__MATMAT ( compute_structure , info -> temp_buffer_2_size , info -> temp_buffer_2 ); 1621std:: int64_t * nnz_axb_ptr = sycl:: malloc_host < std:: int64_t > ( 1 , queue ); 1622__MATMAT ( get_nnz , nnz_axb_ptr , nullptr ); 1623queue . wait (); 1624nnz_axb = * nnz_axb_ptr ; 1625sycl:: free ( nnz_axb_ptr , queue ); 1626#endif 1627 1628info -> col_ind_axb = ( int * )::dpct::cs:: malloc ( nnz_axb * sizeof( int ), queue ); 1629info -> val_axb = ( Ty * )::dpct::cs:: malloc ( nnz_axb * sizeof( Ty ), queue ); 1630info -> matrix_handle_axb . set_matrix_data < Ty > ( 1631m , n , oneapi::mkl::index_base:: zero , info -> row_ptr_axb , info -> col_ind_axb , 1632info -> val_axb ); 1633 1634__MATMAT ( finalize_structure , nullptr , nullptr ); 1635 1636std:: int64_t ws_size = 0 ; 1637oneapi::mkl::sparse:: omatadd_buffer_size ( 1638queue , oneapi::mkl::transpose:: nontrans , oneapi::mkl::transpose:: nontrans , 1639info -> matrix_handle_axb . get_handle (), info -> matrix_handle_d . get_handle (), 1640info -> matrix_handle_c . get_handle (), 1641oneapi::mkl::sparse::omatadd_alg:: default_alg , 1642info -> omatadd_desc . get_handle (), ws_size ); 1643* buffer_size_in_bytes = ws_size ; 1644} 1645 1646/// Calculate the non-zero elements number of the matrix C in following operation: 1647/// C = alpha * A * B + beta * D 1648/// \param [in] desc The descriptor of this calculation. 1649/// \param [in] m The rows number of A, D and C. 1650/// \param [in] n The columns number of B, D and C. 1651/// \param [in] k The columns number of A and rows number of B. 1652/// \param [in] info_a Matrix info of the matrix A. 1653/// \param [in] nnz_a Non-zero elements number of matrix A. 1654/// \param [in] row_ptr_a An array of length \p m + 1. 1655/// \param [in] col_ind_a An array containing the column indices in index-based 1656/// numbering. 1657/// \param [in] info_b Matrix info of the matrix B. 1658/// \param [in] nnz_b Non-zero elements number of matrix B. 1659/// \param [in] row_ptr_b An array of length \p k + 1. 1660/// \param [in] col_ind_b An array containing the column indices in index-based 1661/// numbering. 1662/// \param [in] info_d Matrix info of the matrix D. 1663/// \param [in] nnz_d Non-zero elements number of matrix D. 1664/// \param [in] row_ptr_d An array of length \p m + 1. 1665/// \param [in] col_ind_d An array containing the column indices in index-based 1666/// numbering. 1667/// \param [in] info_c Matrix info of the matrix C. 1668/// \param [in] row_ptr_c An array of length \p m + 1. 1669/// \param [out] nnz_ptr Non-zero elements number of matrix C. 1670/// \param [in] info The information of csrgemm2 operation. 1671/// \param [in] buffer Workspace memory. 1672inline void csrgemm2_nnz ( descriptor_ptr desc , int m , int n , int k , 1673const std:: shared_ptr < matrix_info > info_a , int nnz_a , 1674const int * row_ptr_a , const int * col_ind_a , 1675const std:: shared_ptr < matrix_info > info_b , int nnz_b , 1676const int * row_ptr_b , const int * col_ind_b , 1677const std:: shared_ptr < matrix_info > info_d , int nnz_d , 1678const int * row_ptr_d , const int * col_ind_d , 1679const std:: shared_ptr < matrix_info > info_c , 1680int * row_ptr_c , int * nnz_ptr , 1681std:: shared_ptr < csrgemm2_info > info , void * buffer ) { 1682sycl:: queue & queue = desc -> get_queue (); 1683if ( info -> matrix_c_datatype == 1684csrgemm2_info::matrix_c_datatype_t:: mcd_float ) { 1685info -> matrix_handle_c . set_matrix_data < float > ( m , n , info_c -> get_index_base (), 1686nullptr , nullptr , nullptr ); 1687} else if ( info -> matrix_c_datatype == 1688csrgemm2_info::matrix_c_datatype_t:: mcd_double ) { 1689info -> matrix_handle_c . set_matrix_data < double > ( 1690m , n , info_c -> get_index_base (), nullptr , nullptr , nullptr ); 1691} else if ( info -> matrix_c_datatype == 1692csrgemm2_info::matrix_c_datatype_t:: mcd_float2 ) { 1693info -> matrix_handle_c . set_matrix_data < std:: complex < float >> ( 1694m , n , info_c -> get_index_base (), nullptr , nullptr , nullptr ); 1695} else { 1696info -> matrix_handle_c . set_matrix_data < std:: complex < double >> ( 1697m , n , info_c -> get_index_base (), nullptr , nullptr , nullptr ); 1698} 1699auto data_buffer = dpct::detail:: get_memory < std:: uint8_t > ( buffer ); 1700oneapi::mkl::sparse:: omatadd_analyze ( 1701queue , oneapi::mkl::transpose:: nontrans , oneapi::mkl::transpose:: nontrans , 1702info -> matrix_handle_axb . get_handle (), info -> matrix_handle_d . get_handle (), 1703info -> matrix_handle_c . get_handle (), 1704oneapi::mkl::sparse::omatadd_alg:: default_alg , 1705info -> omatadd_desc . get_handle (), 1706#ifdef DPCT_USM_LEVEL_NONE 1707& data_buffer 1708#else 1709data_buffer 1710#endif 1711); 1712std:: int64_t nnz_c = 0 ; 1713oneapi::mkl::sparse:: omatadd_get_nnz ( 1714queue , oneapi::mkl::transpose:: nontrans , oneapi::mkl::transpose:: nontrans , 1715info -> matrix_handle_axb . get_handle (), info -> matrix_handle_d . get_handle (), 1716info -> matrix_handle_c . get_handle (), 1717oneapi::mkl::sparse::omatadd_alg:: default_alg , 1718info -> omatadd_desc . get_handle (), nnz_c ); 1719 1720assert (( nnz_c >= INT_MIN && nnz_c <= INT_MAX ) && "nnz_c is out of range." ); 1721int nnz_c_int = nnz_c ; 1722if ( nnz_ptr ) 1723::dpct::cs:: memcpy ( queue , nnz_ptr , & nnz_c_int , sizeof( int )). wait (); 1724int row_ptr_c_0 = 1725( info_c -> get_index_base () == oneapi::mkl::index_base:: zero ) ? 0 : 1 ; 1726nnz_c_int += row_ptr_c_0 ; 1727::dpct::cs:: memcpy ( queue , row_ptr_c + m , & nnz_c_int , sizeof( int )); 1728::dpct::cs:: memcpy ( queue , row_ptr_c , & row_ptr_c_0 , sizeof( int )). wait (); 1729} 1730 1731/// Computes the matrix C in the following operation: 1732/// C = alpha * A * B + beta * D 1733/// \param [in] desc The descriptor of this calculation. 1734/// \param [in] m The rows number of A, D and C. 1735/// \param [in] n The columns number of B, D and C. 1736/// \param [in] k The columns number of A and rows number of B. 1737/// \param [in] alpha Scaling factor. 1738/// \param [in] info_a Matrix info of the matrix A. 1739/// \param [in] nnz_a Non-zero elements number of matrix A. 1740/// \param [in] val_a An array containing the non-zero elements of the matrix A. 1741/// \param [in] row_ptr_a An array of length \p m + 1. 1742/// \param [in] col_ind_a An array containing the column indices in index-based 1743/// numbering. 1744/// \param [in] info_b Matrix info of the matrix B. 1745/// \param [in] nnz_b Non-zero elements number of matrix B. 1746/// \param [in] val_b An array containing the non-zero elements of the matrix B. 1747/// \param [in] row_ptr_b An array of length \p k + 1. 1748/// \param [in] col_ind_b An array containing the column indices in index-based 1749/// numbering. 1750/// \param [in] beta Scaling factor. 1751/// \param [in] info_d Matrix info of the matrix D. 1752/// \param [in] nnz_d Non-zero elements number of matrix D. 1753/// \param [in] val_d An array containing the non-zero elements of the matrix D. 1754/// \param [in] row_ptr_d An array of length \p m + 1. 1755/// \param [in] col_ind_d An array containing the column indices in index-based 1756/// numbering. 1757/// \param [in] info_c Matrix info of the matrix C. 1758/// \param [out] val_c An array containing the non-zero elements of the matrix 1759/// C. 1760/// \param [in] row_ptr_c An array of length \p m + 1. 1761/// \param [out] col_ind_c An array containing the column indices in index-based 1762/// numbering. 1763/// \param [in] info The information of csrgemm2 operation. 1764/// \param [in] buffer Workspace memory. 1765template < typename T > 1766void csrgemm2 ( descriptor_ptr desc , int m , int n , int k , const T * alpha , 1767const std:: shared_ptr < matrix_info > info_a , int nnz_a , 1768const T * val_a , const int * row_ptr_a , const int * col_ind_a , 1769const std:: shared_ptr < matrix_info > info_b , int nnz_b , 1770const T * val_b , const int * row_ptr_b , const int * col_ind_b , 1771const T * beta , const std:: shared_ptr < matrix_info > info_d , 1772int nnz_d , const T * val_d , const int * row_ptr_d , 1773const int * col_ind_d , const std:: shared_ptr < matrix_info > info_c , 1774T * val_c , const int * row_ptr_c , int * col_ind_c , 1775std:: shared_ptr < csrgemm2_info > info , void * buffer ) { 1776using Ty = typename ::dpct::detail:: lib_data_traits_t < T > ; 1777sycl:: queue & queue = desc -> get_queue (); 1778auto alpha_value = 1779dpct::detail:: get_value ( reinterpret_cast < const Ty *> ( alpha ), queue ); 1780auto beta_value = 1781dpct::detail:: get_value ( reinterpret_cast < const Ty *> ( beta ), queue ); 1782 1783info -> matrix_handle_a . set_matrix_data < Ty > ( m , k , info_a -> get_index_base (), 1784row_ptr_a , col_ind_a , val_a ); 1785info -> matrix_handle_b . set_matrix_data < Ty > ( k , n , info_b -> get_index_base (), 1786row_ptr_b , col_ind_b , val_b ); 1787__MATMAT ( compute , nullptr , nullptr ); 1788__MATMAT ( finalize , nullptr , nullptr ); 1789 1790info -> matrix_handle_d . set_matrix_data < Ty > ( m , n , info_d -> get_index_base (), 1791row_ptr_d , col_ind_d , val_d ); 1792info -> matrix_handle_c . set_matrix_data < Ty > ( m , n , info_c -> get_index_base (), 1793row_ptr_c , col_ind_c , val_c ); 1794 1795sycl:: event e ; 1796#ifndef DPCT_USM_LEVEL_NONE 1797e = 1798#endif 1799oneapi::mkl::sparse:: omatadd ( 1800queue , oneapi::mkl::transpose:: nontrans , 1801oneapi::mkl::transpose:: nontrans , alpha_value , 1802info -> matrix_handle_axb . get_handle (), beta_value , 1803info -> matrix_handle_d . get_handle (), 1804info -> matrix_handle_c . get_handle (), 1805oneapi::mkl::sparse::omatadd_alg:: default_alg , 1806info -> omatadd_desc . get_handle ()); 1807 1808std:: vector < sycl:: event > events ; 1809events . push_back ( e ); 1810events . push_back ( info -> release ( queue , e )); 1811queue . submit ([ & ](sycl:: handler & cgh ) { 1812cgh . depends_on ( events ); 1813cgh . host_task ([ _p1 = info -> row_ptr_axb , _p2 = info -> col_ind_axb , 1814_p3 = info -> val_axb , _p4 = info -> temp_buffer_1_size , 1815_p5 = info -> temp_buffer_1 , _p6 = info -> temp_buffer_2_size , 1816_p7 = info -> temp_buffer_2 , _q = queue ] { 1817::dpct::cs:: free ( _p1 , _q ); 1818::dpct::cs:: free ( _p2 , _q ); 1819::dpct::cs:: free ( _p3 , _q ); 1820#ifdef DPCT_USM_LEVEL_NONE 1821delete _p4 ; 1822delete _p5 ; 1823delete _p6 ; 1824delete _p7 ; 1825#else 1826sycl:: free ( _p4 , _q ); 1827sycl:: free ( _p5 , _q ); 1828sycl:: free ( _p6 , _q ); 1829sycl:: free ( _p7 , _q ); 1830#endif 1831}); 1832}); 1833} 1834 1835#endif 1836} // namespace dpct::sparse 1837 1838#endif // __DPCT_SPARSE_UTILS_HPP__