// File: /workspace/github_cpp_makefile_star2_O1/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 0x244ae(%rip), %rsi     # 0x39fd5
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movl $0x22, %edx
xorl %ecx, %ecx
callq 0x373d6
leaq 0x24550(%rip), %rsi     # 0x3a095
movl $0x15, %edx
movq %rbx, %rdi
callq 0x8350
leaq 0x90(%rsp), %rdi
callq 0x375fe
movq (%r13), %rcx
movq 0x8(%r13), %rsi
movq %rsi, %rax
subq %rcx, %rax
movq 0x8(%r14), %rdx
subq (%r14), %rdx
cmpq %rdx, %rax
jne 0x15e96
movq 0x8(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rdx
movq %rdx, %rdi
subq %rax, %rdi
movq 0x28(%rsp), %r9
movq 0x8(%r9), %r8
subq (%r9), %r8
cmpq %r8, %rdi
jne 0x15e96
cmpq %rcx, %rsi
je 0x15bae
movq (%rcx), %rdi
jmp 0x15bb0
xorl %edi, %edi
cmpq %rax, %rdx
je 0x15bba
movq (%rax), %r8
jmp 0x15bbd
xorl %r8d, %r8d
cmpq %r8, %rdi
jne 0x15e96
cmpq %rcx, %rsi
je 0x15bd1
movq 0x8(%rcx), %rcx
jmp 0x15bd3
xorl %ecx, %ecx
cmpq %rax, %rdx
je 0x15bde
movq 0x8(%rax), %rax
jmp 0x15be0
xorl %eax, %eax
cmpq %rax, %rcx
jne 0x15e96
movq (%r13), %rax
movq 0x8(%r13), %rcx
cmpq %rax, %rcx
je 0x15bfb
movq (%rax), %rsi
jmp 0x15bfd
xorl %esi, %esi
cmpq %rax, %rcx
je 0x15c08
movq 0x8(%rax), %rdx
jmp 0x15c0a
xorl %edx, %edx
movq %rbp, %rdi
callq 0x15f34
testb %al, %al
je 0x15e84
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 0x158d0
cmpq $0x0, (%rbp)
je 0x15e69
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 0x15958
movq 0x68(%rsp), %rax
cmpq 0x70(%rsp), %rax
je 0x15de7
xorl %r15d, %r15d
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rdx
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x163e2
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x1604c
movq %rbx, %rdi
callq 0x18696
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x163e2
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x160b6
movq %r14, %r13
movq %rbx, %rdi
callq 0x18696
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 0x15d8c
movq 0x18(%rbx), %r14
movq 0x20(%rbx), %rbp
cmpq %rbp, %r14
je 0x15d8c
movq %rdx, %r12
incq %r12
movss 0x10(%rbx), %xmm0
movss %xmm0, 0x24(%rsp)
movq (%r14), %rdi
movq %r12, %rsi
movss 0x24(%rsp), %xmm0
callq 0x16570
addq $0x8, %r14
cmpq %rbp, %r14
jne 0x15d72
movq %r15, %rax
movabsq $-0x70a3d70a3d70a3d7, %rcx # imm = 0x8F5C28F5C28F5C29
imulq %rcx, %rax
rorq $0x2, %rax
movabsq $0x28f5c28f5c28f5c, %rcx # imm = 0x28F5C28F5C28F5C
cmpq %rcx, %rax
ja 0x15dcb
leaq 0x243a5(%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 0x15cd4
callq 0x8070
subq 0x78(%rsp), %rax
leaq 0x24384(%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 0x15e5c
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x16254
movq %rax, %r12
callq 0x8070
subq %r15, %rax
leaq 0x24360(%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 0x15ca8
movq 0x48(%rsp), %rdi
cmpq %rdi, 0x50(%rsp)
je 0x15e7a
movq %rdi, 0x50(%rsp)
testq %rdi, %rdi
je 0x15e84
callq 0x8300
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24138(%rip), %rsi     # 0x39fd5
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movl $0x27, %edx
movl $0x3, %ecx
callq 0x373d6
leaq 0x241ed(%rip), %rsi     # 0x3a0ab
movl $0xb0, %edx
movq %rbx, %rdi
callq 0x8350
leaq 0x90(%rsp), %rdi
callq 0x375fe
jmp 0x15be9
jmp 0x15edf
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x375fe
jmp 0x15f2b
jmp 0x15f0d
jmp 0x15f0d
jmp 0x15ef9
jmp 0x15f0d
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x18696
jmp 0x15f10
jmp 0x15f0d
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %rdi, 0x50(%rsp)
je 0x15f21
movq %rdi, 0x50(%rsp)
testq %rdi, %rdi
je 0x15f2b
callq 0x8300
movq %rbx, %rdi
callq 0x8500
nop