import java.util.*; import java.io.*; public class PlatformCreator { static int cpuBound = 130; /* min cpu 50.0, max cpu 50.0+cpuBound */ static int bwBound= 130; /* min bw 50.0, max bw 50.0+bwBound */ // static int costBound = 1000; /* min cost 1000, max cost 1000+costBound */ static int linkBound = 40; static int deltaBound = 10; static int actInternalNumber = 0; static double[] frequency; private static int getNextInternalNum() { return actInternalNumber++; } static int[] subtreeSizes; public static ArrayList createPlatform(int numProcs, Random rand) { int id; double cpu; double bw; int cost; //Random rand = new Random(randomBase); ArrayList procs = new ArrayList(); double links[] = new double[numProcs]; for(int i = 0; i< numProcs; i++) { links[i]=0.0; } for(int i = 0; i< numProcs; i++) { id = i; cpu = ((double)rand.nextInt(cpuBound)+60); bw = ((double)rand.nextInt(bwBound)+60); //cost = (rand.nextInt(costBound)+1000); cost = ((int)cpu)*10; Proc proc = new Proc(id, cpu, bw, links, cost); procs.add(proc); } Proc p1, p2; double link; for(int i = 0; i< numProcs; i++) { for(int j = i+1; j< numProcs; j++) { p1 = procs.get(i); p2 = procs.get(j); link = ((double)rand.nextInt(linkBound)+40); p1.links[j] = link; p2.links[i] = link; } } // for(Proc proc : procs) { // System.out.println(proc.toStringAll()+"\n"); // } return procs; } public static ArrayList createObjects(int numObjects, Random rand) { ArrayList objects = new ArrayList(); //Random rand = new Random(randomBase); for(int i = 0; i createOperators(int totalNumOperators, ArrayList objects, Random rand, int bound, double ccrIndex) { double delta, w; double rW = 0.5; double rDelta = ccrIndex - rW; if( totalNumOperators < bound || bound < 2) { System.out.println("Attention: bound > totalNum of operators. Please decrease bound and try again.\n bound must be > 2."); System.exit(0); } MyArrayList operators = new MyArrayList(); subtreeSizes = new int[totalNumOperators]; if(CompileTimeCondition.DEBUG) { System.out.println("Object creation:"); } //Random rand = new Random(randomBase); Operator actOp = new Operator(getNextInternalNum(), 0); delta = rand.nextDouble()+rDelta; w = rand.nextDouble()+rW; actOp.delta = delta; actOp.w = w; int numChildren = rand.nextInt(2)+1; for(int i = 0; i < numChildren; i++) { int objectPos = rand.nextInt(objects.size()); BasicObject obj = objects.get(objectPos); actOp.objects.add(obj); } operators.add(actOp); subtreeSizes[0]=1; if(CompileTimeCondition.DEBUG) { System.out.println(actOp+", subtree: 1"); } int numObjects, numOperators; actOp = new Operator(getNextInternalNum(), 1); delta = rand.nextDouble()+rDelta; w = rand.nextDouble()+rW; actOp.delta = delta; actOp.w = w; numOperators = rand.nextInt(3); numObjects = 2 - numOperators; for(int j = 0; j= 3) numOperators = 2; numObjects = 2 - numOperators; for(int j = 0; j= 3) numOperators = 2; numObjects = 2 - numOperators; for(int j = 0; j CommonFunctions.rho[genOp.app]) genOp.app = appli; for(BasicObject tmpObj : genOp.objects) { //System.out.println("appli: "+appli+", tmpObj app: "+tmpObj.app); //System.out.println("frequency appli : "+TopDownBFS.frequency[appli]+", frequency tmpObj: "+TopDownBFS.frequency[tmpObj.app]); if(CommonFunctions.frequency[appli] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = appli; BasicObject obj = new BasicObject(tmpObj.id, appli, tmpObj.delta); child.objects.add(obj); } for(Operator tmpChild : genOp.operators) { traverseTreeForOperators(tmpChild,child, appli); } //father.operators.add(child); } public static void traverseTreeForOperatorsAndTranspose(Operator genOp, Operator father, int appli, int transposalBase) { Operator genChild = new Operator(getNextInternalNum(), genOp.id+transposalBase, appli, father); Operator child = new Operator(getNextInternalNum(), genOp.id+transposalBase, appli, father); father.operators.add(child); if(CommonFunctions.rho[appli] > CommonFunctions.rho[genOp.app]) genOp.app = appli; for(BasicObject tmpObj : genOp.objects) { //System.out.println("appli: "+appli+", tmpObj app: "+tmpObj.app); //System.out.println("frequency appli : "+TopDownBFS.frequency[appli]+", frequency tmpObj: "+TopDownBFS.frequency[tmpObj.app]); if(CommonFunctions.frequency[appli] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = appli; BasicObject obj = new BasicObject(tmpObj.id, appli, tmpObj.delta); child.objects.add(obj); genChild.objects.add(obj); } RunHeuristics.operators.add(genChild); for(Operator tmpChild : genOp.operators) { traverseTreeForOperatorsAndTranspose(tmpChild, child, appli,transposalBase); } //father.operators.add(child); } public static void traverseTreeForOperatorsNoReuse(Operator genOp, Operator father, int appli, MyArrayList newOperatorList) { int nextId = getNextInternalNum(); Operator child = new Operator(nextId, nextId, appli, father); father.operators.add(child); if(CommonFunctions.rho[appli] > CommonFunctions.rho[genOp.app]) genOp.app = appli; for(BasicObject tmpObj : genOp.objects) { //System.out.println("appli: "+appli+", tmpObj app: "+tmpObj.app); //System.out.println("frequency appli : "+TopDownBFS.frequency[appli]+", frequency tmpObj: "+TopDownBFS.frequency[tmpObj.app]); if(CommonFunctions.frequency[appli] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = appli; BasicObject obj = new BasicObject(tmpObj.id, appli, tmpObj.delta); child.objects.add(obj); } RunHeuristics.operators.add(child); for(Operator tmpChild : genOp.operators) { traverseTreeForOperatorsNoReuse(tmpChild,child, appli,newOperatorList); } //father.operators.add(child); } public static void traverseTree(Operator root, PrintWriter appliWriter) { if(CompileTimeCondition.DEBUG) System.out.println(root.toStringLong()); appliWriter.println(root.toStringLong()); for(Operator op : root.operators) { traverseTree(op, appliWriter); } } /* creates one application, where size is not fixed */ public static Operator createApplication(int appli, Random rand, MyArrayList operators, int bound, boolean reuse ) { //Random rand = new Random(randomBase); int pos = operators.size() - (rand.nextInt(bound-1)+1); //int pos = rand.nextInt(operators.size()-4) +4; Operator tmp = operators.get(pos); Operator father = new Operator(appli); Operator root = new Operator(getNextInternalNum(), tmp.id, appli, father); if(CommonFunctions.rho[appli] > CommonFunctions.rho[tmp.app]) tmp.app = appli; if(reuse) for(Operator tmpChild : tmp.operators) { traverseTreeForOperators(tmpChild, root, appli); } else { MyArrayList newOperatorList = new MyArrayList(); for(Operator tmpChild : tmp.operators) { traverseTreeForOperatorsNoReuse(tmpChild,root, appli, newOperatorList); } } for(BasicObject tmpObj : tmp.objects) { if(CommonFunctions.frequency[appli] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = appli; BasicObject obj = new BasicObject(tmpObj.id, appli, tmpObj.delta); root.objects.add(obj); } return root; } /* creates a list of applications with a fixed application size */ /* size tolerance +-2 */ public static MyArrayList createApplicationsWithFixedSize(int numApplications, int appSize, Random rand, MyArrayList operators, boolean reuse ) { //Random rand = new Random(randomBase); if(CompileTimeCondition.DEBUG){ System.out.println("subtreeSizes "+subtreeSizes.length); for(int i = 0; i< subtreeSizes.length; i++) { System.out.print(subtreeSizes[i] + "\t"); } System.out.println(""); } MyArrayList applications = new MyArrayList(); MyArrayList possibleRoots = new MyArrayList(); for(int i = subtreeSizes.length-1; i>=0; i--) { if(((2 >= subtreeSizes[i] - appSize) && (subtreeSizes[i] - appSize >= 0)) || ((2 >= appSize - subtreeSizes[i]) && (appSize - subtreeSizes[i] >= 0 ))) { possibleRoots.add(operators.get(i)); if(CompileTimeCondition.DEBUG) System.out.println("act poss roots: "+operators.get(i)); } } if(possibleRoots.size() == 0) return possibleRoots; //if(CompileTimeCondition.DEBUG) System.out.println("num of possibleRoots: "+possibleRoots.size()); for(int i = 1; i <= numApplications; i++) { /* i = appli */ int pos = rand.nextInt(possibleRoots.size()); //int pos = rand.nextInt(operators.size()-4) +4; Operator tmp = possibleRoots.get(pos); Operator father = new Operator(i); Operator root = new Operator(getNextInternalNum(), tmp.id, i, father); if(CommonFunctions.rho[i] > CommonFunctions.rho[tmp.app]) tmp.app = i; if(reuse) for(Operator tmpChild : tmp.operators) { traverseTreeForOperators(tmpChild, root, i); } else { MyArrayList newOperatorList = new MyArrayList(); for(Operator tmpChild : tmp.operators) { traverseTreeForOperatorsNoReuse(tmpChild,root,i, newOperatorList); } } for(BasicObject tmpObj : tmp.objects) { if(CommonFunctions.frequency[i] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = i; BasicObject obj = new BasicObject(tmpObj.id, i, tmpObj.delta); root.objects.add(obj); } applications.add(root); } return applications; } /* creates two applications with a fixed application size */ /* size tolerance +-2 */ /* and given similarity */ public static MyArrayList createApplicationsOfFixedSizeWithFixedSimilarity(int numApplications, int appSize, int appSize1, int appSize2, Random rand, MyArrayList operators, boolean reuse , int similarity) { //Random rand = new Random(randomBase); if(CompileTimeCondition.DEBUG){ System.out.println("subtreeSizes "+subtreeSizes.length); for(int i = 0; i< subtreeSizes.length; i++) { System.out.print(subtreeSizes[i] + "\t"); } System.out.println(""); } MyArrayList applications = new MyArrayList(); MyArrayList possibleRootsApp1 = new MyArrayList(); MyArrayList possibleRootsApp2 = new MyArrayList(); for(int i = subtreeSizes.length-1; i>=0; i--) { /* tree part that is the same for both applications */ if(((1 >= subtreeSizes[i] - appSize1) && (subtreeSizes[i] - appSize1 >= 0)) || ((1 >= appSize1 - subtreeSizes[i]) && (appSize1 - subtreeSizes[i] >= 0 ))) { possibleRootsApp1.add(operators.get(i)); if(CompileTimeCondition.DEBUG) System.out.println("act poss roots: "+operators.get(i)); } /* tree part that differs in the applications */ if(((1 >= subtreeSizes[i] - appSize2) && (subtreeSizes[i] - appSize2 >= 0)) || ((1 >= appSize2 - subtreeSizes[i]) && (appSize2 - subtreeSizes[i] >= 0 ))) { possibleRootsApp2.add(operators.get(i)); if(CompileTimeCondition.DEBUG) System.out.println("act poss roots: "+operators.get(i)); } } if(possibleRootsApp1.size() == 0) return possibleRootsApp1; if(possibleRootsApp2.size() <= 1) return possibleRootsApp2; //if(CompileTimeCondition.DEBUG) System.out.println("num of possibleRoots for common part: "+possibleRootsApp1.size()); System.out.println("num of possibleRoots for different part: "+possibleRootsApp2.size()); int pos1 = rand.nextInt(possibleRootsApp1.size()); int pos2 = rand.nextInt(possibleRootsApp2.size()); // for(int i = 0; i < numApplications; i++) { // /* i = appli */ // //int pos = rand.nextInt(possibleRootsApp1.size()); // //int pos = rand.nextInt(operators.size()-4) +4; // if(i == 1) { // int newPos = rand.nextInt(possibleRootsApp2.size()); // while(newPos == pos2) // newPos = rand.nextInt(possibleRootsApp2.size()); // pos2 = newPos; // } if (similarity == 0) { for(int i = 0; i < numApplications; i++) { Operator tmp1 = possibleRootsApp1.get(pos1); Operator father = new Operator(i); Operator root = new Operator(getNextInternalNum(), tmp1.id, i, father); if(CommonFunctions.rho[i] > CommonFunctions.rho[tmp1.app]) tmp1.app = i; if(reuse) for(Operator tmpChild : tmp1.operators) { traverseTreeForOperators(tmpChild, root, i); } else { MyArrayList newOperatorList = new MyArrayList(); for(Operator tmpChild : tmp1.operators) { traverseTreeForOperatorsNoReuse(tmpChild, root, i, newOperatorList); } } for(BasicObject tmpObj : tmp1.objects) { if(CommonFunctions.frequency[i] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = i; BasicObject obj = new BasicObject(tmpObj.id, i, tmpObj.delta); root.objects.add(obj); } applications.add(root); } } else if (similarity == 1) { for(int i = 0; i < numApplications; i++) { Operator tmp1 = possibleRootsApp1.get(pos1); Operator father = new Operator(i); int newOperatorId = operators.get(operators.size()-1).id + 1; Operator newGenOp = new Operator(getNextInternalNum(), newOperatorId, i, father); newGenOp.operators.add(tmp1); operators.add(newGenOp); Operator root = new Operator(getNextInternalNum(), newOperatorId, i, father); Operator child = new Operator(getNextInternalNum(), tmp1.id, i, root); root.operators.add(child); if(CommonFunctions.rho[i] > CommonFunctions.rho[tmp1.app]) tmp1.app = i; if(reuse) for(Operator tmpChild : tmp1.operators) { traverseTreeForOperators(tmpChild, child, i); } else { MyArrayList newOperatorList = new MyArrayList(); for(Operator tmpChild : tmp1.operators) { traverseTreeForOperatorsNoReuse(tmpChild,child,i, newOperatorList); } } for(BasicObject tmpObj : tmp1.objects) { if(CommonFunctions.frequency[i] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = i; BasicObject obj = new BasicObject(tmpObj.id, i, tmpObj.delta); child.objects.add(obj); } applications.add(root); } } else { /* application 1: new root with child1 and child2 */ Operator tmp1 = possibleRootsApp1.get(pos1); Operator tmp2 = possibleRootsApp2.get(pos2); int app = 1; Operator father = new Operator(app); int newOperatorId = operators.get(operators.size()-1).id + 1; Operator newGenOp = new Operator(getNextInternalNum(), newOperatorId, app, father); newGenOp.operators.add(tmp1); newGenOp.operators.add(tmp2); operators.add(newGenOp); Operator root = new Operator(getNextInternalNum(), newOperatorId, app, father); Operator child1 = new Operator(getNextInternalNum(), tmp1.id, app, root); Operator child2 = new Operator(getNextInternalNum(), tmp2.id, app, root); root.operators.add(child1); root.operators.add(child2); System.out.println("tree 1: root: "+root+", child 1: "+child1+", child 2: "+child2); if(CommonFunctions.rho[app] > CommonFunctions.rho[tmp1.app]) tmp1.app = app; if(CommonFunctions.rho[app] > CommonFunctions.rho[tmp2.app]) tmp2.app = app; // if(reuse) { for(Operator tmpChild : tmp1.operators) { traverseTreeForOperators(tmpChild, child1, app); } for(Operator tmpChild : tmp2.operators) { traverseTreeForOperators(tmpChild, child2, app); } // } else { // MyArrayList newOperatorList = new MyArrayList(); // for(Operator tmpChild : tmp1.operators) { // traverseTreeForOperatorsNoReuse(tmpChild,child1,app, newOperatorList); // } // for(Operator tmpChild : tmp2.operators) { // traverseTreeForOperatorsNoReuse(tmpChild,child2,app, newOperatorList); // } // } for(BasicObject tmpObj : tmp1.objects) { if(CommonFunctions.frequency[app] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = app; BasicObject obj = new BasicObject(tmpObj.id, app, tmpObj.delta); child1.objects.add(obj); } for(BasicObject tmpObj : tmp2.objects) { if(CommonFunctions.frequency[app] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = app; BasicObject obj = new BasicObject(tmpObj.id, app, tmpObj.delta); child2.objects.add(obj); } if(!reuse) { MyArrayList newOperatorList = new MyArrayList(); Operator rootTree1 = new Operator(getNextInternalNum(),getNextInternalNum(),root.app,father); for(Operator tmpChild : newGenOp.operators) { traverseTreeForOperatorsNoReuse(tmpChild,rootTree1, app, newOperatorList); } applications.add(rootTree1); RunHeuristics.operators.add(rootTree1); } else { applications.add(root); } /* application 2: new root with child 1 , child 2 has to be transposed to ensure that */ /* no operator exists in the subtree of child 2 int the application1 */ int newPos = rand.nextInt(possibleRootsApp2.size()); while(newPos == pos2) newPos = rand.nextInt(possibleRootsApp2.size()); pos2 = newPos; tmp2 = possibleRootsApp2.get(pos2); app = 2; newOperatorId = operators.get(operators.size()-1).id + 1; child1 = new Operator(getNextInternalNum(), tmp1.id, app, null); child2 = new Operator(getNextInternalNum(), tmp2.id+newOperatorId, app, null); Operator newGenTmp2 = new Operator(getNextInternalNum(), child2.id, app, null); for(BasicObject tmpObj : tmp2.objects) { //System.out.println("appli: "+appli+", tmpObj app: "+tmpObj.app); //System.out.println("frequency appli : "+TopDownBFS.frequency[appli]+", frequency tmpObj: "+TopDownBFS.frequency[tmpObj.app]); if(CommonFunctions.frequency[app] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = app; BasicObject obj = new BasicObject(tmpObj.id, app, tmpObj.delta); newGenTmp2.objects.add(obj); } if(CommonFunctions.rho[app] > CommonFunctions.rho[tmp1.app]) tmp1.app = app; if(CommonFunctions.rho[app] > CommonFunctions.rho[tmp2.app]) tmp2.app = app; // if(reuse) { for(Operator tmpChild : tmp1.operators) { traverseTreeForOperators(tmpChild, child1, app); } for(Operator tmpChild : tmp2.operators) { traverseTreeForOperatorsAndTranspose(tmpChild, child2, app, newOperatorId); } //} else { // MyArrayList newOperatorList = new MyArrayList(); // for(Operator tmpChild : tmp1.operators) { // traverseTreeForOperatorsNoReuse(tmpChild,child1,app, newOperatorList); // } // for(Operator tmpChild : tmp2.operators) { // traverseTreeForOperatorsNoReuse(tmpChild,child2,app, newOperatorList); // } // } for(BasicObject tmpObj : tmp1.objects) { if(CommonFunctions.frequency[app] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = app; BasicObject obj = new BasicObject(tmpObj.id, app, tmpObj.delta); child1.objects.add(obj); } for(BasicObject tmpObj : tmp2.objects) { if(CommonFunctions.frequency[app] > CommonFunctions.frequency[tmpObj.app]) tmpObj.app = app; BasicObject obj = new BasicObject(tmpObj.id, app, tmpObj.delta); child2.objects.add(obj); } father = new Operator(app); newOperatorId = child2.id + 1; newGenOp = new Operator(getNextInternalNum(), newOperatorId, app, father); newGenOp.operators.add(tmp1); newGenOp.operators.add(newGenTmp2); operators.add(newGenOp); newGenTmp2.father = newGenOp; RunHeuristics.operators.add(newGenTmp2); RunHeuristics.operators.add(newGenOp); root = new Operator(getNextInternalNum(), newOperatorId, app, father); root.operators.add(child1); root.operators.add(child2); child1.father = root; child2.father = root; if(!reuse) { MyArrayList newOperatorList = new MyArrayList(); Operator rootTree2 = new Operator(getNextInternalNum(),getNextInternalNum(),root.app,father); for(Operator tmpChild : newGenOp.operators) { traverseTreeForOperatorsNoReuse(tmpChild,rootTree2,app, newOperatorList); } applications.add(rootTree2); RunHeuristics.operators.add(rootTree2); } else { System.out.println("tree 2: root: "+root+", child 1: "+child1+", child 2: "+child2); applications.add(root); } } return applications; } public static void assignObjectsToProcs(ArrayList objects, ArrayList procs, Random random) { int numCopies, procPos; Proc actProc; for(BasicObject obj : objects) { numCopies = random.nextInt(2)+1; for(int i = 0; i < numCopies; i++) { procPos = random.nextInt(procs.size()); actProc = procs.get(procPos); if(!actProc.holds.contains(obj)) actProc.holds.add(obj); } } for(Proc proc : procs) { if(CompileTimeCondition.DEBUG) System.out.println(proc.toStringAll()); } } }