// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %r12
cmpb $0x1, 0x3c8(%rdi)
jne 0x5f49
movq 0x3d0(%r12), %rax
incq %rax
movslq 0x3cc(%r12), %rcx
xorl %edx, %edx
divq %rcx
movq %rdx, 0x3d0(%r12)
movq (%r12), %rax
movq %r12, %rdi
callq *0x130(%rax)
testl $0xfffffffb, 0xc(%r12)  # imm = 0xFFFFFFFB
jne 0x5f61
movq (%r12), %rax
movq %r12, %rdi
callq *0x108(%rax)
movq (%r12), %rax
movq %r12, %rdi
callq *0x110(%rax)
movq (%r12), %rax
movq %r12, %rdi
callq *0x118(%rax)
movq (%r12), %rax
movq %r12, %rdi
callq *0x160(%rax)
movq 0x400(%r12), %rax
movslq (%rax), %rbx
leaq 0x138(%r12), %rdi
movq %rbx, %rsi
callq 0x1367a
leaq 0x150(%r12), %rdi
movq %rbx, %rsi
callq 0x1367a
leaq 0x168(%r12), %rdi
movq %rbx, %rsi
callq 0x1367a
leaq 0x180(%r12), %rdi
movq %rbx, %rsi
callq 0x1367a
leaq 0x198(%r12), %rdi
movq %rbx, %rsi
callq 0x1367a
leaq 0x1b0(%r12), %rdi
movq %rbx, %rsi
callq 0x1367a
leaq 0x1c8(%r12), %rdi
movq %rbx, %rsi
callq 0x1367a
leaq 0x1e0(%r12), %rdi
movq %rbx, %rsi
callq 0x1367a
leaq 0x1f8(%r12), %rdi
movq %rbx, (%rsp)
movq %rbx, %rsi
callq 0x136f6
movq 0x90(%r12), %rbx
movq 0x98(%r12), %r14
cmpq %rbx, %r14
je 0x6058
movq %rbx, %r15
movq %r15, %rdi
callq 0x1353a
addq $0x18, %r15
cmpq %r14, %r15
jne 0x603f
movq %rbx, 0x98(%r12)
movq 0xa8(%r12), %rbx
movq 0xb0(%r12), %r14
cmpq %rbx, %r14
je 0x6089
movq %rbx, %r15
movq %r15, %rdi
callq 0x1353a
addq $0x18, %r15
cmpq %r14, %r15
jne 0x6070
movq %rbx, 0xb0(%r12)
movq 0x40(%r12), %r15
movq %r12, 0x8(%rsp)
movq 0x48(%r12), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r15
je 0x6120
movq (%r15), %rax
movq 0x8(%r15), %rbp
subq %rax, %rbp
je 0x6115
movq (%rax), %rcx
movq 0x8(%rax), %r13
movq %r13, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
subq %rcx, %r13
sarq $0x3, %r13
sarq $0x3, %rbp
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rbp
cmpq $0x1, %rbp
adcq $0x0, %rbp
xorl %r12d, %r12d
movq 0x18(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x610d
leaq (%r12,%r12,2), %r14
xorl %ebx, %ebx
movq (%r15), %rax
movq (%rax,%r14,8), %rax
movq (%rax,%rbx,8), %rdi
callq 0x21de8
incq %rbx
cmpq %rbx, %r13
jne 0x60f5
incq %r12
cmpq %rbp, %r12
jne 0x60e3
addq $0x18, %r15
cmpq 0x10(%rsp), %r15
jne 0x60a2
movq 0x8(%rsp), %rbx
leaq 0x218(%rbx), %rdi
movq (%rsp), %r14
movq %r14, %rsi
callq 0x1367a
leaq 0x230(%rbx), %rdi
movq %r14, %rsi
callq 0x1367a
leaq 0x248(%rbx), %rdi
movq %r14, %rsi
callq 0x1367a
leaq 0x260(%rbx), %rdi
movq %r14, %rsi
callq 0x1367a
leaq 0x278(%rbx), %rdi
movl $0x1, %esi
callq 0x1367a
leaq 0x290(%rbx), %rdi
movl $0x1, %esi
callq 0x1367a
leaq 0x2a8(%rbx), %rdi
movl $0x1, %esi
callq 0x1367a
leaq 0x2c0(%rbx), %rdi
movl $0x1, %esi
callq 0x1367a
leaq 0x2d8(%rbx), %rdi
movq %r14, %rsi
callq 0x1367a
addq $0x2f0, %rbx            # imm = 0x2F0
movq %rbx, %rdi
movq %r14, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1367a