// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/svm.c
// Function: vl_svm_sgd_train
void _vl_svm_sgd_train (VlSvm *self)
{
  vl_index * permutation ;
  double * scores ;
  double * previousScores ;
  vl_uindex i, t, k ;
  double inner, gradient, rate, biasRate, p ;
  double factor = 1.0 ;
  double biasFactor = 1.0 ; /* to allow slower bias learning rate */
  vl_index t0 = VL_MAX(2, vl_ceil_d(1.0 / self->lambda)) ;
  //t0=2 ;

  double startTime = vl_get_cpu_time () ;
  VlRand * rand = vl_get_rand() ;

  permutation = vl_calloc(self->numData, sizeof(vl_index)) ;
  scores = vl_calloc(self->numData * 2, sizeof(double)) ;
  previousScores = scores + self->numData ;

  for (i = 0 ; i < (unsigned)self->numData; i++) {
    permutation [i] = i ;
    previousScores [i] = - VL_INFINITY_D ;
  }

  /*
   We store the w vector as the product fw (factor * model).
   We also use a different factor for the bias: biasFactor * biasMultiplier
   to enable a slower learning rate for the bias.

   Given this representation, it is easy to carry the two key operations:

   * Inner product: <fw,x> = f <w,x>

   * Model update: fp wp = fw - rate * lambda * w - rate * g
                         = f(1 - rate * lambda) w - rate * g

     Thus the update equations are:

                   fp = f(1 - rate * lambda), and
                   wp = w + rate / fp * g ;

   * Realization of the scaling factor. Before the statistics function
     is called, or training finishes, the factor (and biasFactor)
     are explicitly applied to the model and the bias.
  */

  for (t = 0 ; 1 ; ++t) {

    if (t % self->numData == 0) {
      /* once a new epoch is reached (all data have been visited),
       change permutation */
      vl_rand_permute_indexes(rand, permutation, self->numData) ;
    }

    /* pick a sample and compute update */
    i = permutation[t % self->numData] ;
    p = (self->weights) ? self->weights[i] : 1.0 ;
    p = VL_MAX(0.0, p) ; /* we assume non-negative weights, so this is just for robustness */
    inner = factor * self->innerProductFn(self->data, i, self->model) ;
    inner += biasFactor * (self->biasMultiplier * self->bias) ;
    gradient = p * self->lossDerivativeFn(inner, self->labels[i]) ;
    previousScores[i] = scores[i] ;
    scores[i] = inner ;

    /* apply update */
    rate = 1.0 /  (self->lambda * (t + t0)) ;
    biasRate = rate * self->biasLearningRate ;
    factor *= (1.0 - self->lambda * rate) ;
    biasFactor *= (1.0 - self->lambda * biasRate) ;

    /* debug: realize the scaling factor all the times */
    /*
    for (k = 0 ; k < self->dimension ; ++k) self->model[k] *= factor ;
    self->bias *= biasFactor;
    factor = 1.0 ;
    biasFactor = 1.0 ;
    */

    if (gradient != 0) {
      self->accumulateFn(self->data, i, self->model, - gradient * rate / factor) ;
      self->bias += self->biasMultiplier * (- gradient * biasRate / biasFactor) ;
    }

    /* call diagnostic occasionally */
    if ((t + 1) % self->diagnosticFrequency == 0 || t + 1 == self->maxNumIterations) {

      /* realize factor before computing statistics or completing training */
      for (k = 0 ; k < self->dimension ; ++k) self->model[k] *= factor ;
      self->bias *= biasFactor;
      factor = 1.0 ;
      biasFactor = 1.0 ;

      _vl_svm_update_statistics (self) ;

      for (k = 0 ; k < self->numData ; ++k) {
        double delta = scores[k] - previousScores[k] ;
        self->statistics.scoresVariation += delta * delta ;
      }
      self->statistics.scoresVariation = sqrt(self->statistics.scoresVariation) / self->numData ;

      self->statistics.elapsedTime = vl_get_cpu_time() - startTime ;
      self->statistics.iteration = t ;
      self->statistics.epoch = t / self->numData ;

      self->statistics.status = VlSvmStatusTraining ;
      if (self->statistics.scoresVariation < self->epsilon) {
        self->statistics.status = VlSvmStatusConverged ;
      }
      else if (t + 1 == self->maxNumIterations) {
        self->statistics.status = VlSvmStatusMaxNumIterationsReached ;
      }

      if (self->diagnosticFn) {
        self->diagnosticFn(self, self->diagnosticFnData) ;
      }

      if (self->statistics.status != VlSvmStatusTraining) {
        break ;
      }
    }
  } /* next iteration */

  vl_free (scores) ;
  vl_free (permutation) ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movsd 0x1d40(%rip), %xmm0     # 0x2d238
divsd 0x28(%rdi), %xmm0
callq 0x9a40
cvttsd2si %xmm0, %r13
callq 0x9370
movsd %xmm0, 0x40(%rsp)
callq 0x9910
movq %rax, 0x38(%rsp)
movq 0x38(%rbx), %rdi
pushq $0x8
popq %r12
movq %r12, %rsi
callq 0x9960
movq %rax, %r15
movq 0x38(%rbx), %rdi
addq %rdi, %rdi
movq %r12, %rsi
callq 0x9960
movq %rax, %r12
movq 0x38(%rbx), %rcx
leaq (%rax,%rcx,8), %r14
xorl %eax, %eax
movabsq $-0x10000000000000, %rdx # imm = 0xFFF0000000000000
movl %ecx, %esi
cmpq %rsi, %rax
jae 0x2b56d
movq %rax, (%r15,%rax,8)
movq %rdx, (%r14,%rax,8)
incq %rax
movq 0x38(%rbx), %rcx
jmp 0x2b555
cmpq $0x3, %r13
pushq $0x2
popq %rax
cmovgeq %r13, %rax
movq %rax, 0x48(%rsp)
movaps 0x240c(%rip), %xmm0     # 0x2d990
xorl %r13d, %r13d
movaps %xmm0, (%rsp)
movq %r13, %rax
xorl %edx, %edx
divq %rcx
testq %rdx, %rdx
jne 0x2b5b1
movq 0x38(%rsp), %rdi
movq %r15, %rsi
movq %rcx, %rdx
callq 0x9330
movq %r13, %rax
xorl %edx, %edx
divq 0x38(%rbx)
movq (%r15,%rdx,8), %rbp
movq 0x48(%rbx), %rax
movsd 0x1c77(%rip), %xmm0     # 0x2d238
testq %rax, %rax
je 0x2b5cb
movsd (%rax,%rbp,8), %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %rbp, %rsi
callq *0x90(%rbx)
movapd (%rsp), %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
mulsd %xmm0, %xmm1
movsd 0x20(%rbx), %xmm0
mulsd 0x18(%rbx), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x40(%rbx), %rax
movsd (%rax,%rbp,8), %xmm1
callq *0x80(%rbx)
movapd %xmm0, %xmm3
mulsd 0x20(%rsp), %xmm3
movsd (%r12,%rbp,8), %xmm0
movsd %xmm0, (%r14,%rbp,8)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r12,%rbp,8)
movsd 0x28(%rbx), %xmm1
movq 0x48(%rsp), %rax
addq %r13, %rax
movq %rax, %xmm0
punpckldq 0x1b02(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1b0a(%rip), %xmm0     # 0x2d170
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd 0x1bba(%rip), %xmm0     # 0x2d238
divsd %xmm2, %xmm0
movsd 0x118(%rbx), %xmm4
mulsd %xmm0, %xmm4
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd %xmm4, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
movapd 0x22ea(%rip), %xmm1     # 0x2d990
subpd %xmm2, %xmm1
mulpd (%rsp), %xmm1
ucomisd 0x1949(%rip), %xmm3     # 0x2d000
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
jne 0x2b6c3
jnp 0x2b72d
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movapd %xmm1, (%rsp)
movapd %xmm3, %xmm1
xorpd 0x19c4(%rip), %xmm1     # 0x2d0a0
mulsd %xmm1, %xmm0
divsd %xmm2, %xmm0
movq %rbp, %rsi
movapd %xmm2, 0x20(%rsp)
movapd %xmm3, 0x10(%rsp)
movapd %xmm4, 0x50(%rsp)
callq *0x98(%rbx)
movapd 0x20(%rsp), %xmm2
movapd (%rsp), %xmm1
movapd 0x50(%rsp), %xmm3
mulsd 0x10(%rsp), %xmm3
divsd %xmm1, %xmm3
movsd 0x18(%rbx), %xmm0
mulsd 0x20(%rbx), %xmm3
subsd %xmm3, %xmm0
movsd %xmm0, 0x18(%rbx)
leaq 0x1(%r13), %rbp
movq %rbp, %rax
xorl %edx, %edx
divq 0x68(%rbx)
testq %rdx, %rdx
je 0x2b74c
cmpq 0xa8(%rbx), %rbp
jne 0x2b87e
movq 0x8(%rbx), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x2b76e
movq 0x10(%rbx), %rdx
movsd (%rdx,%rcx,8), %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0x2b752
mulsd 0x18(%rbx), %xmm1
movsd %xmm1, 0x18(%rbx)
movq %rbx, %rdi
callq 0x2afba
movq 0x38(%rbx), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x2b7b0
movsd (%r12,%rcx,8), %xmm0
subsd (%r14,%rcx,8), %xmm0
mulsd %xmm0, %xmm0
addsd 0x100(%rbx), %xmm0
movsd %xmm0, 0x100(%rbx)
incq %rcx
jmp 0x2b786
movsd 0x100(%rbx), %xmm0
ucomisd 0x1840(%rip), %xmm0     # 0x2d000
jb 0x2b7c8
sqrtsd %xmm0, %xmm0
jmp 0x2b7cd
callq 0x9a00
movsd 0x38(%rbx), %xmm1
unpcklps 0x1987(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x198f(%rip), %xmm1     # 0x2d170
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movsd %xmm0, 0x100(%rbx)
callq 0x9370
subsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x108(%rbx)
movq %r13, 0xc0(%rbx)
movq %r13, %rax
xorl %edx, %edx
divq 0x38(%rbx)
movq %rax, 0xc8(%rbx)
movl $0x1, 0xb8(%rbx)
movsd 0xb0(%rbx), %xmm0
ucomisd 0x100(%rbx), %xmm0
pushq $0x2
popq %rcx
ja 0x2b851
pushq $0x3
popq %rcx
pushq $0x1
popq %rax
cmpq 0xa8(%rbx), %rbp
jne 0x2b859
movl %ecx, 0xb8(%rbx)
movl %ecx, %eax
movq 0x58(%rbx), %rcx
testq %rcx, %rcx
je 0x2b871
movq 0x60(%rbx), %rsi
movq %rbx, %rdi
callq *%rcx
movl 0xb8(%rbx), %eax
movapd 0x2117(%rip), %xmm1     # 0x2d990
cmpl $0x1, %eax
jne 0x2b88f
movq 0x38(%rbx), %rcx
movq %rbp, %r13
movapd %xmm1, (%rsp)
jmp 0x2b58b
movq %r12, %rdi
callq 0x95b0
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x95b0