// File: /workspace/github_cpp_makefile_star2_O3/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 $0xf8, %rsp
movq %rdi, -0x38(%rbp)
movq 0x40(%rbp), %r11
movq 0x38(%rbp), %r10
movq 0x30(%rbp), %r12
movq 0x28(%rbp), %rbx
movq 0x10(%rbp), %r14
movq %r14, %rax
shrq $0x3d, %rax
xorl %edi, %edi
negq %rax
movq 0x18(%rbp), %r13
movq %rsi, -0xc8(%rbp)
movq %rsi, -0x120(%rbp)
movq %rdx, -0x118(%rbp)
movq %rcx, -0x110(%rbp)
movq %r8, -0x108(%rbp)
movq %r9, -0x100(%rbp)
movsd %xmm0, -0xd0(%rbp)
movsd %xmm0, -0xf8(%rbp)
movsd %xmm1, -0xd8(%rbp)
movsd %xmm1, -0xf0(%rbp)
movsd %xmm2, -0xe0(%rbp)
movsd %xmm2, -0xe8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
sbbq %rdi, %rdi
leaq (,%r14,8), %r15
orq %r15, %rdi
cmpq $0x1, 0x20(%rbp)
jne 0x2ced
movq %rdx, -0x88(%rbp)
movq %r11, -0x90(%rbp)
movq %r10, -0x98(%rbp)
movq %r12, -0xa0(%rbp)
movq %rbx, -0xa8(%rbp)
movq %rcx, -0xb0(%rbp)
movq %r9, -0xb8(%rbp)
movq %r8, -0xc0(%rbp)
callq 0x2030
movq %rax, -0x40(%rbp)
leaq (,%r13,8), %rbx
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r13
movq $-0x1, %rdi
cmovbeq %rbx, %rdi
callq 0x2030
movq %rax, %r12
testq %r14, %r14
jle 0x2c6d
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2110
testq %r13, %r13
jle 0x2c7f
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2110
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %r12, %rdx
movq -0xc8(%rbp), %rcx
movq -0x88(%rbp), %r8
movq -0xb0(%rbp), %r9
movsd -0xd0(%rbp), %xmm0
movsd -0xd8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm2
pushq -0x90(%rbp)
pushq -0x98(%rbp)
pushq -0xa0(%rbp)
pushq -0xa8(%rbp)
pushq %r13
pushq %r14
pushq -0xb8(%rbp)
pushq -0xc0(%rbp)
callq 0x2160
addq $0x40, %rsp
jmp 0x2e9d
callq 0x2030
movq %rax, -0x60(%rbp)
testq %r14, %r14
jle 0x2d0b
xorl %ecx, %ecx
xorl %edx, %edx
xchgq %rdx, (%rax,%rcx,8)
incq %rcx
cmpq %r14, %rcx
jl 0x2cfd
leaq (,%r13,8), %rax
movabsq $0x1fffffffffffffff, %rcx # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rcx, %r13
movq $-0x1, %rbx
cmovbeq %rax, %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, -0x58(%rbp)
testq %r13, %r13
jle 0x2d4c
xorl %ecx, %ecx
xorl %edx, %edx
xchgq %rdx, (%rax,%rcx,8)
incq %rcx
cmpq %r13, %rcx
jl 0x2d3e
movq %rbx, %rdi
callq 0x2030
movq -0x38(%rbp), %r15
movq %rax, -0x50(%rbp)
testq %r13, %r13
jle 0x2d73
xorl %ecx, %ecx
movq (%r15,%rcx,8), %rdx
xchgq %rdx, (%rax,%rcx,8)
incq %rcx
cmpq %r13, %rcx
jl 0x2d63
movl $0x0, -0x2c(%rbp)
cmpq $0x0, 0x20(%rbp)
jle 0x2e63
leaq -0x48(%rbp), %r15
leaq -0x60(%rbp), %r13
leaq -0x58(%rbp), %rbx
leaq -0x120(%rbp), %r14
leaq -0x80(%rbp), %r12
movq %r15, %rdi
movq 0x3222(%rip), %rsi      # 0x5fc8
leaq -0x50(%rbp), %rdx
movq %r13, %rcx
movq %rbx, %r8
movq %r14, %r9
leaq 0x40(%rbp), %rax
pushq %rax
leaq -0x2c(%rbp), %rax
pushq %rax
leaq 0x38(%rbp), %rax
pushq %rax
leaq 0x30(%rbp), %rax
pushq %rax
leaq 0x28(%rbp), %rax
pushq %rax
leaq -0xe8(%rbp), %rax
pushq %rax
leaq -0xf0(%rbp), %rax
pushq %rax
leaq -0xf8(%rbp), %rax
pushq %rax
leaq 0x18(%rbp), %rax
pushq %rax
leaq 0x10(%rbp), %rax
pushq %rax
leaq -0x100(%rbp), %rax
pushq %rax
leaq -0x108(%rbp), %rax
pushq %rax
leaq -0x110(%rbp), %rax
pushq %rax
leaq -0x118(%rbp), %rax
pushq %rax
callq 0x20a0
addq $0x70, %rsp
movq %r12, %rdi
movq %r15, %rsi
callq 0x20c0
cmpq $0x0, -0x48(%rbp)
jne 0x2eba
movslq -0x2c(%rbp), %rax
incq %rax
movl %eax, -0x2c(%rbp)
cmpq 0x20(%rbp), %rax
jl 0x2d9c
movq -0x80(%rbp), %rbx
movq -0x78(%rbp), %r14
cmpq %r14, %rbx
movq -0x38(%rbp), %r15
je 0x2e63
movq %rbx, %rdi
callq 0x2270
addq $0x8, %rbx
cmpq %r14, %rbx
jne 0x2e52
cmpq $0x0, 0x18(%rbp)
jle 0x2e81
xorl %eax, %eax
movq -0x50(%rbp), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, (%r15,%rax,8)
incq %rax
cmpq %rax, 0x18(%rbp)
jg 0x2e6c
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2e8f
callq 0x2210
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x2e9d
callq 0x2210
leaq -0x80(%rbp), %rdi
callq 0x2060
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2070
jmp 0x2ece
jmp 0x2ece
jmp 0x2ece
jmp 0x2ece
cmpq $0x0, -0x48(%rbp)
jne 0x2ee2
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x2280
callq 0x2070