// File: /workspace/github_cpp_makefile_star2_O2/WeihuaZhu[P]hpc_prog2/mpi_jacobi.cpp
// Function: distribute_vector(int, double*, double**, ompi_communicator_t*)
void distribute_vector(const int n, double* input_vector, double** local_vector, 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 sendcounts and displs
    int *sendcounts = (int*) malloc(sizeof(int) * p);
    int *displs = (int*) malloc(sizeof(int) * p);
    if (rank == sourceRank) {
        for (int i = 0; i < q; i++) {
            for (int j = 0; j < q; j++) {
                if (j != 0) {
                    sendcounts[i * q + j] = 0;
                } else {
                    sendcounts[i * q + j] = block_decompose(n, q, i);
                }
                if (i == 0 && j == 0) {
                    displs[0] = 0;
                } else {
                    displs[i * q + j] = displs[i * q + j - 1] + sendcounts[i * q + j - 1];
                }
            }
        }
    }

    //perform the distribution using scatterv, and store the result in local_vector
    int count_first_col = 0;
    if (coords[1] == 0) { //the first column
        count_first_col = block_decompose(n, q, coords[0]);
    }
    *local_vector = (double*) malloc(sizeof(double) * count_first_col);
    MPI_Scatterv(input_vector, sendcounts, displs, MPI_DOUBLE, *local_vector, count_first_col, 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 $0x58, %rsp
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movl %edi, %ebp
leaq 0x4(%rsp), %rsi
movq %rcx, %rdi
callq 0x67d0
movq %rsp, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6600
cvtsi2sdl (%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
jb 0x1d448
sqrtsd %xmm0, %xmm0
jmp 0x1d44d
callq 0x63b0
cvttsd2si %xmm0, %r15d
pushq $0x2
popq %rsi
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x8(%rsp), %r8
movq %rbx, %rdi
callq 0x6430
leaq 0x40(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x3c(%rsp), %r14
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rdx
callq 0x6a10
movslq (%rsp), %r13
shlq $0x2, %r13
movq %r13, %rdi
callq 0x6750
movq %rax, %r12
movq %r13, %rdi
callq 0x6750
movq %rax, %r13
movl (%r14), %eax
movq %rax, 0x10(%rsp)
cmpl %eax, 0x4(%rsp)
jne 0x1d543
movslq %r15d, %rax
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %edi
cmovgl %r15d, %edi
movl %eax, %r8d
movq %rax, 0x30(%rsp)
leaq (,%rax,4), %r9
movq %r13, %r10
movq %r12, %r11
cmpq %rdi, %rsi
je 0x1d543
movq %rsi, %r14
imulq 0x30(%rsp), %r14
xorl %ebx, %ebx
cmpq %rbx, %r8
je 0x1d538
testq %rbx, %rbx
je 0x1d501
andl $0x0, (%r11,%rbx,4)
jmp 0x1d518
movl %ebp, %eax
cltd
idivl %r15d
movslq %edx, %rdx
xorl %ecx, %ecx
cmpq %rdx, %rsi
setl %cl
addl %eax, %ecx
movl %ecx, (%r12,%r14,4)
movl %ebx, %eax
orl %esi, %eax
je 0x1d52e
movl -0x4(%r11,%rbx,4), %eax
addl -0x4(%r10,%rbx,4), %eax
movl %eax, (%r10,%rbx,4)
jmp 0x1d533
andl $0x0, (%r13)
incq %rbx
jmp 0x1d4f0
incq %rsi
addq %r9, %r11
addq %r9, %r10
jmp 0x1d4e0
xorl %ecx, %ecx
cmpl $0x0, 0xc(%rsp)
jne 0x1d55d
movl %ebp, %eax
cltd
idivl %r15d
xorl %ecx, %ecx
cmpl 0x8(%rsp), %edx
setg %cl
addl %eax, %ecx
movslq %ecx, %r14
leaq (,%r14,8), %rdi
callq 0x6750
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
subq $0x8, %rsp
movq 0x13a28(%rip), %rcx     # 0x30fa8
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rax, %r8
movl %r14d, %r9d
pushq 0x30(%rsp)
pushq 0x20(%rsp)
pushq %rcx
callq 0x62b0
addq $0x20, %rsp
movq %r12, %rdi
callq 0x6650
movq %r13, %rdi
callq 0x6650
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq