AIG to npz

noah
panhongyang0 2024-03-04 18:02:58 +08:00
parent 2014ccb1e3
commit 5134f1969f
3 changed files with 245 additions and 0 deletions

71
src/commands/to_npz.hpp Normal file
View File

@ -0,0 +1,71 @@
/* phyLS: powerful heightened yielded Logic Synthesis
* Copyright (C) 2024 */
/**
* @file to_npz.hpp
*
* @brief transforms the current network into adjacency matrix(.npz)
*
* @author Homyoung
* @since 2024/03/01
*/
#ifndef TO_NPZ_HPP
#define TO_NPZ_HPP
#include <algorithm>
#include <mockturtle/networks/aig.hpp>
#include <vector>
#include "../core/tonpz.hpp"
using namespace std;
using namespace mockturtle;
namespace alice {
class write_npz_command : public command {
public:
explicit write_npz_command(const environment::ptr& env)
: command(env, "write npz file, default: AIG") {
add_flag("--xmg_network,-x", "write xmg_network into dot files");
add_flag("--mig_network,-m", "write mig_network into dot files");
add_flag("--klut_network,-l", "write klut_network into dot files");
add_option("--csv, -c", filename_csv,
"The path to store csv file, default: ./placement/test.csv");
add_option("--def, -d", filename_def,
"The path to store def file, default: "
"./placement/mfloorplan.def & ./placement/floorplan.def");
}
protected:
void execute() {
if (is_set("xmg_network")) {
xmg_network xmg = store<xmg_network>().current();
} else if (is_set("mig_network")) {
mig_network mig = store<mig_network>().current();
} else if (is_set("klut_network")) {
klut_network klut = store<klut_network>().current();
} else {
aig_network aig = store<aig_network>().current();
if (is_set("csv")) {
phyLS::write_npz(aig, filename_csv);
} else if (is_set("def")) {
phyLS::write_def(aig, filename_def, filename_mdef);
} else {
assert(false && "At least one filename should be specified. ");
}
}
}
private:
std::string filename_csv = "./placement/test.csv";
std::string filename_def = "./placement/ploorplan.def";
std::string filename_mdef = "./placement/mfloorplan.def";
};
ALICE_ADD_COMMAND(write_npz, "I/O")
} // namespace alice
#endif

170
src/core/tonpz.hpp Normal file
View File

@ -0,0 +1,170 @@
/* phyLS: powerful heightened yielded Logic Synthesis
* Copyright (C) 2024 */
/**
* @file to_npz.hpp
*
* @brief transforms the current network into adjacency matrix(.npz)
*
* @author Homyoung
* @since 2024/03/01
*/
#pragma once
#include <fmt/format.h>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <memory>
#include <mockturtle/networks/aig.hpp>
#include <mockturtle/views/depth_view.hpp>
#include <mockturtle/views/fanout_view.hpp>
#include <sstream>
#include <string>
using namespace std; // 名字空间
namespace phyLS {
template <class Ntk>
void write_npz(Ntk const& ntk, std::ostream& os) {
std::stringstream connect;
cout << "num_pis: " << ntk.num_pis() << endl;
cout << "num_pos: " << ntk.num_pos() << endl;
cout << "num_gates: " << ntk.num_gates() << endl;
int num_pis = ntk.num_pis(), num_pos = ntk.num_pos(),
num_gates = ntk.num_gates();
int size = num_pis + num_pos + num_gates;
vector<vector<double>> adj(size, vector<double>(size, 0));
ntk.foreach_node([&](auto const& n) {
if (ntk.is_constant(n) || ntk.is_ci(n)) return true;
ntk.foreach_fanin(n, [&](auto const& f) {
if (ntk.node_to_index(ntk.get_node(f)) <= num_pis) {
adj[ntk.node_to_index(n) - num_pis - 1]
[ntk.node_to_index(ntk.get_node(f)) + num_gates - 1] = 1;
adj[ntk.node_to_index(ntk.get_node(f)) + num_gates - 1]
[ntk.node_to_index(n) - num_pis - 1] = 1;
} else {
adj[ntk.node_to_index(n) - num_pis - 1]
[ntk.node_to_index(ntk.get_node(f)) - num_pis - 1] = 0.5;
adj[ntk.node_to_index(ntk.get_node(f)) - num_pis - 1]
[ntk.node_to_index(n) - num_pis - 1] = 0.5;
}
return true;
});
return true;
});
ntk.foreach_po([&](auto const& f, auto i) {
if (ntk.node_to_index(ntk.get_node(f)) <= num_pis) {
adj[i + num_pis + num_gates]
[ntk.node_to_index(ntk.get_node(f)) + num_gates - 1] = 1;
adj[ntk.node_to_index(ntk.get_node(f)) + num_gates - 1]
[i + num_pis + num_gates] = 1;
} else {
adj[i + num_pis + num_gates]
[ntk.node_to_index(ntk.get_node(f)) - num_pis - 1] = 0.5;
adj[ntk.node_to_index(ntk.get_node(f)) - num_pis - 1]
[i + num_pis + num_gates] = 0.5;
}
});
for (auto x : adj) {
for (int i = 0; i < x.size(); i++) {
if (i == x.size() - 1) {
connect << fmt::format("{}", x[i]);
} else {
connect << fmt::format("{}", x[i]) << ",";
}
}
connect << "\n";
}
os << connect.str();
}
template <class Ntk>
void write_def(Ntk const& ntk, std::ostream& os_def, std::ostream& os_mdef) {
std::stringstream components, pins, pins_def;
cout << "num_pis: " << ntk.num_pis() << endl;
cout << "num_pos: " << ntk.num_pos() << endl;
cout << "num_gates: " << ntk.num_gates() << endl;
int num_pis = ntk.num_pis(), num_pos = ntk.num_pos(),
num_gates = ntk.num_gates();
int size = num_pis + num_pos + num_gates;
int output_position = 0, input_position = 0, max_position = 10 * size;
components << fmt::format("COMPONENTS {} ;\n", num_gates);
ntk.foreach_node([&](auto const& n) {
if (ntk.is_constant(n) || ntk.is_ci(n)) return true;
components << fmt::format(" - and_{}_ and\n", ntk.node_to_index(n))
<< " + UNPLACED ;\n";
return true;
});
components << "END COMPONENTS\n"
<< "\n";
pins << fmt::format("PINS {} ;\n", num_pis + num_pos + 1);
pins_def << fmt::format("PINS {} ;\n", num_pis + num_pos + 1);
ntk.foreach_po([&](auto const& f, auto i) {
pins << fmt::format(" - output_{} + NET output_{}\n", i, i)
<< " + DIRECTION OUTPUT\n"
<< fmt::format(" + PLACED ( {} {} ) N\n", output_position * 10,
max_position)
<< " + LAYER metal3 ( 0 0 ) ( 510 100 ) ;\n";
pins_def << fmt::format(" - output_{} + NET output_{}\n", i, i)
<< " + DIRECTION OUTPUT\n"
<< fmt::format(" + PLACED ( {} {} ) N\n",
output_position * 10, max_position)
<< " + LAYER metal3 ( 0 0 ) ( 510 100 ) ;\n";
output_position++;
});
ntk.foreach_pi([&](auto const& f, auto i) {
pins << fmt::format(" - input_{} + NET input_{}\n", i, i)
<< " + DIRECTION INPUT\n"
<< fmt::format(" + PLACED ( {} {} ) N\n", input_position * 10, 0)
<< " + LAYER metal3 ( 0 0 ) ( 510 100 ) ;\n";
pins_def << fmt::format(" - input_{} + NET input_{}\n", i, i)
<< " + DIRECTION INPUT\n"
<< fmt::format(" + PLACED ( {} {} ) N\n", input_position * 10,
0)
<< " + LAYER metal3 ( 0 0 ) ( 510 100 ) ;\n";
input_position++;
});
pins << "END PINS\n";
pins_def << " - input_clk + NET input_clk\n"
<< " + DIRECTION INPUT\n"
<< fmt::format(" + PLACED ( {} {} ) N\n", input_position * 10,
0)
<< " + LAYER metal3 ( 0 0 ) ( 510 100 ) ;\n";
pins_def << "END PINS\n";
os_mdef << components.str() << pins.str();
os_def << components.str() << pins_def.str();
}
template <class Ntk>
void write_npz(Ntk const& ntk, std::string const& filename) {
std::ofstream os(filename.c_str(), std::ofstream::out);
write_npz(ntk, os);
os.close();
}
template <class Ntk>
void write_def(Ntk const& ntk, std::string const& filename_def,
std::string const& filename_mdef) {
std::ofstream os_def(filename_def.c_str(), std::ofstream::out);
std::ofstream os_mdef(filename_mdef.c_str(), std::ofstream::out);
write_def(ntk, os_def, os_mdef);
os_def.close();
os_mdef.close();
}
} // namespace phyLS

View File

@ -76,5 +76,9 @@
#include "commands/xag/xagrs.hpp" #include "commands/xag/xagrs.hpp"
#include "commands/xmg/xmgrs.hpp" #include "commands/xmg/xmgrs.hpp"
#include "commands/xmg/xmgrw.hpp" #include "commands/xmg/xmgrw.hpp"
#include "commands/exact/exact_multi.hpp"
#include "commands/exact/exact_klut.hpp"
#include "commands/exact/exactlut.hpp"
#include "commands/to_npz.hpp"
ALICE_MAIN(phyLS) ALICE_MAIN(phyLS)