import java.util.*; import java.io.*; public class RunHeuristics { /* strategy */ static final int FASTEST = 0; /* ohne zuruecklegen */ static final int NETWORK_CARD = 1; /* ohne zuruecklegen */ static final int REMAINING_BW = 2; /* mit zureucklegen */ static final int REMAINING_CPU = 3; /* mit zuruecklegen */ /* heuristics */ static final int TOP_DOWN_BFS = 1; static final int TOP_DOWN_DFS = 2; static final int BOTTOM_UP_BFS = 3; static final int BOTTOM_UP_DFS = 4; static final int RANDOM = 5; /* with reuse */ static final int RANDOM_SIMPLE = 6; /* no reuse */ public static MyArrayList operators; public static ArrayList objects; // private static ArrayList usedProcs; public static ArrayList procs; //public static int[] treeSizes; public static void init(int numProcs, int numObjects, int numOperators, Random random, int bound, double ccrIndex) { procs = PlatformCreator.createPlatform(numProcs, random); objects = PlatformCreator.createObjects(numObjects, random); operators = PlatformCreator.createOperators(numOperators, objects, random, bound, ccrIndex); } public static void fillList(Operator root, MyArrayList list) { list.add(root); for(Operator op : root.operators) { fillList(op, list); } } public static void main(String[] args) { int similarity=0; /* only used within exp5 */ int realSimilarity =0; /* default values */ int numProcs = 15; int numObjects = 10; int numOperators = 100;//20;//100; int bound = 20;//5;//20; // todo: mit bound = 5 testen, das gibt bisher error!! double ccrIndex=1.0; /* bound indicates the last #bound operators, which will have at least one operator as child */ int randomBase = 1; /*default random Base */ int numApplications = 4; int exp = 0; /* expID */ int range = 0; /* act value in the expID */ /* eg. exp 1, range 10: in this exp, #Apps varies, actual runs have 10 apps */ int applicationSize = 50; /* values to be specified */ int strategy; int heuristic; boolean reuse; /* indicates wheather operators have all different ids to avoid reuse or not */ int runs; /* number of different random platforms and applications */ System.out.println(CompileTimeCondition.DEBUG+"\n"); if(CompileTimeCondition.DEBUG) System.out.println("in the condition"); if(args.length < 4) { System.out.println("Abort!\n"+ "Default:\n"+ "Arguments: heuristic strategy runs reuse\n"); System.out.println("or with specific values:\n"+ "Arguments: heuristic strategy runs reuse "+ "numProcs numObjects numOperators bound numApplications "+ "exp range\n"); //"Arguments: heuristic strategy randomBase\n"); return; } // System.out.println(args.length); if(args.length > 4 && args.length < 11) { System.out.println("Abort!\n"+ "Arguments: heuristic strategy runs reuse "+ "numProcs numObjects numOperators bound numApplications "+ "exp range\n"); //"Arguments: heuristic strategy randomBase\n"); return; } //int heuristics = 6;/* the number of heuristis */ int heuristics = 6;/* the number of heuristis */ //int heuristics = 1; int costs[] = new int[heuristics]; heuristic = Integer.parseInt(args[0]); strategy = Integer.parseInt(args[1]); //randomBase = Integer.parseInt(args[2]); runs = Integer.parseInt(args[2]); //runs = 1; reuse = Boolean.parseBoolean(args[3]); System.out.println(reuse); //if(reuse) return; if(args.length > 4) { numProcs = Integer.parseInt(args[4]); numObjects = Integer.parseInt(args[5]); numOperators = Integer.parseInt(args[6]); bound = Integer.parseInt(args[7]); numApplications = Integer.parseInt(args[8]); exp = Integer.parseInt(args[9]); range = Integer.parseInt(args[10]); } if(exp == 3 || exp == 13) { /* application size varies */ applicationSize = range; } if(exp == 4 || exp == 14) { /* CCR varies , minCCR > 0.5 */ ccrIndex= (double)range/10.0; } if(exp == 5 || exp == 15) { /* similarity of 2 applications */ similarity = range; numApplications = 2; System.out.println("similarity: "+similarity); } //treeSizes = new int[numApplications]; System.out.println("numProcs: "+numProcs+", numObjects: "+numObjects+ ", numOperators: "+numOperators+", bound: "+bound+ "\nnumApplications: "+numApplications+", exp: "+exp+", range: "+range+"\n"); try{ PrintWriter writer; writer = new PrintWriter(new FileWriter("forPlots/exp"+exp+"/strategy-"+strategy+"-"+range)); for(int r = 1; r<= runs; r++) { int maxruns = 50; //randomBase = maxruns*r+exp; randomBase = maxruns*r; // heuristics = 1; for (int h = 1; h <= heuristics; h++) { heuristic = h; System.out.println("\n###################################"); System.out.println("## run "+r+", heuristic "+heuristic+", strategy "+strategy+" ##"); System.out.println("###################################\n"); Random rand = new Random(randomBase); init(numProcs, numObjects, numOperators, rand, bound, ccrIndex); MyArrayList applications = new MyArrayList(); double[] frequency = new double[numApplications+1]; frequency[0]= 0.0; double[] rho = new double[numApplications+1]; rho[0]= 0.0; if(CompileTimeCondition.DEBUG) System.out.println("rho[0]: "+rho[0]+", f[0]: "+frequency[0]); for(int i = 1; i<= numApplications; i++) { frequency[i] = rand.nextDouble(); if(frequency[i] == 0.0) frequency[i] = 0.5; rho[i] = 1.0 + rand.nextDouble(); if(CompileTimeCondition.DEBUG) System.out.println("rho["+i+"]: "+rho[i]+", f["+i+"]: "+frequency[i]); } CommonFunctions.setRhoAndFrequency(rho, frequency); /* this can be used when we do not care about different application sizes */ // for(int i = 1; i<= numApplications; i++) { // int appli = i; // //randomBase = 10 * i; // Operator root = PlatformCreator.createApplication(appli, rand, operators, bound,reuse); // System.out.println("next application root: "+root); // applications.add(root); // } /* this is used when we want to have applications of a fixed size */ int loopcounter =0; if(exp == 5 || exp == 15) { //applicationSize = 20; int appSize1 = applicationSize; int appSize2 = 0; appSize1 = applicationSize - similarity; if (similarity >= 2) { appSize2 = similarity - 1; } System.out.println("appSize1: "+appSize1+", appSize2: "+appSize2); while(applications.size() == 0) { applications = PlatformCreator.createApplicationsOfFixedSizeWithFixedSimilarity(numApplications, applicationSize, appSize1, appSize2, rand, operators,reuse, similarity); int changeRand = rand.nextInt(); loopcounter++; if(loopcounter == 100) { rand = new Random(randomBase*r+exp); init(numProcs, numObjects, numOperators, rand, bound, ccrIndex); loopcounter =0; System.out.println("new Random"); } } } else { while(applications.size() == 0) { applications = PlatformCreator.createApplicationsWithFixedSize(numApplications, applicationSize, rand, operators,reuse); int changeRand = rand.nextInt(); loopcounter++; if(loopcounter == 100) { rand = new Random(randomBase*r+exp); init(numProcs, numObjects, numOperators, rand, bound, ccrIndex); loopcounter =0; System.out.println("new Random"); } } } if(exp == 5 || exp == 15) { /* application similarity, only 2 applications are used!! */ if(applications.size() < 2) { System.out.println("hey, you did not create enoough applications! you need 2 of them!!"); return; } MyArrayList app1 = new MyArrayList(); MyArrayList app2 = new MyArrayList(); fillList(applications.get(0), app1); fillList(applications.get(1), app2); realSimilarity = SimilarityTester.compare2to2(app1, app2); System.out.println("real Similarity: "+realSimilarity); //writer.println(similarity+"\t"); } PlatformCreator.assignObjectsToProcs(objects, procs, rand); try{ PrintWriter platformWriter = new PrintWriter(new FileWriter("platforms/run-"+r)); for(Proc proc : procs) platformWriter.println(proc.toStringAll()); platformWriter.close(); } catch (IOException e) { System.err.println(e.getMessage()); } try{ PrintWriter appliWriter = new PrintWriter(new FileWriter("applis/run-"+r)); for(Operator root : applications) { System.out.println("\nApplication "+root.app+":"); appliWriter.println("\nApplication "+root.app+":"); PlatformCreator.traverseTree(root, appliWriter); CommonFunctions.traverseTree(root); } appliWriter.close(); } catch (IOException e) { System.out.println(e.getMessage()); } // System.out.println("\noperators:"); // for(Operator op : operators) { // System.out.println(op.toStringParams()); // } // System.out.println("\nprocs sorted by ratios cpu/bw:"); // QuickSorter.sortRatioBW(procs); // for(Proc proc : procs) { // System.out.println(proc+" (cpu: "+proc.cpu+", bw: "+proc.bw+", cost: "+proc.cost+"), ratio: "+(proc.bw/proc.cost)); // } //boolean result = topDownDFS(applications, strategy, procs); //boolean result = false; Mapping mapping = new Mapping(); switch(heuristic) { case TOP_DOWN_BFS: System.out.println("calling TopDownBFS"); mapping = TopDownBFS.topDownBFS(applications, strategy, procs); break; case TOP_DOWN_DFS: System.out.println("calling TopDownDFS"); mapping = TopDownDFS.topDownDFS(applications, strategy, procs); break; case BOTTOM_UP_BFS: System.out.println("calling BottomUpBFS"); mapping = BottomUpBFS.bottomUpBFS(applications, strategy, procs); break; case BOTTOM_UP_DFS: System.out.println("calling BottomUpDFS"); mapping = BottomUpDFS.bottomUpDFS(applications, strategy, procs); break; case RANDOM: System.out.println("calling RandomIntelligent"); mapping = RandomIntelligent.randomIntelligent(applications, strategy, procs, rand); break; case RANDOM_SIMPLE: System.out.println("calling RandomSimple"); mapping = RandomSimple.randomSimple(applications, strategy, procs, rand); break; } costs[heuristic-1] = 0; //costs[0] = 0; if(!mapping.validMapping) { System.out.println("Mapping ws not possible"); //printMapping(); } else { System.out.println("Mapping was successful"); if(CompileTimeCondition.BIG_OUTPUT) CommonFunctions.printMapping(mapping); System.out.println("run: "+r+", heuristic: "+heuristic+", strategy: "+strategy); boolean passedCheck = MappingTester.verifyMapping(mapping.usedProcs, operators); int cost = CommonFunctions.computeMappingCost(); System.out.println("\nCost of this mapping: "+cost); if(passedCheck) costs[heuristic-1] = cost; //costs[0]=cost; } } /* end for heuristics */ // if(exp == 5) // writer.print(similarity+"\t"); for(int i = 0; i< (heuristics-1); i++) writer.print(costs[i]+"\t"); // if(exp == 5) // writer.println(costs[heuristics-1]+"\t"+realSimilarity); // else writer.println(costs[heuristics-1]); } /* end for runs */ writer.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } }