// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/trainhmm.cpp
// Function: cross_validate(double*, char const*, char const*, long*, long*, _IO_FILE*)
NUMBER cross_validate(NUMBER* metrics, const char *filename, const char *model_file_name, clock_t *tm_fit, clock_t *tm_predict, FILE *fid_console) {//SEQ
//NUMBER cross_validate(NUMBER* metrics, const char *filename, const char *model_file_name, double *tm_fit, double *tm_predict, FILE *fid_console) {//PAR
    clock_t tm0;//SEQ
//    double _tm0;//PAR
    char *ch;
    NPAR f;
    NCAT g,k;
    FILE *fid = NULL; // file for storing prediction should that be necessary
    FILE *fid_folds = NULL; // file for reading/writing folds
    if(param.predictions>0) {  // if we have to write the predictions file
        fid = fopen(filename,"w");
        if(fid == NULL)
        {
            fprintf(stderr, "Can't write output model file %s\n",filename);
            if(param.duplicate_console==1) fprintf(fid_console, "Can't write output model file %s\n",filename);
            exit(1);
        }
    }
    // produce folds
    NPAR *folds = Calloc(NPAR, (size_t)param.nG);//[param.nG];
    srand ( (unsigned int)time(NULL) );
    // folds file
    if(param.cv_folds_file[0] > 0) { // file is specified
        if(param.cv_inout_flag=='i') {
            fid_folds = fopen(param.cv_folds_file,"r");
            if(fid_folds == NULL)
            {
                fprintf(stderr, "Can't open folds file for reading %s\n",filename);
                if(param.duplicate_console==1) fprintf(fid_console, "Can't open folds file for reading %s\n",filename);
                exit(1);
            }
            max_line_length = 1024;
            line = (char *)malloc((size_t)max_line_length);// Malloc(char,max_line_length);
        } else if(param.cv_inout_flag=='o') {
            fid_folds = fopen(param.cv_folds_file,"w");
            if(fid_folds == NULL)
            {
                fprintf(stderr, "Can't open folds file for writing %s\n",filename);
                if(param.duplicate_console==1) fprintf(fid_console, "Can't open folds file for writing %s\n",filename);
                exit(1);
            }
        }
    }
    for(g=0; g<param.nG; g++) {
        if( param.cv_folds_file[0]==0 || (param.cv_folds_file[0] > 0 && param.cv_inout_flag=='o') ) { // output or default
            folds[g] = (NPAR)(rand() % param.cv_folds);
            if(param.cv_folds_file[0] > 0 )
                fprintf(fid_folds,"%i\n",folds[g]); // write out
        } else if( (param.cv_folds_file[0] > 0 && param.cv_inout_flag=='i') ) {
            readline(fid_folds);//
            ch = strtok(line,"\t\n\r");
            if(ch == NULL) {
                fprintf(stderr, "Error reading input folds file (potentialy wrong number of rows)\n");
                if(param.duplicate_console==1) fprintf(fid_console, "Error reading input folds file (potentialy wrong number of rows)\n");
                exit(1);
            }
            folds[g] = (NPAR)(atoi(ch));
        }
    }
    if(param.cv_folds_file[0] > 0) { // file is specified
        fclose(fid_folds);
        if(param.cv_inout_flag=='i')
            free(line);
    }
	param.metrics_target_obs = param.cv_target_obs;

    
    // create and fit multiple problems
    HMMProblem* hmms[param.cv_folds];
    int q = param.quiet;
//    param.quiet = 1;
    for(f=0; f<param.cv_folds; f++) {
        switch(param.structure)
        {
            case STRUCTURE_SKILL: // Conjugate Gradient Descent
//            case STRUCTURE_GROUP: // Conjugate Gradient Descent
                hmms[f] = new HMMProblem(&param);
                break;
            case STRUCTURE_SKABslc: // Conjugate Gradient Descent
                hmms[f] = new HMMProblemSlicedAB(&param);
                break;
            case STRUCTURE_SKAslc: // Conjugate Gradient Descent
                hmms[f] = new HMMProblemSlicedA(&param);
                break;
//            case STRUCTURE_PIg: // Gradient Descent: PI by group, A,B by skill
//                hmm = new HMMProblemPiG(&param);
//                break;
            case STRUCTURE_PIgk: // Gradient Descent, pLo=f(K,G), other by K
                hmms[f] = new HMMProblemPiGK(&param);
                break;
            case STRUCTURE_PIgkww: // Gradient Descent, pLo=f(K,G), other by K
                hmms[f] = new HMMProblemPiGKww(&param);
                break;
            case STRUCTURE_PIAgk: // Gradient Descent, pLo=f(K,G), pT=f(K,G), other by K
                hmms[f] = new HMMProblemPiAGK(&param);
                break;
            case STRUCTURE_Agk: // Gradient Descent, pT=f(K,G), other by K
                hmms[f] = new HMMProblemAGK(&param);
                break;
            case STRUCTURE_PIABgk: // Gradient Descent, pT=f(K,G), other by K
                hmms[f] = new HMMProblemPiABGK(&param);
                break;
//            case BKT_GD_T: // Gradient Descent with Transfer
//                hmm = new HMMProblemKT(&param);
//                break;
        }
        // block respective data - do not fit the data belonging to the fold
        for(g=0; g<param.nG; g++) // for all groups
            if(folds[g]==f) { // if in current fold
                for(k=0; k<param.g_numk[g]; k++) // for all skills in it
                    param.g_k_data[g][k]->cnt = 1; // block it
            }
        // block nulls
        for(NCAT x=0; x<param.n_null_skill_group; x++) {
            if( param.null_skills[x].g == f)
                param.null_skills[x].cnt = 1;
        }

        // now compute
        tm0 = clock(); //SEQ
//        _tm0 = omp_get_wtime(); //PAR
        hmms[f]->fit();
        *(tm_fit) += (clock_t)(clock()- tm0);//SEQ
//        *(tm_fit) += omp_get_wtime()-_tm0;//PAR
        
        // write model
        char fname[1024];
        sprintf(fname,"%s_%i",model_file_name,f);
        hmms[f]->toFile(fname);
        
        // UN-block respective data
        for(g=0; g<param.nG; g++) // for all groups
            if(folds[g]==f) { // if in current fold
                for(k=0; k<param.g_numk[g]; k++) // for all skills in it
                    param.g_k_data[g][k]->cnt = 0; // UN-block it
            }
        // UN-block nulls
        for(NCAT x=0; x<param.n_null_skill_group; x++) {
            if( param.null_skills[x].g == f)
                param.null_skills[x].cnt = 0;
        }
//        if(q == 0) {
            printf("fold %d is done\n",f+1);
            if(param.duplicate_console==1) fprintf(fid_console,"fold %d is done\n",f+1);
//        }
    }
    param.quiet = (NPAR)q;
    
    tm0 = clock();//SEQ
//    _tm0 = omp_get_wtime();//PAR
	
	// new prediction
	//		create a general fold-identifying array
	NPAR *dat_fold = Calloc(NPAR, param.N);
	for(NDAT t=0; t<param.N; t++) dat_fold[t] = folds[ param.dat_group[t] ];
	//		predict
	hmms[0]->predict(metrics, filename, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix, hmms, param.cv_folds/*nhmms*/, dat_fold);
	
    printf("Cross-validation is done wrong, if A or B matrices are 'sliced'\n");
    
//    if(param.structure==STRUCTURE_SKAslc) {
//        ((HMMProblemSlicedA *)hmm)->predict(metrics, predict_file, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix);
//    } else if(param.structure==STRUCTURE_SKABslc) {
//        ((HMMProblemSlicedAB *)hmm)->predict(metrics, predict_file, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix);
//    } else {
//        HMMProblem::predict(metrics, predict_file, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix, &hmm, 1, NULL);
//    }
    
    free(dat_fold);
	
	*(tm_predict) += (clock_t)(clock()- tm0);//SEQ
//    *(tm_predict) += omp_get_wtime()-_tm0;//PAR
	
	
    // delete problems
    NUMBER n_par = 0;
    for(f=0; f<param.cv_folds; f++) {
        n_par += hmms[f]->getNparams();
        delete hmms[f];
    }
    n_par /= param.cv_folds;
	
    free(folds);
	
	return (n_par);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %r9, -0x38(%rbp)
movq %r8, %r15
movq %rcx, -0x40(%rbp)
movq %rdx, -0x60(%rbp)
movq %rsi, %r12
movq %rdi, %r13
cmpl $0x0, 0x3cb0c(%rip)     # 0x45308
jle 0x8816
leaq 0x2e9cb(%rip), %rsi     # 0x371d0
movq %r12, %rdi
callq 0x42c0
testq %rax, %rax
je 0x8df1
movslq 0x3d347(%rip), %rdi     # 0x45b64
movl $0x1, %esi
callq 0x4140
movq %rax, %rbx
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x4200
movl %eax, %edi
callq 0x4220
cmpb $0x0, 0x3ced5(%rip)     # 0x45717
jle 0x88bb
movzbl 0x3d2cc(%rip), %eax     # 0x45b17
cmpl $0x6f, %eax
je 0x8891
cmpl $0x69, %eax
jne 0x88b8
leaq 0x3cebb(%rip), %rdi     # 0x45717
leaq 0x2f445(%rip), %rsi     # 0x37ca8
callq 0x42c0
testq %rax, %rax
je 0x8dfd
movq %rax, %r14
movl $0x400, 0x3d3d2(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x4330
movq %rax, 0x3d3c9(%rip)     # 0x45c58
jmp 0x88bb
leaq 0x3ce7f(%rip), %rdi     # 0x45717
leaq 0x2e931(%rip), %rsi     # 0x371d0
callq 0x42c0
movq %rax, %r14
testq %rax, %rax
jne 0x88bb
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0x4dea
xorl %r14d, %r14d
movq %r13, -0x48(%rbp)
movq %r12, -0x50(%rbp)
movq %r15, -0x58(%rbp)
cmpl $0x0, 0x3d296(%rip)     # 0x45b64
jle 0x8984
leaq 0x3093b(%rip), %r15     # 0x39216
leaq 0x2ea56(%rip), %r12     # 0x37338
xorl %r13d, %r13d
movb 0x3ce2c(%rip), %al      # 0x45717
testb %al, %al
je 0x8908
setg %dl
movb 0x3d21f(%rip), %cl      # 0x45b17
cmpb $0x6f, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x8933
callq 0x40b0
movsbl 0x3ce00(%rip), %ecx     # 0x45714
cltd
idivl %ecx
movb %dl, (%rbx,%r13)
cmpb $0x0, 0x3cdf5(%rip)     # 0x45717
jle 0x8971
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x4360
jmp 0x8971
testb %al, %al
setg %al
cmpb $0x69, %cl
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x8971
movq %r14, %rdi
callq 0x9a6a
movq 0x3d304(%rip), %rdi     # 0x45c58
movq %r12, %rsi
callq 0x43e0
testq %rax, %rax
je 0x8de8
movq %rax, %rdi
callq 0x43d0
movb %al, (%rbx,%r13)
incq %r13
movslq 0x3d1e9(%rip), %rax     # 0x45b64
cmpq %rax, %r13
jl 0x88e5
cmpb $0x0, 0x3cd8c(%rip)     # 0x45717
jle 0x89aa
movq %r14, %rdi
callq 0x4230
cmpb $0x69, 0x3d17b(%rip)    # 0x45b17
jne 0x89aa
movq 0x3d2b3(%rip), %rdi     # 0x45c58
callq 0x42d0
movb 0x3cd66(%rip), %al      # 0x45716
movb %al, 0x3c94e(%rip)      # 0x45304
movzbl 0x3cd57(%rip), %eax     # 0x45714
leal 0xf(,%rax,8), %ecx
andl $-0x10, %ecx
movq %rsp, %r13
subq %rcx, %r13
movq %r13, %rsp
movb 0x3c90e(%rip), %cl      # 0x452e4
movb %cl, -0x29(%rbp)
testb %al, %al
jle 0x8ca6
leaq -0x460(%rbp), %r15
xorl %r14d, %r14d
movzbl 0x3c8f4(%rip), %eax     # 0x452e6
decl %eax
cmpl $0xb, %eax
ja 0x8b09
leaq 0x2e724(%rip), %rcx     # 0x37128
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x90, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c884(%rip), %rsi     # 0x452a8
callq 0x1447e
jmp 0x8b04
movl $0xa0, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c863(%rip), %rsi     # 0x452a8
callq 0x1f558
jmp 0x8b04
movl $0x98, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c842(%rip), %rsi     # 0x452a8
callq 0x1d500
jmp 0x8b04
movl $0xd0, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c821(%rip), %rsi     # 0x452a8
callq 0x28868
jmp 0x8b04
movl $0xa8, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c803(%rip), %rsi     # 0x452a8
callq 0x2ec4c
jmp 0x8b04
movl $0xa0, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c7e5(%rip), %rsi     # 0x452a8
callq 0x23774
jmp 0x8b04
movl $0x98, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c7c7(%rip), %rsi     # 0x452a8
callq 0x2157c
jmp 0x8b04
movl $0xa8, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c7a9(%rip), %rsi     # 0x452a8
callq 0x25d4c
movq %r12, (%r13,%r14,8)
movslq 0x3d054(%rip), %rax     # 0x45b64
testq %rax, %rax
jle 0x8b5b
movq 0x3d08c(%rip), %rcx     # 0x45ba8
movq 0x3d095(%rip), %rdx     # 0x45bb8
xorl %esi, %esi
movzbl (%rbx,%rsi), %edi
cmpq %rdi, %r14
jne 0x8b53
cmpl $0x0, (%rcx,%rsi,4)
jle 0x8b53
movq (%rdx,%rsi,8), %rdi
xorl %r8d, %r8d
movq (%rdi,%r8,8), %r9
movl $0x1, 0x4(%r9)
incq %r8
movslq (%rcx,%rsi,4), %r9
cmpq %r9, %r8
jl 0x8b3b
incq %rsi
cmpq %rax, %rsi
jne 0x8b25
movslq 0x3d01e(%rip), %rcx     # 0x45b80
testq %rcx, %rcx
jle 0x8b90
movq 0x3d00a(%rip), %rax     # 0x45b78
shlq $0x5, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
cmpl %r14d, 0x5c(%rax,%rdx)
jne 0x8b87
movl $0x1, 0x4(%rax,%rdx)
addq $0x60, %rdx
cmpq %rdx, %rcx
jne 0x8b78
callq 0x4150
movq %rax, %r12
movq (%r13,%r14,8), %rdi
movq (%rdi), %rax
callq *0x48(%rax)
callq 0x4150
subq %r12, %rax
movq -0x40(%rbp), %rcx
addq %rax, (%rcx)
movq %r15, %rdi
leaq 0x2f17c(%rip), %rsi     # 0x37d38
movq -0x60(%rbp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x40a0
movq (%r13,%r14,8), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x40(%rax)
movslq 0x3cf85(%rip), %rax     # 0x45b64
testq %rax, %rax
jle 0x8c2a
movq 0x3cfbd(%rip), %rcx     # 0x45ba8
movq 0x3cfc6(%rip), %rdx     # 0x45bb8
xorl %esi, %esi
movzbl (%rbx,%rsi), %edi
cmpq %rdi, %r14
jne 0x8c22
cmpl $0x0, (%rcx,%rsi,4)
jle 0x8c22
movq (%rdx,%rsi,8), %rdi
xorl %r8d, %r8d
movq (%rdi,%r8,8), %r9
movl $0x0, 0x4(%r9)
incq %r8
movslq (%rcx,%rsi,4), %r9
cmpq %r9, %r8
jl 0x8c0a
incq %rsi
cmpq %rax, %rsi
jne 0x8bf4
movslq 0x3cf4f(%rip), %rcx     # 0x45b80
testq %rcx, %rcx
jle 0x8c5f
movq 0x3cf3b(%rip), %rax     # 0x45b78
shlq $0x5, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
cmpl %r14d, 0x5c(%rax,%rdx)
jne 0x8c56
movl $0x0, 0x4(%rax,%rdx)
addq $0x60, %rdx
cmpq %rdx, %rcx
jne 0x8c47
leal 0x1(%r14), %r12d
leaq 0x2f0d4(%rip), %rdi     # 0x37d3e
movl %r12d, %esi
xorl %eax, %eax
callq 0x4050
cmpb $0x1, 0x3c661(%rip)     # 0x452dc
jne 0x8c92
movq -0x38(%rbp), %rdi
leaq 0x2f0b6(%rip), %rsi     # 0x37d3e
movl %r12d, %edx
xorl %eax, %eax
callq 0x4360
incq %r14
movsbq 0x3ca77(%rip), %rax     # 0x45714
cmpq %rax, %r14
jl 0x89eb
movb -0x29(%rbp), %al
movb %al, 0x3c635(%rip)      # 0x452e4
callq 0x4150
movq %rax, %r14
movslq 0x3ce9a(%rip), %r12     # 0x45b58
movl $0x1, %esi
movq %r12, %rdi
callq 0x4140
movq %rax, %r15
testq %r12, %r12
jle 0x8cef
movq 0x3ce46(%rip), %rax     # 0x45b20
xorl %ecx, %ecx
movslq (%rax,%rcx,4), %rdx
movb (%rbx,%rdx), %dl
movb %dl, (%r15,%rcx)
incq %rcx
cmpq %rcx, %r12
jne 0x8cdc
movq 0x3ce22(%rip), %rdx     # 0x45b18
movq 0x3ce23(%rip), %rcx     # 0x45b20
movq 0x3ce24(%rip), %r8      # 0x45b28
movq 0x3ce25(%rip), %r9      # 0x45b30
movupd 0x3ce25(%rip), %xmm0    # 0x45b38
movsbl 0x3c9fa(%rip), %eax     # 0x45714
subq $0x30, %rsp
movq %r15, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x16c6a
addq $0x30, %rsp
leaq 0x2f101(%rip), %rdi     # 0x37e4a
callq 0x43b0
movq %r15, %rdi
callq 0x42d0
callq 0x4150
subq %r14, %rax
movq -0x58(%rbp), %rcx
addq %rax, (%rcx)
movb 0x3c9a9(%rip), %al      # 0x45714
xorpd %xmm1, %xmm1
testb %al, %al
jle 0x8db9
xorl %r15d, %r15d
movsd %xmm1, -0x40(%rbp)
movq (%r13,%r15,8), %rdi
callq 0x17ce0
movl %eax, %r14d
movq (%r13,%r15,8), %rdi
testq %rdi, %rdi
je 0x8d98
movq (%rdi), %rax
callq *0x8(%rax)
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
incq %r15
movsbq 0x3c960(%rip), %rax     # 0x45714
cmpq %rax, %r15
jl 0x8d76
movsbl %al, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movq %rbx, %rdi
callq 0x42d0
movsd -0x40(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x4e7e
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ec4
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0x4e34
movq %rax, %rbx
movl $0x90, %esi
jmp 0x8e3f
jmp 0x8e17
jmp 0x8e23
movq %rax, %rbx
movl $0x98, %esi
jmp 0x8e3f
jmp 0x8e37
movq %rax, %rbx
movl $0xa0, %esi
jmp 0x8e3f
movq %rax, %rbx
movl $0xd0, %esi
jmp 0x8e3f
movq %rax, %rbx
movl $0xa8, %esi
movq %r12, %rdi
callq 0x4250
movq %rbx, %rdi
callq 0x4460