Skip to content
Closed
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
284 changes: 161 additions & 123 deletions include/mockturtle/algorithms/reconv_cut2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,175 +24,212 @@
*/

/*!
\file reconv_cut.hpp
\file reconv_cut2.hpp
\brief Reconvergence-driven cut

Based on `abcReconv.c`

\author Heinz Riener
*/

#pragma once

#include "../traits.hpp"

#include <optional>
#include <cassert>
#include <optional>
#include <vector>

namespace mockturtle
{

template<typename Ntk>
struct cut_manager
struct reconv_cut_parameters
{
explicit cut_manager( int node_size_max, int node_fan_stop = 100000 )
: node_size_max( node_size_max )
, node_fan_stop( node_fan_stop )
{
}
/*! \brief maximum size of a reconv. cut */
uint32_t node_size_max{8u};

/* \brief limit on the size of the supernode */
int node_size_max;
/*! \brief skip nodes with many fanouts */
uint32_t node_fan_stop{100000u};
};

/* \brief limit on the size of the supernode */
int node_fan_stop;
struct reconv_cut_statistics
{
/*! \brief number of calls */
uint32_t num_calls{0u};

/* \brief fanins of the collapsed node (the cut) */
std::vector<node<Ntk>> node_leaves;
/*! \brief total number of leaves */
uint32_t total_leaves{0u};

/* \brief visited nodes */
std::vector<node<Ntk>> visited;
void reset()
{
*this = {};
}
};

namespace detail
{

template<typename Ntk>
int node_get_leaf_cost_one( Ntk const& ntk, typename Ntk::node const &node, int fanin_limit )
class reconv_cut_computation
{
/* make sure the node is in the construction zone */
assert( ntk.visited( node ) == ntk.trav_id() );

/* cannot expand over the PI node */
if ( ntk.is_constant( node ) || ntk.is_pi( node ) )
return 999;
public:
using node = typename Ntk::node;

public:
explicit reconv_cut_computation( Ntk const& ntk, reconv_cut_parameters const& ps, reconv_cut_statistics& st )
: ntk( ntk )
, ps( ps )
, st( st )
{
}

/* get the cost of the cone */
uint32_t cost = 0;
ntk.foreach_fanin( node, [&]( const auto& f ){
cost += ( ntk.visited( ntk.get_node( f ) ) == ntk.trav_id() ) ? 0 : 1;
} );
std::vector<node> run( node const& pivot )
{
++st.num_calls;

ntk.incr_trav_id();

/* start the visited nodes and mark them */
visited.clear();
visited.push_back( pivot );
ntk.set_visited( pivot, 1 );
ntk.foreach_fanin( pivot, [&]( const auto& f ) {
auto const& n = ntk.get_node( f );
if ( n == 0 )
return true;
visited.push_back( n );
ntk.set_visited( n, ntk.trav_id() );
return true;
});

/* start the cut */
leaves.clear();
ntk.foreach_fanin( pivot, [&]( const auto& f ) {
auto const& n = ntk.get_node( f );
if ( n == 0 )
return true;
leaves.push_back( n );
return true;
} );

/* always accept if the number of leaves does not increase */
if ( cost < ntk.fanin_size( node ) )
return cost;
if ( leaves.size() > ps.node_size_max )
{
/* special case: cut already overflows at the current node
because the cut size limit is very low */
leaves.clear();
return {};
}

/* skip nodes with many fanouts */
if ( int( ntk.fanout_size( node ) ) > fanin_limit )
return 999;
/* compute the cut */
while ( build_cut() );
assert( leaves.size() <= ps.node_size_max );

/* return the number of nodes that will be on the leaves if this node is removed */
return cost;
}
/* update statistics */
st.total_leaves += leaves.size();
return leaves;
}

template<typename Ntk>
bool node_build_cut_level_one_int( Ntk const& ntk, std::vector<typename Ntk::node>& visited, std::vector<typename Ntk::node>& leaves, uint64_t size_limit, int fanin_limit )
{
uint32_t best_cost = 100;
protected:
bool build_cut()
{
uint32_t best_cost{100u};

std::optional<typename Ntk::node> best_fanin;
int best_pos;
std::optional<node> best_fanin;
int best_pos;

/* evaluate fanins of the cut */
auto pos = 0;
for ( const auto& l : leaves )
{
uint32_t const cost_curr = node_get_leaf_cost_one( ntk, l, fanin_limit );
if ( best_cost > cost_curr ||
( best_cost == cost_curr && best_fanin && ntk.level( l ) > ntk.level( *best_fanin ) ) )
/* evaluate fanins of the cut */
auto pos = 0;
for ( const auto& l : leaves )
{
best_cost = cost_curr;
best_fanin = std::make_optional( l );
best_pos = pos;
}
uint32_t const cost_curr = leaf_costs( l );
if ( best_cost > cost_curr ||
( best_cost == cost_curr && best_fanin && ntk.level( l ) > ntk.level( *best_fanin ) ) )
{
best_cost = cost_curr;
best_fanin = std::make_optional( l );
best_pos = pos;
}

if ( best_cost == 0 )
break;
if ( best_cost == 0 )
break;

++pos;
}
++pos;
}

if ( !best_fanin )
return false;
if ( !best_fanin )
return false;

// assert( best_cost < max_fanin_of_graph_structure );
if ( leaves.size() - 1 + best_cost > size_limit )
// assert( best_cost < max_fanin_of_graph_structure );
if ( leaves.size() - 1 + best_cost > ps.node_size_max )
return false;

/* remove the best node from the array */
leaves.erase( leaves.begin() + best_pos );
/* remove the best node from the array */
leaves.erase( leaves.begin() + best_pos );

/* add the fanins of best to leaves and visited */
ntk.foreach_fanin( *best_fanin, [&]( const auto& f ){
auto const& n = ntk.get_node( f );
if ( n != 0 && ( ntk.visited( n ) != ntk.trav_id() ) )
{
ntk.set_visited( n, ntk.trav_id() );
visited.push_back( n );
leaves.push_back( n );
}
});

assert( leaves.size() <= ps.node_size_max );
return true;
}

/* add the fanins of best to leaves and visited */
ntk.foreach_fanin( *best_fanin, [&]( const auto& f ){
auto const& n = ntk.get_node( f );
if ( n != 0 && ( ntk.visited( n ) != ntk.trav_id() ) )
{
ntk.set_visited( n, ntk.trav_id() );
visited.push_back( n );
leaves.push_back( n );
}
});
uint32_t leaf_costs( node const &node ) const
{
/* make sure the node is in the construction zone */
assert( ntk.visited( node ) == ntk.trav_id() );

assert( leaves.size() <= size_limit );
/* cannot expand over the PI node */
if ( ntk.is_constant( node ) || ntk.is_pi( node ) )
return 999;

return true;
}
/* get the cost of the cone */
uint32_t cost = 0u;
ntk.foreach_fanin( node, [&]( const auto& f ){
cost += ( ntk.visited( ntk.get_node( f ) ) == ntk.trav_id() ) ? 0 : 1;
} );

template<class Ntk>
std::vector<typename Ntk::node> node_find_cut( cut_manager<Ntk>& mgr, Ntk const& ntk, typename Ntk::node const& root )
{
ntk.incr_trav_id();

/* start the visited nodes and mark them */
mgr.visited.clear();
mgr.visited.push_back( root );
ntk.set_visited( root, 1 );
ntk.foreach_fanin( root, [&]( const auto& f ){
auto const& n = ntk.get_node( f );
if ( n == 0 ) return true;
mgr.visited.push_back( n );
ntk.set_visited( n, ntk.trav_id() );
return true;
} );

/* start the cut */
mgr.node_leaves.clear();
ntk.foreach_fanin( root, [&]( const auto& f ){
auto const& n = ntk.get_node( f );
if ( n == 0 ) return true;
mgr.node_leaves.push_back( n );
return true;
} );

if ( mgr.node_leaves.size() > uint32_t( mgr.node_size_max ) )
{
/* special case: cut already overflows at the current node
bc. the cut size limit is very low */
mgr.node_leaves.clear();
return {};
/* always accept if the number of leaves does not increase */
if ( cost < ntk.fanin_size( node ) )
return cost;

/* skip nodes with many fanouts */
if ( ntk.fanout_size( node ) > ps.node_fan_stop )
return 999;

/* return the number of nodes that will be on the leaves if this node is removed */
return cost;
}

/* compute the cut */
while ( node_build_cut_level_one_int( ntk, mgr.visited, mgr.node_leaves, mgr.node_size_max, mgr.node_fan_stop ) );
assert( int( mgr.node_leaves.size() ) <= mgr.node_size_max );
protected:
Ntk const& ntk;
reconv_cut_parameters const& ps;
reconv_cut_statistics& st;

return mgr.node_leaves;
}
std::vector<node> visited;
std::vector<node> leaves;
};

} /* namespace detail */
template<typename Ntk>
std::vector<node<Ntk>> reconv_driven_cut( Ntk const& ntk, node<Ntk> const& pivot, reconv_cut_parameters const& ps, reconv_cut_statistics& st )
{
static_assert( is_network_type_v<Ntk>, "Ntk is not a network type" );
static_assert( has_is_constant_v<Ntk>, "Ntk does not implement the is_constant method" );
static_assert( has_is_pi_v<Ntk>, "Ntk does not implement the is_pi method" );
static_assert( has_get_node_v<Ntk>, "Ntk does not implement the is_pi method" );
static_assert( has_visited_v<Ntk>, "Ntk does not implement the has_visited method" );
static_assert( has_set_visited_v<Ntk>, "Ntk does not implement the set_visited method" );
static_assert( has_foreach_fanin_v<Ntk>, "Ntk does not implement the foreach_fanin method" );
static_assert( has_foreach_fanout_v<Ntk>, "Ntk does not implement the foreach_fanout method" );
static_assert( has_level_v<Ntk>, "Ntk does not implement the level method" );

return reconv_cut_computation<Ntk>( ntk, ps, st ).run( pivot );
}

template<typename Ntk>
std::vector<node<Ntk>> reconv_driven_cut( cut_manager<Ntk>& mgr, Ntk const& ntk, node<Ntk> const& pivot )
std::vector<node<Ntk>> reconv_driven_cut( Ntk const& ntk, node<Ntk> const& pivot, reconv_cut_parameters const& ps )
{
static_assert( is_network_type_v<Ntk>, "Ntk is not a network type" );
static_assert( has_is_constant_v<Ntk>, "Ntk does not implement the is_constant method" );
Expand All @@ -204,7 +241,8 @@ std::vector<node<Ntk>> reconv_driven_cut( cut_manager<Ntk>& mgr, Ntk const& ntk,
static_assert( has_foreach_fanout_v<Ntk>, "Ntk does not implement the foreach_fanout method" );
static_assert( has_level_v<Ntk>, "Ntk does not implement the level method" );

return detail::node_find_cut( mgr, ntk, pivot );
reconv_cut_statistics st;
return reconv_cut_computation<Ntk>( ntk, ps, st ).run( pivot );
}

} /* namespace mockturtle */
5 changes: 1 addition & 4 deletions include/mockturtle/algorithms/resubstitution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,6 @@ class resubstitution_impl
{
stopwatch t( st.time_total );

/* start the managers */
cut_manager<Ntk> mgr( ps.max_pis );

progress_bar pbar{ntk.size(), "resub |{0}| node = {1:>4} cand = {2:>4} est. gain = {3:>5}", ps.progress};

auto const size = ntk.num_gates();
Expand All @@ -506,7 +503,7 @@ class resubstitution_impl

/* compute a reconvergence-driven cut */
auto const leaves = call_with_stopwatch( st.time_cuts, [&]() {
return reconv_driven_cut( mgr, ntk, n );
return reconv_driven_cut( ntk, n, reconv_cut_parameters{.node_size_max = ps.max_pis} );
} );

/* evaluate this cut */
Expand Down
4 changes: 1 addition & 3 deletions test/algorithms/reconv_cut2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ TEST_CASE( "generate reconvergence-driven cuts for an AIG", "[cut_generation]" )

fanout_view<aig_network> aig_fanout_view( aig );
depth_view<fanout_view<aig_network>> aig_view( aig_fanout_view );
cut_manager<depth_view<fanout_view<aig_network>>> mgr( 6 );

auto leaves = [&]( const auto& s, uint32_t size = 1000u ){
mgr.node_size_max = size;
const auto leaves = reconv_driven_cut( mgr, aig_view, aig.get_node( s ) );
const auto leaves = reconv_driven_cut( aig_view, aig.get_node( s ), reconv_cut_parameters{.node_size_max = 6u} );
return set_t( leaves.begin(), leaves.end() );
};

Expand Down