// File: 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:
subq $0x88, %rsp
movl %edi, 0x84(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0xf050
movq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xece0
movl 0x60(%rsp), %edi
callq 0x119d0
cvttsd2si %xmm0, %eax
movl %eax, 0x5c(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x54(%rsp), %rdx
leaq 0x44(%rsp), %rcx
leaq 0x4c(%rsp), %r8
movl $0x2, %esi
callq 0xe8f0
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0xe510
movq 0x68(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0xf4b0
movslq 0x60(%rsp), %rdi
shlq $0x2, %rdi
callq 0xef20
movq %rax, 0x30(%rsp)
movslq 0x60(%rsp), %rdi
shlq $0x2, %rdi
callq 0xef20
movq %rax, 0x28(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x40(%rsp), %eax
jne 0x4cd41
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x4cd3f
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x4cd2d
cmpl $0x0, 0x20(%rsp)
je 0x4cc84
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x4ccb2
movl 0x84(%rsp), %edi
movl 0x5c(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0x4e0e0
movl %eax, %edx
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, 0x24(%rsp)
jne 0x4cccd
cmpl $0x0, 0x20(%rsp)
jne 0x4cccd
movq 0x28(%rsp), %rax
movl $0x0, (%rax)
jmp 0x4cd1b
movq 0x28(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movq 0x28(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x4cd1d
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x4cc51
jmp 0x4cd2f
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x4cc3b
jmp 0x4cd41
movl $0x0, 0x1c(%rsp)
cmpl $0x0, 0x50(%rsp)
jne 0x4cd68
movl 0x84(%rsp), %edi
movl 0x5c(%rsp), %esi
movl 0x4c(%rsp), %edx
callq 0x4e0e0
movl %eax, 0x1c(%rsp)
movslq 0x1c(%rsp), %rdi
shlq $0x3, %rdi
callq 0xef20
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq %rcx, (%rax)
movq 0x78(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rax
movq (%rax), %r8
movl 0x1c(%rsp), %r9d
movl 0x40(%rsp), %r10d
movq 0x68(%rsp), %rax
movq 0x1d1fa(%rip), %r11     # 0x69fa8
movq %r11, %rcx
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xe530
movq 0x30(%rsp), %rdi
callq 0xed30
movq 0x28(%rsp), %rdi
callq 0xed30
addq $0x88, %rsp
retq