// File: /workspace/github_cpp_makefile_star2_O3/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 0x8bcb
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 0x1e6fa(%rip), %xmm1    # 0x27020
xorl %ecx, %ecx
pshufd $0xf5, %xmm0, %xmm2     # xmm2 = xmm0[1,1,3,3]
movdqa 0x1e6fb(%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 0x8935
movl %eax, 0x50(%rsp)
movl $0x1, %r13d
movl 0x30(%rsp), %r12d
movq %rsp, %r14
movl %r12d, %r15d
movl 0x30(%rsp,%r13,4), %ebp
movq $0x0, (%rsp)
movl $0x18, %edi
callq 0x4270
leaq 0x2decd(%rip), %rcx     # 0x36868
movq %rcx, (%rax)
movl %ebp, 0x8(%rax)
movl %r15d, 0xc(%rax)
movq %rbx, 0x10(%rax)
movq %rax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x4210
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x89cd
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1718e
cmpq $0x0, (%rsp)
jne 0x8c31
incq %r13
movl %ebp, %r15d
cmpq $0x9, %r13
jne 0x897d
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0x8a0e
movq %r14, %rdi
callq 0x43b0
addq $0x8, %r14
jmp 0x89fb
cmpb $0x0, 0x210(%rbx)
je 0x8aeb
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x190(%rax)
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rax, %rcx
je 0x8a51
movq %rax, %rdx
cmpq $0x0, (%rdx)
jne 0x8c31
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0x8a39
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movq %rsp, %r14
movl %r12d, %r15d
movl 0x34(%rsp,%r13,4), %ebp
movq $0x0, (%rsp)
movl $0x18, %edi
callq 0x4270
leaq 0x2de30(%rip), %rcx     # 0x368a8
movq %rcx, (%rax)
movl %ebp, 0x8(%rax)
movl %r15d, 0xc(%rax)
movq %rbx, 0x10(%rax)
movq %rax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x4210
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x8aaa
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1718e
cmpq $0x0, (%rsp)
jne 0x8c31
incq %r13
movl %ebp, %r15d
cmpq $0x8, %r13
jne 0x8a5a
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0x8aeb
movq %r14, %rdi
callq 0x43b0
addq $0x8, %r14
jmp 0x8ad8
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rax, %rcx
je 0x8b15
movq %rax, %rdx
cmpq $0x0, (%rdx)
jne 0x8c31
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0x8afd
movq %rax, 0x18(%rsp)
movl $0x1, %r13d
movq %rsp, %r14
leaq 0x10(%rsp), %r15
movl 0x30(%rsp,%r13,4), %ebp
movq $0x0, (%rsp)
movl $0x18, %edi
callq 0x4270
leaq 0x2dda7(%rip), %rcx     # 0x368e8
movq %rcx, (%rax)
movl %ebp, 0x8(%rax)
movl %r12d, 0xc(%rax)
movq %rbx, 0x10(%rax)
movq %rax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x4210
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x8b73
movq (%rdi), %rax
callq *0x8(%rax)
movq %r15, %rdi
movq %r14, %rsi
callq 0x1718e
cmpq $0x0, (%rsp)
jne 0x8c31
incq %r13
movl %ebp, %r12d
cmpq $0x9, %r13
jne 0x8b23
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x8bb2
movq %rbx, %rdi
callq 0x43b0
addq $0x8, %rbx
jmp 0x8b9f
leaq 0x10(%rsp), %rdi
callq 0x138d0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ee35(%rip), %rdi     # 0x27a07
leaq 0x1eb71(%rip), %rsi     # 0x2774a
leaq 0x1ee45(%rip), %rcx     # 0x27a25
movl $0x330, %edx            # imm = 0x330
callq 0x4160
jmp 0x8c1c
jmp 0x8c1c
jmp 0x8c1c
jmp 0x8c00
jmp 0x8c15
jmp 0x8c1c
jmp 0x8c1c
jmp 0x8c1c
jmp 0x8c00
jmp 0x8c15
jmp 0x8c1c
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x8c1f
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x8c1f
cmpq $0x0, (%rsp)
jne 0x8c31
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x138d0
movq %rbx, %rdi
callq 0x43e0
callq 0x40a0