Skip to content
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/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
add_library(mockturtle INTERFACE)
target_include_directories(mockturtle INTERFACE ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(mockturtle INTERFACE kitty lorina sparsepp percy json bill libabcesop abcresub)
target_link_libraries(mockturtle INTERFACE kitty lorina parallel_hashmap percy json bill libabcesop abcresub)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
target_link_libraries(mockturtle INTERFACE stdc++fs)
Expand Down
15 changes: 8 additions & 7 deletions include/mockturtle/algorithms/cell_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
#include <unordered_set>
#include <vector>

#include <parallel_hashmap/phmap.h>

#include "../networks/detail/foreach.hpp"
#include "../traits.hpp"
#include "../utils/algorithm.hpp"
#include "../utils/include/spp.hpp"
#include "../utils/node_map.hpp"
#include "../utils/stopwatch.hpp"

Expand Down Expand Up @@ -78,19 +79,19 @@ class cell_window_storage
_gates.reserve( _max_gates );
}

spp::sparse_hash_set<node<Ntk>> _nodes; /* cell roots in current window */
spp::sparse_hash_set<node<Ntk>> _gates; /* gates in current window */
spp::sparse_hash_set<node<Ntk>> _leaves; /* leaves of current window */
spp::sparse_hash_set<signal<Ntk>> _roots; /* roots of current window */
phmap::flat_hash_set<node<Ntk>> _nodes; /* cell roots in current window */
phmap::flat_hash_set<node<Ntk>> _gates; /* gates in current window */
phmap::flat_hash_set<node<Ntk>> _leaves; /* leaves of current window */
phmap::flat_hash_set<signal<Ntk>> _roots; /* roots of current window */

std::array<uint64_t, 2> _window_mask;
spp::sparse_hash_set<std::array<uint64_t, 2>> _window_hash;
phmap::flat_hash_set<std::array<uint64_t, 2>> _window_hash;

node_map<uint32_t, Ntk> _cell_refs; /* ref counts for cells */
node_map<std::vector<node<Ntk>>, Ntk> _cell_parents; /* parent cells */

std::vector<node<Ntk>> _index_to_node;
spp::sparse_hash_map<node<Ntk>, uint32_t> _node_to_index;
phmap::flat_hash_map<node<Ntk>, uint32_t> _node_to_index;

uint32_t _num_constants{1u};
uint32_t _max_gates{};
Expand Down
133 changes: 63 additions & 70 deletions include/mockturtle/io/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#pragma once

#include <mockturtle/networks/aig.hpp>
#include <parallel_hashmap/phmap_dump.h>
#include <fstream>

namespace mockturtle
Expand All @@ -54,49 +55,43 @@ struct serializer
using pointer_type = typename node_type::pointer_type;

public:
bool operator()( std::ostream *os, uint64_t const& data ) const
bool operator()( phmap::BinaryOutputArchive& os, uint64_t const& data ) const
{
os->write( (char*)&data, sizeof( uint64_t ) );
return os->good();
return os.dump( (char*)&data, sizeof( uint64_t ) );
}

bool operator()( std::istream *is, uint64_t* data ) const
bool operator()( phmap::BinaryInputArchive& ar_input, uint64_t* data ) const
{
is->read( (char*)data, sizeof( uint64_t ) );
return is->good();
return ar_input.load( (char*)data, sizeof( uint64_t ) );
}

template<int PointerFieldSize>
bool operator()( std::ostream *os, node_pointer<PointerFieldSize> const& ptr ) const
bool operator()( phmap::BinaryOutputArchive& os, node_pointer<PointerFieldSize> const& ptr ) const
{
os->write( (char*)&ptr.data, sizeof( ptr.data ) );
return os->good();
return os.dump( (char*)&ptr.data, sizeof( ptr.data ) );
}

template<int PointerFieldSize>
bool operator()( std::istream *is, node_pointer<PointerFieldSize>* ptr ) const
bool operator()( phmap::BinaryInputArchive& ar_input, node_pointer<PointerFieldSize>* ptr ) const
{
is->read( (char*)&ptr->data, sizeof( ptr->data ) );
return is->good();
return ar_input.load( (char*)&ptr->data, sizeof( ptr->data ) );
}

bool operator()( std::ostream *os, cauint64_t const& data ) const
bool operator()( phmap::BinaryOutputArchive& os, cauint64_t const& data ) const
{
os->write( (char*)&data.n, sizeof( data.n ) );
return os->good();
return os.dump( (char*)&data.n, sizeof( data.n ) );
}

bool operator()( std::istream *is, cauint64_t* data ) const
bool operator()( phmap::BinaryInputArchive& ar_input, cauint64_t* data ) const
{
is->read( (char*)&data->n, sizeof( data->n ) );
return is->good();
return ar_input.load( (char*)&data->n, sizeof( data->n ) );
}

template<int Fanin, int Size, int PointerFieldSize>
bool operator()( std::ostream *os, regular_node<Fanin, Size, PointerFieldSize> const& n ) const
bool operator()( phmap::BinaryOutputArchive& os, regular_node<Fanin, Size, PointerFieldSize> const& n ) const
{
uint64_t size = n.children.size();
os->write( (char*)&size, sizeof( uint64_t ) );
os.dump( (char*)&size, sizeof( uint64_t ) );

for ( const auto& c : n.children )
{
Expand All @@ -108,7 +103,7 @@ struct serializer
}

size = n.data.size();
os->write( (char*)&size, sizeof( uint64_t ) );
os.dump( (char*)&size, sizeof( uint64_t ) );
for ( const auto& d : n.data )
{
bool result = this->operator()( os, d );
Expand All @@ -118,55 +113,55 @@ struct serializer
}
}

return os->good();
return true;
}

template<int Fanin, int Size, int PointerFieldSize>
bool operator()( std::istream *is, const regular_node<Fanin, Size, PointerFieldSize>* n ) const
bool operator()( phmap::BinaryInputArchive& ar_input, const regular_node<Fanin, Size, PointerFieldSize>* n ) const
{
uint64_t size;
is->read( (char*)&size, sizeof( uint64_t ) );
ar_input.load( (char*)&size, sizeof( uint64_t ) );
for ( uint64_t i = 0; i < size; ++i )
{
pointer_type ptr;
bool result = this->operator()( is, &ptr );
bool result = this->operator()( ar_input, &ptr );
if ( !result )
{
return false;
}
const_cast<regular_node<Fanin, Size, PointerFieldSize>*>( n )->children[i] = ptr;
}

is->read( (char*)&size, sizeof( uint64_t ) );
ar_input.load( (char*)&size, sizeof( uint64_t ) );
for ( uint64_t i = 0; i < size; ++i )
{
cauint64_t data;
bool result = this->operator()( is, &data );
bool result = this->operator()( ar_input, &data );
if ( !result )
{
return false;
}
const_cast<regular_node<Fanin, Size, PointerFieldSize>*>( n )->data[i] = data;
}

return is->good();
return true;
}

bool operator()( std::ostream *os, std::pair<const node_type, uint64_t> const& value ) const
bool operator()( phmap::BinaryOutputArchive& os, std::pair<const node_type, uint64_t> const& value ) const
{
return this->operator()( os, value.first ) && this->operator()( os, value.second );
}

bool operator()( std::istream *is, std::pair<const node_type, uint64_t>* value ) const
bool operator()( phmap::BinaryInputArchive& ar_input, std::pair<const node_type, uint64_t>* value ) const
{
return this->operator()( is, &value->first ) && this->operator()( is, &value->second );
return this->operator()( ar_input, &value->first ) && this->operator()( ar_input, &value->second );
}

bool operator()( std::ostream *os, aig_storage const& storage ) const
bool operator()( phmap::BinaryOutputArchive& os, aig_storage const& storage ) const
{
/* nodes */
uint64_t size = storage.nodes.size();
os->write( (char*)&size, sizeof( uint64_t ) );
os.dump( (char*)&size, sizeof( uint64_t ) );
for ( const auto& n : storage.nodes )
{
if ( !this->operator()( os, n ) )
Expand All @@ -177,7 +172,7 @@ struct serializer

/* inputs */
size = storage.inputs.size();
os->write( (char*)&size, sizeof( uint64_t ) );
os.dump( (char*)&size, sizeof( uint64_t ) );
for ( const auto& i : storage.inputs )
{
if ( !this->operator()( os, i ) )
Expand All @@ -188,7 +183,7 @@ struct serializer

/* outputs */
size = storage.outputs.size();
os->write( (char*)&size, sizeof( uint64_t ) );
os.dump( (char*)&size, sizeof( uint64_t ) );
for ( const auto& o : storage.outputs )
{
if ( !this->operator()( os, o ) )
Expand All @@ -198,93 +193,95 @@ struct serializer
}

/* hash */
const_cast<aig_storage&>( storage ).hash.serialize( *this, os );
if (! const_cast<aig_storage&>( storage ).hash.dump( os ) )
{
return false;
}

/* storage data */
os->write( (char*)&storage.data.num_pis, sizeof( uint32_t ) );
os->write( (char*)&storage.data.num_pos, sizeof( uint32_t ) );
os.dump( (char*)&storage.data.num_pis, sizeof( uint32_t ) );
os.dump( (char*)&storage.data.num_pos, sizeof( uint32_t ) );
size = storage.data.latches.size();
for ( const auto& l : storage.data.latches )
{
os->write( (char*)&l, sizeof( int8_t ) );
os.dump( (char*)&l, sizeof( int8_t ) );
}
os->write( (char*)&storage.data.trav_id, sizeof( uint32_t ) );
os.dump( (char*)&storage.data.trav_id, sizeof( uint32_t ) );

return true;
}

bool operator()( std::istream *is, aig_storage* storage ) const
bool operator()( phmap::BinaryInputArchive& ar_input, aig_storage* storage ) const
{
/* nodes */
uint64_t size;
is->read( (char*)&size, sizeof( uint64_t ) );
ar_input.load( (char*)&size, sizeof( uint64_t ) );
for ( uint64_t i = 0; i < size; ++i )
{
node_type n;
if ( !this->operator()( is, &n ) )
if ( !this->operator()( ar_input, &n ) )
{
return false;
}
storage->nodes.push_back( n );
}

/* inputs */
is->read( (char*)&size, sizeof( uint64_t ) );
ar_input.load( (char*)&size, sizeof( uint64_t ) );
for ( uint64_t i = 0; i < size; ++i )
{
uint64_t value;
is->read( (char*)&value, sizeof( uint64_t ) );
ar_input.load( (char*)&value, sizeof( uint64_t ) );
storage->inputs.push_back( value );
}

/* outputs */
is->read( (char*)&size, sizeof( uint64_t ) );
ar_input.load( (char*)&size, sizeof( uint64_t ) );
for ( uint64_t i = 0; i < size; ++i )
{
pointer_type ptr;
if ( !this->operator()( is, &ptr ) )
if ( !this->operator()( ar_input, &ptr ) )
{
return false;
}
storage->outputs.push_back( ptr );
}

/* hash */
if ( !storage->hash.unserialize( *this, is ) )
if ( !storage->hash.load( ar_input ) )
{
return false;
}

/* aig_storage_data */
is->read( (char*)&storage->data.num_pis, sizeof( uint32_t ) );
is->read( (char*)&storage->data.num_pos, sizeof( uint32_t ) );
is->read( (char*)&size, sizeof( uint64_t ) );
ar_input.load( (char*)&storage->data.num_pis, sizeof( uint32_t ) );
ar_input.load( (char*)&storage->data.num_pos, sizeof( uint32_t ) );
ar_input.load( (char*)&size, sizeof( uint64_t ) );
for ( uint64_t i = 0; i < size; ++i )
{
int8_t l;
is->read( (char*)&l, sizeof( int8_t ) );
ar_input.load( (char*)&l, sizeof( int8_t ) );
storage->data.latches.push_back( l );
}
is->read( (char*)&storage->data.trav_id, sizeof( uint32_t ) );
ar_input.load( (char*)&storage->data.trav_id, sizeof( uint32_t ) );

return true;
}
}; /* struct serializer */

} /* namespace detail */

/*! \brief Serializes a combinational AIG network to a stream
/*! \brief Serializes a combinational AIG network to a archive
*
* \param aig Combinational AIG network
* \param os Output stream
* \param os Output archive
*/
inline std::ostream& serialize_network( aig_network const& aig, std::ostream& os )
inline void serialize_network( aig_network const& aig, phmap::BinaryOutputArchive& os )
{
detail::serializer _serializer;
bool const okay = _serializer( &os, *aig._storage );
bool const okay = _serializer( os, *aig._storage );
(void)okay;
assert( okay && "failed to serialize the network onto stream" );
return os;
}

/*! \brief Serializes a combinational AIG network in a file
Expand All @@ -294,18 +291,16 @@ inline std::ostream& serialize_network( aig_network const& aig, std::ostream& os
*/
inline void serialize_network( aig_network const& aig, std::string const& filename )
{
std::ofstream os;
os.open( filename, std::fstream::out | std::fstream::binary );
serialize_network( aig, os );
os.close();
phmap::BinaryOutputArchive ar_out(filename.c_str());
serialize_network( aig, ar_out );
}

/*! \brief Deserializes a combinational AIG network from a stream
/*! \brief Deserializes a combinational AIG network from a input archive
*
* \param is Input stream
* \param ar_input Input archive
* \return Deserialized AIG network
*/
inline aig_network deserialize_network( std::istream& is )
inline aig_network deserialize_network( phmap::BinaryInputArchive& ar_input )
{
detail::serializer _serializer;
auto storage = std::make_shared<aig_storage>();
Expand All @@ -315,7 +310,7 @@ inline aig_network deserialize_network( std::istream& is )
storage->latch_information.clear();
storage->hash.clear();

bool const okay = _serializer( &is, storage.get() );
bool const okay = _serializer( ar_input, storage.get() );
(void)okay;
assert( okay && "failed to deserialize the network onto stream" );
return aig_network{storage};
Expand All @@ -328,10 +323,8 @@ inline aig_network deserialize_network( std::istream& is )
*/
inline aig_network deserialize_network( std::string const& filename )
{
std::ifstream is;
is.open( filename, std::fstream::in | std::fstream::binary );
auto aig = deserialize_network( is );
is.close();
phmap::BinaryInputArchive ar_input(filename.c_str());
auto aig = deserialize_network( ar_input );
return aig;
}

Expand Down
4 changes: 2 additions & 2 deletions include/mockturtle/networks/abstract_xag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
#include <fmt/format.h>
#include <kitty/dynamic_truth_table.hpp>
#include <kitty/operators.hpp>
#include <parallel_hashmap/phmap.h>

#include "../traits.hpp"
#include "../utils/algorithm.hpp"
#include "../utils/include/spp.hpp"
#include "detail/foreach.hpp"

namespace mockturtle
Expand Down Expand Up @@ -101,7 +101,7 @@ struct abstract_xag_storage
std::vector<uint32_t> children;
std::vector<uint32_t> inputs;
std::vector<std::pair<uint32_t, bool>> outputs;
spp::sparse_hash_map<node_type, uint32_t, abstract_xag_node_hash, abstract_xag_node_eq> hash;
phmap::flat_hash_map<node_type, uint32_t, abstract_xag_node_hash, abstract_xag_node_eq> hash;

uint32_t trav_id = 0u;
uint32_t depth = 0u;
Expand Down
Loading