// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rdi, 0x10(%rsp)
movq 0x88(%rdi), %rbx
cmpb $0x0, 0x3d(%rbx)
jle 0x272fc
movl $0xf8, %edi
callq 0x4210
movq %rax, %r14
movsbl 0x8b8(%rbx), %esi
movsbl 0x8b9(%rbx), %edx
movl 0x8c0(%rbx), %ecx
movl 0x8bc(%rbx), %r8d
movsbl 0x8c8(%rbx), %r9d
movsd 0x28(%rbx), %xmm0
movsbl 0x30(%rbx), %eax
movl %eax, (%rsp)
movq %r14, %rdi
callq 0xcb4c
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x257a4
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0x257c0
movq %rax, %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x257e0
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 0xd01a
movq 0x88(%rbx), %rax
cmpb $0x0, 0x981(%rax)
je 0x26fef
movq $0x0, 0x10(%r14)
cmpb $0x0, 0x982(%rax)
je 0x27000
movq $0x0, 0x18(%r14)
cmpb $0x0, 0x983(%rax)
je 0x27011
movq $0x0, 0x20(%r14)
movq %r14, %rdi
movl $0x2, %esi
callq 0xd3be
movq %r14, %rdi
movl $0x7, %esi
callq 0xd3be
movq %r14, %rdi
movl $0x3, %esi
callq 0xd3be
movq 0x88(%rbx), %rbx
cmpb $0x3, 0x3f(%rbx)
jne 0x27078
movq %r14, %rdi
movl $0x8, %esi
callq 0xd3be
movq %r14, %rdi
movl $0x6, %esi
callq 0xd3be
movq %r14, %rdi
movl $0x4, %esi
callq 0xd3be
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rbx
movslq 0x8dc(%rbx), %r12
movl $0x4, %esi
movq %r12, %rdi
callq 0x4130
movq %rax, %r15
testq %r12, %r12
jle 0x270b9
movq 0x8e8(%rbx), %rax
addq $0x58, %rax
xorl %ecx, %ecx
movl (%rax), %edx
movl %edx, (%r15,%rcx,4)
movl $0x0, (%rax)
incq %rcx
addq $0x60, %rax
cmpq %rcx, %r12
jne 0x270a1
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %r14, 0x28(%rsp)
movq %r14, %rdx
callq 0x291b6
movl (%r12), %ebp
movsd 0x8(%r12), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x18(%r12), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0x20(%r12), %r14d
movl 0x24(%r12), %r13d
movq 0x88(%rbx), %rbx
movslq 0x8dc(%rbx), %r12
testq %r12, %r12
jle 0x27130
movq 0x8e8(%rbx), %rax
addq $0x58, %rax
xorl %ecx, %ecx
movl (%r15,%rcx,4), %edx
movl %edx, (%rax)
incq %rcx
addq $0x60, %rax
cmpq %rcx, %r12
jne 0x2711e
movq %r15, %rdi
callq 0x42a0
cmpb $0x0, 0x3c(%rbx)
jne 0x2716f
leaq 0xd5b1(%rip), %rdi      # 0x346f6
movl %r12d, %esi
movl %r13d, %edx
movl %ebp, %ecx
movsd 0x30(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movl %r14d, %r8d
movb $0x2, %al
callq 0x4050
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rbx
cmpb $0x2, 0x3d(%rbx)
movq 0x10(%rsp), %rbx
jne 0x272db
cmpl $0x0, 0xc(%rbx)
jle 0x272db
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x257a4
movq 0x28(%rsp), %r15
movq 0x10(%r15), %rsi
movq 0x88(%rbx), %rcx
movsbq 0x8b8(%rcx), %rdx
shlq $0x3, %rdx
movq %rax, %rdi
callq 0x41c0
movq %rbx, %r14
movq 0x18(%r15), %rbx
movq %r14, %rdi
movq %r12, 0x18(%rsp)
movl %r12d, %esi
callq 0x257c0
movq %rax, %r12
movq 0x88(%r14), %rax
movsbq 0x8c8(%rax), %rcx
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
jle 0x2723b
movsbq 0x8b8(%rax), %rax
leaq (,%rax,8), %r13
movq %rax, 0x20(%rsp)
movl %eax, %r15d
xorl %ebp, %ebp
cmpb $0x0, 0x20(%rsp)
jle 0x27231
xorl %r14d, %r14d
movq (%r12,%rbp,8), %rax
movq (%rax,%r14,8), %rdi
movq (%rbx,%rbp,8), %rax
movq (%rax,%r14,8), %rsi
movq %r13, %rdx
callq 0x41c0
incq %r14
cmpq %r14, %r15
jne 0x27211
incq %rbp
cmpq 0x30(%rsp), %rbp
jne 0x27207
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rbx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x257e0
movq %rax, %r12
movq 0x88(%r14), %rax
movsbq 0x8c8(%rax), %rcx
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
jle 0x272c4
movsbq 0x8b9(%rax), %r13
movsbq 0x8b8(%rax), %rax
shlq $0x3, %r13
movq %rax, 0x20(%rsp)
movl %eax, %r15d
xorl %ebp, %ebp
cmpb $0x0, 0x20(%rsp)
jle 0x272ba
xorl %r14d, %r14d
movq (%r12,%rbp,8), %rax
movq (%rax,%r14,8), %rdi
movq (%rbx,%rbp,8), %rax
movq (%rax,%r14,8), %rsi
movq %r13, %rdx
callq 0x41c0
incq %r14
cmpq %r14, %r15
jne 0x2729a
incq %rbp
cmpq 0x30(%rsp), %rbp
jne 0x27290
movq 0x18(%rsp), %r12
incl %r12d
movq 0x10(%rsp), %rbx
cmpl 0xc(%rbx), %r12d
jl 0x2718b
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0xcbf8
movl $0xf8, %esi
movq %r14, %rdi
callq 0x4220
movq 0x88(%rbx), %rbx
cmpl $0x0, 0x8c0(%rbx)
jle 0x27589
xorpd %xmm2, %xmm2
movq $0x0, 0x18(%rsp)
cmpb $0x0, 0x8c8(%rbx)
jle 0x274ea
movsd %xmm2, 0x40(%rsp)
xorl %r13d, %r13d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movl $0xf8, %edi
callq 0x4210
movq %rax, %r12
movsbl 0x8b8(%rbx), %esi
movsbl 0x8b9(%rbx), %edx
movl 0x8c0(%rbx), %ecx
movl 0x8bc(%rbx), %r8d
movsbl 0x8c8(%rbx), %r9d
movsd 0x28(%rbx), %xmm0
movsbl 0x30(%rbx), %eax
movl %eax, (%rsp)
movq %r12, %rdi
callq 0xcb4c
movl %r13d, 0x20(%rsp)
movl %ebp, 0x30(%rsp)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %r13
movl %r13d, %esi
callq 0x257a4
movq %rax, %r15
movq %rbx, %rdi
movl %r13d, %esi
callq 0x257c0
movq %rax, %rbp
movq %rbx, %rdi
movl %r13d, %esi
callq 0x257e0
movq 0x88(%rbx), %rcx
movq 0x8e0(%rcx), %rdx
movq 0x8f8(%rcx), %rcx
movl (%rdx,%r13,4), %r8d
movq (%rcx,%r13,8), %r9
movq %r12, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rax, %rcx
callq 0xd01a
movq 0x88(%rbx), %rax
cmpl $-0x1, %r14d
jne 0x273f6
cmpb $0x0, 0x981(%rax)
je 0x273ff
movq $0x0, 0x10(%r12)
cmpb $0x0, 0x982(%rax)
movl 0x30(%rsp), %ebp
movl 0x20(%rsp), %r13d
je 0x2741a
movq $0x0, 0x18(%r12)
cmpb $0x0, 0x983(%rax)
je 0x2742c
movq $0x0, 0x20(%r12)
movq %r12, %rdi
movl $0x2, %esi
callq 0xd3be
movq %r12, %rdi
movl $0x7, %esi
callq 0xd3be
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x291b6
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
addl 0x50(%rsp), %ebp
addl 0x70(%rsp), %r13d
movq 0x88(%rbx), %rax
movsbl 0x8c8(%rax), %r15d
movq %r12, %rdi
callq 0xcbf8
movl $0xf8, %esi
movq %r12, %rdi
callq 0x4220
movq 0x88(%rbx), %rbx
movsbl 0x8c8(%rbx), %ecx
leal 0x1(%r14), %eax
addl $0x2, %r14d
cmpl %ecx, %r14d
movl %eax, %r14d
jl 0x27334
movsd 0x48(%rsp), %xmm1
testl %eax, %eax
movsd 0x28(%rsp), %xmm3
je 0x274d1
xorpd %xmm3, %xmm3
decl %r15d
cmpl %r15d, %eax
movsd 0x40(%rsp), %xmm2
je 0x274e3
xorpd %xmm1, %xmm1
testl %r13d, %r13d
jne 0x274fc
jmp 0x274f4
xorl %ebp, %ebp
xorpd %xmm1, %xmm1
xorpd %xmm3, %xmm3
xorl %r13d, %r13d
cmpl 0x38(%rbx), %ebp
jne 0x27562
xorpd %xmm4, %xmm4
cmpltsd %xmm1, %xmm4
movsd 0xc3e3(%rip), %xmm0     # 0x338f0
andpd %xmm0, %xmm4
mulsd %xmm1, %xmm4
addsd %xmm2, %xmm4
cmpb $0x0, 0x3c(%rbx)
jne 0x27566
movq 0x8e0(%rbx), %rax
movq 0x18(%rsp), %rsi
movl (%rax,%rsi,4), %edx
leaq 0xd20e(%rip), %rdi      # 0x34743
xorl %ecx, %ecx
movl %ebp, %r8d
movapd %xmm3, %xmm0
movl %r13d, %r9d
movb $0x2, %al
movapd %xmm4, 0x30(%rsp)
callq 0x4050
movapd 0x30(%rsp), %xmm4
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rbx
jmp 0x27566
movapd %xmm2, %xmm4
movq 0x18(%rsp), %rdx
incq %rdx
movslq 0x8c0(%rbx), %rax
movapd %xmm4, %xmm2
movq %rdx, 0x18(%rsp)
cmpq %rax, %rdx
jl 0x27316
jmp 0x2758d
xorpd %xmm4, %xmm4
movapd %xmm4, %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xf8, %esi
movq %r14, %rdi
jmp 0x275b8
movq %rax, %rbx
movl $0xf8, %esi
movq %r12, %rdi
callq 0x4220
movq %rbx, %rdi
callq 0x4430
nop