Debian Codegraph @

benchmarks/benchmark-pluq.C

raw
1 /* Copyright (c) FFLAS-FFPACK
2 * Written by Ziad Sultan <[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 //#define __FFLASFFPACK_USE_OPENMP
25 //#define __FFLASFFPACK_USE_TBB
26
27 //#define __FFLASFFPACK_USE_DATAFLOW
28 //#define __FFLASFFPACK_FORCE_SEQ
29 //#define WINOPAR_KERNEL
30 //#define CLASSIC_SEQ
31 // #define PROFILE_PLUQ
32 // #define MONOTONIC_CYCLES
33 // #define MONOTONIC_MOREPIVOTS
34 // #define MONOTONIC_FEWPIVOTS
35
36 #ifdef MONOTONIC_CYCLES
37 #define MONOTONIC_APPLYP
38 #endif
39 #ifdef MONOTONIC_MOREPIVOTS
40 #define MONOTONIC_APPLYP
41 #endif
42 #ifdef MONOTONIC_FEWPIVOTS
43 #define MONOTONIC_APPLYP
44 #endif
45
46 // declare that the call to openblas_set_numthread will be made here, hence don't do it
47 // everywhere in the call stack
48 #define __FFLASFFPACK_OPENBLAS_NT_ALREADY_SET 1
49
50 #include "fflas-ffpack/fflas-ffpack-config.h"
51 #include <givaro/modular.h>
52 #include <givaro/givranditer.h>
53 #include <iostream>
54
55 #include "fflas-ffpack/config-blas.h"
56 #include "fflas-ffpack/fflas/fflas.h"
57 #include "fflas-ffpack/utils/timer.h"
58 #include "fflas-ffpack/utils/fflas_randommatrix.h"
59 #include "fflas-ffpack/utils/args-parser.h"
60 #include "fflas-ffpack/ffpack/ffpack.h"
61
62 #ifdef __FFLASFFPACK_USE_KAAPI
63 #include "libkomp.h"
64 #endif
65
66 using namespace std;
67
68 typedef Givaro::ModularBalanced<double> Field;
69 //typedef Givaro::ModularBalanced<float> Field;
70 //typedef Givaro::ZRing<double> Field;
71 //typedef Givaro::UnparametricZRing<double> Field;
72
73 void verification_PLUQ(const Field & F, typename Field::Element * B, typename Field::Element * A,
74 size_t * P, size_t * Q, size_t m, size_t n, size_t R)
75 {
76
77 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Block,FFLAS::StrategyParameter::Threads> H;
78
79 Field::Element_ptr X = FFLAS::fflas_new (F, m,n);
80 Field::Element_ptr L, U;
81 L = FFLAS::fflas_new(F, m,R);
82 U = FFLAS::fflas_new(F, R,n);
83
84 PARFOR1D (i, m*R,H, F.init(L[i], 0.0); );
85
86 PARFOR1D (i,n*R,H, F.init(U[i], 0.0); );
87
88 PARFOR1D (i,m*n,H, F.init(X[i], 0.0); );
89
90 PARFOR1D (i,R,H,
91 for (size_t j=0; j<i; ++j)
92 F.assign ( *(U + i*n + j), F.zero);
93 for (size_t j=i; j<n; ++j)
94 F.assign (*(U + i*n + j), *(A+ i*n+j));
95 );
96
97 PARFOR1D (j,R,H,
98 for (size_t i=0; i<=j; ++i )
99 F.assign( *(L+i*R+j), F.zero);
100 F.assign(*(L+j*R+j), F.one);
101 for (size_t i=j+1; i<m; i++)
102 F.assign( *(L + i*R+j), *(A+i*n+j));
103 );
104
105 PAR_BLOCK{
106 SYNCH_GROUP(
107
108 TASK(MODE(CONSTREFERENCE(F,P,L)),
109 FFPACK::applyP( F, FFLAS::FflasLeft, FFLAS::FflasTrans, R,0,m, L, R, P););
110 TASK(MODE(CONSTREFERENCE(F,Q,U)),
111 FFPACK::applyP (F, FFLAS::FflasRight, FFLAS::FflasNoTrans, R,0,n, U, n, Q););
112 WAIT;
113 typename FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Block,FFLAS::StrategyParameter::Threads> pWH (MAX_THREADS);
114
115 TASK(MODE(CONSTREFERENCE(F,U,L,X)),
116 FFLAS::fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, m,n,R,
117 F.one, L,R, U,n, F.zero, X,n, pWH););
118 );
119 }
120 bool fail = false;
121 for(size_t i=0; i<m; ++i)
122 for (size_t j=0; j<n; ++j)
123 if (!F.areEqual (*(B+i*n+j), *(X+i*n+j))){
124 std::cout << " Initial["<<i<<","<<j<<"] = " << (*(B+i*n+j))
125 << " Result["<<i<<","<<j<<"] = " << (*(X+i*n+j))
126 << std::endl;
127 fail=true;
128 }
129
130 if (fail)
131 std::cout<<"FAIL"<<std::endl;
132 else
133 std::cout<<"PASS"<<std::endl;
134
135 FFLAS::fflas_delete( U);
136 FFLAS::fflas_delete( L);
137 FFLAS::fflas_delete( X);
138 }
139
140 void Rec_Initialize(Field &F, Field::Element * C, size_t m, size_t n, size_t ldc)
141 {
142 if(std::min(m,n) <= ldc/NUM_THREADS){
143 for(size_t i=0; i<m; i++)
144 FFLAS::fzero(F, 1, n, C+i*n, n);
145 }
146 else{
147 size_t M2 = m >> 1;
148 size_t N2 = n >> 1;
149 typename Field::Element * C2 = C + N2;
150 typename Field::Element * C3 = C + M2*ldc;
151 typename Field::Element * C4 = C3 + N2;
152
153 SYNCH_GROUP(
154 TASK(MODE(CONSTREFERENCE(F)), Rec_Initialize(F,C,M2,N2, ldc););
155 TASK(MODE(CONSTREFERENCE(F)), Rec_Initialize(F,C2,M2,n-N2, ldc););
156 TASK(MODE(CONSTREFERENCE(F)), Rec_Initialize(F,C3,m-M2,N2, ldc););
157 TASK(MODE(CONSTREFERENCE(F)), Rec_Initialize(F,C4,m-M2,n-N2, ldc););
158 );
159 }
160 }
161
162 int main(int argc, char** argv) {
163
164 #ifdef __FFLASFFPACK_OPENBLAS_NUM_THREADS
165 openblas_set_num_threads(__FFLASFFPACK_OPENBLAS_NUM_THREADS);
166 #endif
167
168 size_t iter = 3 ;
169 bool slab=false;
170 int q = 131071 ;
171 int m = 2000 ;
172 int n = 2000 ;
173 int r = 2000 ;
174 int v = 0;
175 int t=MAX_THREADS;
176 int NBK = -1;
177 bool par=false;
178 bool grp =true;
179 Argument as[] = {
180 { 's', "-s S", "Use the Slab recursive algorithm (LUdivine)instead of the tile recursive algorithm (PLUQ).", TYPE_BOOL , &slab },
181 { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INT , &q },
182 { 'm', "-m M", "Set the row dimension of A.", TYPE_INT , &m },
183 { 'n', "-n N", "Set the col dimension of A.", TYPE_INT , &n },
184 { 'r', "-r R", "Set the rank of matrix A.", TYPE_INT , &r },
185 { 'g', "-g yes/no", "Generic rank profile (yes) or random rank profile (no).", TYPE_BOOL , &grp },
186 { 'i', "-i I", "Set number of repetitions.", TYPE_INT , &iter },
187 { 'v', "-v V", "Set 1 if need verification of result else 0.", TYPE_INT , &v },
188 { 't', "-t T", "number of virtual threads to drive the partition.", TYPE_INT , &t },
189 { 'b', "-b B", "number of numa blocks per dimension for the numa placement", TYPE_INT , &NBK },
190 { 'p', "-p P", "whether to run or not the parallel PLUQ", TYPE_BOOL , &par },
191 END_OF_ARGUMENTS
192 };
193 FFLAS::parseArguments(argc,argv,as);
194 Field F(q);
195 if (r > std::min(m,n)){
196 std::cerr<<"Warning: rank can not be greater than min (m,n). It has been forced to min (m,n)"<<std::endl;
197 r=std::min(m,n);
198 }
199 if (!par) { t=1;NBK=1;}
200 if (NBK==-1) NBK = t;
201
202 Field::Element_ptr A, Acop;
203 A = FFLAS::fflas_new(F,m,n);
204
205 PAR_BLOCK{
206 Rec_Initialize(F, A, m, n, n);
207 // FFLAS::pfzero(F,m,n,A,m/NBK);
208 if (grp){
209 size_t * cols = FFLAS::fflas_new<size_t>(n);
210 size_t * rows = FFLAS::fflas_new<size_t>(m);
211 for (int i=0; i<n; ++i)
212 cols[i] = i;
213 for (int i=0; i<m; ++i)
214 rows[i] = i;
215 FFPACK::RandomMatrixWithRankandRPM (F, m, n ,r, A, n, rows, cols);
216 FFLAS::fflas_delete(cols);
217 FFLAS::fflas_delete(rows);
218 } else
219 FFPACK::RandomMatrixWithRankandRandomRPM (F, m, n ,r, A, n);
220 }
221 size_t R;
222 FFLAS::Timer chrono;
223 double *time=new double[iter];
224
225 enum FFLAS::FFLAS_DIAG diag = FFLAS::FflasNonUnit;
226 size_t maxP, maxQ;
227 maxP = m;
228 maxQ = n;
229
230 size_t *P = FFLAS::fflas_new<size_t>(maxP);
231 size_t *Q = FFLAS::fflas_new<size_t>(maxQ);
232
233 Acop = FFLAS::fflas_new(F,m,n);
234 FFLAS::ParSeqHelper::Parallel<FFLAS::CuttingStrategy::Recursive,
235 FFLAS::StrategyParameter::Threads> parH;
236 PARFOR1D(i,(size_t)m,parH,
237 FFLAS::fassign(F, n, A + i*n, 1, Acop + i*n, 1);
238 // for (size_t j=0; j<(size_t)n; ++j)
239 // Acop[i*n+j]= A[i*n+j];
240 );
241 for (size_t i=0;i<=iter;++i){
242
243 PARFOR1D(j,maxP,parH, P[j]=0; );
244 PARFOR1D(j,maxQ,parH, Q[j]=0; );
245 PARFOR1D(k,(size_t)m,parH,
246 FFLAS::fassign(F, n, Acop + k*n, 1, A + k*n, 1);
247 // for (size_t j=0; j<(size_t)n; ++j)
248 // F.assign( A[k*n+j] , Acop[k*n+j]) ;
249 );
250 chrono.clear();
251
252 if (i) chrono.start();
253 if (par){
254 /*
255 PAR_BLOCK{
256 parH.set_numthreads(t);
257 R = FFPACK::PLUQ(F, diag, m, n, A, n, P, Q, parH);
258 }
259 */
260 R = FFPACK::pPLUQ(F, diag, m, n, A, n, P, Q);
261 }
262 else{
263 if (slab)
264 R = FFPACK::LUdivine (F, diag, FFLAS::FflasNoTrans, m, n, A, n, P, Q);
265 else
266 R = FFPACK::PLUQ(F, diag, m, n, A, n, P, Q);
267 }
268 if (i) {chrono.stop(); time[i-1]=chrono.realtime();}
269
270 }
271 std::sort(time, time+iter);
272 double mediantime = time[iter/2];
273 delete[] time;
274 // -----------
275 // Standard output for benchmark - Alexis Breust 2014/11/14
276 #define CUBE(x) ((x)*(x)*(x))
277 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;
278 std::cout << "Time: " << mediantime
279 << " Gfops: " << gflop / mediantime;
280 FFLAS::writeCommandString(std::cout, as) << std::endl;
281
282 //verification
283 if(v)
284 verification_PLUQ(F,Acop,A,P,Q,m,n,R);
285
286 FFLAS::fflas_delete (P);
287 FFLAS::fflas_delete (Q);
288 FFLAS::fflas_delete (A);
289 FFLAS::fflas_delete (Acop);
290
291 return 0;
292 }
293
294 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
295 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s