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

    double t, comp_time, partition_time;
    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;
        }

        t = MPI_Wtime();
        
        /* divide work across processes */
        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);
    }

    /* 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);
    
    double stdev = 0, mean = 0, runs[TOTAL_RUNS];
    for (int r = 0; r < TOTAL_RUNS; r++) {
        MPI_Barrier(MPI_COMM_WORLD);
        if (rank == MASTER) t = MPI_Wtime();
        res = mat_vec_mult_parallel(rank, nprocs, proc_info, buf_i_idx, 
                                buf_j_idx, buf_values, buf_x);
        //MPI_Barrier(MPI_COMM_WORLD);
        if (rank == MASTER){
            runs[r] = (MPI_Wtime() - t) * 1000.0; 
            mean += runs[r];
        }
    }

    /* print execution stats */
    if (rank == MASTER) {
        mean /= TOTAL_RUNS; 
        for (int r = 0; r < TOTAL_RUNS; r++) {
            stdev += (runs[r] - mean) * (runs[r] - mean);
        }
        stdev = sqrt(stdev);

        printf("[%d] Computation time: %10.3lf [%4.3lf] ms\n\n", rank, mean, stdev);
        printf("[%d] Total execution time: %10.3lf ms\n", rank, mean + partition_time);
        debug("Finished!\n");
    }

    /* write to output file */
    if (rank == MASTER) {
        
        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 $0x378, %rsp            # imm = 0x378
leaq 0x3c(%rsp), %rax
movl %edi, (%rax)
leaq 0x48(%rsp), %rcx
movq %rsi, (%rcx)
movq %rax, %rdi
movq %rcx, %rsi
callq 0x2100
movq 0x3f53(%rip), %rbx      # 0x7fd8
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0x2160
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21b0
leaq 0x40d7(%rip), %rdi      # 0x8180
callq 0x400a
cmpl $0x0, (%r14)
je 0x40bb
xorl %ebx, %ebx
jmp 0x427f
movl 0x3c(%rsp), %ecx
leal -0x4(%rcx), %edx
movq 0x48(%rsp), %rax
cmpl $-0x3, %edx
ja 0x40e2
movq (%rax), %rsi
leaq 0x12f1(%rip), %rdi      # 0x53c7
xorl %eax, %eax
callq 0x2040
jmp 0x447d
movq 0x8(%rax), %r15
cmpl $0x3, %ecx
jne 0x40f1
movq 0x10(%rax), %rbx
jmp 0x40f3
xorl %ebx, %ebx
movslq 0xc(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x36cb
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 0x3178
testl %eax, %eax
jne 0x44a5
movslq 0xc(%rsp), %rax
testq %rax, %rax
jle 0x4164
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x415b
movq 0x10(%r14), %rdx
movq %rdx, 0x10(%r14,%rcx)
movdqu (%r14), %xmm0
movdqu %xmm0, (%r14,%rcx)
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x4142
movslq (%r14), %rdi
shlq $0x3, %rdi
callq 0x36cb
movq %rax, %r15
movslq (%r14), %rdi
shlq $0x3, %rdi
callq 0x36cb
movslq (%r14), %rcx
testq %rcx, %rcx
jle 0x4224
leal 0x1(%rcx), %eax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %eax
shlq $0x4, %rax
movdqa 0x1157(%rip), %xmm1     # 0x5300
xorl %ecx, %ecx
movdqa 0x115d(%rip), %xmm2     # 0x5310
pxor %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
pcmpeqd %xmm3, %xmm3
movdqa 0x1153(%rip), %xmm4     # 0x5320
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x4203
movq %rdx, (%r15,%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x4217
movq %rdx, 0x8(%r15,%rcx)
paddq %xmm4, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x41cd
callq 0x2260
movq %xmm0, 0x20(%rsp)
movl 0x3f3b(%rip), %eax      # 0x8170
cmpl $0x1, %eax
je 0x4255
testl %eax, %eax
jne 0x44aa
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x3460
jmp 0x4266
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x353a
callq 0x2260
subsd 0x20(%rsp), %xmm0
mulsd 0x10b7(%rip), %xmm0     # 0x5330
movsd %xmm0, 0x40(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
movq %r15, (%rsp)
movq %r14, %rdx
callq 0x3700
pxor %xmm2, %xmm2
xorl %ebp, %ebp
movq 0x3d29(%rip), %r13      # 0x7fd8
movq %xmm2, 0x10(%rsp)
movq %r13, %rdi
callq 0x2030
cmpl $0x0, 0x8(%rsp)
jne 0x42cf
callq 0x2260
movsd %xmm0, 0x20(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
movq %r15, (%rsp)
movq %r14, %rdx
callq 0x3700
movq %rax, %r12
cmpl $0x0, 0x8(%rsp)
je 0x4304
movsd 0x10(%rsp), %xmm2
jmp 0x4327
callq 0x2260
subsd 0x20(%rsp), %xmm0
mulsd 0x1019(%rip), %xmm0     # 0x5330
movsd %xmm0, 0x50(%rsp,%rbp,8)
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
incq %rbp
cmpq $0x64, %rbp
jne 0x42af
cmpl $0x0, 0x8(%rsp)
jne 0x43c4
divsd 0xff1(%rip), %xmm2      # 0x5338
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd 0x50(%rsp,%rax,8), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x64, %rax
jne 0x434d
pxor %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm2, 0x10(%rsp)
jb 0x4381
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x4390
callq 0x22b0
movsd 0x10(%rsp), %xmm2
movapd %xmm0, %xmm1
movl 0x8(%rsp), %esi
leaq 0x107d(%rip), %rdi      # 0x5418
movapd %xmm2, %xmm0
movb $0x2, %al
callq 0x2040
movl 0x8(%rsp), %esi
movsd 0x40(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
leaq 0x1088(%rip), %rdi      # 0x5445
movb $0x1, %al
callq 0x2040
cmpl $0x0, 0x8(%rsp)
jne 0x4478
testq %rbx, %rbx
je 0x444a
leaq 0x1091(%rip), %rdi      # 0x546c
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
leaq 0xd94(%rip), %rsi       # 0x5180
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x20f0
testq %rax, %rax
je 0x4491
movq %rax, %r13
cmpl $0x0, (%r14)
jle 0x4432
xorl %ebx, %ebx
leaq 0x1090(%rip), %rbp      # 0x54a4
movsd (%r12,%rbx,8), %xmm0
movq %r13, %rdi
movq %rbp, %rsi
movb $0x1, %al
callq 0x22a0
incq %rbx
movslq (%r14), %rax
cmpq %rax, %rbx
jl 0x4414
movq %r13, %rdi
callq 0x2250
testl %eax, %eax
jne 0x449b
leaq 0x1066(%rip), %rdi      # 0x54ab
callq 0x2070
movq 0x28(%rsp), %rdi
callq 0x2130
movq 0x18(%rsp), %rdi
callq 0x2130
movq 0x30(%rsp), %rdi
callq 0x2130
movq %r15, %rdi
callq 0x2130
movq %r12, %rdi
callq 0x2130
callq 0x21f0
xorl %eax, %eax
addq $0x378, %rsp            # imm = 0x378
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
callq 0x23f4
movq 0x10(%rsp), %rdi
callq 0x23d0
callq 0x23ab
callq 0x2418