// File: /workspace/github_cpp_makefile_star2_O3/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, 0x48(%rsp)
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbp
movq %rdx, 0x68(%rsp)
movl %r8d, 0x5c(%rsp)
movq %rsi, %rdi
callq 0x2e9da
movq %rbp, %rdi
movq %rax, %rsi
callq 0x5be8
movq (%rbp), %rdi
movq 0x8(%rbp), %rdx
cmpq %rdx, %rdi
je 0x39d8
subq %rdi, %rdx
addq $-0x8, %rdx
andq $-0x8, %rdx
addq $0x8, %rdx
xorl %esi, %esi
callq 0x2110
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %rdx
movapd %xmm0, (%rdx)
xorl %eax, %eax
movq %rax, 0x10(%rdx)
movq %rax, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movq %r15, %rdi
movl %r12d, %esi
callq 0x2ee54
movl 0x6c(%rsp), %esi
leaq 0x70(%rsp), %rdx
movq %r15, %rdi
callq 0x2ee54
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 0x2e9da
movl %ebx, %ecx
cmpq %rcx, %rax
jbe 0x3a8f
movl 0x30(%rsp), %eax
movq 0x90(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x70(%rsp), %rdx
cmpl (%rdx,%rax,4), %ecx
je 0x3a87
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x3a7c
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3a87
movq %r12, %rdi
movq %r13, %rdx
callq 0x6f14
movl 0x30(%rsp), %ebx
incl %ebx
jmp 0x3a37
movq 0x3b532(%rip), %rdi     # 0x3efc8
leaq 0x58(%rsp), %rsi
callq 0x2300
movq 0x3b521(%rip), %rdi     # 0x3efc8
leaq 0x54(%rsp), %rsi
callq 0x22a0
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
subq %rdi, %rcx
sarq $0x2, %rcx
movslq 0x54(%rsp), %rsi
cmpq %rsi, %rcx
jb 0x3e83
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 0x3b44
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 0x3b34
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x3b3c
movq %rbx, %rdi
callq 0x6f14
incq %r14
cmpq %r12, %r14
jl 0x3b0c
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5d4e
movq %rbp, 0x60(%rsp)
xorpd %xmm0, %xmm0
leaq 0x120(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movslq 0x48(%rsp), %rbx
movq %rbx, %rsi
callq 0x5e32
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
movq %rsi, %rax
subq %rcx, %rax
sarq $0x2, %rax
cmpq %rbx, %rax
jb 0x3e5a
xorl %edx, %edx
movq %rbx, 0xa8(%rsp)
divq %rbx
cmpq %rcx, %rsi
je 0x3c34
movq %rax, %r13
movq 0x48(%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
leaq (%rcx,%r14), %rdx
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x3c12
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x3c1c
callq 0x6f14
movq 0x10(%rsp), %rcx
incq %rbp
movq 0x18(%rsp), %rax
subq %rcx, %rax
sarq $0x2, %rax
addq $0x4, %r14
cmpq %rbp, %rax
ja 0x3bc6
xorpd %xmm0, %xmm0
leaq 0x100(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0xa8(%rsp), %rbx
movq %rbx, %rsi
callq 0x5e6e
leaq 0xe0(%rsp), %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq %rbx, %rsi
callq 0x5eba
movq 0x60(%rsp), %r12
movq 0x48(%rsp), %rax
testl %eax, %eax
jle 0x3de2
movl %eax, %r13d
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 0x68(%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 0x3a4(%rip), %rsi       # 0x40bb
leaq 0xd0(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
movq %r12, %r9
callq 0x5ef6
movq 0x100(%rsp), %rax
cmpq $0x0, (%rax,%rbp,8)
jne 0x3ede
movq 0xd8(%rsp), %rcx
movq %rcx, (%rax,%rbp,8)
incq %rbp
cmpq %r13, %rbp
jne 0x3cab
movq 0x48(%rsp), %r15
testl %r15d, %r15d
movq 0x60(%rsp), %r12
jle 0x3de2
leaq (,%r13,8), %rbx
xorl %r14d, %r14d
movq 0x100(%rsp), %rdi
addq %r14, %rdi
callq 0x2390
addq $0x8, %r14
cmpq %r14, %rbx
jne 0x3d72
testl %r15d, %r15d
jle 0x3de2
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 0x3dda
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 0x3dbd
incq %rdi
cmpq %r13, %rdi
jne 0x3dad
leaq 0x2c267(%rip), %rdi     # 0x30050
leaq 0x2c264(%rip), %rsi     # 0x30054
callq 0x22b0
movq %rax, %rbx
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x3e38
xorl %r15d, %r15d
leaq 0x2c64d(%rip), %r14     # 0x3045a
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 0x3e0d
movq %rbx, %rdi
callq 0x2230
leaq 0xe0(%rsp), %rdi
callq 0x5fa4
leaq 0x100(%rsp), %rdi
callq 0x5ff8
leaq 0x120(%rsp), %rdi
callq 0x6034
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3e7e
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3e95
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3eaf
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x3ecc
movq 0xa0(%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 0x3f01
movq %rax, %rbx
jmp 0x3f11
jmp 0x3eee
jmp 0x3efa
movq %rax, %rbx
jmp 0x3f1e
jmp 0x3f44
movq %rax, %rbx
jmp 0x3f5e
movq %rax, %rbx
jmp 0x3f2b
jmp 0x3f01
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x5fa4
leaq 0x100(%rsp), %rdi
callq 0x5ff8
leaq 0x120(%rsp), %rdi
callq 0x6034
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3f47
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
jmp 0x3f47
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3f5e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3f78
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x3f95
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
movq %rbx, %rdi
callq 0x23b0