// File: fjamour[P]icentral/src/_bc.cc
// Function: parallel_iCentral(std::vector<double, std::allocator<double>>&, component_t&, std::pair<unsigned int, unsigned int>, int, operation_t)
void parallel_iCentral(
        vector<double>& dBC_vec,
        component_t&    comp,
        edge_t          e,
        int             num_threads,
        operation_t     op
        )
{
    fill_vec<double>(dBC_vec, comp.subgraph.size(), 0.0);
    vector<int> d_src_vec, d_dst_vec;
    comp.subgraph.find_sssp(e.first, d_src_vec);
    comp.subgraph.find_sssp(e.second, d_dst_vec);
    vector<node_id_t> all_sources_vec;
    for(node_id_t s = 0; s < comp.subgraph.size(); ++s) {
        if(d_src_vec[s] != d_dst_vec[s]) {
            all_sources_vec.push_back(s);
        }
    }
    //MPI shit goes here:
    //all_sources_vec at each machine must have only its share of the sources
    //then continue normally, note that each process will finish its shit
    //and have it's contribution in its dBC_vec
    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Status    status;
    if(all_sources_vec.size() < size)
        return;
    vector<node_id_t> machine_source_vec;
    int num_s_per_machine = all_sources_vec.size()/size;
    int start, end;
    start   = rank * num_s_per_machine;
    end     = start + num_s_per_machine;
    if(rank == size - 1)
        end = all_sources_vec.size();
    for(int i = start; i < end; ++i) {
        machine_source_vec.push_back(all_sources_vec[i]);
    }
    all_sources_vec = machine_source_vec;
    //printf("RANK[%d] -- num sources [%d]\n", rank, all_sources_vec.size());
    //////////////
    vector<vector<node_id_t> > thread_source_vec;
    thread_source_vec.resize(num_threads);
    if(all_sources_vec.size() < num_threads)
        return;
    int num_s_per_thread = all_sources_vec.size()/num_threads;
    int t = -1;
    for(int i = 0; i < all_sources_vec.size(); ++i) {
        if(i%num_s_per_thread == 0 && t < num_threads-1)
            t++;
        thread_source_vec[t].push_back(all_sources_vec[i]);
    }
    
    vector<std::thread> thread_vec;
    thread_vec.resize(num_threads);
    //start the threads
    vector<vector<double> > dBC_vec_vec;
    dBC_vec_vec.resize(num_threads);
    
    for(int t = 0; t < num_threads; ++t) {
        thread_vec[t] = std::thread(iCentral_block, &(dBC_vec_vec[t]), &comp, &e, &(thread_source_vec[t]), &op);
    }
    //wait for the threads to finish, then accumulate the dBC_vec
    for(int t = 0; t < num_threads; ++t) {
        thread_vec[t].join();
    }
    for(int t = 0; t < num_threads; ++t) {
        for(node_id_t v = 0; v < dBC_vec.size(); ++v) {
            dBC_vec[v] += dBC_vec_vec[t][v];
        }
    }
    
    //now dBC_vec of each machine is ready.
    //master gets and accumulates all dBC_vec from everyone
    //printf("R[%d] done\n", rank);
//    if(rank == 0) {
//        //receive dBC_vec from everyone and accumulate
//        timer tm;
//        tm.start();
//        vector<double> rcv_dBC_vec;
//        rcv_dBC_vec.resize(dBC_vec.size());
//        for(int p = 1; p < size; ++p) {
//            MPI_Recv(&rcv_dBC_vec[0], rcv_dBC_vec.size(), MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
//            for(int i = 0; i < dBC_vec.size(); ++i) {
//                dBC_vec[i] += rcv_dBC_vec[i];
//            }
//        }
//        tm.stop();
//        printf("\t\tR[0] -- Accumulation time: [%f]\n", rank, tm.interval());
//    } else {
//        //just send dBC_vec
//        MPI_Send(&dBC_vec[0], dBC_vec.size(), MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
//    }
    
    
    FILE* fout = fopen("dBC", "w");
    for(int i = 0; i < dBC_vec.size(); ++i) {
        fprintf(fout, "%f\n", dBC_vec[i]);
    }
    fclose(fout);
}

// Assembly:
subq $0x208, %rsp            # imm = 0x208
movq %rdx, 0x200(%rsp)
movq %rdi, 0x1f8(%rsp)
movq %rsi, 0x1f0(%rsp)
movl %ecx, 0x1ec(%rsp)
movl %r8d, 0x1e8(%rsp)
movq 0x1f8(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x77430
movq 0x78(%rsp), %rdi
movq %rax, %rsi
xorps %xmm0, %xmm0
callq 0xfbe0
leaq 0x1d0(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0xfc40
leaq 0x1b8(%rsp), %rdi
callq 0xfc40
movq 0x80(%rsp), %rdx
movq 0x1f0(%rsp), %rdi
movl 0x200(%rsp), %esi
callq 0x77ae0
jmp 0xaca8
movq 0x1f0(%rsp), %rdi
movl 0x204(%rsp), %esi
leaq 0x1b8(%rsp), %rdx
callq 0x77ae0
jmp 0xacc6
leaq 0x190(%rsp), %rdi
callq 0xfcd0
movl $0x0, 0x18c(%rsp)
movl 0x18c(%rsp), %eax
movq %rax, 0x68(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x77430
movq %rax, 0x70(%rsp)
jmp 0xacfe
movq 0x68(%rsp), %rax
movq 0x70(%rsp), %rcx
cmpq %rcx, %rax
jae 0xadb1
movl 0x18c(%rsp), %eax
movl %eax, %esi
leaq 0x1d0(%rsp), %rdi
callq 0xfc50
movl (%rax), %eax
movl %eax, 0x64(%rsp)
movl 0x18c(%rsp), %eax
movl %eax, %esi
leaq 0x1b8(%rsp), %rdi
callq 0xfc50
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl (%rcx), %eax
je 0xad99
leaq 0x190(%rsp), %rdi
leaq 0x18c(%rsp), %rsi
callq 0xfce0
jmp 0xad65
jmp 0xad99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0xb485
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0xb478
jmp 0xad9b
movl 0x18c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18c(%rsp)
jmp 0xacde
movq 0x90200(%rip), %rdi     # 0x9afb8
leaq 0x188(%rsp), %rsi
callq 0x9a70
jmp 0xadc7
movq 0x901ea(%rip), %rdi     # 0x9afb8
leaq 0x184(%rsp), %rsi
callq 0x9880
jmp 0xaddd
leaq 0x190(%rsp), %rdi
callq 0xfd50
movslq 0x184(%rsp), %rcx
cmpq %rcx, %rax
jae 0xae07
movl $0x1, 0x164(%rsp)
jmp 0xb422
leaq 0x148(%rsp), %rdi
callq 0xfcd0
leaq 0x190(%rsp), %rdi
callq 0xfd50
movslq 0x184(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, 0x144(%rsp)
movl 0x188(%rsp), %eax
imull 0x144(%rsp), %eax
movl %eax, 0x140(%rsp)
movl 0x140(%rsp), %eax
addl 0x144(%rsp), %eax
movl %eax, 0x13c(%rsp)
movl 0x188(%rsp), %eax
movl 0x184(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xae89
leaq 0x190(%rsp), %rdi
callq 0xfd50
movl %eax, 0x13c(%rsp)
movl 0x140(%rsp), %eax
movl %eax, 0x138(%rsp)
movl 0x138(%rsp), %eax
cmpl 0x13c(%rsp), %eax
jge 0xaefc
movslq 0x138(%rsp), %rsi
leaq 0x190(%rsp), %rdi
callq 0xfbc0
movq %rax, %rsi
leaq 0x148(%rsp), %rdi
callq 0xfce0
jmp 0xaece
jmp 0xaed0
movl 0x138(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x138(%rsp)
jmp 0xae97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0xb46b
leaq 0x190(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0xfd70
jmp 0xaf13
leaq 0x120(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x10030
movq 0x58(%rsp), %rdi
movslq 0x1ec(%rsp), %rsi
callq 0x10040
jmp 0xaf39
leaq 0x190(%rsp), %rdi
callq 0xfd50
movslq 0x1ec(%rsp), %rcx
cmpq %rcx, %rax
jae 0xaf7c
movl $0x1, 0x164(%rsp)
jmp 0xb408
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0xb45e
leaq 0x190(%rsp), %rdi
callq 0xfd50
movslq 0x1ec(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, 0x11c(%rsp)
movl $0xffffffff, 0x118(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x114(%rsp)
movslq 0x114(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x190(%rsp), %rdi
callq 0xfd50
movq %rax, %rcx
movq 0x50(%rsp), %rax
cmpq %rcx, %rax
jae 0xb06e
movl 0x114(%rsp), %eax
cltd
idivl 0x11c(%rsp)
cmpl $0x0, %edx
jne 0xb018
movl 0x118(%rsp), %eax
movl 0x1ec(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xb018
movl 0x118(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x118(%rsp)
movslq 0x118(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x100e0
movq %rax, 0x48(%rsp)
movslq 0x114(%rsp), %rsi
leaq 0x190(%rsp), %rdi
callq 0xfbc0
movq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0xfce0
jmp 0xb056
jmp 0xb058
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0xafb3
leaq 0xf8(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x10100
movq 0x40(%rsp), %rdi
movslq 0x1ec(%rsp), %rsi
callq 0x10110
jmp 0xb094
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x101b0
movq 0x38(%rsp), %rdi
movslq 0x1ec(%rsp), %rsi
callq 0x101c0
jmp 0xb0ba
movl $0x0, 0xdc(%rsp)
movl 0xdc(%rsp), %eax
cmpl 0x1ec(%rsp), %eax
jge 0xb20e
movslq 0xdc(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x10260
movq %rax, 0xc8(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0x200(%rsp), %rax
movq %rax, 0xb8(%rsp)
movslq 0xdc(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x100e0
movq %rax, 0xb0(%rsp)
leaq 0x1e8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq %rsp, %rax
leaq 0xa8(%rsp), %rcx
movq %rcx, (%rax)
leaq 0x4e8(%rip), %rsi       # 0xb640
leaq 0xd0(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
leaq 0xb8(%rsp), %r8
leaq 0xb0(%rsp), %r9
callq 0x10280
jmp 0xb187
movslq 0xdc(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0x10380
movq %rax, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x103a0
leaq 0xd0(%rsp), %rdi
callq 0x103e0
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0xb0c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0xb451
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x10400
jmp 0xb451
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x1ec(%rsp), %eax
jge 0xb25d
movslq 0xa4(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0x10380
movq %rax, %rdi
callq 0x9cb0
jmp 0xb248
jmp 0xb24a
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0xb219
movl $0x0, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0x1ec(%rsp), %eax
jge 0xb32f
movl $0x0, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
movq %rax, 0x30(%rsp)
movq 0x1f8(%rsp), %rdi
callq 0xfa10
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jae 0xb317
movslq 0xa0(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x10260
movq %rax, %rdi
movl 0x9c(%rsp), %eax
movl %eax, %esi
callq 0xfa30
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x1f8(%rsp), %rdi
movl 0x9c(%rsp), %eax
movl %eax, %esi
callq 0xfa30
movsd 0x28(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0xb287
jmp 0xb319
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0xb268
leaq 0x6fd02(%rip), %rdi     # 0x7b038
leaq 0x6fcff(%rip), %rsi     # 0x7b03c
callq 0x9890
movq %rax, 0x20(%rsp)
jmp 0xb349
movq 0x20(%rsp), %rax
movq %rax, 0x90(%rsp)
movl $0x0, 0x8c(%rsp)
movslq 0x8c(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x1f8(%rsp), %rdi
callq 0xfa10
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0xb3d4
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x1f8(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xfa30
movq 0x10(%rsp), %rdi
movsd (%rax), %xmm0
leaq 0x70196(%rip), %rsi     # 0x7b550
movb $0x1, %al
callq 0x9ac0
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0xb361
movq 0x90(%rsp), %rdi
callq 0x9710
jmp 0xb3e3
leaq 0xe0(%rsp), %rdi
callq 0x10400
leaq 0xf8(%rsp), %rdi
callq 0x10460
movl $0x0, 0x164(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x104c0
leaq 0x148(%rsp), %rdi
callq 0x10520
leaq 0x190(%rsp), %rdi
callq 0x10520
leaq 0x1b8(%rsp), %rdi
callq 0xfc70
leaq 0x1d0(%rsp), %rdi
callq 0xfc70
addq $0x208, %rsp            # imm = 0x208
retq
leaq 0xf8(%rsp), %rdi
callq 0x10460
leaq 0x120(%rsp), %rdi
callq 0x104c0
leaq 0x148(%rsp), %rdi
callq 0x10520
leaq 0x190(%rsp), %rdi
callq 0x10520
leaq 0x1b8(%rsp), %rdi
callq 0xfc70
leaq 0x1d0(%rsp), %rdi
callq 0xfc70
movq 0x1b0(%rsp), %rdi
callq 0x9cf0
nopl (%rax)