Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace fanout_view with fanout_view2. #244

Merged
merged 5 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/mockturtle/algorithms/aig_resub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ void aig_resubstitution( Ntk& ntk, resubstitution_params const& ps = {}, resubst
static_assert( has_value_v<Ntk>, "Ntk does not implement the has_value method" );
static_assert( has_visited_v<Ntk>, "Ntk does not implement the has_visited method" );

using resub_view_t = fanout_view2<depth_view<Ntk>>;
using resub_view_t = fanout_view<depth_view<Ntk>>;
depth_view<Ntk> depth_view{ntk};
resub_view_t resub_view{depth_view};

Expand Down
8 changes: 4 additions & 4 deletions include/mockturtle/algorithms/cut_rewriting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include "../utils/progress_bar.hpp"
#include "../utils/stopwatch.hpp"
#include "../views/cut_view.hpp"
#include "../views/fanout_view2.hpp"
#include "../views/fanout_view.hpp"
#include "cut_enumeration.hpp"
#include "detail/mffc_utils.hpp"
#include "dont_cares.hpp"
Expand Down Expand Up @@ -690,10 +690,10 @@ void cut_rewriting( Ntk& ntk, RewritingFn&& rewriting_fn, cut_rewriting_params c
}
else
{
fanout_view2_params fvps;
fanout_view_params fvps;
fvps.update_on_delete = false;
fanout_view2<Ntk> ntk_fo{ntk, fvps};
detail::cut_rewriting_impl<fanout_view2<Ntk>, RewritingFn, NodeCostFn> p( ntk_fo, rewriting_fn, ps, st, cost_fn );
fanout_view<Ntk> ntk_fo{ntk, fvps};
detail::cut_rewriting_impl<fanout_view<Ntk>, RewritingFn, NodeCostFn> p( ntk_fo, rewriting_fn, ps, st, cost_fn );
p.run();
}

Expand Down
2 changes: 1 addition & 1 deletion include/mockturtle/algorithms/mig_resub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ void mig_resubstitution( Ntk& ntk, resubstitution_params const& ps = {}, resubst
static_assert( has_value_v<Ntk>, "Ntk does not implement the has_value method" );
static_assert( has_visited_v<Ntk>, "Ntk does not implement the has_visited method" );

using resub_view_t = fanout_view2<depth_view<Ntk>>;
using resub_view_t = fanout_view<depth_view<Ntk>>;
depth_view<Ntk> depth_view{ntk};
resub_view_t resub_view{depth_view};

Expand Down
4 changes: 2 additions & 2 deletions include/mockturtle/algorithms/resubstitution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "../utils/progress_bar.hpp"
#include "../utils/stopwatch.hpp"
#include "../views/depth_view.hpp"
#include "../views/fanout_view2.hpp"
#include "../views/fanout_view.hpp"
#include "dont_cares.hpp"
#include "reconv_cut2.hpp"

Expand Down Expand Up @@ -818,7 +818,7 @@ void resubstitution( Ntk& ntk, resubstitution_params const& ps = {}, resubstitut
static_assert( has_value_v<Ntk>, "Ntk does not implement the has_value method" );
static_assert( has_visited_v<Ntk>, "Ntk does not implement the has_visited method" );

using resub_view_t = fanout_view2<depth_view<Ntk>>;
using resub_view_t = fanout_view<depth_view<Ntk>>;
depth_view<Ntk> depth_view{ntk};
resub_view_t resub_view{depth_view};

Expand Down
2 changes: 1 addition & 1 deletion include/mockturtle/algorithms/xag_resub_withDC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ void resubstitution_minmc_withDC( Ntk& ntk, resubstitution_params const& ps = {}
static_assert( has_value_v<Ntk>, "Ntk does not implement the has_value method" );
static_assert( has_visited_v<Ntk>, "Ntk does not implement the has_visited method" );

using resub_view_t = fanout_view2<depth_view<Ntk>>;
using resub_view_t = fanout_view<depth_view<Ntk>>;
depth_view<Ntk> depth_view{ntk};
resub_view_t resub_view{depth_view};

Expand Down
2 changes: 1 addition & 1 deletion include/mockturtle/mockturtle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
#include "mockturtle/views/immutable_view.hpp"
#include "mockturtle/views/topo_view.hpp"
#include "mockturtle/views/window_view.hpp"
#include "mockturtle/views/fanout_view2.hpp"
#include "mockturtle/views/fanout_view.hpp"
#include "mockturtle/views/names_view.hpp"
#include "mockturtle/views/mapping_view.hpp"
#include "mockturtle/views/fanout_view.hpp"
Expand Down
115 changes: 72 additions & 43 deletions include/mockturtle/views/fanout_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#pragma once

#include <cstdint>
#include <stack>
#include <vector>

#include "../traits.hpp"
Expand All @@ -43,6 +44,13 @@
namespace mockturtle
{

struct fanout_view_params
{
bool update_on_add{true};
bool update_on_modified{true};
bool update_on_delete{true};
};

/*! \brief Implements `foreach_fanout` methods for networks.
*
* This view computes the fanout of each node of the network.
Expand All @@ -64,8 +72,9 @@ template<typename Ntk>
class fanout_view<Ntk, true> : public Ntk
{
public:
fanout_view( Ntk const& ntk ) : Ntk( ntk )
fanout_view( Ntk const& ntk, fanout_view_params const& ps = {} ) : Ntk( ntk )
{
(void)ps;
}
};

Expand All @@ -77,13 +86,46 @@ class fanout_view<Ntk, false> : public Ntk
using node = typename Ntk::node;
using signal = typename Ntk::signal;

fanout_view( Ntk const& ntk ) : Ntk( ntk ), _fanout( ntk )
fanout_view( Ntk const& ntk, fanout_view_params const& ps = {} ) : Ntk( ntk ), _fanout( ntk ), _ps( ps )
{
static_assert( is_network_type_v<Ntk>, "Ntk is not a network type" );
static_assert( has_foreach_node_v<Ntk>, "Ntk does not implement the foreach_node method" );
static_assert( has_foreach_fanin_v<Ntk>, "Ntk does not implement the foreach_fanin method" );

update_fanout();

if ( _ps.update_on_add )
{
Ntk::events().on_add.push_back( [this]( auto const& n ) {
_fanout.resize();
Ntk::foreach_fanin( n, [&, this]( auto const& f ) {
_fanout[f].push_back( n );
} );
} );
}

if ( _ps.update_on_modified )
{
Ntk::events().on_modified.push_back( [this]( auto const& n, auto const& previous ) {
(void)previous;
for ( auto const& f : previous ) {
_fanout[f].erase( std::remove( _fanout[f].begin(), _fanout[f].end(), n ), _fanout[f].end() );
}
Ntk::foreach_fanin( n, [&, this]( auto const& f ) {
_fanout[f].push_back( n );
} );
} );
}

if ( _ps.update_on_delete )
{
Ntk::events().on_delete.push_back( [this]( auto const& n ) {
_fanout[n].clear();
Ntk::foreach_fanin( n, [&, this]( auto const& f ) {
_fanout[f].erase( std::remove( _fanout[f].begin(), _fanout[f].end(), n ), _fanout[f].end() );
} );
} );
}
}

template<typename Fn>
Expand All @@ -98,50 +140,36 @@ class fanout_view<Ntk, false> : public Ntk
compute_fanout();
}

void resize_fanout()
{
_fanout.resize();
}

std::vector<node> fanout( node const& n ) const /* deprecated */
{
return _fanout[ n ];
return _fanout[n];
}

void set_fanout( node const& n, std::vector<node> const& fanout )
void substitute_node( node const& old_node, signal const& new_signal )
{
_fanout[ n ] = fanout;
}

void add_fanout( node const& n, node const& p )
{
_fanout[ n ].emplace_back( p );
}

void remove_fanout( node const& n, node const& p )
{
auto &f = _fanout[ n ];
f.erase( std::remove( f.begin(), f.end(), p ), f.end() );
}

void substitute_node_of_parents( std::vector<node> const& parents, node const& old_node, signal const& new_signal ) /* deprecated */
{
Ntk::substitute_node_of_parents( parents, old_node, new_signal );

std::vector<node> old_node_fanout = _fanout[ old_node ];
std::sort( old_node_fanout.begin(), old_node_fanout.end() );

std::vector<node> parents_copy( parents );
std::sort( parents_copy.begin(), parents_copy.end() );

_fanout[ old_node ] = {};

std::vector<node> intersection;
std::set_intersection( parents_copy.begin(), parents_copy.end(), old_node_fanout.begin(), old_node_fanout.end(),
std::back_inserter( intersection ) );

resize_fanout();
set_fanout( this->get_node( new_signal ), intersection );
std::stack<std::pair<node, signal>> to_substitute;
to_substitute.push( {old_node, new_signal} );

while ( !to_substitute.empty() )
{
const auto [_old, _new] = to_substitute.top();
to_substitute.pop();

const auto parents = _fanout[_old];
for ( auto n : parents )
{
if ( const auto repl = Ntk::replace_in_node( n, _old, _new ); repl )
{
to_substitute.push( *repl );
}
}

/* check outputs */
Ntk::replace_in_outputs( _old, _new );

/* reset fan-in of old node */
Ntk::take_out_node( _old );
}
}

private:
Expand All @@ -151,7 +179,7 @@ class fanout_view<Ntk, false> : public Ntk

this->foreach_gate( [&]( auto const& n ){
this->foreach_fanin( n, [&]( auto const& c ){
auto& fanout = _fanout[ c ];
auto& fanout = _fanout[c];
if ( std::find( fanout.begin(), fanout.end(), n ) == fanout.end() )
{
fanout.push_back( n );
Expand All @@ -161,9 +189,10 @@ class fanout_view<Ntk, false> : public Ntk
}

node_map<std::vector<node>, Ntk> _fanout;
fanout_view_params _ps;
};

template<class T>
fanout_view(T const&) -> fanout_view<T>;
fanout_view( T const&, fanout_view_params const& ps = {} ) -> fanout_view<T>;

} // namespace mockturtle
Loading