Debian Codegraph @

benchmarks/benchmark-fgemv.C

raw
1 /* Copyright (c) FFLAS-FFPACK
2 * ========LICENCE========
3 * This file is part of the library FFLAS-FFPACK.
4 *
5 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 * ========LICENCE========
19 */
20
21 //#include "goto-def.h"
22
23 // declare that the call to openblas_set_numthread will be made here, hence don't do it
24 // everywhere in the call stack
25 #define __FFLASFFPACK_OPENBLAS_NT_ALREADY_SET 1
26
27 #include "fflas-ffpack/fflas-ffpack-config.h"
28 #include <iostream>
29 #include <givaro/modular-balanced.h>
30
31 #include "fflas-ffpack/config-blas.h"
32 #include "fflas-ffpack/fflas/fflas.h"
33 #include "fflas-ffpack/utils/timer.h"
34 #include "fflas-ffpack/utils/args-parser.h"
35
36 #include "fflas-ffpack/utils/fflas_io.h"
37 #include "fflas-ffpack/utils/test-utils.h"
38
39 #include "fflas-ffpack/utils/timer.h"
40 #include "givaro/modular-integer.h"
41 #include "givaro/givcaster.h"
42
43 using namespace FFPACK;
44
45 using namespace std;
46 using namespace FFLAS;
47
48 template <typename Field>
49 struct need_field_characteristic { static constexpr bool value = false; };
50 template <typename Field>
51 struct need_field_characteristic<Givaro::Modular<Field>>{ static constexpr bool value = true; };
52 template <typename Field>
53 struct need_field_characteristic<Givaro::ModularBalanced<Field>>{ static constexpr bool value = true; };
54
55 template <typename Field>
56 struct compatible_data_type { static constexpr bool value = true; };
57 template <>
58 struct compatible_data_type<Givaro::ZRing<float>>{ static constexpr bool value = false; };
59 template <>
60 struct compatible_data_type<Givaro::ZRing<double>>{ static constexpr bool value = false; };
61
62
63 template <class Field, class RandIter, class Matrix, class Vector>
64 void fill_value(Field& F, RandIter& Rand,
65 Matrix& A, Vector& X, Vector& Y,
66 size_t m, size_t k, size_t incX, size_t incY, size_t lda, int NBK){
67 // TODO: replace by a 1D pfrand
68 SYNCH_GROUP(
69 FORBLOCK1D(iter, m, SPLITTER(NBK, CuttingStrategy::Row, StrategyParameter::Threads),
70 TASK(MODE(CONSTREFERENCE(F,Rand,A)),
71 {
72 frand(F, Rand, iter.end()-iter.begin(), k, A+iter.begin()*lda, lda);
73 }
74 );
75 );
76 );
77 //FFLAS::pfrand(F,Rand, m,k,A,m/NBK);
78 FFLAS::frand(F,Rand, k,1,X,incX);
79 FFLAS::fzero(F, m,1,Y,incY);
80 }
81
82 template <class Field, class Matrix, class Vector>
83 void genData(Field& F,
84 Matrix& A, Vector& X, Vector& Y,
85 size_t m, size_t k, size_t incX, size_t incY, size_t lda, int NBK,
86 uint64_t bitsize, uint64_t seed){
87 Givaro::Integer samplesize(1); samplesize <<= bitsize;
88 typename Field::RandIter Rand(F,seed,samplesize);
89 fill_value(F, Rand, A, X, Y, m, k, incX, incY, lda, NBK);
90 }
91
92 template <class Field, class Matrix, class Vector>
93 bool check_result(Field& F, size_t m, size_t lda, Matrix& A, Vector& X, size_t incX, Vector& Y, size_t incY){
94 //Naive result checking by comparing result from pfgemv against the one from fgemv
95 typename Field::Element_ptr Y2 = FFLAS::fflas_new(F,m,1);
96 FFLAS::fgemv(F, FFLAS::FflasNoTrans, m, lda, F.one, A, lda, X, incX, F.zero, Y2, incY);
97
98 for(size_t j=0; j<m; ++j){
99 if(!F.areEqual(Y2[j],Y[j])){
100 FFLAS::fflas_delete(Y2);
101 return false;
102 }
103 }
104 FFLAS::fflas_delete(Y2);
105 return true;
106 }
107
108
109 template <class Field, class Matrix, class Vector>
110 bool benchmark_with_timer(Field& F, int p, Matrix& A, Vector& X, Vector& Y, size_t m, size_t k, size_t incX,
111 size_t incY, size_t lda, size_t iters, int t, double& time, size_t GrainSize){
112 Timer chrono;
113 bool pass = true;
114 for (size_t i=0;i<=iters;++i){
115
116 chrono.clear();
117
118 if (p){
119
120 typedef CuttingStrategy::Row row;
121 typedef CuttingStrategy::Recursive rec;
122 typedef StrategyParameter::Threads threads;
123 typedef StrategyParameter::Grain grain;
124
125 if (i) { chrono.start(); }
126
127 switch (p){
128 case 1:{
129 ParSeqHelper::Parallel<rec, threads> H(t);
130 FFLAS::fgemv(F, FFLAS::FflasNoTrans, m, lda, F.one, A, lda, X, incX, F.zero, Y, incY, H);
131 break;}
132 case 2:{
133 ParSeqHelper::Parallel<row, threads> H(t);
134 FFLAS::fgemv(F, FFLAS::FflasNoTrans, m, lda, F.one, A, lda, X, incX, F.zero, Y, incY, H);
135 break;
136 }
137 case 3:{
138 ParSeqHelper::Parallel<row, grain> H(GrainSize);
139 FFLAS::fgemv(F, FFLAS::FflasNoTrans, m, lda, F.one, A, lda, X, incX, F.zero, Y, incY, H);
140 break;
141 }
142 default:{
143 FFLAS::fgemv(F, FFLAS::FflasNoTrans, m, lda, F.one, A, lda, X, incX, F.zero, Y, incY);
144 break;
145 }
146 }
147
148 if (i) {chrono.stop(); time+=chrono.realtime();}
149 }else{
150 if (i) chrono.start();
151 FFLAS::fgemv(F, FFLAS::FflasNoTrans, m, lda, F.one, A, lda, X, incX, F.zero, Y, incY);
152 if (i) {chrono.stop(); time+=chrono.realtime();}
153 }
154
155 if(!check_result(F, m, lda, A, X, incX, Y, incY)){
156 pass = false;
157 break;
158 }
159
160 }
161 return pass;
162 }
163
164 template <class Field, class arg>
165 void benchmark_disp(Field& F, bool pass, double& time, size_t iters, int p, size_t m, size_t k, arg& as){
166 if(pass){
167 std::cout << "Time: " << time / double(iters)
168 << " Gflops: " << (2.*double(m)/1000.*double(k)/1000.0/1000.0) / time * double(iters);
169 writeCommandString(std::cout, as) << std::endl;
170 }else{
171 std::cout<<"FAILED for "<<typeid(Field).name()<<std::endl;
172 std::cout << "p:=" << p << ';'<<std::endl;
173 }
174 }
175
176
177 template <class Field, class arg>
178 void benchmark_in_Field(Field& F, int p, size_t m, size_t k, int NBK, uint64_t bitsize, uint64_t seed, size_t iters,
179 int t, arg& as, size_t GrainSize){
180 double time=0.0;
181 size_t lda,incX,incY;
182 lda=k;
183 incX=1;
184 incY=1;
185 typename Field::Element_ptr A,X,Y;
186
187 A = FFLAS::fflas_new(F,m,lda);
188 X = FFLAS::fflas_new(F,k,incX);
189 Y = FFLAS::fflas_new(F,m,incY);
190
191 genData(F, A, X, Y, m, k, incX, incY, lda, NBK, bitsize, seed);
192
193 bool pass=benchmark_with_timer( F, p, A, X, Y, m, k, incX, incY, lda, iters, t, time, GrainSize);
194
195 benchmark_disp(F, pass, time, iters, p, m, k, as);
196
197 FFLAS::fflas_delete(A);
198 FFLAS::fflas_delete(X);
199 FFLAS::fflas_delete(Y);
200
201 }
202
203 template <class Field, class arg >
204 void benchmark_with_field(int p, size_t m, size_t k, int NBK, uint64_t bitsize, uint64_t seed, size_t iters,
205 int t, arg& as, size_t GrainSize){
206 Field F;
207 //static assert to raise compile time error for Non ZRing without providing a characteristic
208 static_assert(!need_field_characteristic<Field>::value,
209 "A field characteristic should be provided for Non ZRing data type !");
210 //static assert to raise compile time error for ZRing with either float or double that could lead to inconsistent result
211 static_assert(compatible_data_type<Field>::value,
212 "The provided data type for ZRing is not compatible for the desired operation and could lead to inconsistent result !");
213
214 benchmark_in_Field(F, p, m, k, NBK, bitsize, seed, iters, t, as, GrainSize);
215
216 }
217
218 template <class Field, class arg>
219 void benchmark_with_field(const Givaro::Integer& q, int p, size_t m, size_t k,
220 int NBK, uint64_t bitsize, uint64_t seed, size_t iters, int t,
221 arg& as, size_t GrainSize){
222 Field F(q);
223 benchmark_in_Field(F, p, m, k, NBK, bitsize, seed, iters, t, as, GrainSize);
224 }
225
226 int main(int argc, char** argv) {
227
228 #ifdef __FFLASFFPACK_OPENBLAS_NUM_THREADS
229 openblas_set_num_threads(__FFLASFFPACK_OPENBLAS_NUM_THREADS);
230 #endif
231
232 int p=0;
233
234 size_t iters = 3;
235 Givaro::Integer q = 131071;
236 size_t m = 4000;
237 size_t k = 4000;
238
239 uint64_t seed = getSeed();
240 int t;
241 PAR_BLOCK { t = NUM_THREADS; }
242 int NBK = -1;
243 uint64_t b=0;
244 size_t GrainSize = 64;
245
246 Argument as[] = {
247 { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INTEGER , &q },
248 { 'b', "-b B", "Set the bitsize of input.", TYPE_INT , &b },
249 { 'p', "-p P", "0 for sequential, 1 for <Recursive,Thread>, 2 for <Row,Thread>, 3 for <Row,Grain>.",
250 TYPE_INT , &p },
251 { 'm', "-m M", "Set the dimension m of the matrix.", TYPE_INT , &m },
252 { 'k', "-k K", "Set the dimension k of the matrix.", TYPE_INT , &k },
253 { 't', "-t T", "number of virtual threads to drive the partition.", TYPE_INT , &t },
254 { 'N', "-n N", "number of numa blocks per dimension for the numa placement", TYPE_INT , &NBK },
255 { 'i', "-i R", "Set number of repetitions.", TYPE_INT , &iters },
256 { 's', "-s S", "Sets seed.", TYPE_INT , &seed },
257 { 'g', "-g G", "Sets GrainSize.", TYPE_INT , &GrainSize },
258 END_OF_ARGUMENTS
259 };
260
261 parseArguments(argc,argv,as);
262
263 if (NBK==-1) NBK = t;
264 if(q==0){
265 PAR_BLOCK {
266 //benchmark_with_field<Givaro::ZRing<int32_t>>( p, m, k, NBK, b, seed, iters, t, as);
267 benchmark_with_field<Givaro::ZRing<Givaro::Integer>>( p, m, k, NBK, b, seed, iters, t, as, GrainSize);
268 }
269 }else{
270 PAR_BLOCK {
271 //benchmark_with_field<Givaro::Modular<float>>(q, p, m, k, NBK, b, seed, iters, t, as);
272 //benchmark_with_field<Givaro::Modular<double>>(q, p, m, k, NBK, b, seed, iters, t, as);
273 //benchmark_with_field<Givaro::Modular<int32_t>>(q, p, m, k, NBK, b, seed, iters, t, as);
274
275 //benchmark_with_field<Givaro::Modular<Givaro::Integer>>(q, p, m, k, NBK, b, seed, iters, t, as);
276
277 //benchmark_with_field<Givaro::ModularBalanced<float>>(q, p, m, k, NBK, b, seed, iters, t, as);
278 benchmark_with_field<Givaro::ModularBalanced<double>>(q, p, m, k, NBK, b, seed, iters, t, as, GrainSize);
279 //benchmark_with_field<Givaro::ModularBalanced<int32_t>>(q, p, m, k, NBK, b, seed, iters, t, as);
280 }
281 }
282
283 return 0;
284 }
285 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
286 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s