1 //****************************************************************************//
2 // Copyright (C) 2018 Florent Hivert <Florent.Hivert@lri.fr>, //
3 // //
4 // Distributed under the terms of the GNU General Public License (GPL) //
5 // //
6 // This code is distributed in the hope that it will be useful, //
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
9 // General Public License for more details. //
10 // //
11 // The full text of the GPL is available at: //
12 // //
13 // http://www.gnu.org/licenses/ //
14 //****************************************************************************//
15
16 #include <iostream>
17 #include <benchmark/benchmark.h>
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include "compilerinfo.hpp"
22 #include "cpu_x86_impl.hpp"
23 #include "bench_fixture.hpp"
24
25 #include "bmat8.hpp"
26
27 using namespace FeatureDetector;
28 using namespace std;
29 using HPCombi::epu8;
30
31 // const Fix_perm16 sample;
32 const std::string SIMDSET = cpu_x86::get_highest_SIMD();
33 const std::string PROCID = cpu_x86::get_proc_string();
34
35 using namespace HPCombi;
36
make_sample(size_t n)37 std::vector<BMat8> make_sample(size_t n) {
38 std::vector<BMat8> res {};
39 for (size_t i=0; i < n; i++) {
40 res.push_back(BMat8::random());
41 }
42 return res;
43 }
44
make_pair_sample(size_t n)45 std::vector<std::pair<BMat8, BMat8>> make_pair_sample(size_t n) {
46 std::vector<std::pair<BMat8, BMat8>> res {};
47 for (size_t i=0; i < n; i++) {
48 res.push_back(make_pair(BMat8::random(),BMat8::random()));
49 }
50 return res;
51 }
52
53 std::vector<BMat8> sample = make_sample(1000);
54 std::vector<std::pair<BMat8, BMat8>> pair_sample = make_pair_sample(1000);
55 // std::vector<BMat8> sample = {BMat8::one()};
56 // std::vector<BMat8> sample = {BMat8(0)};
57
58 //##################################################################################
59 template<typename TF, typename Sample>
myBench(const string & name,TF pfunc,Sample & sample)60 void myBench(const string &name, TF pfunc, Sample &sample) {
61 string fullname = name + "_" + CXX_VER + "_proc-" + PROCID;
62 benchmark::RegisterBenchmark(fullname.c_str(),
63 [pfunc, sample](benchmark::State& st) {
64 for (auto _ : st) {
65 for (auto elem : sample) {
66 benchmark::DoNotOptimize(pfunc(elem));
67 }
68 }
69 });
70 }
71
72 #define myBenchMeth(descr, methname, smp) \
73 myBench(descr, [](BMat8 p) { return p.methname(); }, smp)
74
75 //##################################################################################
Bench_row_space_size()76 int Bench_row_space_size() {
77 myBenchMeth("row_space_size_ref", row_space_size_ref, sample);
78 myBenchMeth("row_space_size_bitset", row_space_size_bitset, sample);
79 myBenchMeth("row_space_size_incl1", row_space_size_incl1, sample);
80 myBenchMeth("row_space_size_incl", row_space_size_incl, sample);
81 myBenchMeth("row_space_size", row_space_size, sample);
82 return 0;
83 }
84
85 //##################################################################################
Bench_transpose()86 int Bench_transpose() {
87 myBenchMeth("transpose_knuth", transpose, sample);
88 myBenchMeth("transpose_mask", transpose_mask, sample);
89 myBenchMeth("transpose_maskd", transpose_maskd, sample);
90 return 0;
91 }
92
Bench_transpose2()93 int Bench_transpose2() {
94 myBench("transpose2_knuth",
95 [](std::pair<BMat8, BMat8> p) {
96 return make_pair(p.first.transpose(),
97 p.second.transpose());
98 }, pair_sample);
99 myBench("transpose2_mask",
100 [](std::pair<BMat8, BMat8> p) {
101 return make_pair(p.first.transpose_mask(),
102 p.second.transpose_mask());
103 }, pair_sample);
104 myBench("transpose2_maskd",
105 [](std::pair<BMat8, BMat8> p) {
106 return make_pair(p.first.transpose_maskd(),
107 p.second.transpose_maskd());
108 }, pair_sample);
109 myBench("transpose2",
110 [](std::pair<BMat8, BMat8> p) {
111 BMat8::transpose2(p.first, p.second);
112 return p;
113 }, pair_sample);
114 return 0;
115 }
116
Bench_row_space_included()117 int Bench_row_space_included() {
118 myBench("row_space_incl_ref",
119 [](std::pair<BMat8, BMat8> p) {
120 return p.first.row_space_included_ref(p.second);
121 }, pair_sample);
122 myBench("row_space_incl_bitset",
123 [](std::pair<BMat8, BMat8> p) {
124 return p.first.row_space_included_bitset(p.second);
125 }, pair_sample);
126 myBench("row_space_incl_rotate",
127 [](std::pair<BMat8, BMat8> p) {
128 return p.first.row_space_included(p.second);
129 }, pair_sample);
130 return 0;
131 }
132
Bench_row_space_included2()133 int Bench_row_space_included2() {
134 myBench("row_space_incl2_rotate",
135 [](std::pair<BMat8, BMat8> p) {
136 return p.first.row_space_included(p.second)
137 == p.second.row_space_included(p.first);
138 }, pair_sample);
139 myBench("row_space_incl2",
140 [](std::pair<BMat8, BMat8> p) {
141 auto res = BMat8::row_space_included2(
142 p.first, p.second, p.second, p.first);
143 return res.first == res.second;
144 }, pair_sample);
145 return 0;
146 }
147
148 auto dummy = {
149 Bench_row_space_size(),
150 Bench_transpose(),
151 Bench_transpose2(),
152 Bench_row_space_included(),
153 Bench_row_space_included2()
154 };
155
156 BENCHMARK_MAIN();
157