// File: /workspace/github_c_makefile_star2_O2/kkanellis[P]matvec-sparse/matvec_mpi_bcast.c
// Function: main
int main(int argc, char * argv[])
{
    char *in_file,
         *out_file = NULL;

    double t, comp_time, partition_time;

    double *x; /* vector to be multiplied */

    int nprocs,     /* number of tasks/processes */
        rank;       /* id of task/process */
    
    /***** MPI MASTER (root) process only ******/
    proc_info_t *proc_info;

    int *buf_i_idx,     /* row index for all matrix elements */
        *buf_j_idx;     /* column index for all matrix elements */
    double *buf_values, /* value for all matrix elements */
           *buf_x,      /* value for all x vector elements */
           *res;        /* final result -> Ax */

    /*******************************************/

    /* Initialize MPI */
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    create_mpi_datatypes(&proc_info_type);

    /* master thread reads matrix */
    if (rank == MASTER) {
        /* read arguments */
        if (argc < 2 || argc > 3) {
            printf("Usage: %s input_file [output_file]\n", argv[0]);
            return 0;
        }
        else {
            in_file = argv[1];
            if (argc == 3) 
                out_file = argv[2];
        }

        /* initialize proc_info array */
        proc_info = (proc_info_t *)malloc_or_exit( nprocs * sizeof(proc_info_t) );
        
        /* read matrix */
        if ( read_matrix(in_file, &buf_i_idx, &buf_j_idx, &buf_values,
                            &proc_info[MASTER].N, &proc_info[MASTER].NZ) != 0) {
            fprintf(stderr, "read_matrix: failed\n");
            exit(EXIT_FAILURE);
        }

        debug("[%d] Read matrix from '%s'!\n", rank, in_file);
        debug("[%d] Matrix properties: N = %d, NZ = %d\n\n",rank, 
                                    proc_info[MASTER].N, proc_info[MASTER].NZ);

        /* initialize process info */
        for (int p = 0; p < nprocs; p++) {
            if (p != MASTER) {
                proc_info[p] = proc_info[MASTER];
            }
        }

        /* allocate x, res vector */
        buf_x = (double *)malloc_or_exit( proc_info[MASTER].N * sizeof(double) );
        res = (double *)malloc_or_exit( proc_info[MASTER].N * sizeof(double) );

        /* generate random vector */
        //random_vec(buf_x, N, MAX_RANDOM_NUM);
        for (int i = 0; i < proc_info[MASTER].N; i++) {
            buf_x[i] = 1;
        }

        /* divide work across processes */
        t = MPI_Wtime();
        if (policy == EQUAL_ROWS) {
            debug("[%d] Policy: Equal number of ROWS\n", rank);
            partition_equal_rows(proc_info, nprocs, buf_i_idx);
        }
        else if (policy == EQUAL_NZ) {
            debug("[%d] Policy: Equal number of NZ ENTRIES\n", rank);
            partition_equal_nz_elements(proc_info, nprocs, buf_i_idx);
        }
        else {
            fprintf(stderr, "Wrong policy defined...");
            exit(EXIT_FAILURE);
        }
        partition_time = (MPI_Wtime() - t) * 1000.0;

        debug("[%d] Partition time: %10.3lf ms\n\n", rank, partition_time);
        debug("[%d] Starting algorithm...\n", rank);
        t = MPI_Wtime();
    }

    /* Matrix-vector multiplication for each processes */
    res = mat_vec_mult_parallel(rank, nprocs, proc_info, buf_i_idx, 
                                buf_j_idx, buf_values, buf_x);

    /* write to output file */
    if (rank == MASTER) {
        comp_time = (MPI_Wtime() - t) * 1000.0; 
        printf("[%d] Computation time: %10.3lf ms\n\n", rank, comp_time);

        printf("[%d] Total execution time: %10.3lf ms\n", rank, comp_time + partition_time);
        debug("Finished!\n");
        if (out_file != NULL) {
            printf("Writing result to '%s'\n", out_file);

            /* open file */
            FILE *f;
            if ( !(f = fopen(out_file, "w")) ) {
                fprintf(stderr, "fopen: failed to open file '%s'", out_file);
                exit(EXIT_FAILURE);
            }

            /* write result */
            for (int i = 0; i < proc_info[MASTER].N; i++) {
                fprintf(f, "%.8lf\n", res[i]);
            }
            
            /* close file */
            if ( fclose(f) != 0) {
                fprintf(stderr, "fopen: failed to open file '%s'", out_file);
                exit(EXIT_FAILURE);
            }

            printf("Done!\n");
        }

        /* free the memory */
        free(buf_values);
        free(buf_i_idx);
        free(buf_j_idx);
        free(buf_x);
        free(res);
    }

    /* MPI: end */
    MPI_Finalize();

        
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
leaq 0x3c(%rsp), %rbx
movl %edi, (%rbx)
leaq 0x40(%rsp), %r14
movq %rsi, (%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10a0
movq 0x278a(%rip), %r15      # 0x4fe0
leaq 0xc(%rsp), %rsi
movq %r15, %rdi
callq 0x1100
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x1140
leaq 0x28c1(%rip), %rdi      # 0x5138
callq 0x27e3
movl (%rbx), %ecx
leal -0x4(%rcx), %edx
movq (%r14), %rax
cmpl $-0x3, %edx
ja 0x28ab
movq (%rax), %rsi
leaq 0xa8d(%rip), %rdi       # 0x3320
xorl %eax, %eax
callq 0x1030
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rax), %r15
cmpl $0x3, %ecx
jne 0x28ba
movq 0x10(%rax), %rbx
jmp 0x28bc
xorl %ebx, %ebx
movslq 0xc(%rsp), %rax
imulq $0x18, %rax, %rdi
callq 0x24ac
movq %rax, %r14
leaq 0x4(%rax), %r9
leaq 0x18(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movq %r15, %rdi
movq %rax, %r8
callq 0x1f40
movq 0x26f6(%rip), %r13      # 0x4fe8
movq (%r13), %rcx
testl %eax, %eax
jne 0x2ba5
movl 0x8(%rsp), %edx
leaq 0xa50(%rip), %rsi       # 0x3359
xorl %r12d, %r12d
movq %rcx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x1220
movq (%r13), %rdi
movl 0x8(%rsp), %edx
movl (%r14), %ecx
movl 0x4(%r14), %r8d
leaq 0xa47(%rip), %rsi       # 0x3376
xorl %eax, %eax
callq 0x1220
movl 0xc(%rsp), %eax
testl %eax, %eax
cmovlel %r12d, %eax
imulq $0x18, %rax, %rax
cmpq %r12, %rax
je 0x2968
testq %r12, %r12
je 0x2962
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%r14,%r12)
movupd (%r14), %xmm0
movupd %xmm0, (%r14,%r12)
addq $0x18, %r12
jmp 0x2944
movslq (%r14), %rdi
shlq $0x3, %rdi
callq 0x24ac
movq %rax, %r15
movslq (%r14), %rdi
shlq $0x3, %rdi
callq 0x24ac
movl (%r14), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
cmpq %rcx, %rax
je 0x29a5
movq %rdx, (%r15,%rcx,8)
incq %rcx
jmp 0x2997
callq 0x11e0
movsd %xmm0, 0x10(%rsp)
movl 0x2772(%rip), %eax      # 0x5128
movq (%r13), %rcx
testl %eax, %eax
je 0x29ef
cmpl $0x1, %eax
jne 0x2bb0
movl 0x8(%rsp), %edx
leaq 0x9f1(%rip), %rsi       # 0x33c3
movq %rcx, %rdi
xorl %eax, %eax
callq 0x1220
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x232b
jmp 0x2a15
movl 0x8(%rsp), %edx
leaq 0x9a6(%rip), %rsi       # 0x33a0
movq %rcx, %rdi
xorl %eax, %eax
callq 0x1220
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x2254
callq 0x11e0
subsd 0x10(%rsp), %xmm0
mulsd 0x8f0(%rip), %xmm0      # 0x3318
movq (%r13), %rdi
movl 0x8(%rsp), %edx
leaq 0x9cd(%rip), %rsi       # 0x3404
movsd %xmm0, 0x10(%rsp)
movb $0x1, %al
callq 0x1220
movq (%r13), %rdi
movl 0x8(%rsp), %edx
leaq 0x9d3(%rip), %rsi       # 0x3426
xorl %eax, %eax
callq 0x1220
callq 0x11e0
movsd %xmm0, 0x20(%rsp)
movl 0x8(%rsp), %edi
movq 0x18(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
movl 0xc(%rsp), %esi
movq %r15, (%rsp)
movq %r14, %rdx
callq 0x2520
cmpl $0x0, 0x8(%rsp)
je 0x2a99
callq 0x1170
jmp 0x289a
movq %rax, %r12
callq 0x11e0
subsd 0x20(%rsp), %xmm0
mulsd 0x869(%rip), %xmm0      # 0x3318
movsd %xmm0, 0x20(%rsp)
movl 0x8(%rsp), %esi
leaq 0x982(%rip), %rdi       # 0x3442
movb $0x1, %al
callq 0x1030
movl 0x8(%rsp), %esi
movsd 0x10(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
leaq 0x988(%rip), %rdi       # 0x3466
movb $0x1, %al
callq 0x1030
movq (%r13), %rcx
leaq 0x99d(%rip), %rdi       # 0x348d
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
testq %rbx, %rbx
je 0x2b72
leaq 0x991(%rip), %rdi       # 0x3498
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1030
leaq 0x668(%rip), %rsi       # 0x3180
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x1090
testq %rax, %rax
je 0x2bca
movq %rax, %r13
xorl %ebx, %ebx
leaq 0x996(%rip), %rbp       # 0x34d0
movslq (%r14), %rax
cmpq %rax, %rbx
jge 0x2b5a
movsd (%r12,%rbx,8), %xmm0
movq %r13, %rdi
movq %rbp, %rsi
movb $0x1, %al
callq 0x1220
incq %rbx
jmp 0x2b3a
movq %r13, %rdi
callq 0x11d0
testl %eax, %eax
jne 0x2bca
leaq 0x96a(%rip), %rdi       # 0x34d7
callq 0x1050
movq 0x28(%rsp), %rdi
callq 0x10d0
movq 0x18(%rsp), %rdi
callq 0x10d0
movq 0x30(%rsp), %rdi
callq 0x10d0
movq %r15, %rdi
callq 0x10d0
movq %r12, %rdi
callq 0x10d0
jmp 0x2a8f
leaq 0x798(%rip), %rdi       # 0x3344
pushq $0x14
jmp 0x2bb9
leaq 0x835(%rip), %rdi       # 0x33ec
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
pushq $0x1
popq %rdi
callq 0x1070
movq 0x2417(%rip), %rax      # 0x4fe8
movq (%rax), %rdi
leaq 0x8d5(%rip), %rsi       # 0x34b0
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1220
jmp 0x2bc2