C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_vector_benchmark.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Vector Processing Benchmark *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <fstream>
22#include <limits>
23#include <string>
24#include <vector>
25
26#include "exprtk.hpp"
27
28
30{
31 std::size_t cost;
32 std::string e;
33
34 test_expression(const std::size_t c, const std::string& ex)
35 : cost(c)
36 , e(ex)
37 {}
38};
39
41 {
42 test_expression( 1, "2 * v0" ),
43 test_expression( 1, "v0 * 2" ),
44 test_expression( 2, "2v0 + 3" ),
45 test_expression( 2, "3 + 2v0" ),
46 test_expression( 5, "(2v0 + 3) * (2v0 + 3)" ),
47 test_expression( 5, "(3 + 2v0) * (3 + 2v0)" ),
48 test_expression( 1, "v0 + v1" ),
49 test_expression( 3, "(v0 + v1) * (v0 - v1)" ),
50 test_expression( 3, "2v0 + 3v1" ),
51 test_expression( 3, "2v0 - v1 / 3" ),
52 test_expression( 2, "v0 * v1 / v2" ),
53 test_expression( 3, "2(v0 * v1 / v2)" ),
54 test_expression( 4, "2(v0 / 3 + v1 / 4)" ),
55 test_expression( 3, "(2v0 - v1 / v2)" ),
56 test_expression( 4, "3(2v0 - v1 / v2)" ),
57 test_expression( 5, "7(5v0 * 3v1 / 2v2)" ),
58 test_expression( 5, "abs(v0 - v1) * abs(v1 - v0)" ),
59 test_expression( 7, "abs(2v0 - v1) * abs(v1 - 2v0)" ),
60 test_expression( 9, "abs(2v0 - 3v1) * abs(3v1 - 5v0)" ),
61 test_expression( 2, "sum(2 * v0)" ),
62 test_expression( 2, "sum(v0 * 2)" ),
63 test_expression( 2, "sum(v0 * v1)" ),
64 test_expression( 3, "sum(2v0 + 3)" ),
65 test_expression( 3, "sum(3 + 2v0)" ),
66 test_expression( 6, "sum((2v0 + 3) * (2v0 + 3))" ),
67 test_expression( 6, "sum((3 + 2v0) * (3 + 2v0))" ),
68 test_expression( 2, "sum(v0 + v1)" ),
69 test_expression( 4, "sum((v0 + v1) * (v0 - v1))" ),
70 test_expression( 4, "sum(2v0 + 3v1)" ),
71 test_expression( 4, "sum((2v0 - v1 / v2))" ),
72 test_expression( 5, "sum(3(2v0 - v1 / v2))" ),
73 test_expression( 4, "sum(abs(v0 * v1) / v2)" ),
74 test_expression( 5, "sum(v0 + v1) + avg(v0 - v1)" ),
75 test_expression( 8, "7(sum(abs(5v0 * 3v1) / 2v2))" ),
76 test_expression( 9, "sum(2v0 + 3v1) + sum(4 / v0 - 5 / v1)"),
77 test_expression( 6, "sum(abs(v0 - v1) * abs(v1 - v0))" ),
78 test_expression( 8, "sum(abs(2v0 - v1) * abs(v1 - 2v0))" ),
79 test_expression(10, "sum(abs(2v0 - 3v1) * abs(3v1 - 5v0))" ),
80 test_expression( 2, "axpbz(2,v0,3,v1)" ),
81 test_expression( 2, "axpy(2,v0,v1)" ),
82 //test_expression( 4, "sum(v0^2.2 + v1^3.3)" ),
83 //test_expression( 4, "exp(-1 / (v0^2))" ),
84 //test_expression( 5, "exp(-1 / (v0^2)) / v1" )
85 };
86
88
89const std::size_t rounds = 2000;
90
91typedef double numeric_type;
92
93template <typename T>
94T run_expression_benchmark(const std::size_t vec_size,
95 const std::string& expr_string,
96 const std::size_t& cost)
97{
98 typedef exprtk::symbol_table<T> symbol_table_t;
99 typedef exprtk::expression<T> expression_t;
100 typedef exprtk::parser<T> parser_t;
101
102 std::vector<T> v0(vec_size, T(2.1234567890));
103 std::vector<T> v1(vec_size, T(3.1234567890));
104 std::vector<T> v2(vec_size, T(5.1234567890));
105
106 exprtk::rtl::vecops::package<T> vecops_package;
107
108 symbol_table_t symbol_table;
109 symbol_table.add_vector("v0",v0);
110 symbol_table.add_vector("v1",v1);
111 symbol_table.add_vector("v2",v2);
112 symbol_table.add_package(vecops_package);
113
114 expression_t expression;
115 expression.register_symbol_table(symbol_table);
116
117 parser_t parser;
118
119 if (!parser.compile(expr_string,expression))
120 {
121 printf("[load_expression] - Parser Error: %s\tExpression: %s\n",
122 parser.error().c_str(),
123 expr_string.c_str());
124
125 return std::numeric_limits<T>::quiet_NaN();
126 }
127
128 exprtk::timer timer;
129
130 T total = T(0);
131
132 timer.start();
133
134 for (std::size_t r = 0; r < rounds; ++r)
135 {
136 total += expression.value();
137 }
138
139 timer.stop();
140
141 if (T(0) != total)
142 printf("Total Time:%10.7f Rate:%11.3fevals/sec Perf: %5.3fGFLOPS Expression: %s\n",
143 timer.time(),
144 rounds / timer.time(),
145 (rounds * vec_size * cost) / (1e+9 * timer.time()),
146 expr_string.c_str());
147 else
148 printf("run_expression_benchmark() - Error running benchmark for expression: %s\n",
149 expr_string.c_str());
150
151 return timer.time();
152}
153
154template <typename T>
155void run_benchmark(const std::size_t& vec_size)
156{
157 T total_time = 0.0;
158
159 for (std::size_t i = 0; i < global_expression_list_size; ++i)
160 {
161 total_time +=
162 run_expression_benchmark<T>(vec_size,
164 global_expression_list[i].cost);
165 }
166
167 unsigned long long total_flops = 0;
168
169 for (std::size_t i = 0; i < global_expression_list_size; ++i)
170 {
171 total_flops += global_expression_list[i].cost;
172 }
173
174 total_flops *= (rounds * vec_size);
175
176 printf("Total Time:%10.7f FLOP: %llu Perf: %7.4fGFLOPS\n",
177 total_time,
178 total_flops,
179 total_flops / (1e9 * total_time));
180}
181
182template <typename T>
183void run_file_benchmark(const std::size_t& vec_size, const std::string& file_name)
184{
185 std::ifstream stream(file_name.c_str());
186
187 if (!stream)
188 {
189 printf("run_file_benchmark() - Failed to open file: %s\n",
190 file_name.c_str());
191 return;
192 }
193
194 std::string line;
195 std::vector<std::string> expr_list;
196
197 while (std::getline(stream, line))
198 {
199 if (line.empty())
200 continue;
201 else if ('#' == line[0])
202 continue;
203
204 expr_list.push_back(line);
205 }
206
207 T total_time = T(0);
208
209 for (std::size_t i = 0; i < expr_list.size(); ++i)
210 {
211 total_time += run_expression_benchmark<T>(vec_size,expr_list[i],0);
212 }
213
214 printf("Total Time:%10.7f\n", total_time);
215}
216
217int main(int argc, char* argv[])
218{
219 const std::size_t vec_size = ((argc >= 2) ? atoi(argv[1]) : 100000);
220 const std::string file_name = ((argc > 3) ? argv[2] : "" );
221
222 switch (argc)
223 {
224 case 2 : run_benchmark <numeric_type>(vec_size ); break;
225 case 3 : run_file_benchmark<numeric_type>(vec_size,file_name); break;
226 }
227
228 return 0;
229}
double time() const
Definition exprtk.hpp:43502
const std::size_t rounds
void run_file_benchmark(const std::size_t &vec_size, const std::string &file_name)
double numeric_type
void run_benchmark(const std::size_t &vec_size)
T run_expression_benchmark(const std::size_t vec_size, const std::string &expr_string, const std::size_t &cost)
const std::size_t global_expression_list_size
const test_expression global_expression_list[]
test_expression(const std::size_t c, const std::string &ex)