// File: /workspace/github_c_makefile_star2_O3/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 0x114c(%rip), %xmm0     # 0x32270
divsd 0x28(%rdi), %xmm0
callq 0x99c0
cvttsd2si %xmm0, %rax
cmpq $0x3, %rax
movl $0x2, %ecx
cmovgeq %rax, %rcx
movq %rcx, 0x48(%rsp)
callq 0x9350
movsd %xmm0, 0x40(%rsp)
callq 0x98a0
movq %rax, 0x38(%rsp)
movq 0x38(%rbx), %rdi
movl $0x8, %esi
callq 0x98f0
movq %rax, %r15
movq 0x38(%rbx), %rdi
addq %rdi, %rdi
movl $0x8, %esi
callq 0x98f0
movq %rax, %r12
movq 0x38(%rbx), %rcx
leaq (%rax,%rcx,8), %r14
testl %ecx, %ecx
je 0x311ad
xorl %eax, %eax
movabsq $-0x10000000000000, %rdx # imm = 0xFFF0000000000000
movq %rax, (%r15,%rax,8)
movq %rdx, (%r14,%rax,8)
incq %rax
movq 0x38(%rbx), %rcx
movl %ecx, %esi
cmpq %rsi, %rax
jb 0x31197
movapd 0x180b(%rip), %xmm0     # 0x329c0
xorl %r13d, %r13d
movapd %xmm0, (%rsp)
movq %r13, %rax
xorl %edx, %edx
divq %rcx
testq %rdx, %rdx
jne 0x311e3
movq 0x38(%rsp), %rdi
movq %r15, %rsi
movq %rcx, %rdx
callq 0x9320
movq %r13, %rax
xorl %edx, %edx
divq 0x38(%rbx)
movq (%r15,%rdx,8), %rbp
movq 0x48(%rbx), %rax
movsd 0x107d(%rip), %xmm0     # 0x32270
testq %rax, %rax
je 0x311fd
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 0xef0(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xef8(%rip), %xmm0      # 0x32190
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd 0xfc0(%rip), %xmm0      # 0x32270
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 0x16e8(%rip), %xmm1     # 0x329c0
subpd %xmm2, %xmm1
mulpd (%rsp), %xmm1
ucomisd 0xd47(%rip), %xmm3      # 0x32030
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
jne 0x312f5
jnp 0x3135f
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movapd %xmm1, (%rsp)
movapd %xmm3, %xmm1
xorpd 0xdc2(%rip), %xmm1      # 0x320d0
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 0x3137e
cmpq 0xa8(%rbx), %rbp
jne 0x314b8
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x313a3
movq 0x10(%rbx), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x3138d
mulsd 0x18(%rbx), %xmm1
movsd %xmm1, 0x18(%rbx)
movq %rbx, %rdi
callq 0x30bc3
movq 0x38(%rbx), %rax
movsd 0x100(%rbx), %xmm0
testq %rax, %rax
je 0x313ec
xorl %ecx, %ecx
movsd (%r12,%rcx,8), %xmm1
subsd (%r14,%rcx,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rbx)
incq %rcx
cmpq %rcx, %rax
jne 0x313c8
ucomisd 0xc3c(%rip), %xmm0      # 0x32030
jb 0x313fc
sqrtsd %xmm0, %xmm0
jmp 0x31401
callq 0x9990
movsd 0x38(%rbx), %xmm1
unpcklps 0xd73(%rip), %xmm1      # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xd7b(%rip), %xmm1      # 0x32190
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movsd %xmm0, 0x100(%rbx)
callq 0x9350
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
movl $0x2, %ecx
ucomisd 0x100(%rbx), %xmm0
ja 0x3148b
movl $0x1, %eax
movl $0x3, %ecx
cmpq 0xa8(%rbx), %rbp
jne 0x31493
movl %ecx, 0xb8(%rbx)
movl %ecx, %eax
movq 0x58(%rbx), %rcx
testq %rcx, %rcx
je 0x314ab
movq 0x60(%rbx), %rsi
movq %rbx, %rdi
callq *%rcx
movl 0xb8(%rbx), %eax
movapd 0x150d(%rip), %xmm1     # 0x329c0
cmpl $0x1, %eax
jne 0x314c9
movq 0x38(%rbx), %rcx
movq %rbp, %r13
movapd %xmm1, (%rsp)
jmp 0x311bd
movq %r12, %rdi
callq 0x9580
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9580