// File: fabianp[P]ProxASAGA/proxasaga_atomic.cpp
// Function: prox_asaga(double*, double*, long*, long*, double*, double*, long, long, long, double, double, double, long, double*, double*, long)
int prox_asaga(
        double* x, double* A_data, int64_t* A_indices, int64_t* A_indptr,
        double* b, double* d, int64_t n_samples, int64_t n_features,
        int64_t n_threads, double alpha, double beta, double step_size,
        int64_t max_iter, double* trace_x, double* trace_time, int64_t iter_freq) {

    std::vector<std::thread> threads;
    std::atomic<double>* memory_gradient;
    std::atomic<double>* gradient_average;
    double* memory_gradient_nonatomic;
    double* gradient_average_nonatomic;

    if (n_threads == 1) {
      memory_gradient_nonatomic = new double[n_samples];
      gradient_average_nonatomic = new double[n_features];
      for(int i=0; i<n_samples; i++){
        memory_gradient_nonatomic[i] = 0;
      }
      for(int j=0; j<n_features; j++){
        gradient_average_nonatomic[j] = 0;
      }
      saga_single_thread_nonatomic(x, memory_gradient_nonatomic,
        gradient_average_nonatomic, A_data, A_indices, A_indptr,
        b, d, n_samples, n_features, alpha, beta, step_size, max_iter,
        trace_x, trace_time, iter_freq);
    } else {
      memory_gradient = new std::atomic<double>[n_samples];
      for(int i=0; i<n_samples; i++){
        memory_gradient[i] = 0;
      }
      gradient_average = new std::atomic<double>[n_features];
      for(int j=0; j<n_features; j++){
        gradient_average[j] = 0;
      }

      std::atomic<double>* x_atomic = new std::atomic<double>[n_features];
      for(int j=0; j<n_features; j++){
        x_atomic[j] = x[j];
      }

      for (int i = 0; i < n_threads; ++i) {
          threads.push_back(std::thread(saga_single_thread, x_atomic, memory_gradient, gradient_average, A_data,
              A_indices, A_indptr, b, d, n_samples, n_features, alpha, beta, step_size, max_iter,
              trace_x, trace_time, i, iter_freq));
      }

      for(auto &t : threads){
          t.join();
      }
      for(int j=0; j<n_features; j++){
        x[j] = x_atomic[j];
      }
      delete[] memory_gradient;
      delete[] gradient_average;

    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
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, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movsd %xmm1, -0x68(%rbp)
movsd %xmm2, -0x70(%rbp)
leaq -0x88(%rbp), %rdi
callq 0xa950
cmpq $0x1, 0x20(%rbp)
jne 0xbdbd
movq 0x10(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xa030
movq %rax, -0x110(%rbp)
jmp 0xbc56
movq -0x110(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x18(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xa030
movq %rax, -0x118(%rbp)
jmp 0xbc8c
movq -0x118(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl $0x0, -0xb8(%rbp)
movslq -0xb8(%rbp), %rax
cmpq 0x10(%rbp), %rax
jge 0xbcef
movq -0xa0(%rbp), %rax
movslq -0xb8(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0xbca4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0xc18a
movl $0x0, -0xbc(%rbp)
movslq -0xbc(%rbp), %rax
cmpq 0x18(%rbp), %rax
jge 0xbd2d
movq -0xa8(%rbp), %rax
movslq -0xbc(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0xbcf9
movq -0x30(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq -0x38(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0x50(%rbp), %r10
movq -0x58(%rbp), %r11
movq 0x10(%rbp), %rbx
movq 0x18(%rbp), %r14
movsd -0x60(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
movq 0x28(%rbp), %r15
movq 0x30(%rbp), %r12
movq 0x38(%rbp), %r13
movq 0x40(%rbp), %rcx
movq %rsp, %rax
movq %rax, -0x128(%rbp)
movq %rcx, 0x38(%rax)
movq -0x120(%rbp), %rcx
movq %r13, 0x30(%rax)
movq %r12, 0x28(%rax)
movq %r15, 0x20(%rax)
movq %r14, 0x18(%rax)
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0xa700
jmp 0xbdb8
jmp 0xc16a
movq 0x10(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xa030
movq %rax, -0x130(%rbp)
jmp 0xbde5
movq -0x130(%rbp), %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0xc0(%rbp)
movslq -0xc0(%rbp), %rax
cmpq 0x10(%rbp), %rax
jge 0xbe38
movq -0x90(%rbp), %rdi
movslq -0xc0(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
xorps %xmm0, %xmm0
callq 0xaa60
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0xbdfd
movq 0x18(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xa030
movq %rax, -0x138(%rbp)
jmp 0xbe60
movq -0x138(%rbp), %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0xc4(%rbp)
movslq -0xc4(%rbp), %rax
cmpq 0x18(%rbp), %rax
jge 0xbeb3
movq -0x98(%rbp), %rdi
movslq -0xc4(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
xorps %xmm0, %xmm0
callq 0xaa60
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xbe78
movq 0x18(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xa030
movq %rax, -0x140(%rbp)
jmp 0xbedb
movq -0x140(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl $0x0, -0xd4(%rbp)
movslq -0xd4(%rbp), %rax
cmpq 0x18(%rbp), %rax
jge 0xbf3b
movq -0x30(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0xd0(%rbp), %rdi
movslq -0xd4(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0xaa60
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0xbef3
movl $0x0, -0xd8(%rbp)
movslq -0xd8(%rbp), %rax
cmpq 0x20(%rbp), %rax
jge 0xc058
movq %rsp, %rax
movq %rax, -0x148(%rbp)
leaq 0x40(%rbp), %rcx
movq %rcx, 0x68(%rax)
leaq -0xd8(%rbp), %rcx
movq %rcx, 0x60(%rax)
leaq 0x38(%rbp), %rcx
movq %rcx, 0x58(%rax)
leaq 0x30(%rbp), %rcx
movq %rcx, 0x50(%rax)
leaq 0x28(%rbp), %rcx
movq %rcx, 0x48(%rax)
leaq -0x70(%rbp), %rcx
movq %rcx, 0x40(%rax)
leaq -0x68(%rbp), %rcx
movq %rcx, 0x38(%rax)
leaq -0x60(%rbp), %rcx
movq %rcx, 0x30(%rax)
leaq 0x18(%rbp), %rcx
movq %rcx, 0x28(%rax)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x20(%rax)
leaq -0x58(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq -0x50(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq -0x48(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movq 0x8fe7(%rip), %rsi      # 0x14fc0
leaq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
leaq -0x90(%rbp), %rcx
leaq -0x98(%rbp), %r8
leaq -0x38(%rbp), %r9
callq 0xa2f0
jmp 0xc000
leaq -0x88(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0xabd0
jmp 0xc015
leaq -0xe0(%rbp), %rdi
callq 0xad30
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
jmp 0xbf45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xad30
jmp 0xc18a
leaq -0x88(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0xa490
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
callq 0xa670
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xb080
testb $0x1, %al
jne 0xc0a5
jmp 0xc0d6
leaq -0xf0(%rbp), %rdi
callq 0xaeb0
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
callq 0xae80
jmp 0xc0c6
jmp 0xc0c8
leaq -0xf0(%rbp), %rdi
callq 0xac10
jmp 0xc08c
movl $0x0, -0x104(%rbp)
movslq -0x104(%rbp), %rax
cmpq 0x18(%rbp), %rax
jge 0xc128
movq -0xd0(%rbp), %rdi
movslq -0x104(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0xa980
movq -0x30(%rbp), %rax
movslq -0x104(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0xc0e0
movq -0x90(%rbp), %rax
movq %rax, -0x150(%rbp)
cmpq $0x0, %rax
je 0xc148
movq -0x150(%rbp), %rdi
callq 0xaad0
movq -0x98(%rbp), %rax
movq %rax, -0x158(%rbp)
cmpq $0x0, %rax
je 0xc168
movq -0x158(%rbp), %rdi
callq 0xaad0
jmp 0xc16a
leaq -0x88(%rbp), %rdi
callq 0xa120
xorl %eax, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x88(%rbp), %rdi
callq 0xa120
movq -0xb0(%rbp), %rdi
callq 0xaf00
nopw %cs:(%rax,%rax)