18 using namespace Eigen;
26 #define SCALARA SCALAR 30 #define SCALARB SCALAR 46 static float fone = 1;
47 static float fzero = 0;
48 static double done = 1;
49 static double szero = 0;
50 static std::complex<float> cfone = 1;
51 static std::complex<float> cfzero = 0;
52 static std::complex<double> cdone = 1;
53 static std::complex<double> cdzero = 0;
55 static char trans =
'T';
57 static char lower =
'L';
58 static char right =
'R';
61 void blas_gemm(
const MatrixXf&
a,
const MatrixXf&
b, MatrixXf&
c)
63 int M = c.rows();
int N = c.cols();
int K =
a.cols();
64 int lda =
a.rows();
int ldb =
b.rows();
int ldc = c.rows();
67 const_cast<float*>(
a.data()),&lda,
68 const_cast<float*>(
b.data()),&ldb,&fone,
74 int M = c.rows();
int N = c.cols();
int K =
a.cols();
75 int lda =
a.rows();
int ldb =
b.rows();
int ldc = c.rows();
78 const_cast<double*>(
a.data()),&lda,
79 const_cast<double*>(
b.data()),&ldb,&done,
83 void blas_gemm(
const MatrixXcf&
a,
const MatrixXcf&
b, MatrixXcf&
c)
85 int M = c.rows();
int N = c.cols();
int K =
a.cols();
86 int lda =
a.rows();
int ldb =
b.rows();
int ldc = c.rows();
89 const_cast<float*>((
const float*)
a.data()),&lda,
90 const_cast<float*>((
const float*)
b.data()),&ldb,(
float*)&cfone,
91 (
float*)c.data(),&ldc);
94 void blas_gemm(
const MatrixXcd&
a,
const MatrixXcd&
b, MatrixXcd&
c)
96 int M = c.rows();
int N = c.cols();
int K =
a.cols();
97 int lda =
a.rows();
int ldb =
b.rows();
int ldc = c.rows();
100 const_cast<double*>((
const double*)
a.data()),&lda,
101 const_cast<double*>((
const double*)
b.data()),&ldb,(
double*)&cdone,
102 (
double*)c.data(),&ldc);
111 cr.noalias() += ar * br;
112 cr.noalias() -= ai * bi;
113 ci.noalias() += ar * bi;
114 ci.noalias() += ai * br;
119 cr.noalias() += a * br;
120 ci.noalias() += a * bi;
125 cr.noalias() += ar *
b;
126 ci.noalias() += ai *
b;
129 template<
typename A,
typename B,
typename C>
132 c.noalias() += a *
b;
135 int main(
int argc,
char ** argv)
139 std::cout <<
"L1 cache size = " << (l1>0 ? l1/1024 : -1) <<
" KB\n";
140 std::cout <<
"L2/L3 cache size = " << (l2>0 ? l2/1024 : -1) <<
" KB\n";
141 typedef internal::gebp_traits<Scalar,Scalar> Traits;
142 std::cout <<
"Register blocking = " << Traits::mr <<
" x " << Traits::nr <<
"\n";
151 int cache_size1=-1, cache_size2=
l2, cache_size3 = 0;
153 bool need_help =
false;
154 for (
int i=1;
i<argc;)
169 else if(argv[
i][1]==
'c')
172 cache_size1 = atoi(argv[
i++]);
175 cache_size2 = atoi(argv[
i++]);
177 cache_size3 = atoi(argv[
i++]);
180 else if(argv[
i][1]==
't')
183 tries = atoi(argv[
i++]);
185 else if(argv[
i][1]==
'p')
188 rep = atoi(argv[
i++]);
200 std::cout << argv[0] <<
" -s <matrix sizes> -c <cache sizes> -t <nb tries> -p <nb repeats>\n";
201 std::cout <<
" <matrix sizes> : size\n";
202 std::cout <<
" <matrix sizes> : rows columns depth\n";
206 #if EIGEN_VERSION_AT_LEAST(3,2,90) 211 A a(m,p); a.setRandom();
212 B b(p,n); b.setRandom();
213 C c(m,n); c.setOnes();
216 std::cout <<
"Matrix sizes = " << m <<
"x" << p <<
" * " << p <<
"x" << n <<
"\n";
217 std::ptrdiff_t mc(m),
nc(n), kc(p);
218 internal::computeProductBlockingSizes<Scalar,Scalar>(kc, mc,
nc);
219 std::cout <<
"blocking size (mc x kc) = " << mc <<
" x " << kc <<
"\n";
224 #if defined EIGEN_HAS_OPENMP 233 r.noalias() += a *
b;
236 c.noalias() += a *
b;
237 if(!r.isApprox(c)) std::cerr <<
"Warning, your parallel product is crap!\n\n";
239 #elif defined HAVE_BLAS 241 c.noalias() += a *
b;
243 std::cout << r - c <<
"\n";
244 std::cerr <<
"Warning, your product is crap!\n\n";
247 if(1.*m*n*p<2000.*2000*2000)
250 r.noalias() += a.cast<
Scalar>() .lazyProduct( b.cast<
Scalar>() );
252 std::cout << r - c <<
"\n";
253 std::cerr <<
"Warning, your product is crap!\n\n";
261 BENCH(tblas, tries, rep, blas_gemm(a,b,c));
272 #ifdef EIGEN_HAS_OPENMP 286 if(1.*m*n*p<30*30*30)
290 BENCH(tmt, tries, rep, c.noalias()+=a.lazyProduct(b));
298 M ar(m,p); ar.setRandom();
299 M ai(m,p); ai.setRandom();
300 M br(p,n); br.setRandom();
301 M bi(p,n); bi.setRandom();
302 M cr(m,n); cr.setRandom();
303 M ci(m,n); ci.setRandom();
312 M a(m,p); a.setRandom();
313 M br(p,n); br.setRandom();
314 M bi(p,n); bi.setRandom();
315 M cr(m,n); cr.setRandom();
316 M ci(m,n); ci.setRandom();
325 M ar(m,p); ar.setRandom();
326 M ai(m,p); ai.setRandom();
327 M b(p,n); b.setRandom();
328 M cr(m,n); cr.setRandom();
329 M ci(m,n); ci.setRandom();
void setCpuCacheSizes(std::ptrdiff_t l1, std::ptrdiff_t l2, std::ptrdiff_t l3)
Matrix< RealScalar, Dynamic, Dynamic > M
const mpreal ai(const mpreal &x, mp_rnd_t r=mpreal::get_default_rnd())
Namespace containing all symbols from the Eigen library.
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Matrix< SCALARB, Dynamic, Dynamic > B
#define EIGEN_DONT_INLINE
void omp_set_num_threads(int num_threads)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
NumTraits< Scalar >::Real RealScalar
EIGEN_DONT_INLINE void gemm(const A &a, const B &b, C &c)
Matrix< Scalar, Dynamic, Dynamic > C
double total(int TIMER=CPU_TIMER) const
int main(int argc, char **argv)
void matlab_cplx_cplx(const M &ar, const M &ai, const M &br, const M &bi, M &cr, M &ci)
void matlab_real_cplx(const M &a, const M &br, const M &bi, M &cr, M &ci)
double best(int TIMER=CPU_TIMER) const
int omp_get_max_threads(void)
#define BENCH(TIMER, TRIES, REP, CODE)
The matrix class, also used for vectors and row-vectors.
void matlab_cplx_real(const M &ar, const M &ai, const M &b, M &cr, M &ci)
int queryTopLevelCacheSize()