// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movq 0x40(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x38(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x30(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
movq 0x18(%rbp), %r12
movq 0x10(%rbp), %r13
andq $0x0, -0x50(%rbp)
movq %rdi, %rbx
movq %r13, %rax
shrq $0x3d, %rax
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
leaq (,%r13,8), %rax
orq %rax, %rdi
movq %rsi, -0x100(%rbp)
movq %rdx, -0x68(%rbp)
movq %rdx, -0xf8(%rbp)
movq %rcx, -0x90(%rbp)
movq %rcx, -0xf0(%rbp)
movq %r8, -0xb8(%rbp)
movq %r8, -0xe8(%rbp)
movq %r9, -0xb0(%rbp)
movq %r9, -0xe0(%rbp)
movsd %xmm0, -0x98(%rbp)
movsd %xmm0, -0xd8(%rbp)
movsd %xmm1, -0xa0(%rbp)
movsd %xmm1, -0xd0(%rbp)
movsd %xmm2, -0xa8(%rbp)
movsd %xmm2, -0xc8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
cmpq $0x1, 0x20(%rbp)
jne 0x3ca1
movq %rsi, %r14
callq 0x3030
movq %rax, %r15
leaq (,%r12,8), %rdi
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r12
pushq $-0x1
popq %rax
cmovaq %rax, %rdi
callq 0x3030
xorl %ecx, %ecx
testq %r13, %r13
movl $0x0, %edx
cmovgq %r13, %rdx
cmpq %rcx, %rdx
je 0x3cf6
andq $0x0, (%r15,%rcx,8)
incq %rcx
jmp 0x3c92
callq 0x3030
movq %rax, -0x48(%rbp)
xorl %ecx, %ecx
cmpq %r13, %rcx
jge 0x3cbc
xorl %edx, %edx
xchgq %rdx, (%rax,%rcx,8)
incq %rcx
jmp 0x3cac
leaq (,%r12,8), %r14
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r12
pushq $-0x1
popq %rax
cmovaq %rax, %r14
movq %r14, %rdi
callq 0x3030
movq %rax, -0x40(%rbp)
xorl %ecx, %ecx
cmpq %r12, %rcx
jge 0x3d13
xorl %edx, %edx
xchgq %rdx, (%rax,%rcx,8)
incq %rcx
jmp 0x3ce6
xorl %ecx, %ecx
testq %r12, %r12
movl $0x0, %edx
cmovgq %r12, %rdx
cmpq %rcx, %rdx
je 0x3d33
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x3d04
movq %r14, %rdi
callq 0x3030
movq %rax, -0x38(%rbp)
xorl %ecx, %ecx
cmpq %r12, %rcx
jge 0x3d8f
movq (%rbx,%rcx,8), %rdx
xchgq %rdx, (%rax,%rcx,8)
incq %rcx
jmp 0x3d21
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
movq %r14, %rcx
movq -0x68(%rbp), %r8
movq -0x90(%rbp), %r9
movsd -0x98(%rbp), %xmm0
movsd -0xa0(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm2
pushq -0x70(%rbp)
pushq -0x78(%rbp)
pushq -0x80(%rbp)
pushq -0x88(%rbp)
pushq %r12
pushq %r13
pushq -0xb0(%rbp)
pushq -0xb8(%rbp)
callq 0x3230
addq $0x40, %rsp
jmp 0x3e9f
xorl %eax, %eax
leaq -0xc0(%rbp), %r15
leaq -0x40(%rbp), %r14
leaq -0x100(%rbp), %r12
leaq -0x60(%rbp), %r13
movl %eax, -0x2c(%rbp)
cltq
cmpq 0x20(%rbp), %rax
jge 0x3e4f
movq %r15, %rdi
movq 0x3208(%rip), %rsi      # 0x6fc8
leaq -0x38(%rbp), %rdx
leaq -0x48(%rbp), %rcx
movq %r14, %r8
movq %r12, %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 -0xc8(%rbp), %rax
pushq %rax
leaq -0xd0(%rbp), %rax
pushq %rax
leaq -0xd8(%rbp), %rax
pushq %rax
leaq 0x18(%rbp), %rax
pushq %rax
leaq 0x10(%rbp), %rax
pushq %rax
leaq -0xe0(%rbp), %rax
pushq %rax
leaq -0xe8(%rbp), %rax
pushq %rax
leaq -0xf0(%rbp), %rax
pushq %rax
leaq -0xf8(%rbp), %rax
pushq %rax
callq 0x30f0
addq $0x70, %rsp
movq %r13, %rdi
movq %r15, %rsi
callq 0x3120
movq %r15, %rdi
callq 0x3390
movl -0x2c(%rbp), %eax
incl %eax
jmp 0x3da7
movq -0x60(%rbp), %r14
movq -0x58(%rbp), %r15
cmpq %r15, %r14
je 0x3e6a
movq %r14, %rdi
callq 0x33d0
addq $0x8, %r14
jmp 0x3e57
xorl %eax, %eax
cmpq %rax, 0x18(%rbp)
jle 0x3e83
movq -0x38(%rbp), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, (%rbx,%rax,8)
incq %rax
jmp 0x3e6c
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x3e91
callq 0x3320
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x3e9f
callq 0x3320
leaq -0x60(%rbp), %rdi
callq 0x3070
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3ed3
jmp 0x3ed3
jmp 0x3ed3
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x3390
jmp 0x3ed6
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x3070
movq %rbx, %rdi
callq 0x33e0