// File: /workspace/github_cpp_makefile_star2_O3/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::UpdateImagesDelta(int)
void ConstructLayer::UpdateImagesDelta(int sample_index)
{
    // __imageDelta shold only store images from all active neurons, the matching info can be achieved from filter matrix
    //    drop out only happens on batch level
    //    so imageDelta will clear after each batch is done
    //    **** on batch level, the filter matrix stays the same, so no need to worry the change of filter matrix on batch level
    //
    //    The above comment is copied from UpdateImagesZ(); just to refresh memory, no use in this function
    //

    // extract the Delta matrices from neurons for current traning sample
    Images sample_image_delta;
    Images sample_image_delta_full;

    // !!! Note:: !!! delta for output layer needs different method, and is done ComputeCostInOutputLayerForCurrentSample(int) function
    //                no need to do it here
    if(__type == LayerType::fullyConnected) // for fully connected layer, 
    {
        for(size_t k=0;k<__neuronDim.k;k++) // kernel
        {
            Matrix Delta( __neuronDim.i, __neuronDim.j, 0);
            //cout<<"sample index: "<<sample_index<<endl;
            //cout<<"before filling delta"<<endl<<Delta<<endl;
            for(size_t i=0;i<__neuronDim.i;i++){
                for(size_t j=0;j<__neuronDim.j;j++)
                {
                    auto & delta_vector = __neurons[k][i][j]->GetDeltaVector();
                    if(__neurons[k][i][j]->IsActive())
                    {  // make sure no over extract
                        //assert(delta_vector.size() - 1 == l); // obsolete
                        //Delta[i][j] = delta_vector.back();    // obsolete
                        Delta[i][j] = delta_vector[sample_index];    // obsolete
                    }
                    else
                    {
                        // if neuron is inactive, set it to 0
                        Delta[i][j] = 0;
                    }
                }
            }
            // save full delta image
            sample_image_delta_full.OutputImageFromKernel.push_back(Delta);
            //cout<<"full image delta: "<<endl<<Delta<<endl;
            //getchar();
            // only save active elements
            Matrix R = filterMatrix(Delta, __activeFlag[k]);
            sample_image_delta.OutputImageFromKernel.push_back(R);

            assert(R.Dimension().first  == __activeNeuronDim.i);
            assert(R.Dimension().second == __activeNeuronDim.j);
        }
        //__imageDelta.push_back(sample_image_delta);
        __imageDelta[sample_index] = sample_image_delta;
        __imageDeltaFull[sample_index] = sample_image_delta_full;
    }
    else if(__type == LayerType::cnn || __type == LayerType::pooling) // for cnn layer and pooling layer
    {
        // for cnn, drop out happens on kernels (weight matrix)
        // so the neurons are all active
        for(size_t k=0;k<__neuronDim.k;k++) // kernel
        {
            Matrix Delta( __neuronDim.i, __neuronDim.j);
            for(size_t i=0;i<__neuronDim.i;i++){
                for(size_t j=0;j<__neuronDim.j;j++)
                {
                    auto & delta_vector = __neurons[k][i][j]->GetDeltaVector();
                    if(__neurons[k][i][j]->IsActive())
                    {  // make sure no over extract
                        //assert(delta_vector.size() - 1 == l);
                        //Delta[i][j] = delta_vector.back();
                        Delta[i][j] = delta_vector[sample_index];
                    }
                    else
                    {
                        Delta[i][j] = 0;
                    }
                }
            }
            sample_image_delta.OutputImageFromKernel.push_back(Delta); // no need to filter
            sample_image_delta_full.OutputImageFromKernel.push_back(Delta); 
        }
        //__imageDelta.push_back(sample_image_delta); // obsolete
        __imageDelta[sample_index] = sample_image_delta;
        __imageDeltaFull[sample_index] = sample_image_delta_full;
    }
    else // reserved for other layer types
    {
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movq %rax, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl 0xc(%rdi), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x8620
cmpq $0x0, 0x3a8(%rbx)
je 0x87ca
movslq %esi, %rax
movq %rax, 0x8(%rsp)
movq $0x0, (%rsp)
leaq 0x18(%rsp), %r14
movq 0x398(%rbx), %rsi
movq 0x3a0(%rbx), %rdx
movq %r14, %rdi
callq 0x1a76e
movq 0x398(%rbx), %rax
testq %rax, %rax
je 0x85d7
movq 0x3a0(%rbx), %rcx
movq (%rsp), %rdx
leaq (%rdx,%rdx,2), %r15
xorl %r13d, %r13d
testq %rcx, %rcx
je 0x85c9
leaq (,%r13,2), %r12
addq %r13, %r12
xorl %r14d, %r14d
movq 0x40(%rbx), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x21dbc
movq %rax, %rbp
movq 0x40(%rbx), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x2089a
testb %al, %al
je 0x8594
movq (%rbp), %rax
movq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x1adb2
movsd 0x10(%rsp), %xmm0
jmp 0x85a4
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x1adb2
xorps %xmm0, %xmm0
movq (%rax), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
movq 0x3a0(%rbx), %rcx
cmpq %rcx, %r14
jb 0x853a
movq 0x398(%rbx), %rax
jmp 0x85cb
xorl %ecx, %ecx
incq %r13
cmpq %rax, %r13
jb 0x8523
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x138a4
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x138a4
movq %r14, %rdi
callq 0x1abf2
movq (%rsp), %rcx
incq %rcx
movq %rcx, (%rsp)
cmpq 0x3a8(%rbx), %rcx
jb 0x84eb
movq 0x8(%rsp), %rax
jmp 0x87cd
testl %eax, %eax
jne 0x8823
cmpq $0x0, 0x3a8(%rbx)
je 0x87eb
movslq %esi, %rax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0x18(%rsp), %r15
movq %r14, (%rsp)
movq 0x398(%rbx), %rsi
movq 0x3a0(%rbx), %rdx
xorps %xmm0, %xmm0
movq %r15, %rdi
callq 0x1a892
movq 0x398(%rbx), %rax
testq %rax, %rax
je 0x8737
movq 0x3a0(%rbx), %rcx
movq (%rsp), %rdx
leaq (%rdx,%rdx,2), %r13
xorl %ebp, %ebp
testq %rcx, %rcx
je 0x8729
leaq (,%rbp,2), %r12
addq %rbp, %r12
xorl %r14d, %r14d
movq 0x40(%rbx), %rax
movq (%rax,%r13,8), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x21dbc
movq %rax, %r15
movq 0x40(%rbx), %rax
movq (%rax,%r13,8), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x2089a
testb %al, %al
je 0x86f4
movq (%r15), %rax
movq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x1adb2
movsd 0x10(%rsp), %xmm0
jmp 0x8704
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x1adb2
xorps %xmm0, %xmm0
movq (%rax), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
movq 0x3a0(%rbx), %rcx
cmpq %rcx, %r14
jb 0x869b
movq 0x398(%rbx), %rax
jmp 0x872b
xorl %ecx, %ecx
incq %rbp
cmpq %rax, %rbp
jb 0x8684
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x138a4
movq (%rsp), %r14
leaq (%r14,%r14,2), %rdx
shlq $0x3, %rdx
addq 0x108(%rbx), %rdx
leaq 0x70(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0xb0f5
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x138a4
movq %r12, %rdi
callq 0x1abb8
cmpq 0x3b0(%rbx), %rax
jne 0x8849
movq %r12, %rdi
callq 0x1abb8
cmpq 0x3b8(%rbx), %rdx
jne 0x8868
movq %r12, %rdi
callq 0x1abf2
movq %r15, %rdi
callq 0x1abf2
incq %r14
cmpq 0x3a8(%rbx), %r14
jb 0x8646
movq 0x8(%rsp), %rax
jmp 0x87ee
movslq %esi, %rax
leaq (%rax,%rax,2), %rdi
shlq $0x3, %rdi
addq 0x180(%rbx), %rdi
leaq 0x30(%rsp), %rsi
movq %rax, %r14
callq 0x14112
jmp 0x880a
movslq %esi, %rax
leaq (%rax,%rax,2), %rdi
shlq $0x3, %rdi
addq 0x180(%rbx), %rdi
leaq 0x30(%rsp), %rsi
movq %rax, %r14
callq 0x14112
leaq (%r14,%r14,2), %rdi
shlq $0x3, %rdi
addq 0x1e0(%rbx), %rdi
leaq 0x50(%rsp), %rsi
callq 0x14112
leaq 0x50(%rsp), %rdi
callq 0x1353a
leaq 0x30(%rsp), %rdi
callq 0x1353a
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f4e7(%rip), %rdi     # 0x27d37
leaq 0x1eef3(%rip), %rsi     # 0x2774a
leaq 0x1fa60(%rip), %rcx     # 0x282be
movl $0x9d6, %edx            # imm = 0x9D6
callq 0x4160
leaq 0x1f51b(%rip), %rdi     # 0x27d8a
leaq 0x1eed4(%rip), %rsi     # 0x2774a
leaq 0x1fa41(%rip), %rcx     # 0x282be
movl $0x9d7, %edx            # imm = 0x9D7
callq 0x4160
jmp 0x88a2
jmp 0x888d
jmp 0x888d
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x1abf2
jmp 0x88ae
jmp 0x88ab
jmp 0x88ab
jmp 0x88a2
movq %rax, %rbx
jmp 0x88b8
jmp 0x88ab
jmp 0x88ab
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x1abf2
leaq 0x50(%rsp), %rdi
callq 0x1353a
leaq 0x30(%rsp), %rdi
callq 0x1353a
movq %rbx, %rdi
callq 0x43e0