// File: /workspace/github_cpp_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movq %rdi, %r14
movl 0x104(%rdi), %ebx
movq 0x8(%rdi), %rdi
callq 0x465a
cltd
idivl %ebx
movl %eax, %ebp
movl %edx, %r12d
movl 0x108(%r14), %r13d
xorl %r15d, %r15d
cmpl %edx, %r13d
cmovll %r13d, %r12d
setl 0x7(%rsp)
movq %r14, 0x20(%rsp)
andq $0x0, 0xf8(%r14)
testl %r13d, %r13d
je 0xb804
movq $0x0, 0x48(%rsp)
jmp 0xb821
movq 0xb7a5(%rip), %rdi      # 0x16fb0
leaq 0x402e(%rip), %rsi      # 0xf840
callq 0x32a0
callq 0x33a0
movq %rax, 0x48(%rsp)
movq 0x20(%rsp), %rbx
movq 0x8(%rbx), %rdi
callq 0x465a
movl %eax, 0x44(%rsp)
leaq 0x110(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rcx
andl $0x0, (%rcx)
movupd %xmm0, -0x20(%rax)
andq $0x0, 0x8(%rcx)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
movq 0x8(%rbx), %rdi
xorl %esi, %esi
callq 0x47c0
movq %rax, %r14
leaq 0x10(%rbx), %rdi
movq %rdi, 0x18(%rsp)
movq %rax, %rsi
callq 0x4556
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x4648
leaq 0xa8(%rsp), %rdi
movq %rax, %rsi
callq 0x4efc
imull %ebp, %r13d
addl %r12d, %r13d
movb 0x7(%rsp), %al
movb %al, %r15b
addl %ebp, %r15d
addl %r13d, %r15d
leaq 0xb8(%rsp), %rax
movq (%rax), %rbx
leaq 0xe0(%rsp), %r12
testq %rbx, %rbx
je 0xb9db
movq 0x8(%rbx), %rbp
movq %rbp, 0x38(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3838
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x8f28
cmpl %r13d, %eax
jl 0xb9d3
movq 0x8(%rsp), %rdi
callq 0x8f28
cmpl %r15d, %eax
jge 0xb9d3
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x4630
testb %al, %al
jne 0xb9d3
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xaa80
testq %rax, %rax
je 0xb9a8
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xaafc
movq (%rax), %rsi
movq %rbp, %rdi
callq 0x3850
testb %al, %al
je 0xb9d3
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xaafc
leaq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0xbdcc
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xab1a
movq 0x8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbp, 0x30(%rsp)
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
callq 0xac1c
jmp 0xb9c4
movq 0x8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbp, 0x30(%rsp)
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
callq 0xac1c
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xbf42
movq (%rbx), %rbx
jmp 0xb8d8
movq 0xb5b6(%rip), %rbx      # 0x16f98
leaq 0xe0(%rsp), %rbp
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x465a
cmpl 0x44(%rsp), %eax
jge 0xbc8a
cmpq $0x0, 0x80(%rsp)
movsd 0x3e26(%rip), %xmm0     # 0xf838
je 0xba2c
movq 0x70(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x18(%rax), %ecx
movl %ecx, 0xa0(%rsp)
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x98(%rsp)
movq %rbx, %rdi
callq 0x31f0
leaq 0x98(%rsp), %rdi
leaq 0x88(%rsp), %rsi
pushq $0x1
popq %rdx
movq 0xb571(%rip), %rcx      # 0x16fc8
movq 0xb54a(%rip), %r8       # 0x16fa8
movq %rbx, %r9
callq 0x3350
movq %rbx, %rdi
callq 0x31f0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
movl 0x90(%rsp), %esi
callq 0x47da
movq %rax, %r14
movq %rax, 0x38(%rsp)
movsd 0x88(%rsp), %xmm0
ucomisd 0x3d9c(%rip), %xmm0     # 0xf838
jb 0xbaaa
leaq 0x3dcd(%rip), %rdi      # 0xf872
callq 0x3420
movq (%r14), %rbx
movq 0x8(%r14), %r12
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x4630
testb %al, %al
movq %rbx, %rax
cmovneq %r12, %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x4250
movsd 0x10(%r14), %xmm0
movq 0x20(%rsp), %rax
addsd 0xf8(%rax), %xmm0
movsd %xmm0, 0xf8(%rax)
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x4556
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xbdcc
movq %rbx, %rdi
callq 0x8f28
cmpl %r13d, %eax
jl 0xbb2c
movq %rbx, %rdi
callq 0x8f28
cmpl %r15d, %eax
jl 0xbb46
movq %r12, %rdi
callq 0x8f28
cmpl %r13d, %eax
jl 0xbb53
movq %r12, %rdi
callq 0x8f28
cmpl %r15d, %eax
jge 0xbb53
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq 0xab1a
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rsp), %rsi
callq 0x4648
leaq 0xa8(%rsp), %rdi
movq %rax, %rsi
callq 0xa78e
leaq 0xb8(%rsp), %rbx
movq 0x18(%rsp), %r14
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xbc7e
movq 0x8(%rbx), %r12
movq %r12, 0x50(%rsp)
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x3838
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x8f28
cmpl %r13d, %eax
jl 0xbb83
movq 0x10(%rsp), %rdi
callq 0x8f28
cmpl %r15d, %eax
jge 0xbb83
movq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x4630
testb %al, %al
jne 0xbb83
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xaa80
testq %rax, %rax
je 0xbc4e
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xaafc
movq (%rax), %rsi
movq %r12, %rdi
callq 0x3850
testb %al, %al
je 0xbb83
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xaafc
leaq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0xbdcc
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xab1a
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
callq 0xac1c
jmp 0xbc6a
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
callq 0xac1c
leaq 0x58(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xbf42
jmp 0xbb83
movq 0xb313(%rip), %rbx      # 0x16f98
jmp 0xb9ef
movq 0xb307(%rip), %rdi      # 0x16f98
callq 0x31f0
movq 0x20(%rsp), %rbx
cmpl $0x0, 0x108(%rbx)
jne 0xbd45
callq 0x33a0
movq %rax, %r14
movq 0xb2f9(%rip), %rdi      # 0x16fb0
leaq 0x3aa5(%rip), %rsi      # 0xf763
callq 0x32a0
movq %rax, %rdi
callq 0x3130
movq %rbx, %rdi
callq 0x944c
movq 0xb2d6(%rip), %rdi      # 0x16fb0
leaq 0x3a87(%rip), %rsi      # 0xf768
callq 0x32a0
movsd 0xf8(%rbx), %xmm0
movq %rax, %rdi
callq 0x3430
movq %rax, %rdi
callq 0x3130
movq 0xb2ab(%rip), %rdi      # 0x16fb0
leaq 0x3a76(%rip), %rsi      # 0xf782
callq 0x32a0
movq %rax, %rdi
subq 0x48(%rsp), %r14
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r14, %rax
cqto
idivq %rcx
movq %rax, %rsi
callq 0x3290
leaq 0x3a6b(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
leaq 0xa8(%rsp), %rdi
callq 0x4cfe
leaq 0x58(%rsp), %rdi
callq 0xc048
leaq 0xe0(%rsp), %rdi
callq 0xa9d8
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd85
movq %rax, %rbx
jmp 0xbdac
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
jmp 0xbd9c
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x4cfe
leaq 0x58(%rsp), %rdi
callq 0xc048
leaq 0xe0(%rsp), %rdi
callq 0xa9d8
movq %rbx, %rdi
callq 0x34b0
nop