// File: 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:
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, 0x2c0(%rsp)
movq %rsi, 0x2b8(%rsp)
movq %rdx, 0x2b0(%rsp)
movq %rcx, 0x2a8(%rsp)
movq 0x2c0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x2b0(%rsp), %rdi
callq 0x31a10
movq %rax, 0x2a0(%rsp)
movq 0x2b0(%rsp), %rdi
callq 0x31a20
movq %rax, 0x298(%rsp)
movq 0x2a8(%rsp), %rdi
callq 0x31a10
movq %rax, 0x290(%rsp)
movq 0x2a8(%rsp), %rdi
callq 0x31a20
movq %rax, 0x288(%rsp)
movq 0x298(%rsp), %rax
cmpq 0x290(%rsp), %rax
sete %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x667d3
jmp 0x6683e
leaq 0x2be4e(%rip), %rsi     # 0x92628
leaq 0x100(%rsp), %rdi
movq %rdi, 0x28(%rsp)
movl $0x21, %edx
movl $0x3, %ecx
callq 0x89860
movq 0x28(%rsp), %rdi
leaq 0x2be43(%rip), %rsi     # 0x92645
callq 0x5ce20
jmp 0x66809
leaq 0x100(%rsp), %rdi
callq 0x89c20
jmp 0x6683e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x89c20
jmp 0x66b9d
movq 0x2a0(%rsp), %rax
imulq 0x288(%rsp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x9030
movq %rax, 0xe8(%rsp)
movq 0x2b0(%rsp), %rdi
callq 0x31a30
movq %rax, 0xe0(%rsp)
movq 0x2a8(%rsp), %rdi
callq 0x31a30
movq 0x30(%rsp), %rdi
movq %rax, 0xd8(%rsp)
movq 0x2a0(%rsp), %rax
imulq 0x288(%rsp), %rax
imulq 0x298(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xd0(%rsp), %rsi
callq 0xa8e0
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rax, %rdx
callq 0xa970
movq %rax, 0xc8(%rsp)
movq 0x2a0(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0xc8(%rsp)
movq %rax, 0xc0(%rsp)
movq 0x2a0(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0xc8(%rsp)
cmpq $0x0, %rdx
je 0x66947
movq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc0(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xa7(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0xa9f0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0xa8(%rsp), %rdi
callq 0xaa00
jmp 0x6697f
leaq 0xa7(%rsp), %rdi
callq 0xaa80
movq $0x0, 0x98(%rsp)
movq 0x98(%rsp), %rax
cmpq 0xc8(%rsp), %rax
je 0x66af8
leaq 0xe8(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0xe0(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0xd8(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x298(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x288(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x98(%rsp), %rax
movq 0xc0(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x60(%rsp)
movq 0x98(%rsp), %rax
incq %rax
movq 0xc0(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x58(%rsp)
leaq 0x2a0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xaa90
movq %rax, 0x8(%rsp)
jmp 0x66a43
movq 0x8(%rsp), %rcx
leaq 0x90(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x66bb0
jmp 0x66a61
movq 0x98(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0xaba0
movq %rax, %rdi
leaq 0x90(%rsp), %rsi
callq 0xabc0
leaq 0x90(%rsp), %rdi
callq 0xac00
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x66998
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
leaq 0xa7(%rsp), %rdi
callq 0xaa80
jmp 0x66b9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0xacf0
jmp 0x66b9d
leaq 0xa8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
callq 0xac20
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
callq 0xac50
movq %rax, 0x40(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0xac80
testb $0x1, %al
jne 0x66b38
jmp 0x66b61
leaq 0x48(%rsp), %rdi
callq 0xacc0
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
callq 0x96c0
jmp 0x66b53
jmp 0x66b55
leaq 0x48(%rsp), %rdi
callq 0xacd0
jmp 0x66b23
movq 0x2b8(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq 0x2a0(%rsp), %rdx
movq 0x288(%rsp), %rcx
callq 0x31fe0
jmp 0x66b88
leaq 0xa8(%rsp), %rdi
callq 0xacf0
addq $0x2c8, %rsp            # imm = 0x2C8
retq
movq 0xf8(%rsp), %rdi
callq 0x9700
nopw (%rax,%rax)