// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::UpdateImagesA(int)
void ConstructLayer::UpdateImagesA(int sample_id)
{
    // __imageA 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 imageA 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 inside a batch
    //size_t l = __imageA.size();
    //cout<<" >>> image in lyaer id: "<<GetID()<<" size: "<<l<<endl;

    // extract the A matrices from neurons for current traning sample
    Images sample_image_A;
    Images sample_image_A_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 A( __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 & a_vector = __neurons[k][i][j]->GetAVector();
                    if(__neurons[k][i][j]->IsActive())
                    {  
                        // make sure no over extract
                        //cout<<a_vector.size()<<"......"<<l<<endl;
                        //assert(a_vector.size() - 1 == l); // obsolete
                        //A[i][j] = a_vector.back();        // obsolete
                        A[i][j] = a_vector[sample_id];
                    }
                    else
                    {
                        // if neuron is inactive, set it to 0
                        A[i][j] = 0;
                    }
                }
            }
            // save full image
            sample_image_A_full.OutputImageFromKernel.push_back(A);
            //cout<<"A after filling: "<<endl<<A<<endl;

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

            assert(R.Dimension().first  == __activeNeuronDim.i);
            assert(R.Dimension().second == __activeNeuronDim.j);
        }
        //__imageA.push_back(sample_image_A); // obsolete
        __imageA[sample_id] = sample_image_A;
        __imageAFull[sample_id] = sample_image_A_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 A( __neuronDim.i, __neuronDim.j);
            for(size_t i=0;i<__neuronDim.i;i++){
                for(size_t j=0;j<__neuronDim.j;j++)
                {
                    auto & a_vector = __neurons[k][i][j]->GetAVector();
                    if(__neurons[k][i][j]->IsActive())
                    {  
                        // make sure no over extract
                        //assert(a_vector.size() - 1 == l); // obsolete
                        //A[i][j] = a_vector.back();        // obsolete
                        A[i][j] = a_vector[sample_id];
                    }
                    else
                    {
                        A[i][j] = 0;
                    }
                }
            }
            sample_image_A.OutputImageFromKernel.push_back(A); // no need to filter
            sample_image_A_full.OutputImageFromKernel.push_back(A); // no need to filter
        }
        //__imageA.push_back(sample_image_A);
        __imageA[sample_id] = sample_image_A;
        __imageAFull[sample_id] = sample_image_A;
    }
    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 0xa5b2
movq 0x98(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0xa5d2
movq 0x98(%rsp), %rax
cmpl $0x4, 0xc(%rax)
jne 0xaa5e
movq $0x0, 0x140(%rsp)
movq 0x98(%rsp), %rcx
movq 0x140(%rsp), %rax
cmpq 0x3a8(%rcx), %rax
jae 0xa9fd
movq 0x98(%rsp), %rax
movq 0x398(%rax), %rsi
movq 0x3a0(%rax), %rdx
leaq 0x128(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x3b590
jmp 0xa623
movq $0x0, 0x120(%rsp)
movq 0x98(%rsp), %rcx
movq 0x120(%rsp), %rax
cmpq 0x398(%rcx), %rax
jae 0xa854
movq $0x0, 0x118(%rsp)
movq 0x98(%rsp), %rcx
movq 0x118(%rsp), %rax
cmpq 0x3a0(%rcx), %rax
jae 0xa839
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 0xa6a8
movq 0x90(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x4a280
movq %rax, 0x88(%rsp)
jmp 0xa6cf
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 0xa712
movq 0x80(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
movb %al, 0x7f(%rsp)
jmp 0xa735
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0xa742
jmp 0xa7e9
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 0xa77d
movq 0x70(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1fd60
movsd 0x68(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0xa81e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xada5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xad98
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xa9eb
movq 0x120(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x3be50
movq %rax, 0x60(%rsp)
jmp 0xa805
movq 0x60(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1fd60
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0xa820
movq 0x118(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x118(%rsp)
jmp 0xa658
jmp 0xa83b
movq 0x120(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x120(%rsp)
jmp 0xa62f
leaq 0x158(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x1fa50
jmp 0xa86b
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 0xa8a1
leaq 0x170(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1fa50
jmp 0xa8b8
leaq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
jmp 0xa8d1
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 0xa906
jmp 0xa94b
leaq 0x47ef1(%rip), %rdi     # 0x527fe
leaq 0x478be(%rip), %rsi     # 0x521d2
movl $0x66a, %edx            # imm = 0x66A
leaq 0x47f09(%rip), %rcx     # 0x52829
callq 0x4170
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3bb80
jmp 0xa9eb
leaq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
jmp 0xa964
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 0xa999
jmp 0xa9b8
leaq 0x47eb1(%rip), %rdi     # 0x52851
leaq 0x4782b(%rip), %rsi     # 0x521d2
movl $0x66b, %edx            # imm = 0x66B
leaq 0x47e76(%rip), %rcx     # 0x52829
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 0xa5de
leaq 0x128(%rsp), %rdi
callq 0x3bb80
jmp 0xad98
movq 0x98(%rsp), %rdi
addq $0x138, %rdi            # imm = 0x138
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xaa2b
movq 0x98(%rsp), %rdi
addq $0x198, %rdi            # imm = 0x198
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x158(%rsp), %rsi
callq 0x1fdb0
jmp 0xaa59
jmp 0xad76
movq 0x98(%rsp), %rax
cmpl $0x1, 0xc(%rax)
je 0xaa7e
movq 0x98(%rsp), %rax
cmpl $0x2, 0xc(%rax)
jne 0xad72
movq $0x0, 0xd0(%rsp)
movq 0x98(%rsp), %rcx
movq 0xd0(%rsp), %rax
cmpq 0x3a8(%rcx), %rax
jae 0xad14
movq 0x98(%rsp), %rax
movq 0x398(%rax), %rsi
movq 0x3a0(%rax), %rdx
leaq 0xb8(%rsp), %rdi
callq 0x3b3b0
jmp 0xaacc
movq $0x0, 0xb0(%rsp)
movq 0x98(%rsp), %rcx
movq 0xb0(%rsp), %rax
cmpq 0x398(%rcx), %rax
jae 0xacc0
movq $0x0, 0xa8(%rsp)
movq 0x98(%rsp), %rcx
movq 0xa8(%rsp), %rax
cmpq 0x3a0(%rcx), %rax
jae 0xaca5
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 0xab4e
movq 0x38(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x4a280
movq %rax, 0x30(%rsp)
jmp 0xab6f
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 0xabac
movq 0x28(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
movb %al, 0x27(%rsp)
jmp 0xabcc
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xabd6
jmp 0xac55
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 0xac11
movq 0x18(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1fd60
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0xac8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
jmp 0xad98
movq 0xb0(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3be50
movq %rax, 0x8(%rsp)
jmp 0xac71
movq 0x8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1fd60
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0xac8c
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0xab01
jmp 0xaca7
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0xaad8
leaq 0x170(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1fa50
jmp 0xacd7
leaq 0x158(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1fa50
jmp 0xacee
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
jmp 0xaa8a
movq 0x98(%rsp), %rdi
addq $0x138, %rdi            # imm = 0x138
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xad42
movq 0x98(%rsp), %rdi
addq $0x198, %rdi            # imm = 0x198
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xad70
jmp 0xad74
jmp 0xad74
jmp 0xad76
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