// File: 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:
subq $0x198, %rsp            # imm = 0x198
movq %rdi, 0x190(%rsp)
movl %esi, 0x18c(%rsp)
movq 0x190(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x1fda0
leaq 0x158(%rsp), %rdi
callq 0x1fda0
jmp 0xb7d2
movq 0x98(%rsp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xbc70
movq $0x0, 0x140(%rsp)
movq 0x98(%rsp), %rcx
movq 0x140(%rsp), %rax
cmpq 0x3a8(%rcx), %rax
jae 0xbc0f
movq 0x98(%rsp), %rax
movq 0x398(%rax), %rsi
movq 0x3a0(%rax), %rdx
leaq 0x128(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x3b590
jmp 0xb835
movq $0x0, 0x120(%rsp)
movq 0x98(%rsp), %rcx
movq 0x120(%rsp), %rax
cmpq 0x398(%rcx), %rax
jae 0xba66
movq $0x0, 0x118(%rsp)
movq 0x98(%rsp), %rcx
movq 0x118(%rsp), %rax
cmpq 0x3a0(%rcx), %rax
jae 0xba4b
movq 0x98(%rsp), %rdi
addq $0x40, %rdi
movq 0x140(%rsp), %rsi
callq 0x1f650
movq %rax, %rdi
movq 0x120(%rsp), %rsi
callq 0x1f5c0
movq %rax, 0x90(%rsp)
jmp 0xb8ba
movq 0x90(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x4a290
movq %rax, 0x88(%rsp)
jmp 0xb8e1
movq 0x98(%rsp), %rdi
movq 0x88(%rsp), %rax
movq %rax, 0x110(%rsp)
addq $0x40, %rdi
movq 0x140(%rsp), %rsi
callq 0x1f650
movq %rax, %rdi
movq 0x120(%rsp), %rsi
callq 0x1f5c0
movq %rax, 0x80(%rsp)
jmp 0xb924
movq 0x80(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
movb %al, 0x7f(%rsp)
jmp 0xb947
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0xb954
jmp 0xb9fb
movq 0x110(%rsp), %rdi
movslq 0x18c(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x120(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x3be50
movq %rax, 0x70(%rsp)
jmp 0xb98f
movq 0x70(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1fd60
movsd 0x68(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0xba30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xbfb7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xbfaa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xbbfd
movq 0x120(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x3be50
movq %rax, 0x60(%rsp)
jmp 0xba17
movq 0x60(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1fd60
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0xba32
movq 0x118(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x118(%rsp)
jmp 0xb86a
jmp 0xba4d
movq 0x120(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x120(%rsp)
jmp 0xb841
leaq 0x158(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x1fa50
jmp 0xba7d
movq 0x98(%rsp), %rdi
addq $0x108, %rdi            # imm = 0x108
movq 0x140(%rsp), %rsi
callq 0x1ff80
movq %rax, %rdx
leaq 0xf8(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x10950
jmp 0xbab3
leaq 0x170(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1fa50
jmp 0xbaca
leaq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
jmp 0xbae3
movq 0x98(%rsp), %rcx
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rdx
movq %rdx, 0xe8(%rsp)
movq %rax, 0xf0(%rsp)
movq 0xe8(%rsp), %rax
cmpq 0x3b0(%rcx), %rax
jne 0xbb18
jmp 0xbb5d
leaq 0x46cdf(%rip), %rdi     # 0x527fe
leaq 0x466ac(%rip), %rsi     # 0x521d2
movl $0x9d6, %edx            # imm = 0x9D6
leaq 0x47253(%rip), %rcx     # 0x52d85
callq 0x4170
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3bb80
jmp 0xbbfd
leaq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
jmp 0xbb76
movq 0x98(%rsp), %rcx
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rdx
movq %rdx, 0xd8(%rsp)
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
cmpq 0x3b8(%rcx), %rax
jne 0xbbab
jmp 0xbbca
leaq 0x46c9f(%rip), %rdi     # 0x52851
leaq 0x46619(%rip), %rsi     # 0x521d2
movl $0x9d7, %edx            # imm = 0x9D7
leaq 0x471c0(%rip), %rcx     # 0x52d85
callq 0x4170
leaq 0xf8(%rsp), %rdi
callq 0x3bb80
leaq 0x128(%rsp), %rdi
callq 0x3bb80
movq 0x140(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x140(%rsp)
jmp 0xb7f0
leaq 0x128(%rsp), %rdi
callq 0x3bb80
jmp 0xbfaa
movq 0x98(%rsp), %rdi
addq $0x180, %rdi            # imm = 0x180
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xbc3d
movq 0x98(%rsp), %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x158(%rsp), %rsi
callq 0x1fdb0
jmp 0xbc6b
jmp 0xbf88
movq 0x98(%rsp), %rax
cmpl $0x1, 0xc(%rax)
je 0xbc90
movq 0x98(%rsp), %rax
cmpl $0x2, 0xc(%rax)
jne 0xbf84
movq $0x0, 0xd0(%rsp)
movq 0x98(%rsp), %rcx
movq 0xd0(%rsp), %rax
cmpq 0x3a8(%rcx), %rax
jae 0xbf26
movq 0x98(%rsp), %rax
movq 0x398(%rax), %rsi
movq 0x3a0(%rax), %rdx
leaq 0xb8(%rsp), %rdi
callq 0x3b3b0
jmp 0xbcde
movq $0x0, 0xb0(%rsp)
movq 0x98(%rsp), %rcx
movq 0xb0(%rsp), %rax
cmpq 0x398(%rcx), %rax
jae 0xbed2
movq $0x0, 0xa8(%rsp)
movq 0x98(%rsp), %rcx
movq 0xa8(%rsp), %rax
cmpq 0x3a0(%rcx), %rax
jae 0xbeb7
movq 0x98(%rsp), %rdi
addq $0x40, %rdi
movq 0xd0(%rsp), %rsi
callq 0x1f650
movq %rax, %rdi
movq 0xb0(%rsp), %rsi
callq 0x1f5c0
movq %rax, 0x38(%rsp)
jmp 0xbd60
movq 0x38(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x4a290
movq %rax, 0x30(%rsp)
jmp 0xbd81
movq 0x98(%rsp), %rdi
movq 0x30(%rsp), %rax
movq %rax, 0xa0(%rsp)
addq $0x40, %rdi
movq 0xd0(%rsp), %rsi
callq 0x1f650
movq %rax, %rdi
movq 0xb0(%rsp), %rsi
callq 0x1f5c0
movq %rax, 0x28(%rsp)
jmp 0xbdbe
movq 0x28(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
movb %al, 0x27(%rsp)
jmp 0xbdde
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xbde8
jmp 0xbe67
movq 0xa0(%rsp), %rdi
movslq 0x18c(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xb0(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3be50
movq %rax, 0x18(%rsp)
jmp 0xbe23
movq 0x18(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1fd60
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0xbe9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
jmp 0xbfaa
movq 0xb0(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3be50
movq %rax, 0x8(%rsp)
jmp 0xbe83
movq 0x8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1fd60
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0xbe9e
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0xbd13
jmp 0xbeb9
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0xbcea
leaq 0x170(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1fa50
jmp 0xbee9
leaq 0x158(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1fa50
jmp 0xbf00
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
jmp 0xbc9c
movq 0x98(%rsp), %rdi
addq $0x180, %rdi            # imm = 0x180
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xbf54
movq 0x98(%rsp), %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x158(%rsp), %rsi
callq 0x1fdb0
jmp 0xbf82
jmp 0xbf86
jmp 0xbf86
jmp 0xbf88
leaq 0x158(%rsp), %rdi
callq 0x1fde0
leaq 0x170(%rsp), %rdi
callq 0x1fde0
addq $0x198, %rsp            # imm = 0x198
retq
leaq 0x158(%rsp), %rdi
callq 0x1fde0
leaq 0x170(%rsp), %rdi
callq 0x1fde0
movq 0x150(%rsp), %rdi
callq 0x43f0
nopw %cs:(%rax,%rax)