// File: /workspace/github_cpp_makefile_star2_O1/WeihuaZhu[P]hpc_prog2/mpi_jacobi.cpp
// Function: distribute_matrix(int, double*, double**, ompi_communicator_t*)
void distribute_matrix(const int n, double* input_matrix, double** local_matrix, MPI_Comm comm)
{
    // obtain the necessary parameters required in thie method
    int rank;
    MPI_Comm_rank(comm, &rank);
    int p;
    MPI_Comm_size(comm, &p);
    int q = sqrt(p);
    int dims[2];
    int coords[2];
    int periods[2];
    MPI_Cart_get(comm, 2, dims, periods, coords);
    int sourceRank;
    int sourceCoord[2] = {0, 0};
    MPI_Cart_rank(comm, sourceCoord, &sourceRank);

    //initialization of the dimention size in each processor
    int count_dim_0 = block_decompose_by_dim(n, comm, 0);
    int count_dim_1 = block_decompose_by_dim(n, comm, 1);

    //initialization of sendcounts and displs
    int *sendcounts = (int*) malloc(sizeof(int) * p);
    int *displs = (int*) malloc(sizeof(int) * p);
    displs[0] = 0;
    if (rank == sourceRank) {
        for (int i = 0; i < q; i++) {
            if (i != 0) {
                displs[i * q] = displs[i * q - 1] + sendcounts[i * q - 1] +
                    (block_decompose(n, q, i - 1) - 1) * n;
            }
            sendcounts[i * q] = (block_decompose(n, q, i) == 0) ? 0 : block_decompose(n, q, 0); //这里已经改了一些
            for (int j = 1; j < q; j++) {
                sendcounts[i * q + j] = (block_decompose(n, q, i) == 0) ? 0 : block_decompose(n, q, j);
                displs[i * q + j] = displs[i * q + j - 1] + sendcounts[i * q + j - 1];
            }
        }
    }

    //perform the matrix distribution for the first n/p lines in each processor
    *local_matrix = (double*) malloc(sizeof(double) * (count_dim_1 * count_dim_0));
    //In each iteration, send n/q lines of data in input matrix to the target place
    int rec_count = (count_dim_0 == 0) ? 0 : count_dim_1;
    for (int iter = 0; iter < n / q; iter++) {
        double *sendbuff = input_matrix + n * iter;
        double *rec_buff = *local_matrix + rec_count * iter;
        MPI_Scatterv(sendbuff, sendcounts, displs, MPI_DOUBLE, rec_buff, rec_count, MPI_DOUBLE, sourceRank, comm);
    }

    //deal with the leftover lines in the processors whose has the value: n % q != 0
    if (n % q != 0) {
        if (rank == 0) {
            for (int i = n % q; i < q; i++) {
                for (int j = 0; j < q; j++) {
                    sendcounts[i * q + j] = 0;
                    displs[i * q + j] = 0;
                }
            }
        }
        if (coords[0] >= n % q) rec_count = 0;
        int iter = n / q;
        double *sendbuff = input_matrix + n * iter;
        double *rec_buff = *local_matrix + rec_count * iter;
        MPI_Scatterv(sendbuff, sendcounts, displs, MPI_DOUBLE, rec_buff, rec_count, MPI_DOUBLE, sourceRank, comm);
    }

    // free the useless memory
    free(sendcounts);
    free(displs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, %r15
movq %rdx, 0x40(%rsp)
movq %rsi, 0x58(%rsp)
movl %edi, 0x8(%rsp)
leaq 0x34(%rsp), %rsi
movq %rcx, %rdi
callq 0x6790
leaq 0x54(%rsp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x65e0
cvtsi2sdl (%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x27ad7
sqrtsd %xmm0, %xmm0
jmp 0x27adc
callq 0x63a0
cvttsd2si %xmm0, %r13d
leaq 0x90(%rsp), %rdx
leaq 0x88(%rsp), %rcx
leaq 0x60(%rsp), %r8
movq %r15, %rdi
movl $0x2, %esi
callq 0x6420
leaq 0x80(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x14(%rsp), %rdx
movq %r15, %rdi
callq 0x69f0
leaq 0x78(%rsp), %rbx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r14
movq %r15, %rdi
movl $0x2, %esi
movq %rbx, %rdx
movq %r14, %r8
callq 0x6420
movl 0x8(%rsp), %ebp
movl %ebp, %eax
cltd
idivl (%rbx)
xorl %ecx, %ecx
cmpl (%r14), %edx
setg %cl
addl %eax, %ecx
movl %ecx, 0xc(%rsp)
leaq 0x78(%rsp), %rbx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r14
movq %r15, 0x48(%rsp)
movq %r15, %rdi
movl $0x2, %esi
movq %rbx, %rdx
movq %r14, %r8
callq 0x6420
movl %ebp, %eax
cltd
idivl 0x4(%rbx)
xorl %ecx, %ecx
cmpl 0x4(%r14), %edx
setg %cl
addl %eax, %ecx
movl %ecx, 0x18(%rsp)
movslq 0x54(%rsp), %rbx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x6720
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
callq 0x6720
movq %rax, %r12
movl $0x0, (%rax)
movl 0x34(%rsp), %eax
leaq 0x14(%rsp), %rcx
cmpl (%rcx), %eax
movl %r13d, 0x10(%rsp)
movq %r12, 0x28(%rsp)
jne 0x27ce3
testl %r13d, %r13d
jle 0x27ce3
movl 0x8(%rsp), %eax
cltd
idivl %r13d
movl %eax, %ecx
xorl %eax, %eax
testl %edx, %edx
setg %al
addl %ecx, %eax
movl %eax, 0x38(%rsp)
movslq %edx, %rdi
movl %r13d, %r8d
leaq (,%r8,4), %r9
xorl %r10d, %r10d
movq 0x20(%rsp), %r11
movq %r12, %rbx
xorl %r14d, %r14d
testq %r14, %r14
movq 0x20(%rsp), %rsi
je 0x27c60
movq %r14, %r15
imulq %r8, %r15
movl -0x4(%rsi,%r15,4), %ebp
addl -0x4(%r12,%r15,4), %ebp
movl 0x8(%rsp), %r13d
movl %r13d, %eax
cltd
idivl 0x10(%rsp)
movslq %edx, %rdx
xorl %r12d, %r12d
cmpq %rdx, %r14
setle %r12b
addl %r12d, %eax
decl %eax
imull %r13d, %eax
movl 0x10(%rsp), %r13d
addl %ebp, %eax
movq 0x28(%rsp), %rdx
movl %eax, (%rdx,%r15,4)
xorl %eax, %eax
cmpq %rdi, %r14
setl %al
negl %eax
cmpl %eax, %ecx
movl 0x38(%rsp), %edx
cmovel %r10d, %edx
movq %r14, %r15
imulq %r8, %r15
movl %edx, (%rsi,%r15,4)
cmpl $0x2, %r13d
jl 0x27ccc
movq %r9, %r15
imulq %r14, %r15
movq 0x28(%rsp), %rdx
movl (%rdx,%r15), %edx
movq 0x20(%rsp), %rsi
movl (%rsi,%r15), %ebp
movl $0x1, %r15d
xorl %r12d, %r12d
cmpq %rdi, %r15
setl %r12b
addl %ecx, %r12d
cmpl %eax, %ecx
cmovel %r10d, %r12d
movl %r12d, (%r11,%r15,4)
addl %ebp, %edx
movl %edx, (%rbx,%r15,4)
incq %r15
movl %r12d, %ebp
cmpq %r15, %r8
jne 0x27ca4
incq %r14
addq %r9, %rbx
addq %r9, %r11
cmpq %r8, %r14
movq 0x28(%rsp), %r12
jne 0x27c12
movl 0x18(%rsp), %r15d
movl %r15d, %eax
movl 0xc(%rsp), %ebp
imull %ebp, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x6720
movq 0x40(%rsp), %rcx
movq %rax, (%rcx)
testl %ebp, %ebp
cmovel %ebp, %r15d
movl 0x8(%rsp), %eax
cltd
idivl %r13d
movl %edx, 0xc(%rsp)
movl %eax, 0x30(%rsp)
testl %eax, %eax
movq 0x48(%rsp), %r10
movq 0x20(%rsp), %rbp
jle 0x27d9b
movq %r12, %r14
movslq 0x8(%rsp), %rax
movslq %r15d, %rdx
movl 0x30(%rsp), %r13d
shlq $0x3, %rdx
movq %rdx, 0x18(%rsp)
shlq $0x3, %rax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
movq 0x58(%rsp), %rbx
movq (%rcx), %r8
addq %r12, %r8
movl 0x14(%rsp), %eax
subq $0x8, %rsp
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq 0x1122e(%rip), %rcx     # 0x38fa0
movl %r15d, %r9d
pushq %r10
pushq %rax
pushq %rcx
callq 0x62a0
movq 0x68(%rsp), %r10
movq 0x60(%rsp), %rcx
addq $0x20, %rsp
addq 0x18(%rsp), %r12
addq 0x38(%rsp), %rbx
decq %r13
jne 0x27d54
cmpl $0x0, 0xc(%rsp)
movq %rbp, %rbx
je 0x27e70
movl %r15d, 0x18(%rsp)
cmpl $0x0, 0x34(%rsp)
movl 0x10(%rsp), %r13d
movq 0x28(%rsp), %r12
jne 0x27e12
testl %r13d, %r13d
js 0x27e12
cmpl $0x2, %r13d
movl $0x1, %r14d
cmovgel %r13d, %r14d
shlq $0x2, %r14
movl %r13d, %r15d
movl 0xc(%rsp), %ebp
imull %ebp, %r15d
movslq %r15d, %r15
leaq (%rbx,%r15,4), %rdi
leaq (%r12,%r15,4), %rbx
xorl %esi, %esi
movq %r14, %rdx
callq 0x6280
movq %rbx, %rdi
movq 0x20(%rsp), %rbx
xorl %esi, %esi
movq %r14, %rdx
callq 0x6280
incl %ebp
addl %r13d, %r15d
cmpl %r13d, %ebp
jl 0x27de1
xorl %r9d, %r9d
movl 0xc(%rsp), %eax
cmpl %eax, 0x60(%rsp)
cmovll 0x18(%rsp), %r9d
movl 0x8(%rsp), %eax
movl 0x30(%rsp), %edx
imull %edx, %eax
cltq
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax,8), %rdi
imull %r9d, %edx
movslq %edx, %r8
shlq $0x3, %r8
movq 0x40(%rsp), %rax
addq (%rax), %r8
movl 0x14(%rsp), %eax
subq $0x8, %rsp
movq 0x11145(%rip), %rcx     # 0x38fa0
movq %rbx, %rsi
movq %r12, %rdx
pushq 0x50(%rsp)
pushq %rax
pushq %rcx
callq 0x62a0
addq $0x20, %rsp
movq %rbx, %rdi
callq 0x6630
movq 0x28(%rsp), %rdi
callq 0x6630
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq