|  | 
| 20 | 20 | import sys | 
| 21 | 21 | import timeit | 
| 22 | 22 | import re | 
|  | 23 | +import platform | 
|  | 24 | +import hashlib | 
|  | 25 | +import os | 
|  | 26 | +import subprocess | 
| 23 | 27 | 
 | 
| 24 | 28 | import numpy as np | 
| 25 | 29 | import sklearn | 
| 26 | 30 | 
 | 
|  | 31 | +def check_for_linux(): | 
|  | 32 | +    return platform.system() == 'Linux' | 
|  | 33 | + | 
|  | 34 | +def detect_stage(func): | 
|  | 35 | +    func = str(func) | 
|  | 36 | +    #print('##', func) | 
|  | 37 | +    if '.fit' in func or 'train_test_split' in func: | 
|  | 38 | +        return 'fit' | 
|  | 39 | +    elif '.predict' in func: | 
|  | 40 | +        return 'predict' | 
|  | 41 | +    elif '.transform' in func: | 
|  | 42 | +        return 'transform' | 
|  | 43 | +    else: | 
|  | 44 | +        print('*' * 100) | 
|  | 45 | +        raise Exception(f'Unknown stage in detect_stage(). String: {func}') | 
|  | 46 | + | 
|  | 47 | +def prepare_output_dir(func, params): | 
|  | 48 | +    outdir = os.path.join('analysis_'+params.timestamp, 'case_'+params.hash) | 
|  | 49 | +    os.makedirs(outdir, exist_ok=True) | 
|  | 50 | +    return outdir | 
|  | 51 | + | 
|  | 52 | +def start_ittpy_domain(func): | 
|  | 53 | +    import itt | 
|  | 54 | +    str_name = str(func) | 
|  | 55 | +    domain = itt.domain_create(str_name) | 
|  | 56 | +    itt.task_begin(domain, str_name) | 
|  | 57 | +    return domain | 
|  | 58 | +     | 
|  | 59 | +def finish_ittpy_domain(domain): | 
|  | 60 | +    import itt | 
|  | 61 | +    itt.task_end(domain) | 
|  | 62 | + | 
|  | 63 | +def start_memory_analysis(outdir): | 
|  | 64 | +    current_proc_id = os.getpid() | 
|  | 65 | +    result_filename = 'psrecord_result' | 
|  | 66 | +    result_filename_log = os.join(outdir, result_filename+'.txt') | 
|  | 67 | +    result_filename_image = os.join(outdir, result_filename+'.png') | 
|  | 68 | +    command = f'psrecord {current_proc_id} --log {result_filename_log} --plot {result_filename_image}' | 
|  | 69 | +    subprocess.Popen(command.split(' ')) | 
|  | 70 | +    # return res.stdout[:-1], res.stderr[:-1] | 
|  | 71 | + | 
|  | 72 | +def stop_memory_analysis(outdir): | 
|  | 73 | +    pass | 
|  | 74 | + | 
|  | 75 | +def start_emon(outdir): | 
|  | 76 | +    pass | 
|  | 77 | + | 
|  | 78 | +def stop_emon(outdir): | 
|  | 79 | +     | 
|  | 80 | + | 
|  | 81 | +def prerun(func, params): | 
|  | 82 | +    if not check_for_linux(): | 
|  | 83 | +        return | 
|  | 84 | +    prerun_result = {} | 
|  | 85 | +    if params.emon == 'True' or params.memory_analysis == 'True': | 
|  | 86 | +        outdir = prepare_output_dir(func, params) | 
|  | 87 | +        prerun_result['outdir'] = outdir | 
|  | 88 | +    if params.ittpy == 'True': | 
|  | 89 | +        domain = start_ittpy_domain() | 
|  | 90 | +        prerun_result['domain'] = domain | 
|  | 91 | +    if params.memory_analysis == 'True': | 
|  | 92 | +        start_memory_analysis(outdir) | 
|  | 93 | +    if params.emon == 'True': | 
|  | 94 | +        start_emon(outdir) | 
|  | 95 | +    return outdir | 
|  | 96 | + | 
|  | 97 | +def postrun(func, params, outdir): | 
|  | 98 | +    if not check_for_linux(): | 
|  | 99 | +        return | 
|  | 100 | +    if params.ittpy == 'True': | 
|  | 101 | +        finish_ittpy_domain() | 
|  | 102 | +    if params.memory_analysis == 'True': | 
|  | 103 | +        stop_memory_analysis(outdir) | 
|  | 104 | +    if params.emon == 'True': | 
|  | 105 | +        stop_emon(outdir) | 
| 27 | 106 | 
 | 
| 28 | 107 | def get_dtype(data): | 
| 29 | 108 |     ''' | 
| @@ -186,6 +265,13 @@ def parse_args(parser, size=None, loop_types=(), | 
| 186 | 265 |     parser.add_argument('--device', default='none', type=str, | 
| 187 | 266 |                         choices=('host', 'cpu', 'gpu', 'none'), | 
| 188 | 267 |                         help='Execution context device') | 
|  | 268 | +    parser.add_argument('--emon', default='False', type=str, | 
|  | 269 | +                        choices=('True', 'False'), | 
|  | 270 | +                        help='Should emon profiling be started') | 
|  | 271 | +    parser.add_argument('--hash', default='0', type=str, | 
|  | 272 | +                        help='Hash of current case') | 
|  | 273 | +    parser.add_argument('--timestamp', default='0', type=str, | 
|  | 274 | +                        help='Timestamp of start benchmarking') | 
| 189 | 275 | 
 | 
| 190 | 276 |     for data in ['X', 'y']: | 
| 191 | 277 |         for stage in ['train', 'test']: | 
| @@ -271,16 +357,26 @@ def prepare_daal_threads(num_threads=-1): | 
| 271 | 357 | 
 | 
| 272 | 358 |     return num_threads | 
| 273 | 359 | 
 | 
| 274 |  | - | 
| 275 | 360 | def measure_function_time(func, *args, params, **kwargs): | 
| 276 |  | -    return time_box_filter(func, *args, | 
|  | 361 | +    # print('*************') | 
|  | 362 | +    # print(str(func)) | 
|  | 363 | +    # print(args) | 
|  | 364 | +    # print(params) | 
|  | 365 | +    # print(kwargs) | 
|  | 366 | +    # print('*************') | 
|  | 367 | +     | 
|  | 368 | +    #meta = detect_meta() | 
|  | 369 | +    outdir = prerun(func, params) | 
|  | 370 | +    results = time_box_filter(func, *args, | 
| 277 | 371 |                            n_meas=params.box_filter_measurements, | 
| 278 | 372 |                            time_limit=params.time_limit, **kwargs) | 
|  | 373 | +    postrun(func, params, outdir) | 
|  | 374 | +    return results | 
| 279 | 375 | 
 | 
| 280 | 376 | 
 | 
| 281 | 377 | def time_box_filter(func, *args, n_meas, time_limit, **kwargs): | 
| 282 | 378 |     times = [] | 
| 283 |  | -    while len(times) < n_meas: | 
|  | 379 | +    while len(times) < 1: | 
| 284 | 380 |         t0 = timeit.default_timer() | 
| 285 | 381 |         val = func(*args, **kwargs) | 
| 286 | 382 |         t1 = timeit.default_timer() | 
|  | 
0 commit comments