// File: /workspace/github_cpp_makefile_star2_O2/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 0x5d9f
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 0x5db4
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 0x1118e
leaq 0x150(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x168(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x180(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x198(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x1b0(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x1c8(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x1e0(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x1f8(%rbx), %rdi
movq %r14, (%rsp)
movq %r14, %rsi
callq 0x111c4
leaq 0x90(%rbx), %rdi
callq 0x111f6
leaq 0xa8(%rbx), %rdi
callq 0x111f6
movq 0x40(%rbx), %r15
movq %rbx, 0x8(%rsp)
movq 0x48(%rbx), %rax
movq %rax, 0x10(%rsp)
cmpq 0x10(%rsp), %r15
je 0x5edf
movq %r15, %rdi
callq 0x111fe
movq %rax, %r12
movq %rdx, %r13
xorl %r14d, %r14d
cmpq %r12, %r14
je 0x5ed9
imulq $0x18, %r14, %rbx
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0x5ed4
movq (%r15), %rax
movq (%rax,%rbx), %rax
movq (%rax,%rbp,8), %rdi
callq 0x1d5e6
incq %rbp
jmp 0x5eba
incq %r14
jmp 0x5eaf
addq $0x18, %r15
jmp 0x5e97
movq 0x8(%rsp), %rbx
leaq 0x218(%rbx), %rdi
movq (%rsp), %r14
movq %r14, %rsi
callq 0x1118e
leaq 0x230(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x248(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x260(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
leaq 0x278(%rbx), %rdi
pushq $0x1
popq %r15
movq %r15, %rsi
callq 0x1118e
leaq 0x290(%rbx), %rdi
movq %r15, %rsi
callq 0x1118e
leaq 0x2a8(%rbx), %rdi
movq %r15, %rsi
callq 0x1118e
leaq 0x2c0(%rbx), %rdi
movq %r15, %rsi
callq 0x1118e
leaq 0x2d8(%rbx), %rdi
movq %r14, %rsi
callq 0x1118e
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 0x1118e
nop