// File: 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:
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 0x9bb2
movq 0x98(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0x9bd2
movq 0x98(%rsp), %rax
cmpl $0x4, 0xc(%rax)
jne 0xa05e
movq $0x0, 0x140(%rsp)
movq 0x98(%rsp), %rcx
movq 0x140(%rsp), %rax
cmpq 0x3a8(%rcx), %rax
jae 0x9ffd
movq 0x98(%rsp), %rax
movq 0x398(%rax), %rsi
movq 0x3a0(%rax), %rdx
leaq 0x128(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x3b590
jmp 0x9c23
movq $0x0, 0x120(%rsp)
movq 0x98(%rsp), %rcx
movq 0x120(%rsp), %rax
cmpq 0x398(%rcx), %rax
jae 0x9e54
movq $0x0, 0x118(%rsp)
movq 0x98(%rsp), %rcx
movq 0x118(%rsp), %rax
cmpq 0x3a0(%rcx), %rax
jae 0x9e39
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 0x9ca8
movq 0x90(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x4a2a0
movq %rax, 0x88(%rsp)
jmp 0x9ccf
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 0x9d12
movq 0x80(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
movb %al, 0x7f(%rsp)
jmp 0x9d35
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0x9d42
jmp 0x9de9
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 0x9d7d
movq 0x70(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1fd60
movsd 0x68(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0x9e1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xa3a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xa398
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x9feb
movq 0x120(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x3be50
movq %rax, 0x60(%rsp)
jmp 0x9e05
movq 0x60(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1fd60
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0x9e20
movq 0x118(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x118(%rsp)
jmp 0x9c58
jmp 0x9e3b
movq 0x120(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x120(%rsp)
jmp 0x9c2f
leaq 0x158(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x1fa50
jmp 0x9e6b
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 0x9ea1
leaq 0x170(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1fa50
jmp 0x9eb8
leaq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
jmp 0x9ed1
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 0x9f06
jmp 0x9f4b
leaq 0x488f1(%rip), %rdi     # 0x527fe
leaq 0x482be(%rip), %rsi     # 0x521d2
movl $0x71b, %edx            # imm = 0x71B
leaq 0x4898e(%rip), %rcx     # 0x528ae
callq 0x4170
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3bb80
jmp 0x9feb
leaq 0xf8(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
jmp 0x9f64
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 0x9f99
jmp 0x9fb8
leaq 0x488b1(%rip), %rdi     # 0x52851
leaq 0x4822b(%rip), %rsi     # 0x521d2
movl $0x71c, %edx            # imm = 0x71C
leaq 0x488fb(%rip), %rcx     # 0x528ae
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 0x9bde
leaq 0x128(%rsp), %rdi
callq 0x3bb80
jmp 0xa398
movq 0x98(%rsp), %rdi
addq $0x150, %rdi            # imm = 0x150
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xa02b
movq 0x98(%rsp), %rdi
addq $0x1b0, %rdi            # imm = 0x1B0
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x158(%rsp), %rsi
callq 0x1fdb0
jmp 0xa059
jmp 0xa376
movq 0x98(%rsp), %rax
cmpl $0x1, 0xc(%rax)
je 0xa07e
movq 0x98(%rsp), %rax
cmpl $0x2, 0xc(%rax)
jne 0xa372
movq $0x0, 0xd0(%rsp)
movq 0x98(%rsp), %rcx
movq 0xd0(%rsp), %rax
cmpq 0x3a8(%rcx), %rax
jae 0xa314
movq 0x98(%rsp), %rax
movq 0x398(%rax), %rsi
movq 0x3a0(%rax), %rdx
leaq 0xb8(%rsp), %rdi
callq 0x3b3b0
jmp 0xa0cc
movq $0x0, 0xb0(%rsp)
movq 0x98(%rsp), %rcx
movq 0xb0(%rsp), %rax
cmpq 0x398(%rcx), %rax
jae 0xa2c0
movq $0x0, 0xa8(%rsp)
movq 0x98(%rsp), %rcx
movq 0xa8(%rsp), %rax
cmpq 0x3a0(%rcx), %rax
jae 0xa2a5
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 0xa14e
movq 0x38(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x4a2a0
movq %rax, 0x30(%rsp)
jmp 0xa16f
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 0xa1ac
movq 0x28(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
movb %al, 0x27(%rsp)
jmp 0xa1cc
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xa1d6
jmp 0xa255
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 0xa211
movq 0x18(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1fd60
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0xa28a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
jmp 0xa398
movq 0xb0(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3be50
movq %rax, 0x8(%rsp)
jmp 0xa271
movq 0x8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1fd60
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0xa28c
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0xa101
jmp 0xa2a7
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0xa0d8
leaq 0x170(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1fa50
jmp 0xa2d7
leaq 0x158(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1fa50
jmp 0xa2ee
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
jmp 0xa08a
movq 0x98(%rsp), %rdi
addq $0x150, %rdi            # imm = 0x150
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x170(%rsp), %rsi
callq 0x1fdb0
jmp 0xa342
movq 0x98(%rsp), %rdi
addq $0x1b0, %rdi            # imm = 0x1B0
movslq 0x18c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x158(%rsp), %rsi
callq 0x1fdb0
jmp 0xa370
jmp 0xa374
jmp 0xa374
jmp 0xa376
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