okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- codepin.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_HPP__ 9#define __DPCT_CODEPIN_HPP__ 10 11#include "serialization/basic.hpp" 12#include <chrono> 13#include <ctime> 14#include <fstream> 15#include <iomanip> 16#include <map> 17#include <memory> 18#include <numeric> 19#include <sstream> 20#include <stdexcept> 21#include <string> 22#include <type_traits> 23#include <unordered_set> 24#include <utility> 25#include <vector> 26#include <iostream> 27#include <stdlib.h> 28 29// Random seed for data sampling. 30#ifndef CODEPIN_RAND_SEED 31#define CODEPIN_RAND_SEED 0 32#endif 33 34// Data size threshold to trigger data sampling. 35// array/pointer size larger than the threshold will be sampled. 36#ifndef CODEPIN_SAMPLING_THRESHOLD 37#define CODEPIN_SAMPLING_THRESHOLD 20 38#endif 39 40// Sampling percent, interval: [0, 100] 41// 0: No data will be logged. 42// 100: all data will be logged. 43#ifndef CODEPIN_SAMPLING_PERCENT 44#define CODEPIN_SAMPLING_PERCENT 1 45#endif 46 47#define CODEPIN_TO_STR (x ) CODEPIN_STR(x) 48#define CODEPIN_STR (x ) #x 49#pragma message( \ 50 "CodePin data sampling feature is enabled for data dump. As follow list 3 configs for data sampling:") 51#pragma message("CODEPIN_RAND_SEED: " CODEPIN_TO_STR(CODEPIN_RAND_SEED)) 52#pragma message("CODEPIN_SAMPLING_THRESHOLD: " CODEPIN_TO_STR(CODEPIN_SAMPLING_THRESHOLD)) 53#pragma message("CODEPIN_SAMPLING_PERCENT: " CODEPIN_TO_STR(CODEPIN_SAMPLING_PERCENT)) 54#pragma message( \ 55 "Define the macros in the build command to change sampling configs. Also refer to codepin.hpp for definitions and default value of the macros.") 56 57namespace dpct { 58namespace experimental { 59namespace codepin { 60 61inline static std::map < std::string ,int > api_index ; 62inline static std::map < std::string ,event_t > event_map ; 63inline static bool rand_seed_setup = false; 64namespace detail { 65 66inline static std::unordered_set < void *> ptr_unique ; 67 68class logger { 69public : 70logger (const std::string & dump_file ) 71 :opf (dump_file ),json_ss (opf ),arr (json_ss ) { 72auto top_obj = arr .object (); 73top_obj .key ("CodePin Random Seed" ); 74top_obj .value (CODEPIN_RAND_SEED ); 75top_obj .key ("CodePin Sampling Threshold" ); 76top_obj .value (CODEPIN_SAMPLING_THRESHOLD ); 77top_obj .key ("CodePin Sampling Percent" ); 78top_obj .value (CODEPIN_SAMPLING_PERCENT ); 79 } 80 ~logger () {} 81 82 detail::json_stringstream & get_stringstream () { 83return this -> json_ss ; 84 } 85 86template < class ...Args > 87void print_CP (const std::string & cp_id , std::string device_name , 88size_t free_byte ,size_t total_byte ,float elapse_time , 89queue_t queue ,Args ...args ) { 90ptr_unique .clear (); 91auto obj = arr .object (); 92obj .key ("ID" ); 93obj .value (cp_id ); 94obj .key ("Device Name" ); 95obj .value (device_name ); 96obj .key ("Device ID" ); 97#ifdef __NVCC__ 98int device_id ; 99cudaGetDevice (& device_id ); 100obj .value (device_id ); 101#else 102obj .value ((int )dpct::get_current_device_id ()); 103#endif 104obj .key ("Stream Address" ); 105obj .value ((void * )queue ); 106obj .key ("Free Device Memory" ); 107obj .value (free_byte ); 108obj .key ("Total Device Memory" ); 109obj .value (total_byte ); 110obj .key ("Elapse Time(ms)" ); 111obj .value (elapse_time ); 112obj .key ("CheckPoint" ); 113auto cp_obj = 114obj .value < detail::json_stringstream::json_obj > (); 115print_args (cp_obj ,queue ,0 ,args ...); 116 } 117 118void print_args (json_stringstream::json_obj & obj ,queue_t queue , 119int index = 0 ) {} 120 121template < class First ,class ...RestArgs > 122void print_args (json_stringstream::json_obj & obj ,queue_t queue ,int index , 123 std::string_view arg_name ,First & arg ,RestArgs ...args ) { 124obj .key (arg_name ); 125 { 126auto type_obj = 127obj .value < detail::json_stringstream::json_obj > (); 128 detail::data_ser < First > ::print_type_name (type_obj ); 129type_obj .key ("Address" ); 130print_address (type_obj ,arg ); 131type_obj .key ("Index" ); 132type_obj .value (index ); 133type_obj .key ("Data" ); 134 detail::data_ser < First > ::dump (json_ss ,arg ,queue ); 135 } 136print_args (obj ,queue ,index + 1 ,args ...); 137 } 138template < class ArgT > 139void print_address (json_stringstream::json_obj & obj ,ArgT arg ) { 140if constexpr (std::is_pointer < ArgT > ::value ) { 141obj .value ((void * )arg ); 142 }else { 143obj .value ((void * )& arg ); 144 } 145 } 146 147private : 148 std::ofstream opf ; 149 detail::json_stringstream json_ss ; 150 detail::json_stringstream::json_array arr ; 151}; 152 153#ifdef __NVCC__ 154inline std::string data_file_prefix = "CodePin_CUDA_" ; 155#else 156inline std::string data_file_prefix = "CodePin_SYCL_" ; 157#endif 158 159inline std::string get_data_file_name (std::string_view data_file_prefix ) { 160 std::time_t now_time = std::time (nullptr ); 161 std::stringstream strs ; 162strs <<data_file_prefix 163 << std::put_time (std::localtime (& now_time ),"%Y-%m-%d_%H-%M-%S" ) 164 <<".json" ; 165return strs .str (); 166} 167 168inline logger log (get_data_file_name (data_file_prefix )); 169 170inline std::map < void * ,uint32_t > & get_ptr_size_map () { 171static std::map < void * ,uint32_t > ptr_size_map ; 172return ptr_size_map ; 173} 174 175inline uint32_t get_ptr_size_in_bytes (void * ptr ) { 176const std::map < void * ,uint32_t > & ptr_size_map = get_ptr_size_map (); 177const auto & it = ptr_size_map .find (ptr ); 178return (it != ptr_size_map .end ()) ?it -> second :0 ; 179} 180 181template < class T > 182class data_ser < T * ,void > { 183public : 184static void dump (detail::json_stringstream & ss ,T * value , 185queue_t queue ) { 186using PointeeType = std::remove_cv_t < std::remove_pointer_t < T >> ; 187PointeeType * non_const_value = const_cast < PointeeType *> (value ); 188if (ptr_unique .find (non_const_value )!= ptr_unique .end ()) { 189return ; 190 } 191ptr_unique .insert (non_const_value ); 192int size = get_ptr_size_in_bytes (non_const_value ); 193size = size == 0 ?1 :size /sizeof (* value ); 194PointeeType * dump_addr = non_const_value ; 195bool is_dev = is_dev_ptr (non_const_value ); 196if (is_dev ) { 197PointeeType * h_data = new PointeeType [size ]; 198#ifdef __NVCC__ 199cudaMemcpyAsync (h_data ,value ,size * sizeof (PointeeType ), 200cudaMemcpyDeviceToHost ,queue ); 201cudaStreamSynchronize (queue ); 202#else 203queue -> memcpy ((void * )h_data , (void * )value ,size * sizeof (PointeeType )) 204 .wait (); 205#endif 206dump_addr = h_data ; 207 } 208auto arr = ss .array (); 209for (int i = 0 ;i < size ;++ i ) { 210if (size > CODEPIN_SAMPLING_THRESHOLD && i != 0 ) { 211float r = static_cast < float > (rand ()) /static_cast < float > (RAND_MAX ); 212if (r > (float )CODEPIN_SAMPLING_PERCENT /(float )100 ) 213continue ; 214 } 215auto obj = arr .object (); 216 detail::data_ser < PointeeType > ::print_type_name (obj ); 217obj .key ("Data" ); 218 detail::data_ser < PointeeType > ::dump ( 219ss ,* (dump_addr + i ),queue ); 220 } 221if (is_dev ) 222delete []dump_addr ; 223 } 224static void print_type_name ( 225 detail::json_stringstream::json_obj & obj ) { 226obj .key ("Type" ); 227obj .value ("Pointer" ); 228 } 229}; 230 231template < class T > 232class data_ser < T ,typename std::enable_if < std::is_array < T > ::value > ::type > { 233public : 234static void dump (detail::json_stringstream & ss ,T value , 235queue_t queue ) { 236auto arr = ss .array (); 237size_t size = sizeof (T ) /sizeof (value [0 ]); 238for (size_t i = 0 ;i < size ;++ i ) { 239if (size > CODEPIN_SAMPLING_THRESHOLD && i != 0 ) { 240float r = static_cast < float > (rand ()) /static_cast < float > (RAND_MAX ); 241if (r > (float )CODEPIN_SAMPLING_PERCENT /(float )100 ) 242continue ; 243 } 244auto obj = arr .object (); 245 detail::data_ser < 246 std::remove_extent_t < T >> ::print_type_name (obj ); 247obj .key ("Data" ); 248 detail::data_ser < std::remove_extent_t < T >> ::dump ( 249ss ,value [i ],queue ); 250 } 251 } 252static void print_type_name ( 253 detail::json_stringstream::json_obj & obj ) { 254obj .key ("Type" ); 255obj .value ("Array" ); 256 } 257}; 258 259template < class ...Args > 260void gen_log_API_CP (const std::string & cp_id , std::string device_name , 261size_t free_byte ,size_t total_byte ,float elapse_time , 262queue_t queue ,Args ...args ) { 263if (!rand_seed_setup ) { 264srand (CODEPIN_RAND_SEED ); 265rand_seed_setup = true; 266 std::cout <<"CodePin data sampling is enabled for data dump. As follow list 3 " 267"configs for data sampling:" 268 << std::endl ; 269 std::cout <<"CODEPIN_RAND_SEED: " <<CODEPIN_RAND_SEED << std::endl ; 270 std::cout <<"CODEPIN_SAMPLING_THRESHOLD: " <<CODEPIN_SAMPLING_THRESHOLD 271 << std::endl ; 272 std::cout <<"CODEPIN_SAMPLING_PERCENT: " <<CODEPIN_SAMPLING_PERCENT 273 << std::endl ; 274 } 275log .print_CP (cp_id ,device_name ,free_byte ,total_byte ,elapse_time ,queue , 276args ...); 277} 278}// namespace detail 279 280#ifdef __NVCC__ 281inline void synchronize (cudaStream_t stream ) {cudaStreamSynchronize (stream ); } 282#else 283inline void synchronize (sycl::queue * q ) {q -> wait (); } 284#endif 285 286/// Generate API check point prolog. 287/// \param cp_id The UID of the function call. 288/// \param queue The sycl queue to synchronize the command execution. 289/// \param args The var name string and variable value pair list. 290template < class ...Args > 291void gen_prolog_API_CP (const std::string & cp_id , 292queue_t queue ,Args && ...args ) { 293synchronize (queue ); 294 std::string prolog_tag = cp_id + ":" + "prolog" ; 295if (api_index .find (cp_id )== api_index .end ()) { 296api_index [cp_id ]= 0 ; 297 }else { 298api_index [cp_id ]++ ; 299 } 300 std::string event_id = 301cp_id + ":" + std::to_string (api_index [cp_id ]); 302size_t free_byte ,total_byte ; 303#ifdef __NVCC__ 304int device ; 305cudaGetDevice (& device ); 306cudaDeviceProp deviceProp ; 307cudaGetDeviceProperties (& deviceProp ,device ); 308 std::string device_name (deviceProp .name ); 309cudaMemGetInfo (& free_byte ,& total_byte ); 310cudaEvent_t event ; 311cudaEventCreate (& event ); 312cudaEventRecord (event ,queue ); 313event_map [event_id ]= event ; 314#else 315 dpct::get_current_device ().get_memory_info (free_byte ,total_byte ); 316 std::string device_name = dpct::get_current_device ().get_info < sycl::info::device::name > (); 317#ifdef DPCT_PROFILING_ENABLED 318 sycl::event event = queue -> ext_oneapi_submit_barrier (); 319event_map [event_id ]= event ; 320#endif //DPCT_PROFILING_ENABLED 321#endif 322 323 detail::gen_log_API_CP (prolog_tag ,device_name ,free_byte ,total_byte ,0.0f , 324queue ,args ...); 325} 326 327/// Generate API check point epilog. 328/// \param cp_id The UID of the function call. 329/// \param stream The sycl queue to synchronize the command execution. 330/// \param args The var name string and variable value pair list. 331template < class ...Args > 332void gen_epilog_API_CP (const std::string & cp_id , 333queue_t queue ,Args && ...args ) { 334synchronize (queue ); 335 std::string epilog_tag = cp_id + ":" + "epilog" ; 336 std::string event_id = 337cp_id + ":" + std::to_string (api_index [cp_id ]); 338size_t free_byte ,total_byte ; 339float kernel_elapsed_time = 0.0f ; 340#ifdef __NVCC__ 341int device ; 342cudaGetDevice (& device ); 343cudaDeviceProp deviceProp ; 344cudaGetDeviceProperties (& deviceProp ,device ); 345 std::string device_name (deviceProp .name ); 346cudaMemGetInfo (& free_byte ,& total_byte ); 347cudaEvent_t event ; 348cudaEventCreate (& event ); 349cudaEventRecord (event ,queue ); 350auto pre_event = event_map [event_id ]; 351event_map .erase (event_id ); 352cudaEventSynchronize (event ); 353cudaEventElapsedTime (& kernel_elapsed_time ,pre_event ,event ); 354#else 355#ifdef DPCT_PROFILING_ENABLED 356 sycl::event event = queue -> ext_oneapi_submit_barrier (); 357auto pre_event = event_map [event_id ]; 358event_map .erase (event_id ); 359event .wait_and_throw (); 360kernel_elapsed_time = 361 (event .get_profiling_info < sycl::info::event_profiling::command_end > ()- 362pre_event 363 .get_profiling_info < sycl::info::event_profiling::command_start > ()) / 3641000000.0f ; 365#endif //DPCT_PROFILING_ENABLED 366 std::string device_name = dpct::get_current_device ().get_info < sycl::info::device::name > (); 367 dpct::get_current_device ().get_memory_info (free_byte ,total_byte ); 368#endif 369 detail::gen_log_API_CP (epilog_tag ,device_name ,free_byte ,total_byte , 370kernel_elapsed_time ,queue ,args ...); 371} 372 373inline std::map < void * ,uint32_t > & get_ptr_size_map () { 374return detail::get_ptr_size_map (); 375} 376}// namespace codepin 377}// namespace experimental 378}// namespace dpct 379 380namespace dpctexp= dpct::experimental; 381 382 383#endif // End of __DPCT_CODEPIN_HPP__