Debian Codegraph @

benchmarks/benchmark-fgemm.C

raw
1 /* Copyright (c) FFLAS-FFPACK
2 * Written by Clément Pernet <[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 // Please do not commit with any of these defines on - AB 2015-01-12
25 //#define __FFLASFFPACK_USE_TBB
26 //#define __FFLASFFPACK_USE_OPENMP
27 //#define __FFLASFFPACK_USE_DATAFLOW
28 //#define WINO_PARALLEL_TMPS
29 //#define __FFLASFFPACK_FORCE_SEQ
30 //#define PFGEMM_WINO_SEQ 32
31 //#define CLASSIC_SEQ
32 #define CLASSIC_HYBRID
33 //#define WINO_SEQ
34 //#define FFT_PROFILER
35 //#define PROFILE_FGEMM_MP
36 #include "fflas-ffpack/fflas-ffpack-config.h"
37 #include <iostream>
38 #include <givaro/modular-balanced.h>
39
40 #include "fflas-ffpack/config-blas.h"
41 #include "fflas-ffpack/fflas/fflas.h"
42 #include "fflas-ffpack/utils/timer.h"
43 #include "fflas-ffpack/utils/args-parser.h"
44
45 #ifdef __FFLASFFPACK_USE_KAAPI
46 #include "libkomp.h"
47 #endif
48
49
50 using namespace std;
51 using namespace FFLAS;
52
53
54 int main(int argc, char** argv) {
55
56 size_t iter = 3 ;
57 Givaro::Integer q = 131071 ;
58 size_t m = 2000 ;
59 size_t k = 2000 ;
60 size_t n = 2000 ;
61 int nbw = -1 ;
62 int p=0;
63 int t=MAX_THREADS;
64 int NBK = -1;
65
66 Argument as[] = {
67 { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INTEGER , &q },
68 { 'm', "-m M", "Set the row dimension of A.", TYPE_INT , &m },
69 { 'k', "-k K", "Set the col dimension of A.", TYPE_INT , &k },
70 { 'n', "-n N", "Set the col dimension of B.", TYPE_INT , &n },
71 { 'w', "-w N", "Set the number of winograd levels (-1 for random).", TYPE_INT , &nbw },
72 { 'i', "-i R", "Set number of repetitions.", TYPE_INT , &iter },
73 { 'p', "-p P", "0 for sequential, 1 for 2D iterative, 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 },
74 { 't', "-t T", "number of virtual threads to drive the partition.", TYPE_INT , &t },
75 { 'b', "-b B", "number of numa blocks per dimension for the numa placement", TYPE_INT , &NBK },
76 END_OF_ARGUMENTS
77 };
78
79 parseArguments(argc,argv,as);
80
81 if (NBK==-1) NBK = t;
82 // typedef Givaro::Modular<Givaro::Integer> Field;
83 // typedef Givaro::Modular<int64_t> Field;
84 // typedef Givaro::Modular<double> Field;
85 // typedef Givaro::Modular<float> Field;
86 // typedef Givaro::ModularBalanced<float> Field;
87 typedef Givaro::ModularBalanced<double> Field;
88 // typedef Givaro::ModularBalanced<int64_t> Field;
89 // typedef Givaro::Modular<Givaro::Integer> Field;
90 typedef Field::Element Element;
91
92 Field F(q);
93
94 Timer chrono, TimFreivalds;
95 double timev=0.0;
96 double *time=new double[iter];
97
98 Element * A, * B, * C;
99
100 Field::RandIter G(F);
101 A = fflas_new(F,m,k,Alignment::CACHE_PAGESIZE);
102 //#pragma omp parallel for collapse(2) schedule(runtime)
103 PAR_BLOCK { pfrand(F,G, m,k,A,m/size_t(NBK)); }
104
105 B = fflas_new(F,k,n,Alignment::CACHE_PAGESIZE);
106 //#pragma omp parallel for collapse(2) schedule(runtime)
107 PAR_BLOCK { pfrand(F,G, k,n,B,k/NBK); }
108
109 C = fflas_new(F,m,n,Alignment::CACHE_PAGESIZE);
110 //#pragma omp parallel for collapse(2) schedule(runtime)
111 PAR_BLOCK { pfzero(F, m,n,C,m/NBK); }
112
113
114 for (size_t i=0;i<=iter;++i){
115
116 chrono.clear();
117 if (p && p!=7){
118 // CuttingStrategy meth = RECURSIVE;
119 // StrategyParameter strat = THREADS;
120
121 typedef CuttingStrategy::Block block;
122 typedef CuttingStrategy::Recursive rec;
123 typedef StrategyParameter::Threads threads;
124 typedef StrategyParameter::TwoD twod;
125 typedef StrategyParameter::TwoDAdaptive twoda;
126 typedef StrategyParameter::ThreeD threed;
127 typedef StrategyParameter::ThreeDAdaptive threeda;
128 typedef StrategyParameter::ThreeDInPlace threedip;
129 PAR_BLOCK{
130 if (i) { chrono.start(); }
131
132 switch (p){
133 case 1:{
134 MMHelper<Field, MMHelperAlgo::Winograd, typename ModeTraits<Field>::value, ParSeqHelper::Parallel<block,threads> > WH(F,nbw, SPLITTER(t,block,threads));
135 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n, WH);
136 break;}
137 case 2:{
138 MMHelper<Field, MMHelperAlgo::Winograd, typename ModeTraits<Field>::value, ParSeqHelper::Parallel<rec,twod> > WH(F,nbw, SPLITTER(t,rec,twod));
139 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n, WH);
140 break;
141 }
142 case 3:{
143 MMHelper<Field, MMHelperAlgo::Winograd, typename ModeTraits<Field>::value, ParSeqHelper::Parallel<rec,twoda> > WH(F,nbw, SPLITTER(t,rec,twoda));
144 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n, WH);
145 break;
146 }
147 case 4:{
148 MMHelper<Field, MMHelperAlgo::Winograd, typename ModeTraits<Field>::value, ParSeqHelper::Parallel<rec,threedip> > WH(F,nbw, SPLITTER(t,rec,threedip));
149 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n, WH);
150 break;
151 }
152 case 5:{
153 MMHelper<Field, MMHelperAlgo::Winograd, typename ModeTraits<Field>::value, ParSeqHelper::Parallel<rec,threed> > WH(F,nbw, SPLITTER(t,rec,threed));
154 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n, WH);
155 break;
156 }
157 case 6:{
158 MMHelper<Field, MMHelperAlgo::Winograd, typename ModeTraits<Field>::value, ParSeqHelper::Parallel<rec,threeda> > WH(F,nbw, SPLITTER(t,rec,threeda));
159 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n, WH);
160 break;
161 }
162 default:{
163 MMHelper<Field, MMHelperAlgo::Winograd, typename ModeTraits<Field>::value, ParSeqHelper::Parallel<block,threads> > WH(F,nbw, SPLITTER(t,block,threads));
164 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n, WH);
165 break;
166 }
167 }
168 }
169 if (i) {chrono.stop(); time[i-1]=chrono.realtime();}
170 }else{
171 if(p==7){
172
173 int nrec = 0;
174 int dim = m;
175 // if(dim < 19000)
176 nrec--;
177 while(dim >= __FFLASFFPACK_WINOTHRESHOLD*2){
178 dim=dim/2;
179 nrec++;
180 }
181 nrec=std::max(1,nrec);
182 // std::cout<<" WINO_THREShold"<<__FFLASFFPACK_WINOTHRESHOLD<<" nrec = "<<nrec<<" dim = "<<dim<<std::endl;
183 if(nbw != -1)
184 nrec=nbw;
185 nbw=nrec;
186 if (i) chrono.start();
187 PAR_BLOCK
188 {
189 MMHelper<Field, MMHelperAlgo::WinogradPar,ModeTraits<Field>::value,ParSeqHelper::Parallel<> > WH (F, nrec, ParSeqHelper::Parallel<>(t));
190 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n,WH);
191 }
192 if (i) {chrono.stop(); time[i-1]=chrono.realtime();}
193
194
195 // MMHelper<Field, MMHelperAlgo::WinogradPar>
196 // WH (F, nbw, ParSeqHelper::Sequential());
197 // // cout<<"wino parallel"<<endl;
198 // if (i) chrono.start();
199 // PAR_BLOCK
200 // {
201 // fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n,WH);
202 // }
203 // if (i) {chrono.stop(); time+=chrono.realtime();}
204 }
205 else{
206
207 MMHelper<Field,MMHelperAlgo::Winograd>//,
208 //typename FieldTraits<Field>::value,
209 //ParSeqHelper::Sequential>
210 WH (F, nbw, ParSeqHelper::Sequential());
211 if (i) chrono.start();
212 fgemm (F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, F.zero, C,n,WH);
213 if (i) {chrono.stop(); time[i-1]=chrono.realtime();}
214 }
215 }
216
217 TimFreivalds.clear();
218 TimFreivalds.start();
219
220 bool pass = freivalds(F, FflasNoTrans, FflasNoTrans, m,n,k, F.one, A, k, B, n, C,n);
221 TimFreivalds.stop();
222 timev+=TimFreivalds.usertime();
223 if (!pass)
224 std::cout<<"FAILED"<<std::endl;
225 }
226 fflas_delete( A);
227 fflas_delete( B);
228 fflas_delete( C);
229 std::sort(time, time+iter);
230 double mediantime = time[iter/2];
231 delete[] time;
232
233 // -----------
234 // Standard output for benchmark - Alexis Breust 2014/11/14
235 std::cout << "Time: " << mediantime
236 << " Gfops: " << (2.*double(m)/1000.*double(n)/1000.*double(k)/1000.0) / mediantime;
237 writeCommandString(std::cout, as) << std::endl;
238
239 #if __FFLASFFPACK_DEBUG
240 std::cout<<"Freivalds vtime: "<<timev/(double)iter<<std::endl;
241 #endif
242
243 return 0;
244 }
245
246 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
247 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s