// File: /workspace/github_cpp_makefile_star2_O2/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, -0x38(%rbp)
movq %r8, %r12
movq %rcx, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, %rbx
movq %rdi, %r13
cmpl $0x0, 0x36f1d(%rip)     # 0x3e328
jle 0x7425
leaq 0x27dbc(%rip), %rsi     # 0x2f1d0
movq %rbx, %rdi
callq 0x42b0
testq %rax, %rax
je 0x79c3
movslq 0x37760(%rip), %rdi     # 0x3eb8c
pushq $0x1
popq %rsi
callq 0x4130
movq %rax, %r14
movsbq 0x372f5(%rip), %rdi     # 0x3e734
pushq $0x4
popq %rsi
callq 0x4130
movq %rax, -0x48(%rbp)
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x4200
movl %eax, %edi
callq 0x4220
cmpb $0x0, 0x372d4(%rip)     # 0x3e737
jle 0x750c
movzbl 0x376c7(%rip), %eax     # 0x3eb37
cmpl $0x6f, %eax
je 0x74ba
cmpl $0x69, %eax
jne 0x7509
leaq 0x372b2(%rip), %rdi     # 0x3e737
leaq 0x2881c(%rip), %rsi     # 0x2fca8
callq 0x42b0
testq %rax, %rax
je 0x79f0
movq %rax, %r15
movl $0x400, 0x377c9(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x4330
movq %rax, 0x377c0(%rip)     # 0x3ec78
jmp 0x750c
leaq 0x37276(%rip), %rdi     # 0x3e737
leaq 0x27d08(%rip), %rsi     # 0x2f1d0
callq 0x42b0
movq %rax, %r15
testq %rax, %rax
jne 0x750c
movq 0x36afc(%rip), %rax     # 0x3dfd8
movq (%rax), %rdi
leaq 0x287ea(%rip), %rsi     # 0x2fcd0
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4380
cmpb $0x1, 0x36e05(%rip)     # 0x3e2fc
jne 0x7a29
leaq 0x287cc(%rip), %rsi     # 0x2fcd0
jmp 0x7a1b
xorl %r15d, %r15d
movq %r13, -0x50(%rbp)
movq %r12, -0x58(%rbp)
movq %rbx, -0x60(%rbp)
leaq 0x29d87(%rip), %r12     # 0x312a6
leaq 0x27e12(%rip), %r13     # 0x2f338
xorl %ebx, %ebx
movslq 0x3765d(%rip), %rcx     # 0x3eb8c
movb 0x37202(%rip), %al      # 0x3e737
cmpq %rcx, %rbx
jge 0x75cc
testb %al, %al
je 0x755b
setg %dl
movb 0x375ec(%rip), %cl      # 0x3eb37
cmpb $0x6f, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x7586
callq 0x40b0
movsbl 0x371cd(%rip), %ecx     # 0x3e734
cltd
idivl %ecx
movb %dl, (%r14,%rbx)
cmpb $0x0, 0x371c2(%rip)     # 0x3e737
jle 0x75c4
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x4380
jmp 0x75c4
testb %al, %al
setg %al
cmpb $0x69, %cl
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x75c4
movq %r15, %rdi
callq 0x8155
movq 0x376d1(%rip), %rdi     # 0x3ec78
movq %r13, %rsi
callq 0x43f0
testq %rax, %rax
je 0x7a31
movq %rax, %rdi
callq 0x43e0
movb %al, (%r14,%rbx)
incq %rbx
jmp 0x7528
testb %al, %al
jle 0x75ed
movq %r15, %rdi
callq 0x4240
cmpb $0x69, 0x37558(%rip)    # 0x3eb37
jne 0x75ed
movq 0x37690(%rip), %rdi     # 0x3ec78
callq 0x42c0
movl 0x37585(%rip), %eax     # 0x3eb78
movq 0x3756e(%rip), %rcx     # 0x3eb68
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
movq -0x48(%rbp), %rbx
cmpq %rdx, %rax
je 0x761b
movslq (%rcx,%rdx,4), %rsi
movsbq (%r14,%rsi), %rsi
incl (%rbx,%rsi,4)
incq %rdx
jmp 0x7605
movzbl 0x37112(%rip), %eax     # 0x3e734
leal 0xf(,%rax,8), %ecx
andl $-0x10, %ecx
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x40(%rbp)
movq %rdx, %rsp
movb 0x36cc5(%rip), %cl      # 0x3e304
movb %cl, -0x29(%rbp)
xorl %r13d, %r13d
movsbq %al, %rax
cmpq %rax, %r13
jge 0x7893
movzbl 0x36cad(%rip), %eax     # 0x3e306
decl %eax
cmpl $0xb, %eax
ja 0x7752
leaq 0x27aed(%rip), %rcx     # 0x2f158
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x90, %edi
callq 0x4250
movq %rax, %r12
movq %rax, %rdi
leaq 0x36c3d(%rip), %rsi     # 0x3e2c8
callq 0x10be8
jmp 0x774a
movl $0xd0, %edi
callq 0x4250
movq %rax, %r12
movq %rax, %rdi
leaq 0x36c1c(%rip), %rsi     # 0x3e2c8
callq 0x226c0
jmp 0x774a
movl $0x98, %edi
callq 0x4250
movq %rax, %r12
movq %rax, %rdi
leaq 0x36bfb(%rip), %rsi     # 0x3e2c8
callq 0x18ddc
jmp 0x774a
movl $0xa8, %edi
callq 0x4250
movq %rax, %r12
movq %rax, %rdi
leaq 0x36bdd(%rip), %rsi     # 0x3e2c8
callq 0x27eb6
jmp 0x774a
movl $0xa0, %edi
callq 0x4250
movq %rax, %r12
movq %rax, %rdi
leaq 0x36bbf(%rip), %rsi     # 0x3e2c8
callq 0x1e104
jmp 0x774a
movl $0x98, %edi
callq 0x4250
movq %rax, %r12
movq %rax, %rdi
leaq 0x36ba1(%rip), %rsi     # 0x3e2c8
callq 0x1c43c
jmp 0x774a
movl $0xa8, %edi
callq 0x4250
movq %rax, %r12
movq %rax, %rdi
leaq 0x36b83(%rip), %rsi     # 0x3e2c8
callq 0x2018c
movq -0x40(%rbp), %rax
movq %r12, (%rax,%r13,8)
movslq (%rbx,%r13,4), %rdi
pushq $0x1
popq %rsi
callq 0x4130
movq %rax, %r12
xorl %eax, %eax
movl 0x3740f(%rip), %edx     # 0x3eb78
xorl %ecx, %ecx
movslq %edx, %rsi
cmpq %rsi, %rax
jge 0x77ab
movq 0x373ee(%rip), %rsi     # 0x3eb68
movslq (%rsi,%rax,4), %rsi
movzbl (%r14,%rsi), %esi
cmpq %rsi, %r13
jne 0x77a6
movq 0x373a9(%rip), %rdx     # 0x3eb38
movb (%rdx,%rax), %sil
movslq %ecx, %rdi
incl %ecx
movb %sil, (%r12,%rdi)
movb $-0x1, (%rdx,%rax)
movl 0x373d2(%rip), %edx     # 0x3eb78
incq %rax
jmp 0x776b
callq 0x4140
movq %rax, %r15
movq -0x40(%rbp), %rax
movq (%rax,%r13,8), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x48(%rax)
callq 0x4140
subq %r15, %rax
movq -0x70(%rbp), %rcx
addq %rax, (%rcx)
xorl %r15d, %r15d
leaq -0x470(%rbp), %rdi
leaq 0x28554(%rip), %rsi     # 0x2fd38
movq -0x68(%rbp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x40a0
movq (%rbx), %rax
movq %rbx, %rdi
leaq -0x470(%rbp), %rsi
callq *0x40(%rax)
movl 0x37370(%rip), %ecx     # 0x3eb78
xorl %eax, %eax
movslq %ecx, %rdx
cmpq %rdx, %r15
jge 0x7846
movq 0x3734f(%rip), %rdx     # 0x3eb68
movslq (%rdx,%r15,4), %rdx
movzbl (%r14,%rdx), %edx
cmpq %rdx, %r13
jne 0x7841
movslq %eax, %rcx
incl %eax
movb (%r12,%rcx), %cl
movq 0x37301(%rip), %rdx     # 0x3eb38
movb %cl, (%rdx,%r15)
movl 0x37337(%rip), %ecx     # 0x3eb78
incq %r15
jmp 0x780a
movq %r12, %rdi
callq 0x42c0
leal 0x1(%r13), %r12d
leaq 0x284e5(%rip), %rdi     # 0x2fd3e
movl %r12d, %esi
xorl %eax, %eax
callq 0x4050
cmpb $0x1, 0x36a92(%rip)     # 0x3e2fc
movq -0x48(%rbp), %rbx
jne 0x7885
movq -0x38(%rbp), %rdi
leaq 0x284c3(%rip), %rsi     # 0x2fd3e
movl %r12d, %edx
xorl %eax, %eax
callq 0x4380
incq %r13
movb 0x36ea6(%rip), %al      # 0x3e734
jmp 0x7645
movq %rbx, %rdi
callq 0x42c0
movb -0x29(%rbp), %al
movb %al, 0x36a60(%rip)      # 0x3e304
callq 0x4140
movq %rax, %rbx
movslq 0x372c5(%rip), %r12     # 0x3eb78
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x4130
movq %rax, %r15
movq 0x372a0(%rip), %rax     # 0x3eb68
xorl %ecx, %ecx
testl %r12d, %r12d
movl $0x0, %edx
cmovgl %r12d, %edx
movq -0x58(%rbp), %r12
movq -0x50(%rbp), %rdi
cmpq %rcx, %rdx
je 0x78f4
movslq (%rax,%rcx,4), %rsi
movb (%r14,%rsi), %sil
movb %sil, (%r15,%rcx)
incq %rcx
jmp 0x78de
movq 0x3723d(%rip), %rdx     # 0x3eb38
movq 0x3723e(%rip), %rcx     # 0x3eb40
movq 0x3723f(%rip), %r8      # 0x3eb48
movq 0x37240(%rip), %r9      # 0x3eb50
movups 0x37241(%rip), %xmm0    # 0x3eb58
movsbl 0x36e16(%rip), %eax     # 0x3e734
subq $0x30, %rsp
movq %r15, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movq -0x40(%rbp), %rax
movq %rax, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x60(%rbp), %rsi
callq 0x12f48
addq $0x30, %rsp
leaq 0x284fe(%rip), %rdi     # 0x2fe4a
callq 0x43d0
movq %r15, %rdi
callq 0x42c0
callq 0x4140
subq %rbx, %rax
addq %rax, (%r12)
xorl %r12d, %r12d
xorl %ebx, %ebx
movq -0x40(%rbp), %r13
movsbq 0x36dbe(%rip), %rax     # 0x3e734
cmpq %rax, %r12
jge 0x79a0
movq (%r13,%r12,8), %rdi
callq 0x13d9c
movl %eax, %r15d
movq (%r13,%r12,8), %rdi
testq %rdi, %rdi
je 0x7998
movq (%rdi), %rax
callq *0x8(%rax)
addl %r15d, %ebx
incq %r12
jmp 0x796e
movl %ebx, %eax
cltd
idivl %r12d
movl %eax, %ebx
movq %r14, %rdi
callq 0x42c0
cvtsi2sd %ebx, %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3660e(%rip), %rax     # 0x3dfd8
movq (%rax), %rdi
leaq 0x282b2(%rip), %rsi     # 0x2fc86
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4380
cmpb $0x1, 0x36917(%rip)     # 0x3e2fc
jne 0x7a29
leaq 0x28298(%rip), %rsi     # 0x2fc86
jmp 0x7a1b
movq 0x365e1(%rip), %rax     # 0x3dfd8
movq (%rax), %rdi
leaq 0x282a9(%rip), %rsi     # 0x2fcaa
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4380
cmpb $0x1, 0x368ea(%rip)     # 0x3e2fc
jne 0x7a29
leaq 0x2828f(%rip), %rsi     # 0x2fcaa
movq -0x38(%rbp), %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4380
pushq $0x1
popq %rdi
callq 0x42d0
movq 0x365a0(%rip), %rax     # 0x3dfd8
movq (%rax), %rcx
leaq 0x282b4(%rip), %rdi     # 0x2fcf6
pushq $0x41
popq %rsi
pushq $0x1
popq %rdx
callq 0x44e0
cmpb $0x1, 0x368a8(%rip)     # 0x3e2fc
jne 0x7a29
leaq 0x28299(%rip), %rdi     # 0x2fcf6
pushq $0x41
popq %rsi
pushq $0x1
popq %rdx
movq -0x38(%rbp), %rcx
callq 0x44e0
jmp 0x7a29
jmp 0x7a7c
movq %rax, %rbx
movl $0xa0, %esi
jmp 0x7aa2
jmp 0x7a9a
movq %rax, %rbx
movl $0x98, %esi
jmp 0x7aa2
movq %rax, %rbx
movl $0xd0, %esi
jmp 0x7aa2
movq %rax, %rbx
movl $0x90, %esi
jmp 0x7aa2
movq %rax, %rbx
movl $0xa8, %esi
movq %r12, %rdi
callq 0x4260
movq %rbx, %rdi
callq 0x4480