// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdi, %r14
movq 0x88(%rdi), %rbx
movl 0x8c0(%rbx), %eax
movq %rax, 0x58(%rsp)
cmpb $0x0, 0x3d(%rbx)
jle 0x29393
movl $0xf8, %edi
callq 0x4210
movq %rax, %rbp
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 %rbp, %rdi
callq 0xcb4c
movq %r14, %rdi
xorl %esi, %esi
callq 0x25c26
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
callq 0x25c42
movq %rax, %r12
movq %r14, %rdi
xorl %esi, %esi
callq 0x25c62
movq 0x88(%r14), %rcx
movl 0x8dc(%rcx), %r8d
movq 0x8f0(%rcx), %r9
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0xd010
movq 0x88(%r14), %rax
cmpb $0x0, 0x981(%rax)
je 0x2908e
movq $0x0, 0x10(%rbp)
cmpb $0x0, 0x982(%rax)
je 0x2909f
movq $0x0, 0x18(%rbp)
cmpb $0x0, 0x983(%rax)
je 0x290b0
movq $0x0, 0x20(%rbp)
movq %rbp, %rdi
movl $0x2, %esi
callq 0xd392
movq %rbp, %rdi
movl $0x3, %esi
callq 0xd392
movq 0x88(%r14), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x290fe
movq %rbp, %rdi
movl $0x8, %esi
callq 0xd392
movq %rbp, %rdi
movl $0x6, %esi
callq 0xd392
movq %rbp, %rdi
movl $0x4, %esi
callq 0xd392
movq 0x88(%r14), %rax
cmpb $0x5, 0x3f(%rax)
jne 0x29118
movq %rbp, %rdi
movl $0x4, %esi
callq 0xd392
movq %rbp, %rdi
movl $0x7, %esi
callq 0xd392
movq 0x88(%r14), %rbx
movslq 0x8dc(%rbx), %r12
movl $0x4, %esi
movq %r12, %rdi
callq 0x4130
movq %rax, %r15
testq %r12, %r12
jle 0x2916d
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 0x29155
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
movq %rbp, 0x10(%rsp)
movq %rbp, %rdx
callq 0x27d0e
movq 0x88(%r14), %r13
movslq 0x8dc(%r13), %r12
testq %r12, %r12
jle 0x291b4
movq 0x8e8(%r13), %rax
addq $0x58, %rax
xorl %ecx, %ecx
movl (%r15,%rcx,4), %edx
movl %edx, (%rax)
incq %rcx
addq $0x60, %rax
cmpq %rcx, %r12
jne 0x291a2
movl 0x68(%rsp), %ebx
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x8c(%rsp), %ebp
movq %r15, %rdi
callq 0x42a0
cmpb $0x0, 0x3c(%r13)
jne 0x2921a
leaq 0xc41e(%rip), %rdi      # 0x35619
movl %r12d, %esi
movl %ebp, %edx
movl %ebx, %ecx
movsd 0x20(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movl 0x38(%rsp), %r8d
movb $0x2, %al
callq 0x4050
movq 0x88(%r14), %rax
cmpb $0x2, 0x3d(%rax)
jne 0x29379
cmpl $0x0, 0xc(%r14)
jle 0x29379
xorl %r15d, %r15d
movq %r14, 0x50(%rsp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c26
movq 0x10(%rsp), %rbx
movq 0x10(%rbx), %rsi
movq 0x88(%r14), %rcx
movsbq 0x8b8(%rcx), %rdx
shlq $0x3, %rdx
movq %rax, %rdi
callq 0x41c0
movq 0x18(%rbx), %rbp
movq %r14, %rdi
movq %r15, 0x18(%rsp)
movl %r15d, %esi
callq 0x25c42
movq %rax, %r12
movq 0x88(%r14), %rax
movsbq 0x8c8(%rax), %rcx
movq %rcx, 0x20(%rsp)
testq %rcx, %rcx
jle 0x292e2
movsbq 0x8b8(%rax), %r15
leaq (,%r15,8), %r13
xorl %ebx, %ebx
testb %r15b, %r15b
jle 0x292d8
xorl %r14d, %r14d
movq (%r12,%rbx,8), %rax
movq (%rax,%r14,8), %rdi
movq (%rbp,%rbx,8), %rax
movq (%rax,%r14,8), %rsi
movq %r13, %rdx
callq 0x41c0
incq %r14
cmpq %r14, %r15
jne 0x292b7
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0x292af
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rbp
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x25c62
movq %rax, %r12
movq 0x88(%rbx), %rax
movsbq 0x8c8(%rax), %rcx
movq %rcx, 0x20(%rsp)
testq %rcx, %rcx
jle 0x29362
movsbq 0x8b8(%rax), %r15
movsbq 0x8b9(%rax), %r13
shlq $0x3, %r13
xorl %ebx, %ebx
testb %r15b, %r15b
jle 0x29358
xorl %r14d, %r14d
movq (%r12,%rbx,8), %rax
movq (%rax,%r14,8), %rdi
movq (%rbp,%rbx,8), %rax
movq (%rax,%r14,8), %rsi
movq %r13, %rdx
callq 0x41c0
incq %r14
cmpq %r14, %r15
jne 0x29337
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0x2932f
movq 0x18(%rsp), %r15
incl %r15d
movq 0x50(%rsp), %r14
cmpl 0xc(%r14), %r15d
jl 0x2923e
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0xcbf8
movl $0xf8, %esi
movq %rbx, %rdi
callq 0x4220
movq 0x88(%r14), %rax
cmpb $0x2, 0x3d(%rax)
xorpd %xmm2, %xmm2
je 0x296ab
cmpl $0x0, 0x58(%rsp)
jle 0x296ab
xorpd %xmm0, %xmm0
xorl %r15d, %r15d
movsd %xmm0, 0x60(%rsp)
movq 0x88(%r14), %r12
movq 0x8e0(%r12), %rax
movl (%rax,%r15,4), %eax
movl %eax, 0x44(%rsp)
cmpb $0x0, 0x8c8(%r12)
movq %r15, 0x38(%rsp)
jle 0x29613
movq 0x8f8(%r12), %rax
movq (%rax,%r15,8), %rax
movq %rax, 0x90(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r15d, %r15d
movsd %xmm0, 0x48(%rsp)
movl $0xf8, %edi
callq 0x4210
movq %rax, %rbp
movsbl 0x8b8(%r12), %esi
movsbl 0x8b9(%r12), %edx
movl 0x8c0(%r12), %ecx
movl 0x8bc(%r12), %r8d
movsbl 0x8c8(%r12), %r9d
movsd 0x28(%r12), %xmm0
movsbl 0x30(%r12), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
callq 0xcb4c
movl %r15d, 0x18(%rsp)
movl %ebx, 0x20(%rsp)
movq %r14, %rdi
movq %r14, %r15
movq 0x38(%rsp), %r14
movl %r14d, %esi
callq 0x25c26
movq %rax, %rbx
movq %r15, %rdi
movl %r14d, %esi
callq 0x25c42
movq %rax, %r12
movq %r15, %rdi
movl %r14d, %esi
movq %r15, %r14
callq 0x25c62
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %rax, %rcx
movl 0x44(%rsp), %r8d
movq 0x90(%rsp), %r9
callq 0xd010
movq 0x88(%r15), %rax
testl %r13d, %r13d
jne 0x294d5
cmpb $0x0, 0x981(%rax)
je 0x294dd
movq $0x0, 0x10(%rbp)
cmpb $0x0, 0x982(%rax)
je 0x294ee
movq $0x0, 0x18(%rbp)
cmpb $0x0, 0x983(%rax)
movl 0x20(%rsp), %ebx
movl 0x18(%rsp), %r15d
je 0x29508
movq $0x0, 0x20(%rbp)
movq %rbp, %rdi
movl $0x2, %esi
callq 0xd392
movq %rbp, %rdi
movl $0x3, %esi
callq 0xd392
movq 0x88(%r14), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x29556
movq %rbp, %rdi
movl $0x8, %esi
callq 0xd392
movq %rbp, %rdi
movl $0x6, %esi
callq 0xd392
movq %rbp, %rdi
movl $0x4, %esi
callq 0xd392
movq 0x88(%r14), %rax
cmpb $0x5, 0x3f(%rax)
jne 0x29570
movq %rbp, %rdi
movl $0x4, %esi
callq 0xd392
movq %rbp, %rdi
movl $0x7, %esi
callq 0xd392
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x27d0e
addl 0x68(%rsp), %r15d
movsd 0x80(%rsp), %xmm1
addl 0x88(%rsp), %ebx
testl %r13d, %r13d
jne 0x295b3
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x88(%r14), %rax
movsbl 0x8c8(%rax), %eax
decl %eax
cmpl %eax, %r13d
je 0x295ce
movsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
movq %rbp, %rdi
callq 0xcbf8
movl $0xf8, %esi
movq %rbp, %rdi
callq 0x4220
movsd 0x20(%rsp), %xmm1
incl %r13d
movq 0x88(%r14), %r12
movsbl 0x8c8(%r12), %eax
movsd %xmm1, 0x10(%rsp)
cmpl %eax, %r13d
jl 0x29417
jmp 0x29626
xorpd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %r15d, %r15d
xorpd %xmm1, %xmm1
xorl %ebx, %ebx
testl %ebx, %ebx
jne 0x29631
cmpl 0x38(%r12), %r15d
jne 0x2968e
movl %r15d, %r8d
xorpd %xmm2, %xmm2
cmpltsd %xmm1, %xmm2
movsd 0xb2ab(%rip), %xmm0     # 0x348f0
andpd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd 0x60(%rsp), %xmm2
cmpb $0x0, 0x3c(%r12)
movq 0x38(%rsp), %r15
jne 0x29699
leaq 0xbf65(%rip), %rdi      # 0x355cc
movl %r15d, %esi
movl 0x44(%rsp), %edx
xorl %ecx, %ecx
movsd 0x48(%rsp), %xmm0
movl %ebx, %r9d
movb $0x2, %al
movapd %xmm2, 0x20(%rsp)
callq 0x4050
movapd 0x20(%rsp), %xmm2
jmp 0x29699
movsd 0x60(%rsp), %xmm2
movq 0x38(%rsp), %r15
incq %r15
movapd %xmm2, %xmm0
cmpq 0x58(%rsp), %r15
jne 0x293ba
movapd %xmm2, %xmm0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x296c3
movq %rax, %rbx
movl $0xf8, %esi
movq %rbp, %rdi
callq 0x4220
movq %rbx, %rdi
callq 0x4430
nop