// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rsi, 0x28(%rsp)
movq %rdi, %r12
movq (%rdx), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rdx), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rcx), %rcx
cmpq (%r14), %rax
movq %rcx, 0x10(%rsp)
jne 0x2ea2e
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %rbp
imulq %r15, %rbp
movq %rbp, %rax
shrq $0x3e, %rax
leaq (,%rbp,4), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x9030
movq %rax, 0x18(%rsp)
movq 0x10(%r13), %rax
movq %rax, 0x68(%rsp)
movq 0x10(%r14), %rax
movq %rax, 0x60(%rsp)
imulq 0x20(%rsp), %rbp
movq (%r12), %r14
movq 0x8(%r12), %rsi
movq %rbp, %rax
xorl %edx, %edx
divq %r14
cmpq $0x1, %rdx
sbbq $-0x1, %rax
cmpq %rbp, %rsi
movq %rbp, %rcx
cmovbq %rsi, %rcx
cmpq %rsi, %rax
cmovaeq %rax, %rcx
cmpq %rcx, %rsi
jb 0x2e90e
movq %rbp, %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 0x48(%rsp), %rdi
leaq 0x70(%rsp), %rdx
movq %r14, %rsi
callq 0xa482
xorl %ebx, %ebx
leaq 0x70(%rsp), %r13
leaq 0x38(%rsp), %rbp
leaq 0x18(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %rbx, %rax
imulq %r15, %rax
movq %rax, 0x38(%rsp)
leaq 0x1(%rbx), %r12
movq %r12, %rax
imulq %r15, %rax
movq %rax, 0x30(%rsp)
leaq 0x30(%rsp), %rcx
cmpq 0x8(%rsp), %rax
jb 0x2e9a7
leaq 0x8(%rsp), %rcx
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2eaa2
movq 0x48(%rsp), %rax
cmpq $0x0, (%rax,%rbx,8)
jne 0x2ea6f
movq 0x40(%rsp), %rcx
movq %rcx, (%rax,%rbx,8)
movq %r12, %rbx
cmpq %r12, %r14
jne 0x2e93f
movq 0x48(%rsp), %r14
movq 0x50(%rsp), %rbx
cmpq %rbx, %r14
je 0x2e9f9
movq %r14, %rdi
callq 0x95a0
addq $0x8, %r14
jmp 0x2e9e6
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rdi
callq 0x18a60
leaq 0x48(%rsp), %rdi
callq 0xa594
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf20b(%rip), %rsi      # 0x3dc40
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movl $0x21, %edx
movl $0x3, %ecx
callq 0x3894e
leaq 0xf20a(%rip), %rsi      # 0x3dc5d
movl $0x1c, %edx
movq %r15, %rdi
callq 0x93b0
leaq 0x70(%rsp), %rdi
callq 0x38b76
jmp 0x2e882
callq 0x90c0
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x38b76
jmp 0x2ea99
jmp 0x2ea8c
movq %rax, %rbx
jmp 0x2ea99
jmp 0x2ea8c
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xa594
movq %rbx, %rdi
callq 0x95d0
nop