// File: myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::BaumWelch()
NUMBER HMMProblem::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;
        FitBit *fb = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, 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);
                NUMBER **aA = this->getA(y);
                NUMBER **aB = this->getB(y);
                cpy3Params(fb->pi, fb->A, fb->B, aPI, aA, aB, 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++) {
            FitBit *fb = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, 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) 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;
            fr = BaumWelchBit(fb);
            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 $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x110(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x0, %eax
jle 0x1f074
movq $0x0, -0x28(%rbp)
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x110(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x118(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r9d
callq 0xd080
jmp 0x1ec3f
movq -0x110(%rbp), %rdi
movq -0x118(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x130(%rbp)
xorl %esi, %esi
callq 0x1a560
movq -0x110(%rbp), %rdi
movq %rax, -0x128(%rbp)
xorl %esi, %esi
callq 0x1a5d0
movq -0x110(%rbp), %rdi
movq %rax, -0x120(%rbp)
xorl %esi, %esi
callq 0x1a640
movq -0x130(%rbp), %rdi
movq -0x128(%rbp), %rsi
movq -0x120(%rbp), %rdx
movq %rax, %rcx
movq -0x110(%rbp), %rax
movq 0x88(%rax), %r8
movl 0x8dc(%r8), %r8d
movq 0x88(%rax), %rax
movq 0x8f0(%rax), %r9
callq 0xd990
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x1ed17
movq -0x50(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x1ed17
movq -0x118(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x1f395
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x1ed3d
movq -0x50(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x1ed63
movq -0x50(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x50(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0x50(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0x50(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x1edce
movq -0x50(%rbp), %rdi
movl $0x8, %esi
callq 0xdcb0
movq -0x50(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0x50(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movslq 0x8dc(%rax), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x68(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x110(%rbp), %rcx
movl -0x44(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x1ee6a
movq -0x110(%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 0x1edf8
movq -0x110(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x24d10
leaq -0x40(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movl $0x0, -0x44(%rbp)
movq -0x110(%rbp), %rcx
movl -0x44(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x1eeef
movq -0x110(%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 0x1ee9d
movq -0x68(%rbp), %rdi
callq 0x5380
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x1ef42
movq -0x110(%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 0x317ff(%rip), %rdi     # 0x5073a
movb $0x2, %al
callq 0x5060
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x2, %eax
jne 0x1f044
movl $0x0, -0x94(%rbp)
movq -0x110(%rbp), %rcx
movl -0x94(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x1f042
movq -0x110(%rbp), %rdi
movl -0x94(%rbp), %esi
callq 0x1a560
movq -0x110(%rbp), %rdi
movq %rax, -0xa0(%rbp)
movl -0x94(%rbp), %esi
callq 0x1a5d0
movq -0x110(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movl -0x94(%rbp), %esi
callq 0x1a640
movq -0x110(%rbp), %rdi
movq %rax, -0xb0(%rbp)
movq -0x50(%rbp), %rsi
addq $0x10, %rsi
movq -0x50(%rbp), %rdx
addq $0x18, %rdx
movq -0x50(%rbp), %rcx
addq $0x20, %rcx
movq 0x88(%rdi), %rax
movb 0x8b8(%rax), %r10b
movq 0x88(%rdi), %rax
leaq -0xa0(%rbp), %r8
leaq -0xa8(%rbp), %r9
leaq -0xb0(%rbp), %r11
movq %r11, (%rsp)
movsbl %r10b, %r10d
movl %r10d, 0x8(%rsp)
movsbl 0x8b9(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0x1f3a0
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x1ef67
jmp 0x1f044
movq -0x50(%rbp), %rax
movq %rax, -0x138(%rbp)
cmpq $0x0, %rax
je 0x1f072
movq -0x138(%rbp), %rdi
callq 0xd3a0
movq -0x138(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x1f074
movl $0x0, -0xc(%rbp)
movq -0x110(%rbp), %rcx
movl -0xc(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8c0(%rcx), %eax
jge 0x1f387
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x110(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x140(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r9d
callq 0xd080
jmp 0x1f0e9
movq -0x110(%rbp), %rdi
movq -0x140(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x158(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a560
movq -0x110(%rbp), %rdi
movq %rax, -0x150(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a5d0
movq -0x110(%rbp), %rdi
movq %rax, -0x148(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a640
movq -0x158(%rbp), %rdi
movq -0x150(%rbp), %rsi
movq -0x148(%rbp), %rdx
movq %rax, %rcx
movq -0x110(%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 0xd990
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x1f1dd
movq -0xb8(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x1f1dd
movq -0x140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x1f395
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x1f206
movq -0xb8(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x110(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x1f22f
movq -0xb8(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0xb8(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0xb8(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0x110(%rbp), %rsi
movq -0xb8(%rbp), %rdx
leaq -0x108(%rbp), %rdi
callq 0x24d10
leaq -0xe0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movq -0xb8(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpq $0x0, %rax
je 0x1f2b4
movq -0x160(%rbp), %rdi
callq 0xd3a0
movq -0x160(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
cmpl $0x0, -0xc0(%rbp)
jne 0x1f2da
movq -0x110(%rbp), %rcx
movl -0xe0(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x1f377
movq -0x110(%rbp), %rax
movsd -0xc8(%rbp), %xmm0
movsd -0xc8(%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 0x1f375
movq -0x110(%rbp), %rax
movl -0xc(%rbp), %esi
movq 0x88(%rax), %rax
movq 0x8e0(%rax), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0xbc(%rbp), %ecx
movl -0xe0(%rbp), %r8d
movsd -0xd8(%rbp), %xmm0
movsd -0xc8(%rbp), %xmm1
movl -0xc0(%rbp), %r9d
leaq 0x31419(%rip), %rdi     # 0x50787
movb $0x2, %al
callq 0x5060
jmp 0x1f377
jmp 0x1f379
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x1f07b
movsd -0x18(%rbp), %xmm0
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x5540
nop