// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rcx, 0x28(%rsp)
movq %rsi, %r15
movq %rdi, %rbp
movq %rdx, 0x48(%rsp)
movl %r8d, 0x5c(%rsp)
movq %rsi, %rdi
callq 0x2ed3a
movq %rbp, %rdi
movq %rax, %rsi
callq 0x5c58
movq (%rbp), %rdi
movq 0x8(%rbp), %rdx
cmpq %rdx, %rdi
je 0x397a
subq %rdi, %rdx
addq $-0x8, %rdx
andq $-0x8, %rdx
addq $0x8, %rdx
xorl %esi, %esi
callq 0x2110
xorpd %xmm0, %xmm0
leaq 0x80(%rsp), %rdx
movapd %xmm0, (%rdx)
xorl %eax, %eax
movq %rax, 0x10(%rdx)
movq %rax, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movl 0x48(%rsp), %esi
movq %r15, %rdi
callq 0x2f1e0
movl 0x4c(%rsp), %esi
leaq 0x60(%rsp), %rdx
movq %r15, %rdi
callq 0x2f1e0
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %r12
movapd %xmm0, (%r12)
movq $0x0, 0x10(%r12)
xorl %ebx, %ebx
leaq 0x30(%rsp), %r13
movl %ebx, 0x30(%rsp)
movq %r15, %rdi
callq 0x2ed3a
movl %ebx, %ecx
cmpq %rcx, %rax
jbe 0x3a2f
movl 0x30(%rsp), %eax
movq 0x80(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x60(%rsp), %rdx
cmpl (%rdx,%rax,4), %ecx
je 0x3a27
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x3a1c
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3a27
movq %r12, %rdi
movq %r13, %rdx
callq 0x6f98
movl 0x30(%rsp), %ebx
incl %ebx
jmp 0x39d7
movq 0x3c592(%rip), %rdi     # 0x3ffc8
leaq 0x58(%rsp), %rsi
callq 0x2300
movq 0x3c581(%rip), %rdi     # 0x3ffc8
leaq 0x54(%rsp), %rsi
callq 0x22a0
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
sarq $0x2, %rcx
movslq 0x54(%rsp), %rsi
cmpq %rsi, %rcx
jb 0x3e1e
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
movl 0x58(%rsp), %edi
movl %edi, %edx
imull %eax, %edx
addl %edx, %eax
decl %esi
cmpl %esi, %edi
cmovnel %eax, %ecx
cmpl %ecx, %edx
jge 0x3ae1
movslq %edx, %r14
movslq %ecx, %r12
leaq 0x30(%rsp), %rbx
leaq (,%r14,4), %rdx
addq 0x10(%rsp), %rdx
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x3ad1
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x3ad9
movq %rbx, %rdi
callq 0x6f98
incq %r14
cmpq %r12, %r14
jl 0x3aa9
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5dd6
xorpd %xmm0, %xmm0
leaq 0x120(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movslq 0x28(%rsp), %rbx
movq %rbx, %rsi
callq 0x5eac
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x2, %rax
cmpq %rbx, %rax
jb 0x3dfa
movq %rbp, 0xa0(%rsp)
xorl %edx, %edx
movq %rbx, 0xa8(%rsp)
divq %rbx
movq 0x10(%rsp), %rcx
cmpq %rcx, 0x18(%rsp)
je 0x3bd7
movq %rax, %r13
movq 0x28(%rsp), %rax
leal -0x1(%rax), %ebx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
xorl %ebp, %ebp
movl %r12d, %esi
movl %ebp, %eax
cltd
idivl %r13d
testl %edx, %edx
sete %al
cmpl %ebx, %r12d
setl %dl
andb %al, %dl
movzbl %dl, %r12d
addl %esi, %r12d
movslq %r12d, %rax
movq 0x120(%rsp), %rdx
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rdi
addq %r14, %rcx
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x3bb2
movl (%rcx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x3bba
movq %rcx, %rdx
callq 0x6f98
incq %rbp
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
subq %rcx, %rax
sarq $0x2, %rax
addq $0x4, %r14
cmpq %rbp, %rax
ja 0x3b67
xorpd %xmm0, %xmm0
leaq 0x100(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0xa8(%rsp), %rbx
movq %rbx, %rsi
callq 0x5ee8
leaq 0xe0(%rsp), %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq %rbx, %rsi
callq 0x5f34
movq 0x28(%rsp), %rax
movl %eax, %r13d
testl %eax, %eax
jle 0x3cf6
xorl %ebp, %ebp
leaq 0xc8(%rsp), %r14
leaq 0xc0(%rsp), %rbx
leaq 0xb8(%rsp), %r12
leaq (,%rbp,8), %rax
leaq (%rax,%rax,2), %rax
movq 0xe0(%rsp), %rcx
addq %rax, %rcx
movq %rcx, 0xd0(%rsp)
movq %r15, 0xc8(%rsp)
leaq 0x48(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
addq 0x120(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x5c(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, (%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0x3a0(%rip), %rsi       # 0x4055
leaq 0xd0(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
movq %r12, %r9
callq 0x5f70
movq 0x100(%rsp), %rax
cmpq $0x0, (%rax,%rbp,8)
jne 0x3e7b
movq 0xd8(%rsp), %rcx
movq %rcx, (%rax,%rbp,8)
incq %rbp
cmpq %r13, %rbp
jne 0x3c49
cmpl $0x0, 0x28(%rsp)
movq 0xa0(%rsp), %r12
jle 0x3d29
leaq (,%r13,8), %rbx
xorl %r14d, %r14d
movq 0x100(%rsp), %rdi
addq %r14, %rdi
callq 0x2390
addq $0x8, %r14
cmpq %r14, %rbx
jne 0x3d10
cmpl $0x0, 0x28(%rsp)
jle 0x3d82
movq (%r12), %rax
movq 0x8(%r12), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0xe0(%rsp), %rsi
xorl %edi, %edi
cmpq %rax, %rcx
je 0x3d7a
leaq (%rdi,%rdi,2), %r8
movq (%rsi,%r8,8), %r8
xorl %r9d, %r9d
movsd (%r8,%r9,8), %xmm0
addsd (%rax,%r9,8), %xmm0
movsd %xmm0, (%rax,%r9,8)
incq %r9
movl %r9d, %r10d
cmpq %r10, %rdx
ja 0x3d5d
incq %rdi
cmpq %r13, %rdi
jne 0x3d4d
leaq 0x2d2c7(%rip), %rdi     # 0x31050
leaq 0x2d2c4(%rip), %rsi     # 0x31054
callq 0x22b0
movq %rax, %rbx
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x3dd8
xorl %r15d, %r15d
leaq 0x2d6ad(%rip), %r14     # 0x3145a
movsd (%rax,%r15,8), %xmm0
movq %rbx, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x2320
incq %r15
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
ja 0x3dad
movq %rbx, %rdi
callq 0x2230
leaq 0xe0(%rsp), %rdi
callq 0x602e
leaq 0x100(%rsp), %rdi
callq 0x607c
leaq 0x120(%rsp), %rdi
callq 0x60b8
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3e1e
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3e35
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3e4c
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x3e69
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2080
jmp 0x3e9e
movq %rax, %rbx
jmp 0x3eae
jmp 0x3e8b
jmp 0x3e97
movq %rax, %rbx
jmp 0x3ebb
jmp 0x3ee1
movq %rax, %rbx
jmp 0x3efb
movq %rax, %rbx
jmp 0x3ec8
jmp 0x3e9e
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x602e
leaq 0x100(%rsp), %rdi
callq 0x607c
leaq 0x120(%rsp), %rdi
callq 0x60b8
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3ee4
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
jmp 0x3ee4
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3efb
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3f12
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x3f2f
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq %rbx, %rdi
callq 0x23b0