Debian Codegraph @

benchmarks/benchmark-checkers.C

raw
1 /*
2 * Copyright (C) 2015 the FFLAS-FFPACK group
3 * Written by Ashley Lesdalons <[email protected]>
4 *
5 * This file is Free Software and part of FFLAS-FFPACK.
6 *
7 * ========LICENCE========
8 * This file is part of the library FFLAS-FFPACK.
9 *
10 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * ========LICENCE========
24 *
25 */
26
27 #define ENABLE_ALL_CHECKINGS 1 // DO NOT CHANGE
28 #define _NR_TESTS 5
29 #define _MAX_SIZE_MATRICES 1000
30
31 #include "fflas-ffpack/fflas-ffpack-config.h"
32 #include <iostream>
33 #include <stdlib.h>
34 #include <time.h>
35 #include "fflas-ffpack/fflas-ffpack.h"
36 #include "fflas-ffpack/utils/args-parser.h"
37 #include "fflas-ffpack/utils/fflas_randommatrix.h"
38 #include "fflas-ffpack/utils/timer.h"
39 #include "fflas-ffpack/fflas/fflas.h"
40 #include "fflas-ffpack/checkers/checkers_fflas.h"
41 #include "fflas-ffpack/checkers/checkers_ffpack.h"
42 #include <fstream>
43
44 using namespace std;
45
46 int main(int argc, char** argv) {
47 size_t NR_TESTS = _NR_TESTS;
48 int q = 131071;
49 size_t MAX_SIZE_MATRICES = _MAX_SIZE_MATRICES;
50 size_t Range = 500;
51 size_t seed( (int) time(NULL) );
52 std::string file("checkers_report.txt");
53
54 Argument as[] = {
55 { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INT , &q },
56 { 'n', "-n N", "Set the dimension of the matrix.", TYPE_INT , &MAX_SIZE_MATRICES },
57 { 'i', "-i R", "Set number of repetitions.", TYPE_INT , &NR_TESTS },
58 { 'r', "-r R", "Set the range of matrix sizes.", TYPE_INT , &Range },
59 { 's', "-s N", "Set the seed.", TYPE_INT , &seed },
60 { 'f', "-f FILE", "Set the output file.", TYPE_STR , &file },
61 END_OF_ARGUMENTS
62 };
63
64 std::ofstream stats_f(file.c_str());
65
66 FFLAS::parseArguments(argc,argv,as);
67
68 srand (seed);
69
70 typedef Givaro::Modular<double> Field;
71 typedef Givaro::Poly1Dom<Field> PolRing;
72 typedef PolRing::Element Polynomial;
73
74 Field F(q);
75 Field::RandIter Rand(F,seed);
76 Field::NonZeroRandIter NZRand(Rand);
77
78 size_t pass;
79 FFLAS::Timer chrono,global;
80 double gffop(0.);
81 global.start();
82 double time1, time2;
83
84 Field::Element_ptr A = FFLAS::fflas_new(F,MAX_SIZE_MATRICES+Range,MAX_SIZE_MATRICES+Range);
85 Field::Element_ptr B = FFLAS::fflas_new(F,MAX_SIZE_MATRICES+Range,MAX_SIZE_MATRICES+Range);
86 Field::Element_ptr C = FFLAS::fflas_new(F,MAX_SIZE_MATRICES+Range,MAX_SIZE_MATRICES+Range);
87 typename Field::Element alpha,beta,tmp;
88 F.init(alpha, rand()%1000+1);
89 F.init(beta, rand()%1000+1);
90 size_t m,n,k,lda,ldb,ldc;
91 FFLAS::FFLAS_TRANSPOSE ta,tb;
92
93 stats_f << " Matrix size\tSuccess rate\t\tTime comput.\t\tTime checker\n\n";
94
95 // ##### FGEMM #####
96 stats_f << "FGEMM:\n";
97 for (size_t i=0; i<MAX_SIZE_MATRICES; i+=Range) {
98 pass = 0; time1 = 0.0; time2 = 0.0;
99 for (size_t j=0; j<NR_TESTS; ++j) {
100 m = rand() % Range + i;
101 n = rand() % Range + i;
102 k = rand() % Range + i;
103 gffop += (2.*double(m)/1000.*double(n)/1000.*double(k)/1000.0);
104
105 ta = FFLAS::FflasNoTrans;//rand()%2 ? FFLAS::FflasNoTrans : FFLAS::FflasTrans,
106 tb = FFLAS::FflasNoTrans;//rand()%2 ? FFLAS::FflasNoTrans : FFLAS::FflasTrans;
107 lda = ta == FFLAS::FflasNoTrans ? k : m,
108 ldb = tb == FFLAS::FflasNoTrans ? n : k,
109 ldc = n;
110
111 PAR_BLOCK { FFLAS::pfrand(F,Rand, m,k,A,m/MAX_THREADS); }
112 PAR_BLOCK { FFLAS::pfrand(F,Rand, k,n,B,k/MAX_THREADS); }
113 PAR_BLOCK { FFLAS::pfrand(F,Rand, m,n,C,n/MAX_THREADS); }
114
115 chrono.clear(); chrono.start();
116 FFLAS::ForceCheck_fgemm<Field> checker1(Rand,m,n,k,beta,C,ldc);
117 chrono.stop(); time1 += chrono.usertime();
118
119 chrono.clear(); chrono.start();
120 FFLAS::fgemm(F,ta,tb,m,n,k,alpha,A,lda,B,ldb,beta,C,ldc);
121 chrono.stop(); time2 += chrono.usertime();
122
123 chrono.clear(); chrono.start();
124 pass += checker1.check(ta,tb,alpha,A,lda,B,ldb,C) ? 1 : 0;
125 chrono.stop(); time1 += chrono.usertime();
126 }
127 time1 /= NR_TESTS;
128 time2 /= NR_TESTS;
129 stats_f << " " << i << "-" << i+Range << "\t\t" << pass << "/" << NR_TESTS << "\t\t\t" << time2
130 << "\t\t" << time1 << endl;
131 }
132 stats_f << endl;
133
134
135
136 // ##### FTRSM #####
137 stats_f << "FTRSM:\n";
138 for (size_t i=0; i<MAX_SIZE_MATRICES; i+=Range) {
139 pass = 0; time1 = 0.0; time2 = 0.0;
140 for (size_t j=0; j<NR_TESTS; ++j) {
141 m = rand() % Range + i;
142 n = rand() % Range + i;
143 gffop += (double(m)/1000.*double(m)/1000.*double(n)/1000.0);
144
145 FFLAS::FFLAS_SIDE side = rand()%2?FFLAS::FflasLeft:FFLAS::FflasRight;
146 FFLAS::FFLAS_UPLO uplo = rand()%2?FFLAS::FflasLower:FFLAS::FflasUpper;
147 FFLAS::FFLAS_TRANSPOSE trans = rand()%2?FFLAS::FflasNoTrans:FFLAS::FflasTrans;
148 FFLAS::FFLAS_DIAG diag = rand()%2?FFLAS::FflasNonUnit:FFLAS::FflasUnit;
149 k = (side==FFLAS::FflasLeft?m:n);
150
151 for( size_t i = 0; i < m*n; ++i ) Rand.random( *(B+i) );
152 for (size_t i=0;i<k;++i) {
153 for (size_t j=0;j<i;++j)
154 A[i*k+j]= (uplo == FFLAS::FflasLower)? Rand.random(tmp) : F.zero;
155 A[i*k+i]= (diag == FFLAS::FflasNonUnit)? NZRand.random(tmp) : F.one;
156 for (size_t j=i+1;j<k;++j)
157 A[i*k+j]= (uplo == FFLAS::FflasUpper)? Rand.random(tmp) : F.zero;
158 }
159
160 chrono.clear(); chrono.start();
161 FFLAS::ForceCheck_ftrsm<Field> checker2(Rand, m, n, alpha, B, n);
162 chrono.stop(); time1 += chrono.usertime();
163
164 chrono.clear(); chrono.start();
165 FFLAS::ftrsm(F, side, uplo, trans, diag, m, n, alpha, A, k, B, n);
166 chrono.stop(); time2 += chrono.usertime();
167
168 chrono.clear(); chrono.start();
169 pass += checker2.check(side, uplo, trans, diag, m, n, A, k, B, n);
170 chrono.stop(); time1 += chrono.usertime();
171 }
172 time1 /= NR_TESTS;
173 time2 /= NR_TESTS;
174 stats_f << " " << i << "-" << i+Range << "\t\t" << pass << "/" << NR_TESTS << "\t\t\t" << time2
175 << "\t\t" << time1 << endl;
176 }
177 stats_f << endl;
178
179
180
181 // ##### INVERT #####
182 stats_f << "INVERT:\n";
183 int nullity;
184 for (size_t i=0; i<MAX_SIZE_MATRICES; i+=Range) {
185 pass = 0; time1 = 0.0; time2 = 0.0;
186 for (size_t j=0; j<NR_TESTS; ++j) {
187 m = rand() % Range + i;
188 gffop += 2*(double(m)/1000.*double(m)/1000.*double(m)/1000.0);
189
190 FFPACK::RandomMatrixWithRankandRandomRPM(F,m,m,m,A,m);
191
192 try {
193 chrono.clear(); chrono.start();
194 FFPACK::ForceCheck_invert<Field> checker3(Rand,m,A,m);
195 chrono.stop(); time1 += chrono.usertime();
196
197 chrono.clear(); chrono.start();
198 FFPACK::Invert(F,m,A,m,nullity);
199 chrono.stop(); time2 += chrono.usertime();
200
201 chrono.clear(); chrono.start();
202 pass += checker3.check(A,nullity);
203 chrono.stop(); time1 += chrono.usertime();
204 } catch(FailureInvertCheck &e) {
205 stats_f << " invert verification failed! " << nullity << std::endl;
206 } catch(FailurePLUQCheck &e) {
207 stats_f << " internal PLUQ verification failed! " << std::endl;
208 }
209 }
210 time1 /= NR_TESTS;
211 time2 /= NR_TESTS;
212 stats_f << " " << i << "-" << i+Range << "\t\t" << pass << "/" << NR_TESTS << "\t\t\t" << time2
213 << "\t\t" << time1 << endl;
214 }
215 stats_f << endl;
216
217
218
219
220 // ##### PLUQ #####
221 stats_f << "PLUQ:\n";
222 for (size_t i=0; i<MAX_SIZE_MATRICES; i+=Range) {
223 pass = 0; time1 = 0.0; time2 = 0.0;
224 for (size_t j=0; j<NR_TESTS; ++j) {
225 m = rand() % Range + i;
226 n = rand() % Range + i;
227
228 PAR_BLOCK { FFLAS::pfrand(F,Rand, m,n,A,m/MAX_THREADS); }
229
230 size_t *P = FFLAS::fflas_new<size_t>(m);
231 size_t *Q = FFLAS::fflas_new<size_t>(n);
232
233 chrono.clear(); chrono.start();
234 FFPACK::ForceCheck_PLUQ<Field> checker4 (Rand,m,n,A,n);
235 chrono.stop(); time1 += chrono.usertime();
236
237 chrono.clear(); chrono.start();
238 k = FFPACK::PLUQ(F, FFLAS::FflasNonUnit, m, n, A, n, P, Q);
239 chrono.stop(); time2 += chrono.usertime();
240
241 #define CUBE(x) ((x)*(x)*(x))
242 gffop += 2.0/3.0*CUBE(double(k)/1000.0) +2*m/1000.0*n/1000.0*double(k)/1000.0 - double(k)/1000.0*double(k)/1000.0*(m+n)/1000;
243
244 chrono.clear(); chrono.start();
245 pass += checker4.check(A,n,FFLAS::FflasNonUnit, k,P,Q);
246 chrono.stop(); time1 += chrono.usertime();
247
248 FFLAS::fflas_delete(P,Q);
249 }
250 time1 /= NR_TESTS;
251 time2 /= NR_TESTS;
252 stats_f << " " << i << "-" << i+Range << "\t\t" << pass << "/" << NR_TESTS << "\t\t\t" << time2
253 << "\t\t" << time1 << endl;
254 }
255 stats_f << endl;
256 global.stop();
257
258
259
260 // ##### CharPoly #####
261 stats_f << "CharPoly:\n";
262 PolRing R(F);
263
264 for (size_t i=0; i<MAX_SIZE_MATRICES; i+=Range) {
265 pass = 0; time1 = 0.0; time2 = 0.0;
266 for (size_t j=0; j<NR_TESTS; ++j) {
267 n = rand() % Range + i;
268
269 PAR_BLOCK { FFLAS::pfrand(F,Rand, n,n,A,n/MAX_THREADS); }
270
271 try {
272 Polynomial g(n);
273
274 chrono.clear(); chrono.start();
275 FFPACK::ForceCheck_charpoly<Field,Polynomial> checker5(Rand,n,A,n);
276 chrono.stop(); time1 += chrono.usertime();
277
278 chrono.clear(); chrono.start();
279 FFPACK::CharPoly(R,g,n,A,n,FFPACK::FfpackLUK);
280 chrono.stop(); time2 += chrono.usertime();
281
282 chrono.clear(); chrono.start();
283 pass += checker5.check(g);
284 chrono.stop(); time1 += chrono.usertime();
285 } catch(FailureCharpolyCheck &e) {
286 stats_f << " charpoly verification failed! " << std::endl;
287 } catch(FailurePLUQCheck &e) {
288 stats_f << " internal PLUQ verification failed! " << std::endl;
289 }
290 }
291 time1 /= NR_TESTS;
292 time2 /= NR_TESTS;
293 stats_f << " " << i << "-" << i+Range << "\t\t" << pass << "/" << NR_TESTS << "\t\t\t" << time2
294 << "\t\t" << time1 << endl;
295 }
296
297
298 FFLAS::fflas_delete(A);
299 FFLAS::fflas_delete(B);
300 FFLAS::fflas_delete(C);
301
302 std::cout << "Time: " << global.realtime()
303 << " Gfops: " << gffop/global.realtime() << std::endl;
304
305 return 0;
306 }
307 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
308 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s