// File: /workspace/github_cpp_makefile_star2_O1/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), %r13
movq %r13, %rax
shrq $0x3d, %rax
xorl %edi, %edi
negq %rax
movq 0x18(%rbp), %r14
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 (,%r13,8), %r15
orq %r15, %rdi
cmpq $0x1, 0x20(%rbp)
jne 0x2cf8
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, -0x58(%rbp)
leaq (,%r14,8), %rbx
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r14
movq $-0x1, %rdi
cmovbeq %rbx, %rdi
callq 0x2030
movq %rax, %r12
testq %r13, %r13
jle 0x2c78
movq -0x58(%rbp), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2110
testq %r14, %r14
jle 0x2c8a
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2110
movq -0x38(%rbp), %rdi
movq -0x58(%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 %r14
pushq %r13
pushq -0xb8(%rbp)
pushq -0xc0(%rbp)
callq 0x2160
addq $0x40, %rsp
jmp 0x2edd
callq 0x2030
movq %rax, -0x50(%rbp)
cmpq $0x0, 0x10(%rbp)
jle 0x2d1d
xorl %eax, %eax
movq -0x50(%rbp), %rcx
xorl %edx, %edx
xchgq %rdx, (%rcx,%rax,8)
incq %rax
cmpq %rax, 0x10(%rbp)
jg 0x2d0a
movq 0x18(%rbp), %rax
leaq (,%rax,8), %rcx
movabsq $0x1fffffffffffffff, %rdx # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rdx, %rax
movq $-0x1, %rdi
cmovbeq %rcx, %rdi
callq 0x2030
movq %rax, -0x48(%rbp)
movq 0x18(%rbp), %rcx
testq %rcx, %rcx
jle 0x2d6b
xorl %eax, %eax
movq -0x48(%rbp), %rcx
xorl %edx, %edx
xchgq %rdx, (%rcx,%rax,8)
incq %rax
movq 0x18(%rbp), %rcx
cmpq %rax, %rcx
jg 0x2d55
leaq (,%rcx,8), %rax
movabsq $0x1fffffffffffffff, %rdx # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rdx, %rcx
movq $-0x1, %rdi
cmovbeq %rax, %rdi
callq 0x2030
movq %rax, -0x40(%rbp)
cmpq $0x0, 0x18(%rbp)
movq -0x38(%rbp), %rsi
jle 0x2db6
xorl %eax, %eax
movq (%rsi,%rax,8), %rcx
movq -0x40(%rbp), %rdx
xchgq %rcx, (%rdx,%rax,8)
incq %rax
cmpq %rax, 0x18(%rbp)
jg 0x2da1
movl $0x0, -0x2c(%rbp)
cmpq $0x0, 0x20(%rbp)
jle 0x2e84
leaq -0x60(%rbp), %r15
leaq -0x50(%rbp), %r13
leaq -0x48(%rbp), %rbx
leaq -0x120(%rbp), %r14
leaq -0x80(%rbp), %r12
movq %r15, %rdi
movq 0x31df(%rip), %rsi      # 0x5fc8
leaq -0x40(%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, -0x60(%rbp)
jne 0x2efa
movslq -0x2c(%rbp), %rax
incq %rax
movl %eax, -0x2c(%rbp)
cmpq 0x20(%rbp), %rax
jl 0x2ddf
movq -0x80(%rbp), %rbx
movq -0x78(%rbp), %r14
cmpq %r14, %rbx
je 0x2e9f
movq %rbx, %rdi
callq 0x2270
addq $0x8, %rbx
jmp 0x2e8c
cmpq $0x0, 0x18(%rbp)
movq -0x38(%rbp), %rdx
jle 0x2ec1
xorl %eax, %eax
movq -0x40(%rbp), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, (%rdx,%rax,8)
incq %rax
cmpq %rax, 0x18(%rbp)
jg 0x2eac
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x2ecf
callq 0x2210
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2edd
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 0x2f11
jmp 0x2f11
jmp 0x2f11
cmpq $0x0, -0x60(%rbp)
je 0x2f11
callq 0x2070
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x2280