|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Boston University (BU) |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package org.cellocad.v2.technologyMapping.algorithm.SimulatedAnnealing.data.feature; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.regex.Matcher; |
| 30 | +import java.util.regex.Pattern; |
| 31 | +import org.apache.commons.lang3.StringUtils; |
| 32 | +import org.cellocad.v2.common.CObjectCollection; |
| 33 | +import org.cellocad.v2.common.target.data.TargetDataInstance; |
| 34 | +import org.cellocad.v2.common.target.data.data.DeviceRules; |
| 35 | +import org.cellocad.v2.common.target.data.data.Input; |
| 36 | +import org.cellocad.v2.common.target.data.data.Part; |
| 37 | +import org.cellocad.v2.common.target.data.data.StructureDevice; |
| 38 | +import org.cellocad.v2.common.target.data.data.StructureObject; |
| 39 | +import org.cellocad.v2.common.target.data.data.StructureTemplate; |
| 40 | +import org.cellocad.v2.results.netlist.Netlist; |
| 41 | +import org.cellocad.v2.results.netlist.NetlistEdge; |
| 42 | +import org.cellocad.v2.results.netlist.NetlistNode; |
| 43 | +import org.cellocad.v2.technologyMapping.common.TMUtils; |
| 44 | + |
| 45 | +/** |
| 46 | + * Tests for roadblocking. |
| 47 | + * |
| 48 | + * @author Timothy Jones |
| 49 | + * @date 2020-06-07 |
| 50 | + */ |
| 51 | +public class RoadBlockUtils { |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets the number of roadblocked nodes in a netlist. |
| 55 | + * |
| 56 | + * @param netlist The netlist. |
| 57 | + * @return The number of roadblocked nodes in a netlist. |
| 58 | + */ |
| 59 | + public static Integer getNumberOfRoadBlockedNodes( |
| 60 | + final Netlist netlist, final DeviceRules rules, final TargetDataInstance tdi) { |
| 61 | + Integer rtn = 0; |
| 62 | + for (int i = 0; i < netlist.getNumVertex(); i++) { |
| 63 | + NetlistNode node = netlist.getVertexAtIdx(i); |
| 64 | + if (isNodeRoadBlocked(node, rules, tdi)) { |
| 65 | + rtn++; |
| 66 | + } |
| 67 | + } |
| 68 | + return rtn; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Tests whether any node in the given netlist has a roadblocking set of inputs. |
| 73 | + * |
| 74 | + * @param netlist The netlist. |
| 75 | + * @return Whether any node in the given netlist has a roadblocking set of inputs. |
| 76 | + */ |
| 77 | + public static Boolean isNetlistRoadBlocked( |
| 78 | + final Netlist netlist, final DeviceRules rules, final TargetDataInstance tdi) { |
| 79 | + Boolean rtn = false; |
| 80 | + for (int i = 0; i < netlist.getNumVertex(); i++) { |
| 81 | + NetlistNode node = netlist.getVertexAtIdx(i); |
| 82 | + rtn = isNodeRoadBlocked(node, rules, tdi); |
| 83 | + if (rtn) { |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + return rtn; |
| 88 | + } |
| 89 | + |
| 90 | + private static void addDownstreamNodesToCollection( |
| 91 | + final NetlistNode node, Collection<NetlistNode> nodes) { |
| 92 | + for (int i = 0; i < node.getNumOutEdge(); i++) { |
| 93 | + NetlistEdge e = node.getOutEdgeAtIdx(i); |
| 94 | + NetlistNode dst = e.getDst(); |
| 95 | + nodes.add(dst); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Tests whether a node as well as its downstream neighbors are roadblocked. |
| 101 | + * |
| 102 | + * @param node The node. |
| 103 | + * @param rules The rules. |
| 104 | + * @param tdi The target data instance. |
| 105 | + * @return Whether a node as well as its downstream neighbors are roadblocked. |
| 106 | + */ |
| 107 | + public static Boolean areNodeAndDownstreamNeighborsRoadBlocked( |
| 108 | + final NetlistNode node, final DeviceRules rules, final TargetDataInstance tdi) { |
| 109 | + Boolean rtn = false; |
| 110 | + Collection<NetlistNode> nodes = new ArrayList<>(); |
| 111 | + nodes.add(node); |
| 112 | + addDownstreamNodesToCollection(node, nodes); |
| 113 | + for (NetlistNode n : nodes) { |
| 114 | + if (isNodeRoadBlocked(n, rules, tdi)) { |
| 115 | + rtn = true; |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + return rtn; |
| 120 | + } |
| 121 | + |
| 122 | + private static Boolean isRoadBlockable(final StructureDevice device) { |
| 123 | + Boolean rtn = false; |
| 124 | + Integer consecutiveInputsSeen = 0; |
| 125 | + // TODO Lazy implementation. This assumes all inputs are at the beginning. |
| 126 | + for (StructureObject obj : device.getComponents()) { |
| 127 | + if (obj instanceof StructureTemplate) { |
| 128 | + consecutiveInputsSeen++; |
| 129 | + } else { |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + if (consecutiveInputsSeen > 1) { |
| 134 | + rtn = true; |
| 135 | + } |
| 136 | + return rtn; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Tests whether a node has a roadblocking set of inputs. |
| 141 | + * |
| 142 | + * @param node The node. |
| 143 | + * @param rules The rules. |
| 144 | + * @param tdi The target data instance. |
| 145 | + * @return Whether a node has a roadblocking set of inputs. |
| 146 | + */ |
| 147 | + public static Boolean isNodeRoadBlocked( |
| 148 | + final NetlistNode node, final DeviceRules rules, final TargetDataInstance tdi) { |
| 149 | + Boolean rtn = false; |
| 150 | + if (node.getNumInEdge() < 2) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + Collection<StructureDevice> devices = |
| 154 | + node.getResultNetlistNodeData().getDevice().getStructure().getDevices(); |
| 155 | + Map<Input, Part> inputMap = TMUtils.getInputs(node, tdi); |
| 156 | + for (StructureDevice device : devices) { |
| 157 | + if (!isRoadBlockable(device)) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + List<Input> inputs = new ArrayList<>(); |
| 161 | + for (StructureObject obj : device.getComponents()) { |
| 162 | + if (obj instanceof StructureTemplate) { |
| 163 | + for (Input input : inputMap.keySet()) { |
| 164 | + if (input.getName().equals(obj.getName())) { |
| 165 | + inputs.add(input); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + CObjectCollection<Part> parts = new CObjectCollection<>(); |
| 171 | + parts.addAll(inputMap.values()); |
| 172 | + String rule = rules.filter(device, parts); |
| 173 | + if (StringUtils.countMatches(rule, "STARTSWITH") > 1) { |
| 174 | + return true; |
| 175 | + } |
| 176 | + for (int i = 1; i < inputs.size(); i++) { |
| 177 | + Input input = inputs.get(i); |
| 178 | + Pattern r = Pattern.compile("STARTSWITH " + inputMap.get(input).getName()); |
| 179 | + Matcher m = r.matcher(rule); |
| 180 | + if (m.matches()) { |
| 181 | + return true; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + return rtn; |
| 186 | + } |
| 187 | +} |
0 commit comments