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

CEC implementation - Dinan #49

Open
wants to merge 7 commits into
base: v2021-cec
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ examples/

docs/_build
docs/doxyxml
.vs
2 changes: 1 addition & 1 deletion include/mockturtle/algorithms/cleanup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ NtkDest cleanup_dangling( NtkSrc const& ntk )
std::vector<signal<NtkDest>> pis;
ntk.foreach_pi( [&]( auto ) {
pis.push_back( dest.create_pi() );
} );
} ); // record all the primiary inputs of ntk in pis

for ( auto f : cleanup_dangling( ntk, dest, pis.begin(), pis.end() ) )
{
Expand Down
14 changes: 8 additions & 6 deletions include/mockturtle/algorithms/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class default_simulator<kitty::dynamic_truth_table>
{
public:
default_simulator() = delete;
default_simulator( unsigned num_vars ) : num_vars( num_vars ) {}
default_simulator( unsigned num_vars) : num_vars( num_vars ){}

kitty::dynamic_truth_table compute_constant( bool value ) const
{
Expand All @@ -121,10 +121,11 @@ class default_simulator<kitty::dynamic_truth_table>

kitty::dynamic_truth_table compute_pi( uint32_t index ) const
{
kitty::dynamic_truth_table tt( num_vars );
kitty::create_nth_var( tt, index );
return tt;
}
kitty::dynamic_truth_table tt( num_vars );
kitty::create_nth_var( tt, index );
return tt;
}


kitty::dynamic_truth_table compute_not( kitty::dynamic_truth_table const& value ) const
{
Expand All @@ -133,6 +134,8 @@ class default_simulator<kitty::dynamic_truth_table>

private:
unsigned num_vars;
bool isMiter;
uint32_t round, split_var;
};

/*! \brief Simulates truth tables.
Expand Down Expand Up @@ -522,7 +525,6 @@ node_map<SimulationType, Ntk> simulate_nodes( Ntk const& ntk, Simulator const& s
} );
node_to_value[n] = ntk.compute( n, fanin_values.begin(), fanin_values.end() );
} );

return node_to_value;
}

Expand Down
82 changes: 78 additions & 4 deletions include/mockturtle/algorithms/simulation_cec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "miter.hpp"
#include "simulation.hpp"

#include <bitset> // used for printing

namespace mockturtle
{

Expand All @@ -56,6 +58,46 @@ struct simulation_cec_stats
namespace detail
{

class cec_simulator
{
public:
cec_simulator(unsigned num_vars, uint32_t split_var, uint32_t round) : num_vars( num_vars ), split_var(split_var),
round(round) {}

kitty::dynamic_truth_table compute_constant( bool value ) const
{
kitty::dynamic_truth_table tt( split_var );
return value ? ~tt : tt;
}

kitty::dynamic_truth_table compute_pi( uint32_t index ) const
{
kitty::dynamic_truth_table tt( split_var );
if ( index < split_var )
{
kitty::create_nth_var( tt, index );
}
else{
bool flag = round >> ( index - split_var ) & 1;
if ( !flag )
{
tt = ~tt;
}
}
return tt;
}

kitty::dynamic_truth_table compute_not( kitty::dynamic_truth_table const& value ) const
{
return ~value;
}

private:
unsigned int num_vars;
uint32_t round, split_var;
};


template<class Ntk>
class simulation_cec_impl
{
Expand All @@ -74,16 +116,48 @@ class simulation_cec_impl
bool run()
{
/* TODO: write your implementation here */
return false;
// computing splitting_var and rounds
gen_st();
// create the pattern with truth table

for ( auto i = 0; i < _st.rounds; i++ )
{
std::cout << "[running] round " << i << std::endl;
cec_simulator sim( _ntk.num_pis(), _st.split_var, i );
const std::vector<kitty::dynamic_truth_table> res = simulate<kitty::dynamic_truth_table>( _ntk, sim );

for ( auto& po : res )
{
if ( !kitty::is_const0( po ) )
{
return false;
}
}
return true;
}
}

private:
/* you can add additional methods here */

private:
Ntk& _ntk;
simulation_cec_stats& _st;
/* you can add other attributes here */

void gen_st()
{
auto n = _ntk.num_pis();
if ( n <= 6 )
{
_st.split_var = n;
}
else
{
auto v = _ntk.size();
int max_m = std::log2l( std::pow( 2, 29 ) / v - 32 ) + 3;
_st.split_var = max_m > n ? n : max_m;
}

_st.rounds = std::pow( 2, n - _st.split_var );
}
};

} // namespace detail
Expand Down
13 changes: 7 additions & 6 deletions lib/kitty/kitty/constructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void create_nth_var( TT& tt, uint8_t var_index, bool complement = false )
tt.mask_bits();
return;
}
}
} // if partial_truth_table
else
{
if ( tt.num_vars() <= 6 )
Expand All @@ -123,13 +123,13 @@ void create_nth_var( TT& tt, uint8_t var_index, bool complement = false )
}
}

if ( var_index < 6 )
{
if ( var_index < 6 ) // if index smaller than six
{ // fill in all the bits with the same projection
std::fill( std::begin( tt._bits ), std::end( tt._bits ), complement ? ~detail::projections[var_index] : detail::projections[var_index] );
}
else
else
{
const auto c = 1 << ( var_index - 6 );
const auto c = 1 << ( var_index - 6 ); // var_index 7, c 2
const auto zero = uint64_t( 0 );
const auto one = ~zero;
auto block = uint64_t( 0u );
Expand All @@ -150,7 +150,8 @@ void create_nth_var( TT& tt, uint8_t var_index, bool complement = false )

/*! \cond PRIVATE */
template<uint32_t NumVars>
void create_nth_var( static_truth_table<NumVars, true>& tt, uint8_t var_index, bool complement = false )
void create_nth_var( static_truth_table<NumVars, true>& tt, uint8_t var_index, bool complement = false )
// cast to static truth table, can only have up to six varialbes
{
/* assign from precomputed table */
tt._bits = complement ? ~detail::projections[var_index] : detail::projections[var_index];
Expand Down
4 changes: 2 additions & 2 deletions lib/kitty/kitty/detail/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ namespace detail
{

static constexpr uint64_t projections[] = {
UINT64_C( 0xaaaaaaaaaaaaaaaa ),
UINT64_C( 0xcccccccccccccccc ),
UINT64_C( 0xaaaaaaaaaaaaaaaa ), // 10101010...
UINT64_C( 0xcccccccccccccccc ), // 11001100...
UINT64_C( 0xf0f0f0f0f0f0f0f0 ),
UINT64_C( 0xff00ff00ff00ff00 ),
UINT64_C( 0xffff0000ffff0000 ),
Expand Down
2 changes: 1 addition & 1 deletion lib/kitty/kitty/dynamic_truth_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct dynamic_truth_table
\param num_vars Number of variables
*/
explicit dynamic_truth_table( uint32_t num_vars )
: _bits( ( num_vars <= 6 ) ? 1u : ( 1u << ( num_vars - 6 ) ) ),
: _bits( ( num_vars <= 6 ) ? 1u : ( 1u << ( num_vars - 6 ) ) ), // initialize length of _bits
_num_vars( num_vars )
{
}
Expand Down