// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movq %rdi, %rbx
movslq (%rdi), %r15
leaq (,%r15,8), %rax
testq %r15, %r15
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x5030
movq %rax, 0x158(%rbx)
xorl %r14d, %r14d
testl %r15d, %r15d
movl $0x0, %r12d
cmovgl %r15d, %r12d
cmpq %r14, %r12
je 0xefdd
pushq $0x18
popq %rdi
callq 0x5300
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x158(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
jmp 0xefb5
movl 0x4(%rbx), %ecx
movl %r15d, %eax
cltd
idivl %ecx
imull %eax, %ecx
xorl %edx, %edx
cmpl %r15d, %ecx
setl %dl
addl %eax, %edx
leaq 0xc(%rsp), %r15
andl $0x0, (%r15)
leaq 0xb8(%rbx), %r12
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
movl %edx, 0x1c(%rsp)
leaq 0xc0(%rbx), %rbp
leaq 0x10(%rsp), %r13
cmpl 0x1c(%rsp), %eax
je 0xf148
movq %rax, 0x58(%rsp)
andq $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
movl 0xc(%rsp), %ecx
movl (%rbx), %eax
movl 0x4(%rbx), %edx
decl %edx
cmpl %edx, %r14d
jge 0xf0eb
leal -0x1(%rax), %edx
cmpl %edx, %ecx
jge 0xf0eb
movq %r12, %rdi
movq %r15, %rsi
callq 0x1d902
movl 0xc(%rsp), %ecx
cmpq %rbp, %rax
je 0xf0dd
movslq %ecx, %rax
movq 0x148(%rbx), %rdx
movq (%rdx,%rax,8), %rdx
movq 0x8(%rdx), %rsi
cmpq (%rdx), %rsi
je 0xf0dd
movq 0x150(%rbx), %rdx
movq (%rdx,%rax,8), %rax
movq 0x8(%rax), %rdx
cmpq (%rax), %rdx
je 0xf0dd
leaq 0x2a3(%rip), %rax       # 0xf342
movq %rax, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
movq %rbx, 0x60(%rsp)
movq %r13, %rdi
leaq 0x68(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movq %r15, %rcx
callq 0x19ec6
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x1d984
movq %r13, %rdi
callq 0x19f4a
movl 0xc(%rsp), %ecx
incl %ecx
movl %ecx, 0xc(%rsp)
incl %r14d
jmp 0xf03e
cmpl %eax, %ecx
jge 0xf10f
movq %r12, %rdi
movq %r15, %rsi
callq 0x1d902
cmpq %rbp, %rax
je 0xf10b
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xf342
incl 0xc(%rsp)
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x20eb0(%rip), %rdx     # 0x2ffd0
xorl %ecx, %ecx
callq 0x19f57
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x19fa0
movq %r14, %rdi
callq 0x19fa8
movq 0x58(%rsp), %rax
incl %eax
jmp 0xf01a
leaq 0x160(%rbx), %r15
xorl %r14d, %r14d
movslq (%rbx), %rax
cmpq %rax, %r14
jge 0xf185
movq 0x158(%rbx), %rax
movq (%rax,%r14,8), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rcx
cmpq %rdx, %rcx
je 0xf180
movq 0x168(%rbx), %rsi
movq %r15, %rdi
callq 0x19fca
incq %r14
jmp 0xf152
leaq 0x28(%rsp), %rcx
andl $0x0, (%rcx)
andq $0x0, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
leaq 0x70(%rsp), %rcx
andl $0x0, (%rcx)
andq $0x0, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
xorl %r14d, %r14d
leaq 0x20(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0x68(%rsp), %r13
movslq %eax, %rcx
cmpq %rcx, %r14
jge 0xf280
xorl %ebp, %ebp
movq 0x158(%rbx), %rax
movq (%rax,%r14,8), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rbp, %rax
jbe 0xf276
movq %rbp, %rsi
callq 0x19fea
movq 0x8(%rax), %rcx
subq (%rax), %rcx
movq %rcx, %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
movl %eax, 0x10(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e11c
leaq 0x28(%rsp), %rcx
cmpq %rcx, %rax
je 0xf23a
movq %r15, %rdi
movq %r12, %rsi
callq 0x1a008
incl (%rax)
jmp 0xf24b
movq %r15, %rdi
movq %r12, %rsi
callq 0x1a008
movl $0x1, (%rax)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1e11c
leaq 0x70(%rsp), %rcx
cmpq %rcx, %rax
jne 0xf26e
movq %r13, %rdi
movq %r12, %rsi
callq 0x1a008
movl %r14d, (%rax)
incq %rbp
jmp 0xf1d9
incq %r14
movl (%rbx), %eax
jmp 0xf1cb
xorl %r12d, %r12d
pushq $0x18
popq %r14
cltq
movq 0x158(%rbx), %rdi
cmpq %rax, %r12
jge 0xf2c8
movq (%rdi,%r12,8), %rdi
callq 0x1a056
movq 0x158(%rbx), %rax
movq (%rax,%r12,8), %r15
testq %r15, %r15
je 0xf2b6
movq %r15, %rdi
callq 0x19a0e
movq %r15, %rdi
movq %r14, %rsi
callq 0x5310
incq %r12
movl (%rbx), %eax
jmp 0xf287
testq %rdi, %rdi
je 0xf2d2
callq 0x53a0
leaq 0x68(%rsp), %rdi
callq 0x1b0cc
leaq 0x20(%rsp), %rdi
callq 0x1b0cc
movb $0x1, %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf313
jmp 0xf313
jmp 0xf313
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x19f4a
jmp 0xf316
jmp 0xf322
jmp 0xf322
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x19fa8
jmp 0xf339
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x1b0cc
leaq 0x20(%rsp), %rdi
callq 0x1b0cc
movq %rbx, %rdi
callq 0x5540
nop