C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Classes | Typedefs | Functions | Variables
exprtk_vector_benchmark.cpp File Reference
#include <cstdio>
#include <fstream>
#include <limits>
#include <string>
#include <vector>
#include "exprtk.hpp"
Include dependency graph for exprtk_vector_benchmark.cpp:

Go to the source code of this file.

Classes

struct  test_expression
 

Typedefs

typedef double numeric_type
 

Functions

template<typename T >
run_expression_benchmark (const std::size_t vec_size, const std::string &expr_string, const std::size_t &cost)
 
template<typename T >
void run_benchmark (const std::size_t &vec_size)
 
template<typename T >
void run_file_benchmark (const std::size_t &vec_size, const std::string &file_name)
 
int main (int argc, char *argv[])
 

Variables

const test_expression global_expression_list []
 
const std::size_t global_expression_list_size = sizeof(global_expression_list) / sizeof(test_expression)
 
const std::size_t rounds = 2000
 

Typedef Documentation

◆ numeric_type

typedef double numeric_type

Definition at line 91 of file exprtk_vector_benchmark.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 217 of file exprtk_vector_benchmark.cpp.

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}

◆ run_benchmark()

template<typename T >
void run_benchmark ( const std::size_t &  vec_size)

Definition at line 155 of file exprtk_vector_benchmark.cpp.

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}
const std::size_t rounds
const std::size_t global_expression_list_size
const test_expression global_expression_list[]

References test_expression::cost, global_expression_list, global_expression_list_size, and rounds.

◆ run_expression_benchmark()

template<typename T >
T run_expression_benchmark ( const std::size_t  vec_size,
const std::string &  expr_string,
const std::size_t &  cost 
)

Definition at line 94 of file exprtk_vector_benchmark.cpp.

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}
double time() const
Definition exprtk.hpp:43502

References rounds, exprtk::timer::start(), exprtk::timer::stop(), and exprtk::timer::time().

Here is the call graph for this function:

◆ run_file_benchmark()

template<typename T >
void run_file_benchmark ( const std::size_t &  vec_size,
const std::string &  file_name 
)

Definition at line 183 of file exprtk_vector_benchmark.cpp.

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}

Variable Documentation

◆ global_expression_list

const test_expression global_expression_list[]

Definition at line 40 of file exprtk_vector_benchmark.cpp.

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 };

Referenced by run_benchmark().

◆ global_expression_list_size

const std::size_t global_expression_list_size = sizeof(global_expression_list) / sizeof(test_expression)

Definition at line 87 of file exprtk_vector_benchmark.cpp.

Referenced by run_benchmark().

◆ rounds

const std::size_t rounds = 2000

Definition at line 89 of file exprtk_vector_benchmark.cpp.

Referenced by run_benchmark(), and run_expression_benchmark().