// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1f8, %rsp            # imm = 0x1F8
movq %r8, 0x20(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r13
leaq 0x1cce6(%rip), %rsi     # 0x30fad
leaq 0x70(%rsp), %rbx
pushq $0x22
popq %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2d5b4
leaq 0x1cd8d(%rip), %rsi     # 0x3106d
movq %rbx, %rdi
callq 0x8300
leaq 0x70(%rsp), %rdi
callq 0x2d7f0
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq 0x8(%r14), %rsi
subq (%r14), %rsi
cmpq %rsi, %rdx
jne 0x1457f
movq 0x8(%rsp), %rsi
movq (%rsi), %rdx
movq 0x8(%rsi), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
movq 0x20(%rsp), %r9
movq 0x8(%r9), %r8
subq (%r9), %r8
cmpq %r8, %rdi
jne 0x1457f
cmpq %rax, %rcx
je 0x14340
movq (%rax), %rdi
jmp 0x14342
xorl %edi, %edi
cmpq %rdx, %rsi
je 0x1434c
movq (%rdx), %r8
jmp 0x1434f
xorl %r8d, %r8d
cmpq %r8, %rdi
jne 0x1457f
cmpq %rax, %rcx
je 0x14363
movq 0x8(%rax), %rdi
jmp 0x14365
xorl %edi, %edi
cmpq %rdx, %rsi
je 0x14370
movq 0x8(%rdx), %rdx
jmp 0x14372
xorl %edx, %edx
cmpq %rdx, %rdi
jne 0x1457f
cmpq %rax, %rcx
je 0x14389
movq (%rax), %rsi
movq 0x8(%rax), %rdx
jmp 0x1438d
xorl %esi, %esi
xorl %edx, %edx
movq %r13, %rdi
callq 0x145fe
testb %al, %al
je 0x1456d
movq %r14, 0x40(%rsp)
movq 0x8(%r15), %rax
movq %r15, 0x48(%rsp)
subq (%r15), %rax
pushq $0x28
popq %rcx
cqto
idivq %rcx
movq %rax, %rbp
movq 0x8(%rsp), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
movq %rdx, 0x38(%rsp)
subq %rdx, %rax
cqto
idivq %rcx
movq %rax, 0x28(%rsp)
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x140c4
leaq -0x1(%rbp), %r14
pushq $0x1
popq %r15
leaq 0x70(%rsp), %rbx
cmpq (%r13), %r15
ja 0x14563
movq %r15, 0x18(%rsp)
callq 0x8060
movq %rax, 0x10(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x1412a
xorl %r15d, %r15d
movq %r15, %rax
xorl %edx, %edx
pushq $0x64
popq %rcx
divq %rcx
cmpq %r15, %rbp
je 0x144e2
movq %rax, %r12
movq 0x58(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
callq 0x14a08
movq %r13, %rdi
movq %rbx, %rsi
callq 0x146fe
movq %rbx, %rdi
callq 0x167fa
movq 0x58(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
callq 0x14a08
movq %r13, %rdi
movq %rbx, %rsi
callq 0x14764
movq %rbx, %rdi
callq 0x167fa
movq 0x8(%r13), %rcx
movq %r15, %rax
xorl %edx, %edx
divq %rcx
decq %rcx
cmpq %rcx, %rdx
setne %al
cmpq %r15, %r14
setne %cl
testb %al, %cl
jne 0x144b8
incq %rdx
movss 0x10(%r13), %xmm0
movq %r13, %rdi
movq %rdx, %rsi
callq 0x14818
imulq $0x64, %r12, %rax
cmpq %r15, %rax
jne 0x144da
leaq 0x1cc6c(%rip), %rdi     # 0x31134
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x8050
incq %r15
jmp 0x1441c
callq 0x8060
subq 0x10(%rsp), %rax
leaq 0x1cc61(%rip), %rdi     # 0x31154
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x8050
callq 0x8060
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
je 0x1455b
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x14858
movq %rax, %r12
callq 0x8060
subq 0x10(%rsp), %rax
leaq 0x1cc39(%rip), %rdi     # 0x3117a
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x8050
incq %r15
jmp 0x143f6
leaq 0x50(%rsp), %rdi
callq 0x14a20
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ca27(%rip), %rsi     # 0x30fad
leaq 0x70(%rsp), %rbx
pushq $0x27
popq %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
callq 0x2d5b4
leaq 0x1cae3(%rip), %rsi     # 0x31083
movq %rbx, %rdi
callq 0x8300
leaq 0x70(%rsp), %rdi
callq 0x2d7f0
movq (%r15), %rax
movq 0x8(%r15), %rcx
jmp 0x1437b
jmp 0x145c0
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x2d7f0
jmp 0x145f5
jmp 0x145e8
jmp 0x145e8
jmp 0x145e8
jmp 0x145d9
jmp 0x145e8
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x167fa
jmp 0x145eb
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x14a20
movq %rbx, %rdi
callq 0x84f0
nop