// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::UpdateImagesSigmaPrime(int)
void ConstructLayer::UpdateImagesSigmaPrime(int sample_id)
{
    // __imageSigmaPrime shold only store images from all active neurons, the matching info can be achieved from filter matrix
    // __imageSigmaPrimeFull stores images from all neurons (active + inactive), with inactive set to 0.
    //    **** on batch level, the filter matrix stays the same, so no need to worry the change of filter matrix inside a batch

    // extract the sigma^\prime matrices from neurons for current traning sample
    Images sample_image_SigmaPrime;
    Images sample_image_SigmaPrime_full;

    if(__type == LayerType::fullyConnected || __type == LayerType::output) 
    {
        // for fully connected layer; output layer is also a fully connected layer
        for(size_t k=0;k<__neuronDim.k;k++) // kernel
        {
            Matrix SigP( __neuronDim.i, __neuronDim.j, 0);
            //cout<<"A before filling"<<endl<<A<<endl;
            for(size_t i=0;i<__neuronDim.i;i++){
                for(size_t j=0;j<__neuronDim.j;j++)
                {
                    auto & s_vector = __neurons[k][i][j]->GetSigmaPrimeVector();
                    if(__neurons[k][i][j]->IsActive())
                    {
                        SigP[i][j] = s_vector[sample_id];
                    }
                    else
                    {
                        // if neuron is inactive, set it to 0
                        SigP[i][j] = 0;
                    }
                }
            }
            // save full image
            sample_image_SigmaPrime_full.OutputImageFromKernel.push_back(SigP);
            //cout<<"A after filling: "<<endl<<A<<endl;

            // only save active elements
            Matrix R = filterMatrix(SigP, __activeFlag[k]);
            sample_image_SigmaPrime.OutputImageFromKernel.push_back(R);

            assert(R.Dimension().first  == __activeNeuronDim.i);
            assert(R.Dimension().second == __activeNeuronDim.j);
        }
        //__imageA.push_back(sample_image_A); // obsolete
        __imageSigmaPrime[sample_id] = sample_image_SigmaPrime;
        __imageSigmaPrimeFull[sample_id] = sample_image_SigmaPrime_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 SigP( __neuronDim.i, __neuronDim.j);
            for(size_t i=0;i<__neuronDim.i;i++){
                for(size_t j=0;j<__neuronDim.j;j++)
                {
                    auto & s_vector = __neurons[k][i][j]->GetSigmaPrimeVector();
                    if(__neurons[k][i][j]->IsActive())
                    { 
                        SigP[i][j] = s_vector[sample_id];
                    }
                    else
                    {
                        SigP[i][j] = 0;
                    }
                }
            }
            sample_image_SigmaPrime.OutputImageFromKernel.push_back(SigP); // no need to filter
            sample_image_SigmaPrime_full.OutputImageFromKernel.push_back(SigP); // no need to filter
        }
        //__imageA.push_back(sample_image_A);
        __imageSigmaPrime[sample_id] = sample_image_SigmaPrime;
        __imageSigmaPrimeFull[sample_id] = sample_image_SigmaPrime;
    }
    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 0xae02
movq 0x98(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0xae22
movq 0x98(%rsp), %rax
cmpl $0x4, 0xc(%rax)
jne 0xb2ae
movq $0x0, 0x140(%rsp)
movq 0x98(%rsp), %rcx
movq 0x140(%rsp), %rax
cmpq 0x3a8(%rcx), %rax
jae 0xb24d
movq 0x98(%rsp), %rax
movq 0x398(%rax), %rsi
movq 0x3a0(%rax), %rdx
leaq 0x128(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x3b590
jmp 0xae73
movq $0x0, 0x120(%rsp)
movq 0x98(%rsp), %rcx
movq 0x120(%rsp), %rax
cmpq 0x398(%rcx), %rax
jae 0xb0a4
movq $0x0, 0x118(%rsp)
movq 0x98(%rsp), %rcx
movq 0x118(%rsp), %rax
cmpq 0x3a0(%rcx), %rax
jae 0xb089
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 0xaef8
movq 0x90(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x4a2b0
movq %rax, 0x88(%rsp)
jmp 0xaf1f
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 0xaf62
movq 0x80(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
movb %al, 0x7f(%rsp)
jmp 0xaf85
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0xaf92
jmp 0xb039
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 0xafcd
movq 0x70(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1fd60
movsd 0x68(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0xb06e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xb5f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xb5e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xb23b
movq 0x120(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x3be50
movq %rax, 0x60(%rsp)
jmp 0xb055
movq 0x60(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1fd60
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0xb070
movq 0x118(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x118(%rsp)
jmp 0xaea8
jmp 0xb08b
movq 0x120(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x120(%rsp)
jmp 0xae7f
leaq 0x158(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x1fa50
jmp 0xb0bb
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 0xb0f1
leaq 0x170(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1fa50
jmp 0xb108
leaq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
jmp 0xb121
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 0xb156
jmp 0xb19b
leaq 0x476a1(%rip), %rdi     # 0x527fe
leaq 0x4706e(%rip), %rsi     # 0x521d2
movl $0x6be, %edx            # imm = 0x6BE
leaq 0x4770d(%rip), %rcx     # 0x5287d
callq 0x4170
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3bb80
jmp 0xb23b
leaq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
jmp 0xb1b4
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 0xb1e9
jmp 0xb208
leaq 0x47661(%rip), %rdi     # 0x52851
leaq 0x46fdb(%rip), %rsi     # 0x521d2
movl $0x6bf, %edx            # imm = 0x6BF
leaq 0x4767a(%rip), %rcx     # 0x5287d
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 0xae2e
leaq 0x128(%rsp), %rdi
callq 0x3bb80
jmp 0xb5e8
movq 0x98(%rsp), %rdi
addq $0x168, %rdi            # imm = 0x168
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xb27b
movq 0x98(%rsp), %rdi
addq $0x1c8, %rdi            # imm = 0x1C8
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x158(%rsp), %rsi
callq 0x1fdb0
jmp 0xb2a9
jmp 0xb5c6
movq 0x98(%rsp), %rax
cmpl $0x1, 0xc(%rax)
je 0xb2ce
movq 0x98(%rsp), %rax
cmpl $0x2, 0xc(%rax)
jne 0xb5c2
movq $0x0, 0xd0(%rsp)
movq 0x98(%rsp), %rcx
movq 0xd0(%rsp), %rax
cmpq 0x3a8(%rcx), %rax
jae 0xb564
movq 0x98(%rsp), %rax
movq 0x398(%rax), %rsi
movq 0x3a0(%rax), %rdx
leaq 0xb8(%rsp), %rdi
callq 0x3b3b0
jmp 0xb31c
movq $0x0, 0xb0(%rsp)
movq 0x98(%rsp), %rcx
movq 0xb0(%rsp), %rax
cmpq 0x398(%rcx), %rax
jae 0xb510
movq $0x0, 0xa8(%rsp)
movq 0x98(%rsp), %rcx
movq 0xa8(%rsp), %rax
cmpq 0x3a0(%rcx), %rax
jae 0xb4f5
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 0xb39e
movq 0x38(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x4a2b0
movq %rax, 0x30(%rsp)
jmp 0xb3bf
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 0xb3fc
movq 0x28(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
movb %al, 0x27(%rsp)
jmp 0xb41c
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xb426
jmp 0xb4a5
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 0xb461
movq 0x18(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1fd60
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0xb4da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
jmp 0xb5e8
movq 0xb0(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3be50
movq %rax, 0x8(%rsp)
jmp 0xb4c1
movq 0x8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1fd60
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0xb4dc
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0xb351
jmp 0xb4f7
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0xb328
leaq 0x170(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1fa50
jmp 0xb527
leaq 0x158(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1fa50
jmp 0xb53e
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
jmp 0xb2da
movq 0x98(%rsp), %rdi
addq $0x168, %rdi            # imm = 0x168
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xb592
movq 0x98(%rsp), %rdi
addq $0x1c8, %rdi            # imm = 0x1C8
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xb5c0
jmp 0xb5c4
jmp 0xb5c4
jmp 0xb5c6
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
nop