// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rdx, 0x48(%rsp)
movq %rsi, 0x58(%rsp)
movl %edi, 0xc(%rsp)
leaq 0x44(%rsp), %rsi
movq %rcx, %rdi
callq 0x6790
leaq 0x54(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x65e0
cvtsi2sdl (%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x274db
sqrtsd %xmm0, %xmm0
jmp 0x274e0
callq 0x63a0
cvttsd2si %xmm0, %r15d
leaq 0x90(%rsp), %rdx
leaq 0x88(%rsp), %rcx
leaq 0x60(%rsp), %r8
movq %r12, %rdi
movl $0x2, %esi
callq 0x6420
leaq 0x80(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x40(%rsp), %r13
movq %r12, %rdi
movq %r13, %rdx
callq 0x69f0
leaq 0x78(%rsp), %rbx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r14
movq %r12, %rdi
movl $0x2, %esi
movq %rbx, %rdx
movq %r14, %r8
callq 0x6420
movl 0xc(%rsp), %ebp
movl %ebp, %eax
cltd
idivl (%rbx)
xorl %ecx, %ecx
cmpl (%r14), %edx
setg %cl
addl %eax, %ecx
movl %ecx, 0x20(%rsp)
leaq 0x78(%rsp), %rbx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r14
movq %r12, %rdi
movl $0x2, %esi
movq %rbx, %rdx
movq %r14, %r8
callq 0x6420
movl %ebp, %eax
movl %r15d, %ebp
cltd
idivl 0x4(%rbx)
xorl %ecx, %ecx
cmpl 0x4(%r14), %edx
setg %cl
addl %eax, %ecx
movl %ecx, 0x28(%rsp)
movslq 0x54(%rsp), %rbx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x6720
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x6720
movq %rax, %rdx
movl $0x0, (%rax)
movl 0x44(%rsp), %eax
cmpl (%r13), %eax
movl %r15d, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
jne 0x27702
testl %ebp, %ebp
jle 0x27702
movl %ebp, %ecx
leaq (,%rcx,4), %rsi
xorl %edi, %edi
movq 0x18(%rsp), %r8
movq %rdx, %r9
xorl %r10d, %r10d
testq %r10, %r10
je 0x27646
movq %r10, %r11
imulq %rcx, %r11
movq 0x18(%rsp), %rax
movl -0x4(%rax,%r11,4), %ebx
addl -0x4(%rdx,%r11,4), %ebx
movl 0xc(%rsp), %r15d
movl %r15d, %eax
movq %rdx, %r13
cltd
idivl %ebp
movslq %edx, %rdx
xorl %r14d, %r14d
cmpq %rdx, %r10
setle %r14b
leal -0x1(%rax,%r14), %ebp
setl %dl
imull %r15d, %ebp
addl %ebx, %ebp
movl %ebp, (%r13,%r11,4)
movl 0x10(%rsp), %ebp
jmp 0x27652
movl 0xc(%rsp), %eax
cltd
idivl %ebp
testl %edx, %edx
setg %dl
movzbl %dl, %edx
negl %edx
xorl %r11d, %r11d
cmpl %edx, %eax
je 0x27671
movl 0xc(%rsp), %eax
cltd
idivl %ebp
xorl %r11d, %r11d
testl %edx, %edx
setg %r11b
addl %eax, %r11d
movq %r10, %rax
imulq %rcx, %rax
movq 0x18(%rsp), %rdx
movl %r11d, (%rdx,%rax,4)
cmpl $0x2, %ebp
jb 0x276e7
movl 0xc(%rsp), %eax
cltd
idivl 0x10(%rsp)
movq %rsi, %r14
imulq %r10, %r14
movslq %edx, %rdx
xorl %r11d, %r11d
cmpq %rdx, %r10
setl %r11b
negl %r11d
movq 0x30(%rsp), %rbx
movl (%rbx,%r14), %ebx
movq 0x18(%rsp), %r15
movl (%r15,%r14), %ebp
movl $0x1, %r14d
xorl %r15d, %r15d
cmpq %rdx, %r14
setl %r15b
addl %eax, %r15d
cmpl %r11d, %eax
cmovel %edi, %r15d
movl %r15d, (%r8,%r14,4)
addl %ebp, %ebx
movl %ebx, (%r9,%r14,4)
incq %r14
movl %r15d, %ebp
cmpq %r14, %rcx
jne 0x276be
incq %r10
addq %rsi, %r9
addq %rsi, %r8
cmpq %rcx, %r10
movl 0x10(%rsp), %ebp
movq 0x30(%rsp), %rdx
jne 0x275f7
movl 0x28(%rsp), %ebx
movl %ebx, %eax
movl 0x20(%rsp), %r14d
imull %r14d, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x6720
movl %ebx, %r9d
movq 0x48(%rsp), %rcx
movq %rax, (%rcx)
testl %r14d, %r14d
cmovel %r14d, %r9d
movl 0xc(%rsp), %eax
cltd
idivl %ebp
movl %edx, 0x14(%rsp)
movl %eax, 0x3c(%rsp)
testl %eax, %eax
movq %r12, %r14
movq 0x18(%rsp), %r13
jle 0x277b8
movslq 0xc(%rsp), %rax
movslq %r9d, %rdx
movl 0x3c(%rsp), %r15d
shlq $0x3, %rdx
movq %rdx, 0x20(%rsp)
shlq $0x3, %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
movq 0x58(%rsp), %rbx
movq (%rcx), %r8
addq %r12, %r8
movl 0x40(%rsp), %eax
subq $0x8, %rsp
movq %rbx, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rdx
movq 0x1180f(%rip), %rcx     # 0x38fa0
movl %r9d, %ebp
pushq %r14
pushq %rax
pushq %rcx
callq 0x62a0
movq 0x68(%rsp), %rcx
movl %ebp, %r9d
addq $0x20, %rsp
addq 0x20(%rsp), %r12
addq 0x28(%rsp), %rbx
decq %r15
jne 0x27771
movq %r14, 0x20(%rsp)
cmpl $0x0, 0x14(%rsp)
movq %r13, %rbx
je 0x27896
movl %r9d, 0x28(%rsp)
cmpl $0x0, 0x44(%rsp)
movl 0x10(%rsp), %r12d
movq 0x30(%rsp), %r13
jne 0x27838
testl %r12d, %r12d
js 0x27838
cmpl $0x1, %r12d
movl %r12d, %r14d
adcl $0x0, %r14d
shlq $0x2, %r14
movl %r12d, %r15d
movl 0x14(%rsp), %ebp
imull %ebp, %r15d
movslq %r15d, %r15
leaq (%rbx,%r15,4), %rdi
leaq (,%r15,4), %rbx
addq %r13, %rbx
xorl %esi, %esi
movq %r14, %rdx
callq 0x6280
movq %rbx, %rdi
movq 0x18(%rsp), %rbx
xorl %esi, %esi
movq %r14, %rdx
callq 0x6280
incl %ebp
addl %r12d, %r15d
cmpl %r12d, %ebp
jl 0x27800
xorl %r9d, %r9d
movl 0x14(%rsp), %eax
cmpl %eax, 0x60(%rsp)
cmovll 0x28(%rsp), %r9d
movl 0xc(%rsp), %eax
movl 0x3c(%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 0x48(%rsp), %rax
addq (%rax), %r8
movl 0x40(%rsp), %eax
subq $0x8, %rsp
movq 0x1171f(%rip), %rcx     # 0x38fa0
movq %rbx, %rsi
movq %r13, %rdx
pushq 0x28(%rsp)
pushq %rax
pushq %rcx
callq 0x62a0
addq $0x20, %rsp
movq %rbx, %rdi
callq 0x6630
movq 0x30(%rsp), %rdi
callq 0x6630
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq