# # Collective Knowledge (demonstrating MILEPOST technology powered by Collective Knowledge Framework) # # See CK LICENSE.txt for licensing details # See CK COPYRIGHT.txt for copyright details # # Developer: Grigori Fursin, Grigori.Fursin@cTuning.org, http://fursin.net # cfg={} # Will be updated by CK (meta description of this module) work={} # Will be updated by CK (temporal data) ck=None # Will be updated by CK (initialized CK kernel) # Local settings hextra='
Program source code | \n' x='' # Get default program (to compare or reset) r=ck.access({'action':'load', 'module_uoa':cfg['module_deps']['program'], 'data_uoa':default_prog_uoa}) if r['return']>0: return r dprog=r['dict'] p=r['path'] prog_path=r['path'] default_prog_uid=r['data_uid'] fn=os.path.join(p,prog_kernel_c) r=ck.load_text_file({'text_file':fn}) if r['return']>0: return r default_prog=r['string'].replace('\\r','').strip() prog=i.get('program_sources','').replace('\\r','').strip() # Strange size, so save file and load it again rx=ck.gen_tmp_file({'prefix':'tmp-', 'suffix':'.tmp'}) if rx['return']>0: return rx ftmp=rx['file_name'] r=ck.save_text_file({'text_file':ftmp, 'string':prog}) if r['return']>0: return r r=ck.load_text_file({'text_file':ftmp, 'delete_after_read':'yes'}) if r['return']>0: return r prog=r['string'] if prog=='' or ar: prog=default_prog y='width:600px;height:270px;' x='\n' h+=''+x+' | \n' h+='
Program language | \n' x='' dt=[{'name':'C', 'value':'CC'}, {'name':'C++','value':'CXX'}, {'name':'Fortran', 'value':'FC'}, ] plang=i.get('program_language','') if plang=='' or ar: plang='CC' ii={'action':'create_selector', 'module_uoa':cfg['module_deps']['wfe'], 'data':dt, 'name':'program_language', 'onchange':onchange, 'skip_sort':'yes', 'selected_value':plang} r=ck.access(ii) if r['return']>0: return r x=r['html'] h+=''+x+' | \n' h+='
MILEPOST GCC flags | \n' x='' flags=i.get('milepost_flags','') if flags=='' or ar: flags='-O1' flags=safe_str(flags) x='\n' h+=''+x+' | \n' h+='
MILEPOST GCC pass to extract features | \n' x='' mpass=i.get('milepost_pass','') if mpass=='' or ar: mpass='fre' mpass=safe_str(mpass) x='\n' h+=''+x+' | \n' h+='
WARNING: features were not extracted - file with features is not found!\n' else: r=ck.load_text_file({'text_file':f1}) if r['return']>0: return r s1=r['string'].strip() r=ck.load_text_file({'text_file':f2}) if r['return']>0: return r s2=r['string'].strip() h+='
'+s2+'
'
# Parse features
fx=s1.split(',')
for q in fx:
qq=q.strip().split('=')
if len(qq)==2:
k=qq[0][2:]
v=float(qq[1])
features['ft'+str(k)]=v
h+='
Feature | Description |
'+ft+' ('+desc+') |
'+r.get('html','')
st+=r.get('style','')
else:
h+='
\n'
if sh=='yes':
h=''
st=''
return {'return':0, 'html':h, 'style':st, 'predicted_opt':popt}
##############################################################################
# make safe strings for CMD ...
def safe_str(s):
s=s.replace(';',' ').replace('&',' ').replace('>',' ').replace('<',' ')
return s
##############################################################################
# open dashboard
def dashboard(i):
"""
Input: {
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
i['action']='browser'
i['cid']=''
i['module_uoa']=''
i['extra_url']='native_action=show&native_module_uoa=milepost'
return ck.access(i)
##############################################################################
# access MILEPOST AI via unified CK JSON API
def ask_ai(i):
"""
Input: {
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
return {'return':0}
##############################################################################
# access XSB remotely on devices with constraint memory
def remote_xsb(i):
"""
Input: {
(input) - XSB input file
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
# Setting output
o=i.get('out','')
er=i.get('exchange_repo','')
if er=='': er=ck.cfg['default_exchange_repo_uoa']
esr=i.get('exchange_subrepo','')
if esr=='': esr=ck.cfg['default_exchange_subrepo_uoa']
if i.get('local','')=='yes':
er='local'
esr=''
# Load featlstn.P
r=ck.load_text_file({'text_file':file_features})
if r['return']>0: return r
fx=r['string']
r=ck.access({'action':'remote_xsb_api',
'repo_uoa':er,
'remote_repo_uoa':esr,
'module_uoa':work['self_module_uid'],
'input':fx})
if r['return']>0: return r
# Save output
ft=r['output']
r=ck.save_text_file({'text_file':file_features_out, 'string':ft})
if r['return']>0: return r
return {'return':0}
##############################################################################
# XSB remote CK API
def remote_xsb_api(i):
"""
Input: {
input - input source
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
output - output features
}
"""
import os
import tempfile
import shutil
import time
fi=i['input']
fo='features.FT'
cur_dir=os.getcwd()
# Go to tmp dir
tdx=tempfile.gettempdir()
rx=ck.gen_tmp_file({'prefix':'tmp-ck-xsb-'})
if rx['return']>0: return rx
ftmp=rx['file_name']
td=os.path.join(tdx,ftmp)
if not os.path.isdir(td):
os.makedirs(td)
os.chdir(td)
# Save file
r=ck.save_text_file({'text_file':file_features, 'string':fi})
if r['return']>0: return r
# Resolve deps on ctuning-cc-plugins
r=ck.access({'action':'set',
'module_uoa':cfg['module_deps']['env'],
'tags':'plugin,milepost,ctuning'})
if r['return']>0: return r
bat=r['bat']
prog=r['dict']['customize']['full_path']
ss=bat+'\n'+prog
os.system(ss)
r=ck.load_text_file({'text_file':file_features_out, 'delete_after_read':'yes'})
if r['return']>0: return r
ft=r['string']
if os.path.isdir(td):
shutil.rmtree(td)
time.sleep(1)
return {'return':0, 'output':ft}
##############################################################################
# CK AI JSON API for web (needed to automatically find such function from higher-level CK AI API)
def ask_ai_web(i):
"""
Input: {
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
return show(i)
##############################################################################
# return json instead of html in show (needed for CK AI API)
def show_json(i):
"""
Input: {
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
r=show(i)
if 'html' in r: del(r['html'])
if 'style' in r: del(r['style'])
return r