-
Notifications
You must be signed in to change notification settings - Fork 5
/
bench-tensorindex.cc
69 lines (59 loc) · 2.39 KB
/
bench-tensorindex.cc
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
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/bench - Iota used as Blitz++'s TensorIndex.
// (c) Daniel Llorens - 2019-2020
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
#include <iostream>
#include "ra/test.hh"
using std::cout, std::endl, std::flush, ra::TestRecorder, ra::Benchmark;
int main()
{
TestRecorder tr;
// rank 1
{
ra::Big<int, 1> a = {0, 0, 0};
ra::ply(map([](auto && i) { std::cout << "i: " << i << std::endl; },
a+ra::iota<0>()));
ra::ply_ravel(map([](auto && i) { std::cout << "i: " << i << std::endl; },
a+ra::iota<0>()));
}
// rank 2
{
ra::Big<int, 2> a = {{0, 0, 0}, {0, 0, 0}};
ra::ply(map([](auto && i, auto && j) { std::cout << "i: " << i << ", " << j << std::endl; },
a+ra::iota<0>(), a+ra::iota<1>()));
ra::ply_ravel(map([](auto && i, auto && j) { std::cout << "i: " << i << ", " << j << std::endl; },
a+ra::iota<0>(), a+ra::iota<1>()));
}
// benchmark
auto taking_view =
[](TestRecorder & tr, auto && a)
{
auto fa = [&a]()
{
int c = 0;
ra::ply(ra::map([&c](auto && i, auto && j) { c += 2*i-j; },
a+ra::iota<0>(), a+ra::iota<1>()));
return c;
};
auto fb = [&a]()
{
int c = 0;
ra::ply_ravel(ra::map([&c](auto && i, auto && j) { c += 2*i-j; },
a+ra::iota<0>(), a+ra::iota<1>()));
return c;
};
tr.test_eq(499500000, fa());
tr.test_eq(499500000, fb());
auto bench = Benchmark {/* repeats */ 30, /* runs */ 30};
bench.info("vala").report(std::cout, bench.run(fa), 1e-6);
bench.info("valb").report(std::cout, bench.run(fb), 1e-6);
};
ra::Big<int, 2> const a({1000, 1000}, 0);
taking_view(tr, a);
auto b = transpose<1, 0>(a);
taking_view(tr, b);
return tr.summary();
}