ExperimentMerger.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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
00025
00026
00027
00028
00029
00030
00031
00032
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
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
00072 RootScope rootScope = new RootScope(merged,"Invisible Outer Root Scope", RootScopeType.Invisible);
00073 merged.setRootScope(rootScope);
00074
00075
00076
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
00089
00090
00091 final File fileMerged = new File( parent_dir + File.separator + Constants.DATABASE_FILENAME);
00092 merged.setXMLExperimentFile( fileMerged );
00093
00094
00095
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
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
00134
00135
00136
00137
00138
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
00146
00147 for (int i=0; i<m1.length; i++) {
00148
00149 BaseMetric mm = m1[i].duplicate();
00150
00151 setMetricCombinedName(1, mm);
00152
00153 metricList.add(mm);
00154 }
00155
00156
00157
00158
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
00165
00166 for (int i=0; i<m2.length; i++) {
00167 final BaseMetric m = m2[i].duplicate();
00168
00169 setMetricCombinedName(2, m);
00170
00171
00172 final int index_new = m1_last_index + m.getIndex();
00173 m.setIndex( index_new );
00174
00175
00176 m.setShortName( String.valueOf(index_new) );
00177
00178 metricList.add(m);
00179 }
00180
00181 return metricList;
00182 }
00183
00184
00185
00186
00187
00188
00189
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
00201
00202
00203
00204
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
00227
00228
00229
00230
00231 private static void setMetricCombinedName( int offset, BaseMetric m )
00232 {
00233 m.setDisplayName( offset + "-" + m.getDisplayName() );
00234 }
00235 }
00236