ExperimentMerger.java

Go to the documentation of this file.
00001 
00002 //                                                                      //
00003 //  ExperimentMerger.java                                               //
00004 //                                                                      //
00005 //  ExperimentMerger -- class to merge two Experiments                  //
00006 //  Created: May 7, 2007                                                //
00007 //                                                                      //
00008 //  (c) Copyright 2007-2012 Rice University. All rights reserved.       //
00009 //                                                                      //
00011 package edu.rice.cs.hpc.data.experiment.merge;
00012 
00013 import java.io.File;
00014 import java.util.*;
00015 
00016 import edu.rice.cs.hpc.data.experiment.Experiment;
00017 import edu.rice.cs.hpc.data.experiment.ExperimentConfiguration;
00018 import edu.rice.cs.hpc.data.experiment.metric.*;
00019 import edu.rice.cs.hpc.data.experiment.scope.*;
00020 import edu.rice.cs.hpc.data.experiment.scope.visitors.*;
00021 import edu.rice.cs.hpc.data.util.Constants;
00022 
00023 /****
00024  * Merging experiments
00025  *
00026  * Steps:
00027  * 
00028  * 1. create the new experiment 
00029  * 2. add metrics       --> add into metric list 
00030  * 3. add raw metrics   --> add into a list
00031  * 4. add trace data    --> add into a list
00032  * 5. merge the experiments
00033  */
00034 public class ExperimentMerger 
00035 {
00036     static final private boolean with_raw_metrics = false;
00037     static public enum MergeType {TOP_DOWN, BOTTOM_UP, FLAT};
00038     
00049     static public Experiment merge(Experiment exp1, Experiment exp2, MergeType type, boolean verbose) {
00050         
00051         File file1 = exp1.getXMLExperimentFile();
00052         String parent_dir = file1.getParentFile().getParent() + File.separator + "merged" + File.separator;
00053 
00054         return merge(exp1, exp2, type, parent_dir, verbose);
00055     }
00056     
00057     static public Experiment merge(Experiment exp1, Experiment exp2, MergeType type, 
00058             String parent_dir, boolean verbose) {
00059         
00060         // -----------------------------------------------
00061         // step 1: create new base Experiment
00062         // -----------------------------------------------
00063         Experiment merged = exp1.duplicate();
00064         
00065         final ExperimentConfiguration configuration = new ExperimentConfiguration();
00066         configuration.setName( ExperimentConfiguration.NAME_EXPERIMENT, exp1.getName() + " & " + exp2.getName() );
00067         configuration.searchPaths = exp1.getConfiguration().searchPaths;
00068         
00069         merged.setConfiguration( configuration );
00070 
00071         // Add tree1, walk tree2 & add; just CCT/Flat
00072         RootScope rootScope = new RootScope(merged,"Invisible Outer Root Scope", RootScopeType.Invisible);
00073         merged.setRootScope(rootScope);
00074                 
00075         // -----------------------------------------------
00076         // step 2: combine all metrics
00077         // -----------------------------------------------
00078         List<BaseMetric> metrics = buildMetricList(merged, exp1.getMetrics(), exp2.getMetrics());
00079         merged.setMetrics(metrics);
00080         
00081         if (with_raw_metrics)
00082         {
00083             final MetricRaw metricRaw[] = buildMetricRaws( exp1.getMetricRaw(), exp2.getMetricRaw() );
00084             merged.setMetricRaw(metricRaw);
00085         }
00086 
00087         // -----------------------------------------------
00088         // step 3: mark the new experiment file
00089         // -----------------------------------------------
00090 
00091         final File fileMerged  = new File( parent_dir + File.separator + Constants.DATABASE_FILENAME); 
00092         merged.setXMLExperimentFile( fileMerged );
00093 
00094         // -----------------------------------------------
00095         // step 4: create roots
00096         // -----------------------------------------------      
00097 
00098         int root_type = 0;
00099         switch (type) {
00100         case TOP_DOWN:
00101             root_type = 0; break;
00102         case BOTTOM_UP:
00103             root_type = 1; break;
00104         case FLAT:
00105             root_type = 2; break;
00106         }
00107         
00108         RootScope root2 = (RootScope) exp2.getRootScopeChildren()[root_type];   
00109 
00110         RootScope root2_copy = new RootScope(root2.getExperiment(), 
00111                 "Invisible Outer Root Scope", RootScopeType.Invisible);
00112 
00113         DuplicateScopeTreesVisitor visitor = new DuplicateScopeTreesVisitor(root2_copy);
00114         root2.dfsVisitScopeTree(visitor);
00115         
00116         // -----------------------------------------------
00117         // step 5: merge the two experiments
00118         // -----------------------------------------------
00119 
00120         mergeScopeTrees(exp1,new DuplicateScopeTreesVisitor(rootScope), root_type);     
00121         
00122         RootScope root1 = (RootScope) merged.getRootScopeChildren()[0]; 
00123         RootScope root2_copy_cct = (RootScope) root2_copy.getChildAt(0);
00124 
00125         final int metricCount = exp1.getMetricCount();
00126         new TreeSimilarity(metricCount, root1, root2_copy_cct, verbose);
00127         
00128         return merged;
00129     }
00130     
00131     
00132     /***
00133      * combine metrics from exp 1 and exp 2
00134      * 
00135      * @param exp
00136      * @param m1
00137      * @param m2
00138      * @return
00139      */
00140     private static ArrayList<BaseMetric> buildMetricList(Experiment exp, BaseMetric[] m1, BaseMetric[] m2) 
00141     {
00142         final ArrayList<BaseMetric> metricList = new ArrayList<BaseMetric>( m1.length + m2.length );
00143         
00144         // ----------------------------------------------------------------
00145         // step 1: add the first metrics into the merged experiment
00146         // ----------------------------------------------------------------
00147         for (int i=0; i<m1.length; i++) {
00148             // add metric into the merged list
00149             BaseMetric mm = m1[i].duplicate();
00150             
00151             setMetricCombinedName(1, mm);
00152             
00153             metricList.add(mm);
00154         }
00155         
00156         // attention: hpcprof doesn't guarantee the ID of metric starts with zero
00157         //  we should make sure that the next ID for the second group of metrics is not
00158         //  overlapped with the ID from the first group of metrics
00159         final int m1_last = m1.length - 1;
00160         final int m1_last_shortname = Integer.valueOf(m1[m1_last].getShortName());
00161         int m1_last_index = Math.max(m1_last_shortname, m1[m1_last].getIndex()) + 1;
00162         
00163         // ----------------------------------------------------------------
00164         // step 2: append the second metrics, and reset the index and the key
00165         // ----------------------------------------------------------------
00166         for (int i=0; i<m2.length; i++) {
00167             final BaseMetric m = m2[i].duplicate();
00168 
00169             setMetricCombinedName(2, m);
00170             
00171             // recompute the index of the metric from the second experiment
00172             final int index_new = m1_last_index + m.getIndex();
00173             m.setIndex( index_new );
00174             
00175             // reset the key
00176             m.setShortName( String.valueOf(index_new) );
00177             
00178             metricList.add(m);
00179         }
00180         
00181         return metricList;
00182     }
00183 
00184 
00185     /***
00186      * recursively merge trees
00187      * 
00188      * @param exp2
00189      * @param visitor
00190      */
00191     private static void mergeScopeTrees(Experiment exp2, 
00192             BaseDuplicateScopeTreesVisitor visitor, int iRoot) {
00193 
00194         RootScope root2 = (RootScope) exp2.getRootScopeChildren()[iRoot];       
00195 
00196         root2.dfsVisitScopeTree(visitor);
00197     }
00198     
00199     /***
00200      * merge two metric raws
00201      * 
00202      * @param raws1
00203      * @param raws2
00204      * @return
00205      */
00206     private static MetricRaw[] buildMetricRaws( MetricRaw raws1[], MetricRaw raws2[]) 
00207     {
00208         MetricRaw rawList[] = new MetricRaw[ raws1.length + raws2.length ];
00209         
00210         for (int i=0; i<raws1.length; i++)
00211         {
00212             rawList[i] = (MetricRaw) raws1[i].duplicate();
00213             setMetricCombinedName(1, rawList[i]);
00214         }
00215         
00216         for (int i=0; i<raws2.length; i++)
00217         {
00218             rawList[i + raws1.length] = (MetricRaw) raws2[i].duplicate();
00219             setMetricCombinedName(2, rawList[i + raws1.length]);
00220         }
00221         
00222         return rawList;
00223     }
00224 
00225     /***
00226      * create a new metric name based on the offset of the experiment and the metric
00227      * 
00228      * @param offset
00229      * @param m
00230      */
00231     private static void setMetricCombinedName( int offset, BaseMetric m )
00232     {
00233         m.setDisplayName( offset + "-" + m.getDisplayName() );
00234     }
00235 }
00236 

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1