// File: /workspace/github_cpp_makefile_star2_O2/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 $0x148, %rsp            # imm = 0x148
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r13
movq %rdx, 0xb8(%rsp)
movl %r8d, 0x1c(%rsp)
movq %rsi, %rdi
callq 0x2622a
xorpd %xmm0, %xmm0
movq %r13, %rdi
movq %rax, %rsi
callq 0x4756
xorpd %xmm0, %xmm0
leaq 0x120(%rsp), %rdx
movapd %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq 0x26540
shrq $0x20, %rbx
leaq 0x60(%rsp), %rdx
movq %r15, %rdi
movl %ebx, %esi
callq 0x26540
leaq 0x20(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rbx)
xorl %r12d, %r12d
leaq 0x40(%rsp), %r14
movl %r12d, 0x40(%rsp)
movq %r15, %rdi
callq 0x2622a
movl %r12d, %ecx
cmpq %rcx, %rax
jbe 0x2ce0
movl 0x40(%rsp), %r12d
movq 0x120(%rsp), %rax
movl (%rax,%r12,4), %eax
movq 0x60(%rsp), %rcx
cmpl (%rcx,%r12,4), %eax
je 0x2cdb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x478c
movl 0x40(%rsp), %r12d
incl %r12d
jmp 0x2c9a
movq 0x362e1(%rip), %rdi     # 0x38fc8
leaq 0x18(%rsp), %rsi
callq 0x2310
movq 0x362d0(%rip), %rdi     # 0x38fc8
leaq 0x14(%rsp), %rsi
callq 0x22a0
movq 0x28(%rsp), %rcx
subq 0x20(%rsp), %rcx
sarq $0x2, %rcx
movslq 0x14(%rsp), %rsi
cmpq %rsi, %rcx
jb 0x304b
leaq 0x40(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rbx)
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
movl 0x18(%rsp), %edx
movl %edx, %edi
imull %eax, %edi
addl %edi, %eax
decl %esi
cmpl %esi, %edx
cmovnel %eax, %ecx
movslq %edi, %r14
movslq %ecx, %r12
cmpq %r12, %r14
jge 0x2d6f
leaq (,%r14,4), %rsi
addq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x478c
incq %r14
jmp 0x2d50
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x47aa
xorpd %xmm0, %xmm0
leaq 0x100(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movslq %ebp, %r14
movq %r14, %rsi
callq 0x4874
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq %rcx, %rax
subq %rsi, %rax
sarq $0x2, %rax
cmpq %r14, %rax
jb 0x3034
movq %rbp, 0x8(%rsp)
movq %r13, 0x80(%rsp)
xorl %ebx, %ebx
xorl %edx, %edx
divq %r14
movq %rax, %r13
movq %r14, 0x88(%rsp)
decl %r14d
pushq $-0x1
popq %r12
xorl %ebp, %ebp
subq %rsi, %rcx
sarq $0x2, %rcx
cmpq %rbp, %rcx
jbe 0x2e32
movl %ebp, %eax
cltd
idivl %r13d
testl %edx, %edx
sete %al
cmpl %r14d, %r12d
setl %cl
andb %al, %cl
movzbl %cl, %eax
addl %eax, %r12d
movslq %r12d, %rax
imulq $0x18, %rax, %rdi
addq 0x100(%rsp), %rdi
addq %rbx, %rsi
callq 0x478c
incq %rbp
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
addq $0x4, %rbx
jmp 0x2de3
xorpd %xmm0, %xmm0
leaq 0xe0(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq 0x88(%rsp), %rbx
movq %rbx, %rsi
callq 0x48aa
leaq 0xc0(%rsp), %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq %rbx, %rsi
callq 0x48d4
movq 0x8(%rsp), %rax
xorl %r14d, %r14d
testl %eax, %eax
cmovlel %r14d, %eax
movq %rax, 0x8(%rsp)
leaq (,%rax,8), %rbp
leaq 0x140(%rsp), %r13
leaq 0x98(%rsp), %r12
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x2f53
movq 0xc0(%rsp), %rax
addq %r14, %rax
movq %rax, 0xb0(%rsp)
movq %r15, 0xa8(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x100(%rsp), %rax
addq %r14, %rax
movq %rax, 0x98(%rsp)
leaq 0x1c(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0x30b(%rip), %rsi       # 0x3216
leaq 0xb0(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
leaq 0xa0(%rsp), %r8
movq %r12, %r9
callq 0x490a
movq 0xe0(%rsp), %rdi
addq %rbx, %rdi
movq %r13, %rsi
callq 0x49b2
movq %r13, %rdi
callq 0x49cc
addq $0x8, %rbx
addq $0x18, %r14
jmp 0x2ea1
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x2f70
movq 0xe0(%rsp), %rdi
addq %rbx, %rdi
callq 0x23b0
addq $0x8, %rbx
jmp 0x2f55
movq 0x80(%rsp), %r12
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
xorl %edx, %edx
movq 0x8(%rsp), %r9
cmpq %r9, %rdx
je 0x2fc5
imulq $0x18, %rdx, %rsi
addq 0xc0(%rsp), %rsi
xorl %edi, %edi
movl %edi, %edi
cmpq %rdi, %rcx
jbe 0x2fc0
movq (%rsi), %r8
movsd (%r8,%rdi,8), %xmm0
addsd (%rax,%rdi,8), %xmm0
movsd %xmm0, (%rax,%rdi,8)
incl %edi
jmp 0x2fa2
incq %rdx
jmp 0x2f8f
leaq 0x25080(%rip), %rdi     # 0x2804c
leaq 0x2507d(%rip), %rsi     # 0x28050
callq 0x22b0
movq %rax, %rbx
xorl %r15d, %r15d
leaq 0x25435(%rip), %r14     # 0x2841a
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
jbe 0x3012
movsd (%rax,%r15,8), %xmm0
movq %rbx, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x2330
incq %r15
jmp 0x2fe5
movq %rbx, %rdi
callq 0x2230
leaq 0xc0(%rsp), %rdi
callq 0x49da
leaq 0xe0(%rsp), %rdi
callq 0x49fc
leaq 0x100(%rsp), %rdi
callq 0x4a1e
leaq 0x40(%rsp), %rdi
callq 0x56ec
leaq 0x20(%rsp), %rdi
callq 0x56ec
leaq 0x60(%rsp), %rdi
callq 0x5720
leaq 0x120(%rsp), %rdi
callq 0x5720
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3092
movq %rax, %rbx
jmp 0x30a2
jmp 0x30b1
jmp 0x30c3
jmp 0x30d2
movq %rax, %rbx
jmp 0x30df
jmp 0x3092
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x49da
leaq 0xe0(%rsp), %rdi
callq 0x49fc
jmp 0x30b4
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x4a1e
jmp 0x30c6
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x56ec
jmp 0x30d5
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x56ec
leaq 0x60(%rsp), %rdi
callq 0x5720
leaq 0x120(%rsp), %rdi
callq 0x5720
movq %rbx, %rdi
callq 0x23d0