53using Clock = std::chrono::steady_clock;
62 volatile long double benchmark_sink = 0;
66 bool validate =
false;
68 size_t repetitions = 128;
71 unsigned seed = 20260317u;
78 double median_us = 0.0;
81 double stddev_us = 0.0;
86 std::string case_name;
88 size_t repetitions = 0;
93 parse_sizes(
const std::string &csv)
96 std::stringstream ss(csv);
99 while (std::getline(ss, token,
','))
103 sizes.
append(
static_cast<size_t>(std::stoull(token)));
109 [[nodiscard]] Options
110 parse_options(
const int argc,
char **argv)
114 for (
int i = 1; i < argc; ++i)
116 const std::string arg = argv[i];
117 if (arg ==
"--validate")
119 else if (arg ==
"--json")
121 else if (arg ==
"--repetitions" and i + 1 < argc)
122 options.repetitions =
static_cast<size_t>(std::stoull(argv[++i]));
123 else if (arg ==
"--warmup" and i + 1 < argc)
124 options.warmup =
static_cast<size_t>(std::stoull(argv[++i]));
125 else if (arg ==
"--samples" and i + 1 < argc)
126 options.samples =
static_cast<size_t>(std::stoull(argv[++i]));
127 else if (arg ==
"--seed" and i + 1 < argc)
128 options.seed =
static_cast<unsigned>(std::stoul(argv[++i]));
129 else if (arg ==
"--sizes" and i + 1 < argc)
130 options.sizes = parse_sizes(argv[++i]);
131 else if (arg ==
"--help")
134 <<
"Usage: polynomial_benchmark [--validate] [--json]"
135 <<
" [--repetitions N] [--warmup N] [--samples N]"
136 <<
" [--seed N] [--sizes a,b,c]\n";
141 std::cerr <<
"Unknown option: " << arg <<
"\n";
148 std::cerr <<
"Repetitions, warmup, and samples must be positive\n";
154 std::cerr <<
"At least one benchmark size is required\n";
161 [[nodiscard]]
long long
162 deterministic_coeff(
const unsigned seed,
const size_t slot)
164 uint64_t x =
static_cast<uint64_t
>(
seed) * 0x9E3779B185EBCA87ULL;
165 x +=
static_cast<uint64_t
>(slot + 1) * 0xC2B2AE3D27D4EB4FULL;
167 x *= 0xFF51AFD7ED558CCDULL;
169 long long value =
static_cast<long long>(x % 17ULL) - 8LL;
175 template <
typename Poly>
176 [[nodiscard]]
typename Poly::Coeff
177 manual_eval_univariate(
const Poly &p,
const typename Poly::Coeff &x)
179 using Coeff =
typename Poly::Coeff;
180 Coeff result = Coeff{};
181 p.for_each_term([&result, &x](
size_t exp,
const Coeff &coeff)
188 [[nodiscard]] RealPoly
189 make_sparse_real_poly(
const size_t degree,
const unsigned seed,
const size_t variant)
194 std::max<size_t>(1, degree / 7 + 1),
195 std::max<size_t>(2, degree / 3 + 1),
196 std::max<size_t>(3, (2 * degree) / 3 + 1),
200 for (
size_t i = 0; i < exponents.
size(); ++i)
201 p.set_coeff(exponents(i),
202 static_cast<double>(deterministic_coeff(
seed +
static_cast<unsigned>(variant), i + variant * 11)));
208 make_sparse_int_poly(
const size_t degree,
const unsigned seed,
const size_t variant)
213 std::max<size_t>(1, degree / 5 + 1),
214 std::max<size_t>(2, degree / 2 + 1),
218 for (
size_t i = 0; i < exponents.
size(); ++i)
219 p.
set_coeff(exponents(i), deterministic_coeff(
seed +
static_cast<unsigned>(variant), i + variant * 7));
224 template <
typename MultiPoly>
225 [[nodiscard]] MultiPoly
226 make_sparse_multi_poly(
const size_t degree,
const unsigned seed,
const size_t variant)
233 Array<size_t>{std::max<size_t>(1, degree / 2), std::max<size_t>(1, degree / 3)},
234 Array<size_t>{std::max<size_t>(1, degree / 3), std::max<size_t>(1, degree / 2)},
239 for (
size_t i = 0; i < exponents.
size(); ++i)
240 p.add_to_coeff(exponents(i),
241 typename MultiPoly::Coeff(
242 deterministic_coeff(
seed +
static_cast<unsigned>(variant), i + variant * 13)));
248 make_groebner_system(
const size_t size)
250 auto x = LexMultiPoly::variable(3, 0);
251 auto y = LexMultiPoly::variable(3, 1);
252 auto z = LexMultiPoly::variable(3, 2);
258 gens.
append(x *
y - LexMultiPoly(3, 1.0));
264 gens.
append(x *
y - LexMultiPoly(3, 1.0));
266 gens.
append(x - LexMultiPoly(3, 1.0));
271 gens.
append(
y * z - LexMultiPoly(3, 1.0));
276 template <
typename Fn>
277 [[nodiscard]] TimingStats
278 measure(
const Options &
options, Fn &&fn)
283 for (
size_t sample = 0; sample <
options.samples; ++sample)
285 for (
size_t warm = 0; warm <
options.warmup; ++warm)
289 const auto start = Clock::now();
292 const auto end = Clock::now();
294 const double elapsed_us =
295 std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(end - start).
count();
296 samples.
append(elapsed_us /
static_cast<double>(
options.repetitions));
299 std::sort(&samples[0], &samples[0] + samples.
size());
302 stats.min_us = samples(0);
303 stats.max_us = samples(samples.
size() - 1);
304 stats.median_us = samples(samples.
size() / 2);
306 std::accumulate(&samples[0], &samples[0] + samples.
size(), 0.0) /
static_cast<double>(samples.
size());
309 for (
size_t i = 0; i < samples.
size(); ++i)
311 const double diff = samples(i) - stats.mean_us;
315 stats.stddev_us = std::sqrt(
variance);
320 validate_suite(
const Options &
options)
324 for (
size_t i = 0; i <
options.sizes.size(); ++i)
326 const size_t degree =
options.sizes(i);
328 RealPoly real_a = make_sparse_real_poly(degree,
options.seed, 0);
329 RealPoly real_b = make_sparse_real_poly(degree,
options.seed, 1);
330 const double x = 0.875;
332 if (std::abs(real_a.eval(x) - manual_eval_univariate(real_a, x)) > 1.0e-10)
334 std::cerr <<
"[validate] univariate eval mismatch at size " << degree <<
"\n";
338 RealPoly inner({0.0, 0.5});
339 RealPoly composed = real_a.compose(inner);
340 if (std::abs(composed.eval(0.5) - real_a.eval(inner.eval(0.5))) > 1.0e-10)
342 std::cerr <<
"[validate] univariate compose mismatch at size " << degree <<
"\n";
347 IntPoly int_b = make_sparse_int_poly(std::max<size_t>(3, degree / 4),
options.seed, 1);
350 if ((q_uni * int_b + r_uni) !=
product)
352 std::cerr <<
"[validate] univariate divmod identity mismatch at size " << degree <<
"\n";
356 IntMultiPoly multi_a = make_sparse_multi_poly<IntMultiPoly>(std::max<size_t>(2, degree / 8 + 2),
358 IntMultiPoly multi_b = make_sparse_multi_poly<IntMultiPoly>(std::max<size_t>(2, degree / 10 + 2),
360 IntMultiPoly multi_product = multi_a * multi_b;
362 auto [q_multi, r_multi] = multi_product.divmod(divisors);
363 if (((q_multi(0) * multi_b) + r_multi) != multi_product)
365 std::cerr <<
"[validate] multivariate divmod identity mismatch at size " << degree <<
"\n";
369 Array<LexMultiPoly> gb = LexMultiPoly::reduced_groebner_basis(make_groebner_system(degree));
370 for (
size_t g = 0; g < gb.
size(); ++g)
373 for (
size_t h = 0;
h < gb.
size(); ++
h)
378 if (gb(g).divmod(others).second != gb(g))
380 std::cerr <<
"[validate] reduced Groebner basis not autoreduced at size "
396 for (
size_t i = 0; i <
options.sizes.size(); ++i)
401 RealPoly p = make_sparse_real_poly(
size,
options.seed, 0);
403 row.case_name =
"uni_eval_sparse";
408 benchmark_sink += p.eval(0.875);
414 RealPoly a = make_sparse_real_poly(
size,
options.seed, 0);
415 RealPoly b = make_sparse_real_poly(
size,
options.seed, 1);
417 row.case_name =
"uni_mul_sparse";
423 benchmark_sink +=
static_cast<long double>(c.degree() + c.num_terms());
429 RealPoly outer = make_sparse_real_poly(
size,
options.seed, 0);
430 RealPoly inner({0.0, 0.5});
432 row.case_name =
"uni_compose_sparse";
435 row.stats = measure(
options, [&outer, &inner]()
437 RealPoly c = outer.compose(inner);
438 benchmark_sink +=
static_cast<long double>(c.degree() + c.num_terms());
448 row.case_name =
"uni_divmod_sparse_exact";
451 row.stats = measure(
options, [÷nd, &b]()
453 auto [q,
r] = dividend.
divmod(b);
454 benchmark_sink +=
static_cast<long double>(q.degree() +
r.degree());
460 const size_t multi_size = std::max<size_t>(2,
size / 8 + 2);
461 IntMultiPoly a = make_sparse_multi_poly<IntMultiPoly>(multi_size,
options.seed, 0);
462 IntMultiPoly b = make_sparse_multi_poly<IntMultiPoly>(multi_size,
options.seed, 1);
464 row.case_name =
"multi_mul_sparse";
469 IntMultiPoly c = a * b;
470 benchmark_sink +=
static_cast<long double>(c.degree() + c.num_terms());
476 const size_t multi_size = std::max<size_t>(2,
size / 10 + 2);
477 IntMultiPoly a = make_sparse_multi_poly<IntMultiPoly>(multi_size,
options.seed, 0);
478 IntMultiPoly b = make_sparse_multi_poly<IntMultiPoly>(multi_size,
options.seed, 1);
479 IntMultiPoly dividend = a * b;
482 row.case_name =
"multi_divmod_exact";
485 row.stats = measure(
options, [÷nd, &divisors]()
487 auto [q,
r] = dividend.divmod(divisors);
488 benchmark_sink +=
static_cast<long double>(q(0).degree() +
r.degree());
496 row.case_name =
"multi_groebner_reduced_lex";
498 row.repetitions = std::max<size_t>(1,
options.repetitions / 8);
501 row.stats = measure(lighter, [&gens]()
504 benchmark_sink +=
static_cast<long double>(gb.
size());
517 <<
" \"mode\": \"benchmark\",\n"
518 <<
" \"metadata\": {\n"
519 <<
" \"seed\": " <<
options.seed <<
",\n"
520 <<
" \"repetitions\": " <<
options.repetitions <<
",\n"
521 <<
" \"warmup\": " <<
options.warmup <<
",\n"
522 <<
" \"samples\": " <<
options.samples <<
"\n"
526 for (
size_t i = 0; i <
rows.size(); ++i)
528 const BenchmarkRow &
row =
rows(i);
530 <<
" \"case\": \"" <<
row.case_name <<
"\",\n"
531 <<
" \"size\": " <<
row.size <<
",\n"
532 <<
" \"repetitions\": " <<
row.repetitions <<
",\n"
533 <<
" \"mean_us\": " << std::setprecision(12) <<
row.stats.mean_us <<
",\n"
534 <<
" \"median_us\": " <<
row.stats.median_us <<
",\n"
535 <<
" \"min_us\": " <<
row.stats.min_us <<
",\n"
536 <<
" \"max_us\": " <<
row.stats.max_us <<
",\n"
537 <<
" \"stddev_us\": " <<
row.stats.stddev_us <<
"\n"
538 <<
" }" << (i + 1 ==
rows.size() ?
"\n" :
",\n");
548 std::cout <<
"Polynomial benchmark (microseconds per call)\n"
549 << std::left << std::setw(28) <<
"case"
550 << std::setw(8) <<
"size"
551 << std::setw(12) <<
"median"
552 << std::setw(12) <<
"mean"
553 << std::setw(12) <<
"min"
554 << std::setw(12) <<
"max"
557 for (
size_t i = 0; i <
rows.size(); ++i)
559 const BenchmarkRow &
row =
rows(i);
560 std::cout << std::left << std::setw(28) <<
row.case_name
561 << std::setw(8) <<
row.size
562 << std::setw(12) << std::fixed << std::setprecision(3) <<
row.stats.median_us
563 << std::setw(12) <<
row.stats.mean_us
564 << std::setw(12) <<
row.stats.min_us
565 << std::setw(12) <<
row.stats.max_us
566 <<
row.stats.stddev_us <<
"\n";
569 std::cout <<
"Benchmark sink: " <<
static_cast<long double>(benchmark_sink) <<
"\n";
578 const Options
options = parse_options(argc, argv);
582 const bool ok = validate_suite(
options);
586 std::cout <<
"polynomial_benchmark: validation passed\n";
593 print_rows_table(
rows);
597 catch (
const std::exception &e)
599 std::cerr <<
"polynomial_benchmark: " << e.what() <<
"\n";
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Sparse multivariate polynomial.
Univariate polynomial over a generic coefficient ring.
std::pair< Gen_Polynomial, Gen_Polynomial > divmod(const Gen_Polynomial &d) const
Polynomial long division: returns (quotient, remainder).
void set_coeff(size_t exp, const Coefficient &c)
Set coefficient at exponent; removes entry if zero.
std::chrono::steady_clock Clock
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
C power(C base, size_t exp)
Fast exponentiation by squaring.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
auto variance(const Container &data, bool population=false) -> std::decay_t< decltype(*std::begin(data))>
Compute variance using Welford's numerically stable algorithm.
DynList< T > rep(const size_t n, const T &item)
Create a sequence of repeated items.
and
Check uniqueness with explicit hash + equality functors.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
static struct argp_option options[]
void run_benchmarks(const BenchmarkConfig &config)
Sparse multivariate polynomial over an arbitrary coefficient ring.
Univariate polynomial ring arithmetic over generic coefficients.