okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- lib_common_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_LIB_COMMON_UTILS_DETAIL_HPP__ 10#define __DPCT_LIB_COMMON_UTILS_DETAIL_HPP__ 11 12namespace dpct::detail { 13template < typename T > struct lib_data_traits {using type = T ; }; 14template < typename T > struct lib_data_traits < sycl::vec < T ,2 >> { 15using type = std::complex < T > ; 16}; 17template < class T > using lib_data_traits_t = typename lib_data_traits < T > ::type ; 18 19template < typename T > inline auto get_memory (const void * x ) { 20T * new_x = reinterpret_cast < T *> (const_cast < void *> (x )); 21#ifdef DPCT_USM_LEVEL_NONE 22return dpct::get_buffer < std::remove_cv_t < T >> (new_x ); 23#else 24return new_x ; 25#endif 26} 27 28template < typename T > 29inline typename ::dpct::detail::lib_data_traits_t < T > get_value (const T * s , 30 sycl::queue & q ) { 31using Ty = typename ::dpct::detail::lib_data_traits_t < T > ; 32Ty s_h ; 33if (::dpct::cs::detail::get_pointer_attribute (q ,s )== 34 ::dpct::cs::detail::pointer_access_attribute::device_only ) 35 ::dpct::cs::memcpy (q , (void * )& s_h , (void * )s ,sizeof (T ), 36 ::dpct::cs::memcpy_direction::device_to_host ) 37 .wait (); 38else 39s_h = * reinterpret_cast < const Ty *> (s ); 40return s_h ; 41} 42 43template < typename ArgT > 44inline constexpr std::uint64_t get_type_combination_id (ArgT Val ) { 45 static_assert((unsigned char )library_data_t::library_data_t_size <= 46 std::numeric_limits < unsigned char > ::max ()&& 47"library_data_t size exceeds limit." ); 48 static_assert(std::is_same_v < ArgT ,library_data_t > ,"Unsupported ArgT" ); 49return (std::uint64_t )Val ; 50} 51 52template < typename FirstT ,typename ...RestT > 53inline constexpr std::uint64_t get_type_combination_id (FirstT FirstVal , 54RestT ...RestVal ) { 55 static_assert((std::uint8_t )library_data_t::library_data_t_size <= 56 std::numeric_limits < unsigned char > ::max ()&& 57"library_data_t size exceeds limit." ); 58 static_assert(sizeof ...(RestT ) <=8 && "Too many parameters" ); 59 static_assert(std::is_same_v < FirstT ,library_data_t > ,"Unsupported FirstT" ); 60return get_type_combination_id (RestVal ...) <<8 | ((std::uint64_t )FirstVal ); 61} 62 63inline constexpr std::size_t library_data_size []= { 648 * sizeof (float ),// real_float 658 * sizeof (std::complex < float > ),// complex_float 668 * sizeof (double ),// real_double 678 * sizeof (std::complex < double > ),// complex_double 688 * sizeof (sycl::half ),// real_half 698 * sizeof (std::complex < sycl::half > ),// complex_half 7016 ,// real_bfloat16 7116 * 2 ,// complex_bfloat16 724 ,// real_int4 734 * 2 ,// complex_int4 744 ,// real_uint4 754 * 2 ,// complex_uint4 768 ,// real_int8 778 * 2 ,// complex_int8 788 ,// real_uint8 798 * 2 ,// complex_uint8 8016 ,// real_int16 8116 * 2 ,// complex_int16 8216 ,// real_uint16 8316 * 2 ,// complex_uint16 8432 ,// real_int32 8532 * 2 ,// complex_int32 8632 ,// real_uint32 8732 * 2 ,// complex_uint32 8864 ,// real_int64 8964 * 2 ,// complex_int64 9064 ,// real_uint64 9164 * 2 ,// complex_uint64 928 ,// real_int8_4 938 ,// real_int8_32 948 // real_uint8_4 95}; 96 97template < class func_t ,typename ...args_t > 98std::invoke_result_t < func_t ,args_t ...> 99catch_batch_error (int * has_execption ,const std::string & api_names , 100 sycl::queue exec_queue ,int * info ,int * dev_info , 101int batch_size ,func_t && f ,args_t && ...args ) { 102try { 103return f (std::forward < args_t > (args )...); 104 }catch (oneapi::mkl::lapack::batch_error const & be ) { 105 std::cerr <<"Unexpected exception caught during call to API(s): " 106 <<api_names << std::endl 107 <<"reason: " <<be .what () << std::endl 108 <<"number: " <<be .info () << std::endl ; 109if (be .info ()< 0 && info ) 110* info = be .info (); 111int i = 0 ; 112auto & ids = be .ids (); 113 std::vector < int > info_vec (batch_size ,0 ); 114for (auto const & e :be .exceptions ()) { 115try { 116 std::rethrow_exception (e ); 117 }catch (oneapi::mkl::lapack::exception & e ) { 118 std::cerr <<"Exception in problem: " <<ids [i ] << std::endl 119 <<"reason: " <<e .what () << std::endl 120 <<"info: " <<e .info () << std::endl ; 121info_vec [ids [i ]]= e .info (); 122i ++ ; 123 } 124 } 125if (dev_info ) 126exec_queue .memcpy (dev_info ,info_vec .data (),batch_size * sizeof (int )) 127 .wait (); 128 }catch (sycl::exception const & e ) { 129 std::cerr <<"Caught synchronous SYCL exception:" << std::endl 130 <<"reason: " <<e .what () << std::endl ; 131if (dev_info ) 132exec_queue .memset (dev_info ,0 ,batch_size * sizeof (int )).wait (); 133 } 134if (has_execption ) 135* has_execption = 1 ; 136return {}; 137} 138 139template < typename ret_t ,typename ...args_t > 140ret_t catch_batch_error_f (int * has_execption ,const std::string & api_names , 141 sycl::queue exec_queue ,int * info ,int * dev_info , 142int batch_size ,ret_t (* f )(args_t ...), 143args_t && ...args ) { 144return catch_batch_error (has_execption ,api_names ,exec_queue ,info ,dev_info , 145batch_size ,f ,args ...); 146} 147}// namespace dpct::detail 148 149#endif // __DPCT_LIB_COMMON_UTILS_DETAIL_HPP__