// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %rbx
movq %rdx, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movl %edi, %ebp
leaq 0x4(%rsp), %rsi
movq %rcx, %rdi
callq 0x6790
movq %rsp, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x65e0
cvtsi2sdl (%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x27772
sqrtsd %xmm0, %xmm0
jmp 0x27777
callq 0x63a0
cvttsd2si %xmm0, %r15d
leaq 0x40(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x8(%rsp), %r8
movq %rbx, %rdi
movl $0x2, %esi
callq 0x6420
leaq 0x30(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x2c(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rdx
callq 0x69f0
movslq (%rsp), %r13
shlq $0x2, %r13
movq %r13, %rdi
callq 0x6720
movq %rax, %r12
movq %r13, %rdi
callq 0x6720
movq %rax, %r13
movl (%r14), %eax
movq %rax, 0x10(%rsp)
cmpl %eax, 0x4(%rsp)
jne 0x27864
testl %r15d, %r15d
jle 0x27864
movl %r15d, %ecx
leaq (,%rcx,4), %rsi
xorl %edi, %edi
movq %r13, %r8
movq %r12, %r9
movq %rdi, %r10
imulq %rcx, %r10
xorl %r11d, %r11d
testq %r11, %r11
je 0x27815
movl $0x0, (%r9,%r11,4)
jmp 0x2782f
movl %ebp, %eax
cltd
idivl %r15d
movslq %edx, %rdx
xorl %r14d, %r14d
cmpq %rdx, %rdi
setl %r14b
addl %eax, %r14d
movl %r14d, (%r12,%r10,4)
movl %r11d, %eax
orl %edi, %eax
je 0x27846
movl -0x4(%r9,%r11,4), %eax
addl -0x4(%r8,%r11,4), %eax
movl %eax, (%r8,%r11,4)
jmp 0x2784e
movl $0x0, (%r13)
incq %r11
cmpq %r11, %rcx
jne 0x27806
incq %rdi
addq %rsi, %r9
addq %rsi, %r8
cmpq %rcx, %rdi
jne 0x277fc
xorl %ecx, %ecx
cmpl $0x0, 0xc(%rsp)
jne 0x2787e
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 0x6720
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
subq $0x8, %rsp
movq 0x116ff(%rip), %rcx     # 0x38fa0
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rax, %r8
movl %r14d, %r9d
pushq %rbx
pushq 0x20(%rsp)
pushq %rcx
callq 0x62a0
addq $0x20, %rsp
movq %r12, %rdi
callq 0x6630
movq %r13, %rdi
callq 0x6630
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq