// File: 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
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x130(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x0, %eax
jle 0x3c4ca
movq $0x0, -0x28(%rbp)
movl $0xf8, %edi
callq 0x52d0
movq %rax, %rdi
movq -0x130(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x138(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsbl 0x8c8(%rax), %r9d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r10d
movq %rsp, %rax
movl %r10d, (%rax)
callq 0x114e0
jmp 0x3c03d
movq -0x130(%rbp), %rdi
movq -0x138(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x150(%rbp)
xorl %esi, %esi
callq 0x39b90
movq -0x130(%rbp), %rdi
movq %rax, -0x148(%rbp)
xorl %esi, %esi
callq 0x39c00
movq -0x130(%rbp), %rdi
movq %rax, -0x140(%rbp)
xorl %esi, %esi
callq 0x39c70
movq -0x150(%rbp), %rdi
movq -0x148(%rbp), %rsi
movq -0x140(%rbp), %rdx
movq %rax, %rcx
movq -0x130(%rbp), %rax
movq 0x88(%rax), %r8
movl 0x8dc(%r8), %r8d
movq 0x88(%rax), %rax
movq 0x8f0(%rax), %r9
callq 0x11e00
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x3c115
movq -0x50(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x3c115
movq -0x138(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl $0xf8, %esi
callq 0x52e0
jmp 0x3c92a
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x3c13b
movq -0x50(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x3c161
movq -0x50(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x50(%rbp), %rdi
movl $0x2, %esi
callq 0x12130
movq -0x50(%rbp), %rdi
movl $0x7, %esi
callq 0x12130
movq -0x50(%rbp), %rdi
movl $0x3, %esi
callq 0x12130
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x3c1cc
movq -0x50(%rbp), %rdi
movl $0x8, %esi
callq 0x12130
movq -0x50(%rbp), %rdi
movl $0x6, %esi
callq 0x12130
movq -0x50(%rbp), %rdi
movl $0x4, %esi
callq 0x12130
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movslq 0x8dc(%rax), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x68(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x130(%rbp), %rcx
movl -0x44(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x3c268
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x8e8(%rcx), %rcx
movslq -0x44(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rcx
movl 0x58(%rcx), %esi
movq -0x68(%rbp), %rcx
movslq -0x44(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x88(%rax), %rax
movq 0x8e8(%rax), %rax
movslq -0x44(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x58(%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x3c1f6
movq -0x130(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x3fb70
leaq -0x40(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movl $0x0, -0x44(%rbp)
movq -0x130(%rbp), %rcx
movl -0x44(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x3c2ed
movq -0x130(%rbp), %rax
movq -0x68(%rbp), %rcx
movslq -0x44(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movq 0x88(%rax), %rax
movq 0x8e8(%rax), %rax
movslq -0x44(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x58(%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x3c29b
movq -0x68(%rbp), %rdi
callq 0x5380
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x3c340
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movl 0x8dc(%rax), %esi
movl -0x1c(%rbp), %edx
movl -0x40(%rbp), %ecx
movsd -0x38(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
movl -0x20(%rbp), %r8d
leaq 0x14401(%rip), %rdi     # 0x5073a
movb $0x2, %al
callq 0x5060
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x2, %eax
jne 0x3c49a
movl $0x0, -0x94(%rbp)
movq -0x130(%rbp), %rcx
movl -0x94(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x3c498
movq -0x130(%rbp), %rdi
movl -0x94(%rbp), %esi
callq 0x39b90
movq %rax, %rcx
movq -0x130(%rbp), %rax
movq %rcx, -0xa0(%rbp)
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rdi
movq -0xa0(%rbp), %rsi
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %edx
callq 0xc440
movq -0x130(%rbp), %rdi
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x160(%rbp)
movl -0x94(%rbp), %esi
callq 0x39c00
movq -0x160(%rbp), %rdi
movq %rax, %rsi
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rcx
movsbl 0x8c8(%rcx), %edx
movq 0x88(%rax), %rcx
movsbl 0x8b8(%rcx), %ecx
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %r8d
callq 0x11450
movq -0x130(%rbp), %rdi
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x158(%rbp)
movl -0x94(%rbp), %esi
callq 0x39c70
movq -0x158(%rbp), %rdi
movq %rax, %rsi
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rcx
movsbl 0x8c8(%rcx), %edx
movq 0x88(%rax), %rcx
movsbl 0x8b8(%rcx), %ecx
movq 0x88(%rax), %rax
movsbl 0x8b9(%rax), %r8d
callq 0x11450
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x3c365
jmp 0x3c49a
movq -0x50(%rbp), %rax
movq %rax, -0x168(%rbp)
cmpq $0x0, %rax
je 0x3c4c8
movq -0x168(%rbp), %rdi
callq 0x117b0
movq -0x168(%rbp), %rdi
movl $0xf8, %esi
callq 0x52e0
jmp 0x3c4ca
movl $0x0, -0xc(%rbp)
movq -0x130(%rbp), %rcx
movl -0xc(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8c0(%rcx), %eax
jge 0x3c91c
movl $0x0, -0xc8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xb0(%rbp)
movl $0x0, -0xa8(%rbp)
movl $0x0, -0xa4(%rbp)
movb $0x0, -0xc9(%rbp)
movq -0x130(%rbp), %rcx
movsbl -0xc9(%rbp), %eax
movq 0x88(%rcx), %rcx
movsbl 0x8c8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3c849
movl $0xf8, %edi
callq 0x52d0
movq %rax, %rdi
movq -0x130(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x170(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsbl 0x8c8(%rax), %r9d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r10d
movq %rsp, %rax
movl %r10d, (%rax)
callq 0x114e0
jmp 0x3c5ac
movq -0x130(%rbp), %rdi
movq -0x170(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x188(%rbp)
movl -0xc(%rbp), %esi
callq 0x39b90
movq -0x130(%rbp), %rdi
movq %rax, -0x180(%rbp)
movl -0xc(%rbp), %esi
callq 0x39c00
movq -0x130(%rbp), %rdi
movq %rax, -0x178(%rbp)
movl -0xc(%rbp), %esi
callq 0x39c70
movq -0x188(%rbp), %rdi
movq -0x180(%rbp), %rsi
movq -0x178(%rbp), %rdx
movq %rax, %rcx
movq -0x130(%rbp), %rax
movq 0x88(%rax), %r8
movq 0x8e0(%r8), %r8
movslq -0xc(%rbp), %r9
movl (%r8,%r9,4), %r8d
movq 0x88(%rax), %rax
movq 0x8f8(%rax), %rax
movslq -0xc(%rbp), %r9
movq (%rax,%r9,8), %r9
callq 0x11e00
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
jne 0x3c679
movsbl -0xc9(%rbp), %eax
cmpl $0x0, %eax
jle 0x3c6ac
movq -0xd8(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x3c6ac
movq -0x170(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl $0xf8, %esi
callq 0x52e0
jmp 0x3c92a
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x3c6d5
movq -0xd8(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x130(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x3c6fe
movq -0xd8(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0xd8(%rbp), %rdi
movl $0x2, %esi
callq 0x12130
movq -0xd8(%rbp), %rdi
movl $0x7, %esi
callq 0x12130
movq -0x130(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0x128(%rbp), %rdi
callq 0x3fb70
leaq -0x100(%rbp), %rdi
leaq -0x128(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movl -0x100(%rbp), %eax
addl -0xc8(%rbp), %eax
movl %eax, -0xc8(%rbp)
movl -0xe0(%rbp), %eax
addl -0xa8(%rbp), %eax
movl %eax, -0xa8(%rbp)
movsbl -0xc9(%rbp), %eax
cmpl $0x0, %eax
jne 0x3c794
movsd -0xf8(%rbp), %xmm0
movsd %xmm0, -0x190(%rbp)
jmp 0x3c7a1
xorps %xmm0, %xmm0
movsd %xmm0, -0x190(%rbp)
jmp 0x3c7a1
movq -0x130(%rbp), %rcx
movsd -0x190(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsbl -0xc9(%rbp), %eax
movq 0x88(%rcx), %rcx
movsbl 0x8c8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3c7e6
movsd -0xe8(%rbp), %xmm0
movsd %xmm0, -0x198(%rbp)
jmp 0x3c7f3
xorps %xmm0, %xmm0
movsd %xmm0, -0x198(%rbp)
jmp 0x3c7f3
movsd -0x198(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x1a0(%rbp)
cmpq $0x0, %rax
je 0x3c834
movq -0x1a0(%rbp), %rdi
callq 0x117b0
movq -0x1a0(%rbp), %rdi
movl $0xf8, %esi
callq 0x52e0
jmp 0x3c836
movb -0xc9(%rbp), %al
addb $0x1, %al
movb %al, -0xc9(%rbp)
jmp 0x3c529
cmpl $0x0, -0xa8(%rbp)
jne 0x3c86f
movq -0x130(%rbp), %rcx
movl -0xc8(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x3c90c
movq -0x130(%rbp), %rax
movsd -0xb0(%rbp), %xmm0
movsd -0xb0(%rbp), %xmm1
xorps %xmm2, %xmm2
ucomisd %xmm2, %xmm1
seta %cl
andb $0x1, %cl
movzbl %cl, %ecx
cvtsi2sd %ecx, %xmm2
movsd -0x18(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x18(%rbp)
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x3c90a
movq -0x130(%rbp), %rax
movl -0xc(%rbp), %esi
movq 0x88(%rax), %rax
movq 0x8e0(%rax), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0xa4(%rbp), %ecx
movl -0xc8(%rbp), %r8d
movsd -0xc0(%rbp), %xmm0
movsd -0xb0(%rbp), %xmm1
movl -0xa8(%rbp), %r9d
leaq 0x13e84(%rip), %rdi     # 0x50787
movb $0x2, %al
callq 0x5060
jmp 0x3c90c
jmp 0x3c90e
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x3c4d1
movsd -0x18(%rbp), %xmm0
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x5540
nopw %cs:(%rax,%rax)