// File: /workspace/github_cpp_makefile_star2_O3/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, 0x3ca1d(%rip)     # 0x45308
jle 0x8905
leaq 0x2e92c(%rip), %rsi     # 0x37220
movq %r12, %rdi
callq 0x42c0
testq %rax, %rax
je 0x8eda
movslq 0x3d258(%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, 0x3cde6(%rip)     # 0x45717
jle 0x89aa
movzbl 0x3d1dd(%rip), %eax     # 0x45b17
cmpl $0x6f, %eax
je 0x8980
cmpl $0x69, %eax
jne 0x89a7
leaq 0x3cdcc(%rip), %rdi     # 0x45717
leaq 0x2f3a6(%rip), %rsi     # 0x37cf8
callq 0x42c0
testq %rax, %rax
je 0x8ee6
movq %rax, %r14
movl $0x400, 0x3d2e3(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x4330
movq %rax, 0x3d2da(%rip)     # 0x45c58
jmp 0x89aa
leaq 0x3cd90(%rip), %rdi     # 0x45717
leaq 0x2e892(%rip), %rsi     # 0x37220
callq 0x42c0
movq %rax, %r14
testq %rax, %rax
jne 0x89aa
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, 0x3d1a7(%rip)     # 0x45b64
jle 0x8a73
leaq 0x3092c(%rip), %r15     # 0x392f6
leaq 0x2e9b7(%rip), %r12     # 0x37388
xorl %r13d, %r13d
movb 0x3cd3d(%rip), %al      # 0x45717
testb %al, %al
je 0x89f7
setg %dl
movb 0x3d130(%rip), %cl      # 0x45b17
cmpb $0x6f, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x8a22
callq 0x40b0
movsbl 0x3cd11(%rip), %ecx     # 0x45714
cltd
idivl %ecx
movb %dl, (%rbx,%r13)
cmpb $0x0, 0x3cd06(%rip)     # 0x45717
jle 0x8a60
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x4360
jmp 0x8a60
testb %al, %al
setg %al
cmpb $0x69, %cl
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x8a60
movq %r14, %rdi
callq 0x9b93
movq 0x3d215(%rip), %rdi     # 0x45c58
movq %r12, %rsi
callq 0x43e0
testq %rax, %rax
je 0x8ed1
movq %rax, %rdi
callq 0x43d0
movb %al, (%rbx,%r13)
incq %r13
movslq 0x3d0fa(%rip), %rax     # 0x45b64
cmpq %rax, %r13
jl 0x89d4
cmpb $0x0, 0x3cc9d(%rip)     # 0x45717
jle 0x8a99
movq %r14, %rdi
callq 0x4230
cmpb $0x69, 0x3d08c(%rip)    # 0x45b17
jne 0x8a99
movq 0x3d1c4(%rip), %rdi     # 0x45c58
callq 0x42d0
movb 0x3cc77(%rip), %al      # 0x45716
movb %al, 0x3c85f(%rip)      # 0x45304
movzbl 0x3cc68(%rip), %eax     # 0x45714
leal 0xf(,%rax,8), %ecx
andl $-0x10, %ecx
movq %rsp, %r13
subq %rcx, %r13
movq %r13, %rsp
movb 0x3c81f(%rip), %cl      # 0x452e4
movb %cl, -0x29(%rbp)
testb %al, %al
jle 0x8d96
xorl %r14d, %r14d
movzbl 0x3c80c(%rip), %eax     # 0x452e6
decl %eax
cmpl $0xb, %eax
ja 0x8bf1
leaq 0x2e63c(%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 0x3c79c(%rip), %rsi     # 0x452a8
callq 0x144c6
jmp 0x8bec
movl $0xa0, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c77b(%rip), %rsi     # 0x452a8
callq 0x1f538
jmp 0x8bec
movl $0x98, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c75a(%rip), %rsi     # 0x452a8
callq 0x1d4d8
jmp 0x8bec
movl $0xd0, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c739(%rip), %rsi     # 0x452a8
callq 0x28574
jmp 0x8bec
movl $0xa8, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c71b(%rip), %rsi     # 0x452a8
callq 0x2e900
jmp 0x8bec
movl $0xa0, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c6fd(%rip), %rsi     # 0x452a8
callq 0x23600
jmp 0x8bec
movl $0x98, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c6df(%rip), %rsi     # 0x452a8
callq 0x214b8
jmp 0x8bec
movl $0xa8, %edi
callq 0x4240
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c6c1(%rip), %rsi     # 0x452a8
callq 0x25b1c
movq %r12, (%r13,%r14,8)
movslq 0x3cf6c(%rip), %rax     # 0x45b64
testq %rax, %rax
jle 0x8c43
movq 0x3cfa4(%rip), %rcx     # 0x45ba8
movq 0x3cfad(%rip), %rdx     # 0x45bb8
xorl %esi, %esi
movzbl (%rbx,%rsi), %edi
cmpq %rdi, %r14
jne 0x8c3b
cmpl $0x0, (%rcx,%rsi,4)
jle 0x8c3b
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 0x8c23
incq %rsi
cmpq %rax, %rsi
jne 0x8c0d
movslq 0x3cf36(%rip), %rcx     # 0x45b80
testq %rcx, %rcx
jle 0x8c78
movq 0x3cf22(%rip), %rax     # 0x45b78
shlq $0x5, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
cmpl %r14d, 0x5c(%rax,%rdx)
jne 0x8c6f
movl $0x1, 0x4(%rax,%rdx)
addq $0x60, %rdx
cmpq %rdx, %rcx
jne 0x8c60
callq 0x4150
movq %rax, %r12
movq (%r13,%r14,8), %r15
movq (%r15), %rax
movq %r15, %rdi
callq *0x48(%rax)
callq 0x4150
subq %r12, %rax
movq -0x40(%rbp), %rcx
addq %rax, (%rcx)
leaq -0x460(%rbp), %r12
movq %r12, %rdi
leaq 0x2f0da(%rip), %rsi     # 0x37d88
movq -0x60(%rbp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x40a0
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0x40(%rax)
movslq 0x3ce95(%rip), %rax     # 0x45b64
testq %rax, %rax
jle 0x8d1a
movq 0x3cecd(%rip), %rcx     # 0x45ba8
movq 0x3ced6(%rip), %rdx     # 0x45bb8
xorl %esi, %esi
movzbl (%rbx,%rsi), %edi
cmpq %rdi, %r14
jne 0x8d12
cmpl $0x0, (%rcx,%rsi,4)
jle 0x8d12
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 0x8cfa
incq %rsi
cmpq %rax, %rsi
jne 0x8ce4
movslq 0x3ce5f(%rip), %rcx     # 0x45b80
testq %rcx, %rcx
jle 0x8d4f
movq 0x3ce4b(%rip), %rax     # 0x45b78
shlq $0x5, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
cmpl %r14d, 0x5c(%rax,%rdx)
jne 0x8d46
movl $0x0, 0x4(%rax,%rdx)
addq $0x60, %rdx
cmpq %rdx, %rcx
jne 0x8d37
leal 0x1(%r14), %r12d
leaq 0x2f034(%rip), %rdi     # 0x37d8e
movl %r12d, %esi
xorl %eax, %eax
callq 0x4050
cmpb $0x1, 0x3c571(%rip)     # 0x452dc
jne 0x8d82
movq -0x38(%rbp), %rdi
leaq 0x2f016(%rip), %rsi     # 0x37d8e
movl %r12d, %edx
xorl %eax, %eax
callq 0x4360
incq %r14
movsbq 0x3c987(%rip), %rax     # 0x45714
cmpq %rax, %r14
jl 0x8ad3
movb -0x29(%rbp), %al
movb %al, 0x3c545(%rip)      # 0x452e4
callq 0x4150
movq %rax, %r14
movslq 0x3cdaa(%rip), %r12     # 0x45b58
movl $0x1, %esi
movq %r12, %rdi
callq 0x4140
movq %rax, %r15
movq 0x3cd5b(%rip), %rcx     # 0x45b20
testq %r12, %r12
jle 0x8ddf
xorl %eax, %eax
movslq (%rcx,%rax,4), %rdx
movb (%rbx,%rdx), %dl
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %r12
jne 0x8dcc
movq 0x3cd32(%rip), %rdx     # 0x45b18
movq 0x3cd3b(%rip), %r8      # 0x45b28
movq 0x3cd3c(%rip), %r9      # 0x45b30
movupd 0x3cd3c(%rip), %xmm0    # 0x45b38
movsbl 0x3c911(%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 0x16c8e
addq $0x30, %rsp
leaq 0x2f068(%rip), %rdi     # 0x37e9a
callq 0x43b0
movq %r15, %rdi
callq 0x42d0
callq 0x4150
subq %r14, %rax
movq -0x58(%rbp), %rcx
addq %rax, (%rcx)
movb 0x3c8c0(%rip), %al      # 0x45714
xorpd %xmm1, %xmm1
testb %al, %al
jle 0x8ea2
xorl %r15d, %r15d
movsd %xmm1, -0x40(%rbp)
movq (%r13,%r15,8), %rdi
callq 0x17d50
movl %eax, %r14d
movq (%r13,%r15,8), %rdi
testq %rdi, %rdi
je 0x8e81
movq (%rdi), %rax
callq *0x8(%rax)
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
incq %r15
movsbq 0x3c877(%rip), %rax     # 0x45714
cmpq %rax, %r15
jl 0x8e5f
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 0x8f28
jmp 0x8f00
jmp 0x8f0c
movq %rax, %rbx
movl $0x98, %esi
jmp 0x8f28
jmp 0x8f20
movq %rax, %rbx
movl $0xa0, %esi
jmp 0x8f28
movq %rax, %rbx
movl $0xd0, %esi
jmp 0x8f28
movq %rax, %rbx
movl $0xa8, %esi
movq %r12, %rdi
callq 0x4250
movq %rbx, %rdi
callq 0x4460