okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- blas_utils_detail.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_BLAS_UTILS_DETAIL_HPP__ 10#define __DPCT_BLAS_UTILS_DETAIL_HPP__ 11 12#include <thread> 13#include <utility> 14#include <vector> 15 16namespace dpct { 17namespace blas { 18namespace detail { 19template < typename target_t ,typename source_t > class parameter_wrapper_base_t { 20public : 21parameter_wrapper_base_t (sycl::queue q ,source_t * source ,size_t ele_num ) 22 :_source_attribute (::dpct::cs::detail::get_pointer_attribute (q ,source )), 23_q (q ),_source (source ),_ele_num (ele_num ), 24_target (construct_member_variable_target ()) {} 25 26 ~parameter_wrapper_base_t () { 27if (_need_free ) { 28_q .submit ([& ](sycl::handler & cgh ) { 29cgh .host_task ([t = _target ,q = _q ] { ::dpct::cs::free (t ,q ); }); 30 }); 31 } 32 } 33 34protected : 35 ::dpct::cs::detail::pointer_access_attribute _source_attribute ; 36 sycl::queue _q ; 37source_t * _source = nullptr ; 38size_t _ele_num ; 39bool _need_free = true; 40target_t * _target ; 41 42private : 43target_t * construct_member_variable_target () { 44if constexpr (std::is_same_v < target_t ,source_t > ) { 45if (_source_attribute == 46 ::dpct::cs::detail::pointer_access_attribute::host_only ) 47return (target_t * )::dpct::cs::malloc (sizeof (target_t )* _ele_num ,_q ); 48#ifdef DPCT_USM_LEVEL_NONE 49auto alloc = dpct::detail::mem_mgr::instance ().translate_ptr (_source ); 50size_t offset = (byte_t * )_source - alloc .alloc_ptr ; 51if (offset ) 52return (target_t * )::dpct::cs::malloc (sizeof (target_t )* _ele_num ,_q ); 53#endif 54// If (data type is same && it is device pointer && (USM || buffer offset 55// is 0)), it can be used directly. 56_need_free = false; 57return _source ; 58 }else { 59return (target_t * )::dpct::cs::malloc (sizeof (target_t )* _ele_num ,_q ); 60 } 61 } 62}; 63 64inline library_data_t compute_type_to_library_data_t (compute_type ct ) { 65switch (ct ) { 66case compute_type::f16 : 67case compute_type::f16_standard : 68return library_data_t::real_half ; 69case compute_type::f32 : 70case compute_type::f32_standard : 71case compute_type::f32_fast_bf16 : 72case compute_type::f32_fast_tf32 : 73return library_data_t::real_float ; 74case compute_type::f64 : 75case compute_type::f64_standard : 76return library_data_t::real_double ; 77case compute_type::i32 : 78case compute_type::i32_standard : 79return library_data_t::real_int32 ; 80default : 81throw std::runtime_error ("conversion is not supported." ); 82 } 83} 84}// namespace detail 85}// namespace blas 86 87namespace detail { 88 89inline void mem_free (sycl::queue * exec_queue , 90 std::vector < void *> pointers_array , sycl::event e ) { 91e .wait (); 92for (auto p :pointers_array ) 93 sycl::free (p ,* exec_queue ); 94} 95 96inline int stride_for (int num_elems ,int mem_align_in_elems ) { 97return ((num_elems - 1 ) /mem_align_in_elems + 1 )* mem_align_in_elems ; 98} 99 100#ifndef DPCT_USM_LEVEL_NONE 101template < typename T > class working_memory { 102T * _input_ptr ; 103T * _temp_ptr ; 104bool _is_sycl_malloced = false; 105bool _is_scalar_value = false; 106 sycl::queue _q ; 107 sycl::event _e ; 108 109public : 110working_memory (size_t size , sycl::queue q ) :_q (q ) { 111_is_scalar_value = false; 112_temp_ptr = (T * )sycl::malloc_device (size ,q ); 113 } 114working_memory (T * result_ptr , sycl::queue q ) :_input_ptr (result_ptr ),_q (q ) { 115_is_scalar_value = true; 116_is_sycl_malloced = sycl::get_pointer_type (_input_ptr ,_q .get_context ())!= 117 sycl::usm::alloc::unknown ; 118if (!_is_sycl_malloced ) 119_temp_ptr = sycl::malloc_shared < T > (1 ,_q ); 120 } 121auto get_ptr () { 122if (_is_scalar_value && _is_sycl_malloced ) 123return _input_ptr ; 124return _temp_ptr ; 125 } 126void set_event (sycl::event e ) {_e = e ; } 127 ~working_memory () { 128if (_is_scalar_value ) { 129if (!_is_sycl_malloced ) { 130_q .memcpy (_input_ptr ,_temp_ptr ,sizeof (T )).wait (); 131 sycl::free (_temp_ptr ,_q ); 132 } 133 }else { 134 std::vector < void *> ptrs {_temp_ptr }; 135 ::dpct::cs::enqueue_free (ptrs , {_e }); 136 } 137 } 138}; 139#endif 140 141template < typename Tx ,typename Tr > 142inline void nrm2_impl (sycl::queue & q , std::int64_t n ,const void * x , 143 std::int64_t incx ,void * result ) { 144#ifdef DPCT_USM_LEVEL_NONE 145auto x_buffer = dpct::get_buffer < Tx > (x ); 146auto r_buffer = 147 sycl::buffer < Tr ,1 > (reinterpret_cast < Tr *> (result ), sycl::range < 1 > (1 )); 148if (dpct::is_device_ptr (result )) 149r_buffer = dpct::get_buffer < Tr > (result ); 150 oneapi::mkl::blas::column_major::nrm2 (q ,n ,x_buffer ,incx ,r_buffer ); 151#else 152working_memory < Tr > res_mem (reinterpret_cast < Tr *> (result ),q ); 153 oneapi::mkl::blas::column_major::nrm2 (q ,n ,reinterpret_cast < const Tx *> (x ), 154incx ,res_mem .get_ptr ()); 155#endif 156} 157 158template < bool is_conjugate ,class Txy ,class Tr > 159inline void dotuc_impl (sycl::queue & q , std::int64_t n ,const Txy * x , 160 std::int64_t incx ,const Txy * y , std::int64_t incy , 161Tr * result ) { 162#ifdef DPCT_USM_LEVEL_NONE 163auto x_buffer = dpct::get_buffer < Txy > (x ); 164auto y_buffer = dpct::get_buffer < Txy > (y ); 165auto r_buffer = sycl::buffer < Tr ,1 > ((Tr * )result , sycl::range < 1 > (1 )); 166if (dpct::is_device_ptr (result )) 167r_buffer = dpct::get_buffer < Tr > (result ); 168if constexpr (std::is_same_v < Txy , std::complex < float >> || 169 std::is_same_v < Txy , std::complex < double >> ) { 170if constexpr (is_conjugate ) 171 oneapi::mkl::blas::column_major::dotc (q ,n ,x_buffer ,incx ,y_buffer , 172incy ,r_buffer ); 173else 174 oneapi::mkl::blas::column_major::dotu (q ,n ,x_buffer ,incx ,y_buffer , 175incy ,r_buffer ); 176 }else 177 oneapi::mkl::blas::column_major::dot (q ,n ,x_buffer ,incx ,y_buffer ,incy , 178r_buffer ); 179#else 180working_memory < Tr > res_mem (result ,q ); 181if constexpr (std::is_same_v < Txy , std::complex < float >> || 182 std::is_same_v < Txy , std::complex < double >> ) { 183if constexpr (is_conjugate ) 184 oneapi::mkl::blas::column_major::dotc (q ,n ,x ,incx ,y ,incy , 185res_mem .get_ptr ()); 186else 187 oneapi::mkl::blas::column_major::dotu (q ,n ,x ,incx ,y ,incy , 188res_mem .get_ptr ()); 189 }else 190 oneapi::mkl::blas::column_major::dot (q ,n ,x ,incx ,y ,incy , 191res_mem .get_ptr ()); 192#endif 193} 194 195template < bool is_conjugate > 196inline void dotuc (sycl::queue & q , std::int64_t n ,const void * x , 197library_data_t x_type , std::int64_t incx ,const void * y , 198library_data_t y_type , std::int64_t incy ,void * result , 199library_data_t result_type ) { 200 std::uint64_t key = 201 detail::get_type_combination_id (x_type ,y_type ,result_type ); 202switch (key ) { 203case detail::get_type_combination_id (library_data_t::real_float , 204 library_data_t::real_float , 205 library_data_t::real_float ): { 206 detail::dotuc_impl < is_conjugate > (q ,n ,reinterpret_cast < const float *> (x ), 207incx ,reinterpret_cast < const float *> (y ), 208incy ,reinterpret_cast < float *> (result )); 209break ; 210 } 211case detail::get_type_combination_id (library_data_t::real_double , 212 library_data_t::real_double , 213 library_data_t::real_double ): { 214 detail::dotuc_impl < is_conjugate > (q ,n ,reinterpret_cast < const double *> (x ), 215incx ,reinterpret_cast < const double *> (y ), 216incy ,reinterpret_cast < double *> (result )); 217break ; 218 } 219case detail::get_type_combination_id (library_data_t::complex_float , 220 library_data_t::complex_float , 221 library_data_t::complex_float ): { 222 detail::dotuc_impl < is_conjugate > ( 223q ,n ,reinterpret_cast < const std::complex < float > *> (x ),incx , 224reinterpret_cast < const std::complex < float > *> (y ),incy , 225reinterpret_cast < std::complex < float > *> (result )); 226break ; 227 } 228case detail::get_type_combination_id (library_data_t::complex_double , 229 library_data_t::complex_double , 230 library_data_t::complex_double ): { 231 detail::dotuc_impl < is_conjugate > ( 232q ,n ,reinterpret_cast < const std::complex < double > *> (x ),incx , 233reinterpret_cast < const std::complex < double > *> (y ),incy , 234reinterpret_cast < std::complex < double > *> (result )); 235break ; 236 } 237#ifdef __INTEL_MKL__ 238case detail::get_type_combination_id (library_data_t::real_half , 239 library_data_t::real_half , 240 library_data_t::real_half ): { 241 detail::dotuc_impl < is_conjugate > ( 242q ,n ,reinterpret_cast < const sycl::half *> (x ),incx , 243reinterpret_cast < const sycl::half *> (y ),incy , 244reinterpret_cast < sycl::half *> (result )); 245break ; 246 } 247#endif 248default : 249throw std::runtime_error ("the combination of data type is unsupported" ); 250 } 251} 252 253template < class Tx ,class Te > 254inline void scal_impl (sycl::queue & q , std::int64_t n ,const void * alpha , 255void * x , std::int64_t incx ) { 256Te alpha_val = get_value (reinterpret_cast < const Te *> (alpha ),q ); 257auto data_x = get_memory < Tx > (x ); 258 oneapi::mkl::blas::column_major::scal (q ,n ,alpha_val ,data_x ,incx ); 259} 260 261template < class Txy ,class Te > 262inline void axpy_impl (sycl::queue & q , std::int64_t n ,const void * alpha , 263const void * x , std::int64_t incx ,void * y , 264 std::int64_t incy ) { 265Te alpha_val = get_value (reinterpret_cast < const Te *> (alpha ),q ); 266auto data_x = get_memory < const Txy > (x ); 267auto data_y = get_memory < Txy > (y ); 268 oneapi::mkl::blas::column_major::axpy (q ,n ,alpha_val ,data_x ,incx ,data_y , 269incy ); 270} 271 272template < class Txy ,class Tc ,class Ts > 273inline void rot_impl (sycl::queue & q , std::int64_t n ,void * x , std::int64_t incx , 274void * y , std::int64_t incy ,const void * c ,const void * s ) { 275Tc c_value = get_value (reinterpret_cast < const Tc *> (c ),q ); 276Ts s_value = get_value (reinterpret_cast < const Ts *> (s ),q ); 277auto data_x = get_memory < Txy > (x ); 278auto data_y = get_memory < Txy > (y ); 279 oneapi::mkl::blas::column_major::rot (q ,n ,data_x ,incx ,data_y ,incy , 280c_value ,s_value ); 281} 282 283template < class Tx ,class Ty ,class Tparam > 284inline void rotm_impl (sycl::queue & q , std::int64_t n ,void * x ,int64_t incx , 285void * y ,int64_t incy ,const void * param ) { 286auto data_x = get_memory < Tx > (x ); 287auto data_y = get_memory < Ty > (y ); 288auto data_param = get_memory < Tparam > (const_cast < void *> (param )); 289 oneapi::mkl::blas::column_major::rotm (q ,n ,data_x ,incx ,data_y ,incy , 290data_param ); 291} 292 293template < class Tx ,class Ty > 294inline void copy_impl (sycl::queue & q , std::int64_t n ,const void * x , 295 std::int64_t incx ,void * y , std::int64_t incy ) { 296auto data_x = get_memory < const Tx > (x ); 297auto data_y = get_memory < Ty > (y ); 298 oneapi::mkl::blas::column_major::copy (q ,n ,data_x ,incx ,data_y ,incy ); 299} 300 301template < class Tx ,class Ty > 302inline void swap_impl (sycl::queue & q , std::int64_t n ,void * x , 303 std::int64_t incx ,void * y , std::int64_t incy ) { 304auto data_x = get_memory < Tx > (x ); 305auto data_y = get_memory < Ty > (y ); 306 oneapi::mkl::blas::column_major::swap (q ,n ,data_x ,incx ,data_y ,incy ); 307} 308 309template < class Tx ,class Tres > 310inline void asum_impl (sycl::queue & q , std::int64_t n ,const void * x , 311 std::int64_t incx ,void * res ) { 312auto data_x = get_memory < Tx > (x ); 313auto data_res = get_memory < Tres > (res ); 314 oneapi::mkl::blas::column_major::asum (q ,n ,data_x ,incx ,data_res ); 315} 316 317template < class T > 318inline void iamax_impl (sycl::queue & q , std::int64_t n ,const void * x , 319 std::int64_t incx , std::int64_t * res ) { 320#ifndef __INTEL_MKL__ 321throw std::runtime_error ("The oneAPI Math Kernel Library (oneMKL) Interfaces " 322"Project does not support this API." ); 323#else 324auto data_x = get_memory < T > (x ); 325auto data_res = get_memory < std::int64_t > (res ); 326 oneapi::mkl::blas::column_major::iamax (q ,n ,data_x ,incx ,data_res , 327 oneapi::mkl::index_base::one ); 328#endif 329} 330 331template < class T > 332inline void iamin_impl (sycl::queue & q , std::int64_t n ,const void * x , 333 std::int64_t incx , std::int64_t * res ) { 334#ifndef __INTEL_MKL__ 335throw std::runtime_error ("The oneAPI Math Kernel Library (oneMKL) Interfaces " 336"Project does not support this API." ); 337#else 338auto data_x = get_memory < T > (x ); 339auto data_res = get_memory < std::int64_t > (res ); 340 oneapi::mkl::blas::column_major::iamin (q ,n ,data_x ,incx ,data_res , 341 oneapi::mkl::index_base::one ); 342#endif 343} 344 345#ifdef __INTEL_MKL__ 346template < class Ta ,class Tb ,class Tc ,class Ts > 347inline void gemm_impl (sycl::queue & q , oneapi::mkl::transpose a_trans , 348 oneapi::mkl::transpose b_trans ,int m ,int n ,int k , 349const void * alpha ,const void * a ,int lda ,const void * b , 350int ldb ,const void * beta ,void * c ,int ldc , 351 oneapi::mkl::blas::compute_mode cm ) { 352Ts alpha_value = get_value (reinterpret_cast < const Ts *> (alpha ),q ); 353Ts beta_value = get_value (reinterpret_cast < const Ts *> (beta ),q ); 354auto data_a = get_memory < const Ta > (a ); 355auto data_b = get_memory < const Tb > (b ); 356auto data_c = get_memory < Tc > (c ); 357 oneapi::mkl::blas::column_major::gemm (q ,a_trans ,b_trans ,m ,n ,k , 358alpha_value ,data_a ,lda ,data_b ,ldb , 359beta_value ,data_c ,ldc ,cm ); 360} 361#endif 362 363#ifdef __INTEL_MKL__ 364#define DPCT_COMPUTE_MODE_PARAM , oneapi::mkl::blas::compute_mode cm 365#define DPCT_COMPUTE_MODE_ARG , cm 366#else 367#define DPCT_COMPUTE_MODE_PARAM 368#define DPCT_COMPUTE_MODE_ARG 369#endif 370 371template < class Ta ,class Tb ,class Tc ,class Ts > 372inline void gemm_batch_impl (sycl::queue & q , oneapi::mkl::transpose a_trans , 373 oneapi::mkl::transpose b_trans ,int m ,int n ,int k , 374const void * alpha ,const void ** a ,int lda , 375const void ** b ,int ldb ,const void * beta ,void ** c , 376int ldc ,int batch_sizeDPCT_COMPUTE_MODE_PARAM ) { 377struct matrix_info_t { 378 oneapi::mkl::transpose transpose_info [2 ]; 379Ts value_info [2 ]; 380 std::int64_t size_info [3 ]; 381 std::int64_t ld_info [3 ]; 382 std::int64_t groupsize_info ; 383 }; 384 385Ts alpha_value = get_value (reinterpret_cast < const Ts *> (alpha ),q ); 386Ts beta_value = get_value (reinterpret_cast < const Ts *> (beta ),q ); 387 388matrix_info_t * matrix_info = 389 (matrix_info_t * )std::malloc (sizeof (matrix_info_t )); 390matrix_info -> transpose_info [0 ]= a_trans ; 391matrix_info -> transpose_info [1 ]= b_trans ; 392matrix_info -> value_info [0 ]= alpha_value ; 393matrix_info -> value_info [1 ]= beta_value ; 394matrix_info -> size_info [0 ]= m ; 395matrix_info -> size_info [1 ]= n ; 396matrix_info -> size_info [2 ]= k ; 397matrix_info -> ld_info [0 ]= lda ; 398matrix_info -> ld_info [1 ]= ldb ; 399matrix_info -> ld_info [2 ]= ldc ; 400matrix_info -> groupsize_info = batch_size ; 401 402 sycl::event e = oneapi::mkl::blas::column_major::gemm_batch ( 403q ,matrix_info -> transpose_info ,matrix_info -> transpose_info + 1 , 404matrix_info -> size_info ,matrix_info -> size_info + 1 , 405matrix_info -> size_info + 2 ,matrix_info -> value_info , 406reinterpret_cast < const Ta **> (a ),matrix_info -> ld_info , 407reinterpret_cast < const Tb **> (b ),matrix_info -> ld_info + 1 , 408matrix_info -> value_info + 1 ,reinterpret_cast < Tc **> (c ), 409matrix_info -> ld_info + 2 ,1 , 410& (matrix_info -> groupsize_info )DPCT_COMPUTE_MODE_ARG ); 411 412q .submit ([& ](sycl::handler & cgh ) { 413cgh .depends_on (e ); 414cgh .host_task ([= ] { std::free (matrix_info ); }); 415 }); 416} 417 418template < class Ta ,class Tb ,class Tc ,class Ts > 419inline void gemm_batch_impl (sycl::queue & q , oneapi::mkl::transpose a_trans , 420 oneapi::mkl::transpose b_trans ,int m ,int n ,int k , 421const void * alpha ,const void * a ,int lda , 422long long int stride_a ,const void * b ,int ldb , 423long long int stride_b ,const void * beta ,void * c , 424int ldc ,long long int stride_c , 425int batch_sizeDPCT_COMPUTE_MODE_PARAM ) { 426Ts alpha_value = get_value (reinterpret_cast < const Ts *> (alpha ),q ); 427Ts beta_value = get_value (reinterpret_cast < const Ts *> (beta ),q ); 428auto data_a = get_memory < const Ta > (a ); 429auto data_b = get_memory < const Tb > (b ); 430auto data_c = get_memory < Tc > (c ); 431 oneapi::mkl::blas::column_major::gemm_batch ( 432q ,a_trans ,b_trans ,m ,n ,k ,alpha_value ,data_a ,lda ,stride_a ,data_b , 433ldb ,stride_b ,beta_value ,data_c ,ldc ,stride_c , 434batch_size DPCT_COMPUTE_MODE_ARG ); 435} 436 437template < bool is_hermitian ,class T > 438inline void syherk_impl (sycl::queue & q , oneapi::mkl::uplo uplo , 439 oneapi::mkl::transpose trans ,int n ,int k , 440const void * alpha ,const void * a ,int lda , 441const void * beta ,void * c , 442int ldc DPCT_COMPUTE_MODE_PARAM ) { 443auto data_a = get_memory < const T > (a ); 444auto data_c = get_memory < T > (c ); 445if constexpr (is_hermitian ) { 446auto alpha_value = 447get_value (reinterpret_cast < const typename T ::value_type *> (alpha ),q ); 448auto beta_value = 449get_value (reinterpret_cast < const typename T ::value_type *> (beta ),q ); 450 oneapi::mkl::blas::column_major::herk (q ,uplo ,trans ,n ,k ,alpha_value , 451data_a ,lda ,beta_value ,data_c , 452ldc DPCT_COMPUTE_MODE_ARG ); 453 }else { 454T alpha_value = get_value (reinterpret_cast < const T *> (alpha ),q ); 455T beta_value = get_value (reinterpret_cast < const T *> (beta ),q ); 456 oneapi::mkl::blas::column_major::syrk (q ,uplo ,trans ,n ,k ,alpha_value , 457data_a ,lda ,beta_value ,data_c , 458ldc DPCT_COMPUTE_MODE_ARG ); 459 } 460} 461 462template < bool is_hermitian ,class T ,class Tbeta > 463inline void rk_impl (sycl::queue & q , oneapi::mkl::uplo uplo , 464 oneapi::mkl::transpose trans ,int n ,int k ,const T * alpha , 465const T * a ,int lda ,const T * b ,int ldb ,const Tbeta * beta , 466T * c ,int ldc DPCT_COMPUTE_MODE_PARAM ) { 467// For symmetric matrix, this function performs: C = alpha*OP(A)*(OP(B))^T + 468// beta*C For Hermitian matrix, this function performs: C = 469// alpha*OP(A)*(OP(B))^H + beta*C The gemmt() function performs: C = 470// alpha*OPA(A)*OPB(B) + beta*C So the OPB need be updated before we call 471// gemmt(). 472using Ty = typename ::dpct::detail::lib_data_traits_t < T > ; 473using Ts = typename ::dpct::detail::lib_data_traits_t < Tbeta > ; 474Ty alpha_value = get_value (reinterpret_cast < const Ty *> (alpha ),q ); 475Ts beta_value = get_value (reinterpret_cast < const Ts *> (beta ),q ); 476 oneapi::mkl::transpose trans_A = trans ,trans_B = trans ; 477int origin_b_rows = trans == oneapi::mkl::transpose::nontrans ?n :k ; 478int origin_b_cols = trans == oneapi::mkl::transpose::nontrans ?k :n ; 479 480if ((is_hermitian && trans == oneapi::mkl::transpose::trans )|| 481 (!is_hermitian && !std::is_floating_point_v < Ty > && 482trans == oneapi::mkl::transpose::conjtrans )) { 483// In this case, OPB need be a conjugate operation, 484// but only notrans, conjtrans and trans are available. 485// So we need do a conjtrans operation first, then do a trans operation. 486trans_B = oneapi::mkl::transpose::trans ; 487auto data_a = get_memory < const Ty > (a ); 488auto data_c = get_memory < Ty > (c ); 489#ifdef DPCT_USM_LEVEL_NONE 490auto new_B_buffer = 491 sycl::buffer < Ty ,1 > (sycl::range < 1 > (origin_b_rows * origin_b_cols )); 492auto from_buffer = dpct::get_buffer < Ty > (b ); 493 oneapi::mkl::blas::column_major::omatcopy_batch ( 494q , oneapi::mkl::transpose::conjtrans ,origin_b_rows ,origin_b_cols , 495Ts (1.0 ),from_buffer ,ldb ,origin_b_rows * ldb ,new_B_buffer , 496origin_b_cols ,origin_b_rows * origin_b_cols ,1 ); 497 oneapi::mkl::blas::column_major::gemmt ( 498q ,uplo ,trans_A ,trans_B ,n ,k ,alpha_value ,data_a ,lda ,new_B_buffer , 499origin_b_cols ,beta_value ,data_c ,ldc DPCT_COMPUTE_MODE_ARG ); 500#else 501working_memory < T > new_B (origin_b_rows * origin_b_cols * sizeof (T ),q ); 502 oneapi::mkl::blas::column_major::omatcopy_batch ( 503q , oneapi::mkl::transpose::conjtrans ,origin_b_rows ,origin_b_cols , 504Ts (1.0 ),reinterpret_cast < const Ty *> (b ),ldb ,origin_b_rows * ldb , 505reinterpret_cast < Ty *> (new_B .get_ptr ()),origin_b_cols , 506origin_b_rows * origin_b_cols ,1 ); 507 sycl::event e = oneapi::mkl::blas::column_major::gemmt ( 508q ,uplo ,trans_A ,trans_B ,n ,k ,alpha_value ,data_a ,lda , 509reinterpret_cast < Ty *> (new_B .get_ptr ()),origin_b_cols ,beta_value , 510data_c ,ldc DPCT_COMPUTE_MODE_ARG ); 511new_B .set_event (e ); 512#endif 513 }else { 514if constexpr (is_hermitian ) { 515trans_B = trans == oneapi::mkl::transpose::nontrans 516 ? oneapi::mkl::transpose::conjtrans 517 : oneapi::mkl::transpose::nontrans ; 518 }else { 519trans_B = trans == oneapi::mkl::transpose::nontrans 520 ? oneapi::mkl::transpose::trans 521 : oneapi::mkl::transpose::nontrans ; 522 } 523auto data_a = get_memory < const Ty > (a ); 524auto data_b = get_memory < const Ty > (b ); 525auto data_c = get_memory < Ty > (c ); 526 oneapi::mkl::blas::column_major::gemmt ( 527q ,uplo ,trans_A ,trans_B ,n ,k ,alpha_value ,data_a ,lda ,data_b ,ldb , 528beta_value ,data_c ,ldc DPCT_COMPUTE_MODE_ARG ); 529 } 530} 531 532template < class Ta ,class Tb ,class Ts > 533inline void 534trsm_batch_impl (sycl::queue & q , oneapi::mkl::side left_right , 535 oneapi::mkl::uplo upper_lower , oneapi::mkl::transpose trans , 536 oneapi::mkl::diag unit_diag ,int m ,int n ,const void * alpha , 537const void ** a ,int lda ,void ** b ,int ldb , 538int batch_sizeDPCT_COMPUTE_MODE_PARAM ) { 539struct matrix_info_t { 540matrix_info_t (oneapi::mkl::side side_info , oneapi::mkl::uplo uplo_info , 541 oneapi::mkl::transpose transpose_info , 542 oneapi::mkl::diag diag_info ,Ts value_info , std::int64_t m , 543 std::int64_t n , std::int64_t lda , std::int64_t ldb , 544 std::int64_t groupsize_info ) 545 :side_info (side_info ),uplo_info (uplo_info ), 546transpose_info (transpose_info ),diag_info (diag_info ), 547value_info (value_info ),groupsize_info (groupsize_info ) { 548size_info [0 ]= m ; 549size_info [1 ]= n ; 550ld_info [0 ]= lda ; 551ld_info [1 ]= ldb ; 552 } 553 oneapi::mkl::side side_info ; 554 oneapi::mkl::uplo uplo_info ; 555 oneapi::mkl::transpose transpose_info ; 556 oneapi::mkl::diag diag_info ; 557Ts value_info ; 558 std::int64_t size_info [2 ]; 559 std::int64_t ld_info [2 ]; 560 std::int64_t groupsize_info ; 561 }; 562 563Ts alpha_value = get_value (reinterpret_cast < const Ts *> (alpha ),q ); 564 565matrix_info_t * matrix_info = 566new matrix_info_t (left_right ,upper_lower ,trans ,unit_diag ,alpha_value , 567m ,n ,lda ,ldb ,batch_size ); 568 569 sycl::event e = oneapi::mkl::blas::column_major::trsm_batch ( 570q ,& (matrix_info -> side_info ),& (matrix_info -> uplo_info ), 571& (matrix_info -> transpose_info ),& (matrix_info -> diag_info ), 572matrix_info -> size_info ,matrix_info -> size_info + 1 , 573& (matrix_info -> value_info ),reinterpret_cast < const Ta **> (a ), 574matrix_info -> ld_info ,reinterpret_cast < Tb **> (b ), 575matrix_info -> ld_info + 1 ,1 , 576& (matrix_info -> groupsize_info )DPCT_COMPUTE_MODE_ARG ); 577 578q .submit ([& ](sycl::handler & cgh ) { 579cgh .depends_on (e ); 580cgh .host_task ([= ] {delete matrix_info ; }); 581 }); 582} 583 584template < typename T > 585inline void getrfnp_batch_wrapper (sycl::queue & exec_queue ,int n ,T * a [], 586int lda ,int * info ,int batch_size ) { 587#ifndef __INTEL_MKL__ 588throw std::runtime_error ("The oneAPI Math Kernel Library (oneMKL) Interfaces " 589"Project does not support this API." ); 590#else 591using Ty = typename ::dpct::detail::lib_data_traits_t < T > ; 592// Set the info array value to 0 593 ::dpct::cs::fill < unsigned char > (exec_queue ,info ,0 , 594sizeof (int )* batch_size ); 595 std::int64_t stride_a = n * lda ; 596 std::int64_t scratchpad_size = 597 oneapi::mkl::lapack::getrfnp_batch_scratchpad_size < Ty > ( 598exec_queue ,n ,n ,lda ,stride_a ,batch_size ); 599 600Ty * a_strided_mem = 601 (Ty * )::dpct::cs::malloc (stride_a * batch_size * sizeof (Ty ),exec_queue ); 602T ** host_a = (T ** )std::malloc (batch_size * sizeof (T * )); 603 ::dpct::cs::memcpy (::dpct::cs::get_default_queue (),host_a ,a , 604batch_size * sizeof (T * )) 605 .wait (); 606for (std::int64_t i = 0 ;i < batch_size ;++ i ) 607 ::dpct::cs::memcpy (::dpct::cs::get_default_queue (), 608a_strided_mem + i * stride_a ,host_a [i ], 609n * lda * sizeof (T )) 610 .wait (); 611 612#ifdef DPCT_USM_LEVEL_NONE 613 { 614 sycl::buffer < Ty ,1 > scratchpad {sycl::range < 1 > (scratchpad_size )}; 615auto a_buffer = get_buffer < Ty > (a_strided_mem ); 616 oneapi::mkl::lapack::getrfnp_batch (exec_queue ,n ,n ,a_buffer ,lda , 617stride_a ,batch_size ,scratchpad , 618scratchpad_size ); 619 } 620 std::vector < sycl::event > events ; 621for (std::int64_t i = 0 ;i < batch_size ;++ i ) 622events .push_back (::dpct::cs::memcpy ( 623exec_queue ,host_a [i ],a_strided_mem + i * stride_a , 624n * lda * sizeof (T ), ::dpct::cs::memcpy_direction::automatic )); 625#else 626Ty * scratchpad = sycl::malloc_device < Ty > (scratchpad_size ,exec_queue ); 627 sycl::event e = oneapi::mkl::lapack::getrfnp_batch ( 628exec_queue ,n ,n ,a_strided_mem ,lda ,stride_a ,batch_size ,scratchpad , 629scratchpad_size ); 630 std::vector < sycl::event > events ; 631for (std::int64_t i = 0 ;i < batch_size ;++ i ) 632events .push_back (::dpct::cs::memcpy ( 633exec_queue ,host_a [i ],a_strided_mem + i * stride_a , 634n * lda * sizeof (T ), ::dpct::cs::memcpy_direction::automatic , {e })); 635 636 std::vector < void *> ptrs {scratchpad ,a_strided_mem }; 637 ::dpct::cs::enqueue_free (ptrs ,events ,exec_queue ); 638#endif 639 640exec_queue .submit ([& ](sycl::handler & cgh ) { 641cgh .depends_on (events ); 642cgh .host_task ([= ] { std::free (host_a ); }); 643 }); 644#endif 645} 646}// namespace detail 647}// namespace dpct 648 649#endif // __DPCT_BLAS_UTILS_DETAIL_HPP__