// File: /workspace/github_cpp_makefile_star2_O1/medvedevgroup[P]varmatch/src/wholegenome.cpp
// Function: WholeGenome::ParallelClustering()
bool WholeGenome::ParallelClustering(){
    // parallel by chr
    variant_cluster_by_chrid = new vector<vector<VariantIndicator>> *[chrom_num];
    for (int j = 0; j < chrom_num; j++) {
        variant_cluster_by_chrid[j] = new vector<vector<VariantIndicator>>;
    }

    int parallel_steps = chrom_num / thread_num;
    if(parallel_steps*thread_num < chrom_num) parallel_steps += 1;
    int chr_id = 0;
    for(int i = 0; i < parallel_steps; i++){
        vector<thread> threads;
        for(int j = 0; j < thread_num-1 && chr_id < chrom_num-1; j++){
            if(chrname_by_chrid.find(chr_id) != chrname_by_chrid.end()){
                if(ref_variant_by_chrid[chr_id]->size() > 0 && que_variant_by_chrid[chr_id]->size() > 0){
                    threads.push_back(thread(&WholeGenome::SingleThreadClustering, this, chr_id));
                }
            }
            chr_id ++;
        }
        if(chr_id < chrom_num){
            if(chrname_by_chrid.find(chr_id) != chrname_by_chrid.end()){
                SingleThreadClustering(chr_id);
            }
            chr_id ++;
        }
        std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
        threads.clear();
    }


    for(int i = 0; i < chrom_num; i++){
        if(variant_cluster_by_chrid[i]->size() > 0){
            variants_by_cluster.insert(variants_by_cluster.end(), variant_cluster_by_chrid[i]->begin(), variant_cluster_by_chrid[i]->end());
        }
    }

    // test output
    //dout << endl;
    map<int, int> size_num;
    map<int, int> size_chrid;
    for(int i = 0; i < chrom_num; i++){
        //dout << i << ": " << variant_cluster_by_chrid[i]->size() << endl;
        for(int j = 0; j < variant_cluster_by_chrid[i]->size(); j++){
            int temp_size = variant_cluster_by_chrid[i]->at(j).size();
            if(size_num.find(temp_size) != size_num.end()){
                size_num[temp_size] ++;
            }else{
                size_num[temp_size] = 1;
            }
            if(size_chrid.find(temp_size) == size_chrid.end()){
                size_chrid[temp_size] = i;
            }
        }
    }

    //cout << endl;
    //for(auto it = size_num.begin(); it != size_num.end(); ++it){
    //    dout << it->first << ": " << it->second << endl;
    //}

//    cout << endl;
//    cout << "size and location:" << endl;
//    for(auto it = size_chrid.begin(); it != size_chrid.end(); ++it){
//        dout << it->first << ": " << it->second << endl;
//    }
        // clean at the end of function

    for(int j = 0; j < chrom_num; j++){
        variant_cluster_by_chrid[j]->clear();
        delete variant_cluster_by_chrid[j];
    }
    delete[] variant_cluster_by_chrid;

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movslq (%rdi), %r14
leaq (,%r14,8), %rax
testq %r14, %r14
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x5030
movq %rax, 0x158(%rbx)
testq %r14, %r14
jle 0x1429a
xorl %r15d, %r15d
movl $0x18, %edi
callq 0x5330
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x158(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
cmpq %r15, %r14
jne 0x1426f
movl 0x4(%rbx), %ecx
movl %r14d, %eax
cltd
idivl %ecx
imull %eax, %ecx
xorl %edx, %edx
cmpl %r14d, %ecx
setl %dl
movl $0x0, 0xc(%rsp)
addl %eax, %edx
movl %edx, 0x1c(%rsp)
jle 0x14479
leaq 0xc0(%rbx), %r13
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
cmpl $0x2, 0x4(%rbx)
jl 0x143c7
xorl %r15d, %r15d
movslq 0xc(%rsp), %rax
movl (%rbx), %ecx
decl %ecx
cmpl %ecx, %eax
jge 0x143c7
movq 0xc8(%rbx), %rdx
movq %r13, %rcx
testq %rdx, %rdx
je 0x14329
movq %r13, %rcx
xorl %esi, %esi
cmpl %eax, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x14312
movq %r13, %rdx
cmpq %r13, %rcx
je 0x1433b
cmpl 0x20(%rcx), %eax
cmovlq %r13, %rcx
movq %rcx, %rdx
cmpq %r13, %rdx
je 0x143b2
movq 0x148(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x8(%rcx), %rdx
cmpq (%rcx), %rdx
je 0x143b2
movq 0x150(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x143b2
leaq 0x381(%rip), %rax       # 0x146f0
movq %rax, 0x58(%rsp)
movq $0x0, 0x60(%rsp)
movq %rbx, 0x50(%rsp)
movq %r14, %rdi
leaq 0x58(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0xc(%rsp), %rcx
callq 0x28e0e
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x2c3f8
cmpq $0x0, 0x10(%rsp)
jne 0x146ba
incl 0xc(%rsp)
incl %r15d
movl 0x4(%rbx), %eax
decl %eax
cmpl %eax, %r15d
jl 0x142ef
movl 0xc(%rsp), %esi
cmpl (%rbx), %esi
jge 0x1441a
movq 0xc8(%rbx), %rcx
movq %r13, %rax
testq %rcx, %rcx
je 0x143f7
movq %r13, %rax
xorl %edx, %edx
cmpl %esi, 0x20(%rcx)
setl %dl
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x143e1
movq %r13, %rcx
cmpq %r13, %rax
je 0x14409
cmpl 0x20(%rax), %esi
cmovlq %r13, %rax
movq %rax, %rcx
cmpq %r13, %rcx
je 0x14416
movq %rbx, %rdi
callq 0x146f0
incl 0xc(%rsp)
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rbp
cmpq %rbp, %r15
je 0x14437
movq %r15, %rdi
callq 0x5580
addq $0x8, %r15
jmp 0x14424
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rax, %rdx
cmpq %rax, %rcx
je 0x14461
cmpq $0x0, (%rdx)
jne 0x146ba
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0x14449
movq %rax, 0x28(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x28ea6
incl %r12d
cmpl 0x1c(%rsp), %r12d
jne 0x142d1
cmpl $0x0, (%rbx)
jle 0x144b9
leaq 0x160(%rbx), %r14
xorl %r15d, %r15d
movq 0x158(%rbx), %rax
movq (%rax,%r15,8), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rcx
cmpq %rdx, %rcx
je 0x144ae
movq 0x168(%rbx), %rsi
movq %r14, %rdi
callq 0x2c596
incq %r15
movslq (%rbx), %rax
cmpq %rax, %r15
jl 0x14488
leaq 0x28(%rsp), %r13
xorl %eax, %eax
movl %eax, (%r13)
movq %rax, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rax, 0x20(%r13)
leaq 0x60(%rsp), %rbp
movl %eax, (%rbp)
movq %rax, 0x8(%rbp)
movq %rbp, 0x10(%rbp)
movq %rbp, 0x18(%rbp)
movq %rax, 0x20(%rbp)
cmpl %eax, (%rbx)
jle 0x1462c
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
movq 0x158(%rbx), %rax
movq (%rax,%r12,8), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x1461d
xorl %r14d, %r14d
leaq (%r14,%r14,2), %rcx
movq 0x8(%rax,%rcx,8), %rdx
subq (%rax,%rcx,8), %rdx
shrq $0x2, %rdx
imull $0xaaaaaaab, %edx, %eax # imm = 0xAAAAAAAB
movl %eax, 0x10(%rsp)
movq 0x30(%rsp), %rdx
movq %r13, %rcx
testq %rdx, %rdx
je 0x14559
movq %r13, %rcx
xorl %esi, %esi
cmpl %eax, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x14542
movq %r13, %rdx
cmpq %r13, %rcx
je 0x1456b
cmpl %eax, 0x20(%rcx)
cmovgq %r13, %rcx
movq %rcx, %rdx
cmpq %r13, %rdx
je 0x14581
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x28ee2
incl (%rax)
jmp 0x14594
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x28ee2
movl $0x1, (%rax)
movq 0x68(%rsp), %rcx
movq %rbp, %rax
testq %rcx, %rcx
je 0x145bf
movl 0x10(%rsp), %edx
movq %rbp, %rax
xorl %esi, %esi
cmpl %edx, 0x20(%rcx)
setl %sil
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x145a8
movq %rbp, %rcx
cmpq %rbp, %rax
je 0x145d5
movl 0x10(%rsp), %ecx
cmpl 0x20(%rax), %ecx
cmovlq %rbp, %rax
movq %rax, %rcx
cmpq %rbp, %rcx
jne 0x145ea
leaq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x28ee2
movl %r12d, (%rax)
incq %r14
movq 0x158(%rbx), %rax
movq (%rax,%r12,8), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %r14, %rcx
ja 0x14517
incq %r12
movslq (%rbx), %rax
cmpq %rax, %r12
jl 0x144fc
cmpl $0x0, (%rbx)
jle 0x14677
xorl %r15d, %r15d
movq 0x158(%rbx), %rax
movq (%rax,%r15,8), %rdi
movq (%rdi), %rsi
callq 0x2cd84
movq 0x158(%rbx), %rax
movq (%rax,%r15,8), %r14
testq %r14, %r14
je 0x1466c
movq %r14, %rdi
callq 0x2890a
movl $0x18, %esi
movq %r14, %rdi
callq 0x5340
incq %r15
movslq (%rbx), %rax
cmpq %rax, %r15
jl 0x14634
movq 0x158(%rbx), %rdi
testq %rdi, %rdi
je 0x14688
callq 0x53f0
leaq 0x58(%rsp), %rdi
callq 0x29ea4
leaq 0x20(%rsp), %rdi
callq 0x29ea4
movb $0x1, %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x146c1
cmpq $0x0, 0x10(%rsp)
je 0x146c1
callq 0x50d0
jmp 0x146c1
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x28ea6
jmp 0x146e7
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x29ea4
leaq 0x20(%rsp), %rdi
callq 0x29ea4
movq %rbx, %rdi
callq 0x55c0
nop