// File: /workspace/github_cpp_makefile_star2_O2/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 $0x448, %rsp            # imm = 0x448
movq %r9, -0x38(%rbp)
movq %r8, %r12
movq %rcx, -0x40(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
cmpl $0x0, 0x3763f(%rip)     # 0x3e328
jle 0x6d03
leaq 0x284de(%rip), %rsi     # 0x2f1d0
movq %r15, %rdi
callq 0x42b0
testq %rax, %rax
je 0x72ea
movq %rbx, -0x60(%rbp)
movslq 0x37e76(%rip), %rdi     # 0x3eb84
pushq $0x1
popq %rsi
callq 0x4130
movq %rax, %r14
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x4200
movl %eax, %edi
callq 0x4220
cmpb $0x0, 0x37a07(%rip)     # 0x3e737
jle 0x6dd8
movzbl 0x37dfa(%rip), %eax     # 0x3eb37
cmpl $0x6f, %eax
je 0x6d87
cmpl $0x69, %eax
jne 0x6dd6
leaq 0x379e5(%rip), %rdi     # 0x3e737
leaq 0x28f4f(%rip), %rsi     # 0x2fca8
callq 0x42b0
testq %rax, %rax
je 0x7317
movq %rax, %rbx
movl $0x400, 0x37efc(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x4330
movq %rax, 0x37ef3(%rip)     # 0x3ec78
jmp 0x6dd8
leaq 0x379a9(%rip), %rdi     # 0x3e737
leaq 0x2843b(%rip), %rsi     # 0x2f1d0
callq 0x42b0
movq %rax, %rbx
testq %rax, %rax
jne 0x6dd8
movq 0x3722f(%rip), %rax     # 0x3dfd8
movq (%rax), %rdi
leaq 0x28f1d(%rip), %rsi     # 0x2fcd0
movq %r15, %rdx
xorl %eax, %eax
callq 0x4380
cmpb $0x1, 0x37538(%rip)     # 0x3e2fc
jne 0x7350
leaq 0x28eff(%rip), %rsi     # 0x2fcd0
jmp 0x7342
xorl %ebx, %ebx
movq %r12, -0x50(%rbp)
movq %r15, -0x58(%rbp)
leaq 0x2a4bf(%rip), %r15     # 0x312a6
leaq 0x2854a(%rip), %r12     # 0x2f338
xorl %r13d, %r13d
movslq 0x37d8c(%rip), %rcx     # 0x3eb84
movb 0x37939(%rip), %al      # 0x3e737
cmpq %rcx, %r13
jge 0x6e95
testb %al, %al
je 0x6e24
setg %dl
movb 0x37d23(%rip), %cl      # 0x3eb37
cmpb $0x6f, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x6e4f
callq 0x40b0
movsbl 0x37904(%rip), %ecx     # 0x3e734
cltd
idivl %ecx
movb %dl, (%r14,%r13)
cmpb $0x0, 0x378f9(%rip)     # 0x3e737
jle 0x6e8d
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x4380
jmp 0x6e8d
testb %al, %al
setg %al
cmpb $0x69, %cl
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6e8d
movq %rbx, %rdi
callq 0x8155
movq 0x37e08(%rip), %rdi     # 0x3ec78
movq %r12, %rsi
callq 0x43f0
testq %rax, %rax
je 0x7358
movq %rax, %rdi
callq 0x43e0
movb %al, (%r14,%r13)
incq %r13
jmp 0x6df1
testb %al, %al
jle 0x6eb6
movq %rbx, %rdi
callq 0x4240
cmpb $0x69, 0x37c8f(%rip)    # 0x3eb37
jne 0x6eb6
movq 0x37dc7(%rip), %rdi     # 0x3ec78
callq 0x42c0
movb 0x3787a(%rip), %al      # 0x3e736
movb %al, 0x37462(%rip)      # 0x3e324
movzbl 0x3786b(%rip), %eax     # 0x3e734
leal 0xf(,%rax,8), %ecx
andl $-0x10, %ecx
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x48(%rbp)
movq %rdx, %rsp
xorl %r15d, %r15d
movb 0x3741b(%rip), %cl      # 0x3e304
movb %cl, -0x29(%rbp)
xorl %r12d, %r12d
movsbq %al, %rax
cmpq %rax, %r12
jge 0x71b2
movzbl 0x37403(%rip), %eax     # 0x3e306
decl %eax
cmpl $0xb, %eax
ja 0x701d
leaq 0x28213(%rip), %rcx     # 0x2f128
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x90, %edi
callq 0x4250
movq %rax, %r13
movq %rax, %rdi
leaq 0x37393(%rip), %rsi     # 0x3e2c8
callq 0x10be8
jmp 0x7015
movl $0xa0, %edi
callq 0x4250
movq %rax, %r13
movq %rax, %rdi
leaq 0x37372(%rip), %rsi     # 0x3e2c8
callq 0x1a938
jmp 0x7015
movl $0x98, %edi
callq 0x4250
movq %rax, %r13
movq %rax, %rdi
leaq 0x37351(%rip), %rsi     # 0x3e2c8
callq 0x18ddc
jmp 0x7015
movl $0xd0, %edi
callq 0x4250
movq %rax, %r13
movq %rax, %rdi
leaq 0x37330(%rip), %rsi     # 0x3e2c8
callq 0x226c0
jmp 0x7015
movl $0xa8, %edi
callq 0x4250
movq %rax, %r13
movq %rax, %rdi
leaq 0x37312(%rip), %rsi     # 0x3e2c8
callq 0x27eb6
jmp 0x7015
movl $0xa0, %edi
callq 0x4250
movq %rax, %r13
movq %rax, %rdi
leaq 0x372f4(%rip), %rsi     # 0x3e2c8
callq 0x1e104
jmp 0x7015
movl $0x98, %edi
callq 0x4250
movq %rax, %r13
movq %rax, %rdi
leaq 0x372d6(%rip), %rsi     # 0x3e2c8
callq 0x1c43c
jmp 0x7015
movl $0xa8, %edi
callq 0x4250
movq %rax, %r13
movq %rax, %rdi
leaq 0x372b8(%rip), %rsi     # 0x3e2c8
callq 0x2018c
movq -0x48(%rbp), %rax
movq %r13, (%rax,%r12,8)
movl 0x37b61(%rip), %eax     # 0x3eb84
movq 0x37b9e(%rip), %rcx     # 0x3ebc8
testl %eax, %eax
cmovlel %r15d, %eax
xorl %edx, %edx
cmpq %rax, %rdx
je 0x706d
movzbl (%r14,%rdx), %esi
cmpq %rsi, %r12
jne 0x7068
movq 0x37b90(%rip), %rsi     # 0x3ebd8
xorl %edi, %edi
movslq (%rcx,%rdx,4), %r8
cmpq %r8, %rdi
jge 0x7068
movq (%rsi,%rdx,8), %r8
movq (%r8,%rdi,8), %r8
movl $0x1, 0x4(%r8)
incq %rdi
jmp 0x704a
incq %rdx
jmp 0x7032
movl 0x37b2d(%rip), %ecx     # 0x3eba0
movq 0x37b1e(%rip), %rax     # 0x3eb98
testl %ecx, %ecx
cmovlel %r15d, %ecx
imulq $0x60, %rcx, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x70a0
cmpl %r12d, 0x5c(%rax,%rdx)
jne 0x709a
movl $0x1, 0x4(%rax,%rdx)
addq $0x60, %rdx
jmp 0x7086
callq 0x4140
movq %rax, %r13
movq -0x48(%rbp), %rax
movq (%rax,%r12,8), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x48(%rax)
callq 0x4140
subq %r13, %rax
movq -0x40(%rbp), %rcx
addq %rax, (%rcx)
leaq -0x470(%rbp), %r13
movq %r13, %rdi
leaq 0x28c5f(%rip), %rsi     # 0x2fd38
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x40a0
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x40(%rax)
movl 0x37a8b(%rip), %eax     # 0x3eb84
movq 0x37ac8(%rip), %rcx     # 0x3ebc8
testl %eax, %eax
cmovlel %r15d, %eax
xorl %edx, %edx
cmpq %rax, %rdx
je 0x7140
movzbl (%r14,%rdx), %esi
cmpq %rsi, %r12
jne 0x713b
movq 0x37aba(%rip), %rsi     # 0x3ebd8
xorl %edi, %edi
movslq (%rcx,%rdx,4), %r8
cmpq %r8, %rdi
jge 0x713b
movq (%rsi,%rdx,8), %r8
movq (%r8,%rdi,8), %r8
andl $0x0, 0x4(%r8)
incq %rdi
jmp 0x7120
incq %rdx
jmp 0x7108
movl 0x37a5a(%rip), %ecx     # 0x3eba0
movq 0x37a4b(%rip), %rax     # 0x3eb98
testl %ecx, %ecx
cmovlel %r15d, %ecx
imulq $0x60, %rcx, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x7170
cmpl %r12d, 0x5c(%rax,%rdx)
jne 0x716a
andl $0x0, 0x4(%rax,%rdx)
addq $0x60, %rdx
jmp 0x7159
leal 0x1(%r12), %r13d
leaq 0x28bc2(%rip), %rdi     # 0x2fd3e
movl %r13d, %esi
xorl %eax, %eax
callq 0x4050
cmpb $0x1, 0x3716f(%rip)     # 0x3e2fc
jne 0x71a4
movq -0x38(%rbp), %rdi
leaq 0x28ba4(%rip), %rsi     # 0x2fd3e
movl %r13d, %edx
xorl %eax, %eax
callq 0x4380
incq %r12
movb 0x37587(%rip), %al      # 0x3e734
jmp 0x6eef
movb -0x29(%rbp), %al
movb %al, 0x37149(%rip)      # 0x3e304
callq 0x4140
movq %rax, %rbx
movslq 0x379ae(%rip), %r12     # 0x3eb78
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x4130
movq %rax, %r15
movq 0x37961(%rip), %rcx     # 0x3eb40
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %edx
cmovgl %r12d, %edx
movq -0x50(%rbp), %r12
cmpq %rax, %rdx
je 0x7207
movslq (%rcx,%rax,4), %rsi
movb (%r14,%rsi), %sil
movb %sil, (%r15,%rax)
incq %rax
jmp 0x71f1
movq 0x3792a(%rip), %rdx     # 0x3eb38
movq 0x37933(%rip), %r8      # 0x3eb48
movq 0x37934(%rip), %r9      # 0x3eb50
movups 0x37935(%rip), %xmm0    # 0x3eb58
movsbl 0x3750a(%rip), %eax     # 0x3e734
subq $0x30, %rsp
movq %r15, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movq -0x48(%rbp), %rax
movq %rax, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x12f48
addq $0x30, %rsp
leaq 0x28bee(%rip), %rdi     # 0x2fe4a
callq 0x43d0
movq %r15, %rdi
callq 0x42c0
callq 0x4140
subq %rbx, %rax
addq %rax, (%r12)
xorpd %xmm1, %xmm1
xorl %r15d, %r15d
movq -0x48(%rbp), %r12
movsbq 0x374ac(%rip), %rax     # 0x3e734
cmpq %rax, %r15
jge 0x72be
movsd %xmm1, -0x40(%rbp)
movq (%r12,%r15,8), %rdi
callq 0x13d9c
movl %eax, %ebx
movq (%r12,%r15,8), %rdi
testq %rdi, %rdi
je 0x72ac
movq (%rdi), %rax
callq *0x8(%rax)
cvtsi2sd %ebx, %xmm0
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
incq %r15
jmp 0x7280
movsbl %al, %eax
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movq %r14, %rdi
callq 0x42c0
movsd -0x40(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x36ce7(%rip), %rax     # 0x3dfd8
movq (%rax), %rdi
leaq 0x2898b(%rip), %rsi     # 0x2fc86
movq %r15, %rdx
xorl %eax, %eax
callq 0x4380
cmpb $0x1, 0x36ff0(%rip)     # 0x3e2fc
jne 0x7350
leaq 0x28971(%rip), %rsi     # 0x2fc86
jmp 0x7342
movq 0x36cba(%rip), %rax     # 0x3dfd8
movq (%rax), %rdi
leaq 0x28982(%rip), %rsi     # 0x2fcaa
movq %r15, %rdx
xorl %eax, %eax
callq 0x4380
cmpb $0x1, 0x36fc3(%rip)     # 0x3e2fc
jne 0x7350
leaq 0x28968(%rip), %rsi     # 0x2fcaa
movq -0x38(%rbp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x4380
pushq $0x1
popq %rdi
callq 0x42d0
movq 0x36c79(%rip), %rax     # 0x3dfd8
movq (%rax), %rcx
leaq 0x2898d(%rip), %rdi     # 0x2fcf6
pushq $0x41
popq %rsi
pushq $0x1
popq %rdx
callq 0x44e0
cmpb $0x1, 0x36f81(%rip)     # 0x3e2fc
jne 0x7350
leaq 0x28972(%rip), %rdi     # 0x2fcf6
pushq $0x41
popq %rsi
pushq $0x1
popq %rdx
movq -0x38(%rbp), %rcx
callq 0x44e0
jmp 0x7350
jmp 0x739b
jmp 0x73af
jmp 0x73b9
movq %rax, %rbx
movl $0xa8, %esi
jmp 0x73cb
movq %rax, %rbx
movl $0xd0, %esi
jmp 0x73cb
movq %rax, %rbx
movl $0x98, %esi
jmp 0x73cb
movq %rax, %rbx
movl $0xa0, %esi
jmp 0x73cb
movq %rax, %rbx
movl $0x90, %esi
movq %r13, %rdi
callq 0x4260
movq %rbx, %rdi
callq 0x4480