// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/trainhmm.cpp
// Function: cross_validate_item(double*, char const*, char const*, long*, long*, _IO_FILE*)
NUMBER cross_validate_item(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_item(NUMBER* metrics, const char *filename, const char *model_file_name, double *tm_fit, double *tm_predict, FILE *fid_console) {//PAR
    NPAR f;
    NCAT I; // item
    NDAT t;
    clock_t tm0;//SEQ
//    double _tm0;//PAR
    char *ch;
    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.nI);
    NDAT *fold_counts = Calloc(NDAT, (size_t)param.cv_folds);
    srand ( (unsigned int)time(NULL) ); // randomize

    // 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(I=0; I<param.nI; I++) {
        if( param.cv_folds_file[0]==0 || (param.cv_folds_file[0] > 0 && param.cv_inout_flag=='o') ) { // output or default
            folds[I] = (NPAR)(rand() % param.cv_folds); // produce folds
            if(param.cv_folds_file[0] > 0 )
                fprintf(fid_folds,"%i\n",folds[I]); // 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[I] = (NPAR)(atoi(ch));
        }
    }
    if(param.cv_folds_file[0] > 0) { // file is specified
        fclose(fid_folds);
        if(param.cv_inout_flag=='i')
            free(line);
    }
    
    // count number of items in each fold
    for(t=0; t<param.N; t++) fold_counts[ folds[param.dat_item[t]/*->get(t)*/] ]++;
    // 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_PIg: // Gradient Descent: PI by group, A,B by skill
//                hmm = new HMMProblemPiG(&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_PIgk: // Gradient Descent, pLo=f(K,G), other by K
                hmms[f] = new HMMProblemPiGK(&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, pLo=f(K,G), 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
        NPAR *saved_obs = Calloc(NPAR, (size_t)fold_counts[f]);
        NDAT count_saved = 0;
        for(t=0; t<param.N; t++) {
            if( folds[ param.dat_item[t]/*->get(t)*/ ] == f ) {
                saved_obs[count_saved++] = param.dat_obs[t];
                param.dat_obs[t] = -1;//->set(t, -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
        count_saved = 0;
        for(t=0; t<param.N; t++)
            if( folds[ param.dat_item[t]/*->get(t)*/ ] == f )
                param.dat_obs[t]=saved_obs[count_saved++];//->set(t, saved_obs[count_saved++]);
        free(saved_obs);
//        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);
//        }
    }
    free(fold_counts);
    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_item[t] ];
	//		predict
	HMMProblem::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");
    
    free(dat_fold);
	
	*(tm_predict) += (clock_t)(clock()- tm0);//SEQ
//    *(tm_predict) += omp_get_wtime()-_tm0;//PAR
    
    // delete problems
    NCAT n_par = 0;
    for(f=0; f<param.cv_folds; f++) {
        n_par += hmms[f]->getNparams();
        delete hmms[f];
    }
    n_par /= f;
    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, -0x40(%rbp)
movq %r8, %r15
movq %rcx, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, %r12
movq %rdi, -0x60(%rbp)
cmpl $0x0, 0x3c39f(%rip)     # 0x45308
jle 0x8f83
leaq 0x2e2ae(%rip), %rsi     # 0x37220
movq %r12, %rdi
callq 0x42c0
testq %rax, %rax
je 0x951e
movslq 0x3cbe2(%rip), %rdi     # 0x45b6c
movl $0x1, %esi
callq 0x4140
movq %rax, %rbx
movsbq 0x3c775(%rip), %rdi     # 0x45714
movl $0x4, %esi
callq 0x4140
movq %rax, %r13
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x4200
movl %eax, %edi
callq 0x4220
cmpb $0x0, 0x3c753(%rip)     # 0x45717
jle 0x903d
movzbl 0x3cb4a(%rip), %eax     # 0x45b17
cmpl $0x6f, %eax
je 0x9013
cmpl $0x69, %eax
jne 0x903a
leaq 0x3c739(%rip), %rdi     # 0x45717
leaq 0x2ed13(%rip), %rsi     # 0x37cf8
callq 0x42c0
testq %rax, %rax
je 0x952a
movq %rax, %r14
movl $0x400, 0x3cc50(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x4330
movq %rax, 0x3cc47(%rip)     # 0x45c58
jmp 0x903d
leaq 0x3c6fd(%rip), %rdi     # 0x45717
leaq 0x2e1ff(%rip), %rsi     # 0x37220
callq 0x42c0
movq %rax, %r14
testq %rax, %rax
jne 0x903d
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x4f0e
xorl %r14d, %r14d
movq %r13, -0x48(%rbp)
movq %r12, -0x50(%rbp)
movq %r15, -0x58(%rbp)
cmpl $0x0, 0x3cb1c(%rip)     # 0x45b6c
jle 0x9106
leaq 0x30299(%rip), %r12     # 0x392f6
leaq 0x2e324(%rip), %r13     # 0x37388
xorl %r15d, %r15d
movb 0x3c6aa(%rip), %al      # 0x45717
testb %al, %al
je 0x908a
setg %dl
movb 0x3ca9d(%rip), %cl      # 0x45b17
cmpb $0x6f, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x90b5
callq 0x40b0
movsbl 0x3c67e(%rip), %ecx     # 0x45714
cltd
idivl %ecx
movb %dl, (%rbx,%r15)
cmpb $0x0, 0x3c673(%rip)     # 0x45717
jle 0x90f3
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x4360
jmp 0x90f3
testb %al, %al
setg %al
cmpb $0x69, %cl
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x90f3
movq %r14, %rdi
callq 0x9b93
movq 0x3cb82(%rip), %rdi     # 0x45c58
movq %r13, %rsi
callq 0x43e0
testq %rax, %rax
je 0x9515
movq %rax, %rdi
callq 0x43d0
movb %al, (%rbx,%r15)
incq %r15
movslq 0x3ca6f(%rip), %rax     # 0x45b6c
cmpq %rax, %r15
jl 0x9067
cmpb $0x0, 0x3c60a(%rip)     # 0x45717
jle 0x912c
movq %r14, %rdi
callq 0x4230
cmpb $0x69, 0x3c9f9(%rip)    # 0x45b17
jne 0x912c
movq 0x3cb31(%rip), %rdi     # 0x45c58
callq 0x42d0
movslq 0x3ca25(%rip), %rax     # 0x45b58
testq %rax, %rax
movq -0x48(%rbp), %r15
jle 0x915a
movq 0x3ca05(%rip), %rcx     # 0x45b48
xorl %edx, %edx
movslq (%rcx,%rdx,4), %rsi
movsbq (%rbx,%rsi), %rsi
incl (%r15,%rsi,4)
incq %rdx
cmpq %rdx, %rax
jne 0x9145
movzbl 0x3c5b3(%rip), %eax     # 0x45714
leal 0xf(,%rax,8), %ecx
andl $-0x10, %ecx
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x38(%rbp)
movq %rdx, %rsp
movb 0x3c166(%rip), %cl      # 0x452e4
movb %cl, -0x29(%rbp)
testb %al, %al
jle 0x93e3
xorl %r12d, %r12d
movzbl 0x3c153(%rip), %eax     # 0x452e6
decl %eax
cmpl $0xb, %eax
ja 0x928c
leaq 0x2dfb3(%rip), %rcx     # 0x37158
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x90, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c0e3(%rip), %rsi     # 0x452a8
callq 0x144c6
jmp 0x9284
movl $0xd0, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c0c2(%rip), %rsi     # 0x452a8
callq 0x28574
jmp 0x9284
movl $0x98, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c0a1(%rip), %rsi     # 0x452a8
callq 0x1d4d8
jmp 0x9284
movl $0xa8, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c083(%rip), %rsi     # 0x452a8
callq 0x2e900
jmp 0x9284
movl $0xa0, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c065(%rip), %rsi     # 0x452a8
callq 0x23600
jmp 0x9284
movl $0x98, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c047(%rip), %rsi     # 0x452a8
callq 0x214b8
jmp 0x9284
movl $0xa8, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c029(%rip), %rsi     # 0x452a8
callq 0x25b1c
movq -0x38(%rbp), %rax
movq %r14, (%rax,%r12,8)
movslq (%r15,%r12,4), %rdi
movl $0x1, %esi
callq 0x4140
movq %rax, %r14
movl 0x3c8b5(%rip), %ecx     # 0x45b58
testl %ecx, %ecx
jle 0x92ef
xorl %eax, %eax
movq 0x3c898(%rip), %rsi     # 0x45b48
xorl %edx, %edx
movslq (%rsi,%rax,4), %rdi
movzbl (%rbx,%rdi), %edi
cmpq %rdi, %r12
jne 0x92e4
movq 0x3c852(%rip), %rcx     # 0x45b18
movb (%rcx,%rax), %sil
movslq %edx, %rdi
incl %edx
movb %sil, (%r14,%rdi)
movb $-0x1, (%rcx,%rax)
movq 0x3c86a(%rip), %rsi     # 0x45b48
movl 0x3c874(%rip), %ecx     # 0x45b58
incq %rax
movslq %ecx, %rdi
cmpq %rdi, %rax
jl 0x92b2
callq 0x4150
movq %rax, %r13
movq -0x38(%rbp), %rax
movq (%rax,%r12,8), %r15
movq (%r15), %rax
movq %r15, %rdi
callq *0x48(%rax)
callq 0x4150
subq %r13, %rax
movq -0x70(%rbp), %rcx
addq %rax, (%rcx)
leaq -0x470(%rbp), %r13
movq %r13, %rdi
leaq 0x2ea60(%rip), %rsi     # 0x37d88
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x40a0
movq (%r15), %rax
movq %r15, %rdi
movq %r13, %rsi
callq *0x40(%rax)
movl 0x3c810(%rip), %ecx     # 0x45b58
testl %ecx, %ecx
jle 0x938f
xorl %eax, %eax
movq 0x3c7f3(%rip), %rsi     # 0x45b48
xorl %edx, %edx
movslq (%rsi,%rax,4), %rdi
movzbl (%rbx,%rdi), %edi
cmpq %rdi, %r12
jne 0x9384
movslq %edx, %rcx
incl %edx
movb (%r14,%rcx), %cl
movq 0x3c7a4(%rip), %rsi     # 0x45b18
movb %cl, (%rsi,%rax)
movq 0x3c7ca(%rip), %rsi     # 0x45b48
movl 0x3c7d4(%rip), %ecx     # 0x45b58
incq %rax
movslq %ecx, %rdi
cmpq %rdi, %rax
jl 0x9357
movq %r14, %rdi
callq 0x42d0
leal 0x1(%r12), %r14d
leaq 0x2e9eb(%rip), %rdi     # 0x37d8e
movl %r14d, %esi
xorl %eax, %eax
callq 0x4050
cmpb $0x1, 0x3bf28(%rip)     # 0x452dc
jne 0x93cb
movq -0x40(%rbp), %rdi
leaq 0x2e9cd(%rip), %rsi     # 0x37d8e
movl %r14d, %edx
xorl %eax, %eax
callq 0x4360
incq %r12
movsbq 0x3c33e(%rip), %rax     # 0x45714
cmpq %rax, %r12
movq -0x48(%rbp), %r15
jl 0x918c
movq %r15, %rdi
callq 0x42d0
movb -0x29(%rbp), %al
movb %al, 0x3bef0(%rip)      # 0x452e4
callq 0x4150
movq %rax, %r14
movslq 0x3c755(%rip), %r12     # 0x45b58
movl $0x1, %esi
movq %r12, %rdi
callq 0x4140
movq %rax, %r15
testq %r12, %r12
jle 0x9434
movq 0x3c729(%rip), %rax     # 0x45b48
xorl %ecx, %ecx
movslq (%rax,%rcx,4), %rdx
movb (%rbx,%rdx), %dl
movb %dl, (%r15,%rcx)
incq %rcx
cmpq %rcx, %r12
jne 0x9421
movq 0x3c6dd(%rip), %rdx     # 0x45b18
movq 0x3c6de(%rip), %rcx     # 0x45b20
movq 0x3c6df(%rip), %r8      # 0x45b28
movq 0x3c6e0(%rip), %r9      # 0x45b30
movups 0x3c6e1(%rip), %xmm0    # 0x45b38
movsbl 0x3c2b6(%rip), %eax     # 0x45714
subq $0x30, %rsp
movq %r15, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movq -0x38(%rbp), %rax
movq %rax, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x16c8e
addq $0x30, %rsp
leaq 0x2ea0a(%rip), %rdi     # 0x37e9a
callq 0x43b0
movq %r15, %rdi
callq 0x42d0
callq 0x4150
subq %r14, %rax
movq -0x58(%rbp), %rcx
addq %rax, (%rcx)
xorl %r12d, %r12d
xorl %r14d, %r14d
movq -0x38(%rbp), %r13
movq (%r13,%r12,8), %rdi
callq 0x17d50
movl %eax, %r15d
movq (%r13,%r12,8), %rdi
testq %rdi, %rdi
je 0x94d3
movq (%rdi), %rax
callq *0x8(%rax)
addl %r15d, %r14d
incq %r12
movsbq 0x3c233(%rip), %rax     # 0x45714
cmpq %rax, %r12
jl 0x94b6
movl %r14d, %eax
cltd
idivl %r12d
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x38(%rbp)
movq %rbx, %rdi
callq 0x42d0
movsd -0x38(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x4fa2
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x4fe8
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x4f58
movq %rax, %rbx
movl $0x90, %esi
jmp 0x956a
jmp 0x954c
movq %rax, %rbx
movl $0xa0, %esi
jmp 0x956a
movq %rax, %rbx
movl $0x98, %esi
jmp 0x956a
jmp 0x9562
movq %rax, %rbx
movl $0xd0, %esi
jmp 0x956a
movq %rax, %rbx
movl $0xa8, %esi
movq %r14, %rdi
callq 0x4250
movq %rbx, %rdi
callq 0x4460