public String toString() { StringBuffer buf = new StringBuffer("{RG "); int len = refs.size(); if (len > 3) len = 3; for (int i = 0; i < len; i++) { if (i > 0) buf.append(','); buf.append(refs.elementAt(i)); } if (len < refs.size()) buf.append(", ..."); buf.append('}'); return buf.toString(); }
private Cost computeRefCostSum(LoopHeaderChord loop, Vector<RefGroup> refGroups) { Cost lc = new Cost(); int l = refGroups.size(); for (int i = 0; i < l; i++) { RefGroup rg = refGroups.elementAt(i); SubscriptExpr se = rg.getRepresentative(); Cost cost = computeRefCost(loop, se); if (cost == null) continue; lc.add(cost); } return lc; }
/** * Create a representation of a range from min to max. * * @param min is the lower bound * @param max is the upper bound */ public static Bound create(Expression min, Expression max) { if (max == null) { if (min == null) return noBound; max = LiteralMap.put(0x7ffffff, Machine.currentMachine.getIntegerCalcType()); } if (bounds != null) { int n = bounds.size(); for (int i = 0; i < n; i++) { Bound ta = bounds.elementAt(i); if (!min.equivalent(ta.min)) continue; if (!max.equivalent(ta.max)) continue; return ta; } } Bound a = new Bound(min, max); return a; }
public void perform() { if (trace) System.out.println("** LP " + scribble.getRoutineDecl().getName()); LoopHeaderChord lt = scribble.getLoopTree(); Vector<LoopHeaderChord> innerLoops = lt.getInnerLoops(); for (int li = 0; li < innerLoops.size(); li++) { LoopHeaderChord loop = innerLoops.elementAt(li); InductionVar ivar = loop.getPrimaryInductionVar(); if (trace) System.out.println(" lp " + loop.nestedLevel() + " " + loop); if ((ivar == null) || !loop.isPerfectlyNested()) { innerLoops.addVectors(loop.getInnerLoops()); continue; } if (loop.nestedLevel() < 2) // no need to check permutation for a simple nest continue; tryPermute(loop); } }
private void tryPermute(LoopHeaderChord topLoop) { Vector<LoopHeaderChord> loopNest = topLoop.getTightlyNestedLoops(); if (loopNest == null) return; int loopDepth = loopNest.size(); LoopHeaderChord bottom = loopNest.get(loopDepth - 1); if (!unsafe && !legalLoop(bottom)) return; // Set the loop costs for the loops in a loop nest. The cost for a loop is // the cost of executing the nest with that loop in the innermost nesting. Table<Declaration, SubscriptExpr> arrayRefs = new Table<Declaration, SubscriptExpr>(); if (!topLoop.getSubscriptsRecursive(arrayRefs)) return; graph = topLoop.getDDGraph(false); if (graph == null) return; if (trace) System.out.println(" " + graph); int[] loopIndex = new int[loopDepth]; Cost[] loopCostList = new Cost[loopDepth]; Vector<RefGroup> refGroups = new Vector<RefGroup>(20); for (int i = 0; i < loopDepth; i++) { LoopHeaderChord loop = loopNest.elementAt(i); if (trace) System.out.println(" " + i + " " + loop); computeRefGroups(loop.getNestedLevel(), 2, 2, arrayRefs, refGroups); Cost lc = computeRefCostSum(loop, refGroups); if (trace) System.out.println(" " + i + " " + lc); loopCostList[i] = lc; loopIndex[i] = i; // the outtermost loop is at position 0 Cost tp = tripProduct(loopNest, loop); if (trace) System.out.println(" " + i + " " + tp); lc.multiply(tp); if (trace) System.out.println(" " + i + " " + lc); } boolean permuted = sortByCost(loopCostList, loopIndex); if (!permuted) return; if (trace) { System.out.print(" permute " + loopDepth); System.out.print(":"); for (int i = 0; i < loopIndex.length; i++) System.out.print(" " + loopIndex[i]); System.out.println(""); } int[][] ddVec = getDDVec(arrayRefs, loopDepth); if (trace) printDDInfo(ddVec, loopDepth); if (!isLegal(loopIndex, ddVec)) return; if (trace) System.out.println(" permute " + loopDepth); int[] rank = new int[loopDepth]; // We will do sorting on the rank vector, which corresponds to the interchange we need. for (int i = 0; i < loopDepth; i++) { int loopNum = loopIndex[i]; rank[loopNum] = i; } if (trace) printOrder(rank); boolean changed = true; while (changed) { changed = false; for (int i = 0; i < loopDepth - 1; i++) { int j = i + 1; int outerRank = rank[i]; int innerRank = rank[j]; if (innerRank >= outerRank) continue; LoopHeaderChord innerLoop = loopNest.elementAt(j); LoopHeaderChord outerLoop = loopNest.elementAt(i); if (!outerLoop.isDDComplete() || outerLoop.inhibitLoopPermute()) continue; if (!innerLoop.isDDComplete() || innerLoop.inhibitLoopPermute()) continue; changed = true; rank[i] = innerRank; rank[j] = outerRank; loopNest.setElementAt(innerLoop, i); loopNest.setElementAt(outerLoop, j); performLoopInterchange(innerLoop, outerLoop); } } }
/** * Compute the reference groups for this loop. Two array references are in the same group with * respect to this loop if - there is a loop-independent dependence between them, or - the * dependence distance(dependence vector entry dl) for this loop is less than some constant * *dist*, and all other dependence vector entries are 0. - the two array refer to the same array * and differ by at most *dist2* in the first subscript dimension, where d is less than or equal * to the cache line size in terms of array elements. All other subscripts must be identical. * Notes: Here we assume dist1 = dist2 = 2 */ private void computeRefGroups( int level, int dist1, int dist2, Table<Declaration, SubscriptExpr> arrayRefs, Vector<RefGroup> refGroups) { if (arrayRefs == null) return; Enumeration<Declaration> ek = arrayRefs.keys(); while (ek.hasMoreElements()) { VariableDecl vd = (VariableDecl) ek.nextElement(); String s = vd.getName(); // array name Object[] v = arrayRefs.getRowArray(vd); // vector of SubscriptExpr's int vi = v.length; for (int j = vi - 1; j >= 0; j--) { SubscriptExpr sr = (SubscriptExpr) v[j]; Vector<LoopHeaderChord> allRelatedLoops = sr.allRelatedLoops(); // ** Incorrect when something like a[(j+i][j] int arls = allRelatedLoops.size(); int firstsub = arls - 1; // ** Making an invalid assumption here // Process the list of references r' with which r has a data // dependence, and r is the source(data flows from r to r'). RefGroup rg = new RefGroup(s, sr); Object[] edges = graph.getEdges(sr); int len = edges.length; for (int i = 0; i < len; i++) { DDEdge edge = (DDEdge) edges[i]; if (edge.isSpatial()) continue; // Condition(1)-(a) in McKinley's paper if (edge.isLoopIndependentDependency()) { // add rP to the RefGroup of r: rg.add(edge); continue; } // Condition(1)-(b) in McKinley's paper computeEdgeRefs(edge, rg, level, dist1); if (arls <= 0) continue; // Condition(2) in McKinley's paper // rlevel is the level of the loop related to the first subscript. int rlevel = allRelatedLoops.elementAt(firstsub).getNestedLevel(); computeEdgeRefs(edge, rg, rlevel, dist2); } boolean isInExistingRefGroups = false; int rgl = refGroups.size(); for (int i = 0; i < rgl; i++) { RefGroup rg2 = refGroups.elementAt(i); if (!rg2.getName().equals(s)) continue; isInExistingRefGroups = rg2.contains(rg); if (isInExistingRefGroups) { rg2.add(rg); break; } } if (!isInExistingRefGroups) refGroups.addElement(rg); } } }
private void printRefGroups(Vector<RefGroup> refGroups) { for (int i = 0; i < refGroups.size(); i++) { RefGroup rg = refGroups.elementAt(i); System.out.println(rg); } }
public SubscriptExpr getRepresentative() { if (refs.size() > 0) return (SubscriptExpr) refs.elementAt(0); return null; }
public void doIt() { CacheHolderDetail det; CacheHolder ch; ProgressBarForm pbf = new ProgressBarForm(); Handle h = new Handle(); int exportErrors = 0; new String(); FileChooser fc = new FileChooser(FileChooserBase.DIRECTORY_SELECT, pref.getExportPath(expName)); fc.setTitle("Select target directory:"); String targetDir; if (fc.execute() != FormBase.IDCANCEL) { targetDir = fc.getChosen() + "/"; pref.setExportPath(expName, targetDir); Vector cache_index = new Vector(); Vector cacheImg = new Vector(); Vector logImg = new Vector(); Vector mapImg = new Vector(); Vector usrImg = new Vector(); Vector logIcons = new Vector(15); String icon; Hashtable varParams; Hashtable imgParams; Hashtable logImgParams; Hashtable usrImgParams; Hashtable mapImgParams; // Generate index page int counter = cacheDB.countVisible(); pbf.showMainTask = false; pbf.setTask(h, "Exporting ..."); pbf.exec(); for (int i = 0; i < counter; i++) { h.progress = (float) (i + 1) / (float) counter; h.changed(); ch = cacheDB.get(i); if (ch.isVisible()) { if (ch.is_incomplete()) { exportErrors++; Global.getPref() .log("HTMLExport: skipping export of incomplete waypoint " + ch.getWayPoint()); continue; } det = ch.getCacheDetails(false, false); varParams = new Hashtable(); varParams.put("TYPE", CacheType.cw2ExportString(ch.getType())); varParams.put("WAYPOINT", ch.getWayPoint()); varParams.put("NAME", ch.getCacheName()); varParams.put("OWNER", ch.getCacheOwner()); if (ch.isAddiWpt() || CacheType.CW_TYPE_CUSTOM == ch.getType()) { varParams.put("SIZE", ""); varParams.put("DIFFICULTY", ""); varParams.put("TERRAIN", ""); } else { varParams.put( "SIZE", CacheSize.isValidSize(ch.getCacheSize()) ? CacheSize.cw2ExportString(ch.getCacheSize()) : ""); varParams.put( "DIFFICULTY", CacheTerrDiff.isValidTD(ch.getHard()) ? CacheTerrDiff.longDT(ch.getHard()) : ""); varParams.put( "TERRAIN", CacheTerrDiff.isValidTD(ch.getTerrain()) ? CacheTerrDiff.longDT(ch.getTerrain()) : ""); } varParams.put("DISTANCE", ch.getDistance()); varParams.put("BEARING", ch.bearing); varParams.put("LATLON", ch.LatLon); varParams.put("STATUS", ch.getCacheStatus()); varParams.put("DATE", ch.getDateHidden()); cache_index.add(varParams); // We can generate the individual page here! try { Template page_tpl = new Template(template_init_page); page_tpl.setParam("TYPE", varParams.get("TYPE").toString()); page_tpl.setParam("SIZE", varParams.get("SIZE").toString()); page_tpl.setParam("WAYPOINT", ch.getWayPoint()); page_tpl.setParam("NAME", ch.getCacheName()); page_tpl.setParam("OWNER", ch.getCacheOwner()); page_tpl.setParam("DIFFICULTY", varParams.get("DIFFICULTY").toString()); page_tpl.setParam("TERRAIN", varParams.get("TERRAIN").toString()); page_tpl.setParam("DISTANCE", ch.getDistance()); page_tpl.setParam("BEARING", ch.bearing); page_tpl.setParam("LATLON", ch.LatLon); page_tpl.setParam("STATUS", ch.getCacheStatus()); page_tpl.setParam("DATE", ch.getDateHidden()); if (det != null) { if (ch.is_HTML()) { page_tpl.setParam("DESCRIPTION", modifyLongDesc(det, targetDir)); } else { page_tpl.setParam( "DESCRIPTION", STRreplace.replace(det.LongDescription, "\n", "<br>")); } page_tpl.setParam("HINTS", det.Hints); page_tpl.setParam("DECRYPTEDHINTS", Common.rot13(det.Hints)); StringBuffer sb = new StringBuffer(2000); for (int j = 0; j < det.CacheLogs.size(); j++) { sb.append( STRreplace.replace( det.CacheLogs.getLog(j).toHtml(), "http://www.geocaching.com/images/icons/", null)); sb.append("<br>"); icon = det.CacheLogs.getLog(j).getIcon(); if (logIcons.find(icon) < 0) logIcons.add(icon); // Add the icon to list of icons to copy to dest directory } page_tpl.setParam("LOGS", sb.toString()); page_tpl.setParam("NOTES", STRreplace.replace(det.getCacheNotes(), "\n", "<br>")); cacheImg.clear(); for (int j = 0; j < det.images.size(); j++) { imgParams = new Hashtable(); String imgFile = new String(det.images.get(j).getFilename()); imgParams.put("FILE", imgFile); imgParams.put("TEXT", det.images.get(j).getTitle()); if (DataMover.copy(profile.dataDir + imgFile, targetDir + imgFile)) cacheImg.add(imgParams); else exportErrors++; } page_tpl.setParam("cacheImg", cacheImg); // Log images logImg.clear(); for (int j = 0; j < det.logImages.size(); j++) { logImgParams = new Hashtable(); String logImgFile = det.logImages.get(j).getFilename(); logImgParams.put("FILE", logImgFile); logImgParams.put("TEXT", det.logImages.get(j).getTitle()); if (DataMover.copy(profile.dataDir + logImgFile, targetDir + logImgFile)) logImg.add(logImgParams); else exportErrors++; } page_tpl.setParam("logImg", logImg); // User images usrImg.clear(); for (int j = 0; j < det.userImages.size(); j++) { usrImgParams = new Hashtable(); String usrImgFile = new String(det.userImages.get(j).getFilename()); usrImgParams.put("FILE", usrImgFile); usrImgParams.put("TEXT", det.userImages.get(j).getTitle()); if (DataMover.copy(profile.dataDir + usrImgFile, targetDir + usrImgFile)) usrImg.add(usrImgParams); else exportErrors++; } page_tpl.setParam("userImg", usrImg); // Map images mapImg.clear(); mapImgParams = new Hashtable(); String mapImgFile = new String(ch.getWayPoint() + "_map.gif"); // check if map file exists File test = new File(profile.dataDir + mapImgFile); if (test.exists()) { mapImgParams.put("FILE", mapImgFile); mapImgParams.put("TEXT", mapImgFile); if (DataMover.copy(profile.dataDir + mapImgFile, targetDir + mapImgFile)) mapImg.add(mapImgParams); else exportErrors++; mapImgParams = new Hashtable(); mapImgFile = ch.getWayPoint() + "_map_2.gif"; mapImgParams.put("FILE", mapImgFile); mapImgParams.put("TEXT", mapImgFile); if (DataMover.copy(profile.dataDir + mapImgFile, targetDir + mapImgFile)) mapImg.add(mapImgParams); else exportErrors++; page_tpl.setParam("mapImg", mapImg); } } else { page_tpl.setParam("DESCRIPTION", ""); page_tpl.setParam("HINTS", ""); page_tpl.setParam("DECRYPTEDHINTS", ""); page_tpl.setParam("LOGS", ""); page_tpl.setParam("NOTES", ""); page_tpl.setParam("cacheImg", cacheImg); page_tpl.setParam("logImg", ""); // ??? page_tpl.setParam("userImg", ""); // ??? page_tpl.setParam("mapImg", ""); // ??? exportErrors++; } PrintWriter pagefile = new PrintWriter( new BufferedWriter(new FileWriter(targetDir + ch.getWayPoint() + ".html"))); pagefile.print(page_tpl.output()); pagefile.close(); } catch (IllegalArgumentException e) { exportErrors++; ch.setIncomplete(true); Global.getPref() .log( "HTMLExport: " + ch.getWayPoint() + " is incomplete reason: ", e, Global.getPref().debug); } catch (Exception e) { exportErrors++; Global.getPref() .log( "HTMLExport: error wehen exporting " + ch.getWayPoint() + " reason: ", e, Global.getPref().debug); } } // if is black, filtered } // Copy the log-icons to the destination directory for (int j = 0; j < logIcons.size(); j++) { icon = (String) logIcons.elementAt(j); if (!DataMover.copy(FileBase.getProgramDirectory() + "/" + icon, targetDir + icon)) exportErrors++; } if (!DataMover.copy( FileBase.getProgramDirectory() + "/recommendedlog.gif", targetDir + "recommendedlog.gif")) exportErrors++; try { Template tpl = new Template(template_init_index); tpl.setParam("cache_index", cache_index); PrintWriter detfile; detfile = new PrintWriter(new BufferedWriter(new FileWriter(targetDir + "/index.html"))); detfile.print(tpl.output()); detfile.close(); // sort by waypoint sortAndPrintIndex(tpl, cache_index, targetDir + "/index_wp.html", "WAYPOINT"); // sort by name sortAndPrintIndex(tpl, cache_index, targetDir + "/index_alpha.html", "NAME", false); // sort by type sortAndPrintIndex(tpl, cache_index, targetDir + "/index_type.html", "TYPE", true); // sort by size sortAndPrintIndex(tpl, cache_index, targetDir + "/index_size.html", "SIZE", true); // sort by distance sortAndPrintIndex(tpl, cache_index, targetDir + "/index_dist.html", "DISTANCE", 10.0); } catch (Exception e) { Vm.debug("Problem writing HTML files\n"); e.printStackTrace(); } // try } // if pbf.exit(0); if (exportErrors > 0) { new MessageBox( "Export Error", exportErrors + " errors during export. See log for details.", FormBase.OKB) .execute(); } }