// File: 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
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x138(%rbp)
movq 0x88(%rax), %rcx
movl 0x8c0(%rcx), %ecx
movl %ecx, -0x10(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x0, %eax
jle 0x3f65f
movq $0x0, -0x28(%rbp)
movl $0xf8, %edi
callq 0x52d0
movq %rax, %rdi
movq -0x138(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x140(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsbl 0x8c8(%rax), %r9d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r10d
movq %rsp, %rax
movl %r10d, (%rax)
callq 0x114e0
jmp 0x3f1ad
movq -0x138(%rbp), %rdi
movq -0x140(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x158(%rbp)
xorl %esi, %esi
callq 0x39b90
movq -0x138(%rbp), %rdi
movq %rax, -0x150(%rbp)
xorl %esi, %esi
callq 0x39c00
movq -0x138(%rbp), %rdi
movq %rax, -0x148(%rbp)
xorl %esi, %esi
callq 0x39c70
movq -0x158(%rbp), %rdi
movq -0x150(%rbp), %rsi
movq -0x148(%rbp), %rdx
movq %rax, %rcx
movq -0x138(%rbp), %rax
movq 0x88(%rax), %r8
movl 0x8dc(%r8), %r8d
movq 0x88(%rax), %rax
movq 0x8f0(%rax), %r9
callq 0x11e00
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x3f285
movq -0x48(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x3f285
movq -0x140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0xf8, %esi
callq 0x52e0
jmp 0x3fb58
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x3f2ab
movq -0x48(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x3f2d1
movq -0x48(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x48(%rbp), %rdi
movl $0x2, %esi
callq 0x12130
movq -0x48(%rbp), %rdi
movl $0x3, %esi
callq 0x12130
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x3f32e
movq -0x48(%rbp), %rdi
movl $0x8, %esi
callq 0x12130
movq -0x48(%rbp), %rdi
movl $0x6, %esi
callq 0x12130
movq -0x48(%rbp), %rdi
movl $0x4, %esi
callq 0x12130
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x3f353
movq -0x48(%rbp), %rdi
movl $0x4, %esi
callq 0x12130
movq -0x48(%rbp), %rdi
movl $0x7, %esi
callq 0x12130
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movslq 0x8dc(%rax), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x60(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x138(%rbp), %rcx
movl -0xc(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x3f3fd
movq -0x138(%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 0x3f38b
movq -0x138(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0x3cb20
leaq -0x40(%rbp), %rdi
leaq -0x88(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movl $0x0, -0xc(%rbp)
movq -0x138(%rbp), %rcx
movl -0xc(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x3f482
movq -0x138(%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 0x3f430
movq -0x60(%rbp), %rdi
callq 0x5380
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x3f4d5
movq -0x138(%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 0x1121f(%rip), %rdi     # 0x506ed
movb $0x2, %al
callq 0x5060
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x2, %eax
jne 0x3f62f
movl $0x0, -0x8c(%rbp)
movq -0x138(%rbp), %rcx
movl -0x8c(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x3f62d
movq -0x138(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x39b90
movq %rax, %rcx
movq -0x138(%rbp), %rax
movq %rcx, -0x98(%rbp)
movq -0x48(%rbp), %rcx
movq 0x10(%rcx), %rdi
movq -0x98(%rbp), %rsi
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %edx
callq 0xc440
movq -0x138(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x168(%rbp)
movl -0x8c(%rbp), %esi
callq 0x39c00
movq -0x168(%rbp), %rdi
movq %rax, %rsi
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rcx
movsbl 0x8c8(%rcx), %edx
movq 0x88(%rax), %rcx
movsbl 0x8b8(%rcx), %ecx
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %r8d
callq 0x11450
movq -0x138(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x160(%rbp)
movl -0x8c(%rbp), %esi
callq 0x39c70
movq -0x160(%rbp), %rdi
movq %rax, %rsi
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rcx
movsbl 0x8c8(%rcx), %edx
movq 0x88(%rax), %rcx
movsbl 0x8b8(%rcx), %ecx
movq 0x88(%rax), %rax
movsbl 0x8b9(%rax), %r8d
callq 0x11450
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x3f4fa
jmp 0x3f62f
movq -0x48(%rbp), %rax
movq %rax, -0x170(%rbp)
cmpq $0x0, %rax
je 0x3f65d
movq -0x170(%rbp), %rdi
callq 0x117b0
movq -0x170(%rbp), %rdi
movl $0xf8, %esi
callq 0x52e0
jmp 0x3f65f
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x2, %eax
je 0x3fb4a
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x3fb48
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x8e0(%rcx), %rcx
movslq -0xc(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x9c(%rbp)
movq 0x88(%rax), %rax
movq 0x8f8(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
movl $0x0, -0xd0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xb8(%rbp)
movl $0x0, -0xb0(%rbp)
movl $0x0, -0xac(%rbp)
movb $0x0, -0xd1(%rbp)
movq -0x138(%rbp), %rcx
movsbl -0xd1(%rbp), %eax
movq 0x88(%rcx), %rcx
movsbl 0x8c8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3fa8b
movl $0xf8, %edi
callq 0x52d0
movq %rax, %rdi
movq -0x138(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x178(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsbl 0x8c8(%rax), %r9d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r10d
movq %rsp, %rax
movl %r10d, (%rax)
callq 0x114e0
jmp 0x3f78a
movq -0x138(%rbp), %rdi
movq -0x178(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x190(%rbp)
movl -0xc(%rbp), %esi
callq 0x39b90
movq -0x138(%rbp), %rdi
movq %rax, -0x188(%rbp)
movl -0xc(%rbp), %esi
callq 0x39c00
movq -0x138(%rbp), %rdi
movq %rax, -0x180(%rbp)
movl -0xc(%rbp), %esi
callq 0x39c70
movq -0x190(%rbp), %rdi
movq -0x188(%rbp), %rsi
movq -0x180(%rbp), %rdx
movq %rax, %rcx
movl -0x9c(%rbp), %r8d
movq -0xa8(%rbp), %r9
callq 0x11e00
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
jne 0x3f832
movsbl -0xd1(%rbp), %eax
cmpl $0x0, %eax
jle 0x3f865
movq -0x108(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x3f865
movq -0x178(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0xf8, %esi
callq 0x52e0
jmp 0x3fb58
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x3f88e
movq -0x108(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x3f8b7
movq -0x108(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x108(%rbp), %rdi
movl $0x2, %esi
callq 0x12130
movq -0x108(%rbp), %rdi
movl $0x3, %esi
callq 0x12130
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x3f923
movq -0x108(%rbp), %rdi
movl $0x8, %esi
callq 0x12130
movq -0x108(%rbp), %rdi
movl $0x6, %esi
callq 0x12130
movq -0x108(%rbp), %rdi
movl $0x4, %esi
callq 0x12130
movq -0x138(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x3f94b
movq -0x108(%rbp), %rdi
movl $0x4, %esi
callq 0x12130
movq -0x108(%rbp), %rdi
movl $0x7, %esi
callq 0x12130
movq -0x138(%rbp), %rsi
movq -0x108(%rbp), %rdx
leaq -0x130(%rbp), %rdi
callq 0x3cb20
leaq -0x100(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movl -0x100(%rbp), %eax
addl -0xd0(%rbp), %eax
movl %eax, -0xd0(%rbp)
movl -0xe0(%rbp), %eax
addl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
movsbl -0xd1(%rbp), %eax
cmpl $0x0, %eax
jne 0x3f9d0
movsd -0xf8(%rbp), %xmm0
movsd %xmm0, -0x198(%rbp)
jmp 0x3f9e0
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0x198(%rbp)
movq -0x138(%rbp), %rcx
movsd -0x198(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsbl -0xd1(%rbp), %eax
movq 0x88(%rcx), %rcx
movsbl 0x8c8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3fa25
movsd -0xe8(%rbp), %xmm0
movsd %xmm0, -0x1a0(%rbp)
jmp 0x3fa35
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movsd -0x1a0(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x1a8(%rbp)
cmpq $0x0, %rax
je 0x3fa76
movq -0x1a8(%rbp), %rdi
callq 0x117b0
movq -0x1a8(%rbp), %rdi
movl $0xf8, %esi
callq 0x52e0
jmp 0x3fa78
movb -0xd1(%rbp), %al
addb $0x1, %al
movb %al, -0xd1(%rbp)
jmp 0x3f707
cmpl $0x0, -0xb0(%rbp)
jne 0x3fab1
movq -0x138(%rbp), %rcx
movl -0xd0(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x3fb38
movq -0x138(%rbp), %rax
movsd -0xb8(%rbp), %xmm0
movsd -0xb8(%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 0x3fb36
movl -0xc(%rbp), %esi
movl -0x9c(%rbp), %edx
movl -0xac(%rbp), %ecx
movl -0xd0(%rbp), %r8d
movsd -0xc8(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm1
movl -0xb0(%rbp), %r9d
leaq 0x10b71(%rip), %rdi     # 0x506a0
movb $0x2, %al
callq 0x5060
jmp 0x3fb38
jmp 0x3fb3a
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x3f681
jmp 0x3fb4a
movsd -0x18(%rbp), %xmm0
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x5540
nopw %cs:(%rax,%rax)