package Analysis.Scheduling;
+import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
+import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Random;
int coreNum;
int scheduleThreshold;
int probThreshold;
+ int generateThreshold;
+
+ //Random rand;
public MCImplSynthesis(State state,
TaskAnalysis ta,
this.ownershipAnalysis = oa;
this.scheduleAnalysis = new ScheduleAnalysis(state,
ta);
- scheduleAnalysis.setCoreNum(this.coreNum);
+ this.scheduleAnalysis.setCoreNum(this.coreNum);
this.scheduleSimulator = new ScheduleSimulator(this.coreNum,
state,
ta);
this.scheduleThreshold = 1000;
this.probThreshold = 0;
+ this.generateThreshold = 30;
+ //this.rand = new Random();
}
public int getCoreNum() {
this.probThreshold = probThreshold;
}
+ public int getGenerateThreshold() {
+ return generateThreshold;
+ }
+
+ public void setGenerateThreshold(int generateThreshold) {
+ this.generateThreshold = generateThreshold;
+ }
+
public Vector<Schedule> synthesis() {
// Print stuff to the original output and error streams.
// The stuff printed through the 'origOut' and 'origErr' references
// generate multiple schedulings
this.scheduleAnalysis.setScheduleThreshold(this.scheduleThreshold);
- this.scheduleAnalysis.schedule();
+ this.scheduleAnalysis.schedule(this.generateThreshold);
+ if(this.generateThreshold > 5) {
+ this.generateThreshold = 5;
+ }
Vector<Vector<ScheduleNode>> scheduleGraphs = null;
Vector<Vector<ScheduleNode>> newscheduleGraphs =
multiparamtds.addElement(td);
}
}
+ it_tasks = null;
int tryindex = 1;
int bestexetime = Integer.MAX_VALUE;
System.out.print("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
System.out.print("Simulate and optimize round: #" + tryindex + ": \n");
gid += newscheduleGraphs.size();
+ if(scheduleGraphs != null) {
+ for(int i = 0; i < scheduleGraphs.size(); i++) {
+ Vector<ScheduleNode> tmpgraph = scheduleGraphs.elementAt(i);
+ for(int j = 0; j < tmpgraph.size(); j++) {
+ tmpgraph.elementAt(j).getEdgeVector().clear();
+ tmpgraph.elementAt(j).getInedgeVector().clear();
+ }
+ tmpgraph.clear();
+ tmpgraph = null;
+ }
+ scheduleGraphs.clear();
+ }
scheduleGraphs = newscheduleGraphs;
schedulings.clear();
// get scheduling layouts from schedule graphs
Vector<Schedule> tmpscheduling =
generateScheduling(scheduleGraph, multiparamtds);
schedulings.add(tmpscheduling);
+ scheduleGraph = null;
+ tmpscheduling = null;
}
selectedSchedulings.clear();
+ for(int i = 0; i < selectedSimExeGraphs.size(); i++) {
+ selectedSimExeGraphs.elementAt(i).clear();
+ }
selectedSimExeGraphs.clear();
int tmpexetime = this.scheduleSimulator.simulate(schedulings,
selectedSchedulings,
selectedSimExeGraphs);
if(tmpexetime < bestexetime) {
bestexetime = tmpexetime;
+ if(scheduling != null) {
+ scheduling.clear();
+ for(int j = 0; j < schedulinggraph.size(); j++) {
+ schedulinggraph.elementAt(j).getEdgeVector().clear();
+ schedulinggraph.elementAt(j).getInedgeVector().clear();
+ }
+ schedulinggraph.clear();
+ }
scheduling = schedulings.elementAt(selectedSchedulings.elementAt(0));
- schedulinggraph = newscheduleGraphs.elementAt(selectedSchedulings.elementAt(0));
+ schedulinggraph = scheduleGraphs.elementAt(selectedSchedulings.elementAt(0));
System.out.print("end of: #" + tryindex + " (bestexetime: " + bestexetime + ")\n");
System.out.print("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
tryindex++;
selectedSimExeGraphs,
gid,
this.scheduleThreshold);
+ if(tmpexetime < bestexetime) {
+ scheduleGraphs.remove(selectedSchedulings.elementAt(0));
+ }
}while(newscheduleGraphs != null); // TODO: could it possibly lead to endless loop?
+ if(scheduleGraphs != null) {
+ scheduleGraphs.clear();
+ }
scheduleGraphs = null;
newscheduleGraphs = null;
+ schedulings.clear();
schedulings = null;
+ selectedSchedulings.clear();
selectedSchedulings = null;
+ for(int i = 0; i < selectedSimExeGraphs.size(); i++) {
+ selectedSimExeGraphs.elementAt(i).clear();
+ }
+ selectedSimExeGraphs.clear();
selectedSimExeGraphs = null;
+ multiparamtds.clear();
multiparamtds = null;
-
+
+ System.out.print("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+ System.out.print("selected bestexetime: " + bestexetime + "\n");
String path = this.state.outputdir + "scheduling_selected.dot";
SchedulingUtil.printScheduleGraph(path, schedulinggraph);
// Close the streams.
try {
stdout.close();
+ stdout = null;
System.setOut(origOut);
} catch (Exception e) {
origOut.println("Redirect: Unable to close files!");
}
+
+ schedulinggraph.clear();
+ schedulinggraph = null;
return scheduling;
}
+
+ // for test
+ // get the distribution info of new search algorithm
+ public void distribution() {
+ // Print stuff to the original output and error streams.
+ // The stuff printed through the 'origOut' and 'origErr' references
+ // should go to the console on most systems while the messages
+ // printed through the 'System.out' and 'System.err' will end up in
+ // the files we created for them.
+ //origOut.println ("\nRedirect: Round #2");
+ //System.out.println ("Test output via 'SimulatorResult.out'.");
+ //origOut.println ("Test output via 'origOut' reference.");
+
+ // Save the current standard input, output, and error streams
+ // for later restoration.
+ PrintStream origOut = System.out;
+
+ // Create a new output stream for the standard output.
+ PrintStream stdout = null;
+ try {
+ stdout = new PrintStream(
+ new FileOutputStream(this.state.outputdir + "SimulatorResult_"
+ + this.coreNum + ".out"));
+ } catch (Exception e) {
+ // Sigh. Couldn't open the file.
+ System.out.println("Redirect: Unable to open output file!");
+ System.exit(1);
+ }
+
+ // Print stuff to the original output and error streams.
+ // On most systems all of this will end up on your console when you
+ // run this application.
+ //origOut.println ("\nRedirect: Round #1");
+ //System.out.println ("Test output via 'System.out'.");
+ //origOut.println ("Test output via 'origOut' reference.");
+
+ // Set the System out and err streams to use our replacements.
+ System.setOut(stdout);
+
+ // generate multiple schedulings
+ this.scheduleAnalysis.setScheduleThreshold(1000);
+ this.scheduleAnalysis.schedule(20);
+ this.generateThreshold = 5;
+ this.probThreshold = 0;
+
+ Vector<Vector<ScheduleNode>> scheduleGraphs = null;
+ Vector<Vector<ScheduleNode>> totestscheduleGraphs =
+ this.scheduleAnalysis.getScheduleGraphs();
+ Vector<Vector<Schedule>> schedulings = new Vector<Vector<Schedule>>();
+ Vector<Integer> selectedSchedulings = new Vector<Integer>();
+ Vector<Vector<SimExecutionEdge>> selectedSimExeGraphs =
+ new Vector<Vector<SimExecutionEdge>>();
+
+ // check all multi-parameter tasks
+ Vector<TaskDescriptor> multiparamtds = new Vector<TaskDescriptor>();
+ Iterator it_tasks = this.state.getTaskSymbolTable().getDescriptorsIterator();
+ while(it_tasks.hasNext()) {
+ TaskDescriptor td = (TaskDescriptor)it_tasks.next();
+ if(td.numParameters() > 1) {
+ multiparamtds.addElement(td);
+ }
+ }
+ it_tasks = null;
+
+ File file=new File(this.state.outputdir + "distributeinfo_s_" + this.coreNum + ".out");
+ FileOutputStream dotstream = null;
+ File file2=new File(this.state.outputdir + "distributeinfo_o_" + this.coreNum + ".out");
+ FileOutputStream dotstream2 = null;
+ try {
+ dotstream = new FileOutputStream(file,false);
+ dotstream2 = new FileOutputStream(file2,false);
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.exit(-1);
+ }
+ PrintWriter output = new java.io.PrintWriter(dotstream, true);
+ PrintWriter output2 = new java.io.PrintWriter(dotstream2, true);
+ output.println("start time(1,000,000 cycles): " + totestscheduleGraphs.size());
+ output2.println("optimized time(1,000,000 cycles): " + totestscheduleGraphs.size());
+ for(int ii = 0; ii < 1/*totestscheduleGraphs.size()*/; ii++) {
+ Vector<Vector<ScheduleNode>> newscheduleGraphs =
+ new Vector<Vector<ScheduleNode>>();
+ newscheduleGraphs.add(totestscheduleGraphs.elementAt(ii));
+ int tryindex = 1;
+ int bestexetime = Integer.MAX_VALUE;
+ int gid = 1;
+ Vector<Schedule> scheduling = null;
+ Vector<ScheduleNode> schedulinggraph = null;
+ boolean isfirst = true;
+ Random rand = new Random();
+ // simulate the generated schedulings and try to optimize it
+ System.out.print("=========================================================\n");
+ System.out.print("# " + ii + ": \n");
+ do {
+ System.out.print("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+ System.out.print("Simulate and optimize round: #" + tryindex + ": \n");
+ gid += newscheduleGraphs.size();
+ if(scheduleGraphs != null) {
+ for(int i = 0; i < scheduleGraphs.size(); i++) {
+ Vector<ScheduleNode> tmpgraph = scheduleGraphs.elementAt(i);
+ for(int j = 0; j < tmpgraph.size(); j++) {
+ ScheduleNode snode = tmpgraph.elementAt(j);
+ snode.getEdgeVector().clear();
+ snode.getInedgeVector().clear();
+ snode.getScheduleEdges().clear();
+ snode.getClassNodes().clear();
+ }
+ tmpgraph.clear();
+ tmpgraph = null;
+ }
+ scheduleGraphs.clear();
+ }
+ scheduleGraphs = newscheduleGraphs;
+ schedulings.clear();
+ // get scheduling layouts from schedule graphs
+ for(int i = 0; i < scheduleGraphs.size(); i++) {
+ Vector<ScheduleNode> scheduleGraph = scheduleGraphs.elementAt(i);
+ Vector<Schedule> tmpscheduling =
+ generateScheduling(scheduleGraph, multiparamtds);
+ schedulings.add(tmpscheduling);
+ scheduleGraph = null;
+ tmpscheduling = null;
+ }
+ selectedSchedulings.clear();
+ for(int i = 0; i < selectedSimExeGraphs.size(); i++) {
+ selectedSimExeGraphs.elementAt(i).clear();
+ }
+ selectedSimExeGraphs.clear();
+ int tmpexetime = this.scheduleSimulator.simulate(schedulings,
+ selectedSchedulings,
+ selectedSimExeGraphs);
+ if(isfirst) {
+ output.println(((float)tmpexetime/1000000));
+ isfirst = false;
+ }
+ if(tmpexetime < bestexetime) {
+ bestexetime = tmpexetime;
+ if(scheduling != null) {
+ scheduling.clear();
+ for(int j = 0; j < schedulinggraph.size(); j++) {
+ ScheduleNode snode = schedulinggraph.elementAt(j);
+ snode.getEdgeVector().clear();
+ snode.getInedgeVector().clear();
+ snode.getScheduleEdges().clear();
+ snode.getClassNodes().clear();
+ }
+ schedulinggraph.clear();
+ }
+ scheduling = schedulings.elementAt(selectedSchedulings.elementAt(0));
+ schedulinggraph = scheduleGraphs.elementAt(selectedSchedulings.elementAt(0));
+ tryindex++;
+ System.out.print("end of: #" + tryindex + " (bestexetime: " + bestexetime + ")\n");
+ System.out.print("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+ } else if(tmpexetime == bestexetime) {
+ System.out.print("end of: #" + tryindex + " (bestexetime: " + bestexetime + ")\n");
+ System.out.print("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+ tryindex++;
+ if((Math.abs(rand.nextInt()) % 100) < this.probThreshold) {
+ break;
+ }
+ } else {
+ break;
+ }
+
+ // try to optimize theschedulings best one scheduling
+ newscheduleGraphs = optimizeScheduling(scheduleGraphs,
+ selectedSchedulings,
+ selectedSimExeGraphs,
+ gid,
+ this.scheduleThreshold);
+ if(tmpexetime < bestexetime) {
+ scheduleGraphs.remove(selectedSchedulings.elementAt(0));
+ }
+ }while(newscheduleGraphs != null); // TODO: could it possibly lead to endless loop?
+
+ scheduleGraphs.clear();
+ scheduleGraphs = null;
+ scheduling = null;
+ schedulinggraph = null;
+ if(newscheduleGraphs != null) {
+ newscheduleGraphs.clear();
+ }
+ newscheduleGraphs = null;
+ totestscheduleGraphs.elementAt(ii).clear();
+ for(int i = 0; i < schedulings.size(); i++) {
+ schedulings.elementAt(i).clear();
+ }
+ schedulings.clear();
+ selectedSchedulings.clear();
+ for(int i = 0; i < selectedSimExeGraphs.size(); i++) {
+ selectedSimExeGraphs.elementAt(i).clear();
+ }
+ selectedSimExeGraphs.clear();
+
+ output2.println(((float)bestexetime/1000000));
+ System.out.print("=========================================================\n");
+ }
+
+ if(scheduleGraphs != null) {
+ scheduleGraphs.clear();
+ }
+ scheduleGraphs = null;
+ totestscheduleGraphs = null;
+ for(int i = 0; i < schedulings.size(); i++) {
+ schedulings.elementAt(i).clear();
+ }
+ schedulings.clear();
+ schedulings = null;
+ selectedSchedulings.clear();
+ selectedSchedulings = null;
+ selectedSimExeGraphs.clear();
+ selectedSimExeGraphs = null;
+ multiparamtds.clear();
+ multiparamtds = null;
+
+ // Close the streams.
+ try {
+ output.close();
+ stdout.close();
+ output = null;
+ stdout = null;
+ System.setOut(origOut);
+ } catch (Exception e) {
+ origOut.println("Redirect: Unable to close files!");
+ }
+
+ return;
+ }
private Vector<Vector<ScheduleNode>> optimizeScheduling(Vector<Vector<ScheduleNode>> scheduleGraphs,
Vector<Integer> selectedScheduleGraphs,
for(int i = 0; i < criticalPath.size(); i++) {
SimExecutionEdge seedge = criticalPath.elementAt(i);
Vector<SimExecutionEdge> predicates = seedge.getPredicates();
- if(predicates.size() > 0) {
+ if(predicates != null) {
// have predicates
int starttime = 0;
// check the latest finish time of all the predicates
(SimExecutionNode)tmpseedge.getTarget();
if(tmpsenode.getTimepoint() > timepoint) {
// update the predicate info
- edge.getPredicates().remove(lastpredicateedge);
+ if(edge.getPredicates() != null) {
+ edge.getPredicates().remove(lastpredicateedge);
+ }
edge.addPredicate(criticalPath.elementAt(index));
break;
}
CombinationUtil.CombineGenerator cGen =
CombinationUtil.allocateCombineGenerator(rootNodes, nodes2combine);
+ Random rand = new Random();
while ((left > 0) && (cGen.nextGen())) {
- Vector<Vector<CombinationUtil.Combine>> combine = cGen.getCombine();
- Vector<ScheduleNode> sNodes = SchedulingUtil.generateScheduleGraph(this.state,
- newscheduleGraph,
- scheduleEdges,
- rootNodes,
- combine,
- lgid++);
- if(optimizeschedulegraphs == null) {
- optimizeschedulegraphs = new Vector<Vector<ScheduleNode>>();
+ if(Math.abs(rand.nextInt()) % 100 > this.generateThreshold) {
+ Vector<Vector<CombinationUtil.Combine>> combine = cGen.getCombine();
+ Vector<ScheduleNode> sNodes = SchedulingUtil.generateScheduleGraph(this.state,
+ newscheduleGraph,
+ scheduleEdges,
+ rootNodes,
+ combine,
+ lgid++);
+ if(optimizeschedulegraphs == null) {
+ optimizeschedulegraphs = new Vector<Vector<ScheduleNode>>();
+ }
+ optimizeschedulegraphs.add(sNodes);
+ combine = null;
+ sNodes = null;
+ left--;
}
- optimizeschedulegraphs.add(sNodes);
- combine = null;
- sNodes = null;
- left--;
}
+ cGen.clear();
rootNodes = null;
nodes2combine = null;
newscheduleGraph = null;
scheduleEdges.clear();
scheduleEdges = null;
+ roots = null;
+ tocombines = null;
return optimizeschedulegraphs;
}
setstartupcore = true;
}
}
+ it_edges = null;
}
it_flags = null;
}
return scheduleEdges;
}
- public void schedule() {
+ public void schedule(int generateThreshold) {
try {
Vector<ScheduleEdge> toBreakDown = new Vector<ScheduleEdge>();
ScheduleNode startupNode = null;
// as possible
CFSTGTransform();
// mappint to real multi-core processor
- coreMapping();
+ coreMapping(generateThreshold);
+ toBreakDown = null;
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}
+ it_rootnodes = null;
}
Iterator it_flags = this.taskanalysis.getFlagStates(cd).iterator();
while(it_flags.hasNext()) {
((FlagState)edge.getSource()).setByObj(taskinfo.m_byObj);
}
}
+ it_edges = null;
}
+ it_flags = null;
}
}
taskinfos = null;
+ it_classes = null;
} else {
randomProfileSetting();
}
edge.setisbackedge(true);
}
}
+ it_edges = null;
}
+ it_fs = null;
}
}
}
fss = null;
}
}
+ it_classes = null;
}
private void readProfileInfo(java.util.Hashtable<String, TaskInfo> taskinfos) {
taskinfos.put(inname, tinfo);
inindex = profiledata.indexOf('\n');
}
+ inStream.close();
+ inStream = null;
+ b = null;
} catch(Exception e) {
e.printStackTrace();
}
// Randomly set the newRate and probability of FEdges
java.util.Random r=new java.util.Random();
int tint = 0;
- for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator(); it_classes.hasNext();) {
+ Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator();
+ for(; it_classes.hasNext();) {
ClassDescriptor cd=(ClassDescriptor) it_classes.next();
if(cd.hasFlags()) {
Vector rootnodes=this.taskanalysis.getRootNodes(cd);
if(rootnodes!=null) {
- for(Iterator it_rootnodes=rootnodes.iterator(); it_rootnodes.hasNext();) {
+ Iterator it_rootnodes=rootnodes.iterator();
+ for(; it_rootnodes.hasNext();) {
FlagState root=(FlagState)it_rootnodes.next();
Vector allocatingTasks = root.getAllocatingTasks();
if(allocatingTasks != null) {
}
}
}
+ it_rootnodes = null;
}
Iterator it_flags = this.taskanalysis.getFlagStates(cd).iterator();
total -= tint;
}
}
+ it_edges = null;
}
+ it_flags = null;
}
}
+ it_classes = null;
}
private ScheduleNode buildCFSTG(Vector<ScheduleEdge> toBreakDown) {
cNode.setTransTime(45);
}
}
+ rootnodes = null;
fStates = null;
sFStates = null;
}
+ it_classes = null;
ScheduleNode startupNode = null;
// For each ClassNode create a ScheduleNode containing it
for(i = 0; i < toBreakDown.size(); i++ ) {
cloneSNodeList(toBreakDown.elementAt(i), false);
}
- toBreakDown = null;
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
while(it_edge.hasNext()) {
toVisit.add((ScheduleNode)((ScheduleEdge)it_edge.next()).getTarget());
}
+ it_edge = null;
}
}
toVisit = null;
ses = null;
}
keys = null;
+ it_keys = null;
}
fe2ses.clear();
sn2fes.clear();
toClone = null;
clone = null;
qcn2cn = null;
+ cn2cn.clear();
cn2cn = null;
origins = null;
sn2sn = null;
sfss.removeElement(temp);
}
}
+ it_edges = null;
}
sfss = null;
Vector<FlagState> sFStates = FlagState.DFS.topology(fStates, null);
toiterate.add(temp);
}
}
+ it_inedges = null;
}
toiterate = null;
}
}
}
+ it_isEdges = null;
oldSNode.getScheduleEdges().removeAll(toremove);
toremove.clear();
// redirect ScheudleEdges out of this subtree to the new ScheduleNode
}
}
}
+ it_sEdges = null;
se.getSource().getEdgeVector().removeAll(toremove);
toremove = null;
rCNodes = null;
// TODO: restrict the number of generated scheduling according to the setted
// scheduleThreshold
- private void coreMapping() throws Exception {
+ private void coreMapping(int generateThreshold) throws Exception {
if(this.coreNum == -1) {
throw new Exception("Error: un-initialized coreNum when doing scheduling.");
}
this.coreNum);
int gid = 1;
+ Random rand = new Random();
while((gid <= this.scheduleThreshold) && (rGen.nextGen())) {
// first get the chosen rootNodes
Vector<Vector<ScheduleNode>> rootNodes = rGen.getRootNodes();
CombinationUtil.allocateCombineGenerator(rootNodes,
nodes2combine);
while((gid <= this.scheduleThreshold) && cGen.nextGen()) {
- Vector<Vector<CombinationUtil.Combine>> combine = cGen.getCombine();
- Vector<ScheduleNode> sNodes = SchedulingUtil.generateScheduleGraph(this.state,
- this.scheduleNodes,
- this.scheduleEdges,
- rootNodes,
- combine,
- gid++);
- this.scheduleGraphs.add(sNodes);
- combine = null;
- sNodes = null;
+ if(Math.abs(rand.nextInt()) % 100 > generateThreshold) {
+ Vector<Vector<CombinationUtil.Combine>> combine = cGen.getCombine();
+ Vector<ScheduleNode> sNodes =
+ SchedulingUtil.generateScheduleGraph(this.state,
+ this.scheduleNodes,
+ this.scheduleEdges,
+ rootNodes,
+ combine,
+ gid++);
+ this.scheduleGraphs.add(sNodes);
+ combine = null;
+ sNodes = null;
+ }
}
+ cGen.clear();
rootNodes = null;
nodes2combine = null;
}
+ rGen.clear();
sNodeVecs = null;
}
}