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

Extract adders updates #631

Merged
merged 2 commits into from
Feb 19, 2024
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
79 changes: 79 additions & 0 deletions experiments/extract_adders.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* mockturtle: C++ logic network library
* Copyright (C) 2018-2023 EPFL
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include <string>
#include <vector>

#include <fmt/format.h>
#include <lorina/aiger.hpp>
#include <mockturtle/algorithms/experimental/decompose_multioutput.hpp>
#include <mockturtle/algorithms/aig_balancing.hpp>
#include <mockturtle/algorithms/extract_adders.hpp>
#include <mockturtle/io/aiger_reader.hpp>
#include <mockturtle/networks/aig.hpp>
#include <mockturtle/networks/block.hpp>

#include <experiments.hpp>

int main()
{
using namespace experiments;
using namespace mockturtle;

experiment<std::string, uint32_t, uint32_t, uint32_t, float, bool> exp(
"map_adders", "benchmark", "size", "HA", "FA", "runtime", "cec" );

for ( auto const& benchmark : epfl_benchmarks() )
{
fmt::print( "[i] processing {}\n", benchmark );

aig_network aig;
if ( lorina::read_aiger( benchmark_path( benchmark ), aiger_reader( aig ) ) != lorina::return_code::success )
{
continue;
}

/* Remove structural redundancies (increases the number of discoverable HAs/FAs) */
aig_balance( aig, { false } );

const uint32_t size_before = aig.num_gates();

/* Map HAs/FAs */
extract_adders_params ps;
extract_adders_stats st;
block_network res = extract_adders( aig, ps, &st );

/* check correctness */
aig_network aig_res = decompose_multioutput<block_network, aig_network>( res );
bool const cec = benchmark == "hyp" ? true : abc_cec( aig_res, benchmark );

exp( benchmark, size_before, st.mapped_ha, st.mapped_fa, to_seconds( st.time_total ), cec );
}

exp.save();
exp.table();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,17 @@ void decompose_multioutput_impl( NtkSrc const& ntk, NtkDest& dest, LeavesIterato
}
std::cerr << "[e] something went wrong, could not copy node " << ntk.node_to_index( node ) << "\n";
} while ( false );

/* copy name */
if constexpr ( has_has_name_v<NtkSrc> && has_get_name_v<NtkSrc> && has_set_name_v<NtkDest> )
}
/* copy name */
if constexpr ( has_has_name_v<NtkSrc> && has_get_name_v<NtkSrc> && has_set_name_v<NtkDest> )
{
if ( ntk.has_name( f ) )
{
if ( ntk.has_name( f ) )
{
dest.set_name( old_to_new[f], ntk.get_name( f ) );
}
if ( ntk.has_name( !f ) )
{
dest.set_name( !old_to_new[f], ntk.get_name( !f ) );
}
dest.set_name( old_to_new[f], ntk.get_name( f ) );
}
if ( ntk.has_name( !f ) )
{
dest.set_name( !old_to_new[f], ntk.get_name( !f ) );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/mockturtle/algorithms/extract_adders.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct extract_adders_params
/*! \brief Map inverted (NAND2-XNOR2, MIN3-XNOR3) */
bool map_inverted{ false };

/*! \brief Filter cuts using the MFFC */
/*! \brief Filter HAs/FAs using MFFC inclusion */
bool use_mffc_filter{ true };

/*! \brief Be verbose */
Expand Down Expand Up @@ -439,7 +439,7 @@ class extract_adders_impl
{
bool valid = true;

/* check containment of cut1 in cut2 and viceversa */
/* check containment of cut1 in cut2 and vice versa */
if ( index1 > index2 )
{
std::swap( index1, index2 );
Expand Down
4 changes: 4 additions & 0 deletions include/mockturtle/networks/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,21 +394,25 @@ class block_network

signal create_ha( signal a, signal b )
{
/* PO0: carry, PO1: sum */
return _create_node( { a, b }, { 4, 12 } );
}

signal create_hai( signal a, signal b )
{
/* PO0: carry, PO1: sum */
return _create_node( { a, b }, { 5, 13 } );
}

signal create_fa( signal a, signal b, signal c )
{
/* PO0: carry, PO1: sum */
return _create_node( { a, b, c }, { 14, 18 } );
}

signal create_fai( signal a, signal b, signal c )
{
/* PO0: carry, PO1: sum */
return _create_node( { a, b, c }, { 15, 19 } );
}
#pragma endregion
Expand Down
215 changes: 215 additions & 0 deletions test/algorithms/experimental/decompose_multioutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include <catch.hpp>
#include <vector>

#include <kitty/constructors.hpp>
#include <kitty/dynamic_truth_table.hpp>
#include <kitty/operators.hpp>
#include <kitty/static_truth_table.hpp>
#include <mockturtle/algorithms/experimental/decompose_multioutput.hpp>
#include <mockturtle/algorithms/simulation.hpp>
#include <mockturtle/networks/aig.hpp>
#include <mockturtle/networks/block.hpp>
#include <mockturtle/networks/mig.hpp>
#include <mockturtle/networks/sequential.hpp>
#include <mockturtle/networks/xag.hpp>
#include <mockturtle/networks/xmg.hpp>
#include <mockturtle/traits.hpp>
#include <mockturtle/views/dont_touch_view.hpp>
#include <mockturtle/views/names_view.hpp>

using namespace mockturtle;

template<class Ntk>
void test_decompose_multioutput_single()
{
block_network ntk;

const auto a = ntk.create_pi();
const auto b = ntk.create_pi();

const auto f1 = ntk.create_nand( a, b );
const auto f2 = ntk.create_nand( a, f1 );
const auto f3 = ntk.create_nand( b, f1 );
ntk.create_nand( f2, f3 );

CHECK( ntk.size() == 8 );

const Ntk ntk2 = decompose_multioutput<block_network, Ntk>( ntk );

CHECK( ntk2.size() == 3 );
}

template<class Ntk>
Ntk test_decompose_multioutput( bool set_dont_touch = false )
{
block_network ntk;
const auto a = ntk.create_pi();
const auto b = ntk.create_pi();
const auto c = ntk.create_pi();
const auto d = ntk.create_pi();

const auto ha = ntk.create_ha( a, b );
const auto fa = ntk.create_fa( c, d, ha );

ntk.create_po( ntk.next_output_pin( ha ) );
ntk.create_po( ntk.next_output_pin( fa ) );

CHECK( 4u == ntk.num_pis() );
CHECK( 2u == ntk.num_pos() );
CHECK( 2u == ntk.num_gates() );
CHECK( 8u == ntk.size() );

decompose_multioutput_params ps;
ps.set_multioutput_as_dont_touch = set_dont_touch;
return decompose_multioutput<block_network, Ntk>( ntk, ps );
}

TEST_CASE( "decompose multioutput without PO", "[decompose_multioutput]" )
{
test_decompose_multioutput_single<aig_network>();
test_decompose_multioutput_single<xag_network>();
test_decompose_multioutput_single<mig_network>();
test_decompose_multioutput_single<xmg_network>();
}

TEST_CASE( "decompose multioutput with adders AIG", "[decompose_multioutput]" )
{
aig_network aig = test_decompose_multioutput<aig_network>();

CHECK( aig.num_pis() == 4 );
CHECK( aig.num_pos() == 2 );
CHECK( aig.num_gates() == 14 );
CHECK( aig.size() == 19 );
}

TEST_CASE( "decompose multioutput with adders XAG", "[decompose_multioutput]" )
{
xag_network xag = test_decompose_multioutput<xag_network>();

CHECK( xag.num_pis() == 4 );
CHECK( xag.num_pos() == 2 );
CHECK( xag.num_gates() == 7 );
CHECK( xag.size() == 12 );
}

TEST_CASE( "decompose multioutput with adders MIG", "[decompose_multioutput]" )
{
mig_network mig = test_decompose_multioutput<mig_network>();

CHECK( mig.num_pis() == 4 );
CHECK( mig.num_pos() == 2 );
CHECK( mig.num_gates() == 8 );
CHECK( mig.size() == 13 );
}

TEST_CASE( "decompose multioutput with adders XMG", "[decompose_multioutput]" )
{
xmg_network xmg = test_decompose_multioutput<xmg_network>();

CHECK( xmg.num_pis() == 4 );
CHECK( xmg.num_pos() == 2 );
CHECK( xmg.num_gates() == 4 );
CHECK( xmg.size() == 9 );
}

TEST_CASE( "decompose multioutput with adders", "[decompose_multioutput]" )
{
block_network ntk = test_decompose_multioutput<block_network>();

CHECK( ntk.num_pis() == 4 );
CHECK( ntk.num_pos() == 2 );
CHECK( ntk.num_gates() == 4 );
CHECK( ntk.size() == 10 );
}

TEST_CASE( "decompose multioutput with adders don't touch", "[decompose_multioutput]" )
{
block_network ntk;
const auto a = ntk.create_pi();
const auto b = ntk.create_pi();
const auto c = ntk.create_pi();
const auto d = ntk.create_pi();

const auto ha = ntk.create_ha( a, b );
const auto fa = ntk.create_fa( c, d, ha );
const auto f = ntk.create_and( ha, fa );

ntk.create_po( ntk.next_output_pin( ha ) );
ntk.create_po( ntk.next_output_pin( fa ) );
ntk.create_po( f );

CHECK( ntk.num_pis() == 4 );
CHECK( ntk.num_pos() == 3 );
CHECK( ntk.num_gates() == 3 );
CHECK( ntk.size() == 9 );

decompose_multioutput_params ps;
ps.set_multioutput_as_dont_touch = true;
dont_touch_view<block_network> res = decompose_multioutput<block_network, dont_touch_view<block_network>>( ntk, ps );

CHECK( res.num_pis() == 4 );
CHECK( res.num_pos() == 3 );
CHECK( res.num_gates() == 5 );
CHECK( res.size() == 11 );

std::vector<block_network::node> gates;
res.foreach_gate( [&]( auto const& g ) {
gates.push_back( g );
} );

for ( uint32_t i = 0; i < gates.size() - 1; ++i )
{
CHECK( res.is_dont_touch( gates[i] ) == true );
}
CHECK( res.is_dont_touch( gates.back() ) == false );
}

TEST_CASE( "decompose multioutput network with names", "[decompose_multioutput]" )
{
names_view<block_network> ntk_orig;
ntk_orig.set_network_name( "network" );
const auto pi0 = ntk_orig.create_pi();
ntk_orig.set_name( pi0, "pi0" );
const auto pi1 = ntk_orig.create_pi();
ntk_orig.set_name( pi1, "pi1" );
const auto pi2 = ntk_orig.create_pi();
ntk_orig.set_name( pi2, "pi2" );
const auto nand2 = ntk_orig.create_nand( pi1, pi0 );
ntk_orig.set_name( nand2, "nand2" );
const auto and2 = ntk_orig.create_and( pi1, pi2 );
ntk_orig.set_name( and2, "and2" );
const auto inv = ntk_orig.create_not( pi1 );
ntk_orig.set_name( inv, "inv" );
ntk_orig.create_po( nand2 );
ntk_orig.set_output_name( 0, "po0" );
ntk_orig.create_po( and2 );
ntk_orig.set_output_name( 1, "po1" );
ntk_orig.create_po( inv );
ntk_orig.set_output_name( 2, "po2" );
ntk_orig.create_po( pi0 );
ntk_orig.set_output_name( 3, "po3" );

names_view<block_network> ntk = decompose_multioutput<names_view<block_network>>( ntk_orig );

CHECK( ntk.get_network_name() == "network" );
CHECK( ntk.has_name( pi0 ) );
CHECK( ntk.get_name( pi0 ) == "pi0" );
CHECK( ntk.has_name( pi1 ) );
CHECK( ntk.get_name( pi1 ) == "pi1" );
CHECK( ntk.has_name( pi2 ) );
CHECK( ntk.get_name( pi2 ) == "pi2" );
CHECK( ntk.has_name( and2 ) );
CHECK( ntk.get_name( and2 ) == "and2" );
CHECK( ntk.has_name( nand2 ) );
CHECK( ntk.get_name( nand2 ) == "nand2" );
CHECK( ntk.has_name( inv ) );
CHECK( ntk.get_name( inv ) == "inv" );
CHECK( ntk.has_output_name( 0 ) );
CHECK( ntk.get_output_name( 0 ) == "po0" );
CHECK( ntk.has_output_name( 1 ) );
CHECK( ntk.get_output_name( 1 ) == "po1" );
CHECK( ntk.has_output_name( 2 ) );
CHECK( ntk.get_output_name( 2 ) == "po2" );
CHECK( ntk.has_output_name( 3 ) );
CHECK( ntk.get_output_name( 3 ) == "po3" );
}
Loading