// File: /workspace/github_cpp_makefile_star2_O3/CastellanZhang[P]lambdaFM/./src/FTRL/ftrl_trainer.h
// Function: ftrl_trainer::trainPair(fm_sample&, fm_sample&, double)
void ftrl_trainer::trainPair(fm_sample& sample1, fm_sample& sample2, const double deltaNDCG)
{
    unordered_map<string, ftrl_model_unit*> theta;
    for(unordered_map<string, double>::iterator iter = sample1.x.begin(); iter != sample1.x.end(); ++iter)
    {
        const string& index = iter->first;
        if(theta.find(index) == theta.end())
        {
            theta[index] = pModel->getOrInitModelUnit(index);
        }
    }
    for(unordered_map<string, double>::iterator iter = sample2.x.begin(); iter != sample2.x.end(); ++iter)
    {
        const string& index = iter->first;
        if(theta.find(index) == theta.end())
        {
            theta[index] = pModel->getOrInitModelUnit(index);
        }
    }
    //update w via FTRL
    if(k1)
    {
        for(unordered_map<string, ftrl_model_unit*>::iterator iter = theta.begin(); iter != theta.end(); ++iter)
        {
            ftrl_model_unit& mu = *(iter->second);
            mu.mtx.lock();
            if(fabs(mu.w_zi) <= w_l1)
            {
                mu.wi = 0.0;
            }
            else
            {
                if(force_v_sparse && mu.w_ni > 0 && 0.0 == mu.wi)
                {
                    mu.reinit_vi(pModel->init_mean, pModel->init_stdev);
                }
                mu.wi = (-1) *
                    (1 / (w_l2 + (w_beta + sqrt(mu.w_ni)) / w_alpha)) *
                    (mu.w_zi - utils::sgn(mu.w_zi) * w_l1);
            }
            mu.mtx.unlock();
        }
    }
    //update v via FTRL
    for(unordered_map<string, ftrl_model_unit*>::iterator iter = theta.begin(); iter != theta.end(); ++iter)
    {
        ftrl_model_unit& mu = *(iter->second);
        for(int f = 0; f < pModel->factor_num; ++f)
        {
            mu.mtx.lock();
            double& vif = mu.vi[f];
            double& v_nif = mu.v_ni[f];
            double& v_zif = mu.v_zi[f];
            if(v_nif > 0)
            {
                if(force_v_sparse && 0.0 == mu.wi)
                {
                    vif = 0.0;
                }
                else if(fabs(v_zif) <= v_l1)
                {
                    vif = 0.0;
                }
                else
                {
                    vif = (-1) *
                        (1 / (v_l2 + (v_beta + sqrt(v_nif)) / v_alpha)) *
                        (v_zif - utils::sgn(v_zif) * v_l1);
                }
            }
            mu.mtx.unlock();
        }
    }
    vector<double> sum1(pModel->factor_num);
    double p1 = pModel->predict(sample1.x, theta, sum1);
    vector<double> sum2(pModel->factor_num);
    double p2 = pModel->predict(sample2.x, theta, sum2);
    double lambda12 = -1 / (1 + exp(p1 - p2)) * deltaNDCG;
    //update w_n, w_z
    if(k1)
    {
        for(unordered_map<string, ftrl_model_unit*>::iterator iter = theta.begin(); iter != theta.end(); ++iter)
        {
            ftrl_model_unit& mu = *(iter->second);
            const string& index = iter->first;
            unordered_map<string, double>::iterator iter_x1 = sample1.x.find(index);
            double xi1 = (iter_x1 == sample1.x.end()) ? 0 : iter_x1->second;
            unordered_map<string, double>::iterator iter_x2 = sample2.x.find(index);
            double xi2 = (iter_x2 == sample2.x.end()) ? 0 : iter_x2->second;
            double xi = xi1 - xi2;
            if(xi != 0)
            {
                mu.mtx.lock();
                double w_gi = lambda12 * xi;
                double w_si = 1 / w_alpha * (sqrt(mu.w_ni + w_gi * w_gi) - sqrt(mu.w_ni));
                mu.w_zi += w_gi - w_si * mu.wi;
                mu.w_ni += w_gi * w_gi;
                mu.mtx.unlock();
            }
        }
    }
    //update v_n, v_z
    for(unordered_map<string, ftrl_model_unit*>::iterator iter = theta.begin(); iter != theta.end(); ++iter)
    {
        ftrl_model_unit& mu = *(iter->second);
        const string& index = iter->first;
        unordered_map<string, double>::iterator iter_x1 = sample1.x.find(index);
        double xi1 = (iter_x1 == sample1.x.end()) ? 0 : iter_x1->second;
        unordered_map<string, double>::iterator iter_x2 = sample2.x.find(index);
        double xi2 = (iter_x2 == sample2.x.end()) ? 0 : iter_x2->second;
        for(int f = 0; f < pModel->factor_num; ++f)
        {
            mu.mtx.lock();
            double& vif = mu.vi[f];
            double& v_nif = mu.v_ni[f];
            double& v_zif = mu.v_zi[f];
            double v_gif = lambda12 * ((sum1[f] * xi1 - vif * xi1 * xi1) - (sum2[f] * xi2 - vif * xi2 * xi2));
            double v_sif = 1 / v_alpha * (sqrt(v_nif + v_gif * v_gif) - sqrt(v_nif));
            v_zif += v_gif - v_sif * vif;
            v_nif += v_gif * v_gif;
            //有的特征在整个训练集中只出现一次，这里还需要对vif做一次处理
            if(force_v_sparse && v_nif > 0 && 0.0 == mu.wi)
            {
                vif = 0.0;
            }
            mu.mtx.unlock();
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movsd %xmm0, 0x78(%rsp)
movq %rdx, 0x40(%rsp)
movq %rdi, %r15
leaq 0x120(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq %rsi, 0x50(%rsp)
movq 0x38(%rsi), %r14
testq %r14, %r14
je 0x4d27
leaq 0xf0(%rsp), %r12
leaq 0xb8(%rsp), %r13
leaq 0x8(%r14), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x8608
testq %rax, %rax
jne 0x4d1f
movq 0x8(%r15), %rbx
leaq 0xc8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x83c4
movq %rbx, %rdi
movq %r13, %rsi
callq 0x387e
movq %rax, %rbx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x8a30
movq %rbx, (%rax)
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x4d1f
callq 0x32a0
movq (%r14), %r14
testq %r14, %r14
jne 0x4cae
movq 0x40(%rsp), %rax
movq 0x38(%rax), %r14
testq %r14, %r14
je 0x4dc2
leaq 0xf0(%rsp), %r12
leaq 0x98(%rsp), %r13
leaq 0x8(%r14), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x8608
testq %rax, %rax
jne 0x4dba
movq 0x8(%r15), %rbx
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x83c4
movq %rbx, %rdi
movq %r13, %rsi
callq 0x387e
movq %rax, %rbx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x8a30
movq %rbx, (%rax)
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x4dba
callq 0x32a0
movq (%r14), %r14
testq %r14, %r14
jne 0x4d49
movq 0x100(%rsp), %rbx
cmpb $0x0, 0x50(%r15)
movq %r15, 0x48(%rsp)
je 0x4f17
testq %rbx, %rbx
je 0x5089
movq 0x28(%rbx), %r13
leaq 0x60(%r13), %r12
movq %r12, %rdi
callq 0x33a0
testl %eax, %eax
jne 0x54f3
movsd 0x10(%r13), %xmm0
andpd 0x7227(%rip), %xmm0     # 0xc030
movsd 0x20(%r15), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm0, %xmm1
jae 0x4ef5
cmpb $0x1, 0x51(%r15)
jne 0x4e5c
movsd 0x8(%r13), %xmm0
ucomisd 0x71ee(%rip), %xmm0     # 0xc020
jbe 0x4e5c
movsd (%r13), %xmm0
ucomisd 0x71de(%rip), %xmm0     # 0xc020
jne 0x4e5c
jp 0x4e5c
movq 0x8(%r15), %rax
movsd 0x40(%rax), %xmm1
movsd 0x48(%rax), %xmm0
movq %r13, %rdi
callq 0x6a96
movsd 0x18(%r15), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x28(%r15), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%r13), %xmm0
ucomisd 0x719e(%rip), %xmm0     # 0xc020
jb 0x4e8a
sqrtsd %xmm0, %xmm0
jmp 0x4e8f
callq 0x3220
movsd %xmm0, 0x30(%rsp)
movsd 0x10(%r15), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x10(%r13), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0xb458
movsd 0x60(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x7142(%rip), %xmm0     # 0xc018
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd 0x20(%r15), %xmm1
movsd 0x8(%rsp), %xmm2
subsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, (%r13)
movq %r12, %rdi
callq 0x31f0
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x4de3
movq 0x100(%rsp), %rbx
testq %rbx, %rbx
je 0x5089
movq 0x8(%r15), %rax
movq %rbx, 0x58(%rsp)
cmpl $0x0, 0x38(%rax)
jle 0x5076
movq 0x58(%rsp), %rax
movq 0x28(%rax), %r13
leaq 0x60(%r13), %r12
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x33a0
testl %eax, %eax
jne 0x54e5
movq 0x30(%r13), %rax
movsd (%rax,%rbp,8), %xmm0
ucomisd 0x70bd(%rip), %xmm0     # 0xc020
jbe 0x505a
movq 0x18(%r13), %r14
movq %r15, %rax
movq 0x48(%r13), %r15
cmpb $0x1, 0x51(%rax)
jne 0x4f90
movsd (%r13), %xmm1
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm1
jne 0x4f90
jnp 0x504f
movsd (%r15,%rbp,8), %xmm1
andpd 0x7092(%rip), %xmm1     # 0xc030
movq 0x48(%rsp), %rax
movsd 0x40(%rax), %xmm2
xorpd %xmm3, %xmm3
ucomisd %xmm1, %xmm2
jae 0x504f
movq 0x48(%rsp), %rax
movsd 0x38(%rax), %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x48(%rax), %xmm1
movsd %xmm1, 0x60(%rsp)
ucomisd 0x7047(%rip), %xmm0     # 0xc020
jb 0x4fe1
sqrtsd %xmm0, %xmm0
jmp 0x4fe6
callq 0x3220
movsd %xmm0, 0x30(%rsp)
movq 0x48(%rsp), %rbx
movsd 0x30(%rbx), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd (%r15,%rbp,8), %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0xb458
movsd 0x8(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x6fe7(%rip), %xmm0     # 0xc018
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd 0x40(%rbx), %xmm1
movsd 0x10(%rsp), %xmm3
subsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
movsd %xmm3, (%r14,%rbp,8)
movq 0x48(%rsp), %r15
movq %r12, %rdi
callq 0x31f0
incq %rbp
movq 0x8(%r15), %rax
movslq 0x38(%rax), %rcx
cmpq %rcx, %rbp
jl 0x4f42
movq 0x58(%rsp), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x4f24
jmp 0x508d
movq 0x8(%r15), %rax
movslq 0x38(%rax), %rsi
leaq 0xd8(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x6af4
movq 0x50(%rsp), %rsi
addq $0x28, %rsi
movq 0x8(%r15), %rdi
leaq 0xf0(%rsp), %rdx
leaq 0xd8(%rsp), %rcx
movq %rsi, 0x50(%rsp)
callq 0x395a
movsd %xmm0, 0x10(%rsp)
movq 0x8(%r15), %rax
movslq 0x38(%rax), %rsi
leaq 0x80(%rsp), %rdi
leaq 0x27(%rsp), %rdx
callq 0x6af4
movq 0x40(%rsp), %rsi
addq $0x28, %rsi
movq 0x8(%r15), %rdi
leaq 0xf0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movq %rsi, 0x40(%rsp)
callq 0x395a
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x3490
addsd 0x6ee1(%rip), %xmm0     # 0xc010
movsd 0x6ee1(%rip), %xmm1     # 0xc018
divsd %xmm0, %xmm1
mulsd 0x78(%rsp), %xmm1
movsd %xmm1, 0x28(%rsp)
movq 0x100(%rsp), %rbx
cmpb $0x1, 0x50(%r15)
jne 0x5499
testq %rbx, %rbx
je 0x54a2
leaq 0x8(%rbx), %r12
movq 0x28(%rbx), %r13
movq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x84f0
testq %rax, %rax
je 0x5184
movsd 0x28(%rax), %xmm0
jmp 0x5188
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x84f0
testq %rax, %rax
je 0x51a7
movsd 0x28(%rax), %xmm0
jmp 0x51ab
xorpd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
ucomisd 0x6e5d(%rip), %xmm1     # 0xc020
jne 0x51cb
jnp 0x52a0
leaq 0x60(%r13), %r12
movq %r12, %rdi
callq 0x33a0
testl %eax, %eax
jne 0x54fa
movsd 0x10(%rsp), %xmm0
mulsd 0x28(%rsp), %xmm0
movsd 0x10(%r15), %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x8(%r13), %xmm0
movsd %xmm1, 0x60(%rsp)
addsd %xmm1, %xmm0
ucomisd 0x6e03(%rip), %xmm0     # 0xc020
jb 0x5228
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x5231
callq 0x3220
movapd %xmm0, %xmm1
movsd 0x8(%r13), %xmm0
ucomisd 0x6de1(%rip), %xmm0     # 0xc020
jb 0x5247
sqrtsd %xmm0, %xmm0
jmp 0x5258
movsd %xmm1, 0x30(%rsp)
callq 0x3220
movsd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x6db4(%rip), %xmm0     # 0xc018
divsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
mulsd (%r13), %xmm0
addsd 0x10(%rsp), %xmm0
addsd 0x10(%r13), %xmm0
movsd %xmm0, 0x10(%r13)
movsd 0x60(%rsp), %xmm0
addsd 0x8(%r13), %xmm0
movsd %xmm0, 0x8(%r13)
movq %r12, %rdi
callq 0x31f0
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x5163
movq 0x100(%rsp), %rbx
jmp 0x5499
leaq 0x8(%rbx), %r12
movq 0x28(%rbx), %r13
movq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x84f0
testq %rax, %rax
je 0x52da
movsd 0x28(%rax), %xmm0
jmp 0x52de
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x84f0
movq %rbx, 0x78(%rsp)
testq %rax, %rax
je 0x5302
movsd 0x28(%rax), %xmm0
jmp 0x5306
xorpd %xmm0, %xmm0
movq 0x8(%r15), %rax
cmpl $0x0, 0x38(%rax)
jle 0x5491
leaq 0x60(%r13), %r12
movapd 0x30(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd %xmm1, 0x30(%rsp)
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x33a0
testl %eax, %eax
jne 0x54ec
movq 0x18(%r13), %r14
movq 0x30(%r13), %rbx
movq %r15, %rdx
movq 0x48(%r13), %r15
movq 0xd8(%rsp), %rax
movq 0x80(%rsp), %rcx
movsd (%r14,%rbp,8), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd 0x30(%rsp), %xmm1
mulpd %xmm1, %xmm0
mulpd %xmm1, %xmm0
movsd (%rax,%rbp,8), %xmm2
movhpd (%rcx,%rbp,8), %xmm2    # xmm2 = xmm2[0],mem[0]
mulpd %xmm1, %xmm2
subpd %xmm0, %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
subsd %xmm0, %xmm2
mulsd 0x28(%rsp), %xmm2
movsd 0x30(%rdx), %xmm3
movapd %xmm2, %xmm1
mulsd %xmm2, %xmm1
movsd (%rbx,%rbp,8), %xmm0
addsd %xmm1, %xmm0
ucomisd 0x6c6d(%rip), %xmm0     # 0xc020
movapd %xmm2, 0x10(%rsp)
movapd %xmm1, 0x60(%rsp)
movsd %xmm3, 0x8(%rsp)
jb 0x53d0
xorps %xmm4, %xmm4
sqrtsd %xmm0, %xmm4
jmp 0x53d9
callq 0x3220
movapd %xmm0, %xmm4
movsd (%rbx,%rbp,8), %xmm0
ucomisd 0x6c3a(%rip), %xmm0     # 0xc020
jb 0x53ee
sqrtsd %xmm0, %xmm0
jmp 0x53ff
movsd %xmm4, 0x58(%rsp)
callq 0x3220
movsd 0x58(%rsp), %xmm4
movapd 0x10(%rsp), %xmm1
movapd 0x60(%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
subsd %xmm0, %xmm4
movsd 0x6bfb(%rip), %xmm0     # 0xc018
divsd %xmm3, %xmm0
mulsd %xmm4, %xmm0
mulsd (%r14,%rbp,8), %xmm0
addsd %xmm1, %xmm0
addsd (%r15,%rbp,8), %xmm0
movsd %xmm0, (%r15,%rbp,8)
addsd (%rbx,%rbp,8), %xmm2
movsd %xmm2, (%rbx,%rbp,8)
movq 0x48(%rsp), %r15
cmpb $0x1, 0x51(%r15)
jne 0x5475
ucomisd 0x6bc7(%rip), %xmm2     # 0xc020
jbe 0x5475
movsd (%r13), %xmm0
ucomisd 0x6bb7(%rip), %xmm0     # 0xc020
jne 0x5475
jp 0x5475
movq $0x0, (%r14,%rbp,8)
movq %r12, %rdi
callq 0x31f0
incq %rbp
movq 0x8(%r15), %rax
movslq 0x38(%rax), %rax
cmpq %rax, %rbp
jl 0x532a
movq 0x78(%rsp), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x52b9
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x54b4
callq 0x32a0
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x54c6
callq 0x32a0
leaq 0xf0(%rsp), %rdi
callq 0x7f26
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x31a0
movl %eax, %edi
callq 0x31a0
movl %eax, %edi
callq 0x31a0
movl %eax, %edi
callq 0x31a0
jmp 0x5553
jmp 0x5505
movq %rax, %rbx
jmp 0x5568
jmp 0x557c
jmp 0x557c
jmp 0x5553
jmp 0x557c
jmp 0x557c
jmp 0x5553
jmp 0x5553
jmp 0x557c
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
jmp 0x5542
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
jne 0x5575
jmp 0x557f
jmp 0x5553
jmp 0x5553
jmp 0x557c
jmp 0x557c
jmp 0x557c
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x5568
callq 0x32a0
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x557f
callq 0x32a0
jmp 0x557f
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x7f26
movq %rbx, %rdi
callq 0x3470