// File: fabianp[P]ProxASAGA/proxasaga_atomic.cpp
// Function: saga_single_thread(std::atomic<double>*, std::atomic<double>*, std::atomic<double>*, double*, long*, long*, double*, double*, long, long, double, double, double, long, double*, double*, int, long)
void saga_single_thread(
        std::atomic<double>* x, std::atomic<double>* memory_gradient, std::atomic<double>* gradient_average,
        double* A_data, int64_t* A_indices, int64_t* A_indptr, double* b,
        double* d, int64_t n_samples, int64_t n_features, double alpha,
        double beta, double step_size, int64_t max_iter, double* trace_x,
        double* trace_time, int thread_id, int64_t iter_freq) {
    int64_t i, j, j_idx, local_counter=0, global_counter;
    double p, grad_i, incr, old_grad, delta;
    std::random_device rd;
    std::mt19937 rng(rd());
    std::uniform_int_distribution<int64_t> uni(0, n_samples-1);
    struct timespec start, finish;
    double elapsed;
    clock_gettime(CLOCK_MONOTONIC, &start);

    while (true) {
      /* take a snapshot of the current vector of iterates in trace_x
         and time in trace_time, in order to plot convergence later on */
      if (local_counter % iter_freq == 0 && thread_id == 0) {
          int64_t c = local_counter / iter_freq;
          for (j=0; j<n_features; j++) {
              trace_x[n_features * c + j] = x[j];
          }
          clock_gettime(CLOCK_MONOTONIC, &finish);
          elapsed = (finish.tv_sec - start.tv_sec);
          elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
          trace_time[c] = (double) elapsed;
          printf(".. iteration %lld, time elapsed %f min ..\n", c, elapsed / 60.);
      }

      i = uni(rng);
      p = 0.;
      for (j=A_indptr[i]; j < A_indptr[i+1]; j++) {
          j_idx = A_indices[j];
          p += x[j_idx] * A_data[j];
      }
      grad_i = partial_gradient(p, b[i]);
      old_grad = memory_gradient[i].load();
      while (!memory_gradient[i].compare_exchange_weak(old_grad, grad_i))
          ;
      incr = grad_i - old_grad;

      // .. update coefficients ..
      for (j=A_indptr[i]; j < A_indptr[i+1]; j++){
          j_idx = A_indices[j];
          delta = incr * A_data[j] + d[j_idx] * (gradient_average[j_idx] + alpha * x[j_idx]);
          x[j_idx] = prox(x[j_idx] - step_size * delta, beta * step_size * d[j_idx]);
          add_atomic(&gradient_average[j_idx], incr * A_data[j] / n_samples);
      }
      local_counter ++;
      if (local_counter >= iter_freq * max_iter){
          if (thread_id == 0) {
              printf("..  done %lld iterations ..\n", local_counter / iter_freq);
          }
          return;
      }
  }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2850, %rsp           # imm = 0x2850
movq 0x50(%rbp), %rax
movl 0x48(%rbp), %eax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movsd %xmm1, -0x40(%rbp)
movsd %xmm2, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
leaq -0x1420(%rbp), %rdi
movq %rdi, -0x2808(%rbp)
callq 0xa6d0
movq -0x2808(%rbp), %rdi
callq 0xa540
movl %eax, -0x27fc(%rbp)
jmp 0xb244
movl -0x27fc(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x27a8(%rbp), %rdi
callq 0xa680
jmp 0xb25c
movq 0x20(%rbp), %rdx
decq %rdx
xorl %eax, %eax
movl %eax, %esi
leaq -0x27c8(%rbp), %rdi
callq 0xa2c0
jmp 0xb275
movl $0x1, %edi
leaq -0x27d8(%rbp), %rsi
callq 0xa7e0
jmp 0xb288
movq -0x68(%rbp), %rax
cqto
idivq 0x50(%rbp)
cmpq $0x0, %rdx
jne 0xb3d1
cmpl $0x0, 0x48(%rbp)
jne 0xb3d1
movq -0x68(%rbp), %rax
cqto
idivq 0x50(%rbp)
movq %rax, -0x27f8(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq 0x28(%rbp), %rax
jge 0xb327
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0xa980
movq 0x38(%rbp), %rax
movq 0x28(%rbp), %rcx
imulq -0x27f8(%rbp), %rcx
addq -0x58(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xb2bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x27b0(%rbp)
movl %eax, -0x27b4(%rbp)
leaq -0x1420(%rbp), %rdi
callq 0xa5d0
jmp 0xb706
movl $0x1, %edi
leaq -0x27e8(%rbp), %rsi
callq 0xa7e0
movq -0x27e8(%rbp), %rax
movq -0x27d8(%rbp), %rcx
subq %rcx, %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x27f0(%rbp)
movq -0x27e0(%rbp), %rax
movq -0x27d0(%rbp), %rcx
subq %rcx, %rax
cvtsi2sd %rax, %xmm1
movsd 0x5c8c(%rip), %xmm0     # 0x11000
divsd %xmm0, %xmm1
movsd -0x27f0(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x27f0(%rbp)
movsd -0x27f0(%rbp), %xmm0
movq 0x40(%rbp), %rax
movq -0x27f8(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x27f8(%rbp), %rsi
movsd -0x27f0(%rbp), %xmm0
movsd 0x5c4d(%rip), %xmm1     # 0x11008
divsd %xmm1, %xmm0
leaq 0x5c66(%rip), %rdi      # 0x1102c
movb $0x1, %al
callq 0xa070
jmp 0xb3cf
jmp 0xb3d1
leaq -0x27c8(%rbp), %rdi
leaq -0x27a8(%rbp), %rsi
callq 0xa600
movq %rax, -0x2810(%rbp)
jmp 0xb3ed
movq -0x2810(%rbp), %rax
movq %rax, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x50(%rbp), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jge 0xb474
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0xa980
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x78(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xb410
movsd -0x78(%rbp), %xmm0
movq 0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0xa2a0
movsd %xmm0, -0x2818(%rbp)
jmp 0xb495
movsd -0x2818(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movl $0x5, %esi
callq 0xa2e0
movsd %xmm0, -0x90(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movsd -0x80(%rbp), %xmm0
leaq -0x90(%rbp), %rsi
movl $0x5, %edx
callq 0xa100
xorb $-0x1, %al
testb $0x1, %al
jne 0xb4f0
jmp 0xb4f2
jmp 0xb4c3
movsd -0x80(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x50(%rbp), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jge 0xb6a6
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x2828(%rbp)
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x2830(%rbp)
movq 0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x2838(%rbp)
movq -0x18(%rbp), %rax
leaq (%rax,%rcx,8), %rdi
callq 0xa980
movsd %xmm0, -0x2840(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x2848(%rbp)
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
leaq (%rax,%rcx,8), %rdi
callq 0xa980
movsd -0x2848(%rbp), %xmm3
movsd -0x2840(%rbp), %xmm4
movsd -0x2838(%rbp), %xmm1
movsd -0x2830(%rbp), %xmm2
movaps %xmm0, %xmm5
movsd -0x2828(%rbp), %xmm0
mulsd %xmm5, %xmm3
addsd %xmm4, %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
leaq (%rax,%rcx,8), %rdi
callq 0xa980
movsd -0x48(%rbp), %xmm2
movsd -0x98(%rbp), %xmm3
movaps %xmm2, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
movsd -0x40(%rbp), %xmm1
mulsd %xmm2, %xmm1
movq 0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm1
callq 0xa530
movsd %xmm0, -0x2820(%rbp)
jmp 0xb644
movsd -0x2820(%rbp), %xmm0
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
leaq (%rax,%rcx,8), %rdi
callq 0xaa60
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
leaq (%rax,%rcx,8), %rdi
movsd -0x88(%rbp), %xmm0
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdq 0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0xa4f0
jmp 0xb693
jmp 0xb695
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xb517
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq 0x50(%rbp), %rcx
imulq 0x30(%rbp), %rcx
cmpq %rcx, %rax
jl 0xb701
cmpl $0x0, 0x48(%rbp)
jne 0xb6ec
movq -0x68(%rbp), %rax
movq 0x50(%rbp), %rcx
cqto
idivq %rcx
movq %rax, %rsi
leaq 0x5976(%rip), %rdi      # 0x11057
xorl %eax, %eax
callq 0xa070
jmp 0xb6ea
jmp 0xb6ec
leaq -0x1420(%rbp), %rdi
callq 0xa5d0
addq $0x2850, %rsp           # imm = 0x2850
popq %rbp
retq
jmp 0xb286
movq -0x27b0(%rbp), %rdi
callq 0xaf00
nopw %cs:(%rax,%rax)