okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==----------- basic.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#ifndef __DPCT_CODEPIN_SER_BASIC_HPP__ 9#define __DPCT_CODEPIN_SER_BASIC_HPP__ 10 11#if defined(__linux__ ) 12#include <cxxabi.h> 13#endif 14#include <fstream> 15#include <iostream> 16#include <sstream> 17#include <string> 18#ifdef __NVCC__ 19#include <cuda_bf16.h> 20#include <cuda_fp16.h> 21#include <cuda_runtime.h> 22#else 23#include <dpct/dpct.hpp> 24#include <sycl/sycl.hpp> 25#endif 26 27namespace dpct { 28namespace experimental { 29namespace codepin { 30 31#ifdef __NVCC__ 32typedef cudaStream_t queue_t ; 33#else 34typedef dpct::queue_ptr queue_t ; 35#endif 36 37#ifdef __NVCC__ 38typedef cudaEvent_t event_t ; 39#else 40typedef sycl::event event_t ; 41#endif 42 43namespace detail { 44 45inline bool is_dev_ptr (void * p ) { 46#ifdef __NVCC__ 47cudaPointerAttributes attr ; 48cudaPointerGetAttributes (& attr ,p ); 49if (attr .type == cudaMemoryTypeDevice ) 50return true; 51return false; 52#else 53 dpct::pointer_attributes attributes ; 54attributes .init (p ); 55if (attributes .get_device_pointer ()!= nullptr ) 56return true; 57return false; 58#endif 59} 60 61class json_stringstream { 62public : 63json_stringstream (std::ofstream & ofst ) :os (ofst ) { 64if (!ofst .is_open ()) { 65throw std::runtime_error ("Error while openning file: " ); 66 } 67 } 68 69private : 70 std::string indent ; 71const size_t tab_length = 2 ; 72 std::ofstream & os ; 73 74#if defined(__linux__ ) 75const std::string newline = "\n" ; 76#elif defined(_WIN64 ) 77const std::string newline = "\r\n" ; 78#else 79#error Only support windows and Linux. 80#endif 81 82 83public : 84class json_obj { 85bool isFirst = true; 86json_stringstream & js ; 87private : 88friend class json_stringstream ; 89json_obj (json_stringstream & json_ss ) :js (json_ss ) { 90js .os <<"{" <<js .newline ; 91js .indent .append (js .tab_length ,' ' ); 92js .os <<js .indent ; 93 } 94 95public : 96template < class T > T value (); 97 98void key (std::string_view key ) { 99if (!isFirst ){ 100js .os <<"," <<js .newline <<js .indent ; 101 }else { 102isFirst = false; 103 } 104js .os <<"\"" <<key <<"\": " ; 105 }; 106 107void value (std::string_view value ) {js .os <<"\"" <<value <<"\"" ; }; 108void value (float value ) {js .os <<"\"" <<value <<"\"" ; }; 109void value (size_t value ) {js .os <<"\"" <<value <<"\"" ; }; 110void value (int value ) {js .os <<"\"" <<value <<"\"" ; }; 111void value (void * value ) {js .os <<"\"" <<value <<"\"" ; }; 112 ~json_obj () { 113js .indent .resize (js .indent .size ()- js .tab_length ); 114js .os <<js .newline ; 115js .os <<js .indent ; 116js .os <<"}" ; 117 } 118 }; 119 120class json_array { 121bool isFirst = true; 122json_stringstream & js ; 123public : 124json_array (json_stringstream & json_ss ) :js (json_ss ) { 125if (!(js .os )) 126return ; 127js .os <<"[" <<js .newline ; 128js .indent .append (js .tab_length ,' ' ); 129js .os <<js .indent ; 130 } 131 132json_obj object () { 133if (!isFirst ){ 134js .os <<"," <<js .newline <<js .indent ; 135 }else { 136isFirst = false; 137 } 138return json_obj (js ); 139 } 140 141template < class MemberT > 142void member (const MemberT & t ) { 143if (!isFirst ){ 144js .os <<"," <<js .newline <<js .indent ; 145 }else { 146isFirst = false; 147 } 148js .os <<t ; 149 } 150 151 ~json_array () { 152js .indent .resize (js .indent .size ()- js .tab_length ); 153js .os <<js .newline ; 154js .os <<js .indent ; 155js .os <<"]" ; 156 } 157 }; 158 159template < typename T ,typename = std::enable_if_t < 160 !std::is_same_v < const char * , std::decay_t < T >>>> 161json_stringstream & operator<<(T && value ) { 162os << std::forward < T > (value ); 163return * this ; 164 } 165 166json_obj object (){ 167return json_obj (* this ); 168 } 169json_array array (){ 170return json_array (* this ); 171 } 172}; 173 174template <> 175inline json_stringstream::json_obj 176json_stringstream::json_obj::value < json_stringstream::json_obj > () { 177return js .object (); 178} 179 180template < typename T > inline std::string demangle_name () { 181 std::string ret_str = "" ; 182#if defined(__linux__ ) 183int s ; 184auto mangle_name = typeid (T ).name (); 185auto demangle_name = abi::__cxa_demangle (mangle_name ,NULL ,NULL ,& s ); 186if (s != 0 ) { 187ret_str = "CODEPIN:ERROR:0: Unable to demangle symbol " + 188 std::string (mangle_name )+ "." ; 189 }else { 190ret_str = demangle_name ; 191 std::free (demangle_name ); 192 } 193#else 194ret_str = typeid (T ).name (); 195#endif 196return ret_str ; 197} 198 199#ifdef __NVCC__ 200template <> inline std::string demangle_name < __half > () {return "fp16" ; } 201template <> inline std::string demangle_name < __nv_bfloat16 > () {return "bf16" ; } 202#else 203template <> inline std::string demangle_name < sycl::half > () {return "fp16" ; } 204template <> inline std::string demangle_name < sycl::ext::oneapi::bfloat16 > () { 205return "bf16" ; 206} 207#endif 208 209template < class T ,class T2 = void > class data_ser { 210 211public : 212static void dump (json_stringstream & ss ,T value ,queue_t queue ) { 213auto obj = ss .object (); 214obj .key ("Data" ); 215obj .value ("CODEPIN:ERROR:1: Unable to find the corresponding serialization " 216"function." ); 217 } 218static void print_type_name (json_stringstream::json_obj & obj ) { 219obj .key ("Type" ); 220obj .value (std::string (demangle_name < T > ())); 221 } 222}; 223 224template < class T > 225class data_ser < T ,typename std::enable_if < std::is_arithmetic < T > ::value > ::type > { 226public : 227static void dump (json_stringstream & ss ,const T & value ,queue_t queue ) { 228auto arr = ss .array (); 229arr .member < T > (value ); 230 } 231static void print_type_name (json_stringstream::json_obj & obj ) { 232obj .key ("Type" ); 233obj .value (std::string (demangle_name < T > ())); 234 } 235}; 236 237#ifdef __NVCC__ 238template <> class data_ser < __half > { 239public : 240static void dump (json_stringstream & ss ,const __half & value ,queue_t queue ) { 241float f = __half2float (value ); 242auto arr = ss .array (); 243arr .member < float > (value ); 244 } 245static void print_type_name (json_stringstream::json_obj & obj ) { 246obj .key ("Type" ); 247obj .value (std::string (demangle_name < __half > ())); 248 } 249}; 250template <> class data_ser < __nv_bfloat16 > { 251public : 252static void dump (json_stringstream & ss ,const __nv_bfloat16 & value , 253queue_t queue ) { 254float f = __bfloat162float (value ); 255auto arr = ss .array (); 256arr .member < float > (value ); 257 } 258static void print_type_name (json_stringstream::json_obj & obj ) { 259obj .key ("Type" ); 260obj .value (std::string (demangle_name < __nv_bfloat16 > ())); 261 } 262}; 263#else 264template < typename T > 265class data_ser < T , 266typename std::enable_if < 267 std::is_same < T , sycl::half > ::value || 268 std::is_same < T , sycl::ext::oneapi::bfloat16 > ::value > ::type > { 269public : 270static void dump (json_stringstream & ss ,const T & value ,queue_t queue ) { 271auto arr = ss .array (); 272arr .member < T > (value ); 273 } 274static void print_type_name (json_stringstream::json_obj & obj ) { 275obj .key ("Type" ); 276obj .value (std::string (demangle_name < T > ())); 277 } 278 279}; 280#endif 281 282#ifdef __NVCC__ 283template <> class data_ser < int3 > { 284public : 285static void dump (json_stringstream & ss ,const int3 & value , 286queue_t queue ) { 287auto arr = ss .array (); 288 { 289auto obj_x = arr .object (); 290obj_x .key ("x" ); 291auto value_x = 292obj_x 293 .value < json_stringstream::json_obj > (); 294data_ser < int > ::print_type_name (value_x ); 295value_x .key ("Data" ); 296data_ser < int > ::dump (ss ,value .x ,queue ); 297 } 298 { 299auto obj_y = arr .object (); 300obj_y .key ("y" ); 301auto value_y = 302obj_y 303 .value < json_stringstream::json_obj > (); 304data_ser < int > ::print_type_name (value_y ); 305value_y .key ("Data" ); 306data_ser < int > ::dump (ss ,value .y ,queue ); 307 } 308 { 309auto obj_z = arr .object (); 310obj_z .key ("z" ); 311auto value_z = 312obj_z 313 .value < json_stringstream::json_obj > (); 314data_ser < int > ::print_type_name (value_z ); 315value_z .key ("Data" ); 316data_ser < int > ::dump (ss ,value .z ,queue ); 317 } 318 } 319static void print_type_name (json_stringstream::json_obj & obj ){ 320obj .key ("Type" ); 321obj .value ("int3" ); 322 } 323}; 324 325template <> class data_ser < float3 > { 326public : 327static void dump (json_stringstream & ss ,const float3 & value , 328queue_t queue ) { 329auto arr = ss .array (); 330 { 331auto obj_x = arr .object (); 332obj_x .key ("x" ); 333auto value_x = 334obj_x 335 .value < json_stringstream::json_obj > (); 336data_ser < float > ::print_type_name (value_x ); 337value_x .key ("Data" ); 338data_ser < float > ::dump (ss ,value .x ,queue ); 339 } 340 { 341auto obj_y = arr .object (); 342obj_y .key ("y" ); 343auto value_y = 344obj_y 345 .value < json_stringstream::json_obj > (); 346data_ser < float > ::print_type_name (value_y ); 347value_y .key ("Data" ); 348data_ser < float > ::dump (ss ,value .y ,queue ); 349 } 350 { 351auto obj_z = arr .object (); 352obj_z .key ("z" ); 353auto value_z = 354obj_z 355 .value < json_stringstream::json_obj > (); 356data_ser < float > ::print_type_name (value_z ); 357value_z .key ("Data" ); 358data_ser < float > ::dump (ss ,value .z ,queue ); 359 } 360 } 361static void print_type_name (json_stringstream::json_obj & obj ){ 362obj .key ("Type" ); 363obj .value ("float3" ); 364 } 365}; 366 367#else 368template <> class data_ser < sycl::int3 > { 369public : 370static void dump (json_stringstream & ss ,const sycl::int3 & value , 371queue_t queue ) { 372auto arr = ss .array (); 373 { 374auto obj_x = arr .object (); 375obj_x .key ("x" ); 376auto value_x = 377obj_x 378 .value < json_stringstream::json_obj > (); 379data_ser < int > ::print_type_name (value_x ); 380value_x .key ("Data" ); 381data_ser < int > ::dump (ss ,value .x (),queue ); 382 } 383 { 384auto obj_y = arr .object (); 385obj_y .key ("y" ); 386auto value_y = 387obj_y 388 .value < json_stringstream::json_obj > (); 389data_ser < int > ::print_type_name (value_y ); 390value_y .key ("Data" ); 391data_ser < int > ::dump (ss ,value .y (),queue ); 392 } 393 { 394auto obj_z = arr .object (); 395obj_z .key ("z" ); 396auto value_z = 397obj_z 398 .value < json_stringstream::json_obj > (); 399data_ser < int > ::print_type_name (value_z ); 400value_z .key ("Data" ); 401data_ser < int > ::dump (ss ,value .z (),queue ); 402 } 403 } 404static void print_type_name (json_stringstream::json_obj & obj ){ 405obj .key ("Type" ); 406obj .value ("sycl::int3" ); 407 } 408}; 409 410template <> class data_ser < sycl::float3 > { 411public : 412static void dump (json_stringstream & ss ,const sycl::float3 & value , 413queue_t queue ) { 414auto arr = ss .array (); 415 { 416auto obj_x = arr .object (); 417obj_x .key ("x" ); 418auto value_x = 419obj_x 420 .value < json_stringstream::json_obj > (); 421data_ser < float > ::print_type_name (value_x ); 422value_x .key ("Data" ); 423data_ser < float > ::dump (ss ,value .x (),queue ); 424 } 425 { 426auto obj_y = arr .object (); 427obj_y .key ("y" ); 428auto value_y = 429obj_y 430 .value < json_stringstream::json_obj > (); 431data_ser < float > ::print_type_name (value_y ); 432value_y .key ("Data" ); 433data_ser < float > ::dump (ss ,value .y (),queue ); 434 } 435 { 436auto obj_z = arr .object (); 437obj_z .key ("z" ); 438auto value_z = 439obj_z 440 .value < json_stringstream::json_obj > (); 441data_ser < float > ::print_type_name (value_z ); 442value_z .key ("Data" ); 443data_ser < float > ::dump (ss ,value .z (),queue ); 444 } 445 } 446static void print_type_name (json_stringstream::json_obj & obj ){ 447obj .key ("Type" ); 448obj .value ("sycl::float3" ); 449 } 450}; 451#endif 452 453template <> class data_ser < char *> { 454public : 455static void dump (json_stringstream & ss ,const char * value , 456queue_t queue ) { 457auto obj = ss .object (); 458obj .key ("Data" ); 459const char * dump_addr = value ; 460bool is_dev = is_dev_ptr ((void * )value ); 461if (is_dev ) { 462const char * h_data = new char [strlen (value )]; 463#ifdef __NVCC__ 464cudaMemcpyAsync ((void * )h_data , (void * )value , 465strlen (value )* sizeof (char ),cudaMemcpyDeviceToHost , 466queue ); 467cudaStreamSynchronize (queue ); 468#else 469queue -> memcpy ((void * )h_data , (void * )value ,strlen (value )* sizeof (char )) 470 .wait (); 471#endif 472dump_addr = h_data ; 473 } 474obj .value (std::string (dump_addr )); 475 } 476static void print_type_name (json_stringstream::json_obj & obj ){ 477obj .key ("Type" ); 478obj .value ("char *" ); 479 } 480}; 481 482template <> class data_ser < std::string > { 483public : 484static void dump (json_stringstream & ss ,const std::string & value , 485queue_t queue ) { 486auto obj = ss .object (); 487obj .key ("Data" ); 488obj .value (value ); 489 } 490static void print_type_name (json_stringstream::json_obj & obj ){ 491obj .key ("Type" ); 492obj .value ("std::string" ); 493 } 494}; 495 496}// namespace detail 497}// namespace codepin 498}// namespace experimental 499}// namespace dpct 500 501#endif