// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::BatchNormalization_UpdateZ_2DLayer()
void ConstructLayer::BatchNormalization_UpdateZ_2DLayer()
{
    // 1) get mu and sigma_square
    // part I) active
    Images mu, sigma_square;
    GetMu_BFor2DLayer(__imageZ, mu);
    GetSigmaSquareFor2DLayer(__imageZ, mu, sigma_square);
    // part II) full
    Images mu_full, sigma_square_full;
    GetMu_BFor2DLayer(__imageZFull, mu_full);
    GetSigmaSquareFor2DLayer(__imageZFull, mu_full, sigma_square_full);

    // 2) save mu and sigma square
    // part I) active
    __imageAverage_BN[0] = mu;
    __imageVariance_BN[0] = sigma_square;
    // part II) full
    __imageAverageFull_BN[0] = mu_full;
    __imageVarianceFull_BN[0] = sigma_square_full;

    // 3) get normalized \hat{x} and save it to __imageZ_BN
    size_t batch_size = __imageZ.size();
    double epsilon = 1e-8; // avoid divide by zero issue; a hard-coded parameter from algorithm
    size_t nKernel = __imageZ[0].OutputImageFromKernel.size();
    // for parallelization
    std::vector<std::thread> vth;
    int Range[NTHREAD+1];
    for(int i=0;i<NTHREAD;i++)
        Range[i] = batch_size/NTHREAD * i;
    Range[NTHREAD] = batch_size;

    /*
    for(size_t k=0;k<nKernel;k++)
    {
        // part I) active mu and sigma matrix
        Matrix & average_m_active = mu.OutputImageFromKernel[k];
        Matrix variance_m_active = sigma_square.OutputImageFromKernel[k];
        variance_m_active = variance_m_active + epsilon;
        variance_m_active(sqrt);
        // part II) full mu and sigma matrix
        Matrix & average_m_full = mu_full.OutputImageFromKernel[k];
        Matrix variance_m_full = sigma_square_full.OutputImageFromKernel[k];
        variance_m_full = variance_m_full + epsilon;
        variance_m_full(sqrt);

        // since one kernel corresponds to one pair of (\gamma, \beta)
        // so matrix average_m_active... etc must have dimension(1, 1)
        assert(average_m_active.Dimension().first == 1);
        assert(average_m_active.Dimension().second == 1);
        double _fMu_active = average_m_active[0][0];
        double _fVariance_active = variance_m_active[0][0];
        double _fMu_full = average_m_full[0][0];
        double _fVariance_full = variance_m_full[0][0];

        for(size_t i=0;i<batch_size;i++) // TO-DO: need to parallize
        {
            // part I) for active image
            Matrix m = __imageZ[i].OutputImageFromKernel[k]; // using copy assignment
            m = m - _fMu_active;
            m = m / _fVariance_active;
            __imageZ_BN[i].OutputImageFromKernel.push_back(m);

            // part II) for full image
            Matrix m_full = __imageZFull[i].OutputImageFromKernel[k];
            m_full = m_full - _fMu_full;
            m_full = m_full / _fVariance_full;
            __imageZFull_BN[i].OutputImageFromKernel.push_back(m_full);
        }
    }
    */

    // parallelize
    auto fill_z = [&](size_t start, size_t end)
    {
        for(size_t k=0;k<nKernel;k++)
        {
            // part I) active mu and sigma matrix
            Matrix & average_m_active = mu.OutputImageFromKernel[k];
            Matrix variance_m_active = sigma_square.OutputImageFromKernel[k];
            variance_m_active = variance_m_active + epsilon;
            variance_m_active(sqrt);
            // part II) full mu and sigma matrix
            Matrix & average_m_full = mu_full.OutputImageFromKernel[k];
            Matrix variance_m_full = sigma_square_full.OutputImageFromKernel[k];
            variance_m_full = variance_m_full + epsilon;
            variance_m_full(sqrt);

            // since one kernel corresponds to one pair of (\gamma, \beta)
            // so matrix average_m_active... etc must have dimension(1, 1)
            assert(average_m_active.Dimension().first == 1);
            assert(average_m_active.Dimension().second == 1);
            double _fMu_active = average_m_active[0][0];
            double _fVariance_active = variance_m_active[0][0];
            double _fMu_full = average_m_full[0][0];
            double _fVariance_full = variance_m_full[0][0];

            for(size_t i=start;i<end;i++) // TO-DO: need to parallize
            {
                // part I) for active image
                Matrix m = __imageZ[i].OutputImageFromKernel[k]; // using copy assignment
                m = m - _fMu_active;
                m = m / _fVariance_active;
                __imageZ_BN[i].OutputImageFromKernel.push_back(m);

                // part II) for full image
                Matrix m_full = __imageZFull[i].OutputImageFromKernel[k];
                m_full = m_full - _fMu_full;
                m_full = m_full / _fVariance_full;
                __imageZFull_BN[i].OutputImageFromKernel.push_back(m_full);
            }
        }
    };

#ifdef MULTI_THREAD
    vth.clear();
    for(int n = 0;n<NTHREAD;n++)
        vth.push_back(std::thread(fill_z, Range[n], Range[n+1]));
    for(auto &th: vth)
        th.join();
#else
    fill_z(0, batch_size);
#endif

    // 4) affine transformation to get y
    assert(__gamma_BN.size() == nKernel);
    assert(__beta_BN.size() == nKernel);

    /*
    for(size_t i = 0; i<batch_size;i++) // To-Do: need to parallize
    {
        for(size_t k=0;k<nKernel;k++)
        {
            double gamma = (__gamma_BN[k])[0][0];
            double beta = (__beta_BN[k])[0][0];
            // part I) active 
            Matrix m = __imageZ_BN[i].OutputImageFromKernel[k] * gamma;
            m = m + beta;
            __imageY_BN[i].OutputImageFromKernel.push_back(m);

            // part II) full
            m = __imageZFull_BN[i].OutputImageFromKernel[k] * gamma;
            m = m + beta;
            __imageYFull_BN[i].OutputImageFromKernel.push_back(m);
        }
    }
    */

    // parallelize
    auto fill_y = [&](size_t start, size_t end)
    {
        for(size_t i = start; i<end;i++) // To-Do: need to parallize
        {
            for(size_t k=0;k<nKernel;k++)
            {
                double gamma = (__gamma_BN[k])[0][0];
                double beta = (__beta_BN[k])[0][0];
                // part I) active 
                Matrix m = __imageZ_BN[i].OutputImageFromKernel[k] * gamma;
                m = m + beta;
                __imageY_BN[i].OutputImageFromKernel.push_back(m);

                // part II) full
                m = __imageZFull_BN[i].OutputImageFromKernel[k] * gamma;
                m = m + beta;
                __imageYFull_BN[i].OutputImageFromKernel.push_back(m);
            }
        }
    };
    vth.clear();
    for(int i=0;i<NTHREAD;i++)
        vth.push_back(std::thread(fill_y, Range[i], Range[i+1]));
    for(auto &i: vth) i.join();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rbx
leaq 0x90(%rsp), %rdx
andq $0x0, 0x10(%rdx)
pxor %xmm0, %xmm0
andq $0x0, 0x60(%rsp)
movdqa %xmm0, (%rdx)
movdqa %xmm0, 0x50(%rsp)
leaq 0x150(%rdi), %r14
movq (%rdi), %rax
movq %r14, %rsi
callq *0x1b0(%rax)
movq (%rbx), %rax
leaq 0x90(%rsp), %rdx
leaq 0x50(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x1c0(%rax)
leaq 0x70(%rsp), %rdx
andq $0x0, 0x10(%rdx)
pxor %xmm0, %xmm0
andq $0x0, 0x40(%rsp)
movdqa %xmm0, (%rdx)
movdqa %xmm0, 0x30(%rsp)
leaq 0x1b0(%rbx), %r14
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x1b0(%rax)
movq (%rbx), %rax
leaq 0x70(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x1c0(%rax)
movq 0x278(%rbx), %rdi
leaq 0x90(%rsp), %rsi
callq 0x11a9e
movq 0x2a8(%rbx), %rdi
leaq 0x50(%rsp), %rsi
callq 0x11a9e
movq 0x290(%rbx), %rdi
leaq 0x70(%rsp), %rsi
callq 0x11a9e
movq 0x2c0(%rbx), %rdi
leaq 0x30(%rsp), %rsi
callq 0x11a9e
movq 0x150(%rbx), %rsi
movq 0x158(%rbx), %rax
subq %rsi, %rax
pushq $0x18
popq %rdi
cqto
idivq %rdi
movq %rax, %rcx
movabsq $0x3e45798ee2308c3a, %rax # imm = 0x3E45798EE2308C3A
movq %rax, 0xa8(%rsp)
movq 0x8(%rsi), %rax
subq (%rsi), %rax
cqto
idivq %rdi
movq %rax, 0x28(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq %rcx, %rax
shrq $0x3, %rax
xorl %edx, %edx
xorl %esi, %esi
cmpq $0x8, %rsi
je 0xb74a
movl %edx, 0xb0(%rsp,%rsi,4)
incq %rsi
addl %eax, %edx
jmp 0xb736
movl %ecx, 0xd0(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x113d6
xorl %r13d, %r13d
pushq $0x48
popq %r14
leaq 0xa8(%rsp), %rbp
leaq 0x8(%rsp), %r15
movq %rsp, %r12
cmpq $0x8, %r13
je 0xb81d
andq $0x0, 0x8(%rsp)
movq %r14, %rdi
callq 0x4250
leaq 0x2729f(%rip), %rcx     # 0x32a30
movq %rcx, (%rax)
movq 0xb0(%rsp,%r13,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
leaq 0x28(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x90(%rsp), %rcx
movq %rcx, 0x18(%rax)
leaq 0x50(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq %rbp, 0x28(%rax)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x30(%rax)
leaq 0x30(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq %rbx, 0x40(%rax)
movq %rax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x4200
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb800
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x1470a
movq %r15, %rdi
callq 0x113c8
incq %r13
jmp 0xb772
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0xb83a
movq %r14, %rdi
callq 0x4390
addq $0x8, %r14
jmp 0xb827
movq 0x310(%rbx), %rcx
subq 0x308(%rbx), %rcx
movq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
cmpq 0x28(%rsp), %rax
jne 0xb969
movq 0x328(%rbx), %rax
subq 0x320(%rbx), %rax
cmpq %rcx, %rax
jne 0xb988
leaq 0x10(%rsp), %rdi
callq 0x113d6
xorl %ebp, %ebp
pushq $0x20
popq %r15
leaq 0x271e4(%rip), %r14     # 0x32a70
leaq 0x8(%rsp), %r12
movq %rsp, %r13
cmpq $0x8, %rbp
je 0xb905
andq $0x0, 0x8(%rsp)
movq %r15, %rdi
callq 0x4250
movq %r14, (%rax)
movq 0xb0(%rsp,%rbp,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
leaq 0x28(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rbx, 0x18(%rax)
movq %rax, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x4200
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb8eb
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x1470a
movq %r12, %rdi
callq 0x113c8
incq %rbp
jmp 0xb894
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0xb922
movq %rbx, %rdi
callq 0x4390
addq $0x8, %rbx
jmp 0xb90f
leaq 0x10(%rsp), %rdi
callq 0x113de
leaq 0x30(%rsp), %rdi
callq 0x110e4
leaq 0x70(%rsp), %rdi
callq 0x110e4
leaq 0x50(%rsp), %rdi
callq 0x110e4
leaq 0x90(%rsp), %rdi
callq 0x110e4
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16748(%rip), %rdi     # 0x220b8
leaq 0x15d63(%rip), %rsi     # 0x216da
leaq 0x16757(%rip), %rcx     # 0x220d5
movl $0x8fe, %edx            # imm = 0x8FE
callq 0x4180
leaq 0x16788(%rip), %rdi     # 0x22117
leaq 0x15d44(%rip), %rsi     # 0x216da
leaq 0x16738(%rip), %rcx     # 0x220d5
movl $0x8ff, %edx            # imm = 0x8FF
callq 0x4180
movq %rax, %rbx
jmp 0xb9ff
movq %rax, %rbx
jmp 0xb9eb
jmp 0xb9bb
jmp 0xb9bb
jmp 0xb9c0
jmp 0xb9d4
jmp 0xb9bb
movq %rax, %rbx
jmp 0xb9e1
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb9e1
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xb9e1
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x113c8
leaq 0x10(%rsp), %rdi
callq 0x113de
leaq 0x30(%rsp), %rdi
callq 0x110e4
leaq 0x70(%rsp), %rdi
callq 0x110e4
leaq 0x50(%rsp), %rdi
callq 0x110e4
leaq 0x90(%rsp), %rdi
callq 0x110e4
movq %rbx, %rdi
callq 0x43b0