// File: btbujiangjun[P]abcdl/src/algebra/MatrixHelper.cpp
// Function: abcdl::algebra::MatrixHelper<int>::transpose(abcdl::algebra::Matrix<int>&, abcdl::algebra::Matrix<int> const&)
void MatrixHelper<T>::transpose(Matrix<T>& mat, const Matrix<T>& mat_a){
	size_t rows = mat_a.rows();
	size_t cols = mat_a.cols();
	if(rows == 1 || cols == 1){
        if(&mat != &mat_a){
    		mat.set_data(mat_a.data(), rows, cols);
        }
		mat.reshape(cols, rows);
        return ;
	}

    T* src_data       = mat_a.data();
    T* data           = new T[mat_a.get_size()];
    size_t num_thread = _po.get_num_thread(mat_a.get_size(), _po.get_block_size(mat_a.get_size()));
    size_t block_size = cols / num_thread;
    if(cols % 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, &src_data, rows, cols](size_t start_idx, size_t end_idx){
				for(size_t ti = start_idx; ti < end_idx; ti++){
		    		for(size_t tj = 0; tj != rows; tj++){
                        data[ti * rows + tj] = src_data[tj * cols + ti];
	    			}
		    	}
            }, i * block_size , std::min(cols, (i + 1) * block_size)
        );
    }

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

    mat.set_shallow_data(data, cols, rows);
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq %rdx, 0xf0(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x31a10
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x31a20
movq %rax, 0xe0(%rsp)
cmpq $0x1, 0xe8(%rsp)
je 0x69d8c
cmpq $0x1, 0xe0(%rsp)
jne 0x69df7
movq 0xf8(%rsp), %rax
cmpq 0xf0(%rsp), %rax
je 0x69dd5
movq 0xf8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x31a30
movq 0x20(%rsp), %rdi
movq %rax, %rsi
movq 0xe8(%rsp), %rdx
movq 0xe0(%rsp), %rcx
callq 0x31ea0
movq 0xf8(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
callq 0x335a0
jmp 0x6a10b
movq 0xf0(%rsp), %rdi
callq 0x31a30
movq %rax, 0xd8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x317f0
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x9030
movq %rax, 0xd0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x317f0
movq %rax, 0x18(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x317f0
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0xa8e0
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
callq 0xa970
movq %rax, 0xc8(%rsp)
movq 0xe0(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0xc8(%rsp)
movq %rax, 0xc0(%rsp)
movq 0xe0(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0xc8(%rsp)
cmpq $0x0, %rdx
je 0x69ed2
movq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc0(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0xa7(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0xa9f0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0xa8(%rsp), %rdi
callq 0xaa00
jmp 0x69f0a
leaq 0xa7(%rsp), %rdi
callq 0xaa80
movq $0x0, 0x88(%rsp)
movq 0x88(%rsp), %rax
cmpq 0xc8(%rsp), %rax
je 0x6a06e
leaq 0xd0(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0xd8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x88(%rsp), %rax
movq 0xc0(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x58(%rsp)
movq 0x88(%rsp), %rax
incq %rax
movq 0xc0(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x50(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xaa90
movq %rax, (%rsp)
jmp 0x69fba
movq (%rsp), %rcx
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x6a120
jmp 0x69fd7
movq 0x88(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0xaba0
movq %rax, %rdi
leaq 0x80(%rsp), %rsi
callq 0xabc0
leaq 0x80(%rsp), %rdi
callq 0xac00
movq 0x88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
jmp 0x69f23
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa7(%rsp), %rdi
callq 0xaa80
jmp 0x6a113
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0xacf0
jmp 0x6a113
leaq 0xa8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
callq 0xac20
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rdi
callq 0xac50
movq %rax, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xac80
testb $0x1, %al
jne 0x6a0ae
jmp 0x6a0d7
leaq 0x40(%rsp), %rdi
callq 0xacc0
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
callq 0x96c0
jmp 0x6a0c9
jmp 0x6a0cb
leaq 0x40(%rsp), %rdi
callq 0xacd0
jmp 0x6a099
movq 0xf8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movq 0xe8(%rsp), %rcx
callq 0x31fe0
jmp 0x6a0fe
leaq 0xa8(%rsp), %rdi
callq 0xacf0
addq $0x108, %rsp            # imm = 0x108
retq
movq 0x98(%rsp), %rdi
callq 0x9700