Debian Codegraph @

benchmarks/benchmark-echelon.C

raw
1 /* Copyright (c) FFLAS-FFPACK
2 * Written by Clement Pernet <[email protected]>, from benchmark-pluq by Ziad Sultan
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 // declare that the call to openblas_set_numthread will be made here, hence don't do it
23 // everywhere in the call stack
24 #define __FFLASFFPACK_OPENBLAS_NT_ALREADY_SET
25
26 #include "fflas-ffpack/fflas-ffpack-config.h"
27 #include <iostream>
28 #include "fflas-ffpack/config-blas.h"
29 #include "fflas-ffpack/fflas/fflas.h"
30 #include <givaro/modular-balanced.h>
31 #include "fflas-ffpack/utils/timer.h"
32 #include "fflas-ffpack/utils/args-parser.h"
33 #include "fflas-ffpack/ffpack/ffpack.h"
34 using namespace std;
35
36 typedef Givaro::ModularBalanced<double> Field;
37
38 // random generator function:
39 ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
40
41 // pointer object to it:
42 ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
43
44 typename Field::Element* construct_U(const Field& F, Field::RandIter& G, size_t n, size_t r, std::vector<size_t>& P, size_t commonseed, size_t seed, int nt)
45 {
46 size_t lda = n;
47 Field::Element *U=new Field::Element[n*n];
48
49 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Recursive,FFLAS::StrategyParameter::Threads> H(nt);
50
51 std::vector<size_t> E(r);
52 PARFOR1D(i,r,H, E[i]=i; );
53
54
55 srand48(commonseed);
56 std::vector<size_t> Z(n);
57 PARFOR1D(i,n,H, Z[i]=i;);
58
59 P.resize(r);
60 for(size_t i=0; i<r; ++i) {
61 size_t index=lrand48() % Z.size();
62 P[i] = Z[ index ];
63 Z.erase(Z.begin()+index);
64 }
65
66 PARFOR1D(i,r,H,
67 while( F.isZero( G.random(U[ P[i]*lda+P[i] ]) ) ) {}
68 for(size_t j=P[i]+1;j<n;++j)
69 G.random(U[ P[i]*lda+j]);
70 );
71
72 return U;
73 }
74
75 typename Field::Element* construct_L(const Field& F, Field::RandIter& G, size_t m, size_t r, const std::vector<size_t>& P, size_t seed, int nt)
76 {
77 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Recursive,FFLAS::StrategyParameter::Threads> H(nt);
78
79 size_t lda = m;
80 size_t taille=m*m;
81 Field::Element * L= new Field::Element[taille];
82 PARFOR1D(i,taille,H, { F.init(L[i]); F.assign(L[i],F.zero); } );
83
84 std::vector<size_t> E(r);
85 PARFOR1D(i,r,H, E[i]=i;);
86
87 srand48(seed);
88 std::vector<size_t> Z(m);
89 PARFOR1D(i,m,H, Z[i]=i; );
90
91 std::vector<size_t> Q(r);
92 for(size_t i=0; i<r; ++i) {
93 size_t index=lrand48() % Z.size();
94 Q[i] = Z[ index ];
95 Z.erase(Z.begin()+index);
96 }
97
98 for(size_t i=0; i<r; ++i) {
99 size_t index=lrand48() % E.size();
100 size_t perm = E[ index ];
101
102 E.erase(E.begin()+index);
103 F.init(L[Q[perm]*lda+P[perm]]);
104 F.assign(L[Q[perm]*lda+P[perm]], F.one);
105 for(size_t j=Q[perm]+1;j<m;++j)
106 G.random(L[j*lda+P[perm]]);
107 }
108 return L;
109 }
110
111
112 typename Field::Element* M_randgen(const Field& F, typename Field::Element* L,typename Field::Element* U, size_t r, size_t m, size_t n, int nt)
113 {
114 Field::Element alpha, beta;
115 F.init(alpha);
116 F.assign(alpha, F.one);
117 F.init(beta);
118 F.assign(beta, F.zero);
119 size_t lda = n;
120 Field::Element * A = new Field::Element[m*n];
121
122 // Computing produit L * U (ideally should use parallel ftrmm
123
124 /*
125 FFLAS::ftrmm(F, FFLAS::FflasRight, FFLAS::FflasUpper, FFLAS::FflasNoTrans, FFLAS::FflasNonUnit, m,n,1.0, U, lda, L, lda);
126 */
127
128
129 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Block,FFLAS::StrategyParameter::Threads> pWH(nt);
130 PAR_BLOCK{
131 FFLAS::fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
132 m,n,r, alpha, L,r, U,
133 lda,beta,A,lda,pWH);
134 }
135 return L;
136 }
137
138 void verification_PLUQ(const Field & F, typename Field::Element * B, typename Field::Element * A,
139 size_t * P, size_t * Q, size_t m, size_t n, size_t R, int nt)
140 {
141
142 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Recursive,FFLAS::StrategyParameter::Threads> H(nt);
143
144 Field::Element * X = FFLAS::fflas_new<Field::Element>(m*n);
145 Field::Element * L, *U;
146 L = FFLAS::fflas_new<Field::Element>(m*R);
147 U = FFLAS::fflas_new<Field::Element>(R*n);
148
149 PARFOR1D (i,m*R,H, { F.init(L[i]); F.assign(L[i], 0.0); } );
150 PARFOR1D (i,n*R,H, { F.init(U[i]); F.assign(U[i], 0.0); } );
151 PARFOR1D (i,m*n,H, { F.init(X[i]); F.assign(X[i], 0.0); } );
152
153
154 PARFOR1D (i,R,H,
155 for (size_t j=0; j<i; ++j)
156 F.assign ( *(U + i*n + j), F.zero);
157 for (size_t j=i; j<n; ++j)
158 F.assign (*(U + i*n + j), *(A+ i*n+j));
159 );
160 PARFOR1D (j,R,H,
161 for (size_t i=0; i<=j; ++i )
162 F.assign( *(L+i*R+j), F.zero);
163 F.assign(*(L+j*R+j), F.one);
164 for (size_t i=j+1; i<m; i++)
165 F.assign( *(L + i*R+j), *(A+i*n+j));
166 );
167
168 PAR_BLOCK{
169 #pragma omp task shared(F, P, L)
170 FFPACK::applyP( F, FFLAS::FflasLeft, FFLAS::FflasTrans, R,0,m, L, R, P);
171 #pragma omp task shared(F, Q, U)
172 FFPACK::applyP (F, FFLAS::FflasRight, FFLAS::FflasNoTrans, R,0,n, U, n, Q);
173 #pragma omp taskwait
174 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Block,FFLAS::StrategyParameter::Threads> pWH (nt);
175 #pragma omp task shared(F, L, U, X)
176 FFLAS::fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, m,n,R,
177 F.one, L,R, U,n, F.zero, X,n, pWH);
178
179 }
180 bool fail = false;
181 // PARFOR1D (size_t i=0; i<m; ++i)
182 for(size_t i=0; i<m; ++i)
183 for (size_t j=0; j<n; ++j)
184 if (!F.areEqual (*(B+i*n+j), *(X+i*n+j))){
185 std::cout << " Initial["<<i<<","<<j<<"] = " << (*(B+i*n+j))
186 << " Result"<<i<<","<<j<<"] = " << (*(X+i*n+j))
187 << std::endl;
188
189 std::stringstream errs;
190 errs << " B["<<i<<","<<j<<"] = " << (*(B+i*n+j))
191 << " X["<<i<<","<<j<<"] = " << (*(X+i*n+j))
192 << std::endl;
193 std::cout << errs.str();
194 fail=true;
195 std::cout<<" end verification"<<std::endl;
196 exit(1);
197 }
198
199 if (fail)
200 std::cout<<"FAIL"<<std::endl;
201 else
202 std::cout<<"PASS"<<std::endl;
203
204 FFLAS::fflas_delete( U);
205 FFLAS::fflas_delete( L);
206 FFLAS::fflas_delete( X);
207 }
208
209
210
211 int main(int argc, char** argv) {
212
213 #ifdef __FFLASFFPACK_OPENBLAS_NUM_THREADS
214 openblas_set_num_threads(__FFLASFFPACK_OPENBLAS_NUM_THREADS);
215 #endif
216
217 size_t iter = 3 ;
218 int q = 131071 ;
219 Field F(q);
220 int m = 2000 ;
221 int n = 2000 ;
222 size_t r = 2000 ;
223 int v = 0;
224
225 int nt = -1;
226
227 int NBK = -1;
228 int a=1;
229 bool transform = false;
230 Argument as[] = {
231 { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INT , &q },
232 { 'm', "-m M", "Set the row dimension of A.", TYPE_INT , &m },
233 { 'n', "-n N", "Set the col dimension of A.", TYPE_INT , &n },
234 { 'r', "-r R", "Set the rank of matrix A.", TYPE_INT , &r },
235 { 'i', "-i I", "Set number of repetitions.", TYPE_INT , &iter },
236 { 'v', "-v V", "Set 1 if need verification of result else 0.", TYPE_INT , &v },
237 { 't', "-t T", "whether to compute the transformation matrix.", TYPE_BOOL , &transform },
238 { 'a', "-a A", "Algorithm for PLUQ decomposition: 0=LUdivine 1=PLUQ.", TYPE_INT , &a },
239 { 'b', "-b B", "number of numa blocks per dimension for the numa placement", TYPE_INT , &NBK },
240 { 'p', "-p P", "number of threads to use", TYPE_INT , &nt },
241 END_OF_ARGUMENTS
242 };
243 // { 'p', "-p P", "0 for sequential, 1 for 2D iterative,
244 //2 for 2D rec, 3 for 2D rec adaptive, 4 for 3D rc in-place, 5 for 3D rec, 6 for 3D rec adaptive.", TYPE_INT , &p },
245 FFLAS::parseArguments(argc,argv,as);
246 std::cerr<<"nt:="<<nt<<std::endl; if(nt==-1) PAR_BLOCK{ nt=NUM_THREADS; } std::cerr<<"nt:="<<nt<<std::endl;
247 FFPACK::FFPACK_LU_TAG LuTag = a?FFPACK::FfpackTileRecursive:FFPACK::FfpackSlabRecursive;
248 if (NBK==-1) NBK = nt;
249 typedef Field::Element Element;
250 Element * A, * Acop;
251 A = FFLAS::fflas_new(F,m,n,Alignment::CACHE_PAGESIZE);
252 Acop = FFLAS::fflas_new(F,m,n,Alignment::CACHE_PAGESIZE);
253
254 Field::Element * U = new Field::Element[n*n];
255 // random seed
256 ifstream f("/dev/urandom");
257 size_t seed1, seed2, seed3,seed4;
258 f.read(reinterpret_cast<char*>(&seed1), sizeof(seed1));
259 f.read(reinterpret_cast<char*>(&seed2), sizeof(seed2));
260 f.read(reinterpret_cast<char*>(&seed3), sizeof(seed3));
261 f.read(reinterpret_cast<char*>(&seed4), sizeof(seed4));
262 std::vector<size_t> Index_P(r);
263 Field::RandIter GG(F, seed1);
264
265 PAR_BLOCK{ FFLAS::pfrand(F,GG,m,n,A,m/NBK); }
266
267
268 // std::cout<<"Construct U"<<endl;
269 U = construct_U(F,GG, n, r, Index_P, seed4, seed3,nt);
270 // std::cout<<"Construct L"<<endl;
271 A = construct_L(F,GG, m, r, Index_P, seed2, nt);
272 // std::cout<<"randgen"<<endl;
273 A = M_randgen(F, A, U, r, m, n, nt);
274 size_t R=0;
275 FFLAS::Timer chrono;
276 double time=0.0;
277
278 // enum FFLAS::FFLAS_DIAG diag = FFLAS::FflasNonUnit;
279 size_t maxP, maxQ;
280 maxP = m;
281 maxQ = n;
282
283 size_t *P = FFLAS::fflas_new<size_t>(maxP);
284 size_t *Q = FFLAS::fflas_new<size_t>(maxQ);
285
286 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Recursive,FFLAS::StrategyParameter::Threads> H(nt);
287
288 PARFOR1D(i,(size_t)m,H,
289 for (size_t j=0; j<(size_t)n; ++j)
290 Acop[i*n+j]= (*(A+i*n+j));
291 );
292
293 for (size_t i=0;i<=iter;++i){
294
295 PARFOR1D(j,maxP,H, P[j]=0; );
296
297 PARFOR1D(j,maxQ,H, Q[j]=0; );
298
299 PARFOR1D(k,(size_t)m,H,
300 for (size_t j=0; j<(size_t)n; ++j)
301 *(A+k*n+j) = *(Acop+k*n+j) ;
302 );
303
304 //Additional tests for templated helper
305 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Recursive,FFLAS::StrategyParameter::Threads> parH(nt);
306
307
308 chrono.clear();
309
310 if (i) chrono.start();
311 // Added by AB 2014-12-15
312 //#ifdef __FFLASFFPACK_USE_OPENMP
313 PAR_BLOCK{
314 r = FFPACK::RowEchelonForm(F,m,n,A,n,P,Q,transform,LuTag,parH);
315 }
316 if (i) {chrono.stop(); time+=chrono.realtime();}
317
318 }
319
320 // -----------
321 // Standard output for benchmark - Alexis Breust 2014/11/14
322 #define CUBE(x) ((x)*(x)*(x))
323 double gflop = 2.0/3.0*CUBE(double(r)/1000.0) +2*m/1000.0*n/1000.0*double(r)/1000.0 - double(r)/1000.0*double(r)/1000.0*(m+n)/1000;
324 if (transform)
325 gflop += CUBE(double(r)/1000.0)/3.0 + double(r)/1000.0*double(r)/1000.0*double(n-r)/1000.0;
326 std::cout << "Time: " << time / double(iter) << " Gfops: " << gflop / time * double(iter-1);
327 FFLAS::writeCommandString(std::cout, as) << std::endl;
328
329 //verification
330 if(v)
331 verification_PLUQ(F,Acop,A,P,Q,m,n,R,nt);
332 FFLAS::fflas_delete( A);
333 FFLAS::fflas_delete( Acop);
334
335 return 0;
336 }
337
338 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
339 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
340