// File: /workspace/github_cpp_makefile_star2_O2/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 0x82a8
movq %rdi, %rbx
movl (%rax), %ecx
pxor %xmm0, %xmm0
andq $0x0, 0x20(%rsp)
movdqa %xmm0, 0x10(%rsp)
pushq $0x8
popq %rsi
movl %ecx, %eax
cltd
idivl %esi
xorl %edx, %edx
xorl %esi, %esi
cmpq $0x8, %rsi
je 0x809a
movl %edx, 0x30(%rsp,%rsi,4)
incq %rsi
addl %eax, %edx
jmp 0x8089
movl %ecx, 0x50(%rsp)
xorl %ebp, %ebp
leaq 0x2a7c9(%rip), %r14     # 0x32870
leaq 0x8(%rsp), %r15
movq %rsp, %r12
leaq 0x10(%rsp), %r13
cmpq $0x8, %rbp
je 0x8117
andq $0x0, 0x8(%rsp)
pushq $0x18
popq %rdi
callq 0x4250
movq %r14, (%rax)
movq 0x30(%rsp,%rbp,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq %rax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x4200
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x80ff
movq (%rdi), %rax
callq *0x8(%rax)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1470a
movq %r15, %rdi
callq 0x113c8
incq %rbp
jmp 0x80b4
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0x8134
movq %r14, %rdi
callq 0x4390
addq $0x8, %r14
jmp 0x8121
cmpb $0x0, 0x210(%rbx)
je 0x81ee
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x190(%rax)
leaq 0x10(%rsp), %rdi
callq 0x113d6
xorl %ebp, %ebp
pushq $0x18
popq %r15
leaq 0x2a74c(%rip), %r14     # 0x328b0
leaq 0x8(%rsp), %r12
movq %rsp, %r13
cmpq $0x8, %rbp
je 0x81d1
andq $0x0, 0x8(%rsp)
movq %r15, %rdi
callq 0x4250
movq %r14, (%rax)
movq 0x30(%rsp,%rbp,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq %rax, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x4200
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x81b7
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x1470a
movq %r12, %rdi
callq 0x113c8
incq %rbp
jmp 0x816c
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0x81ee
movq %r14, %rdi
callq 0x4390
addq $0x8, %r14
jmp 0x81db
leaq 0x10(%rsp), %rdi
callq 0x113d6
xorl %ebp, %ebp
pushq $0x18
popq %r15
leaq 0x2a6eb(%rip), %r14     # 0x328f0
leaq 0x8(%rsp), %r12
movq %rsp, %r13
cmpq $0x8, %rbp
je 0x8272
andq $0x0, 0x8(%rsp)
movq %r15, %rdi
callq 0x4250
movq %r14, (%rax)
movq 0x30(%rsp,%rbp,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq %rax, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x4200
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8258
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x1470a
movq %r12, %rdi
callq 0x113c8
incq %rbp
jmp 0x820d
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x828f
movq %rbx, %rdi
callq 0x4390
addq $0x8, %rbx
jmp 0x827c
leaq 0x10(%rsp), %rdi
callq 0x113de
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x196e8(%rip), %rdi     # 0x21997
leaq 0x19424(%rip), %rsi     # 0x216da
leaq 0x196f8(%rip), %rcx     # 0x219b5
movl $0x330, %edx            # imm = 0x330
callq 0x4180
jmp 0x82db
jmp 0x82db
jmp 0x82db
jmp 0x82e0
jmp 0x82f4
jmp 0x82db
jmp 0x82db
jmp 0x82e0
jmp 0x82f4
jmp 0x82db
movq %rax, %rbx
jmp 0x8301
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8301
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x8301
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x113c8
leaq 0x10(%rsp), %rdi
callq 0x113de
movq %rbx, %rdi
callq 0x43b0
nop