// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
andq $0x0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl 0xc(%rdi), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0x75f9
cmpl $0x4, %eax
je 0x7499
testl %eax, %eax
jne 0x7754
movslq %esi, %r14
xorl %r12d, %r12d
leaq 0x18(%rsp), %r13
movq %r14, (%rsp)
cmpq 0x3a8(%rbx), %r12
jae 0x7727
movq 0x398(%rbx), %rsi
movq 0x3a0(%rbx), %rdx
xorps %xmm0, %xmm0
movq %r13, %rdi
callq 0x177ee
movq %r12, 0x8(%rsp)
imulq $0x18, %r12, %rbp
xorl %r13d, %r13d
cmpq 0x398(%rbx), %r13
jae 0x7575
imulq $0x18, %r13, %r12
xorl %r15d, %r15d
cmpq 0x3a0(%rbx), %r15
jae 0x756d
movq 0x40(%rbx), %rax
movq (%rax,%rbp), %rax
movq (%rax,%r12), %rax
movq (%rax,%r15,8), %rdi
callq 0x1d5b4
movq %rax, %r14
movq 0x40(%rbx), %rax
movq (%rax,%rbp), %rax
movq (%rax,%r12), %rax
movq (%rax,%r15,8), %rdi
callq 0x1c256
testb %al, %al
je 0x754f
movq (%r14), %rax
movq (%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x17c46
movsd 0x10(%rsp), %xmm0
jmp 0x755f
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x17c46
xorps %xmm0, %xmm0
movq (%rax), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
jmp 0x74ee
incq %r13
jmp 0x74da
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %r13
movq %r13, %rsi
callq 0x1139c
addq 0x108(%rbx), %rbp
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0xa45e
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x1139c
movq (%rsp), %r14
movq 0x8(%rsp), %r12
movq %r15, %rdi
callq 0x17a6a
cmpq 0x3b0(%rbx), %rax
jne 0x7799
movq %r15, %rdi
callq 0x17a6a
cmpq 0x3b8(%rbx), %rdx
jne 0x777a
movq %r15, %rdi
callq 0x17a9a
movq %r13, %rdi
callq 0x17a9a
incq %r12
jmp 0x74a8
movslq %esi, %r14
xorl %r12d, %r12d
leaq 0x18(%rsp), %r13
leaq 0x30(%rsp), %r15
movq %r14, (%rsp)
cmpq 0x3a8(%rbx), %r12
jae 0x7712
movq 0x398(%rbx), %rsi
movq 0x3a0(%rbx), %rdx
movq %r13, %rdi
callq 0x176e6
movq %r12, 0x8(%rsp)
imulq $0x18, %r12, %rbp
xorl %r13d, %r13d
cmpq 0x398(%rbx), %r13
jae 0x76d7
imulq $0x18, %r13, %r12
xorl %r15d, %r15d
cmpq 0x3a0(%rbx), %r15
jae 0x76cf
movq 0x40(%rbx), %rax
movq (%rax,%rbp), %rax
movq (%rax,%r12), %rax
movq (%rax,%r15,8), %rdi
callq 0x1d5b4
movq %rax, %r14
movq 0x40(%rbx), %rax
movq (%rax,%rbp), %rax
movq (%rax,%r12), %rax
movq (%rax,%r15,8), %rdi
callq 0x1c256
testb %al, %al
je 0x76b1
movq (%r14), %rax
movq (%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x17c46
movsd 0x10(%rsp), %xmm0
jmp 0x76c1
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x17c46
xorps %xmm0, %xmm0
movq (%rax), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
jmp 0x7650
incq %r13
jmp 0x763c
leaq 0x30(%rsp), %r15
movq %r15, %rdi
leaq 0x18(%rsp), %r13
movq %r13, %rsi
callq 0x1139c
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0x1139c
movq %r13, %rdi
callq 0x17a9a
movq 0x8(%rsp), %r12
incq %r12
movq (%rsp), %r14
jmp 0x760d
imulq $0x18, %r14, %rdi
addq 0x138(%rbx), %rdi
movq %r15, %rsi
callq 0x11a9e
jmp 0x7741
imulq $0x18, %r14, %rdi
addq 0x138(%rbx), %rdi
leaq 0x30(%rsp), %rsi
callq 0x11a9e
leaq 0x50(%rsp), %r15
imulq $0x18, %r14, %rdi
addq 0x198(%rbx), %rdi
movq %r15, %rsi
callq 0x11a9e
leaq 0x50(%rsp), %rdi
callq 0x110e4
leaq 0x30(%rsp), %rdi
callq 0x110e4
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a599(%rip), %rdi     # 0x21d1a
leaq 0x19f52(%rip), %rsi     # 0x216da
leaq 0x1a563(%rip), %rcx     # 0x21cf2
movl $0x66b, %edx            # imm = 0x66B
callq 0x4180
leaq 0x1a527(%rip), %rdi     # 0x21cc7
leaq 0x19f33(%rip), %rsi     # 0x216da
leaq 0x1a544(%rip), %rcx     # 0x21cf2
movl $0x66a, %edx            # imm = 0x66A
callq 0x4180
jmp 0x77d3
jmp 0x77c0
jmp 0x77dc
jmp 0x77c0
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x17a9a
jmp 0x77df
jmp 0x77dc
jmp 0x77d3
movq %rax, %rbx
jmp 0x77e9
jmp 0x77dc
jmp 0x77dc
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x17a9a
leaq 0x50(%rsp), %rdi
callq 0x110e4
leaq 0x30(%rsp), %rdi
callq 0x110e4
movq %rbx, %rdi
callq 0x43b0
nop