// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movq 0x88(%rdi), %r14
cmpb $0x0, 0x3d(%r14)
jle 0x1112c
movl $0x110, %edi            # imm = 0x110
callq 0x4220
movq %rax, %r13
movsbl 0x8b8(%r14), %esi
movsbl 0x8b9(%r14), %edx
movl 0x8c0(%r14), %ecx
movl 0x8bc(%r14), %r8d
movsd 0x28(%r14), %xmm0
movsbl 0x30(%r14), %r9d
movq %rax, %rdi
callq 0x81d8
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe1f8
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe230
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe268
movq 0x88(%rbx), %rcx
movl 0x8dc(%rcx), %r8d
movq 0x8f0(%rcx), %r9
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x8586
movq 0x88(%rbx), %rax
cmpb $0x0, 0x981(%rax)
je 0x10f00
andq $0x0, 0x10(%r13)
cmpb $0x0, 0x982(%rax)
je 0x10f0e
andq $0x0, 0x18(%r13)
cmpb $0x0, 0x983(%rax)
je 0x10f1c
andq $0x0, 0x20(%r13)
pushq $0x2
popq %rsi
movq %r13, %rdi
callq 0x878c
pushq $0x7
popq %rsi
movq %r13, %rdi
callq 0x878c
pushq $0x3
popq %rsi
movq %r13, %rdi
callq 0x878c
movq 0x88(%rbx), %r12
cmpb $0x3, 0x3f(%r12)
jne 0x10f74
pushq $0x8
popq %rsi
movq %r13, %rdi
callq 0x878c
pushq $0x6
popq %rsi
movq %r13, %rdi
callq 0x878c
pushq $0x4
popq %rsi
movq %r13, %rdi
callq 0x878c
movq 0x88(%rbx), %r12
movslq 0x8dc(%r12), %r14
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x4120
movq %rax, %r15
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
imulq $0x18, %rcx, %rcx
movq %r15, %rdx
cmpq %rax, %rcx
je 0x10fc1
movq 0x8e8(%r12), %rsi
movl 0x58(%rsi,%rax,4), %edi
movl %edi, (%rdx)
andl $0x0, 0x58(%rsi,%rax,4)
addq $0x18, %rax
addq $0x4, %rdx
jmp 0x10f9f
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, (%rsp)
movq %r13, %rdx
callq 0x13ec4
movl (%r12), %ebp
movsd 0x8(%r12), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x18(%r12), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x20(%r12), %eax
movl %eax, 0x2c(%rsp)
movl 0x24(%r12), %r12d
movq 0x88(%rbx), %r14
movl 0x8dc(%r14), %r13d
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
imulq $0x18, %rcx, %rcx
movq %r15, %rdx
cmpq %rax, %rcx
je 0x11043
movl (%rdx), %esi
movq 0x8e8(%r14), %rdi
movl %esi, 0x58(%rdi,%rax,4)
addq $0x18, %rax
addq $0x4, %rdx
jmp 0x11027
movq %r15, %rdi
callq 0x4290
cmpb $0x0, 0x3c(%r14)
jne 0x11080
leaq 0x1c69d(%rip), %rdi     # 0x2d6f6
movl %r13d, %esi
movl %r12d, %edx
movl %ebp, %ecx
movsd 0x20(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movl 0x2c(%rsp), %r8d
movb $0x2, %al
callq 0x4050
movq 0x88(%rbx), %r14
cmpb $0x2, 0x3d(%r14)
jne 0x11113
movq (%rsp), %r13
leaq 0x10(%r13), %r15
leaq 0x18(%r13), %r12
addq $0x20, %r13
xorl %ebp, %ebp
leaq 0x38(%rsp), %r14
cmpl 0xc(%rbx), %ebp
jge 0x11113
movq %rbx, %rdi
movl %ebp, %esi
callq 0xe1f8
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
callq 0xe230
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
callq 0xe268
movq %rax, 0x30(%rsp)
movq 0x88(%rbx), %rax
movsbl 0x8b8(%rax), %edi
movsbl 0x8b9(%rax), %eax
subq $0x8, %rsp
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
leaq 0x48(%rsp), %r8
movq %r14, %r9
pushq %rax
pushq %rdi
leaq 0x48(%rsp), %rax
pushq %rax
callq 0x11320
addq $0x20, %rsp
incl %ebp
jmp 0x110a2
movq (%rsp), %r14
movq %r14, %rdi
callq 0x8272
movl $0x110, %esi            # imm = 0x110
movq %r14, %rdi
callq 0x4230
xorpd %xmm0, %xmm0
xorl %r14d, %r14d
movq 0x88(%rbx), %r15
movslq 0x8c0(%r15), %rax
cmpq %rax, %r14
jge 0x112f7
movsd %xmm0, (%rsp)
movl $0x110, %edi            # imm = 0x110
callq 0x4220
movq %rax, %r13
movsbl 0x8b8(%r15), %esi
movsbl 0x8b9(%r15), %edx
movl 0x8c0(%r15), %ecx
movl 0x8bc(%r15), %r8d
movsd 0x28(%r15), %xmm0
movsbl 0x30(%r15), %r9d
movq %rax, %rdi
callq 0x81d8
movq %rbx, %rdi
movl %r14d, %esi
callq 0xe1f8
movq %rax, %r15
movq %rbx, %rdi
movl %r14d, %esi
callq 0xe230
movq %rax, %r12
movq %rbx, %rdi
movl %r14d, %esi
callq 0xe268
movq 0x88(%rbx), %rcx
movq 0x8e0(%rcx), %rdx
movq 0x8f8(%rcx), %rcx
movl (%rdx,%r14,4), %r8d
movq (%rcx,%r14,8), %r9
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x8586
movq 0x88(%rbx), %rax
cmpb $0x0, 0x981(%rax)
je 0x111f7
andq $0x0, 0x10(%r13)
cmpb $0x0, 0x982(%rax)
je 0x11205
andq $0x0, 0x18(%r13)
cmpb $0x0, 0x983(%rax)
je 0x11213
andq $0x0, 0x20(%r13)
movq %r13, %rdi
pushq $0x2
popq %rsi
callq 0x878c
movq %r13, %rdi
pushq $0x7
popq %rsi
callq 0x878c
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x13ec4
movl 0x40(%rsp), %ebp
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0x60(%rsp), %r15d
movl 0x64(%rsp), %r12d
movq %r13, %rdi
callq 0x8272
movl $0x110, %esi            # imm = 0x110
movq %r13, %rdi
callq 0x4230
movq 0x88(%rbx), %rax
testl %r15d, %r15d
jne 0x11285
cmpl 0x38(%rax), %ebp
jne 0x112e6
xorpd %xmm2, %xmm2
movsd 0x20(%rsp), %xmm1
cmpltsd %xmm1, %xmm2
movsd 0x1b654(%rip), %xmm0    # 0x2c8f0
andpd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd (%rsp), %xmm2
cmpb $0x0, 0x3c(%rax)
jne 0x112eb
movq 0x8e0(%rax), %rax
movl (%rax,%r14,4), %edx
leaq 0x1c482(%rip), %rdi     # 0x2d743
movl %r14d, %esi
movl %r12d, %ecx
movl %ebp, %r8d
movsd 0x18(%rsp), %xmm0
movl %r15d, %r9d
movb $0x2, %al
movapd %xmm2, (%rsp)
callq 0x4050
movapd (%rsp), %xmm2
jmp 0x112eb
movsd (%rsp), %xmm2
incq %r14
movapd %xmm2, %xmm0
jmp 0x11133
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x11308
movq %rax, %rbx
movl $0x110, %esi            # imm = 0x110
movq %r13, %rdi
callq 0x4230
movq %rbx, %rdi
callq 0x4450