-
Notifications
You must be signed in to change notification settings - Fork 11
/
exprtk_rtc_overhead.cpp
307 lines (245 loc) · 9.35 KB
/
exprtk_rtc_overhead.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* ExprTk Vector RTC Overhead Analysis *
* Author: Arash Partow (1999-2024) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
struct vector_access_rtc final : public exprtk::vector_access_runtime_check
{
bool handle_runtime_violation(violation_context&) override
{
throw std::runtime_error("Runtime vector access violation.");
return false;
}
};
struct assert_handler final : public exprtk::assert_check
{
void handle_assert(const assert_context& /*context*/) override
{
throw std::runtime_error("assert: vector access violation.");
}
};
template <typename T>
void vector_rtc_overhead_analysis_01()
{
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string sieve_of_eratosthenes_program =
" var sieve[10^8] := [false]; "
" var m := trunc(sqrt(sieve[])); "
" "
" sieve[0] := true; "
" sieve[1] := true; "
" "
" for (var i := 0; i <= m; i += 1) "
" { "
" assert(i < sieve[]); "
" if (false == sieve[i]) "
" { "
" for (var j := i^2; j < sieve[]; j += i) "
" { "
" assert(j < sieve[]); "
" sieve[j] := true; "
" } "
" } "
" }; "
" "
" var prime_count := sieve[] - sum(sieve); "
" "
" prime_count == 5761455; ";
T nortc_result = T(0);
T rtc_result = T(0);
T assert_result = T(0);
T nortc_time_sec = T(0);
T rtc_time_sec = T(0);
T assert_time_sec = T(0);
{
expression_t expression;
parser_t parser;
parser.compile(sieve_of_eratosthenes_program,expression);
exprtk::timer timer;
timer.start();
nortc_result = expression.value();
timer.stop();
nortc_time_sec = timer.time();
}
{
vector_access_rtc vec_rtc;
expression_t expression;
parser_t parser;
parser.register_vector_access_runtime_check(vec_rtc);
parser.compile(sieve_of_eratosthenes_program,expression);
exprtk::timer timer;
timer.start();
rtc_result = expression.value();
timer.stop();
rtc_time_sec = timer.time();
}
{
assert_handler asrt_handler;
expression_t expression;
parser_t parser;
parser.register_assert_check(asrt_handler);
parser.compile(sieve_of_eratosthenes_program,expression);
exprtk::timer timer;
timer.start();
assert_result = expression.value();
timer.stop();
assert_time_sec = timer.time();
}
if (
(nortc_result == T(1) ) &&
(nortc_result == rtc_result ) &&
(nortc_result == assert_result)
)
{
printf("Prime Sieve - NORTC time: %5.3fsec\tRTC time: %5.3fsec\tassert time: %5.3fsec\trtc_overhead: %5.3f%%\tassert_overhead: %5.3f%%\n",
nortc_time_sec,
rtc_time_sec,
assert_time_sec,
std::max(0.0, 100.0 * ((rtc_time_sec - nortc_time_sec) / nortc_time_sec)),
std::max(0.0, 100.0 * ((assert_time_sec - nortc_time_sec) / nortc_time_sec)));
}
else
printf("ERROR - Results of NORTC and RTC runs do not match!\n");
}
template <typename T>
void vector_rtc_overhead_analysis_02()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string bubble_sort_program =
" var upper_bound := v[]; "
" "
" repeat "
" var new_upper_bound := 0; "
" "
" for (var i := 1; i < upper_bound; i += 1) "
" { "
" assert(i < v[]); "
" if (v[i - 1] > v[i]) "
" { "
" v[i - 1] <=> v[i]; "
" new_upper_bound := i; "
" }; "
" }; "
" "
" upper_bound := new_upper_bound; "
" "
" until (upper_bound <= 1); "
" "
" var result := true; "
" "
" for (var i := 1; i < v[]; i += 1) "
" { "
" assert(i < v[]); "
" if (v[i - 1] > v[i]) "
" { "
" result := false; "
" }; "
" }; "
" "
" result; ";
T nortc_result = T(0);
T rtc_result = T(0);
T assert_result = T(0);
T nortc_time_sec = T(0);
T rtc_time_sec = T(0);
T assert_time_sec = T(0);
const std::size_t num_values = 10000;
std::vector<T> values(num_values, 0.0);
for (std::size_t i = 0; i < values.size(); ++i)
{
values[i] = static_cast<double>(values.size() - i);
}
{
std::vector<T> v = values;
symbol_table_t symbol_table;
symbol_table.add_vector("v", v);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(bubble_sort_program, expression);
exprtk::timer timer;
timer.start();
nortc_result = expression.value();
timer.stop();
nortc_time_sec = timer.time();
}
{
std::vector<T> v = values;
symbol_table_t symbol_table;
symbol_table.add_vector("v", v);
vector_access_rtc vec_rtc;
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.register_vector_access_runtime_check(vec_rtc);
parser.compile(bubble_sort_program, expression);
exprtk::timer timer;
timer.start();
rtc_result = expression.value();
timer.stop();
rtc_time_sec = timer.time();
}
{
std::vector<T> v = values;
symbol_table_t symbol_table;
symbol_table.add_vector("v", v);
expression_t expression;
expression.register_symbol_table(symbol_table);
assert_handler asrt_handler;
parser_t parser;
parser.register_assert_check(asrt_handler);
parser.compile(bubble_sort_program, expression);
exprtk::timer timer;
timer.start();
assert_result = expression.value();
timer.stop();
assert_time_sec = timer.time();
}
if (
(nortc_result == T(1) ) &&
(nortc_result == rtc_result ) &&
(nortc_result == assert_result)
)
{
printf("Bubble Sort - NORTC time: %5.3fsec\tRTC time: %5.3fsec\tassert time: %5.3fsec\trtc_overhead: %5.3f%%\tassert_overhead: %5.3f%%\n",
nortc_time_sec,
rtc_time_sec,
assert_time_sec,
std::max(0.0, 100.0 * ((rtc_time_sec - nortc_time_sec) / nortc_time_sec)),
std::max(0.0, 100.0 * ((assert_time_sec - nortc_time_sec) / nortc_time_sec)));
}
else
printf("ERROR - Results of NORTC and RTC runs do not match!\n");
}
int main(int argc, char* argv[])
{
const std::size_t rounds = ((argc == 2) ? atoi(argv[1]) : 5);
for (std::size_t i = 0; i < rounds; ++i)
{
vector_rtc_overhead_analysis_01<double>();
}
printf("-------------------------\n");
for (std::size_t i = 0; i < rounds; ++i)
{
vector_rtc_overhead_analysis_02<double>();
}
return 0;
}