00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 ini={}
00012 cm_kernel=None
00013
00014
00015 import copy
00016 import time
00017 import json
00018 import random
00019 import math
00020
00021
00022 def init(i):
00023 return {'cm_return':0}
00024
00025
00026 def scenario(i):
00027 """
00028 Extract program MILEPOST static properties (features), save to repository
00029
00030 Input: {
00031 caller_module_uid - caller module UID
00032 state - pipeline state
00033 exploration_scenario - exploraiton scenario parameters
00034 summary_point - summary dimensions describing aggregated point
00035 aggregated_entry - all aggregated points
00036 space - if aggregated, current space
00037 original_input - to get other additional forms per scenario
00038 cm_data_desc - data description (from the original form)
00039 cm_form_explore - form explore (form the original form)
00040 number_of_iterations - max number of iterations
00041 }
00042
00043 Output: {
00044 cm_return - if =0, success
00045 }
00046 """
00047
00048
00049 es=i.get('exploration_scenario',{})
00050 sr=es.get('save_results_in_repository','')
00051 aggr=es.get('aggregate_in_one_entry','')
00052 wrepo=es.get('work_repo_uoa','')
00053 lwrepo=es.get('work_repo_uoa','')
00054 pmd=i.get('pipeline_module_desc',{})
00055
00056 desc=i.get('cm_data_desc',{})
00057 desc_copy=copy.deepcopy(desc)
00058
00059
00060 state_orig=i.get('state',{})
00061 state=copy.deepcopy(state_orig)
00062
00063 ae=i.get('aggregated_entry',{})
00064
00065 space=i.get('space',{})
00066
00067
00068 ioi=i.get('original_input',{})
00069
00070 frm1='#form_'+ini['cm_module_uid']+'_1'
00071 frm2='#form_'+ini['cm_module_uid']+'_2'
00072
00073 a1={}
00074 r=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00075 'cm_action':'detect_form_params',
00076 'cm_array':ioi,
00077 'cm_prefix':frm1})
00078 if r['cm_return']>0: return r
00079 cm_form_array1=r['cm_array']
00080
00081 r=cm_kernel.restore_flattened_array({'cm_array':cm_form_array1,
00082 'cm_replace_in_keys':{'^35^':'#', '^64^':'@'}})
00083 if r['cm_return']>0: return r
00084 a1=r['cm_array']
00085
00086 ags=a1.get('aggregate_statistics','')
00087 mgbf=a1.get('milepost_gcc_base_flag','')
00088 mgaf=a1.get('milepost_gcc_additional_flags','')
00089
00090
00091 mu=i['caller_module_uid']
00092 r=cm_kernel.get_data_description({'cm_module_uoa':mu})
00093 if r['cm_return']>0: return r
00094 pu=r['cm_data_obj']['cfg']['cm_p_uids']
00095
00096
00097 cm_explore_state=[]
00098
00099 cm_kernel.print_for_con('')
00100 cm_kernel.print_for_con('Preparing choices (fine-grain point in multi-dimensional space):')
00101
00102
00103 hot_funcs=[]
00104
00105 prog_uoa=state[pu['init']]['input']['program_uoa']
00106 rx=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['code.source'],
00107 'cm_data_uoa':prog_uoa,
00108 'cm_action':'load'})
00109 if rx['cm_return']>0: return rx
00110 prog_uid=rx['cm_uid']
00111
00112 dprog=rx['cm_data_obj']['cfg']
00113
00114 rcmd=dprog.get('run_cmds',{})
00115 for q in rcmd:
00116 q1=rcmd[q].get('hot_functions',[])
00117 for q2 in q1:
00118 func=q2.get('name','')
00119 if func!='':
00120 hot_funcs.append(func)
00121
00122 if len(hot_funcs)>0:
00123 state[pu['use_ctuning_milepost']]['input']['hot_functions']=hot_funcs
00124
00125
00126 state[pu['run_program']]['enable']='no'
00127 state[pu['use_ctuning_milepost']]['enable']='yes'
00128 state[pu['use_ctuning_milepost']]['input']['mode']='extract_program_static_properties'
00129
00130 milepost_compiler_code_uoa=state[pu['use_ctuning_milepost']]['input'].get('milepost_compiler_code_uoa','')
00131 state[pu['init']]['input']['compiler_code_uoa']=milepost_compiler_code_uoa
00132
00133 kp='##'+pu['prepare_compiler_flags']+'#input#all_compiler_flags#'
00134
00135 state[pu['build_program']]['enable']='yes'
00136 del(state[pu['prepare_compiler_flags']]['input']['all_compiler_flags'])
00137 state[pu['prepare_compiler_flags']]['input']['all_compiler_flags']={}
00138 state[pu['prepare_compiler_flags']]['input']['all_compiler_flags']['base_flag']=mgbf
00139 state[pu['build_program']]['input']['compiler_flags']=mgaf
00140
00141 jj={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-choice'],
00142 'cm_action':'prepare_choices',
00143 'cm_array':state,
00144 'cm_data_desc':desc,
00145 'cm_explore':i.get('cm_form_explore',{}),
00146 'cm_explore_state':cm_explore_state,
00147 'cm_explore_level':'1'}
00148 r=cm_kernel.access(jj)
00149 if r['cm_return']>0: return r
00150
00151 cm_explore_state=r['cm_explore_state']
00152
00153
00154 del(state[pu['prepare_compiler_flags']]['input']['all_compiler_flags'])
00155 state[pu['prepare_compiler_flags']]['input']['all_compiler_flags']={}
00156 state[pu['prepare_compiler_flags']]['input']['all_compiler_flags']['base_flag']=mgbf
00157
00158
00159 ii={}
00160 ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['ctuning.pipeline.build_and_run_program']
00161 ii['cm_action']='run_stat'
00162 ii['state']=state
00163 ii_copy=copy.deepcopy(ii)
00164
00165 ii['desc']=desc
00166 ii['additional_params']=a1
00167 r=cm_kernel.access(ii)
00168 if r['cm_return']>0: return r
00169
00170
00171 if ags=='yes' and len(space.get('points',[]))>0:
00172 space['points'][0]['pipeline_input']=ii_copy
00173 space['points'][0]['pipeline_output']=r
00174 else:
00175 space['points'].append({'pipeline_input': ii_copy,
00176 'pipeline_output': r})
00177
00178
00179 if sr=='yes':
00180 cm_kernel.print_for_con('')
00181 cm_kernel.print_for_con('Recording point in repository ...')
00182
00183 jj={'cm_run_module_uoa':ini['cm_module_uid'],
00184 'cm_action':'add',
00185 'cm_array':space}
00186
00187 if aggr=='yes' and len(ae)>0:
00188 cm_kernel.print_for_con('')
00189 cm_kernel.print_for_con('Recording point in aggregated entry '+ae['cm_data_uoa']+' ...')
00190 jj['cm_action']='update'
00191 jj.update(ae)
00192 else:
00193
00194 jj['cm_data_uid']=prog_uid
00195
00196 jj['cm_repo_uoa']=wrepo
00197
00198 r=cm_kernel.access(jj)
00199 if r['cm_return']>0: return r
00200
00201 return {'cm_return':0}
00202
00203
00204 def prepare_input_params(i):
00205 """
00206 Prepare input parameters for this scenario
00207
00208 Input: {
00209 cm_form_array - array with forms
00210 form_exists - yes or no
00211 cm_array1 - array from form 1 (coarse-grain points)
00212 cm_array2 - array from form 2 (additional parameters) just in case
00213 caller_cfg - caller module cfg
00214 }
00215
00216 Output: {
00217 cm_return - if =0, success
00218 }
00219 """
00220
00221
00222 af=i.get('cm_form_array',{})
00223 frm1='#form_'+ini['cm_module_uid']+'_1'
00224 frm2='#form_'+ini['cm_module_uid']+'_2'
00225 form_exists=i.get('form_exists','')
00226 xa1=i.get('cm_array1',{})
00227 xa2=i.get('cm_array2',{})
00228 cc=i.get('caller_cfg',{})
00229
00230
00231 a1={}
00232 r=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00233 'cm_action':'detect_form_params',
00234 'cm_array':af,
00235 'cm_prefix':frm1})
00236 if r['cm_return']>0: return r
00237 cm_form_array1=r['cm_array']
00238 cm_form_commands1=r['cm_commands']
00239 cm_form_explore1=r['cm_explore']
00240 cm_data_desc1=ini['cfg']['scenario_desc']
00241
00242
00243 a2={}
00244 r=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00245 'cm_action':'detect_form_params',
00246 'cm_array':af,
00247 'cm_prefix':frm2})
00248 if r['cm_return']>0: return r
00249 cm_form_array2=r['cm_array']
00250 cm_form_commands2=r['cm_commands']
00251 cm_form_explore2=r['cm_explore']
00252
00253 if form_exists=='yes':
00254 r=cm_kernel.restore_flattened_array({'cm_array':cm_form_array1,
00255 'cm_replace_in_keys':{'^35^':'#', '^64^':'@'}})
00256 if r['cm_return']>0: return r
00257 a1=r['cm_array']
00258
00259 r=cm_kernel.restore_flattened_array({'cm_array':cm_form_array2,
00260 'cm_replace_in_keys':{'^35^':'#', '^64^':'@'}})
00261 if r['cm_return']>0: return r
00262 a2=r['cm_array']
00263
00264 ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00265 'cm_action':'visualize_data',
00266 'cm_array':a1,
00267 'cm_data_desc':cm_data_desc1,
00268 'cm_form_commands':cm_form_commands1,
00269 'cm_form_explore':cm_form_explore1,
00270 'cm_separator':'#',
00271 'cm_separator_form':frm1+'#',
00272 'cm_forms_exists':form_exists,
00273 'cm_support_raw_edit':'no',
00274 'cm_mode':'add',
00275 'hide_add_new_object':'yes',
00276 'cm_explore':'yes'}
00277 r=cm_kernel.access(ii)
00278 if r['cm_return']>0: return r
00279 cm_kernel.print_for_web(r['cm_string'])
00280
00281 return {'cm_return':0}
00282
00283
00284 def web_view_header(i):
00285 """
00286 Prepare header
00287
00288 Input: {
00289 header_desc - header description
00290 }
00291
00292 Output: {
00293 cm_return - if =0, success
00294 cm_html - header html
00295 cm_length - header length
00296 }
00297 """
00298
00299
00300 if 'cfg' in cm_kernel.ini['web_style']: web=cm_kernel.ini['web_style']['cfg']
00301 else:
00302 return {'cm_return':1, 'cm_error':'web style is not defined'}
00303
00304 local_pipeline_desc=ini['cfg'].get('pipeline_point_viewer_desc',{})
00305
00306 x1=''
00307 if 'table_bgcolor_line5' in web: x1=' bgcolor="'+web['table_bgcolor_line5']+'" '
00308 x2=''
00309 if 'table_bgcolor_line6' in web: x2=' bgcolor="'+web['table_bgcolor_line6']+'" '
00310
00311 vsx=ini['cfg']['pipeline_point_viewer']
00312 vs=vsx.get('main',[])
00313 ks=vsx.get('key_prefix','')
00314
00315 desc=i.get('header_desc',{})
00316
00317 h=''
00318
00319 col=True;
00320
00321 for kx in vs:
00322 if kx=='': break
00323
00324 k=ks+kx
00325 ky='#'+kx
00326 v=desc.get(ky,{}).get('desc_text','')
00327 if v=='': v=local_pipeline_desc.get(ky,{}).get('desc_text','')
00328
00329 if col: col=False ; x=x1
00330 else: col=True ; x=x2
00331
00332 h+='<td align="center"'+x+'><small><b>'+v+':</b></small></td>'
00333
00334 if col: col=False ; x=x1
00335 else: col=True ; x=x2
00336 h+='<td align="center"'+x+'><small><b>Function:</b></small></td>'
00337
00338 if col: col=False ; x=x1
00339 else: col=True ; x=x2
00340 h+='<td align="center"'+x+'><small><b>Extracted after this compiler pass:</b></small></td>'
00341
00342
00343 ff=ini['cfg']['milepost_features_description']
00344 for qx in ff:
00345 kx=qx.keys()[0]
00346 v=qx[kx]
00347
00348 vd=v.get('desc','')
00349 vn=v.get('normalized','')
00350
00351 if col: col=False ; x=x1
00352 else: col=True ; x=x2
00353
00354 h+='<td align="center"'+x+'><small><b>Program feature '+kx+':<br><br><i>'+vd+'</i>:</b></small></td>'
00355
00356 return {'cm_return':0, 'cm_html':h, 'cm_length':str(len(vs))}
00357
00358
00359 def web_view_data(i):
00360 """
00361 Prepare header
00362
00363 Input: {
00364 cm_data - data object from pipeline
00365 header_desc - header description
00366 cur_line_style - {
00367 cur - cur (style1 or style2)
00368 style1
00369 style2
00370 }
00371 point - point to visualize
00372 }
00373
00374 Output: {
00375 cm_return - if =0, success
00376 cm_html - header html
00377 }
00378 """
00379
00380
00381 if 'cfg' in cm_kernel.ini['web_style']: web=cm_kernel.ini['web_style']['cfg']
00382 else:
00383 return {'cm_return':1, 'cm_error':'web style is not defined'}
00384
00385 desc=i.get('header_desc',{})
00386
00387
00388 cls=i.get('cur_line_style',{})
00389 y=cls.get('cur','')
00390 y1=cls.get('style1','')
00391 y2=cls.get('style2','')
00392
00393 d=i.get('cm_data',{})
00394 d1=d.get('cm_data_obj',{}).get('cfg',{})
00395 points=d1.get('points',[])
00396
00397
00398 x1=''
00399 if 'table_bgcolor_line3' in web: x1=' bgcolor="'+web['table_bgcolor_line3']+'" '
00400 x2=''
00401 if 'table_bgcolor_line4' in web: x2=' bgcolor="'+web['table_bgcolor_line4']+'" '
00402 x3=''
00403 if 'table_bgcolor_line5' in web: x3=' bgcolor="'+web['table_bgcolor_line5']+'" '
00404 x4=''
00405 if 'table_bgcolor_line6' in web: x4=' bgcolor="'+web['table_bgcolor_line6']+'" '
00406
00407 vsx=ini['cfg']['pipeline_point_viewer']
00408 vs=vsx.get('main',[])
00409 vsf=vsx.get('fail',[])
00410 vss=vsx.get('strange',[])
00411 vsff=vsx.get('full_fail',[])
00412 vnt=vsx.get('normality_test',[])
00413
00414 ks=vsx.get('key_prefix','')
00415
00416 d=i.get('cm_data',{})
00417 d1=d.get('cm_data_obj',{}).get('cfg',{})
00418 points=d1.get('points',[])
00419
00420
00421 mu=d1.get('scenario_module_uoa','')
00422 r=cm_kernel.get_data_description({'cm_module_uoa':mu})
00423 if r['cm_return']>0: return r
00424 pu=r['cm_data_obj']['cfg']['cm_p_uids']
00425
00426 h=''
00427
00428 col=True;
00429 for kx in vs:
00430
00431 if kx=='': break
00432
00433 k=ks+kx
00434
00435 if col:
00436 col=False
00437 if y==y1: x=x1
00438 else: x=x3
00439 else:
00440 col=True
00441 if y==y1: x=x2
00442 else: x=x4
00443
00444 p=points[int(i.get('point','0'))]
00445 rg=cm_kernel.get_value_by_flattened_key({'cm_array':p, 'cm_key':k})
00446 if rg['cm_return']>0: return rg
00447 v=rg['cm_value']
00448 if v==None: v=''
00449
00450 d1=desc.get('#'+kx,{})
00451 d1['cm_view_as_text']='yes'
00452 ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00453 'cm_action':'convert_field_to_html',
00454 'cm_value':str(v),
00455 'cm_key':'',
00456 'cm_mode':'view',
00457 'cm_data_desc':d1,
00458 'cm_disable_coloring':'yes'}
00459 r1=cm_kernel.access(ii)
00460 if r1['cm_return']>0: return r1
00461 vh=r1['cm_value_html']
00462 vt=r1['cm_value_text']
00463
00464 h+='<td '+x+' align="right" valign="top"><small>'
00465 if kx in vsf:
00466 if v=='' or int(v)!=0:
00467 h+='<span style="color:#9F0000"><B>Fail ('+str(v)+')</B></span>'
00468 else:
00469 h+=str(v)
00470 elif kx in vss:
00471 if v=='' or int(v)!=0:
00472 h+='<span style="color:#9F0000">Strange ('+str(v)+')</span>'
00473 else:
00474 h+=str(v)
00475 elif kx in vsff:
00476 if v=='yes':
00477 h+='<span style="color:#9F0000"><b>Fail</b></span>'
00478 else:
00479 h+='no'
00480 elif kx in vnt:
00481 if v!='' and float(v)<0.05:
00482 h+='<span style="color:#9F0000"><B>'+str(v)+'</B></span>'
00483 else:
00484 h+=str(v)
00485 else:
00486 h+=vh
00487 h+='<BR>\n'
00488 h+='</small></td>'
00489
00490 if col:
00491 col=False
00492 if y==y1: x=x1
00493 else: x=x3
00494 else:
00495 col=True
00496 if y==y1: x=x2
00497 else: x=x4
00498 h+='<td align="center"'+x+'><small><b>'
00499 mf=p.get('pipeline_output',{}).get('state',{}).get(pu['finish_ctuning_milepost'],{}).get('output',{}).get('program_structure',{})
00500 for q in mf:
00501 h+=str(q)+'<br>'
00502 h+='</b></small></td>'
00503
00504 if col:
00505 col=False
00506 if y==y1: x=x1
00507 else: x=x3
00508 else:
00509 col=True
00510 if y==y1: x=x2
00511 else: x=x4
00512 h+='<td align="center"'+x+'><small>'
00513 mf=p.get('pipeline_output',{}).get('state',{}).get(pu['finish_ctuning_milepost'],{}).get('output',{}).get('program_structure',{})
00514 for q in mf:
00515 qq=mf[q].get('program_static_properties',{})
00516 qq1=qq.keys()
00517 if len(qq1)>0:
00518 kx=qq1[0]
00519 v=qq[kx]
00520
00521 h+=str(kx)+'<br>'
00522 h+='</small></td>'
00523
00524 ff=ini['cfg']['milepost_features_description']
00525 for qx in ff:
00526 kx=qx.keys()[0]
00527 v=qx[kx]
00528
00529 if col:
00530 col=False
00531 if y==y1: x=x1
00532 else: x=x3
00533 else:
00534 col=True
00535 if y==y1: x=x2
00536 else: x=x4
00537 h+='<td align="center"'+x+'><small>'
00538 mf=p.get('pipeline_output',{}).get('state',{}).get(pu['finish_ctuning_milepost'],{}).get('output',{}).get('program_structure',{})
00539
00540 for q in mf:
00541 qq=mf[q].get('program_static_properties',{})
00542
00543 v1=''
00544 v2=''
00545
00546 qq1=qq.keys()
00547 if len(qq1)>0:
00548 kx1=qq1[0]
00549 v1=qq[kx1]
00550
00551 zqx=kx[2:]
00552
00553 v2=v1.get(zqx,'')
00554
00555 h+=str(v2)+'<br>'
00556 h+='</small></td>'
00557
00558 return {'cm_return':0, 'cm_html':h}
00559
00560
00561 def calculate_similarity(i):
00562 """
00563 Calculate similarity
00564 FGG: for now simple euclidean distance - it's just a demo of the concept,
00565 since it has many limitations so we need to collaboratively extend it
00566 (using PCA, SVM, etc)
00567
00568 Input: {
00569 program1_milepost_features - MILEPOST features for program one
00570 program2_milepost_features - MILEPOST features for program two
00571 }
00572
00573 Output: {
00574 cm_return - if =0, success
00575 distance - Euclidean distance (can be more than 1)
00576 }
00577 """
00578
00579 prog1=i['program1_milepost_features']
00580 prog2=i['program2_milepost_features']
00581
00582
00583 fd=ini['cfg']['milepost_features_description']
00584
00585 ftn=''
00586 for q in fd:
00587 q1=q.keys()[0]
00588 v=q[q1]
00589
00590 if v.get('normalizator','')=='yes':
00591 ftn=q1[2:]
00592 break
00593
00594
00595 ftnp1=float(prog1[ftn])
00596 ftnp2=float(prog2[ftn])
00597
00598 distance=0.0
00599
00600
00601 for q in fd:
00602 q1=q.keys()[0]
00603 v=q[q1]
00604
00605 q2=q1[2:]
00606
00607 ftp1=float(prog1[q2])
00608 ftp2=float(prog2[q2])
00609
00610 if v.get('use_for_euclidean_distance','')=='yes':
00611 if v.get('normalized','')!='yes':
00612 ftp1/=ftnp1
00613 ftp2/=ftnp2
00614
00615 distance+=(ftp1-ftp2)*(ftp1-ftp2)
00616
00617 distance=math.sqrt(distance)
00618
00619 return {'cm_return':0, 'distance':str(distance)}