// File: /workspace/github_cpp_makefile_star2_O3/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 %r14, %rsi
movq %rax, 0x158(%rbx)
testq %r14, %r14
jle 0x141f7
xorl %r14d, %r14d
movl $0x18, %edi
movq %rsi, %r15
callq 0x5330
movq %r15, %rsi
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x158(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
cmpq %r14, %r15
jne 0x141c6
movl 0x4(%rbx), %ecx
movl %esi, %eax
cltd
idivl %ecx
imull %eax, %ecx
xorl %edx, %edx
cmpl %esi, %ecx
setl %dl
movl $0x0, 0xc(%rsp)
addl %eax, %edx
movl %edx, 0x1c(%rsp)
jle 0x143ce
leaq 0xc0(%rbx), %r13
xorl %r14d, %r14d
leaq 0x10(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movl 0x4(%rbx), %eax
movl 0xc(%rsp), %esi
cmpl $0x2, %eax
jl 0x14325
xorl %r15d, %r15d
movl (%rbx), %ecx
decl %ecx
cmpl %ecx, %esi
jge 0x14325
movq 0xc8(%rbx), %rcx
testq %rcx, %rcx
je 0x14310
movq %r13, %rdx
xorl %edi, %edi
cmpl %esi, 0x20(%rcx)
setl %dil
cmovgeq %rcx, %rdx
movq 0x10(%rcx,%rdi,8), %rcx
testq %rcx, %rcx
jne 0x1426f
cmpq %r13, %rdx
je 0x14310
cmpl 0x20(%rdx), %esi
jl 0x14310
movq 0x148(%rbx), %rdx
movslq %esi, %rcx
movq (%rdx,%rcx,8), %rdx
movq 0x8(%rdx), %rdi
cmpq (%rdx), %rdi
je 0x14310
movq 0x150(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movq 0x8(%rcx), %rdx
cmpq (%rcx), %rdx
je 0x14310
leaq 0x388(%rip), %rax       # 0x1464e
movq %rax, 0x58(%rsp)
movq $0x0, 0x60(%rsp)
movq %rbx, 0x50(%rsp)
movq %r12, %rdi
leaq 0x58(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0xc(%rsp), %rcx
callq 0x2898e
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x2be7c
cmpq $0x0, 0x10(%rsp)
jne 0x1461a
movl 0xc(%rsp), %esi
movl 0x4(%rbx), %eax
incl %esi
movl %esi, 0xc(%rsp)
incl %r15d
leal -0x1(%rax), %ecx
cmpl %ecx, %r15d
jl 0x14250
cmpl (%rbx), %esi
jge 0x1436a
movq 0xc8(%rbx), %rax
testq %rax, %rax
je 0x14364
movq %r13, %rcx
xorl %edx, %edx
cmpl %esi, 0x20(%rax)
setl %dl
cmovgeq %rax, %rcx
movq 0x10(%rax,%rdx,8), %rax
testq %rax, %rax
jne 0x14338
cmpq %r13, %rcx
je 0x14364
cmpl 0x20(%rcx), %esi
jl 0x14364
movq %rbx, %rdi
callq 0x1464e
movl 0xc(%rsp), %esi
incl %esi
movl %esi, 0xc(%rsp)
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rbp
cmpq %rbp, %r15
je 0x143b4
movq %r15, %rdi
callq 0x5580
addq $0x8, %r15
cmpq %rbp, %r15
jne 0x14379
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rax, %rdx
cmpq %rax, %rcx
je 0x143b4
cmpq $0x0, (%rdx)
jne 0x1461a
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0x1439c
movq %rax, 0x28(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x28a16
incl %r14d
cmpl 0x1c(%rsp), %r14d
jne 0x1422c
movl (%rbx), %esi
testl %esi, %esi
jle 0x1440f
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 0x14404
movq 0x168(%rbx), %rsi
movq %r14, %rdi
callq 0x2c018
movl (%rbx), %esi
incq %r15
movslq %esi, %rax
cmpq %rax, %r15
jl 0x143dc
leaq 0x28(%rsp), %rbp
xorl %eax, %eax
movl %eax, (%rbp)
movq %rax, 0x8(%rbp)
movq %rbp, 0x10(%rbp)
movq %rbp, 0x18(%rbp)
movq %rax, 0x20(%rbp)
leaq 0x60(%rsp), %r13
movl %eax, (%r13)
movq %rax, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rax, 0x20(%r13)
testl %esi, %esi
jle 0x145d5
movq 0x158(%rbx), %rax
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
movq (%rax,%r12,8), %rdx
movq (%rdx), %rcx
cmpq %rcx, 0x8(%rdx)
je 0x1457c
xorl %r14d, %r14d
leaq (%r14,%r14,2), %rax
movq 0x8(%rcx,%rax,8), %rdx
subq (%rcx,%rax,8), %rdx
shrq $0x2, %rdx
imull $0xaaaaaaab, %edx, %eax # imm = 0xAAAAAAAB
movl %eax, 0x10(%rsp)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x144cb
movq %rbp, %r8
movq %r8, %rcx
movq %rdi, %rdx
movl 0x20(%rdi), %esi
xorl %edi, %edi
cmpl %eax, %esi
setl %dil
movq %rdx, %r8
cmovlq %rcx, %r8
movq 0x10(%rdx,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x14495
cmpq %rbp, %r8
je 0x144cb
cmpl %eax, %esi
cmovlq %rcx, %rdx
cmpl %eax, 0x20(%rdx)
jle 0x14566
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x28a52
movl $0x1, (%rax)
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x14521
movl 0x10(%rsp), %eax
movq %r13, %r8
movq %r8, %rcx
movq %rdi, %rdx
movl 0x20(%rdi), %esi
xorl %edi, %edi
cmpl %eax, %esi
setl %dil
movq %rdx, %r8
cmovlq %rcx, %r8
movq 0x10(%rdx,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x144ef
cmpq %r13, %r8
je 0x14521
cmpl %eax, %esi
cmovlq %rcx, %rdx
cmpl 0x20(%rdx), %eax
jge 0x14531
leaq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x28a52
movl %r12d, (%rax)
incq %r14
movq 0x158(%rbx), %rax
movq (%rax,%r12,8), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq %r14, %rdx
ja 0x1446d
jmp 0x1457a
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x28a52
incl (%rax)
jmp 0x144de
movl (%rbx), %esi
incq %r12
movslq %esi, %rcx
cmpq %rcx, %r12
jl 0x14459
testl %esi, %esi
jle 0x145d5
xorl %r15d, %r15d
movq 0x158(%rbx), %rax
movq (%rax,%r15,8), %rdi
movq (%rdi), %rsi
callq 0x2c7e2
movq 0x158(%rbx), %rax
movq (%rax,%r15,8), %r14
testq %r14, %r14
je 0x145bd
movq %r14, %rdi
callq 0x28498
movl $0x18, %esi
movq %r14, %rdi
callq 0x5340
incq %r15
movslq (%rbx), %rax
cmpq %rax, %r15
jl 0x14592
movq 0x158(%rbx), %rdi
testq %rdi, %rdi
je 0x145e6
callq 0x53f0
leaq 0x58(%rsp), %rdi
callq 0x29a0e
leaq 0x20(%rsp), %rdi
callq 0x29a0e
movb $0x1, %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1461f
jmp 0x1461f
cmpq $0x0, 0x10(%rsp)
je 0x1461f
callq 0x50d0
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x28a16
jmp 0x14645
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x29a0e
leaq 0x20(%rsp), %rdi
callq 0x29a0e
movq %rbx, %rdi
callq 0x55c0
nop