GeneralBlockPanelKernel.h 45.2 KB
Newer Older
LM's avatar
LM committed
1 2 3 4 5
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
//
Don Gagne's avatar
Don Gagne committed
6 7 8
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
LM's avatar
LM committed
9 10 11 12

#ifndef EIGEN_GENERAL_BLOCK_PANEL_H
#define EIGEN_GENERAL_BLOCK_PANEL_H

Don Gagne's avatar
Don Gagne committed
13 14
namespace Eigen { 
  
LM's avatar
LM committed
15 16 17 18 19
namespace internal {

template<typename _LhsScalar, typename _RhsScalar, bool _ConjLhs=false, bool _ConjRhs=false>
class gebp_traits;

Don Gagne's avatar
Don Gagne committed
20 21 22 23 24 25 26

/** \internal \returns b if a<=0, and returns a otherwise. */
inline std::ptrdiff_t manage_caching_sizes_helper(std::ptrdiff_t a, std::ptrdiff_t b)
{
  return a<=0 ? b : a;
}

LM's avatar
LM committed
27 28 29 30 31
/** \internal */
inline void manage_caching_sizes(Action action, std::ptrdiff_t* l1=0, std::ptrdiff_t* l2=0)
{
  static std::ptrdiff_t m_l1CacheSize = 0;
  static std::ptrdiff_t m_l2CacheSize = 0;
Don Gagne's avatar
Don Gagne committed
32
  if(m_l2CacheSize==0)
LM's avatar
LM committed
33
  {
Don Gagne's avatar
Don Gagne committed
34 35
    m_l1CacheSize = manage_caching_sizes_helper(queryL1CacheSize(),8 * 1024);
    m_l2CacheSize = manage_caching_sizes_helper(queryTopLevelCacheSize(),1*1024*1024);
LM's avatar
LM committed
36
  }
Don Gagne's avatar
Don Gagne committed
37
  
LM's avatar
LM committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  if(action==SetAction)
  {
    // set the cpu cache size and cache all block sizes from a global cache size in byte
    eigen_internal_assert(l1!=0 && l2!=0);
    m_l1CacheSize = *l1;
    m_l2CacheSize = *l2;
  }
  else if(action==GetAction)
  {
    eigen_internal_assert(l1!=0 && l2!=0);
    *l1 = m_l1CacheSize;
    *l2 = m_l2CacheSize;
  }
  else
  {
    eigen_internal_assert(false);
  }
}

/** \brief Computes the blocking parameters for a m x k times k x n matrix product
  *
  * \param[in,out] k Input: the third dimension of the product. Output: the blocking size along the same dimension.
  * \param[in,out] m Input: the number of rows of the left hand side. Output: the blocking size along the same dimension.
  * \param[in,out] n Input: the number of columns of the right hand side. Output: the blocking size along the same dimension.
  *
  * Given a m x k times k x n matrix product of scalar types \c LhsScalar and \c RhsScalar,
  * this function computes the blocking size parameters along the respective dimensions
  * for matrix products and related algorithms. The blocking sizes depends on various
  * parameters:
  * - the L1 and L2 cache sizes,
  * - the register level blocking sizes defined by gebp_traits,
  * - the number of scalars that fit into a packet (when vectorization is enabled).
  *
  * \sa setCpuCacheSizes */
Don Gagne's avatar
Don Gagne committed
72 73
template<typename LhsScalar, typename RhsScalar, int KcFactor, typename SizeType>
void computeProductBlockingSizes(SizeType& k, SizeType& m, SizeType& n)
LM's avatar
LM committed
74
{
Don Gagne's avatar
Don Gagne committed
75
  EIGEN_UNUSED_VARIABLE(n);
LM's avatar
LM committed
76 77 78 79 80 81 82
  // Explanations:
  // Let's recall the product algorithms form kc x nc horizontal panels B' on the rhs and
  // mc x kc blocks A' on the lhs. A' has to fit into L2 cache. Moreover, B' is processed
  // per kc x nr vertical small panels where nr is the blocking size along the n dimension
  // at the register level. For vectorization purpose, these small vertical panels are unpacked,
  // e.g., each coefficient is replicated to fit a packet. This small vertical panel has to
  // stay in L1 cache.
Don Gagne's avatar
Don Gagne committed
83
  std::ptrdiff_t l1, l2;
LM's avatar
LM committed
84 85 86 87 88 89 90 91 92 93

  typedef gebp_traits<LhsScalar,RhsScalar> Traits;
  enum {
    kdiv = KcFactor * 2 * Traits::nr
         * Traits::RhsProgress * sizeof(RhsScalar),
    mr = gebp_traits<LhsScalar,RhsScalar>::mr,
    mr_mask = (0xffffffff/mr)*mr
  };

  manage_caching_sizes(GetAction, &l1, &l2);
Don Gagne's avatar
Don Gagne committed
94 95
  k = std::min<SizeType>(k, l1/kdiv);
  SizeType _m = k>0 ? l2/(4 * sizeof(LhsScalar) * k) : 0;
LM's avatar
LM committed
96 97 98
  if(_m<m) m = _m & mr_mask;
}

Don Gagne's avatar
Don Gagne committed
99 100
template<typename LhsScalar, typename RhsScalar, typename SizeType>
inline void computeProductBlockingSizes(SizeType& k, SizeType& m, SizeType& n)
LM's avatar
LM committed
101 102 103 104 105 106 107 108 109 110 111
{
  computeProductBlockingSizes<LhsScalar,RhsScalar,1>(k, m, n);
}

#ifdef EIGEN_HAS_FUSE_CJMADD
  #define MADD(CJ,A,B,C,T)  C = CJ.pmadd(A,B,C);
#else

  // FIXME (a bit overkill maybe ?)

  template<typename CJ, typename A, typename B, typename C, typename T> struct gebp_madd_selector {
Don Gagne's avatar
Don Gagne committed
112
    EIGEN_ALWAYS_INLINE static void run(const CJ& cj, A& a, B& b, C& c, T& /*t*/)
LM's avatar
LM committed
113 114 115 116 117 118
    {
      c = cj.pmadd(a,b,c);
    }
  };

  template<typename CJ, typename T> struct gebp_madd_selector<CJ,T,T,T,T> {
Don Gagne's avatar
Don Gagne committed
119
    EIGEN_ALWAYS_INLINE static void run(const CJ& cj, T& a, T& b, T& c, T& t)
LM's avatar
LM committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    {
      t = b; t = cj.pmul(a,t); c = padd(c,t);
    }
  };

  template<typename CJ, typename A, typename B, typename C, typename T>
  EIGEN_STRONG_INLINE void gebp_madd(const CJ& cj, A& a, B& b, C& c, T& t)
  {
    gebp_madd_selector<CJ,A,B,C,T>::run(cj,a,b,c,t);
  }

  #define MADD(CJ,A,B,C,T)  gebp_madd(CJ,A,B,C,T);
//   #define MADD(CJ,A,B,C,T)  T = B; T = CJ.pmul(A,T); C = padd(C,T);
#endif

/* Vectorization logic
 *  real*real: unpack rhs to constant packets, ...
 * 
 *  cd*cd : unpack rhs to (b_r,b_r), (b_i,b_i), mul to get (a_r b_r,a_i b_r) (a_r b_i,a_i b_i),
 *          storing each res packet into two packets (2x2),
 *          at the end combine them: swap the second and addsub them 
 *  cf*cf : same but with 2x4 blocks
 *  cplx*real : unpack rhs to constant packets, ...
 *  real*cplx : load lhs as (a0,a0,a1,a1), and mul as usual
 */
template<typename _LhsScalar, typename _RhsScalar, bool _ConjLhs, bool _ConjRhs>
class gebp_traits
{
public:
  typedef _LhsScalar LhsScalar;
  typedef _RhsScalar RhsScalar;
  typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;

  enum {
    ConjLhs = _ConjLhs,
    ConjRhs = _ConjRhs,
    Vectorizable = packet_traits<LhsScalar>::Vectorizable && packet_traits<RhsScalar>::Vectorizable,
    LhsPacketSize = Vectorizable ? packet_traits<LhsScalar>::size : 1,
    RhsPacketSize = Vectorizable ? packet_traits<RhsScalar>::size : 1,
    ResPacketSize = Vectorizable ? packet_traits<ResScalar>::size : 1,
    
    NumberOfRegisters = EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS,

    // register block size along the N direction (must be either 2 or 4)
    nr = NumberOfRegisters/4,

    // register block size along the M direction (currently, this one cannot be modified)
    mr = 2 * LhsPacketSize,
    
    WorkSpaceFactor = nr * RhsPacketSize,

    LhsProgress = LhsPacketSize,
    RhsProgress = RhsPacketSize
  };

  typedef typename packet_traits<LhsScalar>::type  _LhsPacket;
  typedef typename packet_traits<RhsScalar>::type  _RhsPacket;
  typedef typename packet_traits<ResScalar>::type  _ResPacket;

  typedef typename conditional<Vectorizable,_LhsPacket,LhsScalar>::type LhsPacket;
  typedef typename conditional<Vectorizable,_RhsPacket,RhsScalar>::type RhsPacket;
  typedef typename conditional<Vectorizable,_ResPacket,ResScalar>::type ResPacket;

  typedef ResPacket AccPacket;
  
  EIGEN_STRONG_INLINE void initAcc(AccPacket& p)
  {
    p = pset1<ResPacket>(ResScalar(0));
  }

  EIGEN_STRONG_INLINE void unpackRhs(DenseIndex n, const RhsScalar* rhs, RhsScalar* b)
  {
    for(DenseIndex k=0; k<n; k++)
      pstore1<RhsPacket>(&b[k*RhsPacketSize], rhs[k]);
  }

  EIGEN_STRONG_INLINE void loadRhs(const RhsScalar* b, RhsPacket& dest) const
  {
    dest = pload<RhsPacket>(b);
  }

  EIGEN_STRONG_INLINE void loadLhs(const LhsScalar* a, LhsPacket& dest) const
  {
    dest = pload<LhsPacket>(a);
  }

  EIGEN_STRONG_INLINE void madd(const LhsPacket& a, const RhsPacket& b, AccPacket& c, AccPacket& tmp) const
  {
    tmp = b; tmp = pmul(a,tmp); c = padd(c,tmp);
  }

  EIGEN_STRONG_INLINE void acc(const AccPacket& c, const ResPacket& alpha, ResPacket& r) const
  {
    r = pmadd(c,alpha,r);
  }

protected:
//   conj_helper<LhsScalar,RhsScalar,ConjLhs,ConjRhs> cj;
//   conj_helper<LhsPacket,RhsPacket,ConjLhs,ConjRhs> pcj;
};

template<typename RealScalar, bool _ConjLhs>
class gebp_traits<std::complex<RealScalar>, RealScalar, _ConjLhs, false>
{
public:
  typedef std::complex<RealScalar> LhsScalar;
  typedef RealScalar RhsScalar;
  typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;

  enum {
    ConjLhs = _ConjLhs,
    ConjRhs = false,
    Vectorizable = packet_traits<LhsScalar>::Vectorizable && packet_traits<RhsScalar>::Vectorizable,
    LhsPacketSize = Vectorizable ? packet_traits<LhsScalar>::size : 1,
    RhsPacketSize = Vectorizable ? packet_traits<RhsScalar>::size : 1,
    ResPacketSize = Vectorizable ? packet_traits<ResScalar>::size : 1,
    
    NumberOfRegisters = EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS,
    nr = NumberOfRegisters/4,
    mr = 2 * LhsPacketSize,
    WorkSpaceFactor = nr*RhsPacketSize,

    LhsProgress = LhsPacketSize,
    RhsProgress = RhsPacketSize
  };

  typedef typename packet_traits<LhsScalar>::type  _LhsPacket;
  typedef typename packet_traits<RhsScalar>::type  _RhsPacket;
  typedef typename packet_traits<ResScalar>::type  _ResPacket;

  typedef typename conditional<Vectorizable,_LhsPacket,LhsScalar>::type LhsPacket;
  typedef typename conditional<Vectorizable,_RhsPacket,RhsScalar>::type RhsPacket;
  typedef typename conditional<Vectorizable,_ResPacket,ResScalar>::type ResPacket;

  typedef ResPacket AccPacket;

  EIGEN_STRONG_INLINE void initAcc(AccPacket& p)
  {
    p = pset1<ResPacket>(ResScalar(0));
  }

  EIGEN_STRONG_INLINE void unpackRhs(DenseIndex n, const RhsScalar* rhs, RhsScalar* b)
  {
    for(DenseIndex k=0; k<n; k++)
      pstore1<RhsPacket>(&b[k*RhsPacketSize], rhs[k]);
  }

  EIGEN_STRONG_INLINE void loadRhs(const RhsScalar* b, RhsPacket& dest) const
  {
    dest = pload<RhsPacket>(b);
  }

  EIGEN_STRONG_INLINE void loadLhs(const LhsScalar* a, LhsPacket& dest) const
  {
    dest = pload<LhsPacket>(a);
  }

  EIGEN_STRONG_INLINE void madd(const LhsPacket& a, const RhsPacket& b, AccPacket& c, RhsPacket& tmp) const
  {
    madd_impl(a, b, c, tmp, typename conditional<Vectorizable,true_type,false_type>::type());
  }

  EIGEN_STRONG_INLINE void madd_impl(const LhsPacket& a, const RhsPacket& b, AccPacket& c, RhsPacket& tmp, const true_type&) const
  {
    tmp = b; tmp = pmul(a.v,tmp); c.v = padd(c.v,tmp);
  }

  EIGEN_STRONG_INLINE void madd_impl(const LhsScalar& a, const RhsScalar& b, ResScalar& c, RhsScalar& /*tmp*/, const false_type&) const
  {
    c += a * b;
  }

  EIGEN_STRONG_INLINE void acc(const AccPacket& c, const ResPacket& alpha, ResPacket& r) const
  {
    r = cj.pmadd(c,alpha,r);
  }

protected:
  conj_helper<ResPacket,ResPacket,ConjLhs,false> cj;
};

template<typename RealScalar, bool _ConjLhs, bool _ConjRhs>
class gebp_traits<std::complex<RealScalar>, std::complex<RealScalar>, _ConjLhs, _ConjRhs >
{
public:
  typedef std::complex<RealScalar>  Scalar;
  typedef std::complex<RealScalar>  LhsScalar;
  typedef std::complex<RealScalar>  RhsScalar;
  typedef std::complex<RealScalar>  ResScalar;
  
  enum {
    ConjLhs = _ConjLhs,
    ConjRhs = _ConjRhs,
    Vectorizable = packet_traits<RealScalar>::Vectorizable
                && packet_traits<Scalar>::Vectorizable,
    RealPacketSize  = Vectorizable ? packet_traits<RealScalar>::size : 1,
    ResPacketSize   = Vectorizable ? packet_traits<ResScalar>::size : 1,
    
    nr = 2,
    mr = 2 * ResPacketSize,
    WorkSpaceFactor = Vectorizable ? 2*nr*RealPacketSize : nr,

    LhsProgress = ResPacketSize,
    RhsProgress = Vectorizable ? 2*ResPacketSize : 1
  };
  
  typedef typename packet_traits<RealScalar>::type RealPacket;
  typedef typename packet_traits<Scalar>::type     ScalarPacket;
  struct DoublePacket
  {
    RealPacket first;
    RealPacket second;
  };

  typedef typename conditional<Vectorizable,RealPacket,  Scalar>::type LhsPacket;
  typedef typename conditional<Vectorizable,DoublePacket,Scalar>::type RhsPacket;
  typedef typename conditional<Vectorizable,ScalarPacket,Scalar>::type ResPacket;
  typedef typename conditional<Vectorizable,DoublePacket,Scalar>::type AccPacket;
  
  EIGEN_STRONG_INLINE void initAcc(Scalar& p) { p = Scalar(0); }

  EIGEN_STRONG_INLINE void initAcc(DoublePacket& p)
  {
    p.first   = pset1<RealPacket>(RealScalar(0));
    p.second  = pset1<RealPacket>(RealScalar(0));
  }

  /* Unpack the rhs coeff such that each complex coefficient is spread into
   * two packects containing respectively the real and imaginary coefficient
   * duplicated as many time as needed: (x+iy) => [x, ..., x] [y, ..., y]
   */
  EIGEN_STRONG_INLINE void unpackRhs(DenseIndex n, const Scalar* rhs, Scalar* b)
  {
    for(DenseIndex k=0; k<n; k++)
    {
      if(Vectorizable)
      {
        pstore1<RealPacket>((RealScalar*)&b[k*ResPacketSize*2+0],             real(rhs[k]));
        pstore1<RealPacket>((RealScalar*)&b[k*ResPacketSize*2+ResPacketSize], imag(rhs[k]));
      }
      else
        b[k] = rhs[k];
    }
  }

  EIGEN_STRONG_INLINE void loadRhs(const RhsScalar* b, ResPacket& dest) const { dest = *b; }

  EIGEN_STRONG_INLINE void loadRhs(const RhsScalar* b, DoublePacket& dest) const
  {
    dest.first  = pload<RealPacket>((const RealScalar*)b);
    dest.second = pload<RealPacket>((const RealScalar*)(b+ResPacketSize));
  }

  // nothing special here
  EIGEN_STRONG_INLINE void loadLhs(const LhsScalar* a, LhsPacket& dest) const
  {
    dest = pload<LhsPacket>((const typename unpacket_traits<LhsPacket>::type*)(a));
  }

  EIGEN_STRONG_INLINE void madd(const LhsPacket& a, const RhsPacket& b, DoublePacket& c, RhsPacket& /*tmp*/) const
  {
    c.first   = padd(pmul(a,b.first), c.first);
    c.second  = padd(pmul(a,b.second),c.second);
  }

  EIGEN_STRONG_INLINE void madd(const LhsPacket& a, const RhsPacket& b, ResPacket& c, RhsPacket& /*tmp*/) const
  {
    c = cj.pmadd(a,b,c);
  }
  
  EIGEN_STRONG_INLINE void acc(const Scalar& c, const Scalar& alpha, Scalar& r) const { r += alpha * c; }
  
  EIGEN_STRONG_INLINE void acc(const DoublePacket& c, const ResPacket& alpha, ResPacket& r) const
  {
    // assemble c
    ResPacket tmp;
    if((!ConjLhs)&&(!ConjRhs))
    {
      tmp = pcplxflip(pconj(ResPacket(c.second)));
      tmp = padd(ResPacket(c.first),tmp);
    }
    else if((!ConjLhs)&&(ConjRhs))
    {
      tmp = pconj(pcplxflip(ResPacket(c.second)));
      tmp = padd(ResPacket(c.first),tmp);
    }
    else if((ConjLhs)&&(!ConjRhs))
    {
      tmp = pcplxflip(ResPacket(c.second));
      tmp = padd(pconj(ResPacket(c.first)),tmp);
    }
    else if((ConjLhs)&&(ConjRhs))
    {
      tmp = pcplxflip(ResPacket(c.second));
      tmp = psub(pconj(ResPacket(c.first)),tmp);
    }
    
    r = pmadd(tmp,alpha,r);
  }

protected:
  conj_helper<LhsScalar,RhsScalar,ConjLhs,ConjRhs> cj;
};

template<typename RealScalar, bool _ConjRhs>
class gebp_traits<RealScalar, std::complex<RealScalar>, false, _ConjRhs >
{
public:
  typedef std::complex<RealScalar>  Scalar;
  typedef RealScalar  LhsScalar;
  typedef Scalar      RhsScalar;
  typedef Scalar      ResScalar;

  enum {
    ConjLhs = false,
    ConjRhs = _ConjRhs,
    Vectorizable = packet_traits<RealScalar>::Vectorizable
                && packet_traits<Scalar>::Vectorizable,
    LhsPacketSize = Vectorizable ? packet_traits<LhsScalar>::size : 1,
    RhsPacketSize = Vectorizable ? packet_traits<RhsScalar>::size : 1,
    ResPacketSize = Vectorizable ? packet_traits<ResScalar>::size : 1,
    
    NumberOfRegisters = EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS,
    nr = 4,
    mr = 2*ResPacketSize,
    WorkSpaceFactor = nr*RhsPacketSize,

    LhsProgress = ResPacketSize,
    RhsProgress = ResPacketSize
  };

  typedef typename packet_traits<LhsScalar>::type  _LhsPacket;
  typedef typename packet_traits<RhsScalar>::type  _RhsPacket;
  typedef typename packet_traits<ResScalar>::type  _ResPacket;

  typedef typename conditional<Vectorizable,_LhsPacket,LhsScalar>::type LhsPacket;
  typedef typename conditional<Vectorizable,_RhsPacket,RhsScalar>::type RhsPacket;
  typedef typename conditional<Vectorizable,_ResPacket,ResScalar>::type ResPacket;

  typedef ResPacket AccPacket;

  EIGEN_STRONG_INLINE void initAcc(AccPacket& p)
  {
    p = pset1<ResPacket>(ResScalar(0));
  }

  EIGEN_STRONG_INLINE void unpackRhs(DenseIndex n, const RhsScalar* rhs, RhsScalar* b)
  {
    for(DenseIndex k=0; k<n; k++)
      pstore1<RhsPacket>(&b[k*RhsPacketSize], rhs[k]);
  }

  EIGEN_STRONG_INLINE void loadRhs(const RhsScalar* b, RhsPacket& dest) const
  {
    dest = pload<RhsPacket>(b);
  }

  EIGEN_STRONG_INLINE void loadLhs(const LhsScalar* a, LhsPacket& dest) const
  {
    dest = ploaddup<LhsPacket>(a);
  }

  EIGEN_STRONG_INLINE void madd(const LhsPacket& a, const RhsPacket& b, AccPacket& c, RhsPacket& tmp) const
  {
    madd_impl(a, b, c, tmp, typename conditional<Vectorizable,true_type,false_type>::type());
  }

  EIGEN_STRONG_INLINE void madd_impl(const LhsPacket& a, const RhsPacket& b, AccPacket& c, RhsPacket& tmp, const true_type&) const
  {
    tmp = b; tmp.v = pmul(a,tmp.v); c = padd(c,tmp);
  }

  EIGEN_STRONG_INLINE void madd_impl(const LhsScalar& a, const RhsScalar& b, ResScalar& c, RhsScalar& /*tmp*/, const false_type&) const
  {
    c += a * b;
  }

  EIGEN_STRONG_INLINE void acc(const AccPacket& c, const ResPacket& alpha, ResPacket& r) const
  {
    r = cj.pmadd(alpha,c,r);
  }

protected:
  conj_helper<ResPacket,ResPacket,false,ConjRhs> cj;
};

/* optimized GEneral packed Block * packed Panel product kernel
 *
 * Mixing type logic: C += A * B
 *  |  A  |  B  | comments
 *  |real |cplx | no vectorization yet, would require to pack A with duplication
 *  |cplx |real | easy vectorization
 */
template<typename LhsScalar, typename RhsScalar, typename Index, int mr, int nr, bool ConjugateLhs, bool ConjugateRhs>
struct gebp_kernel
{
  typedef gebp_traits<LhsScalar,RhsScalar,ConjugateLhs,ConjugateRhs> Traits;
  typedef typename Traits::ResScalar ResScalar;
  typedef typename Traits::LhsPacket LhsPacket;
  typedef typename Traits::RhsPacket RhsPacket;
  typedef typename Traits::ResPacket ResPacket;
  typedef typename Traits::AccPacket AccPacket;

  enum {
    Vectorizable  = Traits::Vectorizable,
    LhsProgress   = Traits::LhsProgress,
    RhsProgress   = Traits::RhsProgress,
    ResPacketSize = Traits::ResPacketSize
  };

Don Gagne's avatar
Don Gagne committed
530
  EIGEN_DONT_INLINE
LM's avatar
LM committed
531
  void operator()(ResScalar* res, Index resStride, const LhsScalar* blockA, const RhsScalar* blockB, Index rows, Index depth, Index cols, ResScalar alpha,
Don Gagne's avatar
Don Gagne committed
532 533 534 535 536 537 538 539
                  Index strideA=-1, Index strideB=-1, Index offsetA=0, Index offsetB=0, RhsScalar* unpackedB=0);
};

template<typename LhsScalar, typename RhsScalar, typename Index, int mr, int nr, bool ConjugateLhs, bool ConjugateRhs>
EIGEN_DONT_INLINE
void gebp_kernel<LhsScalar,RhsScalar,Index,mr,nr,ConjugateLhs,ConjugateRhs>
  ::operator()(ResScalar* res, Index resStride, const LhsScalar* blockA, const RhsScalar* blockB, Index rows, Index depth, Index cols, ResScalar alpha,
               Index strideA, Index strideB, Index offsetA, Index offsetB, RhsScalar* unpackedB)
LM's avatar
LM committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  {
    Traits traits;
    
    if(strideA==-1) strideA = depth;
    if(strideB==-1) strideB = depth;
    conj_helper<LhsScalar,RhsScalar,ConjugateLhs,ConjugateRhs> cj;
//     conj_helper<LhsPacket,RhsPacket,ConjugateLhs,ConjugateRhs> pcj;
    Index packet_cols = (cols/nr) * nr;
    const Index peeled_mc = (rows/mr)*mr;
    // FIXME:
    const Index peeled_mc2 = peeled_mc + (rows-peeled_mc >= LhsProgress ? LhsProgress : 0);
    const Index peeled_kc = (depth/4)*4;

    if(unpackedB==0)
      unpackedB = const_cast<RhsScalar*>(blockB - strideB * nr * RhsProgress);

    // loops on each micro vertical panel of rhs (depth x nr)
    for(Index j2=0; j2<packet_cols; j2+=nr)
    {
      traits.unpackRhs(depth*nr,&blockB[j2*strideB+offsetB*nr],unpackedB); 

      // loops on each largest micro horizontal panel of lhs (mr x depth)
      // => we select a mr x nr micro block of res which is entirely
      //    stored into mr/packet_size x nr registers.
      for(Index i=0; i<peeled_mc; i+=mr)
      {
        const LhsScalar* blA = &blockA[i*strideA+offsetA*mr];
        prefetch(&blA[0]);

        // gets res block as register
        AccPacket C0, C1, C2, C3, C4, C5, C6, C7;
                  traits.initAcc(C0);
                  traits.initAcc(C1);
        if(nr==4) traits.initAcc(C2);
        if(nr==4) traits.initAcc(C3);
                  traits.initAcc(C4);
                  traits.initAcc(C5);
        if(nr==4) traits.initAcc(C6);
        if(nr==4) traits.initAcc(C7);

        ResScalar* r0 = &res[(j2+0)*resStride + i];
        ResScalar* r1 = r0 + resStride;
        ResScalar* r2 = r1 + resStride;
        ResScalar* r3 = r2 + resStride;

        prefetch(r0+16);
        prefetch(r1+16);
        prefetch(r2+16);
        prefetch(r3+16);

        // performs "inner" product
        // TODO let's check wether the folowing peeled loop could not be
        //      optimized via optimal prefetching from one loop to the other
        const RhsScalar* blB = unpackedB;
        for(Index k=0; k<peeled_kc; k+=4)
        {
          if(nr==2)
          {
            LhsPacket A0, A1;
Don Gagne's avatar
Don Gagne committed
599
            RhsPacket B_0;
LM's avatar
LM committed
600 601 602 603 604
            RhsPacket T0;
            
EIGEN_ASM_COMMENT("mybegin2");
            traits.loadLhs(&blA[0*LhsProgress], A0);
            traits.loadLhs(&blA[1*LhsProgress], A1);
Don Gagne's avatar
Don Gagne committed
605 606 607 608 609 610
            traits.loadRhs(&blB[0*RhsProgress], B_0);
            traits.madd(A0,B_0,C0,T0);
            traits.madd(A1,B_0,C4,B_0);
            traits.loadRhs(&blB[1*RhsProgress], B_0);
            traits.madd(A0,B_0,C1,T0);
            traits.madd(A1,B_0,C5,B_0);
LM's avatar
LM committed
611 612 613

            traits.loadLhs(&blA[2*LhsProgress], A0);
            traits.loadLhs(&blA[3*LhsProgress], A1);
Don Gagne's avatar
Don Gagne committed
614 615 616 617 618 619
            traits.loadRhs(&blB[2*RhsProgress], B_0);
            traits.madd(A0,B_0,C0,T0);
            traits.madd(A1,B_0,C4,B_0);
            traits.loadRhs(&blB[3*RhsProgress], B_0);
            traits.madd(A0,B_0,C1,T0);
            traits.madd(A1,B_0,C5,B_0);
LM's avatar
LM committed
620 621 622

            traits.loadLhs(&blA[4*LhsProgress], A0);
            traits.loadLhs(&blA[5*LhsProgress], A1);
Don Gagne's avatar
Don Gagne committed
623 624 625 626 627 628
            traits.loadRhs(&blB[4*RhsProgress], B_0);
            traits.madd(A0,B_0,C0,T0);
            traits.madd(A1,B_0,C4,B_0);
            traits.loadRhs(&blB[5*RhsProgress], B_0);
            traits.madd(A0,B_0,C1,T0);
            traits.madd(A1,B_0,C5,B_0);
LM's avatar
LM committed
629 630 631

            traits.loadLhs(&blA[6*LhsProgress], A0);
            traits.loadLhs(&blA[7*LhsProgress], A1);
Don Gagne's avatar
Don Gagne committed
632 633 634 635 636 637
            traits.loadRhs(&blB[6*RhsProgress], B_0);
            traits.madd(A0,B_0,C0,T0);
            traits.madd(A1,B_0,C4,B_0);
            traits.loadRhs(&blB[7*RhsProgress], B_0);
            traits.madd(A0,B_0,C1,T0);
            traits.madd(A1,B_0,C5,B_0);
LM's avatar
LM committed
638 639 640 641 642 643
EIGEN_ASM_COMMENT("myend");
          }
          else
          {
EIGEN_ASM_COMMENT("mybegin4");
            LhsPacket A0, A1;
Don Gagne's avatar
Don Gagne committed
644
            RhsPacket B_0, B1, B2, B3;
LM's avatar
LM committed
645 646 647 648
            RhsPacket T0;
            
            traits.loadLhs(&blA[0*LhsProgress], A0);
            traits.loadLhs(&blA[1*LhsProgress], A1);
Don Gagne's avatar
Don Gagne committed
649
            traits.loadRhs(&blB[0*RhsProgress], B_0);
LM's avatar
LM committed
650 651
            traits.loadRhs(&blB[1*RhsProgress], B1);

Don Gagne's avatar
Don Gagne committed
652
            traits.madd(A0,B_0,C0,T0);
LM's avatar
LM committed
653
            traits.loadRhs(&blB[2*RhsProgress], B2);
Don Gagne's avatar
Don Gagne committed
654
            traits.madd(A1,B_0,C4,B_0);
LM's avatar
LM committed
655
            traits.loadRhs(&blB[3*RhsProgress], B3);
Don Gagne's avatar
Don Gagne committed
656
            traits.loadRhs(&blB[4*RhsProgress], B_0);
LM's avatar
LM committed
657 658 659 660 661 662 663 664 665 666 667
            traits.madd(A0,B1,C1,T0);
            traits.madd(A1,B1,C5,B1);
            traits.loadRhs(&blB[5*RhsProgress], B1);
            traits.madd(A0,B2,C2,T0);
            traits.madd(A1,B2,C6,B2);
            traits.loadRhs(&blB[6*RhsProgress], B2);
            traits.madd(A0,B3,C3,T0);
            traits.loadLhs(&blA[2*LhsProgress], A0);
            traits.madd(A1,B3,C7,B3);
            traits.loadLhs(&blA[3*LhsProgress], A1);
            traits.loadRhs(&blB[7*RhsProgress], B3);
Don Gagne's avatar
Don Gagne committed
668 669 670
            traits.madd(A0,B_0,C0,T0);
            traits.madd(A1,B_0,C4,B_0);
            traits.loadRhs(&blB[8*RhsProgress], B_0);
LM's avatar
LM committed
671 672 673 674 675 676 677 678 679 680 681 682
            traits.madd(A0,B1,C1,T0);
            traits.madd(A1,B1,C5,B1);
            traits.loadRhs(&blB[9*RhsProgress], B1);
            traits.madd(A0,B2,C2,T0);
            traits.madd(A1,B2,C6,B2);
            traits.loadRhs(&blB[10*RhsProgress], B2);
            traits.madd(A0,B3,C3,T0);
            traits.loadLhs(&blA[4*LhsProgress], A0);
            traits.madd(A1,B3,C7,B3);
            traits.loadLhs(&blA[5*LhsProgress], A1);
            traits.loadRhs(&blB[11*RhsProgress], B3);

Don Gagne's avatar
Don Gagne committed
683 684 685
            traits.madd(A0,B_0,C0,T0);
            traits.madd(A1,B_0,C4,B_0);
            traits.loadRhs(&blB[12*RhsProgress], B_0);
LM's avatar
LM committed
686 687 688 689 690 691 692 693 694 695 696
            traits.madd(A0,B1,C1,T0);
            traits.madd(A1,B1,C5,B1);
            traits.loadRhs(&blB[13*RhsProgress], B1);
            traits.madd(A0,B2,C2,T0);
            traits.madd(A1,B2,C6,B2);
            traits.loadRhs(&blB[14*RhsProgress], B2);
            traits.madd(A0,B3,C3,T0);
            traits.loadLhs(&blA[6*LhsProgress], A0);
            traits.madd(A1,B3,C7,B3);
            traits.loadLhs(&blA[7*LhsProgress], A1);
            traits.loadRhs(&blB[15*RhsProgress], B3);
Don Gagne's avatar
Don Gagne committed
697 698
            traits.madd(A0,B_0,C0,T0);
            traits.madd(A1,B_0,C4,B_0);
LM's avatar
LM committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
            traits.madd(A0,B1,C1,T0);
            traits.madd(A1,B1,C5,B1);
            traits.madd(A0,B2,C2,T0);
            traits.madd(A1,B2,C6,B2);
            traits.madd(A0,B3,C3,T0);
            traits.madd(A1,B3,C7,B3);
          }

          blB += 4*nr*RhsProgress;
          blA += 4*mr;
        }
        // process remaining peeled loop
        for(Index k=peeled_kc; k<depth; k++)
        {
          if(nr==2)
          {
            LhsPacket A0, A1;
Don Gagne's avatar
Don Gagne committed
716
            RhsPacket B_0;
LM's avatar
LM committed
717 718 719 720
            RhsPacket T0;

            traits.loadLhs(&blA[0*LhsProgress], A0);
            traits.loadLhs(&blA[1*LhsProgress], A1);
Don Gagne's avatar
Don Gagne committed
721 722 723 724 725 726
            traits.loadRhs(&blB[0*RhsProgress], B_0);
            traits.madd(A0,B_0,C0,T0);
            traits.madd(A1,B_0,C4,B_0);
            traits.loadRhs(&blB[1*RhsProgress], B_0);
            traits.madd(A0,B_0,C1,T0);
            traits.madd(A1,B_0,C5,B_0);
LM's avatar
LM committed
727 728 729 730
          }
          else
          {
            LhsPacket A0, A1;
Don Gagne's avatar
Don Gagne committed
731
            RhsPacket B_0, B1, B2, B3;
LM's avatar
LM committed
732 733 734 735
            RhsPacket T0;

            traits.loadLhs(&blA[0*LhsProgress], A0);
            traits.loadLhs(&blA[1*LhsProgress], A1);
Don Gagne's avatar
Don Gagne committed
736
            traits.loadRhs(&blB[0*RhsProgress], B_0);
LM's avatar
LM committed
737 738
            traits.loadRhs(&blB[1*RhsProgress], B1);

Don Gagne's avatar
Don Gagne committed
739
            traits.madd(A0,B_0,C0,T0);
LM's avatar
LM committed
740
            traits.loadRhs(&blB[2*RhsProgress], B2);
Don Gagne's avatar
Don Gagne committed
741
            traits.madd(A1,B_0,C4,B_0);
LM's avatar
LM committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
            traits.loadRhs(&blB[3*RhsProgress], B3);
            traits.madd(A0,B1,C1,T0);
            traits.madd(A1,B1,C5,B1);
            traits.madd(A0,B2,C2,T0);
            traits.madd(A1,B2,C6,B2);
            traits.madd(A0,B3,C3,T0);
            traits.madd(A1,B3,C7,B3);
          }

          blB += nr*RhsProgress;
          blA += mr;
        }

        if(nr==4)
        {
          ResPacket R0, R1, R2, R3, R4, R5, R6;
          ResPacket alphav = pset1<ResPacket>(alpha);

          R0 = ploadu<ResPacket>(r0);
          R1 = ploadu<ResPacket>(r1);
          R2 = ploadu<ResPacket>(r2);
          R3 = ploadu<ResPacket>(r3);
          R4 = ploadu<ResPacket>(r0 + ResPacketSize);
          R5 = ploadu<ResPacket>(r1 + ResPacketSize);
          R6 = ploadu<ResPacket>(r2 + ResPacketSize);
          traits.acc(C0, alphav, R0);
          pstoreu(r0, R0);
          R0 = ploadu<ResPacket>(r3 + ResPacketSize);

          traits.acc(C1, alphav, R1);
          traits.acc(C2, alphav, R2);
          traits.acc(C3, alphav, R3);
          traits.acc(C4, alphav, R4);
          traits.acc(C5, alphav, R5);
          traits.acc(C6, alphav, R6);
          traits.acc(C7, alphav, R0);
          
          pstoreu(r1, R1);
          pstoreu(r2, R2);
          pstoreu(r3, R3);
          pstoreu(r0 + ResPacketSize, R4);
          pstoreu(r1 + ResPacketSize, R5);
          pstoreu(r2 + ResPacketSize, R6);
          pstoreu(r3 + ResPacketSize, R0);
        }
        else
        {
          ResPacket R0, R1, R4;
          ResPacket alphav = pset1<ResPacket>(alpha);

          R0 = ploadu<ResPacket>(r0);
          R1 = ploadu<ResPacket>(r1);
          R4 = ploadu<ResPacket>(r0 + ResPacketSize);
          traits.acc(C0, alphav, R0);
          pstoreu(r0, R0);
          R0 = ploadu<ResPacket>(r1 + ResPacketSize);
          traits.acc(C1, alphav, R1);
          traits.acc(C4, alphav, R4);
          traits.acc(C5, alphav, R0);
          pstoreu(r1, R1);
          pstoreu(r0 + ResPacketSize, R4);
          pstoreu(r1 + ResPacketSize, R0);
        }
        
      }
      
      if(rows-peeled_mc>=LhsProgress)
      {
        Index i = peeled_mc;
        const LhsScalar* blA = &blockA[i*strideA+offsetA*LhsProgress];
        prefetch(&blA[0]);

        // gets res block as register
        AccPacket C0, C1, C2, C3;
                  traits.initAcc(C0);
                  traits.initAcc(C1);
        if(nr==4) traits.initAcc(C2);
        if(nr==4) traits.initAcc(C3);

        // performs "inner" product
        const RhsScalar* blB = unpackedB;
        for(Index k=0; k<peeled_kc; k+=4)
        {
          if(nr==2)
          {
            LhsPacket A0;
Don Gagne's avatar
Don Gagne committed
828
            RhsPacket B_0, B1;
LM's avatar
LM committed
829 830

            traits.loadLhs(&blA[0*LhsProgress], A0);
Don Gagne's avatar
Don Gagne committed
831
            traits.loadRhs(&blB[0*RhsProgress], B_0);
LM's avatar
LM committed
832
            traits.loadRhs(&blB[1*RhsProgress], B1);
Don Gagne's avatar
Don Gagne committed
833 834
            traits.madd(A0,B_0,C0,B_0);
            traits.loadRhs(&blB[2*RhsProgress], B_0);
LM's avatar
LM committed
835 836 837
            traits.madd(A0,B1,C1,B1);
            traits.loadLhs(&blA[1*LhsProgress], A0);
            traits.loadRhs(&blB[3*RhsProgress], B1);
Don Gagne's avatar
Don Gagne committed
838 839
            traits.madd(A0,B_0,C0,B_0);
            traits.loadRhs(&blB[4*RhsProgress], B_0);
LM's avatar
LM committed
840 841 842
            traits.madd(A0,B1,C1,B1);
            traits.loadLhs(&blA[2*LhsProgress], A0);
            traits.loadRhs(&blB[5*RhsProgress], B1);
Don Gagne's avatar
Don Gagne committed
843 844
            traits.madd(A0,B_0,C0,B_0);
            traits.loadRhs(&blB[6*RhsProgress], B_0);
LM's avatar
LM committed
845 846 847
            traits.madd(A0,B1,C1,B1);
            traits.loadLhs(&blA[3*LhsProgress], A0);
            traits.loadRhs(&blB[7*RhsProgress], B1);
Don Gagne's avatar
Don Gagne committed
848
            traits.madd(A0,B_0,C0,B_0);
LM's avatar
LM committed
849 850 851 852 853
            traits.madd(A0,B1,C1,B1);
          }
          else
          {
            LhsPacket A0;
Don Gagne's avatar
Don Gagne committed
854
            RhsPacket B_0, B1, B2, B3;
LM's avatar
LM committed
855 856

            traits.loadLhs(&blA[0*LhsProgress], A0);
Don Gagne's avatar
Don Gagne committed
857
            traits.loadRhs(&blB[0*RhsProgress], B_0);
LM's avatar
LM committed
858 859
            traits.loadRhs(&blB[1*RhsProgress], B1);

Don Gagne's avatar
Don Gagne committed
860
            traits.madd(A0,B_0,C0,B_0);
LM's avatar
LM committed
861 862
            traits.loadRhs(&blB[2*RhsProgress], B2);
            traits.loadRhs(&blB[3*RhsProgress], B3);
Don Gagne's avatar
Don Gagne committed
863
            traits.loadRhs(&blB[4*RhsProgress], B_0);
LM's avatar
LM committed
864 865 866 867 868 869 870
            traits.madd(A0,B1,C1,B1);
            traits.loadRhs(&blB[5*RhsProgress], B1);
            traits.madd(A0,B2,C2,B2);
            traits.loadRhs(&blB[6*RhsProgress], B2);
            traits.madd(A0,B3,C3,B3);
            traits.loadLhs(&blA[1*LhsProgress], A0);
            traits.loadRhs(&blB[7*RhsProgress], B3);
Don Gagne's avatar
Don Gagne committed
871 872
            traits.madd(A0,B_0,C0,B_0);
            traits.loadRhs(&blB[8*RhsProgress], B_0);
LM's avatar
LM committed
873 874 875 876 877 878 879 880
            traits.madd(A0,B1,C1,B1);
            traits.loadRhs(&blB[9*RhsProgress], B1);
            traits.madd(A0,B2,C2,B2);
            traits.loadRhs(&blB[10*RhsProgress], B2);
            traits.madd(A0,B3,C3,B3);
            traits.loadLhs(&blA[2*LhsProgress], A0);
            traits.loadRhs(&blB[11*RhsProgress], B3);

Don Gagne's avatar
Don Gagne committed
881 882
            traits.madd(A0,B_0,C0,B_0);
            traits.loadRhs(&blB[12*RhsProgress], B_0);
LM's avatar
LM committed
883 884 885 886 887 888 889 890
            traits.madd(A0,B1,C1,B1);
            traits.loadRhs(&blB[13*RhsProgress], B1);
            traits.madd(A0,B2,C2,B2);
            traits.loadRhs(&blB[14*RhsProgress], B2);
            traits.madd(A0,B3,C3,B3);

            traits.loadLhs(&blA[3*LhsProgress], A0);
            traits.loadRhs(&blB[15*RhsProgress], B3);
Don Gagne's avatar
Don Gagne committed
891
            traits.madd(A0,B_0,C0,B_0);
LM's avatar
LM committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905
            traits.madd(A0,B1,C1,B1);
            traits.madd(A0,B2,C2,B2);
            traits.madd(A0,B3,C3,B3);
          }

          blB += nr*4*RhsProgress;
          blA += 4*LhsProgress;
        }
        // process remaining peeled loop
        for(Index k=peeled_kc; k<depth; k++)
        {
          if(nr==2)
          {
            LhsPacket A0;
Don Gagne's avatar
Don Gagne committed
906
            RhsPacket B_0, B1;
LM's avatar
LM committed
907 908

            traits.loadLhs(&blA[0*LhsProgress], A0);
Don Gagne's avatar
Don Gagne committed
909
            traits.loadRhs(&blB[0*RhsProgress], B_0);
LM's avatar
LM committed
910
            traits.loadRhs(&blB[1*RhsProgress], B1);
Don Gagne's avatar
Don Gagne committed
911
            traits.madd(A0,B_0,C0,B_0);
LM's avatar
LM committed
912 913 914 915 916
            traits.madd(A0,B1,C1,B1);
          }
          else
          {
            LhsPacket A0;
Don Gagne's avatar
Don Gagne committed
917
            RhsPacket B_0, B1, B2, B3;
LM's avatar
LM committed
918 919

            traits.loadLhs(&blA[0*LhsProgress], A0);
Don Gagne's avatar
Don Gagne committed
920
            traits.loadRhs(&blB[0*RhsProgress], B_0);
LM's avatar
LM committed
921 922 923 924
            traits.loadRhs(&blB[1*RhsProgress], B1);
            traits.loadRhs(&blB[2*RhsProgress], B2);
            traits.loadRhs(&blB[3*RhsProgress], B3);

Don Gagne's avatar
Don Gagne committed
925
            traits.madd(A0,B_0,C0,B_0);
LM's avatar
LM committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
            traits.madd(A0,B1,C1,B1);
            traits.madd(A0,B2,C2,B2);
            traits.madd(A0,B3,C3,B3);
          }

          blB += nr*RhsProgress;
          blA += LhsProgress;
        }

        ResPacket R0, R1, R2, R3;
        ResPacket alphav = pset1<ResPacket>(alpha);

        ResScalar* r0 = &res[(j2+0)*resStride + i];
        ResScalar* r1 = r0 + resStride;
        ResScalar* r2 = r1 + resStride;
        ResScalar* r3 = r2 + resStride;

                  R0 = ploadu<ResPacket>(r0);
                  R1 = ploadu<ResPacket>(r1);
        if(nr==4) R2 = ploadu<ResPacket>(r2);
        if(nr==4) R3 = ploadu<ResPacket>(r3);

                  traits.acc(C0, alphav, R0);
                  traits.acc(C1, alphav, R1);
        if(nr==4) traits.acc(C2, alphav, R2);
        if(nr==4) traits.acc(C3, alphav, R3);

                  pstoreu(r0, R0);
                  pstoreu(r1, R1);
        if(nr==4) pstoreu(r2, R2);
        if(nr==4) pstoreu(r3, R3);
      }
      for(Index i=peeled_mc2; i<rows; i++)
      {
        const LhsScalar* blA = &blockA[i*strideA+offsetA];
        prefetch(&blA[0]);

        // gets a 1 x nr res block as registers
        ResScalar C0(0), C1(0), C2(0), C3(0);
        // TODO directly use blockB ???
        const RhsScalar* blB = &blockB[j2*strideB+offsetB*nr];
        for(Index k=0; k<depth; k++)
        {
          if(nr==2)
          {
            LhsScalar A0;
Don Gagne's avatar
Don Gagne committed
972
            RhsScalar B_0, B1;
LM's avatar
LM committed
973 974

            A0 = blA[k];
Don Gagne's avatar
Don Gagne committed
975
            B_0 = blB[0];
LM's avatar
LM committed
976
            B1 = blB[1];
Don Gagne's avatar
Don Gagne committed
977
            MADD(cj,A0,B_0,C0,B_0);
LM's avatar
LM committed
978 979 980 981 982
            MADD(cj,A0,B1,C1,B1);
          }
          else
          {
            LhsScalar A0;
Don Gagne's avatar
Don Gagne committed
983
            RhsScalar B_0, B1, B2, B3;
LM's avatar
LM committed
984 985

            A0 = blA[k];
Don Gagne's avatar
Don Gagne committed
986
            B_0 = blB[0];
LM's avatar
LM committed
987 988 989 990
            B1 = blB[1];
            B2 = blB[2];
            B3 = blB[3];

Don Gagne's avatar
Don Gagne committed
991
            MADD(cj,A0,B_0,C0,B_0);
LM's avatar
LM committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
            MADD(cj,A0,B1,C1,B1);
            MADD(cj,A0,B2,C2,B2);
            MADD(cj,A0,B3,C3,B3);
          }

          blB += nr;
        }
                  res[(j2+0)*resStride + i] += alpha*C0;
                  res[(j2+1)*resStride + i] += alpha*C1;
        if(nr==4) res[(j2+2)*resStride + i] += alpha*C2;
        if(nr==4) res[(j2+3)*resStride + i] += alpha*C3;
      }
    }
    // process remaining rhs/res columns one at a time
    // => do the same but with nr==1
    for(Index j2=packet_cols; j2<cols; j2++)
    {
      // unpack B
      traits.unpackRhs(depth, &blockB[j2*strideB+offsetB], unpackedB);

      for(Index i=0; i<peeled_mc; i+=mr)
      {
        const LhsScalar* blA = &blockA[i*strideA+offsetA*mr];
        prefetch(&blA[0]);

        // TODO move the res loads to the stores

        // get res block as registers
        AccPacket C0, C4;
        traits.initAcc(C0);
        traits.initAcc(C4);

        const RhsScalar* blB = unpackedB;
        for(Index k=0; k<depth; k++)
        {
          LhsPacket A0, A1;
Don Gagne's avatar
Don Gagne committed
1028
          RhsPacket B_0;
LM's avatar
LM committed
1029 1030 1031 1032
          RhsPacket T0;

          traits.loadLhs(&blA[0*LhsProgress], A0);
          traits.loadLhs(&blA[1*LhsProgress], A1);
Don Gagne's avatar
Don Gagne committed
1033 1034 1035
          traits.loadRhs(&blB[0*RhsProgress], B_0);
          traits.madd(A0,B_0,C0,T0);
          traits.madd(A1,B_0,C4,B_0);
LM's avatar
LM committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

          blB += RhsProgress;
          blA += 2*LhsProgress;
        }
        ResPacket R0, R4;
        ResPacket alphav = pset1<ResPacket>(alpha);

        ResScalar* r0 = &res[(j2+0)*resStride + i];

        R0 = ploadu<ResPacket>(r0);
        R4 = ploadu<ResPacket>(r0+ResPacketSize);

        traits.acc(C0, alphav, R0);
        traits.acc(C4, alphav, R4);

        pstoreu(r0,               R0);
        pstoreu(r0+ResPacketSize, R4);
      }
      if(rows-peeled_mc>=LhsProgress)
      {
        Index i = peeled_mc;
        const LhsScalar* blA = &blockA[i*strideA+offsetA*LhsProgress];
        prefetch(&blA[0]);

        AccPacket C0;
        traits.initAcc(C0);

        const RhsScalar* blB = unpackedB;
        for(Index k=0; k<depth; k++)
        {
          LhsPacket A0;
Don Gagne's avatar
Don Gagne committed
1067
          RhsPacket B_0;
LM's avatar
LM committed
1068
          traits.loadLhs(blA, A0);
Don Gagne's avatar
Don Gagne committed
1069 1070
          traits.loadRhs(blB, B_0);
          traits.madd(A0, B_0, C0, B_0);
LM's avatar
LM committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
          blB += RhsProgress;
          blA += LhsProgress;
        }

        ResPacket alphav = pset1<ResPacket>(alpha);
        ResPacket R0 = ploadu<ResPacket>(&res[(j2+0)*resStride + i]);
        traits.acc(C0, alphav, R0);
        pstoreu(&res[(j2+0)*resStride + i], R0);
      }
      for(Index i=peeled_mc2; i<rows; i++)
      {
        const LhsScalar* blA = &blockA[i*strideA+offsetA];
        prefetch(&blA[0]);

        // gets a 1 x 1 res block as registers
        ResScalar C0(0);
        // FIXME directly use blockB ??
        const RhsScalar* blB = &blockB[j2*strideB+offsetB];
        for(Index k=0; k<depth; k++)
        {
          LhsScalar A0 = blA[k];
Don Gagne's avatar
Don Gagne committed
1092 1093
          RhsScalar B_0 = blB[k];
          MADD(cj, A0, B_0, C0, B_0);
LM's avatar
LM committed
1094 1095 1096 1097 1098
        }
        res[(j2+0)*resStride + i] += alpha*C0;
      }
    }
  }
Don Gagne's avatar
Don Gagne committed
1099

LM's avatar
LM committed
1100 1101 1102 1103

#undef CJMADD

// pack a block of the lhs
Don Gagne's avatar
Don Gagne committed
1104
// The traversal is as follow (mr==4):
LM's avatar
LM committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
//   0  4  8 12 ...
//   1  5  9 13 ...
//   2  6 10 14 ...
//   3  7 11 15 ...
//
//  16 20 24 28 ...
//  17 21 25 29 ...
//  18 22 26 30 ...
//  19 23 27 31 ...
//
//  32 33 34 35 ...
//  36 36 38 39 ...
template<typename Scalar, typename Index, int Pack1, int Pack2, int StorageOrder, bool Conjugate, bool PanelMode>
struct gemm_pack_lhs
{
Don Gagne's avatar
Don Gagne committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
  EIGEN_DONT_INLINE void operator()(Scalar* blockA, const Scalar* EIGEN_RESTRICT _lhs, Index lhsStride, Index depth, Index rows, Index stride=0, Index offset=0);
};

template<typename Scalar, typename Index, int Pack1, int Pack2, int StorageOrder, bool Conjugate, bool PanelMode>
EIGEN_DONT_INLINE void gemm_pack_lhs<Scalar, Index, Pack1, Pack2, StorageOrder, Conjugate, PanelMode>
  ::operator()(Scalar* blockA, const Scalar* EIGEN_RESTRICT _lhs, Index lhsStride, Index depth, Index rows, Index stride, Index offset)
{
  typedef typename packet_traits<Scalar>::type Packet;
  enum { PacketSize = packet_traits<Scalar>::size };

  EIGEN_ASM_COMMENT("EIGEN PRODUCT PACK LHS");
1131 1132
  EIGEN_UNUSED_VARIABLE(stride)
  EIGEN_UNUSED_VARIABLE(offset)
Don Gagne's avatar
Don Gagne committed
1133 1134 1135 1136 1137 1138 1139
  eigen_assert(((!PanelMode) && stride==0 && offset==0) || (PanelMode && stride>=depth && offset<=stride));
  eigen_assert( (StorageOrder==RowMajor) || ((Pack1%PacketSize)==0 && Pack1<=4*PacketSize) );
  conj_if<NumTraits<Scalar>::IsComplex && Conjugate> cj;
  const_blas_data_mapper<Scalar, Index, StorageOrder> lhs(_lhs,lhsStride);
  Index count = 0;
  Index peeled_mc = (rows/Pack1)*Pack1;
  for(Index i=0; i<peeled_mc; i+=Pack1)
LM's avatar
LM committed
1140
  {
Don Gagne's avatar
Don Gagne committed
1141 1142 1143
    if(PanelMode) count += Pack1 * offset;

    if(StorageOrder==ColMajor)
LM's avatar
LM committed
1144 1145
    {
      for(Index k=0; k<depth; k++)
Don Gagne's avatar
Don Gagne committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
      {
        Packet A, B, C, D;
        if(Pack1>=1*PacketSize) A = ploadu<Packet>(&lhs(i+0*PacketSize, k));
        if(Pack1>=2*PacketSize) B = ploadu<Packet>(&lhs(i+1*PacketSize, k));
        if(Pack1>=3*PacketSize) C = ploadu<Packet>(&lhs(i+2*PacketSize, k));
        if(Pack1>=4*PacketSize) D = ploadu<Packet>(&lhs(i+3*PacketSize, k));
        if(Pack1>=1*PacketSize) { pstore(blockA+count, cj.pconj(A)); count+=PacketSize; }
        if(Pack1>=2*PacketSize) { pstore(blockA+count, cj.pconj(B)); count+=PacketSize; }
        if(Pack1>=3*PacketSize) { pstore(blockA+count, cj.pconj(C)); count+=PacketSize; }
        if(Pack1>=4*PacketSize) { pstore(blockA+count, cj.pconj(D)); count+=PacketSize; }
      }
LM's avatar
LM committed
1157
    }
Don Gagne's avatar
Don Gagne committed
1158
    else
LM's avatar
LM committed
1159 1160
    {
      for(Index k=0; k<depth; k++)
Don Gagne's avatar
Don Gagne committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
      {
        // TODO add a vectorized transpose here
        Index w=0;
        for(; w<Pack1-3; w+=4)
        {
          Scalar a(cj(lhs(i+w+0, k))),
                  b(cj(lhs(i+w+1, k))),
                  c(cj(lhs(i+w+2, k))),
                  d(cj(lhs(i+w+3, k)));
          blockA[count++] = a;
          blockA[count++] = b;
          blockA[count++] = c;
          blockA[count++] = d;
        }
        if(Pack1%4)
          for(;w<Pack1;++w)
            blockA[count++] = cj(lhs(i+w, k));
      }
LM's avatar
LM committed
1179
    }
Don Gagne's avatar
Don Gagne committed
1180
    if(PanelMode) count += Pack1 * (stride-offset-depth);
LM's avatar
LM committed
1181
  }
Don Gagne's avatar
Don Gagne committed
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
  if(rows-peeled_mc>=Pack2)
  {
    if(PanelMode) count += Pack2*offset;
    for(Index k=0; k<depth; k++)
      for(Index w=0; w<Pack2; w++)
        blockA[count++] = cj(lhs(peeled_mc+w, k));
    if(PanelMode) count += Pack2 * (stride-offset-depth);
    peeled_mc += Pack2;
  }
  for(Index i=peeled_mc; i<rows; i++)
  {
    if(PanelMode) count += offset;
    for(Index k=0; k<depth; k++)
      blockA[count++] = cj(lhs(i, k));
    if(PanelMode) count += (stride-offset-depth);
  }
}
LM's avatar
LM committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

// copy a complete panel of the rhs
// this version is optimized for column major matrices
// The traversal order is as follow: (nr==4):
//  0  1  2  3   12 13 14 15   24 27
//  4  5  6  7   16 17 18 19   25 28
//  8  9 10 11   20 21 22 23   26 29
//  .  .  .  .    .  .  .  .    .  .
template<typename Scalar, typename Index, int nr, bool Conjugate, bool PanelMode>
struct gemm_pack_rhs<Scalar, Index, nr, ColMajor, Conjugate, PanelMode>
{
  typedef typename packet_traits<Scalar>::type Packet;
  enum { PacketSize = packet_traits<Scalar>::size };
Don Gagne's avatar
Don Gagne committed
1212 1213 1214 1215 1216 1217 1218 1219
  EIGEN_DONT_INLINE void operator()(Scalar* blockB, const Scalar* rhs, Index rhsStride, Index depth, Index cols, Index stride=0, Index offset=0);
};

template<typename Scalar, typename Index, int nr, bool Conjugate, bool PanelMode>
EIGEN_DONT_INLINE void gemm_pack_rhs<Scalar, Index, nr, ColMajor, Conjugate, PanelMode>
  ::operator()(Scalar* blockB, const Scalar* rhs, Index rhsStride, Index depth, Index cols, Index stride, Index offset)
{
  EIGEN_ASM_COMMENT("EIGEN PRODUCT PACK RHS COLMAJOR");
1220 1221
  EIGEN_UNUSED_VARIABLE(stride)
  EIGEN_UNUSED_VARIABLE(offset)
Don Gagne's avatar
Don Gagne committed
1222 1223 1224 1225 1226
  eigen_assert(((!PanelMode) && stride==0 && offset==0) || (PanelMode && stride>=depth && offset<=stride));
  conj_if<NumTraits<Scalar>::IsComplex && Conjugate> cj;
  Index packet_cols = (cols/nr) * nr;
  Index count = 0;
  for(Index j2=0; j2<packet_cols; j2+=nr)
LM's avatar
LM committed
1227
  {
Don Gagne's avatar
Don Gagne committed
1228 1229 1230 1231 1232 1233 1234
    // skip what we have before
    if(PanelMode) count += nr * offset;
    const Scalar* b0 = &rhs[(j2+0)*rhsStride];
    const Scalar* b1 = &rhs[(j2+1)*rhsStride];
    const Scalar* b2 = &rhs[(j2+2)*rhsStride];
    const Scalar* b3 = &rhs[(j2+3)*rhsStride];
    for(Index k=0; k<depth; k++)
LM's avatar
LM committed
1235
    {
Don Gagne's avatar
Don Gagne committed
1236 1237 1238 1239 1240
                blockB[count+0] = cj(b0[k]);
                blockB[count+1] = cj(b1[k]);
      if(nr==4) blockB[count+2] = cj(b2[k]);
      if(nr==4) blockB[count+3] = cj(b3[k]);
      count += nr;
LM's avatar
LM committed
1241
    }
Don Gagne's avatar
Don Gagne committed
1242 1243 1244
    // skip what we have after
    if(PanelMode) count += nr * (stride-offset-depth);
  }
LM's avatar
LM committed
1245

Don Gagne's avatar
Don Gagne committed
1246 1247 1248 1249 1250 1251
  // copy the remaining columns one at a time (nr==1)
  for(Index j2=packet_cols; j2<cols; ++j2)
  {
    if(PanelMode) count += offset;
    const Scalar* b0 = &rhs[(j2+0)*rhsStride];
    for(Index k=0; k<depth; k++)
LM's avatar
LM committed
1252
    {
Don Gagne's avatar
Don Gagne committed
1253 1254
      blockB[count] = cj(b0[k]);
      count += 1;
LM's avatar
LM committed
1255
    }
Don Gagne's avatar
Don Gagne committed
1256
    if(PanelMode) count += (stride-offset-depth);
LM's avatar
LM committed
1257
  }
Don Gagne's avatar
Don Gagne committed
1258
}
LM's avatar
LM committed
1259 1260 1261 1262 1263 1264

// this version is optimized for row major matrices
template<typename Scalar, typename Index, int nr, bool Conjugate, bool PanelMode>
struct gemm_pack_rhs<Scalar, Index, nr, RowMajor, Conjugate, PanelMode>
{
  enum { PacketSize = packet_traits<Scalar>::size };
Don Gagne's avatar
Don Gagne committed
1265 1266 1267 1268 1269 1270 1271 1272
  EIGEN_DONT_INLINE void operator()(Scalar* blockB, const Scalar* rhs, Index rhsStride, Index depth, Index cols, Index stride=0, Index offset=0);
};

template<typename Scalar, typename Index, int nr, bool Conjugate, bool PanelMode>
EIGEN_DONT_INLINE void gemm_pack_rhs<Scalar, Index, nr, RowMajor, Conjugate, PanelMode>
  ::operator()(Scalar* blockB, const Scalar* rhs, Index rhsStride, Index depth, Index cols, Index stride, Index offset)
{
  EIGEN_ASM_COMMENT("EIGEN PRODUCT PACK RHS ROWMAJOR");
1273 1274
  EIGEN_UNUSED_VARIABLE(stride)
  EIGEN_UNUSED_VARIABLE(offset)
Don Gagne's avatar
Don Gagne committed
1275 1276 1277 1278 1279
  eigen_assert(((!PanelMode) && stride==0 && offset==0) || (PanelMode && stride>=depth && offset<=stride));
  conj_if<NumTraits<Scalar>::IsComplex && Conjugate> cj;
  Index packet_cols = (cols/nr) * nr;
  Index count = 0;
  for(Index j2=0; j2<packet_cols; j2+=nr)
LM's avatar
LM committed
1280
  {
Don Gagne's avatar
Don Gagne committed
1281 1282 1283
    // skip what we have before
    if(PanelMode) count += nr * offset;
    for(Index k=0; k<depth; k++)
LM's avatar
LM committed
1284
    {
Don Gagne's avatar
Don Gagne committed
1285 1286 1287 1288 1289 1290
      const Scalar* b0 = &rhs[k*rhsStride + j2];
                blockB[count+0] = cj(b0[0]);
                blockB[count+1] = cj(b0[1]);
      if(nr==4) blockB[count+2] = cj(b0[2]);
      if(nr==4) blockB[count+3] = cj(b0[3]);
      count += nr;
LM's avatar
LM committed
1291
    }
Don Gagne's avatar
Don Gagne committed
1292 1293 1294 1295 1296 1297 1298 1299 1300
    // skip what we have after
    if(PanelMode) count += nr * (stride-offset-depth);
  }
  // copy the remaining columns one at a time (nr==1)
  for(Index j2=packet_cols; j2<cols; ++j2)
  {
    if(PanelMode) count += offset;
    const Scalar* b0 = &rhs[j2];
    for(Index k=0; k<depth; k++)
LM's avatar
LM committed
1301
    {
Don Gagne's avatar
Don Gagne committed
1302 1303
      blockB[count] = cj(b0[k*rhsStride]);
      count += 1;
LM's avatar
LM committed
1304
    }
Don Gagne's avatar
Don Gagne committed
1305
    if(PanelMode) count += stride-offset-depth;
LM's avatar
LM committed
1306
  }
Don Gagne's avatar
Don Gagne committed
1307
}
LM's avatar
LM committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338

} // end namespace internal

/** \returns the currently set level 1 cpu cache size (in bytes) used to estimate the ideal blocking size parameters.
  * \sa setCpuCacheSize */
inline std::ptrdiff_t l1CacheSize()
{
  std::ptrdiff_t l1, l2;
  internal::manage_caching_sizes(GetAction, &l1, &l2);
  return l1;
}

/** \returns the currently set level 2 cpu cache size (in bytes) used to estimate the ideal blocking size parameters.
  * \sa setCpuCacheSize */
inline std::ptrdiff_t l2CacheSize()
{
  std::ptrdiff_t l1, l2;
  internal::manage_caching_sizes(GetAction, &l1, &l2);
  return l2;
}

/** Set the cpu L1 and L2 cache sizes (in bytes).
  * These values are use to adjust the size of the blocks
  * for the algorithms working per blocks.
  *
  * \sa computeProductBlockingSizes */
inline void setCpuCacheSizes(std::ptrdiff_t l1, std::ptrdiff_t l2)
{
  internal::manage_caching_sizes(SetAction, &l1, &l2);
}

Don Gagne's avatar
Don Gagne committed
1339 1340
} // end namespace Eigen

LM's avatar
LM committed
1341
#endif // EIGEN_GENERAL_BLOCK_PANEL_H