// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/Neuron.cpp
// Function: Neuron::UpdateDeltaFC(int)
void Neuron::UpdateDeltaFC(int sample_index)
{
    // back propagation delta for fully connected layer
    if(__sigmaPrime.size() <= 0) 
    {
        std::cout<<"Error: Neuron::UpdateDeltaFC() computing delta needs sigma^prime computed first."<<std::endl;
        std::cout<<"        "<<__delta.size()<<" deltas, "<<__sigmaPrime.size()<<" sigma^primes"<<std::endl;
        exit(0);
    }
    //cout<<"here..... Neuron::UpdateDeltaFC()"<<endl;

    Layer* __nextLayer = __layer->GetNextLayer();

    auto & __deltaNext = __nextLayer->GetImagesActiveDelta(); // no vectorization needed, b/c "fc->cnn" type connection is not used in backward direction
    //cout<<"delta images batch size: "<<__deltaNext.size()<<endl;
    Images & image_delta_Next = __deltaNext[sample_index]; // get current sample delta
    std::vector<Matrix> &deltaNext = image_delta_Next.OutputImageFromKernel;
    if( deltaNext.size() != 1 ) 
    {
        std::cout<<"Error: Delta matrix dimension not match in FC layer"<<std::endl;
        exit(0);
    }
    Matrix & delta = deltaNext[0];
    //cout<<delta<<endl;

    //cout<<"sample id: "<<sample_index<<endl;

    auto wv = __nextLayer->GetWeightMatrix();
    /* 
    // 1) method 1
    Matrix w = Matrix::ConcatenateMatrixByI(wv); // for large matrix, this line is too time consuming, use method 2 instead

    auto w_dim = w.Dimension();
    if( w_dim.second < __coord.i ) 
    {
        std::cout<<"Error: weight matrix dimension not match in FC layer"<<std::endl;
        std::cout<<"Number of kernels: "<<wv->size()<<std::endl;
        std::cout<<"Neuron coord: "<<__coord<<std::endl;
        exit(0);
    }

    // back propogate delta
    w = w.Transpose();

    //cout<<w<<endl;
    auto dim = w.Dimension();
    w = w.GetSection(__coord.i, __coord.i+1, 0, dim.second);

    Matrix deltaCurrentLayer = w*delta;
    if(deltaCurrentLayer.Dimension().first != 1 || deltaCurrentLayer.Dimension().second != 1) 
    {
        std::cout<<"Error: back propagation delta, matrix dimension not match in FC layer."<<std::endl;
        exit(0);
    }
    */

    // 2) method 2
    auto delta_dim = delta.Dimension();
    assert(delta_dim.second == 1);
    double delta_current = 0;
    for(size_t i=0;i<delta_dim.first;i++)
    {
        delta_current += delta[i][0] * ((*wv)[i])[0][__coord.i];
    }

    // get sigma^\prime for current sample
    double s_prime = __sigmaPrime[sample_index];

    //double v = deltaCurrentLayer[0][0]; // method 1
    double v = delta_current; // method 2
    v = v*s_prime;

    __delta[sample_index] = v;

    //cout<<"here..... Neuron::UpdateDeltaFC()   end"<<endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x68(%rdi), %rax
cmpq 0x60(%rdi), %rax
je 0x1ca83
movl %esi, %ebp
movq 0x78(%rbx), %rdi
movq (%rdi), %rax
callq *0x2c0(%rax)
movq %rax, %r15
movq (%rax), %rax
movq %r15, %rdi
callq *0xf8(%rax)
movslq %ebp, %rdx
movq (%rax), %rax
imulq $0x18, %rdx, %rcx
movq (%rax,%rcx), %r14
movq 0x8(%rax,%rcx), %rax
subq %r14, %rax
cmpq $0x18, %rax
jne 0x1caf3
movq %rdx, (%rsp)
movq (%r15), %rax
movq %r15, %rdi
callq *0x220(%rax)
movq %rax, %r15
movq %r14, %rdi
callq 0x17a6a
cmpq $0x1, %rdx
jne 0x1cb15
movq %rax, %r12
xorpd %xmm0, %xmm0
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x1ca5e
movq %r14, %rdi
movq %r13, %rsi
movsd %xmm0, 0x10(%rsp)
callq 0x17c46
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq (%r15), %rdi
addq %rbp, %rdi
xorl %esi, %esi
callq 0x17c46
movsd 0x10(%rsp), %xmm0
movq 0x88(%rbx), %rcx
movq (%rax), %rax
movsd 0x8(%rsp), %xmm1
mulsd (%rax,%rcx,8), %xmm1
addsd %xmm1, %xmm0
incq %r13
addq $0x18, %rbp
jmp 0x1ca06
movq 0x30(%rbx), %rax
movq 0x60(%rbx), %rcx
movq (%rsp), %rdx
mulsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1654e(%rip), %r14     # 0x32fd8
leaq 0x84ff(%rip), %rsi      # 0x24f90
movq %r14, %rdi
callq 0x4240
movq %rax, %rdi
callq 0x4130
leaq 0x844a(%rip), %rsi      # 0x24ef2
movq %r14, %rdi
callq 0x4240
movq 0x38(%rbx), %rsi
subq 0x30(%rbx), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x4090
leaq 0x8430(%rip), %rsi      # 0x24efb
movq %rax, %rdi
callq 0x4240
movq 0x68(%rbx), %rsi
subq 0x60(%rbx), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x4090
leaq 0x8417(%rip), %rsi      # 0x24f05
movq %rax, %rdi
jmp 0x1cb01
movq 0x164de(%rip), %rdi     # 0x32fd8
leaq 0x84e0(%rip), %rsi      # 0x24fe1
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260
leaq 0x84f9(%rip), %rdi      # 0x25015
leaq 0x7fe3(%rip), %rsi      # 0x24b06
leaq 0x8501(%rip), %rcx      # 0x2502b
movl $0x24e, %edx            # imm = 0x24E
callq 0x4180