// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::TransferValueFromActiveToOriginal_WB()
void ConstructLayer::TransferValueFromActiveToOriginal_WB()
{
    // original WB <= filter => active WB

    size_t nKernels = __weightMatrix.size();
    if(__biasVector.size() != nKernels || __activeFlag.size() != nKernels) 
    {
        std::cout<<"Error: number of kernels not match in filtering"<<std::endl;
        exit(0);
    }

    // for cnn layer
    // cnn layer drop out algorithm only make the filtered element be zero, dimension wont change
    auto active_to_original_cnn = [&](Matrix &original_M, Filter2D &filter_M, Matrix &active_M) 
    {
        auto dim = original_M.Dimension();
        if( dim != filter_M.Dimension())
        {
            std::cout<<"Error: filter M & original M dimension not match."<<std::endl;
            exit(0);
        }

        for(size_t i=0;i<dim.first;i++)
        {
            for(size_t j=0;j<dim.second;j++)
            {
                // transfer value from active matrix to original matrix
                if(filter_M[i][j]) original_M[i][j] = active_M[i][j];
            }
        }
    };

    // for fully connected layer
    // fc layer matrix dimension will change, because drop out will delete neurons
    // since one neuron holds one row of the original w&b matrix, 
    // and collum number still equals the number of active neurons of previous layer
    auto active_to_original_fc = [&](Filter2D &filter_M, Filter2D &filter_M_prev_layer) 
    {
        auto dim = __weightMatrix[0].Dimension();
        assert(dim.first = filter_M.Dimension().first);

        if(filter_M.Dimension().second != 1 || filter_M_prev_layer.Dimension().second != 1)
        {
            std::cout<<"Error: filter matrix fc layer should be a 1-collumn matrix."<<std::endl;
            exit(0);
        }

        size_t active_i = 0;
        for(size_t i=0;i<dim.first;i++)
        {
            if(!filter_M[i][0]) continue;

            // get weights for current active neuron
            Matrix & act_row = __weightMatrixActive[active_i];
            assert(act_row.Dimension().first == 1);

            // get bias for current active neuron
            Matrix & act_b = __biasVectorActive[active_i];
            assert(act_b.Dimension().first == 1);
            assert(act_b.Dimension().second == 1);

            // weight
            size_t active_j = 0;
            for(size_t j=0;j<dim.second;j++)
            {
                if(!filter_M_prev_layer[j][0])
                    continue;
                __weightMatrix[0][i][j] = act_row[active_i][active_j];
                active_j++;
            }
            // bias
            __biasVector[0][i][0] = act_b[0][0];

            active_i++;
            assert(act_row.Dimension().second == active_j);
        }
        assert(__weightMatrixActive.size() == active_i);
    };

    // start transfer
    for(size_t i=0;i<nKernels;i++)
    {
        if(__type == LayerType::cnn || __type == LayerType::pooling) {
            active_to_original_cnn(__weightMatrix[i], __activeFlag[i], __weightMatrixActive[i]);
        }
        else if(__type == LayerType::fullyConnected || __type == LayerType::output)
        {
            // for fc layer, nKernels = 1
            assert(nKernels == 1);
            auto & filter_prev_layer = __prevLayer->GetActiveFlag();
            assert(filter_prev_layer.size() == 1);
            active_to_original_fc(__activeFlag[i], filter_prev_layer[0]);
        }
    }
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq 0x50(%rsp), %rdi
movq %rdi, 0x20(%rsp)
addq $0x60, %rdi
callq 0x1f9c0
movq 0x20(%rsp), %rdi
movq %rax, 0x48(%rsp)
addq $0x78, %rdi
callq 0x1f9c0
cmpq 0x48(%rsp), %rax
jne 0x1486e
movq 0x20(%rsp), %rdi
addq $0x108, %rdi            # imm = 0x108
callq 0x1ffd0
cmpq 0x48(%rsp), %rax
je 0x14897
movq 0x56763(%rip), %rdi     # 0x6afd8
leaq 0x3e817(%rip), %rsi     # 0x53093
callq 0x4230
movq %rax, %rdi
movq 0x56725(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
movq 0x20(%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpq 0x48(%rsp), %rax
jae 0x14a15
movq 0x20(%rsp), %rax
cmpl $0x1, 0xc(%rax)
je 0x148d0
movq 0x20(%rsp), %rax
cmpl $0x2, 0xc(%rax)
jne 0x14935
movq 0x20(%rsp), %rdi
addq $0x60, %rdi
movq 0x30(%rsp), %rsi
callq 0x1f9e0
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%rsp)
addq $0x108, %rdi            # imm = 0x108
movq 0x30(%rsp), %rsi
callq 0x1ff80
movq 0x20(%rsp), %rdi
movq %rax, 0x18(%rsp)
addq $0xd8, %rdi
movq 0x30(%rsp), %rsi
callq 0x1f9e0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
leaq 0x47(%rsp), %rdi
callq 0x14a20
jmp 0x14a00
movq 0x20(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0x1494f
movq 0x20(%rsp), %rax
cmpl $0x4, 0xc(%rax)
jne 0x149fe
cmpq $0x1, 0x48(%rsp)
jne 0x14959
jmp 0x14978
leaq 0x3e763(%rip), %rdi     # 0x530c3
leaq 0x3d86b(%rip), %rsi     # 0x521d2
movl $0xb45, %edx            # imm = 0xB45
leaq 0x3e75e(%rip), %rcx     # 0x530d1
callq 0x4170
movq 0x20(%rsp), %rax
movq 0x3f0(%rax), %rdi
movq (%rdi), %rax
callq *0x268(%rax)
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
callq 0x1ffd0
cmpq $0x1, %rax
jne 0x149a4
jmp 0x149c3
leaq 0x3e76a(%rip), %rdi     # 0x53115
leaq 0x3d820(%rip), %rsi     # 0x521d2
movl $0xb47, %edx            # imm = 0xB47
leaq 0x3e713(%rip), %rcx     # 0x530d1
callq 0x4170
movq 0x20(%rsp), %rdi
addq $0x108, %rdi            # imm = 0x108
movq 0x30(%rsp), %rsi
callq 0x1ff80
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1ff80
movq 0x8(%rsp), %rsi
movq %rax, %rdx
leaq 0x38(%rsp), %rdi
callq 0x14ba0
jmp 0x14a00
jmp 0x14a02
movq 0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x148aa
addq $0x58, %rsp
retq
nopw (%rax,%rax)