// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::GradientDescent()
NUMBER HMMProblemSlicedAB::GradientDescent() {
    NCAT x, nX = this->p->nK;
//    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;
    
    //
    // fit all as 1 skill first
    //
    if(this->p->single_skill>0) {
        FitResult fr;
        fr.pO = 0;
        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);
        // 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);
                    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
//    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;
//            }
            
            FitResult fr;
            fr.iter = 0;
            fr.pO0  = 0.0;
            fr.pO   = 0.0;
            fr.conv = 0; // converged
            fr.ndat = 0;
            // for all slices
            for(NPAR z=0; z<this->p->nZ; z++) {
                FitResult fr_loc;
                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(x), this->getA(x), this->getB(x), xndat, x_data);
//                fb->tag = z; // mark which slice we are actually fitting
                
                if(this->p->block_fitting[0]!=0 || z>0) fb->pi = NULL; // null if not first slice
                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)
                
                fr_loc = GradientDescentBit(fb);
                fr.iter+=fr_loc.iter;
                fr.conv+=fr_loc.conv;
                fr.pO0=(z==0)?fr_loc.pO0:fr.pO0;
                fr.pO =(z==(this->p->nZ-1))?fr_loc.pO:fr.pO;
                delete fb;
            }// for all slices
            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

    return loglik;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movq 0x88(%rdi), %r14
movl 0x8c0(%r14), %ebp
cmpb $0x0, 0x3d(%r14)
jle 0x230c0
movl $0xf8, %edi
callq 0x4220
movq %rax, %r15
movsbl 0x8b8(%r14), %esi
movsbl 0x8b9(%r14), %edx
movl 0x8bc(%r14), %r8d
movsbl 0x8c8(%r14), %r9d
movsd 0x28(%r14), %xmm0
movsbl 0x30(%r14), %eax
movl %eax, (%rsp)
movq %r15, %rdi
movl %ebp, %ecx
callq 0xa20c
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1ff6a
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1ffa2
movq %rax, %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1ffde
movq 0x88(%rbx), %rcx
movl 0x8dc(%rcx), %r8d
movq 0x8f0(%rcx), %r9
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0xa622
movq 0x88(%rbx), %rax
cmpb $0x0, 0x981(%rax)
je 0x22e38
andq $0x0, 0x10(%r15)
cmpb $0x0, 0x982(%rax)
je 0x22e46
andq $0x0, 0x18(%r15)
cmpb $0x0, 0x983(%rax)
je 0x22e54
andq $0x0, 0x20(%r15)
pushq $0x2
popq %rsi
movq %r15, %rdi
callq 0xa854
pushq $0x3
popq %rsi
movq %r15, %rdi
callq 0xa854
movq 0x88(%rbx), %rax
movb 0x3f(%rax), %al
cmpb $0x3, %al
jne 0x22ea3
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0xa854
pushq $0x6
popq %rsi
movq %r15, %rdi
callq 0xa854
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0xa854
movq 0x88(%rbx), %rax
movb 0x3f(%rax), %al
cmpb $0x5, %al
jne 0x22eb2
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0xa854
pushq $0x7
popq %rsi
movq %r15, %rdi
callq 0xa854
movq 0x88(%rbx), %r13
movslq 0x8dc(%r13), %r12
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0x4120
movq %rax, %r14
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
imulq $0x18, %rcx, %rcx
movq %r14, %rdx
cmpq %rax, %rcx
je 0x22f0f
movq 0x8e8(%r13), %rsi
movl 0x58(%rsi,%rax,4), %edi
movl %edi, (%rdx)
andl $0x0, 0x58(%rsi,%rax,4)
addq $0x18, %rax
addq $0x4, %rdx
jmp 0x22eee
movq %rbp, 0x18(%rsp)
leaq 0x60(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x21af8
movl (%r13), %eax
movl %eax, 0x20(%rsp)
movsd 0x8(%r13), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x18(%r13), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x20(%r13), %eax
movl %eax, 0x8(%rsp)
movl 0x24(%r13), %r13d
movq 0x88(%rbx), %r12
movl 0x8dc(%r12), %ebp
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %ecx
cmovgl %ebp, %ecx
imulq $0x18, %rcx, %rcx
movq %r14, %rdx
cmpq %rax, %rcx
je 0x22f92
movl (%rdx), %esi
movq 0x8e8(%r12), %rdi
movl %esi, 0x58(%rdi,%rax,4)
addq $0x18, %rax
addq $0x4, %rdx
jmp 0x22f75
movq %r14, %rdi
callq 0x4290
cmpb $0x0, 0x3c(%r12)
jne 0x22fd1
leaq 0xa700(%rip), %rdi      # 0x2d6a9
movl %ebp, %esi
movl %r13d, %edx
movl 0x20(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movl 0x8(%rsp), %r8d
movb $0x2, %al
callq 0x4050
movq 0x88(%rbx), %r12
cmpb $0x2, 0x3d(%r12)
jne 0x2307f
xorl %ebp, %ebp
cmpl 0xc(%rbx), %ebp
jge 0x2307f
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1ff6a
movq 0x10(%r15), %rsi
movq 0x88(%rbx), %rcx
movsbq 0x8b8(%rcx), %rdx
shlq $0x3, %rdx
movq %rax, %rdi
callq 0x41c0
movq 0x18(%r15), %r14
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1ffa2
movq 0x88(%rbx), %rcx
movsbl 0x8c8(%rcx), %edx
movsbl 0x8b8(%rcx), %ecx
movq %r14, %rdi
movq %rax, %rsi
movl %ecx, %r8d
callq 0xa1a2
movq 0x20(%r15), %r14
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1ffde
movq 0x88(%rbx), %rsi
movsbl 0x8c8(%rsi), %edx
movsbl 0x8b8(%rsi), %ecx
movsbl 0x8b9(%rsi), %r8d
movq %r14, %rdi
movq %rax, %rsi
callq 0xa1a2
incl %ebp
jmp 0x22fdf
movq %r15, %rdi
callq 0xa2b2
movl $0xf8, %esi
movq %r15, %rdi
callq 0x4230
movq 0x88(%rbx), %rax
xorpd %xmm2, %xmm2
cmpb $0x2, 0x3d(%rax)
movq 0x18(%rsp), %rbp
jne 0x230c0
movapd %xmm2, %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
testl %ebp, %ebp
cmovlel %edx, %ebp
xorpd %xmm2, %xmm2
movq %rbp, 0x18(%rsp)
cmpq %rbp, %rdx
je 0x230aa
movq 0x88(%rbx), %r14
movq 0x8e0(%r14), %rax
movq 0x8f8(%r14), %rcx
movl (%rax,%rdx,4), %eax
movl %eax, 0x14(%rsp)
movq %rdx, 0x8(%rsp)
movq (%rcx,%rdx,8), %rax
movq %rax, 0x50(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movsd %xmm2, 0x58(%rsp)
movsbl 0x8c8(%r14), %eax
cmpl %eax, %ebp
jge 0x23308
movsd %xmm1, 0x20(%rsp)
movl $0xf8, %edi
callq 0x4220
movq %rax, %r15
movsbl 0x8b8(%r14), %esi
movsbl 0x8b9(%r14), %edx
movl 0x8c0(%r14), %ecx
movl 0x8bc(%r14), %r8d
movsbl 0x8c8(%r14), %r9d
movsd 0x28(%r14), %xmm0
movsbl 0x30(%r14), %eax
movl %eax, (%rsp)
movq %r15, %rdi
callq 0xa20c
movl %r12d, 0x40(%rsp)
movl %r13d, 0x38(%rsp)
movq %rbx, %rdi
movq %r15, %r12
movl %ebp, %r15d
movq %rbx, %rbp
movq 0x8(%rsp), %rbx
movl %ebx, %esi
callq 0x1ff6a
movq %rax, %r14
movq %rbp, %rdi
movl %ebx, %esi
callq 0x1ffa2
movq %rax, %r13
movq %rbp, %rdi
movl %ebx, %esi
movq %rbp, %rbx
movl %r15d, %ebp
movq %r12, %r15
callq 0x1ffde
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rax, %rcx
movl 0x14(%rsp), %r8d
movq 0x50(%rsp), %r9
callq 0xa622
movq 0x88(%rbx), %rax
testl %ebp, %ebp
jne 0x231ee
cmpb $0x0, 0x981(%rax)
je 0x231f3
andq $0x0, 0x10(%r15)
cmpb $0x0, 0x982(%rax)
je 0x23201
andq $0x0, 0x18(%r15)
cmpb $0x0, 0x983(%rax)
movl 0x38(%rsp), %r13d
je 0x23214
andq $0x0, 0x20(%r15)
movq %r15, %rdi
pushq $0x2
popq %rsi
callq 0xa854
movq %r15, %rdi
pushq $0x3
popq %rsi
callq 0xa854
movq 0x88(%rbx), %rax
movb 0x3f(%rax), %al
cmpb $0x3, %al
movl 0x40(%rsp), %r12d
jne 0x23268
movq %r15, %rdi
pushq $0x8
popq %rsi
callq 0xa854
movq %r15, %rdi
pushq $0x6
popq %rsi
callq 0xa854
movq %r15, %rdi
pushq $0x4
popq %rsi
callq 0xa854
movq 0x88(%rbx), %rax
movb 0x3f(%rax), %al
cmpb $0x5, %al
jne 0x23277
movq %r15, %rdi
pushq $0x4
popq %rsi
callq 0xa854
movq %r15, %rdi
pushq $0x7
popq %rsi
callq 0xa854
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x21af8
addl 0x60(%rsp), %r12d
movsd 0x78(%rsp), %xmm1
addl 0x80(%rsp), %r13d
testl %ebp, %ebp
jne 0x232b5
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x20(%rsp), %xmm0
movq 0x88(%rbx), %rax
movsbl 0x8c8(%rax), %eax
decl %eax
cmpl %eax, %ebp
je 0x232d3
movapd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movq %r15, %rdi
callq 0xa2b2
movl $0xf8, %esi
movq %r15, %rdi
callq 0x4230
incl %ebp
movq 0x88(%rbx), %r14
movsd 0x20(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
jmp 0x2311b
testl %r13d, %r13d
jne 0x23313
cmpl 0x38(%r14), %r12d
jne 0x2336f
xorpd %xmm3, %xmm3
cmpltsd %xmm1, %xmm3
movsd 0x95cc(%rip), %xmm0     # 0x2c8f0
andpd %xmm0, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cmpb $0x0, 0x3c(%r14)
movq 0x18(%rsp), %rbp
jne 0x23378
leaq 0xa319(%rip), %rdi      # 0x2d65c
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %edx
xorl %ecx, %ecx
movl %r12d, %r8d
movsd 0x48(%rsp), %xmm0
movl %r13d, %r9d
movb $0x2, %al
movapd %xmm3, 0x20(%rsp)
callq 0x4050
movapd 0x20(%rsp), %xmm3
jmp 0x23378
movapd %xmm2, %xmm3
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %rdx
incq %rdx
movapd %xmm3, %xmm2
jmp 0x230d0
jmp 0x2338b
movq %rax, %rbx
movl $0xf8, %esi
movq %r15, %rdi
callq 0x4230
movq %rbx, %rdi
callq 0x4450
nop