// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rcx, %rbx
movq %rdx, 0x30(%rsp)
movq %rsi, 0x50(%rsp)
movl %edi, %r12d
leaq 0x14(%rsp), %rsi
movq %rcx, %rdi
callq 0x67d0
leaq 0x4c(%rsp), %r14
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6600
cvtsi2sdl (%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1d7cd
sqrtsd %xmm0, %xmm0
jmp 0x1d7d2
callq 0x63b0
cvttsd2si %xmm0, %ebx
pushq $0x2
popq %rsi
leaq 0x70(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x58(%rsp), %r8
movq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x6430
leaq 0x60(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x10(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rdx
callq 0x6a10
xorl %r13d, %r13d
movl %r12d, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1e2fc
movl %eax, 0x28(%rsp)
pushq $0x1
popq %rdx
movl %r12d, %edi
movq %r14, %rsi
callq 0x1e2fc
movl %eax, 0xc(%rsp)
movslq 0x4c(%rsp), %r14
shlq $0x2, %r14
movq %r14, %rdi
callq 0x6750
movq %rax, 0x20(%rsp)
movq %r14, %rdi
callq 0x6750
movq %rax, 0x18(%rsp)
andl $0x0, (%rax)
movl 0x14(%rsp), %eax
movslq %ebx, %r15
cmpl (%rbp), %eax
jne 0x1d94b
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
pushq $0x1
popq %rsi
xorl %edi, %edi
cmpq %rcx, %rdi
je 0x1d94b
testq %rdi, %rdi
je 0x1d8c9
movq %rdi, %r9
imulq %r15, %r9
movq 0x20(%rsp), %r14
movl -0x4(%r14,%r9,4), %r10d
movq 0x18(%rsp), %rbp
addl -0x4(%rbp,%r9,4), %r10d
movl %r12d, %eax
cltd
idivl %ebx
movslq %edx, %r8
xorl %r11d, %r11d
cmpq %r8, %rdi
setle %r11b
addl %eax, %r11d
decl %r11d
imull %r12d, %r11d
addl %r10d, %r11d
movl %r11d, (%rbp,%r9,4)
jmp 0x1d8df
movl %r12d, %eax
cltd
idivl %ebx
movslq %edx, %r8
xorl %r9d, %r9d
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %rbp
xorl %r10d, %r10d
cmpq %rdi, %r8
setg %r10b
negl %r10d
xorl %r11d, %r11d
testl %edx, %edx
setg %r11b
addl %eax, %r11d
cmpl %r10d, %eax
cmovel %r13d, %r11d
movl %r11d, (%r14,%r9,4)
leaq (,%r9,4), %rdx
addq %rbp, %rdx
leaq (%r14,%r9,4), %r9
movq %rsi, %r11
cmpq %r15, %r11
jge 0x1d943
xorl %ebp, %ebp
cmpq %r11, %r8
setg %bpl
addl %eax, %ebp
cmpl %r10d, %eax
cmovel %r13d, %ebp
movl %ebp, (%r9,%r11,4)
movl -0x4(%r9,%r11,4), %ebp
addl -0x4(%rdx,%r11,4), %ebp
movl %ebp, (%rdx,%r11,4)
incq %r11
jmp 0x1d915
incq %rdi
jmp 0x1d879
movl 0xc(%rsp), %ebp
movl %ebp, %eax
movl 0x28(%rsp), %r14d
imull %r14d, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x6750
testl %r14d, %r14d
cmovel %r14d, %ebp
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
movl %r12d, %eax
cltd
idivl %ebx
movl %edx, 0x48(%rsp)
movl %ebp, 0xc(%rsp)
movslq %ebp, %rcx
movl %r12d, 0x44(%rsp)
movslq %r12d, %rdx
testl %eax, %eax
movl $0x0, %ebp
movl %eax, 0x40(%rsp)
cmovgl %eax, %ebp
shlq $0x3, %rdx
movq %rdx, 0x28(%rsp)
shlq $0x3, %rcx
movq %rcx, %r12
movq 0x50(%rsp), %r14
subq $0x1, %rbp
jb 0x1d9fc
movq 0x30(%rsp), %rax
movq (%rax), %r8
addq %r13, %r8
movl 0x10(%rsp), %eax
subq $0x8, %rsp
movq %r14, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x135ca(%rip), %rcx     # 0x30fa8
movl 0x14(%rsp), %r9d
pushq 0x40(%rsp)
pushq %rax
pushq %rcx
callq 0x62b0
addq $0x20, %rsp
addq 0x28(%rsp), %r14
addq %r12, %r13
jmp 0x1d9b1
movl 0x48(%rsp), %r10d
testl %r10d, %r10d
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r12
je 0x1da75
cmpl $0x0, 0x14(%rsp)
movl 0x44(%rsp), %r8d
je 0x1da94
xorl %r9d, %r9d
cmpl %r10d, 0x58(%rsp)
cmovll 0xc(%rsp), %r9d
movl 0x40(%rsp), %edx
imull %edx, %r8d
movslq %r8d, %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %rdi
imull %r9d, %edx
movslq %edx, %r8
shlq $0x3, %r8
movq 0x30(%rsp), %rax
addq (%rax), %r8
movl 0x10(%rsp), %eax
subq $0x8, %rsp
movq %r14, %rsi
movq %r12, %rdx
movq 0x13542(%rip), %rcx     # 0x30fa8
pushq 0x40(%rsp)
pushq %rax
pushq %rcx
callq 0x62b0
addq $0x20, %rsp
movq %r14, %rdi
callq 0x6650
movq %r12, %rdi
callq 0x6650
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
movslq %r10d, %rax
movq %r15, %rsi
imulq %rax, %rsi
leaq (%r14,%rsi,4), %rcx
leaq (,%r15,4), %rdx
leaq (%r12,%rsi,4), %rsi
cmpq %r15, %rax
jge 0x1da1c
xorl %edi, %edi
cmpq %rdi, %rbx
je 0x1dad2
andl $0x0, (%rcx,%rdi,4)
andl $0x0, (%rsi,%rdi,4)
incq %rdi
jmp 0x1dac0
incq %rax
addq %rdx, %rcx
addq %rdx, %rsi
jmp 0x1dab5