// File: /workspace/github_cpp_makefile_star2_O3/WeihuaZhu[P]hpc_prog2/mpi_jacobi.cpp
// Function: gather_vector(int, double*, double*, ompi_communicator_t*)
void gather_vector(const int n, double* local_vector, double* output_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 *count_first_col = (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) {
                    count_first_col[i * q + j] = 0;
                } else {
                    count_first_col[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] + count_first_col[i * q + j - 1];
                }
            }
        }
    }

    // perform the gather operation using Gatherv, and store the result in output_vector
    int sendcounts = 0;
    if (coords[1] == 0) {
        sendcounts = block_decompose(n, q, coords[0]);
    }
    MPI_Gatherv(local_vector, sendcounts, MPI_DOUBLE, output_vector, count_first_col, displs, MPI_DOUBLE, sourceRank, comm);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%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 0x27332
sqrtsd %xmm0, %xmm0
jmp 0x27337
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 0x27424
testl %r15d, %r15d
jle 0x27424
movl %r15d, %ecx
leaq (,%rcx,4), %rsi
xorl %edi, %edi
movq %r13, %r8
movq %r12, %r9
movq %rdi, %r11
imulq %rcx, %r11
xorl %r14d, %r14d
testq %r14, %r14
je 0x273d5
movl $0x0, (%r9,%r14,4)
jmp 0x273ef
movl %ebp, %eax
cltd
idivl %r15d
movslq %edx, %rdx
xorl %r10d, %r10d
cmpq %rdx, %rdi
setl %r10b
addl %eax, %r10d
movl %r10d, (%r12,%r11,4)
movl %r14d, %eax
orl %edi, %eax
je 0x27406
movl -0x4(%r9,%r14,4), %eax
addl -0x4(%r8,%r14,4), %eax
movl %eax, (%r8,%r14,4)
jmp 0x2740e
movl $0x0, (%r13)
incq %r14
cmpq %r14, %rcx
jne 0x273c6
incq %rdi
addq %rsi, %r9
addq %rsi, %r8
cmpq %rcx, %rdi
jne 0x273bc
xorl %esi, %esi
cmpl $0x0, 0xc(%rsp)
jne 0x2743f
movl %ebp, %eax
cltd
idivl %r15d
xorl %esi, %esi
cmpl 0x8(%rsp), %edx
setg %sil
addl %eax, %esi
subq $0x8, %rsp
movq 0x11b56(%rip), %rdx     # 0x38fa0
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq %r12, %r8
movq %r13, %r9
pushq %rbx
pushq 0x20(%rsp)
pushq %rdx
callq 0x6a20
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