// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::BaumWelch()
NUMBER HMMProblemSlicedAB::BaumWelch() {
	NCAT k;
    NUMBER loglik = 0;
	
    //    bool conv_flags[3] = {true, true, true};
	
	//
	// fit all as 1 skill first
	//
    if(this->p->single_skill>0) {
        FitResult fr;
        fr.pO = 0;
        NCAT x;
        FitBitSlicedAB *fb = new FitBitSlicedAB(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->nZ, this->p->tol, this->p->tol_mode);
        fb->link( this->getPI(0), this->getA(0), this->getB(0), this->p->nSeq, this->p->k_data);// link skill 0 (we'll copy fit parameters to others
        if(this->p->block_fitting[0]!=0) fb->pi = NULL;
        if(this->p->block_fitting[1]!=0) fb->A  = NULL;
        if(this->p->block_fitting[2]!=0) fb->B  = NULL;
        
        fb->init(FBS_PARm1);
        fb->init(FBS_PARm2);
        fb->init(FBS_GRAD);
        if(this->p->solver==METHOD_CGD) {
            fb->init(FBS_DIR);
            fb->init(FBS_DIRm1);
            fb->init(FBS_GRADm1);
        }
        
        NCAT* original_ks = Calloc(NCAT, (size_t)this->p->nSeq);
        for(x=0; x<this->p->nSeq; x++) { original_ks[x] = this->p->all_data[x].k; this->p->all_data[x].k = 0; } // save original k's
        fr = BaumWelchBit(fb);
        for(x=0; x<this->p->nSeq; x++) { this->p->all_data[x].k = original_ks[x]; } // restore original k's
        free(original_ks);
        if(!this->p->quiet)
            printf("skill one, seq %4d, dat %8d, iter#%3d p(O|param)= %15.7f >> %15.7f, conv=%d\n",  this->p->nSeq, fr.ndat, fr.iter,fr.pO0,fr.pO,fr.conv);
        if(this->p->single_skill==2) {
            for(NCAT y=0; y<this->sizes[0]; y++) { // copy the rest
                NUMBER *aPI = this->getPI(y);
                cpy1D<NUMBER>(fb->pi, aPI, this->p->nS);
                cpy3D<NUMBER>(fb->A,  this->getA(y), this->p->nZ, this->p->nS, this->p->nS);
                cpy3D<NUMBER>(fb->B,  this->getB(y), this->p->nZ, this->p->nS, this->p->nO);
            }
        }// force single skill
        
        delete fb;
    }
	
	//
	// Main fit
	//
    
//    int parallel_now = this->p->parallel==1; //PAR
//    #pragma omp parallel if(parallel_now) //num_threads(2) //PAR
//    {//PAR
//        #pragma omp for schedule(dynamic) reduction(+:loglik) //PAR
        for(k=0; k<this->p->nK; k++) {
            FitResult fr;
            fr.iter = 0;
            fr.pO0  = 0.0;
            fr.pO   = 0.0;
            fr.conv = 0; // converged
            fr.ndat = 0;
            for(NPAR z=0; z<this->p->nZ; z++) {
                FitBitSlicedAB *fb = new FitBitSlicedAB(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->nZ, this->p->tol, this->p->tol_mode);
                fb->link(this->getPI(k), this->getA(k), this->getB(k), this->p->k_numg[k], this->p->k_g_data[k]);
                if(this->p->block_fitting[0]!=0 || z>0) fb->pi = NULL;
                if(this->p->block_fitting[1]!=0) fb->A  = NULL;
                if(this->p->block_fitting[2]!=0) fb->B  = NULL;
                
                fb->init(FBS_PARm1);
                fb->init(FBS_PARm2);
                
                FitResult fr_loc;
                fr_loc = BaumWelchBit(fb);
                fr.iter+=fr_loc.iter;
                fr.conv+=fr_loc.conv;
                fr.pO0=(z==0)?fr_loc.pO0:0;
                fr.pO =(z==(this->p->nZ-1))?fr_loc.pO:0;
                delete fb;
            }
            if( ( /*(!conv && iter<this->p->maxiter) ||*/ (fr.conv || fr.iter==this->p->maxiter) )) {
                loglik += fr.pO*(fr.pO>0); // reduction'ed
                if(!this->p->quiet)
                    printf("skill %4d, seq %4d, dat %8d, iter#%3d p(O|param)= %15.7f >> %15.7f, conv=%d\n", k,  this->p->k_numg[k], fr.ndat, fr.iter,fr.pO0,fr.pO,fr.conv);
            }
        } // for all skills
//    }//PAR
    return loglik;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq 0x88(%rdi), %r15
cmpb $0x0, 0x3d(%r15)
jle 0x21747
movl $0xf8, %edi
callq 0x4220
movq %rax, %r14
movsbl 0x8b8(%r15), %esi
movsbl 0x8b9(%r15), %edx
movl 0x8c0(%r15), %ecx
movl 0x8bc(%r15), %r8d
movsbl 0x8c8(%r15), %r9d
movsd 0x28(%r15), %xmm0
movsbl 0x30(%r15), %eax
movl %eax, (%rsp)
movq %r14, %rdi
callq 0xa20c
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1ff6a
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1ffa2
movq %rax, %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1ffde
movq 0x88(%rbx), %rcx
movl 0x8dc(%rcx), %r8d
movq 0x8f0(%rcx), %r9
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0xa622
movq 0x88(%rbx), %rax
cmpb $0x0, 0x981(%rax)
je 0x21509
andq $0x0, 0x10(%r14)
cmpb $0x0, 0x982(%rax)
je 0x21517
andq $0x0, 0x18(%r14)
cmpb $0x0, 0x983(%rax)
je 0x21525
andq $0x0, 0x20(%r14)
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0xa854
pushq $0x7
popq %rsi
movq %r14, %rdi
callq 0xa854
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0xa854
movq 0x88(%rbx), %r13
cmpb $0x3, 0x3f(%r13)
jne 0x2157c
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0xa854
pushq $0x6
popq %rsi
movq %r14, %rdi
callq 0xa854
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0xa854
movq 0x88(%rbx), %r13
movslq 0x8dc(%r13), %r12
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0x4120
movq %rax, %r15
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
imulq $0x18, %rcx, %rcx
movq %r15, %rdx
cmpq %rax, %rcx
je 0x215c7
movq 0x8e8(%r13), %rsi
movl 0x58(%rsi,%rax,4), %edi
movl %edi, (%rdx)
andl $0x0, 0x58(%rsi,%rax,4)
addq $0x18, %rax
addq $0x4, %rdx
jmp 0x215a6
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x233a4
movl (%r13), %eax
movl %eax, 0x20(%rsp)
movsd 0x8(%r13), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x18(%r13), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x20(%r13), %eax
movl %eax, 0x10(%rsp)
movl 0x24(%r13), %r13d
movq 0x88(%rbx), %r12
movl 0x8dc(%r12), %ebp
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %ecx
cmovgl %ebp, %ecx
imulq $0x18, %rcx, %rcx
movq %r15, %rdx
cmpq %rax, %rcx
je 0x21645
movl (%rdx), %esi
movq 0x8e8(%r12), %rdi
movl %esi, 0x58(%rdi,%rax,4)
addq $0x18, %rax
addq $0x4, %rdx
jmp 0x21628
movq %r15, %rdi
callq 0x4290
cmpb $0x0, 0x3c(%r12)
jne 0x21684
leaq 0xc09a(%rip), %rdi      # 0x2d6f6
movl %ebp, %esi
movl %r13d, %edx
movl 0x20(%rsp), %ecx
movsd 0x8(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movl 0x10(%rsp), %r8d
movb $0x2, %al
callq 0x4050
movq 0x88(%rbx), %r12
cmpb $0x2, 0x3d(%r12)
jne 0x21732
xorl %ebp, %ebp
cmpl 0xc(%rbx), %ebp
jge 0x21732
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1ff6a
movq 0x10(%r14), %rsi
movq 0x88(%rbx), %rcx
movsbq 0x8b8(%rcx), %rdx
shlq $0x3, %rdx
movq %rax, %rdi
callq 0x41c0
movq 0x18(%r14), %r15
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1ffa2
movq 0x88(%rbx), %rcx
movsbl 0x8c8(%rcx), %edx
movsbl 0x8b8(%rcx), %ecx
movq %r15, %rdi
movq %rax, %rsi
movl %ecx, %r8d
callq 0xa1a2
movq 0x20(%r14), %r15
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1ffde
movq 0x88(%rbx), %rsi
movsbl 0x8c8(%rsi), %edx
movsbl 0x8b8(%rsi), %ecx
movsbl 0x8b9(%rsi), %r8d
movq %r15, %rdi
movq %rax, %rsi
callq 0xa1a2
incl %ebp
jmp 0x21692
movq %r14, %rdi
callq 0xa2b2
movl $0xf8, %esi
movq %r14, %rdi
callq 0x4230
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
movq 0x88(%rbx), %r12
movslq 0x8c0(%r12), %rax
cmpq %rax, %r14
jge 0x219af
movq %r14, 0x18(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
xorpd %xmm1, %xmm1
xorl %r14d, %r14d
xorl %ebp, %ebp
movsbl 0x8c8(%r12), %eax
cmpl %eax, %r13d
jge 0x21926
movl $0xf8, %edi
callq 0x4220
movq %rax, %r15
movsbl 0x8b8(%r12), %esi
movsbl 0x8b9(%r12), %edx
movl 0x8c0(%r12), %ecx
movl 0x8bc(%r12), %r8d
movsbl 0x8c8(%r12), %r9d
movsd 0x28(%r12), %xmm0
movsbl 0x30(%r12), %eax
movl %eax, (%rsp)
movq %r15, %rdi
callq 0xa20c
movl %r14d, 0x8(%rsp)
movl %ebp, 0x20(%rsp)
movq %rbx, %rdi
movq 0x18(%rsp), %r14
movl %r14d, %esi
callq 0x1ff6a
movq %rax, %r12
movq %rbx, %rdi
movl %r14d, %esi
callq 0x1ffa2
movq %rax, %rbp
movq %rbx, %rdi
movl %r14d, %esi
callq 0x1ffde
movq 0x88(%rbx), %rcx
movq 0x8e0(%rcx), %rdx
movq 0x8f8(%rcx), %rcx
movl (%rdx,%r14,4), %r8d
movq (%rcx,%r14,8), %r9
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %rax, %rcx
callq 0xa622
movq %rbx, %rcx
movq 0x88(%rbx), %rax
testl %r13d, %r13d
jne 0x21864
cmpb $0x0, 0x981(%rax)
je 0x21869
andq $0x0, 0x10(%r15)
cmpb $0x0, 0x982(%rax)
movl 0x20(%rsp), %ebp
movl 0x8(%rsp), %r14d
je 0x21880
andq $0x0, 0x18(%r15)
cmpb $0x0, 0x983(%rax)
movq %rcx, %rbx
je 0x21891
andq $0x0, 0x20(%r15)
movq %r15, %rdi
pushq $0x2
popq %rsi
callq 0xa854
movq %r15, %rdi
pushq $0x7
popq %rsi
callq 0xa854
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x233a4
addl 0x30(%rsp), %r14d
movsd 0x48(%rsp), %xmm0
addl 0x50(%rsp), %ebp
testl %r13d, %r13d
je 0x218d1
xorpd %xmm1, %xmm1
jmp 0x218d7
movsd 0x38(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0x88(%rbx), %rax
movsbl 0x8c8(%rax), %eax
decl %eax
cmpl %eax, %r13d
je 0x218f6
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r15, %rdi
callq 0xa2b2
movl $0xf8, %esi
movq %r15, %rdi
callq 0x4230
movsd 0x20(%rsp), %xmm1
incl %r13d
movq 0x88(%rbx), %r12
jmp 0x21787
testl %ebp, %ebp
jne 0x21931
cmpl 0x38(%r12), %r14d
jne 0x21996
movl %r14d, %r8d
xorpd %xmm2, %xmm2
cmpltsd %xmm1, %xmm2
movsd 0xafab(%rip), %xmm0     # 0x2c8f0
andpd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd 0x10(%rsp), %xmm2
cmpb $0x0, 0x3c(%r12)
movq 0x18(%rsp), %r14
jne 0x219a1
movq 0x8e0(%r12), %rax
movl (%rax,%r14,4), %edx
leaq 0xbdd0(%rip), %rdi      # 0x2d743
movl %r14d, %esi
xorl %ecx, %ecx
movsd 0x8(%rsp), %xmm0
movl %ebp, %r9d
movb $0x2, %al
movapd %xmm2, 0x20(%rsp)
callq 0x4050
movapd 0x20(%rsp), %xmm2
jmp 0x219a1
movsd 0x10(%rsp), %xmm2
movq 0x18(%rsp), %r14
incq %r14
movsd %xmm2, 0x10(%rsp)
jmp 0x21754
movsd 0x10(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xf8, %esi
movq %r14, %rdi
jmp 0x219dc
movq %rax, %rbx
movl $0xf8, %esi
movq %r15, %rdi
callq 0x4230
movq %rbx, %rdi
callq 0x4450
nop