// File: /workspace/github_cpp_makefile_star2_O1/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::UpdateImagesZ(int)
void ConstructLayer::UpdateImagesZ(int sample_id)
{
    // __imageZ 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 imageZ 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

    // extract the A matrices from neurons for current traning sample
    Images sample_image_Z;
    Images sample_image_Z_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 Z( __neuronDim.i, __neuronDim.j, 0);
            //cout<<"Z before filling"<<endl<<Z<<endl;
            for(size_t i=0;i<__neuronDim.i;i++){
                for(size_t j=0;j<__neuronDim.j;j++)
                {
                    auto & z_vector = __neurons[k][i][j]->GetZVector();
                    if(__neurons[k][i][j]->IsActive())
                    {  
                        // make sure no over extract
                        //assert(z_vector.size() - 1 == l); // obsolete
                        //Z[i][j] = z_vector.back();        // obsolete
                        Z[i][j] = z_vector[sample_id];
                    }
                    else
                    {
                        // if neuron is inactive, set it to 0
                        Z[i][j] = 0;
                    }
                }
            }
            //cout<<"Z after filing: "<<endl<<Z<<endl;
            // save full image
            sample_image_Z_full.OutputImageFromKernel.push_back(Z);
            // only save active elements
            Matrix R = filterMatrix(Z, __activeFlag[k]);
            sample_image_Z.OutputImageFromKernel.push_back(R);

            assert(R.Dimension().first  == __activeNeuronDim.i);
            assert(R.Dimension().second == __activeNeuronDim.j);
        }
        //__imageZ.push_back(sample_image_Z);
        __imageZ[sample_id] = sample_image_Z;
        __imageZFull[sample_id] = sample_image_Z_full;
    }
    else if(__type == LayerType::cnn || __type == LayerType::pooling) // for cnn layer and pooling
    {
        // 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 Z( __neuronDim.i, __neuronDim.j);
            for(size_t i=0;i<__neuronDim.i;i++){
                for(size_t j=0;j<__neuronDim.j;j++)
                {
                    auto & z_vector = __neurons[k][i][j]->GetZVector();
                    if(__neurons[k][i][j]->IsActive())
                    {  
                        // make sure no over extract
                        //assert(z_vector.size() - 1 == l); // obsolete
                        //Z[i][j] = z_vector.back();        // obsolete
                        Z[i][j] = z_vector[sample_id];
                    }
                    else
                    {
                        Z[i][j] = 0;
                    }
                }
            }
            sample_image_Z.OutputImageFromKernel.push_back(Z); // no need to filter
            sample_image_Z_full.OutputImageFromKernel.push_back(Z); // no need to filter
        }
        //__imageZ.push_back(sample_image_Z);
        __imageZ[sample_id] = sample_image_Z;
        __imageZFull[sample_id] = sample_image_Z_full;
    }
    else // for other layer types
    {
        // reserved for future types of layers
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movl 0xc(%rdi), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0x778d
cmpl $0x4, %eax
je 0x75db
testl %eax, %eax
jne 0x790a
cmpq $0x0, 0x3a8(%rbx)
movslq %esi, %r14
je 0x776f
xorl %r15d, %r15d
leaq 0x28(%rsp), %r12
movq %r14, 0x18(%rsp)
movq %r15, 0x8(%rsp)
movq 0x398(%rbx), %rsi
movq 0x3a0(%rbx), %rdx
xorps %xmm0, %xmm0
movq %r12, %rdi
callq 0x1a952
cmpq $0x0, 0x398(%rbx)
je 0x76da
movq 0x8(%rsp), %rax
leaq (%rax,%rax,2), %r12
xorl %ebp, %ebp
cmpq $0x0, 0x3a0(%rbx)
je 0x76ca
leaq (,%rbp,2), %r13
addq %rbp, %r13
xorl %r14d, %r14d
movq 0x40(%rbx), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x21eac
movq %rax, %r15
movq 0x40(%rbx), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x2097e
testb %al, %al
je 0x76a5
movq (%r15), %rax
movq 0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x1ae76
movsd 0x20(%rsp), %xmm0
jmp 0x76b5
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x1ae76
xorps %xmm0, %xmm0
movq (%rax), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
cmpq 0x3a0(%rbx), %r14
jb 0x764c
incq %rbp
cmpq 0x398(%rbx), %rbp
jb 0x7630
leaq 0x60(%rsp), %rdi
leaq 0x28(%rsp), %r12
movq %r12, %rsi
callq 0x13a14
movq 0x8(%rsp), %r15
leaq (%r15,%r15,2), %rdx
shlq $0x3, %rdx
addq 0x108(%rbx), %rdx
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xb155
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x13a14
movq %r13, %rdi
callq 0x1ac7c
movq 0x18(%rsp), %r14
cmpq 0x3b0(%rbx), %rax
jne 0x7930
movq %r13, %rdi
callq 0x1ac7c
cmpq 0x3b8(%rbx), %rdx
jne 0x794f
movq %r13, %rdi
callq 0x1acb6
movq %r12, %rdi
callq 0x1acb6
incq %r15
cmpq 0x3a8(%rbx), %r15
jb 0x75f9
leaq (%r14,%r14,2), %rdi
shlq $0x3, %rdi
addq 0x150(%rbx), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1429e
jmp 0x78f1
movl %esi, 0x14(%rsp)
cmpq $0x0, 0x3a8(%rbx)
je 0x78d3
movslq 0x14(%rsp), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
leaq 0x28(%rsp), %r14
movq 0x398(%rbx), %rsi
movq 0x3a0(%rbx), %rdx
movq %r14, %rdi
callq 0x1a82e
cmpq $0x0, 0x398(%rbx)
je 0x7892
movq 0x8(%rsp), %rax
leaq (%rax,%rax,2), %r12
xorl %r13d, %r13d
cmpq $0x0, 0x3a0(%rbx)
je 0x7882
leaq (,%r13,2), %r15
addq %r13, %r15
xorl %r14d, %r14d
movq 0x40(%rbx), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x21eac
movq %rax, %rbp
movq 0x40(%rbx), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x2097e
testb %al, %al
je 0x785d
movq (%rbp), %rax
movq 0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x1ae76
movsd 0x20(%rsp), %xmm0
jmp 0x786d
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x1ae76
xorps %xmm0, %xmm0
movq (%rax), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
cmpq 0x3a0(%rbx), %r14
jb 0x7803
incq %r13
cmpq 0x398(%rbx), %r13
jb 0x77e7
leaq 0x40(%rsp), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x13a14
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x13a14
movq %r14, %rdi
callq 0x1acb6
movq 0x8(%rsp), %rcx
incq %rcx
movq %rcx, 0x8(%rsp)
cmpq 0x3a8(%rbx), %rcx
jb 0x77b7
movslq 0x14(%rsp), %r14
leaq (%r14,%r14,2), %rdi
shlq $0x3, %rdi
addq 0x150(%rbx), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1429e
leaq (%r14,%r14,2), %rdi
shlq $0x3, %rdi
addq 0x1b0(%rbx), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1429e
leaq 0x60(%rsp), %rdi
callq 0x136aa
leaq 0x40(%rsp), %rdi
callq 0x136aa
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2043f(%rip), %rdi     # 0x27d76
leaq 0x1fe0c(%rip), %rsi     # 0x2774a
leaq 0x204e1(%rip), %rcx     # 0x27e26
movl $0x71b, %edx            # imm = 0x71B
callq 0x4160
leaq 0x20473(%rip), %rdi     # 0x27dc9
leaq 0x1fded(%rip), %rsi     # 0x2774a
leaq 0x204c2(%rip), %rcx     # 0x27e26
movl $0x71c, %edx            # imm = 0x71C
callq 0x4160
jmp 0x798c
jmp 0x7974
jmp 0x7974
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x1acb6
jmp 0x7998
jmp 0x7995
jmp 0x7995
jmp 0x798c
movq %rax, %rbx
jmp 0x79a2
jmp 0x7995
jmp 0x7995
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x1acb6
leaq 0x60(%rsp), %rdi
callq 0x136aa
leaq 0x40(%rsp), %rdi
callq 0x136aa
movq %rbx, %rdi
callq 0x43e0