// File: /workspace/github_cpp_makefile_star2_O3/btbujiangjun[P]abcdl/src/algebra/MatrixHelper.cpp
// Function: abcdl::algebra::MatrixHelper<int>::dot(abcdl::algebra::Matrix<int>&, abcdl::algebra::Matrix<int> const&, abcdl::algebra::Matrix<int> const&)
void MatrixHelper<T>::dot(Matrix<T>& mat,
						  const Matrix<T>& mat_a,
						  const Matrix<T>& mat_b){
    size_t row_a = mat_a.rows();
    size_t col_a = mat_a.cols();
    size_t row_b = mat_b.rows();
    size_t col_b = mat_b.cols();

    CHECK(col_a == row_b);

    T* data   = new T[row_a * col_b];
    T* data_a = mat_a.data();
    T* data_b = mat_b.data();

    size_t size = row_a * col_b * col_a;
    size_t num_thread = _po.get_num_thread(size, _po.get_block_size(size));
    size_t block_size = row_a / num_thread;
    if(row_a % num_thread != 0){
        block_size += 1;
    }

    std::vector<std::thread> threads(num_thread);
    for(size_t i = 0; i != num_thread; i++){
        threads[i] = std::thread(
            [&data, &data_a, &data_b, &col_a, &col_b](size_t start_idx, size_t end_idx){
				for(size_t ti = start_idx; ti < end_idx; ti++){
					size_t a_init_idx = ti * col_a;
					for(size_t tj = 0; tj != col_b; tj++){
						T value = 0;
						size_t a_idx = a_init_idx;
						for(size_t tk = 0; tk != col_a; tk++){
							value += data_a[a_idx++] * data_b[tk * col_b + tj];
						}
						data[ti * col_b + tj] = value;
					}
				}
            }, i * block_size , std::min(row_a, (i + 1) * block_size)
        );
    }

    for(auto& thread : threads){
        thread.join();
    }

    mat.set_shallow_data(data, row_a, col_b);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rcx, %r14
movq %rdx, %rbp
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
movq (%rdx), %r15
movq %r15, 0x8(%rsp)
movq 0x8(%rdx), %r12
movq %r12, 0x68(%rsp)
movq 0x8(%rcx), %rbx
cmpq (%rcx), %r12
movq %rbx, 0x18(%rsp)
jne 0x2e855
imulq %r15, %rbx
movq %rbx, %rax
shrq $0x3e, %rax
leaq (,%rbx,4), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x9030
movq %rax, 0x10(%rsp)
movq 0x10(%rbp), %rax
movq %rax, 0x60(%rsp)
movq 0x10(%r14), %rax
movq %rax, 0x58(%rsp)
imulq %rbx, %r12
movq (%r13), %r14
movq 0x8(%r13), %rsi
movq %r12, %rax
xorl %edx, %edx
divq %r14
cmpq $0x1, %rdx
sbbq $-0x1, %rax
cmpq %r12, %rsi
movq %r12, %rcx
cmovbq %rsi, %rcx
cmpq %rsi, %rax
cmovaeq %rax, %rcx
cmpq %rcx, %rsi
jb 0x2e735
movq %r12, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r14
cmpq $0x1, %rdx
sbbq $-0x1, %r14
movq %r15, %rax
xorl %edx, %edx
divq %r14
movq %rax, %r15
cmpq $0x1, %rdx
sbbq $-0x1, %r15
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rdx
movq %r14, %rsi
callq 0xa43e
xorl %ebx, %ebx
leaq 0x70(%rsp), %r13
leaq 0x30(%rsp), %rbp
leaq 0x10(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %rbx, %rax
imulq %r15, %rax
movq %rax, 0x30(%rsp)
leaq 0x1(%rbx), %r12
movq %r12, %rax
imulq %r15, %rax
movq %rax, 0x28(%rsp)
leaq 0x28(%rsp), %rcx
cmpq 0x8(%rsp), %rax
jb 0x2e7ce
leaq 0x8(%rsp), %rcx
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2e8c8
movq 0x40(%rsp), %rax
cmpq $0x0, (%rax,%rbx,8)
jne 0x2e895
movq 0x38(%rsp), %rcx
movq %rcx, (%rax,%rbx,8)
movq %r12, %rbx
cmpq %r12, %r14
jne 0x2e766
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %rbx
cmpq %rbx, %r14
je 0x2e820
movq %r14, %rdi
callq 0x95a0
addq $0x8, %r14
jmp 0x2e80d
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rdi
callq 0x18808
leaq 0x40(%rsp), %rdi
callq 0xa536
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf3c4(%rip), %rsi      # 0x3dc20
leaq 0x70(%rsp), %rdi
movl $0x21, %edx
movl $0x3, %ecx
callq 0x387b6
leaq 0xf3c6(%rip), %rsi      # 0x3dc3d
movl $0x1c, %edx
leaq 0x70(%rsp), %rdi
callq 0x93b0
leaq 0x70(%rsp), %rdi
callq 0x389de
jmp 0x2e6b6
callq 0x90c0
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x389de
jmp 0x2e8bf
jmp 0x2e8b2
movq %rax, %rbx
jmp 0x2e8bf
jmp 0x2e8b2
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xa536
movq %rbx, %rdi
callq 0x95d0
nop