// File: /workspace/github_c_makefile_star2_O1/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 0x1e57(%rip), %xmm0     # 0x33260
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), %rax
leaq (%r12,%rax,8), %r14
testl %eax, %eax
je 0x3148f
xorl %eax, %eax
movabsq $-0x10000000000000, %rcx # imm = 0xFFF0000000000000
movq %rax, (%r15,%rax,8)
movq %rcx, (%r14,%rax,8)
incq %rax
movl 0x38(%rbx), %edx
cmpq %rdx, %rax
jb 0x3147c
movapd 0x2539(%rip), %xmm1     # 0x339d0
xorl %ebp, %ebp
movapd %xmm1, (%rsp)
movq %rbp, %r13
movq 0x38(%rbx), %rcx
movq %rbp, %rax
xorl %edx, %edx
divq %rcx
testq %rdx, %rdx
jne 0x314c2
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 0x1d85(%rip), %xmm0     # 0x33260
testq %rax, %rax
je 0x314e5
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 0x1c08(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1c10(%rip), %xmm0     # 0x33190
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd 0x1cc8(%rip), %xmm0     # 0x33260
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 0x2410(%rip), %xmm1     # 0x339d0
subpd %xmm2, %xmm1
movapd (%rsp), %xmm2
mulpd %xmm1, %xmm2
movapd %xmm2, %xmm1
ucomisd 0x1a57(%rip), %xmm3     # 0x33030
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
jne 0x315e1
jnp 0x3164b
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movapd %xmm1, (%rsp)
movapd %xmm3, %xmm1
xorpd 0x1ad6(%rip), %xmm1     # 0x330d0
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 0x3166a
cmpq 0xa8(%rbx), %rbp
jne 0x31499
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x3168f
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 0x31679
mulsd 0x18(%rbx), %xmm1
movsd %xmm1, 0x18(%rbx)
movq %rbx, %rdi
callq 0x30ebb
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0x316d8
movsd 0x100(%rbx), %xmm0
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 0x316b4
movsd 0x100(%rbx), %xmm0
ucomisd 0x1948(%rip), %xmm0     # 0x33030
jb 0x316f0
sqrtsd %xmm0, %xmm0
jmp 0x316f5
callq 0x9990
movsd 0x38(%rbx), %xmm1
unpcklps 0x1a7f(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1a87(%rip), %xmm1     # 0x33190
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, %eax
ucomisd 0x100(%rbx), %xmm0
ja 0x3177a
movl $0x3, %eax
cmpq 0xa8(%rbx), %rbp
jne 0x31780
movl %eax, 0xb8(%rbx)
movq 0x58(%rbx), %rax
testq %rax, %rax
je 0x31792
movq 0x60(%rbx), %rsi
movq %rbx, %rdi
callq *%rax
cmpl $0x1, 0xb8(%rbx)
movapd 0x222f(%rip), %xmm1     # 0x339d0
je 0x31499
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