// File: /workspace/github_cpp_makefile_star2_O3/btbujiangjun[P]abcdl/src/cnn/CNN.cpp
// Function: abcdl::cnn::CNN::train(abcdl::algebra::MatrixSet<float> const&, abcdl::algebra::MatrixSet<float> const&, abcdl::algebra::MatrixSet<float> const&, abcdl::algebra::MatrixSet<float> const&)
void CNN::train(const abcdl::algebra::MatSet& train_data,
                const abcdl::algebra::MatSet& train_label,
                const abcdl::algebra::MatSet& test_data,
                const abcdl::algebra::MatSet& test_label){
	LOG(INFO) << "cnn train starting...";

	CHECK(train_data.size() == train_label.size() &&
       test_data.size() == test_label.size() &&
       train_data.rows() == test_data.rows() &&
       train_data.cols() == test_data.cols());
    if(!check(train_data.rows(), train_data.cols())){
        return;
    }
    
    auto now = []{return std::chrono::system_clock::now();};
    auto time_diff = [](std::chrono::time_point<std::chrono::system_clock> end) -> long long int {
        return static_cast<long long int>((std::chrono::system_clock::now() - end).count());
    };

    size_t num_train_data = train_data.size();
    size_t num_test_data = test_data.size();
    abcdl::utils::Shuffler shuffler(num_train_data);

    for(size_t i = 1; i <= _epoch; i++){
        auto start_time = now();
        shuffler.shuffle();

        for(size_t j = 0; j != num_train_data; j++){
            forward(train_data[shuffler.get(j)]);
            backward(train_label[shuffler.get(j)]);

            if(j % _batch_size  == _batch_size - 1 || j == num_train_data - 1){
                update_gradient(j % _batch_size + 1, _alpha);
            }

            if(j % 100 == 0){
                printf("Epoch[%ld][%ld/%ld]training...\r", i, j, num_train_data);
            }
        }//end per epoch

        printf("Epoch[%ld] train run time: [%lld] ms\n", i, time_diff(start_time));

        auto evaluate_time = now();
	    if(num_test_data > 0){
            printf("Epoch[%ld] [%ld/%ld] predict run time:[%lld] ms\n", i, evaluate(test_data, test_label), num_test_data, time_diff(evaluate_time));
	    }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %r8, 0x28(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbp
leaq 0x2472c(%rip), %rsi     # 0x39fd5
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movl $0x22, %edx
xorl %ecx, %ecx
callq 0x37222
leaq 0x247ce(%rip), %rsi     # 0x3a095
movl $0x15, %edx
movq %rbx, %rdi
callq 0x8350
leaq 0x90(%rsp), %rdi
callq 0x3744a
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq 0x8(%r14), %rsi
subq (%r14), %rsi
cmpq %rsi, %rdx
jne 0x15c09
movq 0x8(%rsp), %rsi
movq (%rsi), %rdx
movq 0x8(%rsi), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
movq 0x28(%rsp), %r9
movq 0x8(%r9), %r8
subq (%r9), %r8
cmpq %r8, %rdi
jne 0x15c09
cmpq %rax, %rcx
je 0x15930
movq (%rax), %rdi
jmp 0x15932
xorl %edi, %edi
cmpq %rdx, %rsi
je 0x1593c
movq (%rdx), %r8
jmp 0x1593f
xorl %r8d, %r8d
cmpq %r8, %rdi
jne 0x15c09
cmpq %rax, %rcx
je 0x15953
movq 0x8(%rax), %rdi
jmp 0x15955
xorl %edi, %edi
cmpq %rdx, %rsi
je 0x15960
movq 0x8(%rdx), %rdx
jmp 0x15962
xorl %edx, %edx
cmpq %rdx, %rdi
jne 0x15c09
cmpq %rax, %rcx
je 0x15979
movq (%rax), %rsi
movq 0x8(%rax), %rdx
jmp 0x1597d
xorl %esi, %esi
xorl %edx, %edx
movq %rbp, %rdi
callq 0x15cae
testb %al, %al
je 0x15bf7
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, 0x68(%rsp)
movq %rcx, %rsi
movq %rax, 0x70(%rsp)
subq %rax, %rsi
sarq $0x3, %rsi
movabsq $-0x3333333333333333, %rbx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rbx, %rsi
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x38(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
movq %rsi, 0x18(%rsp)
callq 0x1564c
cmpq $0x0, (%rbp)
je 0x15bdc
movq 0x30(%rsp), %rax
subq 0x38(%rsp), %rax
sarq $0x3, %rax
imulq %rbx, %rax
movq %rax, 0x60(%rsp)
movq 0x18(%rsp), %rax
decq %rax
movq %rax, 0x88(%rsp)
movl $0x1, %ebx
movq %r13, 0x80(%rsp)
movq %rbx, 0x10(%rsp)
callq 0x8070
movq %rax, 0x78(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x156d4
movq 0x68(%rsp), %rax
cmpq 0x70(%rsp), %rax
je 0x15b5a
xorl %r15d, %r15d
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rdx
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1615c
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x15dc6
movq %rbx, %rdi
callq 0x1840e
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1615c
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x15e30
movq %r14, %r13
movq %rbx, %rdi
callq 0x1840e
movq %rbp, %rbx
movq 0x8(%rbp), %rcx
movq %r15, %rax
xorl %edx, %edx
divq %rcx
decq %rcx
cmpq %rcx, %rdx
setne %al
cmpq 0x88(%rsp), %r15
setne %cl
testb %al, %cl
jne 0x15aff
movq 0x18(%rbx), %r14
movq 0x20(%rbx), %rbp
cmpq %rbp, %r14
je 0x15aff
movq %rdx, %r12
incq %r12
movss 0x10(%rbx), %xmm0
movss %xmm0, 0x24(%rsp)
movq (%r14), %rdi
movq %r12, %rsi
movss 0x24(%rsp), %xmm0
callq 0x162ea
addq $0x8, %r14
cmpq %rbp, %r14
jne 0x15ae5
movq %r15, %rax
movabsq $-0x70a3d70a3d70a3d7, %rcx # imm = 0x8F5C28F5C28F5C29
imulq %rcx, %rax
rorq $0x2, %rax
movabsq $0x28f5c28f5c28f5c, %rcx # imm = 0x28F5C28F5C28F5C
cmpq %rcx, %rax
ja 0x15b3e
leaq 0x24632(%rip), %rdi     # 0x3a15c
movq 0x10(%rsp), %rsi
movq %r15, %rdx
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x8060
incq %r15
cmpq 0x18(%rsp), %r15
movq %r13, %r14
movq 0x80(%rsp), %r13
movq %rbx, %rbp
jne 0x15a47
callq 0x8070
subq 0x78(%rsp), %rax
leaq 0x24611(%rip), %rdi     # 0x3a17c
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x8060
callq 0x8070
movq %rax, %r15
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
je 0x15bcf
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x15fce
movq %rax, %r12
callq 0x8070
subq %r15, %rax
leaq 0x245ed(%rip), %rdi     # 0x3a1a2
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %r12, %rdx
movq 0x60(%rsp), %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x8060
incq %rbx
cmpq (%rbp), %rbx
jbe 0x15a1b
movq 0x48(%rsp), %rdi
cmpq %rdi, 0x50(%rsp)
je 0x15bed
movq %rdi, 0x50(%rsp)
testq %rdi, %rdi
je 0x15bf7
callq 0x8300
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x243c5(%rip), %rsi     # 0x39fd5
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movl $0x27, %edx
movl $0x3, %ecx
callq 0x37222
leaq 0x2447a(%rip), %rsi     # 0x3a0ab
movl $0xb0, %edx
movq %rbx, %rdi
callq 0x8350
leaq 0x90(%rsp), %rdi
callq 0x3744a
movq (%r13), %rax
movq 0x8(%r13), %rcx
jmp 0x1596b
jmp 0x15c5a
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x3744a
jmp 0x15ca6
jmp 0x15c88
jmp 0x15c88
jmp 0x15c74
jmp 0x15c88
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1840e
jmp 0x15c8b
jmp 0x15c88
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %rdi, 0x50(%rsp)
je 0x15c9c
movq %rdi, 0x50(%rsp)
testq %rdi, %rdi
je 0x15ca6
callq 0x8300
movq %rbx, %rdi
callq 0x8500