// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x98, %rsp
movl $0x0, 0x94(%rsp)
movl %edi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq $0x0, 0x78(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x10a0
movq 0x2b6a(%rip), %rdi      # 0x5fe0
leaq 0x54(%rsp), %rsi
callq 0x1100
movq 0x2b59(%rip), %rdi      # 0x5fe0
leaq 0x50(%rsp), %rsi
callq 0x1130
leaq 0x2c90(%rip), %rdi      # 0x6128
callq 0x33e0
cmpl $0x0, 0x50(%rsp)
jne 0x378c
cmpl $0x2, 0x90(%rsp)
jl 0x34bc
cmpl $0x3, 0x90(%rsp)
jle 0x34e5
movq 0x88(%rsp), %rax
movq (%rax), %rsi
leaq 0xe42(%rip), %rdi       # 0x4310
movb $0x0, %al
callq 0x1030
movl $0x0, 0x94(%rsp)
jmp 0x395d
movq 0x88(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x80(%rsp)
cmpl $0x3, 0x90(%rsp)
jne 0x3514
movq 0x88(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x78(%rsp)
jmp 0x3516
movslq 0x54(%rsp), %rax
imulq $0x18, %rax, %rdi
callq 0x2f30
movq %rax, 0x48(%rsp)
movq 0x80(%rsp), %rdi
movq 0x48(%rsp), %r8
movq 0x48(%rsp), %r9
addq $0x4, %r9
leaq 0x40(%rsp), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x26b0
cmpl $0x0, %eax
je 0x357a
movq 0x2a89(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
leaq 0xdcb(%rip), %rsi       # 0x4334
movb $0x0, %al
callq 0x1200
movl $0x1, %edi
callq 0x1060
movq 0x2a67(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
movl 0x50(%rsp), %edx
movq 0x80(%rsp), %rcx
leaq 0xdb2(%rip), %rsi       # 0x4349
movb $0x0, %al
callq 0x1200
movq 0x2a43(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
movl 0x50(%rsp), %edx
movq 0x48(%rsp), %rax
movl (%rax), %ecx
movq 0x48(%rsp), %rax
movl 0x4(%rax), %r8d
leaq 0xda3(%rip), %rsi       # 0x4366
movb $0x0, %al
callq 0x1200
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x361e
cmpl $0x0, 0x1c(%rsp)
je 0x360f
movq 0x48(%rsp), %rax
movslq 0x1c(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x48(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x3611
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x35d2
movq 0x48(%rsp), %rax
movslq (%rax), %rdi
shlq $0x3, %rdi
callq 0x2f30
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movslq (%rax), %rdi
shlq $0x3, %rdi
callq 0x2f30
movq %rax, 0x20(%rsp)
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x3683
movq 0x28(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsd 0xc97(%rip), %xmm0      # 0x4308
movsd %xmm0, (%rax,%rcx,8)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x3652
callq 0x11d0
movsd %xmm0, 0x70(%rsp)
cmpl $0x0, 0x2a83(%rip)      # 0x6118
jne 0x36c8
movq 0x294a(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
movl 0x50(%rsp), %edx
leaq 0xce4(%rip), %rsi       # 0x4390
movb $0x0, %al
callq 0x1200
movq 0x48(%rsp), %rdi
movl 0x54(%rsp), %esi
movq 0x40(%rsp), %rdx
callq 0x2a70
jmp 0x3726
cmpl $0x1, 0x2a49(%rip)      # 0x6118
jne 0x3702
movq 0x2910(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
movl 0x50(%rsp), %edx
leaq 0xccd(%rip), %rsi       # 0x43b3
movb $0x0, %al
callq 0x1200
movq 0x48(%rsp), %rdi
movl 0x54(%rsp), %esi
movq 0x40(%rsp), %rdx
callq 0x2be0
jmp 0x3724
movq 0x28df(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
leaq 0xcc9(%rip), %rsi       # 0x43dc
movb $0x0, %al
callq 0x1200
movl $0x1, %edi
callq 0x1060
jmp 0x3726
callq 0x11d0
subsd 0x70(%rsp), %xmm0
movsd 0xbc7(%rip), %xmm1      # 0x4300
mulsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x289e(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
movl 0x50(%rsp), %edx
movsd 0x60(%rsp), %xmm0
leaq 0xc96(%rip), %rsi       # 0x43f4
movb $0x1, %al
callq 0x1200
movq 0x287c(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
movl 0x50(%rsp), %edx
leaq 0xc9c(%rip), %rsi       # 0x4416
movb $0x0, %al
callq 0x1200
callq 0x11d0
movsd %xmm0, 0x70(%rsp)
movl 0x50(%rsp), %edi
movl 0x54(%rsp), %esi
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
callq 0x3020
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x50(%rsp)
jne 0x394d
callq 0x11d0
subsd 0x70(%rsp), %xmm0
movsd 0xb27(%rip), %xmm1      # 0x4300
mulsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x50(%rsp), %esi
movsd 0x68(%rsp), %xmm0
leaq 0xc3e(%rip), %rdi       # 0x4432
movb $0x1, %al
callq 0x1030
movl 0x50(%rsp), %esi
movsd 0x68(%rsp), %xmm0
addsd 0x60(%rsp), %xmm0
leaq 0xc44(%rip), %rdi       # 0x4456
movb $0x1, %al
callq 0x1030
movq 0x27c8(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
leaq 0xc53(%rip), %rsi       # 0x447d
movb $0x0, %al
callq 0x1200
cmpq $0x0, 0x78(%rsp)
je 0x391b
movq 0x78(%rsp), %rsi
leaq 0xc3f(%rip), %rdi       # 0x4488
movb $0x0, %al
callq 0x1030
movq 0x78(%rsp), %rdi
leaq 0x924(%rip), %rsi       # 0x4180
callq 0x1090
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
jne 0x3893
movq 0x2775(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
movq 0x78(%rsp), %rdx
leaq 0xc1e(%rip), %rsi       # 0x44a0
movb $0x0, %al
callq 0x1200
movl $0x1, %edi
callq 0x1060
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x38d7
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rax
movslq 0xc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0xbfd(%rip), %rsi       # 0x44c0
movb $0x1, %al
callq 0x1200
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x389b
movq 0x10(%rsp), %rdi
callq 0x11c0
cmpl $0x0, %eax
je 0x390d
movq 0x26fb(%rip), %rax      # 0x5fe8
movq (%rax), %rdi
movq 0x78(%rsp), %rdx
leaq 0xba4(%rip), %rsi       # 0x44a0
movb $0x0, %al
callq 0x1200
movl $0x1, %edi
callq 0x1060
leaq 0xbb3(%rip), %rdi       # 0x44c7
movb $0x0, %al
callq 0x1030
movq 0x30(%rsp), %rdi
callq 0x10d0
movq 0x40(%rsp), %rdi
callq 0x10d0
movq 0x38(%rsp), %rdi
callq 0x10d0
movq 0x28(%rsp), %rdi
callq 0x10d0
movq 0x20(%rsp), %rdi
callq 0x10d0
callq 0x1170
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq