File size: 5,714 Bytes
d1be154 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | #include <ot/timer/sfxt.hpp>
#include <ot/timer/timer.hpp>
namespace ot {
// Constructor
SfxtCache::SfxtCache(Split el, size_t S, size_t T) :
_el {el},
_S {S},
_T {T},
_pins {std::move(__pins)} {
resize_to_fit(std::max(S, T) + 1, __tree, __link, __dist, __spfa);
// debug
//for(const auto& i : __tree) assert(!i);
//for(const auto& i : __dist) assert(!i);
//for(const auto& i : __spfa) assert(!i);
}
// Move constructor
SfxtCache::SfxtCache(SfxtCache&& rhs) :
_el {rhs._el},
_S {rhs._S},
_T {rhs._T},
_pins {std::move(rhs._pins)},
_srcs {std::move(rhs._srcs)} {
}
// Destructor
SfxtCache::~SfxtCache() {
__dist[_S].reset();
__tree[_S].reset();
__link[_S].reset();
__spfa[_S].reset();
for(const auto& p : _pins) {
__dist[p].reset();
__tree[p].reset();
__link[p].reset();
__spfa[p].reset();
}
_pins.clear();
__pins = std::move(_pins);
}
// ----------------------------------------------------------------------------
// Procedure: _topologize
void Timer::_topologize(SfxtCache& sfxt, size_t v) const {
sfxt.__spfa[v] = true;
auto [pin, vrf] = _decode_pin(v);
// Stop at the data source
if(!pin->is_datapath_source()) {
for(auto arc : pin->_fanin) {
FOR_EACH_RF_IF(urf, arc->_delay[sfxt._el][urf][vrf]) {
auto u = _encode_pin(arc->_from, urf);
if(!sfxt.__spfa[u]) {
_topologize(sfxt, u);
}
}
}
}
sfxt._pins.push_back(v);
}
// Procedure: _spdp
void Timer::_spdp(SfxtCache& sfxt) const {
assert(sfxt._pins.empty());
_topologize(sfxt, sfxt._T);
assert(!sfxt._pins.empty());
auto el = sfxt._el;
for(auto itr = sfxt._pins.rbegin(); itr != sfxt._pins.rend(); ++itr) {
auto v = *itr;
auto [pin, vrf] = _decode_pin(v);
assert(sfxt.__dist[v]);
// Stop at the data source
if(pin->is_datapath_source()) {
sfxt._srcs.try_emplace(v, std::nullopt);
continue;
}
// Relax on fanin
for(auto arc : pin->_fanin) {
FOR_EACH_RF_IF(urf, arc->_delay[el][urf][vrf]) {
auto u = _encode_pin(arc->_from, urf);
auto d = (el == MIN) ? *arc->_delay[el][urf][vrf] : -(*arc->_delay[el][urf][vrf]);
sfxt._relax(u, v, _encode_arc(*arc, urf, vrf), d);
}
}
}
}
// Procedure: _spfa
// Perform shortest path fast algorithm (SPFA) to build up the suffix tree.
void Timer::_spfa(SfxtCache& sfxt) const {
auto el = sfxt._el;
std::queue<size_t> queue;
queue.push(sfxt._T);
sfxt.__spfa[sfxt._T] = true;
while(!queue.empty()) {
auto v = queue.front();
queue.pop();
sfxt.__spfa[v] = false;
sfxt._pins.push_back(v);
auto [pin, vrf] = _decode_pin(v);
// Stop at the data source
if(pin->is_datapath_source()) {
sfxt._srcs.try_emplace(v, std::nullopt);
continue;
}
// Relax on fanin
for(auto arc : pin->_fanin) {
FOR_EACH_RF_IF(urf, arc->_delay[el][urf][vrf]) {
auto u = _encode_pin(arc->_from, urf);
auto d = (el == MIN) ? *arc->_delay[el][urf][vrf] : -(*arc->_delay[el][urf][vrf]);
if(sfxt._relax(u, v, _encode_arc(*arc, urf, vrf), d)) {
if(!sfxt.__spfa[u] || *sfxt.__spfa[u] == false) {
queue.push(u);
sfxt.__spfa[u] = true;
}
}
}
}
}
}
// Function: _sfxt_cache
// Find the suffix tree rooted at the primary output po.
SfxtCache Timer::_sfxt_cache(const PrimaryOutput& po, Split el, Tran rf) const {
assert(po._rat[el][rf]);
// create a cache
auto S = _idx2pin.size() << 1;
auto v = _encode_pin(po._pin, rf);
SfxtCache sfxt(el, S, v);
// start at the root
assert(!sfxt.__dist[v]);
sfxt.__dist[v] = (el == MIN) ? -(*po._rat[el][rf]) : *po._rat[el][rf];
// shortest path dynamic programming
_spdp(sfxt);
// shortest path fast algorithm
//_spfa(sfxt);
// relax sources
for(auto& [s, v] : sfxt._srcs) {
if(v = _sfxt_offset(sfxt, s); v) {
sfxt._relax(S, s, std::nullopt, *v);
}
}
return sfxt;
}
// Function: _sfxt_cache
// Find the suffix tree rooted at the test
SfxtCache Timer::_sfxt_cache(const Test& test, Split el, Tran rf) const {
assert(test._rat[el][rf]);
// create a cache
auto S = _idx2pin.size() << 1;
auto v = _encode_pin(test._arc._to, rf);
SfxtCache sfxt(el, S, v);
// Start at the D pin and perform SPFA all the way to the sources of data paths.
assert(!sfxt.__dist[v]);
sfxt.__dist[v] = (el == MIN) ? -(*test._rat[el][rf]) : *test._rat[el][rf];
// shortest path dynamic programming
_spdp(sfxt);
// shortest path fast algorithm
//_spfa(sfxt);
// relaxation from the sources
if(_cppr_analysis) {
auto cppr = _cppr_cache(test, el, rf);
for(auto& [s, v] : sfxt._srcs) {
auto [pin, srf] = _decode_pin(s);
if(v = _cppr_offset(cppr, *pin, el, srf); v) {
sfxt._relax(S, s, std::nullopt, *v);
}
}
}
else {
for(auto& [s, v] : sfxt._srcs) {
if(v = _sfxt_offset(sfxt, s); v) {
sfxt._relax(S, s, std::nullopt, *v);
}
}
}
return sfxt;
}
// Function: _sfxt_cache
SfxtCache Timer::_sfxt_cache(const Endpoint& ept) const {
return std::visit([this, &ept] (auto&& handle) {
return _sfxt_cache(*handle, ept._el, ept._rf);
}, ept._handle);
}
// Function: _sfxt_offset
std::optional<float> Timer::_sfxt_offset(const SfxtCache& sfxt, size_t v) const {
auto [pin, rf] = _decode_pin(v);
if(auto at = pin->_at[sfxt._el][rf]; at) {
return sfxt._el == MIN ? *at : -*at;
}
else {
return std::nullopt;
}
}
}; // end of namespace ot. -----------------------------------------------------------------------
|