// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq 0x88(%rdi), %rbx
cmpb $0x1, 0x3e(%rbx)
jne 0x17c78
movq %rdi, %r14
movl 0x8c0(%rbx), %eax
movq %rax, 0x38(%rsp)
cmpb $0x0, 0x3d(%rbx)
jle 0x17a03
movl $0x110, %edi            # imm = 0x110
callq 0x4210
movq %rax, %r13
movsbl 0x8b8(%rbx), %esi
movsbl 0x8b9(%rbx), %edx
movl 0x8c0(%rbx), %ecx
movl 0x8bc(%rbx), %r8d
movsd 0x28(%rbx), %xmm0
movsbl 0x30(%rbx), %r9d
movq %rax, %rdi
callq 0xa850
movq %r14, %rdi
xorl %esi, %esi
callq 0x114c2
movq %rax, %rbx
movq %r14, %rdi
xorl %esi, %esi
callq 0x114de
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
callq 0x114fa
movq 0x88(%r14), %rcx
movl 0x8dc(%rcx), %r8d
movq 0x8f0(%rcx), %r9
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0xac68
movq 0x88(%r14), %rax
cmpb $0x0, 0x981(%rax)
je 0x17769
movq $0x0, 0x10(%r13)
cmpb $0x0, 0x982(%rax)
je 0x1777a
movq $0x0, 0x18(%r13)
cmpb $0x0, 0x983(%rax)
je 0x1778b
movq $0x0, 0x20(%r13)
movq %r13, %rdi
movl $0x2, %esi
callq 0xaf08
movq %r13, %rdi
movl $0x3, %esi
callq 0xaf08
movq 0x88(%r14), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x177d9
movq %r13, %rdi
movl $0x8, %esi
callq 0xaf08
movq %r13, %rdi
movl $0x6, %esi
callq 0xaf08
movq %r13, %rdi
movl $0x4, %esi
callq 0xaf08
movq 0x88(%r14), %rax
cmpb $0x5, 0x3f(%rax)
jne 0x177f3
movq %r13, %rdi
movl $0x4, %esi
callq 0xaf08
movq %r13, %rdi
movl $0x7, %esi
callq 0xaf08
movq 0x88(%r14), %rbx
movslq 0x8dc(%rbx), %r12
movl $0x4, %esi
movq %r12, %rdi
callq 0x4130
movq %rax, %r15
testq %r12, %r12
jle 0x17848
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 0x17830
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movq %r13, 0x10(%rsp)
movq %r13, %rdx
callq 0x14dc4
movq %r14, 0x30(%rsp)
movq 0x88(%r14), %r14
movslq 0x8dc(%r14), %r12
testq %r12, %r12
jle 0x17894
movq 0x8e8(%r14), %rax
addq $0x58, %rax
xorl %ecx, %ecx
movl (%r15,%rcx,4), %edx
movl %edx, (%rax)
incq %rcx
addq $0x60, %rax
cmpq %rcx, %r12
jne 0x17882
movl 0x40(%rsp), %ebx
movsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x60(%rsp), %ebp
movl 0x64(%rsp), %r13d
movq %r15, %rdi
callq 0x42a0
cmpb $0x0, 0x3c(%r14)
jne 0x178eb
leaq 0x1dd4b(%rip), %rdi     # 0x35619
movl %r12d, %esi
movl %r13d, %edx
movl %ebx, %ecx
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movl %ebp, %r8d
movb $0x2, %al
callq 0x4050
movq 0x30(%rsp), %r14
movq 0x88(%r14), %rax
cmpb $0x2, 0x3d(%rax)
jne 0x179e9
cmpl $0x0, 0xc(%r14)
jle 0x179e9
xorl %ebx, %ebx
movq %r14, %rdi
movl %ebx, %esi
callq 0x114c2
movq %rax, %r12
movq %r14, %rdi
movl %ebx, %esi
callq 0x114de
movq %rax, %rbp
movq %r14, %rdi
movq %rbx, (%rsp)
movl %ebx, %esi
callq 0x114fa
movq %rax, 0x8(%rsp)
movq 0x88(%r14), %rax
movsbq 0x8b8(%rax), %r15
movsbq 0x8b9(%rax), %r13
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
leaq (,%r15,8), %rbx
movq %r12, %rdi
movq %rbx, %rdx
callq 0x41c0
movl %r15d, %r14d
movq %r15, 0x28(%rsp)
testq %r15, %r15
jle 0x179a0
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r15
xorl %r12d, %r12d
movq (%rbp,%r12,8), %rdi
movq (%r15,%r12,8), %rsi
movq %rbx, %rdx
callq 0x41c0
incq %r12
cmpq %r12, %r14
jne 0x17987
cmpb $0x0, 0x28(%rsp)
movq 0x8(%rsp), %r12
jle 0x179d4
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rbx
shlq $0x3, %r13
xorl %r15d, %r15d
movq (%r12,%r15,8), %rdi
movq (%rbx,%r15,8), %rsi
movq %r13, %rdx
callq 0x41c0
incq %r15
cmpq %r15, %r14
jne 0x179bc
movq (%rsp), %rbx
incl %ebx
movq 0x30(%rsp), %r14
cmpl 0xc(%r14), %ebx
jl 0x1790e
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0xa8f2
movl $0x110, %esi            # imm = 0x110
movq %rbx, %rdi
callq 0x4220
movq 0x88(%r14), %rax
cmpb $0x2, 0x3d(%rax)
xorpd %xmm2, %xmm2
je 0x17c65
cmpl $0x0, 0x38(%rsp)
jle 0x17c65
xorpd %xmm0, %xmm0
xorl %r15d, %r15d
movq 0x88(%r14), %rbx
cmpb $0x1, 0x3e(%rbx)
movsd %xmm0, (%rsp)
jne 0x17a58
movq 0x8e0(%rbx), %rax
movq 0x8f8(%rbx), %rcx
movl (%rax,%r15,4), %eax
movl %eax, 0x10(%rsp)
movq (%rcx,%r15,8), %r13
jmp 0x17a63
xorl %r13d, %r13d
movl $0x0, 0x10(%rsp)
movl $0x110, %edi            # imm = 0x110
callq 0x4210
movq %rax, %rbp
movsbl 0x8b8(%rbx), %esi
movsbl 0x8b9(%rbx), %edx
movl 0x8c0(%rbx), %ecx
movl 0x8bc(%rbx), %r8d
movsd 0x28(%rbx), %xmm0
movsbl 0x30(%rbx), %r9d
movq %rax, %rdi
callq 0xa850
movq %r14, %rdi
movl %r15d, %esi
callq 0x114c2
movq %rax, %rbx
movq %r14, %rdi
movl %r15d, %esi
callq 0x114de
movq %rax, %r12
movq %r14, %rdi
movl %r15d, %esi
callq 0x114fa
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %rax, %rcx
movl 0x10(%rsp), %r8d
movq %r13, %r9
callq 0xac68
movq 0x88(%r14), %rax
cmpb $0x0, 0x981(%rax)
je 0x17af5
movq $0x0, 0x10(%rbp)
cmpb $0x0, 0x982(%rax)
je 0x17b06
movq $0x0, 0x18(%rbp)
cmpb $0x0, 0x983(%rax)
je 0x17b17
movq $0x0, 0x20(%rbp)
movq %rbp, %rdi
movl $0x2, %esi
callq 0xaf08
movq %rbp, %rdi
movl $0x3, %esi
callq 0xaf08
movq 0x88(%r14), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x17b65
movq %rbp, %rdi
movl $0x8, %esi
callq 0xaf08
movq %rbp, %rdi
movl $0x6, %esi
callq 0xaf08
movq %rbp, %rdi
movl $0x4, %esi
callq 0xaf08
movq 0x88(%r14), %rax
cmpb $0x5, 0x3f(%rax)
jne 0x17b7f
movq %rbp, %rdi
movl $0x4, %esi
callq 0xaf08
movq %rbp, %rdi
movl $0x7, %esi
callq 0xaf08
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x14dc4
movl 0x40(%rsp), %r13d
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x60(%rsp), %r12d
movq %rbp, %rbx
movl 0x64(%rsp), %ebp
movq %rbx, %rdi
callq 0xa8f2
movl $0x110, %esi            # imm = 0x110
movq %rbx, %rdi
callq 0x4220
testl %r12d, %r12d
jne 0x17bec
movq 0x88(%r14), %rax
cmpl 0x38(%rax), %r13d
jne 0x17c4e
xorpd %xmm2, %xmm2
movsd 0x8(%rsp), %xmm1
cmpltsd %xmm1, %xmm2
movsd 0x1cced(%rip), %xmm0    # 0x348f0
andpd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd (%rsp), %xmm2
movq 0x88(%r14), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x17c53
leaq 0x1d9a8(%rip), %rdi     # 0x355cc
movl %r15d, %esi
movl 0x10(%rsp), %edx
movl %ebp, %ecx
movl %r13d, %r8d
movsd 0x28(%rsp), %xmm0
movapd %xmm2, 0x10(%rsp)
movl %r12d, %r9d
movb $0x2, %al
callq 0x4050
movapd 0x10(%rsp), %xmm2
jmp 0x17c53
movsd (%rsp), %xmm2
incq %r15
movapd %xmm2, %xmm0
cmpq %r15, 0x38(%rsp)
jne 0x17a2a
movapd %xmm2, %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x42b0
movq %r13, %rdi
jmp 0x17c8a
movq %rbp, %rdi
movq %rax, %rbx
movl $0x110, %esi            # imm = 0x110
callq 0x4220
movq %rbx, %rdi
callq 0x4430
nop