AggregateMetric.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.data.experiment.metric;
00002 
00003 import com.graphbuilder.math.Expression;
00004 import com.graphbuilder.math.ExpressionParseException;
00005 import com.graphbuilder.math.ExpressionTree;
00006 
00007 import edu.rice.cs.hpc.data.experiment.BaseExperimentWithMetrics;
00008 import edu.rice.cs.hpc.data.experiment.Experiment;
00009 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00010 import com.graphbuilder.math.FuncMap;
00011 
00012 
00013 /***
00014  * Class for handling derived-incremental type metric
00015  * This metric is generated by hpcprof-mpi which contains two formula to compute the value:
00016  *  combine formula: used for computing temporary value during caller and flat view creation
00017  *  finalize formula: used to finalizing value independent of the type of the view
00018  *  
00019  * @author laksonoadhianto
00020  *
00021  */
00022 public class AggregateMetric extends BaseMetric {
00023  
00024     static final public char FORMULA_COMBINE = 'c';
00025     static final public char FORMULA_FINALIZE = 'f';
00026     
00027     // formula expression
00028     private Expression formulaCombine, formulaFinalize;
00029     //private Expression formulaCurrent = null;
00030     
00031     // map function
00032     private FuncMap fctMap;
00033     // map variable 
00034     private MetricVarMap finalizeVarMap;
00035     private CombineAggregateMetricVarMap combineVarMap;
00036 
00037     
00041     public AggregateMetric(String sID, String sDisplayName, boolean displayed, String format,
00042             AnnotationType annotationType, int index, int partner, MetricType type) {
00043 
00044         super( sID, sDisplayName, displayed, format, annotationType, index, partner, type);
00045         
00046         this.fctMap = new FuncMap();
00047         this.fctMap.loadDefaultFunctions();
00048         
00049         // set up the variables
00050         this.finalizeVarMap = new MetricVarMap();
00051         this.combineVarMap = new CombineAggregateMetricVarMap();
00052     }
00053 
00054 
00055     /****
00056      * set the math expression
00057      * @param type
00058      * @param sFormula
00059      *******/
00060     public void setFormula(char type, String sFormula) {
00061         assert (type == FORMULA_COMBINE || type == FORMULA_FINALIZE);
00062         
00063         try {
00064             if (type == FORMULA_COMBINE) {
00065                 formulaCombine = ExpressionTree.parse(sFormula);                
00066             } else {
00067                 formulaFinalize = ExpressionTree.parse(sFormula);
00068             }
00069         } catch (ExpressionParseException e) {
00070             e.printStackTrace();
00071         }
00072     }
00073     
00074     
00075     /*********
00076      * initialize the metric.
00077      * THIS METHOD HAS TO BE CALLED before asking the value
00078      * @param type
00079      * @param exp
00080      *******/
00081     public void init(BaseExperimentWithMetrics exp) {
00082         this.finalizeVarMap.setExperiment((Experiment)exp);
00083         this.combineVarMap.setExperiment((Experiment)exp);
00084     }
00085     
00086     
00087     /********
00088      * Assign the value of a scope based on the formula of a given type
00089      * @param type
00090      * @param scope
00091      *******/
00092     public void finalize(Scope scope) {
00093         Expression exp = this.formulaFinalize;
00094         
00095         if (exp != null) {
00096             this.finalizeVarMap.setScope(scope);
00097             this.setScopeValue(exp, this.finalizeVarMap, scope);
00098         }
00099     }
00100     
00101     /******
00102      * combining the metric from another view (typically cct) to this view
00103      * if the target metric is not available (or empty) then we initialize it with
00104      *  the value of the source
00105      * @param s_source
00106      * @param s_target
00107      ******/
00108     public void combine(Scope s_source, Scope s_target) {
00109         MetricValue value = s_target.getMetricValue(this); 
00110         if (MetricValue.isAvailable(value)) {
00111             //--------------------------------------------------------------------------
00112             // the target has the metric. we need to "combine" it with the source
00113             //--------------------------------------------------------------------------
00114             Expression expression = this.formulaCombine;
00115             if (expression != null) {
00116                 this.combineVarMap.setScopes(s_source, s_target);
00117                 this.setScopeValue(expression, this.combineVarMap, s_target);
00118             }
00119         } else {
00120             //--------------------------------------------------------------------------
00121             // the target doesn't have the metric. we need to copy from the source
00122             //--------------------------------------------------------------------------
00123             MetricValue v_source = s_source.getMetricValue(this);
00124             s_target.setMetricValue(index, v_source);
00125         }
00126     }
00127     
00128     
00129     
00130     /******
00131      * 
00132      * @param expression
00133      * @param var_map
00134      * @param scope
00135      ******/
00136     private void setScopeValue(Expression expression, MetricVarMap var_map, Scope scope) {
00137         MetricValue mv;
00138         try {
00139             double dValue = expression.eval(var_map, this.fctMap);
00140             mv = new MetricValue(dValue);
00141         } catch(java.lang.Exception e) {
00142             mv = MetricValue.NONE;
00143             e.printStackTrace();
00144         }
00145         scope.setMetricValue(this.index, mv);
00146     }
00147 
00148 
00149     @Override
00150     /*
00151      * (non-Javadoc)
00152      * @see edu.rice.cs.hpc.data.experiment.metric.BaseMetric#getValue(edu.rice.cs.hpc.data.experiment.scope.Scope)
00153      */
00154     public MetricValue getValue(Scope s) {
00155         MetricValue mv = s.getMetricValue(this.index);
00156         return mv;
00157     }
00158 
00159     @Override
00160     /*
00161      * (non-Javadoc)
00162      * @see edu.rice.cs.hpc.data.experiment.metric.BaseMetric#duplicate()
00163      */
00164     public BaseMetric duplicate() {
00165         return new AggregateMetric(shortName, displayName, displayed, 
00166                 null, annotationType, index, partner_index, metricType);
00167     }
00168 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1