Welcome    Usage    Browse    Find CID    Search     Log in

cM API Documentation

module.py

Go to the documentation of this file.
00001 #
00002 # Collective Mind
00003 #
00004 # See cM LICENSE.txt for licensing details.
00005 # See cM Copyright.txt for copyright details.
00006 #
00007 # Developer(s): (C) Grigori Fursin, started on 2011.09
00008 #
00009 
00010 # Should always be here
00011 ini={}
00012 cm_kernel=None
00013 
00014 # Local settings
00015 import json
00016 
00017 # ============================================================================
00018 def init(i):
00019     return {'cm_return':0}
00020 
00021 # ============================================================================
00022 def process(i):
00023     """
00024     Run collective tuning pipeline
00025 
00026     Input:  {
00027               space                 - available points in space
00028               pipeline_input        - pipeline input
00029               pipeline_output       - pipeline output
00030               desc                  - state description
00031               tune_characteristics  - list of characteristics to tune 
00032                                       (priorities, such as global execution time; code size; energy; compilation time, etc -
00033                                        depending on user scenario)
00034               calcluate_improvement_over_ref - if !='', use this as the key in space
00035                                                        to calcluate improvements over first point
00036               max_improvement_threshold - if at least one of the max improvement more than threshold, add
00037 
00038             }
00039 
00040     Output: {
00041               cm_return   - if =0, success
00042               state       - final state
00043               space       - (updated) space
00044               must_add    - if 'yes', should add this point
00045             }
00046     """
00047 
00048     cior=i.get('calcluate_improvement_over_ref','')
00049 
00050     mit=i.get('max_improvement_threshold','')
00051     if mit=='': mit='1.0'
00052     dmit=float(mit)
00053 
00054     space=i.get('space',{})
00055     pipeline_input=i.get('pipeline_input',{})
00056     pipeline_output=i.get('pipeline_output',{})
00057     desc=i.get('desc',{})
00058     tc=i.get('tune_characteristics',[])
00059     tc1=tc
00060     if len(tc)==0: 
00061        for q in desc:
00062            if desc[q].get('characteristic','')=='yes':
00063               tc1.append(q)
00064 
00065     state=pipeline_output.get('state',{})
00066 
00067     cm_kernel.print_for_con('')
00068     cm_kernel.print_for_con('********************* Performing analysis, filtering and pruning of profitable cases ...')
00069 
00070     if 'points' not in space: space['points']=[]
00071     points=space['points']
00072 
00073     # current point
00074     state=pipeline_output.get('state',{})
00075 
00076     cm_kernel.print_for_con('Number of all points: '+str(len(points)))
00077     cm_kernel.print_for_con('')
00078 
00079     # find if improves any given point on one dimension without making it worse on other
00080     cm_kernel.print_for_con('')
00081 
00082     td=[] # to delete
00083 
00084     im_0={}
00085 
00086     add=True
00087     for px1 in range(0, len(points)):
00088         p1=points[px1]
00089         state1=p1.get('pipeline_output',{}).get('state',{})
00090 
00091         cm_kernel.print_for_con('')
00092         cm_kernel.print_for_con('Analyzing solution: '+str(px1)+' of '+str(len(points)-1)+ ' ...')
00093 
00094         found_better=False
00095         found_worse=False
00096         for q in tc1:
00097             v=desc[q]
00098 
00099             cm_kernel.print_for_con('  Processing characteristic '+q+' ...')
00100 
00101             t=v.get('type','')
00102 
00103             rg=cm_kernel.get_value_by_flattened_key({'cm_array':state, 'cm_key':q})
00104             if rg['cm_return']>0: return rg
00105             vv=rg['cm_value']
00106 
00107             if vv!=None and vv!='' and (t=='integer' or t=='float'):
00108 
00109                if t=='integer': dv=int(vv)
00110                elif t=='float': dv=float(vv)
00111 
00112                rg=cm_kernel.get_value_by_flattened_key({'cm_array':state1, 'cm_key':q})
00113                if rg['cm_return']>0: return rg
00114                vv1=rg['cm_value']
00115 
00116                if vv1!=None and vv1!='' and (t=='integer' or t=='float'):
00117 
00118                   if t=='integer':  dv1=int(vv1)
00119                   elif t=='float':  dv1=float(vv1)
00120 
00121                   print '            ',px1, '  ', dv,'  ', dv1
00122 
00123                   if dv<dv1: 
00124                      found_better=True
00125                   elif dv>dv1:
00126                      found_worse=True
00127 
00128                   # If calculating improvement over first point is turned on:
00129                   if cior!='':
00130                      if px1==0:
00131                         im_0[q]=dv1
00132 
00133                      if found_better:
00134                         if cior not in space: 
00135                            space[cior]={}
00136 
00137                         if q not in space[cior] or space[cior].get(q,'')=='':
00138                            space[cior][q]='1.0'
00139 
00140                         cur_im=float(space[cior][q])
00141 
00142                         if im_0[q]!=0 and dv!=0:
00143                            im=im_0[q]/dv
00144                            if im>cur_im: 
00145                               space[cior][q]="%.3f" % im
00146 
00147         if not found_better: add=False
00148 
00149         if not found_worse:
00150            cm_kernel.print_for_con('')
00151            cm_kernel.print_for_con('      new solution improved analyzed one - mark for deletion')
00152            td.append(px1)
00153 
00154     # pruning
00155     if len(td)>0:
00156        cm_kernel.print_for_con('')
00157        cm_kernel.print_for_con(' Pruning the following solutions: '+str(sorted(td)))
00158        for q in sorted(td, reverse=True):
00159            if space['points'][q].get('always_keep_this_point','')!='yes':
00160               del(space['points'][q])
00161 
00162     # Check threshold
00163     if cior!='' and dmit>1 and add:
00164        add=False
00165 
00166        # If at least one dimension is more than a threshold, add
00167        for q in space.get(cior,{}):
00168            if float(space[cior][q])>dmit:
00169               add=True
00170               break
00171 
00172     return {'cm_return':0, 'state':state, 'space':space, 'must_add':add}

Generated on Wed May 28 02:49:01 2014 for Collective Mind Framework by DoxyGen 1.6.1
Concept, design and coordination: Grigori Fursin (C) 1993-2013