// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::ComputeCostInOutputLayerForCurrentSample(int)
void ConstructLayer::ComputeCostInOutputLayerForCurrentSample(int sample_index)
{
    if(__type != LayerType::output)
    {
        std::cout<<"Error: ComputeCostInOutputLayerForCurrentSample() only works for output layer."
            <<std::endl;
        std::exit(0);
    }

    // --- 2) compute the cost function C(a_i, y_i)
    Images & sample_image = __imageA[sample_index]; // output layer drop out is not used for sure, so use __imageA is OK.
    assert(sample_image.GetNumberOfKernels() == 1); // output layer must be a fully connected layer, so one kernel
    // now get a_i
    Matrix & sample_A = sample_image.OutputImageFromKernel[0];
    assert(sample_A.Dimension().second == 1); // must be a collum matrix
    //cout<<sample_A<<endl;

    //assert(sample_number >= 1); // obsolete
    //Matrix sample_label = (__p_data_interface->GetCurrentBatchLabel())[sample_number-1];
    Matrix & sample_label = (__p_data_interface->GetCurrentBatchLabel())[sample_index];
    assert(sample_label.Dimension()  == sample_A.Dimension());

    double cost = 0.;
    if(__cost_func_type == CostFuncType::cross_entropy)
    {
        cost = cross_entropy(sample_A, sample_label);
    }
    else if(__cost_func_type == CostFuncType::log_likelihood)
    {
        cost = log_likelihood(sample_A, sample_label);
    }
    else if(__cost_func_type == CostFuncType::quadratic_sum)
    {
        cost = quadratic_sum(sample_A, sample_label);
    }
    else {
        std::cout<<"Error: cost function only supports cross_entropy, loglikelihood, quadratic_sum"
            <<std::endl;
        exit(0);
    }
    // push cost for current sample to memory
    //__outputLayerCost.push_back(cost);
    __outputLayerCost[sample_index]= cost;
    //cout<<"sample cost: "<<cost<<endl;
    //getchar();


    // --- 3) then calculate delta: delta = delta(a_i, y_i) for this sample
    // -------- please note: this function only calculate \delta for current sample, 
    // ----------- when doing back propagation for hidden layers, only delta with the same sample_index should be updated 
    // ----------- the overall delta for this batch should be an average of all deltas in this batch
    // ----------- this is due to the characteristic of Cost function, which is an averaged sum over all samples

    Matrix delta = Matrix(sample_A.Dimension());
    if(__cost_func_type == CostFuncType::cross_entropy)
    {
        delta = sample_A - sample_label; // softmax and sigmoid all have this form
    }
    else if(__cost_func_type == CostFuncType::log_likelihood)
    {
        delta = sample_A - sample_label;
    }
    else if(__cost_func_type == CostFuncType::quadratic_sum)
    {
        delta = sample_A - sample_label; // this is a place holder, not used
    }
    else {
        std::cout<<"Error: cost function only supports cross_entropy, loglikelihood, quadratic_sum"
            <<std::endl;
        exit(0);
    }
    // push cost for current sample to memory
    Images images_delta_from_current_sample;
    images_delta_from_current_sample.OutputImageFromKernel.push_back(delta); // only one kernel in fc layer
    __imageDelta[sample_index]=images_delta_from_current_sample;
    __imageDeltaFull[sample_index]=images_delta_from_current_sample; // in output layer, dropout is not used for sure
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpl $0x4, 0xc(%rdi)
jne 0xa180
movq %rdi, %rbx
movslq %esi, %rcx
movq 0x138(%rdi), %rax
imulq $0x18, %rcx, %r14
movq (%rax,%r14), %r15
movq 0x8(%rax,%r14), %rax
subq %r15, %rax
cmpq $0x18, %rax
jne 0xa190
movq %rcx, 0x48(%rsp)
movq %r15, %rdi
callq 0x17a6a
cmpq $0x1, %rdx
jne 0xa1af
movq 0x400(%rbx), %rax
movq 0x28(%rax), %r12
addq %r14, %r12
movq %r12, %rdi
callq 0x17a6a
movq %rax, %r13
movq %rdx, %rbp
movq %r15, %rdi
callq 0x17a6a
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x175e8
testb %al, %al
je 0xa1ce
movl 0x3e8(%rbx), %eax
cmpl $0x2, %eax
je 0x9fa4
xorpd %xmm0, %xmm0
cmpl $0x1, %eax
je 0xa070
testl %eax, %eax
jne 0xa1ed
movq %r15, %rdi
callq 0x17a6a
movq %rax, %r13
movq %rdx, %rbp
movq %r12, %rdi
callq 0x17a6a
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x175e8
testb %al, %al
je 0xa24b
cmpq $0x1, %rbp
jne 0xa289
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
cmpq %rbp, %r13
je 0xa070
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x17c46
movq (%rax), %rax
movsd 0x17621(%rip), %xmm0    # 0x21548
ucomisd (%rax), %xmm0
jae 0x9f8e
movq %r15, %rdi
movq %rbp, %rsi
callq 0x17c46
movq (%rax), %rax
movsd (%rax), %xmm0
ucomisd 0x17609(%rip), %xmm0    # 0x21550
jae 0x9f8e
movq %r12, %rdi
movq %rbp, %rsi
callq 0x17c46
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x17c46
movq (%rax), %rax
movsd (%rax), %xmm0
callq 0x41d0
mulsd 0x8(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
jmp 0x9f9c
movsd 0x28(%rsp), %xmm0
addsd 0x17584(%rip), %xmm0    # 0x21520
incq %rbp
jmp 0x9f02
movq %r15, %rdi
callq 0x17a6a
movq %rax, %r13
movq %rdx, %rbp
movq %r12, %rdi
callq 0x17a6a
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x175e8
testb %al, %al
je 0xa22c
cmpq $0x1, %rbp
jne 0xa26a
xorpd %xmm0, %xmm0
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0xa070
movq %r15, %rdi
movq %rbp, %rsi
movsd %xmm0, 0x28(%rsp)
callq 0x17c46
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x17c46
movq (%rax), %rax
movsd 0x8(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x17c46
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x17c46
movsd 0x28(%rsp), %xmm0
movq (%rax), %rax
movsd 0x50(%rsp), %xmm1
subsd (%rax), %xmm1
mulsd 0x8(%rsp), %xmm1
addsd %xmm1, %xmm0
incq %rbp
jmp 0x9fe3
movq 0x1f8(%rbx), %rax
movq 0x48(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq %r15, %rdi
callq 0x17a6a
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x1776a
movl 0x3e8(%rbx), %eax
cmpl $0x2, %eax
je 0xa0f0
cmpl $0x1, %eax
je 0xa0cf
testl %eax, %eax
jne 0xa20f
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x17eb0
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x125b4
jmp 0xa10f
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x17eb0
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x125b4
jmp 0xa10f
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x17eb0
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x125b4
leaq 0x10(%rsp), %rdi
callq 0x17a9a
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x30(%rsp), %rsi
callq 0x1139c
movq 0x180(%rbx), %rdi
addq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x11a9e
addq 0x1e0(%rbx), %r14
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x11a9e
leaq 0x10(%rsp), %rdi
callq 0x110e4
leaq 0x30(%rsp), %rdi
callq 0x17a9a
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28e51(%rip), %rdi     # 0x32fd8
leaq 0x17930(%rip), %rsi     # 0x21abe
jmp 0xa1fb
leaq 0x17976(%rip), %rdi     # 0x21b0d
leaq 0x1753c(%rip), %rsi     # 0x216da
leaq 0x1798f(%rip), %rcx     # 0x21b34
movl $0x591, %edx            # imm = 0x591
callq 0x4180
leaq 0x179c9(%rip), %rdi     # 0x21b7f
leaq 0x1751d(%rip), %rsi     # 0x216da
leaq 0x17970(%rip), %rcx     # 0x21b34
movl $0x594, %edx            # imm = 0x594
callq 0x4180
leaq 0x179cb(%rip), %rdi     # 0x21ba0
leaq 0x174fe(%rip), %rsi     # 0x216da
leaq 0x17951(%rip), %rcx     # 0x21b34
movl $0x59a, %edx            # imm = 0x59A
callq 0x4180
movq 0x28de4(%rip), %rdi     # 0x32fd8
leaq 0x179d6(%rip), %rsi     # 0x21bd1
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260
movq 0x28dc2(%rip), %rdi     # 0x32fd8
leaq 0x179b4(%rip), %rsi     # 0x21bd1
callq 0x4240
movq %rax, %rdi
callq 0x4130
jmp 0xa208
leaq 0x192d4(%rip), %rdi     # 0x23507
leaq 0x174a0(%rip), %rsi     # 0x216da
leaq 0x19304(%rip), %rcx     # 0x23545
movl $0x579, %edx            # imm = 0x579
callq 0x4180
leaq 0x192b5(%rip), %rdi     # 0x23507
leaq 0x17481(%rip), %rsi     # 0x216da
leaq 0x192bc(%rip), %rcx     # 0x2351c
movl $0x55a, %edx            # imm = 0x55A
callq 0x4180
leaq 0x17d9a(%rip), %rdi     # 0x2200b
leaq 0x17462(%rip), %rsi     # 0x216da
leaq 0x192c6(%rip), %rcx     # 0x23545
movl $0x57a, %edx            # imm = 0x57A
callq 0x4180
leaq 0x17d7b(%rip), %rdi     # 0x2200b
leaq 0x17443(%rip), %rsi     # 0x216da
leaq 0x1927e(%rip), %rcx     # 0x2351c
movl $0x55b, %edx            # imm = 0x55B
callq 0x4180
jmp 0xa2c1
jmp 0xa2ae
jmp 0xa2ae
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x17a9a
jmp 0xa2d3
jmp 0xa2c1
jmp 0xa2c1
movq %rax, %rbx
jmp 0xa2d3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x110e4
leaq 0x30(%rsp), %rdi
callq 0x17a9a
movq %rbx, %rdi
callq 0x43b0
nop