// File: myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::GradientDescent()
NUMBER HMMProblem::GradientDescent() {
	NCAT x, nX;
    if(this->p->structure==STRUCTURE_SKILL)
        nX = this->p->nK;
//    else if (this->p->structure==STRUCTURE_GROUP)
//        nX = this->p->nG;
    else
        exit(1);
    NUMBER loglik = 0.0;

    if(true){
	//
	// fit all as 1 skill first
	//
	if(this->p->single_skill>0) {
        FitResult fr;
        fr.pO = 0;
        FitBit *fb = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->tol, this->p->tol_mode);
        // link accordingly
        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_GRAD);
        if(this->p->solver==METHOD_CGD) {
            fb->init(FBS_DIR);
            fb->init(FBS_DIRm1);
            fb->init(FBS_GRADm1);
        }
        if(this->p->solver==METHOD_GBB) {
            fb->init(FBS_GRADm1);
        }
        fb->init(FBS_PARm2); // do this for all in order to capture oscillation, e.g. if new param at t is close to param at t-2 (tolerance)

        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 = GradientDescentBit(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 %5d, 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
//    printf("thread %i|%i\n",omp_get_thread_num(),omp_get_num_threads());//undoPAR
    if(this->p->single_skill!=2){
//        #pragma omp for schedule(dynamic) reduction(+:loglik) //PAR
        for(x=0; x<nX; x++) { // if not "force single skill" too
            NCAT xndat;
            struct data** x_data;
            if(this->p->structure==STRUCTURE_SKILL) {
                xndat = this->p->k_numg[x];
                x_data = this->p->k_g_data[x];
//            } else if(this->p->structure==STRUCTURE_GROUP) {
//                xndat = this->p->g_numk[x];
//                x_data = this->p->g_k_data[x];
            } else {
                xndat = 0;
                x_data = NULL;
            }
            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(x), this->getA(x), this->getB(x), xndat, x_data);
            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;
            
            FitResult fr;
            fb->init(FBS_PARm1);
            fb->init(FBS_GRAD);
            if(this->p->solver==METHOD_CGD) {
                fb->init(FBS_DIR);
                fb->init(FBS_DIRm1);
                fb->init(FBS_GRADm1);
            }
            if(this->p->solver==METHOD_GBB) {
                fb->init(FBS_GRADm1);
            }
            fb->init(FBS_PARm2); // do this for all in order to capture oscillation, e.g. if new param at t is close to param at t-2 (tolerance)
            
            fr = GradientDescentBit(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 %5d, seq %5d, dat %8d, iter#%3d p(O|param)= %15.7f >> %15.7f, conv=%d\n", x, xndat, fr.ndat, fr.iter,fr.pO0,fr.pO,fr.conv);
            }
        } // for all skills
    }// if not force single skill
//    }//#omp //PAR
        
    }
    
    if(false){
    FitBit **fbs = Calloc(FitBit*, (size_t)nX);
    for(x=0;x<nX;x++) {
        NCAT xndat;
        struct data** x_data;
        if(this->p->structure==STRUCTURE_SKILL) {
            xndat = this->p->k_numg[x];
            x_data = this->p->k_g_data[x];
//        } else if(this->p->structure==STRUCTURE_GROUP) {
//            xndat = this->p->g_numk[x];
//            x_data = this->p->g_k_data[x];
        } else {
            xndat = 0;
            x_data = NULL;
        }
        fbs[x] = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->tol, this->p->tol_mode);
        fbs[x]->link( this->getPI(x), this->getA(x), this->getB(x), xndat, x_data);

        fbs[x]->init(FBS_PARm1);
        fbs[x]->init(FBS_GRAD);
        if(this->p->solver==METHOD_CGD) {
            fbs[x]->init(FBS_DIR);
            fbs[x]->init(FBS_DIRm1);
            fbs[x]->init(FBS_GRADm1);
        }
    }
    /*FitResult fr=*/GradientDescentBitBig(fbs, nX);
    
    for(x=0;x<nX;x++) delete fbs[x];
    if(fbs!=NULL) free(fbs);
    }
    
    return loglik;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq 0x88(%rax), %rax
movsbl 0x3e(%rax), %eax
cmpl $0x1, %eax
jne 0x24443
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movl 0x8c0(%rax), %eax
movl %eax, -0x10(%rbp)
jmp 0x2444d
movl $0x1, %edi
callq 0x5390
movq -0x118(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x0, %eax
jle 0x24923
movq $0x0, -0x28(%rbp)
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x118(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x120(%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 0x244c9
movq -0x118(%rbp), %rdi
movq -0x120(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x138(%rbp)
xorl %esi, %esi
callq 0x1a560
movq -0x118(%rbp), %rdi
movq %rax, -0x130(%rbp)
xorl %esi, %esi
callq 0x1a5d0
movq -0x118(%rbp), %rdi
movq %rax, -0x128(%rbp)
xorl %esi, %esi
callq 0x1a640
movq -0x138(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq -0x128(%rbp), %rdx
movq %rax, %rcx
movq -0x118(%rbp), %rax
movq 0x88(%rax), %r8
movl 0x8dc(%r8), %r8d
movq 0x88(%rax), %rax
movq 0x8f0(%rax), %r9
callq 0xd990
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x245a1
movq -0x48(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x245a1
movq -0x120(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x24d05
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x245c7
movq -0x48(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x245ed
movq -0x48(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x48(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0x48(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x2464a
movq -0x48(%rbp), %rdi
movl $0x8, %esi
callq 0xdcb0
movq -0x48(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0x48(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x2466f
movq -0x48(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0x48(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movslq 0x8dc(%rax), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x60(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x118(%rbp), %rcx
movl -0xc(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x24719
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x8e8(%rcx), %rcx
movslq -0xc(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rcx
movl 0x58(%rcx), %esi
movq -0x60(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x88(%rax), %rax
movq 0x8e8(%rax), %rax
movslq -0xc(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x58(%rax)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x246a7
movq -0x118(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0x1f530
leaq -0x40(%rbp), %rdi
leaq -0x88(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movl $0x0, -0xc(%rbp)
movq -0x118(%rbp), %rcx
movl -0xc(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x2479e
movq -0x118(%rbp), %rax
movq -0x60(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movq 0x88(%rax), %rax
movq 0x8e8(%rax), %rax
movslq -0xc(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x58(%rax)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x2474c
movq -0x60(%rbp), %rdi
callq 0x5380
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x247f1
movq -0x118(%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 0x2bf03(%rip), %rdi     # 0x506ed
movb $0x2, %al
callq 0x5060
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x2, %eax
jne 0x248f3
movl $0x0, -0x8c(%rbp)
movq -0x118(%rbp), %rcx
movl -0x8c(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x248f1
movq -0x118(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x1a560
movq -0x118(%rbp), %rdi
movq %rax, -0x98(%rbp)
movl -0x8c(%rbp), %esi
callq 0x1a5d0
movq -0x118(%rbp), %rdi
movq %rax, -0xa0(%rbp)
movl -0x8c(%rbp), %esi
callq 0x1a640
movq -0x118(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movq -0x48(%rbp), %rsi
addq $0x10, %rsi
movq -0x48(%rbp), %rdx
addq $0x18, %rdx
movq -0x48(%rbp), %rcx
addq $0x20, %rcx
movq 0x88(%rdi), %rax
movb 0x8b8(%rax), %r10b
movq 0x88(%rdi), %rax
leaq -0x98(%rbp), %r8
leaq -0xa0(%rbp), %r9
leaq -0xa8(%rbp), %r11
movq %r11, (%rsp)
movsbl %r10b, %r10d
movl %r10d, 0x8(%rsp)
movsbl 0x8b9(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0x1f3a0
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x24816
jmp 0x248f3
movq -0x48(%rbp), %rax
movq %rax, -0x140(%rbp)
cmpq $0x0, %rax
je 0x24921
movq -0x140(%rbp), %rdi
callq 0xd3a0
movq -0x140(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x24923
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x2, %eax
je 0x24cf7
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x24cf5
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3e(%rax), %eax
cmpl $0x1, %eax
jne 0x249a9
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x8e0(%rcx), %rcx
movslq -0xc(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0xac(%rbp)
movq 0x88(%rax), %rax
movq 0x8f8(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb8(%rbp)
jmp 0x249be
movl $0x0, -0xac(%rbp)
movq $0x0, -0xb8(%rbp)
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x118(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x148(%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 0x24a0f
movq -0x118(%rbp), %rdi
movq -0x148(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x160(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a560
movq -0x118(%rbp), %rdi
movq %rax, -0x158(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a5d0
movq -0x118(%rbp), %rdi
movq %rax, -0x150(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a640
movq -0x160(%rbp), %rdi
movq -0x158(%rbp), %rsi
movq -0x150(%rbp), %rdx
movq %rax, %rcx
movl -0xac(%rbp), %r8d
movq -0xb8(%rbp), %r9
callq 0xd990
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x24ade
movq -0xc0(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x24ade
movq -0x148(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x24d05
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x24b07
movq -0xc0(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x24b30
movq -0xc0(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0xc0(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0xc0(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x24b9c
movq -0xc0(%rbp), %rdi
movl $0x8, %esi
callq 0xdcb0
movq -0xc0(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0xc0(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0x118(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x24bc4
movq -0xc0(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0xc0(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0x118(%rbp), %rsi
movq -0xc0(%rbp), %rdx
leaq -0x110(%rbp), %rdi
callq 0x1f530
leaq -0xe8(%rbp), %rdi
leaq -0x110(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movq -0xc0(%rbp), %rax
movq %rax, -0x168(%rbp)
cmpq $0x0, %rax
je 0x24c38
movq -0x168(%rbp), %rdi
callq 0xd3a0
movq -0x168(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
cmpl $0x0, -0xc8(%rbp)
jne 0x24c5e
movq -0x118(%rbp), %rcx
movl -0xe8(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x24ce5
movq -0x118(%rbp), %rax
movsd -0xd0(%rbp), %xmm0
movsd -0xd0(%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 0x24ce3
movl -0xc(%rbp), %esi
movl -0xac(%rbp), %edx
movl -0xc4(%rbp), %ecx
movl -0xe8(%rbp), %r8d
movsd -0xe0(%rbp), %xmm0
movsd -0xd0(%rbp), %xmm1
movl -0xc8(%rbp), %r9d
leaq 0x2b9c4(%rip), %rdi     # 0x506a0
movb $0x2, %al
callq 0x5060
jmp 0x24ce5
jmp 0x24ce7
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x24945
jmp 0x24cf7
movsd -0x18(%rbp), %xmm0
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x5540
nop