// File: /workspace/github_cpp_makefile_star2_O1/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 0x21297
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, %r13
movq (%rax), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq (%rax,%rcx,8), %r14
movq 0x8(%rax,%rcx,8), %rax
subq %r14, %rax
cmpq $0x18, %rax
jne 0x21307
movq (%r15), %rax
movq %r15, %rdi
callq *0x220(%rax)
movq %rax, %r15
movq %r14, %rdi
callq 0x1ac7c
cmpq $0x1, %rdx
jne 0x21329
movq %rax, %r12
movq %r13, (%rsp)
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x21272
xorl %ebp, %ebp
xorl %r13d, %r13d
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x1ae76
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq (%r15), %rdi
addq %rbp, %rdi
xorl %esi, %esi
callq 0x1ae76
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
cmpq %r13, %r12
jne 0x2121c
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 0x15d3a(%rip), %r14     # 0x36fd8
leaq 0x9d7b(%rip), %rsi      # 0x2b020
movq %r14, %rdi
callq 0x4260
movq %rax, %rdi
callq 0x4100
leaq 0x9cc6(%rip), %rsi      # 0x2af82
movq %r14, %rdi
callq 0x4260
movq 0x38(%rbx), %rsi
subq 0x30(%rbx), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x4090
leaq 0x9cac(%rip), %rsi      # 0x2af8b
movq %rax, %rdi
callq 0x4260
movq 0x68(%rbx), %rsi
subq 0x60(%rbx), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x4090
leaq 0x9c93(%rip), %rsi      # 0x2af95
movq %rax, %rdi
jmp 0x21315
movq 0x15cca(%rip), %rdi     # 0x36fd8
leaq 0x9d5c(%rip), %rsi      # 0x2b071
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290
leaq 0x9d75(%rip), %rdi      # 0x2b0a5
leaq 0x985f(%rip), %rsi      # 0x2ab96
leaq 0x9d7d(%rip), %rcx      # 0x2b0bb
movl $0x24e, %edx            # imm = 0x24E
callq 0x4160