// File: /workspace/github_cpp_makefile_star2_O2/Kingsford-Group[P]ghost2/include/Eigen/src/Core/products/GeneralMatrixMatrix.h
// Function: Eigen::internal::general_matrix_matrix_product<long, double, 0, false, double, 0, false, 0>::run(long, long, long, double const*, long, double const*, long, double*, long, double, Eigen::internal::level3_blocking<double, double>&, Eigen::internal::GemmParallelInfo<long>*)
static void run(Index rows, Index cols, Index depth,
  const LhsScalar* _lhs, Index lhsStride,
  const RhsScalar* _rhs, Index rhsStride,
  ResScalar* res, Index resStride,
  ResScalar alpha,
  level3_blocking<LhsScalar,RhsScalar>& blocking,
  GemmParallelInfo<Index>* info = 0)
{
  const_blas_data_mapper<LhsScalar, Index, LhsStorageOrder> lhs(_lhs,lhsStride);
  const_blas_data_mapper<RhsScalar, Index, RhsStorageOrder> rhs(_rhs,rhsStride);

  typedef gebp_traits<LhsScalar,RhsScalar> Traits;

  Index kc = blocking.kc();                   // cache block size along the K direction
  Index mc = (std::min)(rows,blocking.mc());  // cache block size along the M direction
  //Index nc = blocking.nc(); // cache block size along the N direction

  gemm_pack_lhs<LhsScalar, Index, Traits::mr, Traits::LhsProgress, LhsStorageOrder> pack_lhs;
  gemm_pack_rhs<RhsScalar, Index, Traits::nr, RhsStorageOrder> pack_rhs;
  gebp_kernel<LhsScalar, RhsScalar, Index, Traits::mr, Traits::nr, ConjugateLhs, ConjugateRhs> gebp;

#ifdef EIGEN_HAS_OPENMP
  if(info)
  {
    // this is the parallel version!
    Index tid = omp_get_thread_num();
    Index threads = omp_get_num_threads();
    
    std::size_t sizeA = kc*mc;
    std::size_t sizeW = kc*Traits::WorkSpaceFactor;
    ei_declare_aligned_stack_constructed_variable(LhsScalar, blockA, sizeA, 0);
    ei_declare_aligned_stack_constructed_variable(RhsScalar, w, sizeW, 0);
    
    RhsScalar* blockB = blocking.blockB();
    eigen_internal_assert(blockB!=0);

    // For each horizontal panel of the rhs, and corresponding vertical panel of the lhs...
    for(Index k=0; k<depth; k+=kc)
    {
      const Index actual_kc = (std::min)(k+kc,depth)-k; // => rows of B', and cols of the A'

      // In order to reduce the chance that a thread has to wait for the other,
      // let's start by packing A'.
      pack_lhs(blockA, &lhs(0,k), lhsStride, actual_kc, mc);

      // Pack B_k to B' in a parallel fashion:
      // each thread packs the sub block B_k,j to B'_j where j is the thread id.

      // However, before copying to B'_j, we have to make sure that no other thread is still using it,
      // i.e., we test that info[tid].users equals 0.
      // Then, we set info[tid].users to the number of threads to mark that all other threads are going to use it.
      while(info[tid].users!=0) {}
      info[tid].users += threads;

      pack_rhs(blockB+info[tid].rhs_start*actual_kc, &rhs(k,info[tid].rhs_start), rhsStride, actual_kc, info[tid].rhs_length);

      // Notify the other threads that the part B'_j is ready to go.
      info[tid].sync = k;

      // Computes C_i += A' * B' per B'_j
      for(Index shift=0; shift<threads; ++shift)
      {
        Index j = (tid+shift)%threads;

        // At this point we have to make sure that B'_j has been updated by the thread j,
        // we use testAndSetOrdered to mimic a volatile access.
        // However, no need to wait for the B' part which has been updated by the current thread!
        if(shift>0)
          while(info[j].sync!=k) {}

        gebp(res+info[j].rhs_start*resStride, resStride, blockA, blockB+info[j].rhs_start*actual_kc, mc, actual_kc, info[j].rhs_length, alpha, -1,-1,0,0, w);
      }

      // Then keep going as usual with the remaining A'
      for(Index i=mc; i<rows; i+=mc)
      {
        const Index actual_mc = (std::min)(i+mc,rows)-i;

        // pack A_i,k to A'
        pack_lhs(blockA, &lhs(i,k), lhsStride, actual_kc, actual_mc);

        // C_i += A' * B'
        gebp(res+i, resStride, blockA, blockB, actual_mc, actual_kc, cols, alpha, -1,-1,0,0, w);
      }

      // Release all the sub blocks B'_j of B' for the current thread,
      // i.e., we simply decrement the number of users by 1
      for(Index j=0; j<threads; ++j)
        #pragma omp atomic
        --(info[j].users);
    }
  }
  else
#endif // EIGEN_HAS_OPENMP
  {
    EIGEN_UNUSED_VARIABLE(info);

    // this is the sequential version!
    std::size_t sizeA = kc*mc;
    std::size_t sizeB = kc*cols;
    std::size_t sizeW = kc*Traits::WorkSpaceFactor;

    ei_declare_aligned_stack_constructed_variable(LhsScalar, blockA, sizeA, blocking.blockA());
    ei_declare_aligned_stack_constructed_variable(RhsScalar, blockB, sizeB, blocking.blockB());
    ei_declare_aligned_stack_constructed_variable(RhsScalar, blockW, sizeW, blocking.blockW());

    // For each horizontal panel of the rhs, and corresponding panel of the lhs...
    // (==GEMM_VAR1)
    for(Index k2=0; k2<depth; k2+=kc)
    {
      const Index actual_kc = (std::min)(k2+kc,depth)-k2;

      // OK, here we have selected one horizontal panel of rhs and one vertical panel of lhs.
      // => Pack rhs's panel into a sequential chunk of memory (L2 caching)
      // Note that this panel will be read as many times as the number of blocks in the lhs's
      // vertical panel which is, in practice, a very low number.
      pack_rhs(blockB, &rhs(k2,0), rhsStride, actual_kc, cols);

      // For each mc x kc block of the lhs's vertical panel...
      // (==GEPP_VAR1)
      for(Index i2=0; i2<rows; i2+=mc)
      {
        const Index actual_mc = (std::min)(i2+mc,rows)-i2;

        // We pack the lhs's block into a sequential chunk of memory (L1 caching)
        // Note that this block will be read a very high number of times, which is equal to the number of
        // micro vertical panel of the large rhs's panel (e.g., cols/4 times).
        pack_lhs(blockA, &lhs(i2,k2), lhsStride, actual_kc, actual_mc);

        // Everything is packed, we can now call the block * panel kernel:
        gebp(res+i2, resStride, blockA, blockB, actual_mc, actual_kc, cols, alpha, -1, -1, 0, 0, blockW);
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movsd %xmm0, -0xa8(%rbp)
movq %r9, -0x78(%rbp)
movq %r8, -0x68(%rbp)
movq %rcx, %r12
movq %rdx, -0x80(%rbp)
movq %rsi, -0x58(%rbp)
movq 0x28(%rbp), %r15
movq 0x18(%r15), %rbx
movq 0x28(%r15), %rcx
cmpq %rdi, %rbx
movq %rdi, -0xb8(%rbp)
cmovgeq %rdi, %rbx
movq %rbx, -0x50(%rbp)
movq %rcx, -0x38(%rbp)
imulq %rcx, %rbx
movq %rbx, %rax
shrq $0x3d, %rax
je 0x3a73d
callq 0x23674
movq -0x38(%rbp), %r13
imulq -0x58(%rbp), %r13
movq (%r15), %rax
leaq (,%rbx,8), %rdi
testq %rax, %rax
je 0x3a768
movq %rax, -0x48(%rbp)
cmpq $0x4e21, %rdi           # imm = 0x4E21
setae %al
xorl %ecx, %ecx
jmp 0x3a79f
cmpq $0x4e20, %rdi           # imm = 0x4E20
ja 0x3a78a
movq %rsp, %rcx
addq $0xf, %rdi
andq $-0x10, %rdi
subq %rdi, %rcx
movq %rcx, %rsp
xorl %eax, %eax
movq %rcx, -0x48(%rbp)
jmp 0x3a79f
callq 0x236a1
xorl %ecx, %ecx
cmpq $0x0, (%r15)
movq %rax, -0x48(%rbp)
cmoveq %rax, %rcx
movb $0x1, %al
movq %rcx, -0x110(%rbp)
movq %rbx, -0x108(%rbp)
movb %al, -0x100(%rbp)
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r13
jbe 0x3a7c7
callq 0x23674
movq 0x8(%r15), %rax
leaq (,%r13,8), %r14
testq %rax, %rax
je 0x3a7e0
movq %rax, -0x40(%rbp)
xorl %edx, %edx
jmp 0x3a817
cmpq $0x4e20, %r14           # imm = 0x4E20
ja 0x3a800
movq %rsp, %rdx
leaq 0xf(%r14), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, %rsp
movq %rdx, -0x40(%rbp)
jmp 0x3a817
movq %r14, %rdi
callq 0x236a1
xorl %edx, %edx
cmpq $0x0, 0x8(%r15)
movq %rax, -0x40(%rbp)
cmoveq %rax, %rdx
movq -0x38(%rbp), %rcx
leaq (,%rcx,8), %rbx
cmpq $0x4e21, %r14           # imm = 0x4E21
movq %rdx, -0xf8(%rbp)
movq %r13, -0xf0(%rbp)
setae -0xe8(%rbp)
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %rbx
jbe 0x3a853
callq 0x23674
movq 0x10(%r15), %rax
movq -0x38(%rbp), %r14
shlq $0x6, %r14
testq %rax, %rax
je 0x3a868
xorl %edx, %edx
jmp 0x3a89a
cmpq $0x4e20, %r14           # imm = 0x4E20
ja 0x3a887
movq %rsp, %rdx
leaq 0xf(%r14), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, %rsp
movq %rdx, %rax
jmp 0x3a89a
movq %r14, %rdi
callq 0x236a1
xorl %edx, %edx
cmpq $0x0, 0x10(%r15)
cmoveq %rax, %rdx
movq %rax, -0xa0(%rbp)
cmpq $0x4e21, %r14           # imm = 0x4E21
movq %rdx, -0xe0(%rbp)
movq %rbx, -0xd8(%rbp)
setae -0xd0(%rbp)
movq -0x38(%rbp), %rax
imulq -0x68(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0x98(%rbp)
xorl %eax, %eax
xorps %xmm0, %xmm0
movq -0x80(%rbp), %rcx
cmpq %rcx, %rax
jge 0x3aa55
movq -0x38(%rbp), %rdx
addq %rax, %rdx
cmpq %rcx, %rdx
movq %rcx, %r8
movq %rdx, -0x90(%rbp)
cmovlq %rdx, %r8
subq %rax, %r8
movq -0x78(%rbp), %rcx
leaq (%rcx,%rax,8), %rdx
subq $0x10, %rsp
movups %xmm0, (%rsp)
leaq -0x2a(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq 0x10(%rbp), %rcx
movq %r8, -0xb0(%rbp)
movq -0x58(%rbp), %r9
callq 0x3aac8
popq %rax
popq %rcx
movq 0x18(%rbp), %rdx
xorl %r14d, %r14d
movq -0x50(%rbp), %r13
movq %r12, -0x88(%rbp)
xorl %ecx, %ecx
movq -0xb8(%rbp), %rax
cmpq %r13, %rax
movq %r13, %r15
cmovlq %rax, %r15
cmpq %rax, %rcx
jge 0x3aa3e
addq %r14, %r15
subq $0x10, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
leaq -0x2b(%rbp), %rdi
movq %rdx, -0xc8(%rbp)
movq -0x48(%rbp), %rsi
movq %r12, %rdx
movq %rcx, -0x60(%rbp)
movq -0x68(%rbp), %rcx
movq -0xb0(%rbp), %rbx
movq %rbx, %r8
movq %r15, %r9
callq 0x3ac18
movq -0xc8(%rbp), %rsi
movq -0x60(%rbp), %rcx
addq $0x10, %rsp
movq -0x50(%rbp), %rax
addq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq -0x98(%rbp), %rcx
addq %rcx, %r12
addq %rax, %r13
subq %rax, %r14
leaq (%rsi,%rcx), %rax
movq %rax, -0xc0(%rbp)
subq $0x40, %rsp
movq -0xa0(%rbp), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rsp)
orq $-0x1, 0x18(%rsp)
movq -0x58(%rbp), %rax
movq %rax, 0x8(%rsp)
orq $-0x1, 0x10(%rsp)
movq %rbx, (%rsp)
leaq -0x29(%rbp), %rdi
movq 0x20(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r8
movq %r15, %r9
movsd -0xa8(%rbp), %xmm0
callq 0x3ad2c
movq -0x60(%rbp), %rcx
addq $0x40, %rsp
movq -0xc0(%rbp), %rdx
jmp 0x3a950
movq -0x88(%rbp), %r12
addq -0x70(%rbp), %r12
movq -0x90(%rbp), %rax
jmp 0x3a8e3
leaq -0xe0(%rbp), %rdi
callq 0x25cb2
leaq -0xf8(%rbp), %rdi
callq 0x25cb2
leaq -0x110(%rbp), %rdi
callq 0x25cb2
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3aa8c
jmp 0x3aa91
movq %rax, %rbx
jmp 0x3aaa7
movq %rax, %rbx
jmp 0x3aab3
jmp 0x3aa98
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
callq 0x25cb2
leaq -0xf8(%rbp), %rdi
callq 0x25cb2
leaq -0x110(%rbp), %rdi
callq 0x25cb2
movq %rbx, %rdi
callq 0xe9f0
nop