// File: /workspace/github_cpp_makefile_star2_O1/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::BatchInit()
void ConstructLayer::BatchInit()
{
    // init image dimension before each batch starts
    // the necessity of this function is originated from drop out in fc layer
    // fc layer drop out changes the dimension of the images

    // init drop out filters; reset all filters to true
    //InitFilters();

    if(__use_drop_out)  // drop out
    {
        // use different drop-out branches in a rolling sequence
        __dropOutBranchIndex++;
        __dropOutBranchIndex = __dropOutBranchIndex%__dropOutBranches;
        DropOut();
    }

    // only fc layer; cnn layer image won't change
    if(__type == LayerType::fullyConnected || __type == LayerType::output) 
    {
        UpdateCoordsForActiveNeuronFC();
    }

    // prepare weights and bias
    UpdateActiveWeightsAndBias();

    // assign active weights and bias to neurons
    AssignWeightsAndBiasToNeurons();

    // clear training information from last batch for this layer
    ClearImage();

    int batch_size = __p_data_interface->GetBatchSize();
    __imageA.resize(batch_size);
    __imageZ.resize(batch_size);
    __imageSigmaPrime.resize(batch_size);
    __imageDelta.resize(batch_size);

    __imageAFull.resize(batch_size);
    __imageZFull.resize(batch_size);
    __imageSigmaPrimeFull.resize(batch_size);
    __imageDeltaFull.resize(batch_size);
    __outputLayerCost.resize(batch_size);

    __wGradient.clear(); // these two needs to be cleared, not resized
    __bGradient.clear(); // these two needs to be cleared, not resized

    // clear training information from last batch for neurons inside this layer
    for(auto &pixel_2d: __neurons)
    {
        auto dim = pixel_2d.Dimension();
        for(size_t i=0;i<dim.first;i++)
        {
            for(size_t j=0;j<dim.second;j++)
            {
                pixel_2d[i][j]->ClearPreviousBatch();
            }
        }
    }

    // batch normalization
    __imageZ_BN.resize(batch_size);
    __imageZFull_BN.resize(batch_size);
    __imageY_BN.resize(batch_size);
    __imageYFull_BN.resize(batch_size);
    __imageAverage_BN.resize(1); // each batch has only one \mu and \sigma
    __imageAverageFull_BN.resize(1);
    __imageVariance_BN.resize(1);
    __imageVarianceFull_BN.resize(1);
    __imageDelta_BN.resize(batch_size);
    __imageDeltaFull_BN.resize(batch_size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
cmpb $0x1, 0x3c8(%rdi)
jne 0x5fe5
movq 0x3d0(%rbx), %rax
incq %rax
movslq 0x3cc(%rbx), %rcx
xorl %edx, %edx
divq %rcx
movq %rdx, 0x3d0(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x130(%rax)
testl $0xfffffffb, 0xc(%rbx)  # imm = 0xFFFFFFFB
jne 0x5ffa
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x108(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x110(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x118(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x160(%rax)
movq 0x400(%rbx), %rax
movslq (%rax), %r14
leaq 0x138(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x150(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x168(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x180(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x198(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x1b0(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x1c8(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x1e0(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x1f8(%rbx), %rdi
movq %r14, (%rsp)
movq %r14, %rsi
callq 0x13848
movq 0x90(%rbx), %r14
movq 0x98(%rbx), %r12
cmpq %r14, %r12
je 0x60e1
movq %r14, %r15
movq %r15, %rdi
callq 0x136aa
addq $0x18, %r15
cmpq %r12, %r15
jne 0x60c9
movq %r14, 0x98(%rbx)
movq 0xa8(%rbx), %r14
movq 0xb0(%rbx), %r12
cmpq %r14, %r12
je 0x610f
movq %r14, %r15
movq %r15, %rdi
callq 0x136aa
addq $0x18, %r15
cmpq %r12, %r15
jne 0x60f7
movq %r14, 0xb0(%rbx)
movq 0x40(%rbx), %r15
movq %rbx, 0x8(%rsp)
movq 0x48(%rbx), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r15
je 0x619c
movq (%r15), %rax
movq 0x8(%r15), %rbp
subq %rax, %rbp
je 0x6151
sarq $0x3, %rbp
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rbp
movq 0x8(%rax), %r14
subq (%rax), %r14
sarq $0x3, %r14
jmp 0x6156
xorl %ebp, %ebp
xorl %r14d, %r14d
testq %rbp, %rbp
je 0x6191
xorl %r13d, %r13d
testq %r14, %r14
je 0x6189
leaq (,%r13,2), %rbx
addq %r13, %rbx
xorl %r12d, %r12d
movq (%r15), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rdi
callq 0x21ed2
incq %r12
cmpq %r12, %r14
jne 0x6171
incq %r13
cmpq %rbp, %r13
jne 0x615e
addq $0x18, %r15
cmpq 0x10(%rsp), %r15
jne 0x6126
movq 0x8(%rsp), %rbx
leaq 0x218(%rbx), %rdi
movq (%rsp), %r14
movq %r14, %rsi
callq 0x137cc
leaq 0x230(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x248(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x260(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
leaq 0x278(%rbx), %rdi
movl $0x1, %esi
callq 0x137cc
leaq 0x290(%rbx), %rdi
movl $0x1, %esi
callq 0x137cc
leaq 0x2a8(%rbx), %rdi
movl $0x1, %esi
callq 0x137cc
leaq 0x2c0(%rbx), %rdi
movl $0x1, %esi
callq 0x137cc
leaq 0x2d8(%rbx), %rdi
movq %r14, %rsi
callq 0x137cc
addq $0x2f0, %rbx            # imm = 0x2F0
movq %rbx, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x137cc