// File: /workspace/github_cpp_makefile_star2_O1/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::ForwardPropagateForBatch()
void ConstructLayer::ForwardPropagateForBatch()
{
    assert(__p_data_interface != nullptr);
    int sample_size = __p_data_interface->GetBatchSize();

    // ----step 1) forward propagate for Z image
#ifdef MULTI_THREAD
    auto process_range_for_z = [&](int start, int end)
    {
        for(int sample_index = start; sample_index<end; sample_index++)
        {
            ForwardPropagate_Z_ForSample(sample_index);
        }
    };

    std::vector<std::thread> vth;
    int Range[NTHREAD+1];
    for(int i=0;i<NTHREAD;i++)
        Range[i] = sample_size/NTHREAD * i;
    Range[NTHREAD] = sample_size;

    for(int i=0;i<NTHREAD;i++)
    {
        vth.push_back(std::thread(process_range_for_z, Range[i], Range[i+1]));
    }

    for(auto &i: vth)
        i.join();
#else
    for(int sample_index = 0; sample_index<sample_size; sample_index++)
    {
        ForwardPropagate_Z_ForSample(sample_index);
    }
#endif

    // ----step 2) this section is for batch normalization and related jobs
    // after batch propagation finished, do batch normalization
    if(__use_batch_normalization)
    {
        BatchNormalization_UpdateZ();

        // A-image and \sigma^\prime-image are calcualted after finishing all Z calculations in neurons
        // so if replace un-normalized Z values in neurons with normalized Z values
        // then the nueron will automatically use the normalized Z to 
        // calculate A and \sigma^\prime (In this design, the original un-normalized A and \sigma^\prime
        // will not be calculated, saving computation time)
#ifdef MULTI_THREAD
        auto process_range_for_replace_z = [&](int start, int end)
        {
            for(int sample_index = start; sample_index < end; sample_index++)
            {
                ReplaceNeuronZWithBatchNormalization(sample_index);
            }
        };

        vth.clear();
        for(int i=0;i<NTHREAD;i++)
        {
            vth.push_back(std::thread(process_range_for_replace_z, Range[i], Range[i+1]));
        }

        for(auto &i: vth)
            i.join();
#else
        for(int sample_index = 0;sample_index<sample_size;sample_index++)
        {
            ReplaceNeuronZWithBatchNormalization(sample_index);
        }
#endif
    }

    // test batch noarmlization update z
    //TestBatchNormalization_UpdateZ();

    // ----step 3) forward propagate for sigma^prime and A image
#ifdef MULTI_THREAD
    auto process_range_for_A_sigma_prime = [&](int start, int end)
    {
        for(int sample_index = start; sample_index<end; sample_index++)
        {
            ForwardPropagate_SigmaPrimeAndA_ForSample(sample_index);
        }
    };

    vth.clear();
    for(int i=0;i<NTHREAD;i++)
    {
        vth.push_back(std::thread(process_range_for_A_sigma_prime, Range[i], Range[i+1]));
    }

    for(auto &i: vth)
        i.join();
#else
    for(int sample_index = 0; sample_index<sample_size; sample_index++)
    {
        ForwardPropagate_SigmaPrimeAndA_ForSample(sample_index);
    }
#endif

    // progress
    //std::cout<<"forward propagate for batch finished. Layer: "<<GetID()<<std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x400(%rdi), %rax
testq %rax, %rax
je 0x8c17
movq %rdi, %rbx
movl (%rax), %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movq $0x0, 0x20(%rsp)
movd %ecx, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
psrad $0x3, %xmm0
movdqa 0x1e6b2(%rip), %xmm1    # 0x27020
xorl %ecx, %ecx
pshufd $0xf5, %xmm0, %xmm2     # xmm2 = xmm0[1,1,3,3]
movdqa 0x1e6b3(%rip), %xmm3    # 0x27030
movdqa %xmm0, %xmm4
pmuludq %xmm1, %xmm4
pshufd $0xe8, %xmm4, %xmm4     # xmm4 = xmm4[0,2,2,3]
pshufd $0xf5, %xmm1, %xmm5     # xmm5 = xmm1[1,1,3,3]
pmuludq %xmm2, %xmm5
pshufd $0xe8, %xmm5, %xmm5     # xmm5 = xmm5[0,2,2,3]
punpckldq %xmm5, %xmm4    # xmm4 = xmm4[0],xmm5[0],xmm4[1],xmm5[1]
movdqa %xmm4, 0x30(%rsp,%rcx,4)
addq $0x4, %rcx
paddd %xmm3, %xmm1
cmpq $0x8, %rcx
jne 0x897d
movl %eax, 0x50(%rsp)
xorl %r13d, %r13d
leaq 0x2deaa(%rip), %rbp     # 0x36868
leaq 0x8(%rsp), %r14
movq %rsp, %r15
leaq 0x10(%rsp), %r12
movq $0x0, 0x8(%rsp)
movl $0x18, %edi
callq 0x4270
movq %rbp, (%rax)
movq 0x30(%rsp,%r13,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq %rax, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4210
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8a16
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0x1726e
cmpq $0x0, 0x8(%rsp)
jne 0x8c85
incq %r13
cmpq $0x8, %r13
jne 0x89cb
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0x8a53
movq %r14, %rdi
callq 0x43b0
addq $0x8, %r14
jmp 0x8a40
cmpb $0x0, 0x210(%rbx)
je 0x8b35
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x190(%rax)
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rax, %rcx
je 0x8a96
movq %rax, %rdx
cmpq $0x0, (%rdx)
jne 0x8c85
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0x8a7e
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
leaq 0x2de08(%rip), %rbp     # 0x368a8
leaq 0x8(%rsp), %r14
movq %rsp, %r15
leaq 0x10(%rsp), %r12
movq $0x0, 0x8(%rsp)
movl $0x18, %edi
callq 0x4270
movq %rbp, (%rax)
movq 0x30(%rsp,%r13,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq %rax, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4210
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8af8
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0x1726e
cmpq $0x0, 0x8(%rsp)
jne 0x8c85
incq %r13
cmpq $0x8, %r13
jne 0x8aad
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0x8b35
movq %r14, %rdi
callq 0x43b0
addq $0x8, %r14
jmp 0x8b22
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rax, %rcx
je 0x8b5f
movq %rax, %rdx
cmpq $0x0, (%rdx)
jne 0x8c85
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0x8b47
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
leaq 0x2dd7f(%rip), %rbp     # 0x368e8
leaq 0x8(%rsp), %r14
movq %rsp, %r15
leaq 0x10(%rsp), %r12
movq $0x0, 0x8(%rsp)
movl $0x18, %edi
callq 0x4270
movq %rbp, (%rax)
movq 0x30(%rsp,%r13,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq %rax, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4210
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8bc1
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0x1726e
cmpq $0x0, 0x8(%rsp)
jne 0x8c85
incq %r13
cmpq $0x8, %r13
jne 0x8b76
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x8bfe
movq %rbx, %rdi
callq 0x43b0
addq $0x8, %rbx
jmp 0x8beb
leaq 0x10(%rsp), %rdi
callq 0x13a40
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ee28(%rip), %rdi     # 0x27a46
leaq 0x1eb25(%rip), %rsi     # 0x2774a
leaq 0x1ee38(%rip), %rcx     # 0x27a64
movl $0x330, %edx            # imm = 0x330
callq 0x4160
jmp 0x8c70
jmp 0x8c70
jmp 0x8c70
jmp 0x8c4c
jmp 0x8c68
jmp 0x8c70
jmp 0x8c70
jmp 0x8c70
jmp 0x8c4c
jmp 0x8c68
jmp 0x8c70
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8c5e
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, (%rsp)
jmp 0x8c73
cmpq $0x0, 0x8(%rsp)
jne 0x8c85
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x13a40
movq %rbx, %rdi
callq 0x43e0
callq 0x40a0