-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.h
153 lines (126 loc) · 3.66 KB
/
benchmark.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* benckmark.h
*
* Created on: 4 Jan 2024
* Author: massimiliano
*/
#ifndef BENCHMARK_H_
#define BENCHMARK_H_
#include "seal/seal.h"
#include <string>
#include <iostream>
#include <fstream>
#include <ctime>
#include "utils.h"
/*
* Encryption mode
*/
typedef enum {symmetric, asymmetric} encryption_mode;
/*
* Benchmark parameter
*/
typedef struct {
encryption_mode enc_mode;
seal::sec_level_type sec_level;
size_t poly_modulus_degree;
std::vector<int> modulus_bit_sizes;
} benchmark_parms;
/*
* Benchmark return/outcome
*/
typedef struct {
std::string setting;
bool check = true;
std::string text;
} benchmark_return;
/*
* Kernel benchmark stats result
*/
typedef struct {
std::string kernel;
uint32_t run;
double max;
double min;
double avg;
double s_dev;
double q1;
double q3;
double iqr;
double lower;
double upper;
double avg_o; // average without outlier
double s_dev_o; // standard deviation without outlier
uint32_t cnt_o; // outlier count
double perc_o; // outlier percentage
} kernel_stats;
/*
* Benchmark stats result
*/
typedef struct {
std::string setting;
std::vector<kernel_stats> stats;
} benchmark_statistics;
inline std::string benchmark_parms_to_string(benchmark_parms benchmark){
std::string mode = (benchmark.enc_mode==symmetric?"symmetric":"asymmetric");
std::string modulus;
for(auto prime : benchmark.modulus_bit_sizes){
modulus += std::to_string(prime) + ";";
}
modulus.pop_back();
return
"Mode=" + mode +
", Level=" + std::to_string((int)benchmark.sec_level) +
", Degree=" + std::to_string(benchmark.poly_modulus_degree) +
", Modulus={" + modulus + "}";
};
inline std::string benchmark_return_to_string(benchmark_return ret){
return ret.setting + ", precision=" + std::to_string(ret.check) + (!ret.text.empty()? (", " + ret.text) : (""));
};
inline kernel_stats calculate_kernel_stats(std::string kernel, std::vector<uint32_t> execs){
kernel_stats stats;
stats.kernel = kernel;
stats.run = count_value(execs, 0.01);
stats.max = max_val(execs);
stats.min = min_val(execs);
stats.avg = average(execs, 0.01, std::numeric_limits<double>::max());
stats.s_dev = std_deviation(execs, 0.01, std::numeric_limits<double>::max());
// outlier management
stats.q1 = percentile(execs, 0.25);
stats.q3 = percentile(execs, 0.75);
stats.iqr = stats.q3 - stats.q1;
stats.lower = stats.q1 - (1.5*stats.iqr);
stats.upper = stats.q3 + (1.5*stats.iqr);
stats.avg_o = average(execs, stats.lower, stats.upper);
stats.s_dev_o = std_deviation(execs, stats.lower, stats.upper);
stats.cnt_o = execs.size() - count_value(execs, stats.lower, stats.upper);
stats.perc_o = 100.0 * (double)stats.cnt_o / (double)stats.run;
return stats;
};
inline std::string kernel_stats_to_string(kernel_stats stats){
return stats.kernel + "\t\t" +
std::to_string(stats.avg) + "\t" +
std::to_string(stats.s_dev) + "\t" +
std::to_string(stats.max) + "\t" +
std::to_string(stats.min) + "\t" +
std::to_string(stats.run) + "\t" +
std::to_string(stats.avg_o) + "\t" +
std::to_string(stats.s_dev_o) + "\t" +
std::to_string(stats.cnt_o) + "\t" +
std::to_string(stats.perc_o) + "%";
}
inline std::string benckmark_statistics_to_string(benchmark_statistics stats){
std::string s = stats.setting + "\n";
for(const auto &value : stats.stats){
s+= kernel_stats_to_string(value) + "\n";
}
return s;
}
/*
* benchmark function
*/
// calculate kernel computation times elaborated on expression -(A^2 + BC + 3D + coeff)
void ckks_kernel_benchmark_expr();
// calculate kernel computation times elaborated as example/8_performance.cpp
void ckks_kernel_benchmark_seal();
void example_ckks_performance_default();
#endif /* BENCHMARK_H_ */