Debian Codegraph @

benchmarks/benchmark-fgemv-mp.C

raw
1 /*
2 * Copyright (C) FFLAS-FFPACK
3 * Written by Pascal Giorgi <[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 // declare that the call to openblas_set_numthread will be made here, hence don't do it
28 // everywhere in the call stack
29 #define __FFLASFFPACK_OPENBLAS_NT_ALREADY_SET 1
30
31 #if not defined(MG_DEFAULT)
32 #define MG_DEFAULT MG_ACTIVE
33 #endif
34 #if not defined(STD_RECINT_SIZE)
35 #define STD_RECINT_SIZE 8
36 #endif
37
38
39 #include "fflas-ffpack/fflas-ffpack-config.h"
40 #include <iostream>
41 #include <typeinfo>
42 #include <vector>
43 #include <string>
44 using namespace std;
45
46 #include "fflas-ffpack/utils/timer.h"
47 #include "fflas-ffpack/fflas/fflas.h"
48 #include "fflas-ffpack/utils/args-parser.h"
49 #include "givaro/modular-integer.h"
50 #include "givaro/givcaster.h"
51 #include "fflas-ffpack/paladin/parallel.h"
52 #ifdef BENCH_RECINT
53 #include "recint/recint.h"
54 #endif
55
56
57 template<typename T>
58 std::ostream& write_matrix(std::ostream& out, Givaro::Integer p, size_t m, size_t n, T* C, size_t ldc){
59
60 size_t www(size_t((double(p.bitsize())*log(2.))/log(10.)));
61 out<<"Matrix("<<m<<','<<n<<",[[";
62 out.width(www+1);
63 out<<std::right<<C[0];
64 for (size_t j=1;j<n;++j){
65 out<<',';
66 out.width(www);
67 out<<std::right<<C[j];
68 }
69 out<<']';
70 for (size_t i=1;i<m;++i){
71 out<<endl<<",[";
72 out.width(www+1);
73 out<<std::right<<C[i*ldc];
74 for (size_t j=1;j<n;++j){
75 out<<',';
76 out.width(www);
77 out<<std::right<<C[i*ldc+j];
78 }
79 out<<']';
80 }
81 return out<<"])";
82 }
83
84
85 static size_t iters = 3 ;
86 static Givaro::Integer q = -1 ;
87 static unsigned long b = 512 ;
88 static size_t m = 512 ;
89 static size_t k = 512 ;
90 static int nbw = -1 ;
91 static size_t seed= time(NULL);
92 static Argument as[] = {
93 { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INTEGER , &q },
94 { 'b', "-b B", "Set the bitsize of the random characteristic.", TYPE_INT , &b },
95 { 'm', "-m M", "Set the dimension m of the matrix.", TYPE_INT , &m },
96 { 'k', "-k K", "Set the dimension k of the matrix.", TYPE_INT , &k },
97 { 'w', "-w N", "Set the number of winograd levels (-1 for random).", TYPE_INT , &nbw },
98 { 'i', "-i R", "Set number of repetitions.", TYPE_INT , &iters },
99 { 's', "-s S", "Sets seed.", TYPE_INT , &seed },
100 END_OF_ARGUMENTS
101 };
102
103 template<typename Ints>
104 int tmain(){
105 srand( (int)seed);
106 srand48(seed);
107 Givaro::Integer::seeding(seed);
108
109 typedef Givaro::Modular<Ints> Field;
110 Givaro::Integer p;
111 FFLAS::Timer chrono, TimFreivalds;
112 double time=0.;
113 for (size_t loop=0;loop<iters;loop++){
114 Givaro::Integer::random_exact_2exp(p, b);
115 Givaro::IntPrimeDom IPD;
116 IPD.nextprimein(p);
117 Ints ip; Givaro::Caster<Ints,Givaro::Integer>(ip,p);
118 Givaro::Caster<Givaro::Integer,Ints>(p,ip); // to check consistency
119
120 Field F(ip);
121 size_t lda,ldb,ldc;
122 lda=k;
123 ldb=1;
124 ldc=1;
125
126 typename Field::RandIter Rand(F,seed);
127 typename Field::Element_ptr A,B,C;
128 A= FFLAS::fflas_new(F,m,lda);
129 B= FFLAS::fflas_new(F,k,ldb);
130 C= FFLAS::fflas_new(F,m,ldc);
131
132 // for (size_t i=0;i<m;++i)
133 // for (size_t j=0;j<k;++j)
134 // Rand.random(A[i*lda+j]);
135 // for (size_t i=0;i<k;++i)
136 // for (size_t j=0;j<n;++j)
137 // Rand.random(B[i*ldb+j]);
138 // for (size_t i=0;i<m;++i)
139 // for (size_t j=0;j<n;++j)
140 // Rand.random(C[i*ldc+j]);
141
142 PAR_BLOCK { FFLAS::pfrand(F,Rand, m,k,A,m/size_t(MAX_THREADS)); }
143 PAR_BLOCK { FFLAS::pfrand(F,Rand, k,1,B,k/MAX_THREADS); }
144 PAR_BLOCK { FFLAS::pfzero(F, m,1,C,m/MAX_THREADS); }
145
146
147 Ints alpha,beta;
148 alpha=F.one;
149 beta=F.zero;
150
151
152 using FFLAS::CuttingStrategy::Recursive;
153 using FFLAS::StrategyParameter::TwoDAdaptive;
154 // RNS MUL_LA
155 chrono.clear();chrono.start();
156 {
157 FFLAS::ParSeqHelper::Sequential seqH;
158 FFLAS::fgemv(F,FFLAS::FflasNoTrans,m,k,alpha,A,lda,B,ldb,beta,C,ldc,seqH);
159 }
160 chrono.stop();
161 time+=chrono.realtime();
162
163 FFLAS::fflas_delete(A);
164 FFLAS::fflas_delete(B);
165 FFLAS::fflas_delete(C);
166
167 }
168
169 double Mflops=((2.*double(m)-1)/1000.*double(k)/1000.0) /time * double(iters);
170 // Mflops*=p.bitsize()/16.;
171 cout << "Time: "<< (time/double(iters)) <<" Gfops: "<<Mflops*1.0/1000.0
172 << " (total:" << time <<") "
173 <<typeid(Ints).name()
174 <<" perword: "<< (Mflops*double(p.bitsize()))/64. ;
175 FFLAS::writeCommandString(std::cout << " | " << p << " (" << p.bitsize()<<")|", as) << std::endl;
176 return 0;
177 }
178
179
180
181 int main(int argc, char** argv){
182
183 #ifdef __FFLASFFPACK_OPENBLAS_NUM_THREADS
184 openblas_set_num_threads(__FFLASFFPACK_OPENBLAS_NUM_THREADS);
185 #endif
186
187 FFLAS::parseArguments(argc,argv,as);
188
189 int r1 = tmain<Givaro::Integer>();
190
191 #ifdef BENCH_RECINT
192 r1 += tmain<RecInt::rint<STD_RECINT_SIZE>>();
193 #endif
194 return r1;
195 }
196
197 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
198 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s