// File: /workspace/github_cpp_makefile_star2_O3/sebastienmeyer2[P]mst-clustering/src/mst_prim_kumar.cpp
// Function: PrimKumarAlgorithm::compute_mst()
void PrimKumarAlgorithm::compute_mst() 
{
    /*!
     * @brief Computes the Minimum Spanning Tree of the initial graph using 
     * Prim-Kumar's algorithm. More specifically, initialize a priority queue to 
     * sort the edges by their weight. At each iteration, it adds the edge with 
     * minimal weight to the MST and checks for new edges to discover. 
     * 
     */


    int n_slaves = this->world_size;
    int n_nodes = this->initial_graph->get_number_of_nodes();

    int min_nodes_per_proc = n_nodes/(n_slaves);
    int split_nodes = n_nodes % n_slaves;

    int nodes_interval[2];

    nodes_interval[0] = this->rank*min_nodes_per_proc + (this->rank < split_nodes ? this->rank : split_nodes);  
    nodes_interval[1] = nodes_interval[0] + min_nodes_per_proc + (this->rank < split_nodes);

    int node_id_min = nodes_interval[0];
    int node_id_max = nodes_interval[1]; 

    struct {double d; int edge_id;} iter_res;
    struct {double d; int edge_id;} reduction_res;


    this->mst_weight = 0.0;

    std::chrono::steady_clock::time_point begin;
    if(this->rank == 0){
        std::cout << "Computing the MST using Prim-Kumar's algorithm...";
        begin = std::chrono::steady_clock::now();
    }

    int n = this->initial_graph->get_number_of_nodes();
    //std::vector<Node*> nodes = this->initial_graph->get_nodes();

    auto cmp = [](const Edge* e1, const Edge* e2) 
        { 
            double w1 = e1->weight;
            double w2 = e2->weight;

            if (w1 < w2) {
                return true;
            } else if (w1 > w2) {
                return false;
            } else { // Allows multiple keys for same weight values
                intptr_t p1 = reinterpret_cast<intptr_t>(e1);
                intptr_t p2 = reinterpret_cast<intptr_t>(e2);

                return p1 < p2;
            }
        };

    // initialization of the priority queue (ie set), best edge per vertex and beginning vertex
    std::unordered_map<Node*, Edge*> min_edge;
    std::set<Edge*, decltype(cmp)> pq(cmp);


    Node* current_node = this->initial_graph->get_node(0);




    this->mst_graph.add_node(current_node); // just to have the source node in the MST

    std::unordered_set<Edge*> connected_edges = this->initial_graph->connected_edges(current_node);

    for (Edge* e : connected_edges) {

        Node* other_node = e->other_node(current_node);
        if(IS_NODE_ALLOCATED_TO_ME(other_node->get_id())){
            if (!this->mst_graph.has_node(other_node)) {
                if (min_edge.find(other_node) != min_edge.end()) 
                {
                    if (*e < *min_edge.at(other_node)) {
                        pq.erase(min_edge.at(other_node));
                        min_edge.erase(other_node);

                        min_edge.insert(std::pair<Node*, Edge*>(other_node, e));
                        pq.insert(e);
                    }
                }
                else 
                {
                    min_edge.insert(std::pair<Node*, Edge*>(other_node, e));
                    pq.insert(e);
                }
            }
        } 
    }

    //std::printf("Proc %d, %d remaning items in queue\n", this->rank, pq.size());

    // filling the MST with new nodes until it forms a tree 
    while (this->mst_graph.get_number_of_nodes() < n) {

        Edge* new_edge;

        if(!pq.empty()){
            // adding the new edge and node to the MST and the visited nodes
            Edge* new_edge = *pq.begin();

            // We shall now send this to the root processor, so that it decides which edge is the best
            iter_res.edge_id = new_edge->id;
            iter_res.d = new_edge->weight;            
        } else {
            iter_res.d = std::numeric_limits<double>::infinity();
        }


        //std::printf("Proc %d, %d remaning items in queue\n", this->rank, pq.size());
        MPI_Barrier(MPI_COMM_WORLD);
        MPI_Allreduce(&iter_res, &reduction_res, 1, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD);
        MPI_Barrier(MPI_COMM_WORLD);


        // This is the REAL new edge
        new_edge = this->initial_graph->get_edge(reduction_res.edge_id);

        if(reduction_res.d == std::numeric_limits<double>::infinity()){
            printf("ARRRHS\n");
        } else {
            //printf("Elected edge %d\n", reduction_res.edge_id);
        }

        Node* p1 = new_edge->p1;
        Node* p2 = new_edge->p2;
        Node* new_node = (this->mst_graph.has_node(p1)) ? p2 : p1;

        this->mst_graph.add_edge(new_edge);
        this->mst_weight += new_edge->weight;



        this->mst_graph.add_node(new_node);
        
        pq.erase(new_edge);

        if(IS_NODE_ALLOCATED_TO_ME(p1->get_id()) || IS_NODE_ALLOCATED_TO_ME(p2->get_id())){
            // we won't need the best edge for this node anymore
            min_edge.erase(new_node);

        }


        // updating the external nodes best edges
        connected_edges = this->initial_graph->connected_edges(new_node);
            for (Edge* e : connected_edges) {
                Node* other_node = e->other_node(new_node);
                if(IS_NODE_ALLOCATED_TO_ME(other_node->get_id())){
                    if (!this->mst_graph.has_node(other_node)) {
                        if (min_edge.find(other_node) != min_edge.end()) 
                        {
                            if (*e < *min_edge.at(other_node)) {
                                pq.erase(min_edge.at(other_node));
                                min_edge.erase(other_node);

                                min_edge.insert(std::pair<Node*, Edge*>(other_node, e));
                                pq.insert(e);
                            }
                        }
                        else 
                        {
                            min_edge.insert(std::pair<Node*, Edge*>(other_node, e));
                            pq.insert(e);
                        }
                    }
                }
            }
    }
    
    MPI_Barrier(MPI_COMM_WORLD);
    if(this->rank == 0){
        std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
        std::cout << "[OK]" << std::endl;
        this->treatment_done();

        std::cout << "Total weight of the MST: " << this->mst_weight << std::endl;

        std::cout << "Time spent by the algorithm: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << " µs" << std::endl;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, %r12
movl 0x104(%rdi), %ebx
movq 0x8(%rdi), %rdi
callq 0x4cc8
cltd
idivl %ebx
movl %edx, %ebx
movl 0x108(%r12), %ebp
xorl %r14d, %r14d
cmpl %edx, %ebp
cmovll %ebp, %ebx
setl %cl
movq $0x0, 0xf8(%r12)
testl %ebp, %ebp
movl %eax, 0x38(%rsp)
movb %cl, 0x7(%rsp)
je 0xd3cc
movq $0x0, 0x40(%rsp)
jmp 0xd3ee
movq 0x9be5(%rip), %rdi      # 0x16fb8
leaq 0x4496(%rip), %rsi      # 0x11870
movl $0x31, %edx
callq 0x32d0
callq 0x3380
movq %rax, 0x40(%rsp)
movq 0x8(%r12), %rdi
callq 0x4cc8
movl %eax, 0x3c(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
leaq 0x88(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq 0x8(%r12), %rdi
xorl %esi, %esi
callq 0x4f30
movq %rax, %r13
leaq 0x10(%r12), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x4ba0
movq %r12, 0x30(%rsp)
movq 0x8(%r12), %rdi
movq %r13, %rsi
callq 0x4ca2
xorl %ecx, %ecx
leaq 0xd0(%rsp), %rdi
movq %rcx, (%rdi)
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rdi)
movq %rcx, 0x10(%rdi)
movq 0x18(%rax), %rdx
movq %rdx, 0x18(%rdi)
movupd 0x20(%rax), %xmm0
movupd %xmm0, 0x20(%rdi)
movq %rcx, 0x30(%rdi)
leaq 0x20(%rsp), %rdx
movq %rdi, (%rdx)
movq %rax, %rsi
callq 0x541c
movl 0x38(%rsp), %eax
imull %eax, %ebp
addl %ebx, %ebp
movb 0x7(%rsp), %cl
movb %cl, %r14b
addl %eax, %r14d
addl %ebp, %r14d
movq 0xe0(%rsp), %r12
testq %r12, %r12
je 0xd9de
movq 0x8(%r12), %rbx
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x39f8
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0xa57c
cmpl %ebp, %eax
jl 0xd687
movq 0x8(%rsp), %rdi
callq 0xa57c
cmpl %r14d, %eax
jge 0xd687
movq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x4c8a
testb %al, %al
jne 0xd687
movq 0x8(%rsp), %rcx
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xd65c
movq %rdx, %rsi
movq (%rax), %r9
movq 0x8(%r9), %r10
movq %r9, %r11
cmpq %r10, %rcx
je 0xd5b2
movq (%r11), %r11
testq %r11, %r11
je 0xd65c
movq 0x8(%r11), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xd65c
cmpq %rdi, %rcx
jne 0xd561
cmpq %r10, %rcx
je 0xd5b2
movq (%r9), %r9
testq %r9, %r9
je 0xdb85
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xdb85
cmpq %rdi, %rcx
jne 0xd58c
movq 0x10(%r9), %rsi
movq %rbx, %rdi
callq 0x3a10
testb %al, %al
je 0xd687
movq 0x8(%rsp), %rcx
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xdb85
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0xd61c
movq (%r9), %r9
testq %r9, %r9
je 0xdb85
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xdb85
cmpq %rdi, %rcx
jne 0xd5f6
movq 0x10(%r9), %rsi
leaq 0x80(%rsp), %rdi
callq 0xdbe2
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xc400
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xc4f0
jmp 0xd675
movq %rcx, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xc4f0
leaq 0x80(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xdd76
movq (%r12), %r12
jmp 0xd4d0
cmpq $0x0, 0xa8(%rsp)
je 0xd6b8
movq 0x98(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x18(%rax), %ecx
movl %ecx, 0xc8(%rsp)
movsd 0x10(%rax), %xmm0
jmp 0xd6c0
movsd 0x41a8(%rip), %xmm0     # 0x11868
movsd %xmm0, 0xc0(%rsp)
movq 0x98c8(%rip), %rdi      # 0x16f98
callq 0x31c0
movq 0x98ec(%rip), %rcx      # 0x16fc8
movq 0x98cd(%rip), %r8       # 0x16fb0
movq 0x98ae(%rip), %r9       # 0x16f98
leaq 0xc0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movl $0x1, %edx
callq 0x3340
movq 0x988d(%rip), %rdi      # 0x16f98
callq 0x31c0
movq 0x8(%r12), %rdi
movl 0xb8(%rsp), %esi
callq 0x4f5c
movq %rax, %r12
movsd 0xb0(%rsp), %xmm0
ucomisd 0x4133(%rip), %xmm0     # 0x11868
jb 0xd743
leaq 0x4164(%rip), %rdi      # 0x118a2
callq 0x3410
movq (%r12), %r13
movq 0x8(%r12), %rbx
movq %r15, %rdi
movq %r13, %rsi
callq 0x4c8a
testb %al, %al
movq %r13, %rax
cmovneq %rbx, %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x477c
movsd 0x10(%r12), %xmm0
movq 0x30(%rsp), %rax
addsd 0xf8(%rax), %xmm0
movsd %xmm0, 0xf8(%rax)
movq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x4ba0
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0xdbe2
movq %r13, %rdi
callq 0xa57c
cmpl %ebp, %eax
jl 0xd7c2
movq %r13, %rdi
callq 0xa57c
cmpl %r14d, %eax
jl 0xd7db
movq %rbx, %rdi
callq 0xa57c
cmpl %ebp, %eax
jl 0xd7ea
movq %rbx, %rdi
callq 0xa57c
cmpl %r14d, %eax
jge 0xd7ea
leaq 0x48(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xc400
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x18(%rsp), %rsi
callq 0x4ca2
leaq 0xd0(%rsp), %rcx
cmpq %rcx, %rax
je 0xd81a
leaq 0xd0(%rsp), %rdi
movq %rax, %rsi
callq 0xc070
movq 0xe0(%rsp), %r12
testq %r12, %r12
je 0xd9de
movq 0x8(%r12), %rbx
movq %rbx, 0x8(%rsp)
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x39f8
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0xa57c
cmpl %ebp, %eax
jl 0xd9d5
movq 0x10(%rsp), %rdi
callq 0xa57c
cmpl %r14d, %eax
jge 0xd9d5
movq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x4c8a
testb %al, %al
jne 0xd9d5
movq 0x10(%rsp), %rcx
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xd9ad
movq %rdx, %rsi
movq (%rax), %r9
movq 0x8(%r9), %r10
movq %r9, %r11
cmpq %r10, %rcx
je 0xd906
movq (%r11), %r11
testq %r11, %r11
je 0xd9ad
movq 0x8(%r11), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xd9ad
cmpq %rdi, %rcx
jne 0xd8b5
cmpq %r10, %rcx
je 0xd906
movq (%r9), %r9
testq %r9, %r9
je 0xdb79
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xdb79
cmpq %rdi, %rcx
jne 0xd8e0
movq 0x10(%r9), %rsi
movq %rbx, %rdi
callq 0x3a10
testb %al, %al
je 0xd9d5
movq 0x10(%rsp), %rcx
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xdb79
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0xd970
movq (%r9), %r9
testq %r9, %r9
je 0xdb79
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xdb79
cmpq %rdi, %rcx
jne 0xd94a
movq 0x10(%r9), %rsi
leaq 0x80(%rsp), %rdi
callq 0xdbe2
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc400
movq 0x10(%rsp), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x48(%rsp), %rdi
callq 0xc4f0
jmp 0xd9c3
leaq 0x20(%rsp), %rsi
movq %rcx, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x48(%rsp), %rdi
callq 0xc4f0
leaq 0x80(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xdd76
movq (%r12), %r12
jmp 0xd822
movq %r15, %rdi
callq 0x4cc8
cmpl 0x3c(%rsp), %eax
movq 0x30(%rsp), %r12
jl 0xd690
movq 0x959c(%rip), %rdi      # 0x16f98
callq 0x31c0
cmpl $0x0, 0x108(%r12)
jne 0xdb43
callq 0x3380
movq %rax, %rbx
movq 0x9599(%rip), %r15      # 0x16fb8
leaq 0x3d6d(%rip), %rsi      # 0x11793
movl $0x4, %edx
movq %r15, %rdi
callq 0x32d0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq 0x9567(%rip), %rdi      # 0x16fb8
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq %r12, %rdi
callq 0xa9a6
movq 0x954b(%rip), %rdi      # 0x16fb8
leaq 0x3d24(%rip), %rsi      # 0x11798
movl $0x19, %edx
callq 0x32d0
movsd 0xf8(%r12), %xmm0
movq 0x9529(%rip), %rdi      # 0x16fb8
callq 0x3420
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq 0x94f3(%rip), %rdi      # 0x16fb8
leaq 0x3ce6(%rip), %rsi      # 0x117b2
movl $0x1d, %edx
callq 0x32d0
subq 0x40(%rsp), %rbx
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
movq %rbx, %rax
imulq %rcx
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x7, %rdx
addq %rdx, %rsi
movq 0x94b8(%rip), %rdi      # 0x16fb8
callq 0x3260
movq %rax, %rbx
leaq 0x3cc1(%rip), %rsi      # 0x117d0
movl $0x4, %edx
movq %rax, %rdi
callq 0x32d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
leaq 0xd0(%rsp), %rdi
callq 0x529c
movq 0x90(%rsp), %rdi
callq 0xde76
leaq 0x48(%rsp), %rdi
callq 0xc390
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x360a(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0x35fe(%rip), %rdi      # 0x1118a
callq 0x33e0
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdb9b
movq %rax, %rbx
jmp 0xdbc2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
jmp 0xdbb2
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x529c
movq 0x90(%rsp), %rdi
callq 0xde76
leaq 0x48(%rsp), %rdi
callq 0xc390
movq %rbx, %rdi
callq 0x34a0
nop