Skip to content

Commit bf2b964

Browse files
authored
Merge 3118272 into 128c19a
2 parents 128c19a + 3118272 commit bf2b964

File tree

47 files changed

+963
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+963
-58
lines changed

Makefile

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ yosys/yosys:
1515
run: cgploss.so
1616
yosys/yosys -m cgploss.so
1717

18-
tests: cgploss.so
19-
@echo "[info] starting implementation tests"
18+
test: cgploss.so
19+
@echo "[info] starting implementation test $(dir)"
2020
@EXIT_CODE=0
21-
for f in ./tests/*; do \
21+
for f in ./tests/$(dir); do \
2222
if [ -d "$$f" -a $$(echo -n "$$f" | tail -c 1) != "-" -a $$(echo -n "$$f" | tail -c 4) != "unit" ]; then \
2323
#echo "$$f STARTED" && \
2424
yosys/yosys -m cgploss.so < "$$f/run" > test_run.txt || { echo -e "$$f \e[31mFAILED\e[0m" ; EXIT_CODE=1; continue; } && \
@@ -35,35 +35,14 @@ tests: cgploss.so
3535
fi \
3636
done
3737
# remove tests outputs
38-
rm -f test_design
39-
rm -f test_run.txt
40-
rm -f test_out.v
41-
@echo "[info] implementation tests done"
38+
@echo "[info] implementation test $(dir) done"
4239
@exit $$EXIT_CODE
4340

44-
stop-tests: cgploss.so
45-
@echo "[info] starting implementation tests"
46-
for f in ./tests/*; do \
47-
if [ -d "$$f" -a $$(echo -n "$$f" | tail -c 1) != "-" -a $$(echo -n "$$f" | tail -c 4) != "unit" ]; then \
48-
#echo "$$f STARTED" && \
49-
yosys/yosys -m cgploss.so < "$$f/run" > test_run.txt || { echo -e "$$f \e[31mFAILED\e[0m" ; exit 1; } && \
50-
iverilog -o test_design test_out.v "$$f/tb.v" || { echo -e "$$f \e[31mFAILED\e[0m" ; exit 1; } && \
51-
vvp test_design || { echo -e "$$f \e[31mFAILED\e[0m" ; exit 1; } && \
52-
echo -e "$$f \e[32mPASS\e[0m"; \
53-
elif [ -d "$$f" -a $$(echo -n "$$f" | tail -c 1) != "-" -a $$(echo -n "$$f" | tail -c 4) == "unit" ]; then \
54-
#echo "$$f STARTED UNIT TEST" && \
55-
cp $$f/test.cpp src/test.cpp && \
56-
yosys/yosys-config --exec --cxx --cxxflags --ldflags -o cgplossUnit.so -shared $(TEST_SRC) -I yosys/ -I include/ --ldlibs > test_run.txt 2>&1 || { echo -e "$$f \e[31mFAILED\e[0m" ; exit 1; } && \
57-
rm -f src/test.cpp && \
58-
yosys/yosys -m cgplossUnit.so < "$$f/run" >> test_run.txt || { echo -e "$$f \e[31mFAILED\e[0m" ; exit 1; } && \
59-
echo -e "$$f \e[32mPASS\e[0m"; \
60-
fi \
61-
done
62-
# remove tests outputs
41+
tests:
42+
make test dir=*
6343
rm -f test_design
6444
rm -f test_run.txt
6545
rm -f test_out.v
66-
@echo "[info] implementation tests done"
6746

6847
clean:
6948
rm -f cgploss.so

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# CGP Loss optimalization YOSYS extension
1+
# CGP Loss optimalization YOSYS extension (CGP Aproximation)
22

3-
use genetic algoritms for optimalize circuics with accepted error on output or without it.
3+
Use genetic algoritms for optimalize/aproximate circuics with accepted error on output or without it. This tool was created as a part of bachelor thesis at BUT FIT, this thesis is available at https://www.fit.vut.cz/study/thesis/22357/ .
44

55

66
## Getting Started
@@ -100,7 +100,7 @@ make tests
100100
cgploss.so build cgploss extension (default)
101101
multicore build cgploss extension with multicore support
102102
tests run tests
103-
stop-tests run tests when fail stops
103+
test dir=name start test with name
104104
yosys/yosys build yosys submodule
105105
run run yosys with extension
106106
clean clean repo

include/about.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace about {
1010

1111
const std::string name = "cgploss";
12-
const unsigned version = 1;
12+
const unsigned version = 2;
1313
const std::string authors = "Lukas Plevac <xpleva07@vutbr.cz, lukas@plevac.eu>";
1414
const std::string build_time = __DATE__ " " __TIME__;
1515
const int optimalization = __OPTIMIZE_SIZE__;

include/config-parse.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ namespace config {
2020
/**
2121
* @brief Return weight of port
2222
* @param port SigBit of port
23-
* @return unsigned weight
23+
* @return float weight
2424
*/
25-
unsigned port_weight(Yosys::RTLIL::SigBit &port);
25+
float port_weight(Yosys::RTLIL::SigBit &port);
2626

2727
/**
2828
* @brief Parse file with port weights specifications

include/simulation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ namespace simulation {
7474
* @param xor_outputs xor between reference and individual output
7575
* @param rtl_ports map between chromozome index and RTLIL SigBit
7676
* @param config_parse config parser with port weights
77-
* @return unsigned maximal error
77+
* @return float maximal error
7878
*/
79-
unsigned one_max_loss(std::vector<io_t> xor_outputs, std::map<genome::io_id_t, Yosys::RTLIL::SigBit> rtl_ports, config::parse *config_parse);
79+
float one_max_loss(std::vector<io_t> xor_outputs, std::map<genome::io_id_t, Yosys::RTLIL::SigBit> rtl_ports, config::parse *config_parse);
8080

8181
}

include/types.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* yosys-cgploss - Create circuics using Genetic (CGP)
3+
* support data types header file
4+
* @author Lukas Plevac <xpleva07@vutbr.cz>
5+
*/
6+
7+
#pragma once
8+
9+
namespace types {
10+
/**
11+
* @brief Reprezenting number as 2**exp
12+
*/
13+
class Exponent {
14+
public:
15+
/**
16+
* @brief Exponent of reprezented number
17+
*/
18+
unsigned exp;
19+
20+
Exponent(unsigned exponent) {
21+
this->exp = exponent;
22+
}
23+
24+
Exponent() {
25+
this->exp = 0;
26+
}
27+
28+
/**
29+
* Get real value of number
30+
* @return unsigned
31+
*/
32+
unsigned real() {
33+
return 1 << this->exp;
34+
}
35+
36+
Exponent operator*(const Exponent& b) {
37+
Exponent c(this->exp);
38+
c.exp += b.exp;
39+
return c;
40+
}
41+
42+
Exponent operator/(const Exponent& b) {
43+
Exponent c(this->exp);
44+
c.exp -= b.exp;
45+
return c;
46+
}
47+
48+
49+
friend float operator/(const float& lhs, const Exponent& rhs) {
50+
unsigned do_div = rhs.exp;
51+
float out = lhs;
52+
53+
while (do_div) {
54+
out = out / 2;
55+
do_div--;
56+
}
57+
58+
return out;
59+
}
60+
61+
};
62+
}

src/config-parse.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "config-parse.h"
99
#include <algorithm>
10+
#include <math.h>
1011

1112
namespace config {
1213
unsigned parse::parse_file(std::ifstream &config_file, std::map<genome::io_id_t, Yosys::RTLIL::SigBit> &rtl_ports) {
@@ -35,15 +36,15 @@ namespace config {
3536
return true;
3637
}
3738

38-
unsigned parse::port_weight(Yosys::RTLIL::SigBit &port) {
39+
float parse::port_weight(Yosys::RTLIL::SigBit &port) {
3940
if (this->ports.count(port.wire->name.str()) != 0) {
4041
if ((int) this->ports[port.wire->name.str()].size() > port.offset) {
4142
return this->ports[port.wire->name.str()][port.offset];
4243
}
4344
}
4445

4546
//default for unknew
46-
return 1 << port.offset;
47+
return exp2(port.offset);
4748
}
4849

4950
unsigned parse::wire_bits(const std::string& name, std::map<genome::io_id_t, Yosys::RTLIL::SigBit> &rtl_ports) {

src/generation.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
#include "generation.h"
9+
#include "types.h"
910
#include <random>
1011
#include <algorithm>
1112

@@ -81,7 +82,8 @@ namespace evolution {
8182

8283
this->individuals[index].mae = 0;
8384
this->individuals[index].wce = 0;
84-
unsigned variants_count = 1 << TO_REAL_INPUT(this->individuals[index].repres->chromosome->last_input + 1);
85+
types::Exponent variants_count(TO_REAL_INPUT(this->individuals[index].repres->chromosome->last_input + 1));
86+
unsigned variants_count_sim = (ONE_SIM_VARIANTS >= variants_count.exp) ? 1 << variants_count.exp : 1 << ONE_SIM_VARIANTS;
8587

8688
std::vector<simulation::io_t> xor_outputs(this->individuals[index].repres->chromosome->wire_out.size());
8789
std::vector<simulation::io_t> test_circuic(this->individuals[index].repres->chromosome->size());
@@ -100,10 +102,10 @@ namespace evolution {
100102
SET_VARIANTS_BITS(reference_circuic[i].vec, TO_REAL_INPUT(i));
101103
}
102104

103-
unsigned total_error = 0;
105+
float total_error = 0;
104106
bool done = false;
105107

106-
if (ONE_SIM_VARIANTS > TO_REAL_INPUT(this->individuals[index].repres->chromosome->last_input + 1)) {
108+
if (ONE_SIM_VARIANTS >= variants_count.exp) {
107109
done = true;
108110
}
109111

@@ -115,7 +117,7 @@ namespace evolution {
115117
unsigned i = 0;
116118
for (auto output: this->individuals[index].repres->chromosome->wire_out) {
117119
xor_outputs[i].vec = test_circuic[output.first].vec ^ reference_circuic[this->reference_inverse_wire_out[output.second]].vec;
118-
total_error += simulation::bits_count(xor_outputs[i], variants_count) * config_parse->port_weight(output.second);
120+
total_error += simulation::bits_count(xor_outputs[i], variants_count_sim) * config_parse->port_weight(output.second);
119121
i++;
120122
}
121123

@@ -131,7 +133,7 @@ namespace evolution {
131133
}
132134

133135
//Update inputs for next simulation
134-
for (unsigned i = ONE_SIM_VARIANTS; i <= TO_REAL_INPUT(this->individuals[index].repres->chromosome->last_input); i++) {
136+
for (unsigned i = ONE_SIM_VARIANTS; i < TO_REAL_INPUT(this->individuals[index].repres->chromosome->last_input + 1); i++) {
135137
variant_counter[i] = (variant_counter[i] + 1) % (1 << (i - ONE_SIM_VARIANTS));
136138

137139
if (variant_counter[i] == 0) {
@@ -149,7 +151,7 @@ namespace evolution {
149151
}
150152
} while (!done);
151153

152-
this->individuals[index].mae = (float) total_error / variants_count;
154+
this->individuals[index].mae = total_error / variants_count;
153155

154156
if (this->individuals[index].mae > this->max_abs_loss) {
155157
this->individuals[index].score = INFINITY;

src/simulation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ namespace simulation {
2626
return count;
2727
}
2828

29-
unsigned one_max_loss(std::vector<io_t> xor_outputs, std::map<genome::io_id_t, Yosys::RTLIL::SigBit> rtl_ports, config::parse *config_parse) {
30-
unsigned max = 0;
29+
float one_max_loss(std::vector<io_t> xor_outputs, std::map<genome::io_id_t, Yosys::RTLIL::SigBit> rtl_ports, config::parse *config_parse) {
30+
float max = 0;
3131

3232
for (unsigned byte = 0; byte < SIM_BYTES_COUNT; byte++) {
3333
for (unsigned bit = 0; bit < 8; bit++) {
3434
unsigned port_id = 0;
35-
unsigned count = 0;
35+
float count = 0;
3636
for (auto output: rtl_ports) {
3737
count += (xor_outputs[port_id].byte[byte] & 1) * config_parse->port_weight(output.second);
3838
xor_outputs[port_id].byte[byte] = xor_outputs[port_id].byte[byte] >> 1;

tests/128bparity-gates-cgp/code.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module paritydev (input [127:0] a,
2+
output par);
3+
assign par = ^a;
4+
endmodule

tests/128bparity-gates-cgp/run

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
read_verilog tests/128bparity-gates-cgp/code.v
2+
techmap
3+
opt
4+
abc
5+
cgploss -generations=3 -generation_size=3 -selection_size=1 -representation=gates
6+
write_verilog test_out.v

tests/128bparity-gates-cgp/tb.v

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module fulladder_tb;
2+
task assert(input condition);
3+
if(!condition)
4+
$fatal;
5+
endtask
6+
7+
reg [127:0] a;
8+
reg par_exp;
9+
wire par;
10+
11+
integer i;
12+
13+
paritydev UUT(
14+
.a(a),
15+
.par(par)
16+
);
17+
18+
initial begin
19+
a <= 0;
20+
21+
for (i = 0; i < 131072; i = i + 1) begin
22+
a <= {$random, $random, $random, $random};
23+
24+
#10
25+
26+
par_exp = ^a;
27+
28+
#10
29+
30+
assert(par === par_exp);
31+
end
32+
end
33+
endmodule

0 commit comments

Comments
 (0)