Skip to content

Commit

Permalink
Merge pull request #31 from nbulsi/sto
Browse files Browse the repository at this point in the history
Stochastic circuit synthesis
  • Loading branch information
zfchu authored Nov 17, 2020
2 parents 42a4f15 + f59c6af commit ae66b1a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 34 deletions.
11 changes: 10 additions & 1 deletion src/commands/stochastic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ namespace alice
std::cout << std::endl;
}

stochastic_synthesis( num_vars, m, n, vector );
stopwatch<>::duration time{0};
mig_network mig;
call_with_stopwatch( time, [&]()
{
mig = stochastic_synthesis( num_vars, m, n, vector );
} );

store<mig_network>().extend();
store<mig_network>().current() = mig;
std::cout << fmt::format( "[time]: {:5.2f} seconds\n", to_seconds( time ) );
}
else
{
Expand Down
66 changes: 34 additions & 32 deletions src/core/exact_sto_m3ig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <cmath>

#include <kitty/kitty.hpp>
#include <mockturtle/mockturtle.hpp>
#include "exact_m3ig_sto_encoder.hpp"
#include "exact_sto_m3ig.hpp"

Expand All @@ -20,8 +19,8 @@ namespace also
{
public:
sto_syn_manager( unsigned const& num_vars, unsigned const& m, unsigned const& n, std::vector<unsigned> const& vector );
void run();
void preprocess();
mig_network run();
bool preprocess();
unsigned sum_of_vector();
unsigned count_tt_sum_of_x( unsigned const& spec_num, kitty::dynamic_truth_table const& tt_solution );
bool validate( kitty::dynamic_truth_table const& tt );
Expand Down Expand Up @@ -85,7 +84,7 @@ namespace also

/* check all primary inputs and its complements for trivial
* cases */
void sto_syn_manager::preprocess()
bool sto_syn_manager::preprocess()
{
unsigned num_vars_m_plus_n = m + n;

Expand Down Expand Up @@ -128,6 +127,8 @@ namespace also
}
}
}

return trivial;
}

/* find the number of tt entry whose sum is spec number
Expand Down Expand Up @@ -166,8 +167,9 @@ namespace also
return total;
}

void sto_syn_manager::run()
mig_network sto_syn_manager::run()
{
mig_network mig;
if( verbose )
{
std::cout << " num_vars : " << num_vars << " \n"
Expand All @@ -182,48 +184,48 @@ namespace also
{
std::cout << "[i] Not trivial case, need further solve.\n";
}
else
{
return;
}

if( !preprocess() )
{
percy::spec spec;
also::mig3 mig3;

percy::spec spec;
also::mig3 mig3;
kitty::dynamic_truth_table tt( 4 );

kitty::dynamic_truth_table tt( 4 );
kitty::create_from_hex_string( tt, "17e8" );
spec[0] = tt;
spec.verbosity = 0;

kitty::create_from_hex_string( tt, "17e8" );
spec[0] = tt;
spec.verbosity = 0;
auto flag_normal = kitty::is_normal( tt );
if( !flag_normal ) { std::cout << "[i] Function is not normal \n"; }

auto flag_normal = kitty::is_normal( tt );
if( !flag_normal ) { std::cout << "[i] Function is not normal \n"; }

//stochastic problem vector
Problem_Vector_t instance;
instance.num_vars = num_vars;
instance.m = m;
instance.n = n;
instance.v = vector;

percy::bsat_wrapper solver;
mig_three_sto_encoder encoder( solver, instance );
//stochastic problem vector
Problem_Vector_t instance;
instance.num_vars = num_vars;
instance.m = m;
instance.n = n;
instance.v = vector;

percy::bsat_wrapper solver;
mig_three_sto_encoder encoder( solver, instance );

if( mig_three_sto_synthesize( spec, mig3, solver, encoder ) == percy::success )
{
print_all_expr( spec, mig3 );
if( mig_three_sto_synthesize( spec, mig3, solver, encoder ) == percy::success )
{
print_all_expr( spec, mig3 );
mig = mig3_to_mig_network( spec, mig3 );
}
}

return mig;
}

/******************************************************************************
* Public functions *
******************************************************************************/
void stochastic_synthesis( unsigned const& num_vars, unsigned const& m, unsigned const& n, std::vector<unsigned> const& vector )
mig_network stochastic_synthesis( unsigned const& num_vars, unsigned const& m, unsigned const& n, std::vector<unsigned> const& vector )
{
sto_syn_manager mgr( num_vars, m, n, vector );
mgr.run();
return mgr.run();
}

}
4 changes: 3 additions & 1 deletion src/core/exact_sto_m3ig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef EXACT_STO_M3IG_HPP
#define EXACT_STO_M3IG_HPP

#include <mockturtle/mockturtle.hpp>

namespace also
{
typedef struct Problem_Vector_t_ Problem_Vector_t;
Expand All @@ -26,7 +28,7 @@ namespace also
std::vector<unsigned> v;
};

void stochastic_synthesis( unsigned const& num_vars, unsigned const& m, unsigned const& n, std::vector<unsigned> const& vector );
mig_network stochastic_synthesis( unsigned const& num_vars, unsigned const& m, unsigned const& n, std::vector<unsigned> const& vector );

}

Expand Down
49 changes: 49 additions & 0 deletions src/core/m3ig_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,54 @@ namespace also
return ss.str();
}

mig_network mig3_to_mig_network( const spec& spec, mig3& mig3 )
{
mig_network mig;

std::vector<mig_network::signal> pis;

pis.push_back( mig.get_constant( false ) );
for( auto i = 0; i < spec.nr_in; i++ )
{
pis.push_back( mig.create_pi() );
}

for( auto i = 0; i < mig3.get_nr_steps(); i++ )
{
const auto& step = mig3.get_step_inputs( i );

auto tmp_in0 = pis[step[0]];
auto tmp_in1 = pis[step[1]];
auto tmp_in2 = pis[step[2]];

switch( mig3.get_op( i ) )
{
case 0:
pis.push_back( mig.create_maj( tmp_in0, tmp_in1, tmp_in2 ) );
break;

case 1:
pis.push_back( mig.create_maj( tmp_in0 ^ true, tmp_in1, tmp_in2 ) );
break;

case 2:
pis.push_back( mig.create_maj( tmp_in0, tmp_in1 ^ true, tmp_in2 ) );
break;

case 3:
pis.push_back( mig.create_maj( tmp_in0, tmp_in1, tmp_in2 ^ true ) );
break;

default:
assert( false && "ops are not known" );
break;
}
}

const auto driver = pis[ pis.size() - 1 ] ^ ( spec.out_inv ? true : false );
mig.create_po( driver );
return mig;
}

}

2 changes: 2 additions & 0 deletions src/core/m3ig_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ namespace also
std::string mig3_to_string( const spec& spec, const mig3& mig3 );
std::string print_expr( const mig3& mig3, const int& step_idx );
std::string print_all_expr( const spec& spec, const mig3& mig3 );

mig_network mig3_to_mig_network( const spec& spec, mig3& mig3 );

}

Expand Down

0 comments on commit ae66b1a

Please sign in to comment.