123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #define RA_DO_OPT 0
- #define RA_DO_OPT_IOTA 1
- #ifdef RA_DO_OPT_SMALLVECTOR
- #undef RA_DO_OPT_SMALLVECTOR
- #endif
- #define RA_DO_OPT_SMALLVECTOR 1
- #include <iostream>
- #include <iomanip>
- #include "ra/bench.hh"
- using std::cout, std::endl, std::setw, std::setprecision;
- using ra::TestRecorder, ra::Small, ra::View, ra::Unique;
- using Vec = ra::Small<float, 4>;
- int main()
- {
- TestRecorder tr(std::cout);
- tr.section("small vector ops through vector extensions, other types / sizes");
- {
- ra::Small<double, 4> a = 1 + ra::_0;
- ra::Small<double, 2, 4> b = 33 - ra::_1;
- auto c = optimize(a + b(1));
- tr.info("optimization of view").test(std::is_same_v<decltype(c), ra::Small<double, 4>>);
- tr.test_eq(34, c);
- }
- auto bench_type =
- [&](auto v)
- {
- using Vec = decltype(v);
- auto sum_opt =
- [&](auto & a, auto & b, auto & c)
- {
- for (int i=0; i<a.len(0); ++i) {
- c(i) = ra::optimize(a(i)+b(i));
- static_assert(std::is_same_v<decltype(optimize(a(i)+b(i))), Vec>);
- }
- };
- auto sum_unopt =
- [&](auto & a, auto & b, auto & c)
- {
- for (int i=0; i<a.len(0); ++i) {
- c(i) = a(i)+b(i);
- }
- };
- auto bench_all =
- [&](int reps, int m)
- {
- auto bench =
- [&tr, &m, &reps](auto && f, char const * tag)
- {
- alignas (alignof(Vec)) Vec astore[m];
- alignas (alignof(Vec)) Vec bstore[m];
- alignas (alignof(Vec)) Vec cstore[m];
- ra::View<Vec, 1> a({m}, astore);
- ra::View<Vec, 1> b({m}, bstore);
- ra::View<Vec, 1> c({m}, cstore);
- a = +ra::_0 +1;
- b = -ra::_0 -1;
- c = 99;
- auto bv = Benchmark().repeats(reps).runs(3).run([&]() { f(a, b, c); });
- tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/(m)/1e-9, " ns [",
- Benchmark::stddev(bv)/(m)/1e-9 ,"] ", tag).test(true);
- };
- tr.section("[", (std::is_same_v<float, std::decay_t<decltype(std::declval<Vec>()[0])>> ? "float" : "double"),
- " x ", Vec::size(), "] block of ", m, " times ", reps);
- bench(sum_opt, "opt");
- bench(sum_unopt, "unopt");
- };
- bench_all(50000, 10);
- bench_all(50000, 100);
- bench_all(50000, 1000);
- };
- bench_type(ra::Small<float, 2> {});
- bench_type(ra::Small<double, 2> {});
- bench_type(ra::Small<float, 4> {});
- bench_type(ra::Small<double, 4> {});
- bench_type(ra::Small<float, 8> {});
- bench_type(ra::Small<double, 8> {});
- return tr.summary();
- }
|