Skip to content

Commit d2cc1ce

Browse files
committed
added multi threading, progress printer and merged context
1 parent f6f41cd commit d2cc1ce

File tree

13 files changed

+1157
-981
lines changed

13 files changed

+1157
-981
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4+
// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5+
// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6+
// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
// SOFTWARE.
25+
26+
#pragma once
27+
28+
#include "hal_core/defines.h"
29+
30+
namespace hal
31+
{
32+
33+
class ProgressPrinter
34+
{
35+
public:
36+
ProgressPrinter(u32 max_message_size = 0);
37+
38+
void print_progress(float progress, const std::string& message = "");
39+
40+
void clear();
41+
42+
void reset();
43+
44+
private:
45+
// returns -1 if terminal width cannot be determined
46+
int get_terminal_width();
47+
48+
u32 m_printed_progress;
49+
std::string m_last_message;
50+
u32 m_bar_width;
51+
u32 m_max_message_size;
52+
int m_terminal_width; // no terminal found if negative
53+
};
54+
55+
} // namespace hal

plugins/machine_learning/include/machine_learning/features/gate_feature.h

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "hal_core/defines.h"
2-
#include "hal_core/netlist/decorators/netlist_abstraction_decorator.h"
2+
#include "machine_learning/types.h"
33

44
#include <optional>
55
#include <vector>
@@ -14,37 +14,19 @@ namespace hal
1414
{
1515
namespace gate_feature
1616
{
17-
struct FeatureContext
18-
{
19-
public:
20-
FeatureContext() = delete;
21-
FeatureContext(const Netlist* netlist) : nl(netlist){};
22-
23-
const Result<NetlistAbstraction*> get_sequential_abstraction();
24-
const Result<NetlistAbstraction*> get_original_abstraction();
25-
const std::vector<GateTypeProperty>& get_possible_gate_type_properties();
26-
27-
const Netlist* nl;
28-
29-
private:
30-
std::optional<NetlistAbstraction> m_sequential_abstraction;
31-
std::optional<NetlistAbstraction> m_original_abstraction;
32-
std::optional<std::vector<GateTypeProperty>> m_possible_gate_type_properties;
33-
};
34-
3517
class GateFeature
3618
{
3719
public:
38-
virtual Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g) const = 0;
39-
virtual std::string to_string() const = 0;
20+
virtual Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g) const = 0;
21+
virtual std::string to_string() const = 0;
4022
};
4123

4224
class ConnectedGlobalIOs : public GateFeature
4325
{
4426
public:
4527
ConnectedGlobalIOs(){};
4628

47-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g) const override;
29+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g) const override;
4830
std::string to_string() const override;
4931
};
5032

@@ -54,7 +36,7 @@ namespace hal
5436
DistanceGlobalIO(const PinDirection& direction, const bool directed = true, const std::vector<PinType>& forbidden_pin_types = {})
5537
: m_direction(direction), m_directed(directed), m_forbidden_pin_types(forbidden_pin_types){};
5638

57-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g) const override;
39+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g) const override;
5840
std::string to_string() const override;
5941

6042
private:
@@ -69,7 +51,7 @@ namespace hal
6951
SequentialDistanceGlobalIO(const PinDirection& direction, const bool directed = true, const std::vector<PinType>& forbidden_pin_types = {})
7052
: m_direction(direction), m_directed(directed), m_forbidden_pin_types(forbidden_pin_types){};
7153

72-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g) const override;
54+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g) const override;
7355
std::string to_string() const override;
7456

7557
private:
@@ -83,7 +65,7 @@ namespace hal
8365
public:
8466
IODegrees(){};
8567

86-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g) const override;
68+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g) const override;
8769
std::string to_string() const override;
8870
};
8971

@@ -92,7 +74,7 @@ namespace hal
9274
public:
9375
GateTypeOneHot(){};
9476

95-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g) const override;
77+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g) const override;
9678
std::string to_string() const override;
9779
};
9880

@@ -101,7 +83,7 @@ namespace hal
10183
public:
10284
NeighboringGateTypes(const u32 depth, const PinDirection& direction, const bool directed = true) : m_depth(depth), m_direction(direction), m_directed(directed){};
10385

104-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g) const override;
86+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g) const override;
10587
std::string to_string() const override;
10688

10789
private:
@@ -120,10 +102,10 @@ namespace hal
120102
// - distance to nearest bus register
121103

122104
Result<std::vector<u32>> build_feature_vec(const std::vector<const GateFeature*>& features, const Gate* g);
123-
Result<std::vector<u32>> build_feature_vec(FeatureContext& fc, const std::vector<const GateFeature*>& features, const Gate* g);
105+
Result<std::vector<u32>> build_feature_vec(Context& ctx, const std::vector<const GateFeature*>& features, const Gate* g);
124106

125107
Result<std::vector<std::vector<u32>>> build_feature_vecs(const std::vector<const GateFeature*>& features, const std::vector<Gate*>& gates);
126-
Result<std::vector<std::vector<u32>>> build_feature_vecs(FeatureContext& fc, const std::vector<const GateFeature*>& features, const std::vector<Gate*>& gates);
108+
Result<std::vector<std::vector<u32>>> build_feature_vecs(Context& ctx, const std::vector<const GateFeature*>& features, const std::vector<Gate*>& gates);
127109
} // namespace gate_feature
128110
} // namespace machine_learning
129111
} // namespace hal

plugins/machine_learning/include/machine_learning/features/gate_pair_feature.h

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include "hal_core/defines.h"
4-
#include "hal_core/netlist/decorators/netlist_abstraction_decorator.h"
54
#include "machine_learning/types.h"
65

76
#include <optional>
@@ -17,28 +16,11 @@ namespace hal
1716
{
1817
namespace gate_pair_feature
1918
{
20-
21-
struct FeatureContext
22-
{
23-
public:
24-
FeatureContext() = delete;
25-
FeatureContext(const Netlist* netlist) : nl(netlist){};
26-
27-
const Result<NetlistAbstraction*> get_original_abstraction();
28-
const Result<NetlistAbstraction*> get_sequential_abstraction();
29-
30-
const Netlist* nl;
31-
32-
private:
33-
std::optional<NetlistAbstraction> m_original_abstraction;
34-
std::optional<NetlistAbstraction> m_seqential_abstraction;
35-
};
36-
3719
class GatePairFeature
3820
{
3921
public:
40-
virtual Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const = 0;
41-
virtual std::string to_string() const = 0;
22+
virtual Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g_a, const Gate* g_b) const = 0;
23+
virtual std::string to_string() const = 0;
4224
};
4325

4426
class LogicalDistance : public GatePairFeature
@@ -47,7 +29,7 @@ namespace hal
4729
LogicalDistance(const PinDirection direction, const bool directed = true, const std::vector<PinType>& forbidden_pin_types = {})
4830
: m_direction(direction), m_directed(directed), m_forbidden_pin_types(forbidden_pin_types){};
4931

50-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
32+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g_a, const Gate* g_b) const override;
5133
std::string to_string() const override;
5234

5335
private:
@@ -62,7 +44,7 @@ namespace hal
6244
SequentialDistance(const PinDirection direction, const bool directed = true, const std::vector<PinType>& forbidden_pin_types = {})
6345
: m_direction(direction), m_directed(directed), m_forbidden_pin_types(forbidden_pin_types){};
6446

65-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
47+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g_a, const Gate* g_b) const override;
6648
std::string to_string() const override;
6749

6850
private:
@@ -76,7 +58,7 @@ namespace hal
7658
public:
7759
PhysicalDistance(){};
7860

79-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
61+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g_a, const Gate* g_b) const override;
8062
std::string to_string() const override;
8163
};
8264

@@ -85,7 +67,7 @@ namespace hal
8567
public:
8668
SharedControlSignals(){};
8769

88-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
70+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g_a, const Gate* g_b) const override;
8971
std::string to_string() const override;
9072
};
9173

@@ -99,7 +81,7 @@ namespace hal
9981
const std::vector<PinType>& forbidden_pin_types = {})
10082
: m_depth(depth), m_direction(direction), m_directed(directed), m_starting_pin_types(starting_pin_types), m_forbidden_pin_types(forbidden_pin_types){};
10183

102-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
84+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g_a, const Gate* g_b) const override;
10385
std::string to_string() const override;
10486

10587
private:
@@ -120,7 +102,7 @@ namespace hal
120102
const std::vector<PinType>& forbidden_pin_types = {})
121103
: m_depth(depth), m_direction(direction), m_directed(directed), m_starting_pin_types(starting_pin_types), m_forbidden_pin_types(forbidden_pin_types){};
122104

123-
Result<std::vector<u32>> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
105+
Result<std::vector<u32>> calculate_feature(Context& ctx, const Gate* g_a, const Gate* g_b) const override;
124106
std::string to_string() const override;
125107

126108
private:
@@ -132,13 +114,13 @@ namespace hal
132114
};
133115

134116
Result<std::vector<u32>> build_feature_vec(const std::vector<const GatePairFeature*>& features, const Gate* g_a, const Gate* g_b);
135-
Result<std::vector<u32>> build_feature_vec(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const Gate* g_a, const Gate* g_b);
117+
Result<std::vector<u32>> build_feature_vec(Context& ctx, const std::vector<const GatePairFeature*>& features, const Gate* g_a, const Gate* g_b);
136118

137119
Result<std::vector<u32>> build_feature_vec(const std::vector<const GatePairFeature*>& features, const std::pair<Gate*, Gate*>& gate_pair);
138-
Result<std::vector<u32>> build_feature_vec(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const std::pair<Gate*, Gate*>& gate_pair);
120+
Result<std::vector<u32>> build_feature_vec(Context& ctx, const std::vector<const GatePairFeature*>& features, const std::pair<Gate*, Gate*>& gate_pair);
139121

140122
Result<std::vector<std::vector<u32>>> build_feature_vecs(const std::vector<const GatePairFeature*>& features, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs);
141-
Result<std::vector<std::vector<u32>>> build_feature_vecs(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs);
123+
Result<std::vector<std::vector<u32>>> build_feature_vecs(Context& ctx, const std::vector<const GatePairFeature*>& features, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs);
142124
} // namespace gate_pair_feature
143125
} // namespace machine_learning
144126
} // namespace hal

0 commit comments

Comments
 (0)