Skip to content

Commit c3dbd62

Browse files
author
Timothy Jones
committed
ThresholdUtils and RoadBlockUtils.
1 parent 0872f7f commit c3dbd62

2 files changed

Lines changed: 345 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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.Collection;
26+
import org.cellocad.v2.common.exception.CelloException;
27+
import org.cellocad.v2.common.exception.NotImplementedException;
28+
import org.cellocad.v2.common.target.data.data.FixedParameter;
29+
import org.cellocad.v2.common.target.data.data.Function;
30+
import org.cellocad.v2.common.target.data.data.FunctionType;
31+
import org.cellocad.v2.common.target.data.data.Model;
32+
import org.cellocad.v2.common.target.data.data.Variable;
33+
import org.cellocad.v2.results.logicSynthesis.LSResultsUtils;
34+
import org.cellocad.v2.results.logicSynthesis.logic.LSLogicEvaluation;
35+
import org.cellocad.v2.results.logicSynthesis.logic.truthtable.State;
36+
import org.cellocad.v2.results.logicSynthesis.logic.truthtable.TruthTable;
37+
import org.cellocad.v2.results.netlist.Netlist;
38+
import org.cellocad.v2.results.netlist.NetlistNode;
39+
import org.cellocad.v2.results.technologyMapping.activity.TMActivityEvaluation;
40+
import org.cellocad.v2.results.technologyMapping.activity.activitytable.Activity;
41+
import org.cellocad.v2.results.technologyMapping.activity.activitytable.ActivityTable;
42+
43+
/**
44+
* Checks whether all input levels are within ON/OFF thresholds specified by gates.
45+
*
46+
* @author Timothy Jones
47+
* @date 2020-07-01
48+
*/
49+
public class ThresholdUtils {
50+
51+
/**
52+
* Get the number of nodes with activity outputs that violate the thresholds (if specified).
53+
*
54+
* @param netlist A netlist.
55+
* @param tmae The activity evaluation of the netlist.
56+
* @param lsle The logic evaluation of the netlist.
57+
* @return The number of nodes with activity outputs that violate the thresholds (if specified).
58+
* @throws CelloException Unable to get thresholds.
59+
*/
60+
public static Integer getNumberNodesViolatingThreshold(
61+
final Netlist netlist, final TMActivityEvaluation tmae, final LSLogicEvaluation lsle)
62+
throws CelloException {
63+
Integer rtn = 0;
64+
for (int i = 0; i < netlist.getNumVertex(); i++) {
65+
NetlistNode node = netlist.getVertexAtIdx(i);
66+
if (LSResultsUtils.isAllInput(node) || LSResultsUtils.isAllOutput(node)) {
67+
continue;
68+
}
69+
Boolean b =
70+
inputsWithinThreshold(node, tmae.getActivityTable(node), lsle.getTruthTable(node));
71+
if (!b) {
72+
rtn++;
73+
}
74+
}
75+
return rtn;
76+
}
77+
78+
/**
79+
* Checks whether input levels for ON (OFF) states are above (below) the gates ON (OFF) threshold,
80+
* if specified.
81+
*
82+
* @param netlist A netlist.
83+
* @param tmae The activity evaluation of the netlist.
84+
* @param lsle The logic evaluation of the netlist.
85+
* @return Whether input levels for ON (OFF) states are above (below) the gates ON (OFF)
86+
* threshold, if specified.
87+
* @throws CelloException Unable to get thresholds.
88+
*/
89+
public static Boolean inputsWithinThreshold(
90+
final Netlist netlist, final TMActivityEvaluation tmae, final LSLogicEvaluation lsle)
91+
throws CelloException {
92+
Boolean rtn = true;
93+
for (int i = 0; i < netlist.getNumVertex(); i++) {
94+
NetlistNode node = netlist.getVertexAtIdx(i);
95+
if (LSResultsUtils.isAllInput(node) || LSResultsUtils.isAllOutput(node)) {
96+
continue;
97+
}
98+
Boolean b =
99+
inputsWithinThreshold(node, tmae.getActivityTable(node), lsle.getTruthTable(node));
100+
if (!b) {
101+
return false;
102+
}
103+
}
104+
return rtn;
105+
}
106+
107+
/**
108+
* Checks whether input levels for ON (OFF) states are above (below) the gates ON (OFF) threshold,
109+
* if specified.
110+
*
111+
* @param node A netlist node.
112+
* @param activityTable The activity table of the node.
113+
* @param truthTable The truth table of the node.
114+
* @return Whether input levels for ON (OFF) states are above (below) the gates ON (OFF)
115+
* threshold, if specified.
116+
* @throws CelloException Unable to get thresholds.
117+
*/
118+
public static Boolean inputsWithinThreshold(
119+
final NetlistNode node,
120+
final ActivityTable<NetlistNode, NetlistNode> activityTable,
121+
final TruthTable<NetlistNode, NetlistNode> truthTable)
122+
throws CelloException {
123+
Boolean rtn = true;
124+
Model m = node.getResultNetlistNodeData().getDevice().getModel();
125+
Function f = m.getFunctionByName(FunctionType.S_RESPONSEFUNCTION);
126+
Collection<Variable> variables = f.getVariables();
127+
if (variables.size() > 1) {
128+
throw new NotImplementedException(
129+
"Unable to check thresholds for functions with more than one input.");
130+
}
131+
FixedParameter onThresholdParameter = (FixedParameter) m.getParameterByName("on_threshold");
132+
Double onThreshold = null;
133+
if (onThresholdParameter != null) {
134+
onThreshold = onThresholdParameter.evaluate(null).doubleValue();
135+
}
136+
FixedParameter offThresholdParameter = (FixedParameter) m.getParameterByName("off_threshold");
137+
Double offThreshold = null;
138+
if (offThresholdParameter != null) {
139+
offThreshold = offThresholdParameter.evaluate(null).doubleValue();
140+
}
141+
for (int i = 0; i < activityTable.getNumStates(); i++) {
142+
State<NetlistNode> state = activityTable.getStateAtIdx(i);
143+
State<NetlistNode> outputState = truthTable.getStateOutput(state);
144+
Boolean b = outputState.getState(node).equals(outputState.getOne());
145+
Activity<NetlistNode> outputActivity = activityTable.getActivityOutput(state);
146+
if (b && onThreshold != null) {
147+
if (outputActivity.getActivity(node) < onThreshold) {
148+
rtn = false;
149+
}
150+
} else if (offThreshold != null) {
151+
if (outputActivity.getActivity(node) > offThreshold) {
152+
rtn = false;
153+
}
154+
}
155+
}
156+
return rtn;
157+
}
158+
}

0 commit comments

Comments
 (0)