Debian Codegraph @

benchmarks/benchmark-wino.C

raw
1 /* Copyright (c) 2012 FFLAS-FFPACK
2 * Written by J.G. Dumas <[email protected]>
3 * ========LICENCE========
4 * This file is part of the library FFLAS-FFPACK.
5 *
6 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * ========LICENCE========
20 */
21
22 //#include "goto-def.h"
23
24 #include "fflas-ffpack/fflas-ffpack-config.h"
25 #include <iostream>
26 #include <fstream>
27 #include <givaro/modular.h>
28
29 #include "fflas-ffpack/fflas/fflas.h"
30 #include "fflas-ffpack/utils/timer.h"
31 #include "fflas-ffpack/utils/args-parser.h"
32
33 #define CUBE(x) ((x)*(x)*(x))
34
35 template<class Field>
36 void launch_wino(const Field &F,
37 const size_t &n,
38 const size_t &NB,
39 const size_t &wino,
40 const bool &asmax,
41 const size_t &seed,
42 const bool compare)
43 {
44
45 typedef typename Field::Element Element ;
46 typename Field::RandIter G(F);
47
48 if (compare)
49 F.write(std::cout << "Field ") << std::endl;
50
51 double basetime(0.0), time(0.0);
52
53 Element *A, *C;
54 A = FFLAS::fflas_new<Element>(n*n);
55 C = FFLAS::fflas_new<Element>(n*n);
56 for (size_t i=0; i<n*n;++i)
57 G.random(A[i]);
58
59 // ----- Compare with fgemm
60 FFLAS::Timer chrono;
61 if (compare) {
62 for(size_t i=0; i<NB; ++i) {
63 chrono.start();
64 FFLAS::fgemm(F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
65 n,n,n, F.one,
66 A, n, A, n, F.zero, C,n);
67 chrono.stop();
68 basetime+= chrono.usertime();
69 }
70
71 std::cout << "Time: " << basetime / double(NB)
72 << " Gfops: " << 2. * CUBE(double(n)/1000.0) / basetime * double(NB)
73 << " [fgemm result]" << std::endl;
74 }
75
76 // ----- Winograd
77 for(size_t w = (asmax)? 0 : wino; w <= wino; ++w) {
78 FFLAS::MMHelper<Field, FFLAS::MMHelperAlgo::Winograd> WH (F,(int)w);
79 time = 0. ;
80 chrono.clear();
81 for(size_t i=0; i<NB; ++i) {
82 chrono.start();
83 FFLAS::fgemm(F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
84 n, n, n, F.one, A, n, A, n, F.zero, C, n, WH);
85 chrono.stop();
86 time+= chrono.usertime();
87 }
88
89 // -----------
90 // Standard output for benchmark - Alexis Breust 2014/11/14
91 std::cout << "Time: " << time / double(NB)
92 << " Gfops: " << 2. * CUBE(double(n)/1000.0) / time * double(NB);
93
94 if (compare || asmax)
95 std::cout << " [wino" << w << " result]" << std::endl;
96 }
97
98 if (compare)
99 std::cout << std::endl;
100
101 FFLAS::fflas_delete(A);
102 FFLAS::fflas_delete(C);
103 }
104
105 int main (int argc, char ** argv) {
106
107 size_t iter = 1;
108 int q = 1009;
109 int n = 1000;
110 int w = 7;
111 size_t seed = 0;
112 bool compare = false;
113 bool balanced = false;
114 std::string type = "double";
115 bool levelasmax = false;
116
117 Argument as[] = {
118 { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INT , &q },
119 { 'n', "-n N", "Set the dimension of the matrix.", TYPE_INT , &n },
120 { 'w', "-w N", "Set the winograd level.", TYPE_INT , &w },
121 { 'l', "-l {YN}", "Use -w info a max (Yes or No).", TYPE_BOOL , &levelasmax },
122 { 's', "-s S", "Set the seed for randomness (0 for random).", TYPE_INT , &seed },
123 { 'i', "-i R", "Set number of repetitions.", TYPE_INT , &iter },
124 { 'c', "-c {YN}", "Compare mode, overrides -b and -t options (Yes or No).", TYPE_BOOL , &compare },
125 { 'b', "-b {YN}", "Use balanced modular (Yes or No).", TYPE_BOOL , &balanced },
126 { 't', "-t TYPE", "Set the field type (double/float/int).", TYPE_STR , &type },
127 END_OF_ARGUMENTS
128 };
129
130 FFLAS::parseArguments(argc,argv,as);
131
132 if (!seed)
133 seed = FFLAS::BaseTimer::seed();
134 srand((uint32_t)seed);
135
136 if (compare) {
137 Givaro::Modular<double> F1(q);
138 Givaro::Modular<float> F2(q);
139 Givaro::Modular<int> F3(q);
140 Givaro::ModularBalanced<double> F4(q);
141 Givaro::ModularBalanced<float> F5(q);
142 Givaro::ModularBalanced<int> F6(q);
143 // ZZ<double> F7;
144 // ZZ<float> F8;
145 // ZZ<int> F9;
146
147 launch_wino(F1,n,iter,w,levelasmax,seed,true);
148 launch_wino(F2,n,iter,w,levelasmax,seed,true);
149 launch_wino(F3,n,iter,w,levelasmax,seed,true);
150 launch_wino(F4,n,iter,w,levelasmax,seed,true);
151 launch_wino(F5,n,iter,w,levelasmax,seed,true);
152 launch_wino(F6,n,iter,w,levelasmax,seed,true);
153 // launch_wino(F7,n,iter,winomax,seed);
154 // launch_wino(F8,n,iter,winomax,seed);
155 // launch_wino(F9,n,iter,winomax,seed);
156 }
157 else {
158 if (balanced) {
159 if (type == "double") launch_wino(Givaro::ModularBalanced<double>(q),n,iter,w,levelasmax,seed,false);
160 else if (type == "float") launch_wino(Givaro::ModularBalanced<float>(q),n,iter,w,levelasmax,seed,false);
161 else if (type == "int") launch_wino(Givaro::ModularBalanced<int>(q),n,iter,w,levelasmax,seed,false);
162 }
163 else {
164 if (type == "double") launch_wino(Givaro::Modular<double>(q),n,iter,w,levelasmax,seed,false);
165 else if (type == "float") launch_wino(Givaro::Modular<float>(q),n,iter,w,levelasmax,seed,false);
166 else if (type == "int") launch_wino(Givaro::Modular<int>(q),n,iter,w,levelasmax,seed,false);
167 }
168 }
169
170 if (compare || levelasmax)
171 std::cout << "Lauch with:";
172 FFLAS::writeCommandString(std::cout, as) << std::endl;
173
174 return 0;
175 }
176
177 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
178 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s