// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/trainhmmst.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(task.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(task.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)task.nI);
    NDAT *fold_counts = Calloc(NDAT, (size_t)task.cv_folds);
    srand ( (unsigned int)time(NULL) ); // randomize

    // folds file
    if(task.cv_folds_file[0] > 0) { // file is specified
        if(task.cv_inout_flag=='i') {
            fid_folds = fopen(task.cv_folds_file,"r");
            if(fid_folds == NULL)
            {
                fprintf(stderr, "Can't open folds file for reading %s\n",filename);
                if(task.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(task.cv_inout_flag=='o') {
            fid_folds = fopen(task.cv_folds_file,"w");
            if(fid_folds == NULL)
            {
                fprintf(stderr, "Can't open folds file for writing %s\n",filename);
                if(task.duplicate_console==1) fprintf(fid_console, "Can't open folds file for writing %s\n",filename);
                exit(1);
            }
        }
    }
    for(I=0; I<task.nI; I++) {
        if( task.cv_folds_file[0]==0 || (task.cv_folds_file[0] > 0 && task.cv_inout_flag=='o') ) { // output or default
            folds[I] = (NPAR)(rand() % task.cv_folds); // produce folds
            if(task.cv_folds_file[0] > 0 )
                fprintf(fid_folds,"%i\n",folds[I]); // write out
        } else if( (task.cv_folds_file[0] > 0 && task.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(task.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(task.cv_folds_file[0] > 0) { // file is specified
        fclose(fid_folds);
        if(task.cv_inout_flag=='i')
            free(line);
    }
    
    // count number of items in each fold
    for(t=0; t<task.N; t++) fold_counts[ folds[task.dat_item[t]/*->get(t)*/] ]++;
    // create and fit multiple problems
    HMMProblemSt* hmms[task.cv_folds];
    int q = task.quiet;
//    task.quiet = 1;
    for(f=0; f<task.cv_folds; f++) {
        switch(task.structure)
        {
            case STRUCTURE_SKILL: // Conjugate Gradient Descent
//            case STRUCTURE_GROUP: // Conjugate Gradient Descent
                hmms[f] = new HMMProblemSt(&task);
                break;
            case STRUCTURE_ELO: // Conjugate Gradient Descent
                hmms[f] = new HMMProblemEloSt(&task);
                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;
            default:
                fprintf(stderr,"Solver specified (%d) is not supported!\n",task.structure);
                exit(1);
                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<task.N; t++) {
            if( folds[ task.dat_item[t]/*->get(t)*/ ] == f ) {
                saved_obs[count_saved++] = task.dat_obs[t];
                task.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<task.N; t++)
            if( folds[ task.dat_item[t]/*->get(t)*/ ] == f )
                task.dat_obs[t]=saved_obs[count_saved++];//->set(t, saved_obs[count_saved++]);
        free(saved_obs);
        printf("fold %d is done\n",f+1);
        if(task.duplicate_console==1) fprintf(fid_console, "fold %d is done\n",f+1);
    }
    free(fold_counts);
    task.quiet = (NPAR)q;

    tm0 = clock();//SEQ
//    _tm0 = omp_get_wtime();//PAR
	
	// new prediction
	//		create a general fold-identifying array
	NPAR *dat_fold = Calloc(NPAR, task.N);
	for(NDAT t=0; t<task.N; t++) dat_fold[t] = folds[ task.dat_item[t] ];
	//		predict
	HMMProblemSt::predict(metrics, filename,
                          //task.N, task.dat_obs, task.dat_group, task.dat_skill, task.dat_skill_stacked, task.dat_skill_rcount, task.dat_skill_rix,
                          &task,
                          hmms, task.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<task.cv_folds; f++) {
        n_par += hmms[f]->getModelParamN();
        delete hmms[f];
    }
    n_par /= task.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, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, %rbx
movq %rdi, %r14
cmpl $0x0, 0x14c36(%rip)     # 0x1a2e8
jle 0x56cc
leaq 0xca55(%rip), %rsi      # 0x12110
movq %rbx, %rdi
callq 0x3250
testq %rax, %rax
je 0x5b87
movq %r14, -0x58(%rbp)
movslq 0x154ad(%rip), %rdi     # 0x1ab84
pushq $0x1
popq %rsi
callq 0x3110
movq %rax, %r15
movsbq 0x1500a(%rip), %rdi     # 0x1a6f4
pushq $0x4
popq %rsi
callq 0x3110
movq %rax, %r13
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x31b0
movl %eax, %edi
callq 0x31d0
cmpb $0x0, 0x14fea(%rip)     # 0x1a6f7
jle 0x57b6
movzbl 0x153dd(%rip), %eax     # 0x1aaf7
cmpl $0x6f, %eax
je 0x5764
cmpl $0x69, %eax
jne 0x57b3
leaq 0x14fc8(%rip), %rdi     # 0x1a6f7
leaq 0xd4ed(%rip), %rsi      # 0x12c23
callq 0x3250
testq %rax, %rax
je 0x5bb4
movq %rax, %r14
movl $0x400, 0x154b7(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x32d0
movq %rax, 0x154ae(%rip)     # 0x1ac10
jmp 0x57b6
leaq 0x14f8c(%rip), %rdi     # 0x1a6f7
leaq 0xc99e(%rip), %rsi      # 0x12110
callq 0x3250
movq %rax, %r14
testq %rax, %rax
jne 0x57b6
movq 0x14852(%rip), %rax     # 0x19fd8
movq (%rax), %rdi
leaq 0xd4bb(%rip), %rsi      # 0x12c4b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3300
cmpb $0x1, 0x14b1b(%rip)     # 0x1a2bc
jne 0x5bed
leaq 0xd49d(%rip), %rsi      # 0x12c4b
jmp 0x5bdf
xorl %r14d, %r14d
movq %r13, -0x40(%rbp)
movq %r12, -0x48(%rbp)
movq %rbx, -0x50(%rbp)
leaq 0xea61(%rip), %rbx      # 0x1422a
leaq 0xcab6(%rip), %r12      # 0x12286
xorl %r13d, %r13d
movslq 0x153aa(%rip), %rcx     # 0x1ab84
movb 0x14f17(%rip), %al      # 0x1a6f7
cmpq %rcx, %r13
jge 0x5877
testb %al, %al
je 0x5806
setg %dl
movb 0x15301(%rip), %cl      # 0x1aaf7
cmpb $0x6f, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x5831
callq 0x30a0
movsbl 0x14ee2(%rip), %ecx     # 0x1a6f4
cltd
idivl %ecx
movb %dl, (%r15,%r13)
cmpb $0x0, 0x14ed7(%rip)     # 0x1a6f7
jle 0x586f
movq %r14, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3300
jmp 0x586f
testb %al, %al
setg %al
cmpb $0x69, %cl
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x586f
movq %r14, %rdi
callq 0x622e
movq 0x153be(%rip), %rdi     # 0x1ac10
movq %r12, %rsi
callq 0x3360
testq %rax, %rax
je 0x5bf5
movq %rax, %rdi
callq 0x3350
movb %al, (%r15,%r13)
incq %r13
jmp 0x57d3
testb %al, %al
jle 0x5898
movq %r14, %rdi
callq 0x31e0
cmpb $0x69, 0x1526d(%rip)    # 0x1aaf7
jne 0x5898
movq 0x1537d(%rip), %rdi     # 0x1ac10
callq 0x3260
movl 0x152d2(%rip), %eax     # 0x1ab70
movq 0x1528b(%rip), %rcx     # 0x1ab30
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
movq -0x40(%rbp), %r14
cmpq %rdx, %rax
je 0x58c7
movslq (%rcx,%rdx,4), %rsi
movsbq (%r15,%rsi), %rsi
incl (%r14,%rsi,4)
incq %rdx
jmp 0x58b0
movzbl 0x14e26(%rip), %eax     # 0x1a6f4
leal 0xf(,%rax,8), %ecx
andl $-0x10, %ecx
movq %rsp, %r13
subq %rcx, %r13
movq %r13, %rsp
movb 0x149dd(%rip), %cl      # 0x1a2c4
movb %cl, -0x29(%rbp)
xorl %r12d, %r12d
movq %r13, -0x60(%rbp)
movsbq %al, %rax
cmpq %rax, %r12
jge 0x5a90
movzbl 0x149c1(%rip), %eax     # 0x1a2c6
cmpl $0xe, %eax
je 0x5931
cmpl $0x1, %eax
jne 0x5c32
movl $0xd0, %edi
callq 0x31f0
movq %rax, %rbx
movq %rax, %rdi
leaq 0x1494e(%rip), %rsi     # 0x1a278
callq 0xc0c4
jmp 0x594d
movl $0x110, %edi            # imm = 0x110
callq 0x31f0
movq %rax, %rbx
movq %rax, %rdi
leaq 0x14930(%rip), %rsi     # 0x1a278
callq 0x1154e
movq %rbx, (%r13,%r12,8)
movslq (%r14,%r12,4), %rdi
pushq $0x1
popq %rsi
callq 0x3110
movq %rax, %r13
xorl %eax, %eax
movl 0x15207(%rip), %edx     # 0x1ab70
xorl %ecx, %ecx
movslq %edx, %rsi
cmpq %rsi, %rax
jge 0x59ac
movq 0x151b6(%rip), %rsi     # 0x1ab30
movslq (%rsi,%rax,4), %rsi
movzbl (%r15,%rsi), %esi
cmpq %rsi, %r12
jne 0x59a7
movq 0x15169(%rip), %rdx     # 0x1aaf8
movb (%rdx,%rax), %sil
movslq %ecx, %rdi
incl %ecx
movb %sil, (%r13,%rdi)
movb $-0x1, (%rdx,%rax)
movl 0x151c9(%rip), %edx     # 0x1ab70
incq %rax
jmp 0x596b
callq 0x3120
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x48(%rax)
callq 0x3120
subq %r14, %rax
movq -0x70(%rbp), %rcx
addq %rax, (%rcx)
xorl %r14d, %r14d
leaq -0x470(%rbp), %rdi
leaq 0xd2d6(%rip), %rsi      # 0x12cb3
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x3090
movq (%rbx), %rax
movq %rbx, %rdi
leaq -0x470(%rbp), %rsi
callq *0x40(%rax)
movl 0x1516f(%rip), %ecx     # 0x1ab70
xorl %eax, %eax
movslq %ecx, %rdx
cmpq %rdx, %r14
jge 0x5a40
movq 0x1511e(%rip), %rdx     # 0x1ab30
movslq (%rdx,%r14,4), %rdx
movzbl (%r15,%rdx), %edx
cmpq %rdx, %r12
jne 0x5a3b
movslq %eax, %rcx
incl %eax
movb (%r13,%rcx), %cl
movq 0x150c7(%rip), %rdx     # 0x1aaf8
movb %cl, (%rdx,%r14)
movl 0x15135(%rip), %ecx     # 0x1ab70
incq %r14
jmp 0x5a03
movq %r13, %rdi
callq 0x3260
leal 0x1(%r12), %ebx
leaq 0xd265(%rip), %rdi      # 0x12cb9
movl %ebx, %esi
xorl %eax, %eax
callq 0x3050
cmpb $0x1, 0x14858(%rip)     # 0x1a2bc
jne 0x5a7a
movq -0x38(%rbp), %rdi
leaq 0xd248(%rip), %rsi      # 0x12cb9
movl %ebx, %edx
xorl %eax, %eax
callq 0x3300
incq %r12
movb 0x14c71(%rip), %al      # 0x1a6f4
movq -0x40(%rbp), %r14
movq -0x60(%rbp), %r13
jmp 0x58f1
movq %r14, %rdi
callq 0x3260
movb -0x29(%rbp), %al
movb %al, 0x14823(%rip)      # 0x1a2c4
callq 0x3120
movq %rax, %rbx
movslq 0x150c0(%rip), %r14     # 0x1ab70
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x3110
movq %rax, %r12
movq 0x1506b(%rip), %rax     # 0x1ab30
xorl %ecx, %ecx
testl %r14d, %r14d
movl $0x0, %edx
cmovgl %r14d, %edx
movq -0x58(%rbp), %rdi
cmpq %rcx, %rdx
je 0x5aed
movslq (%rax,%rcx,4), %rsi
movb (%r15,%rsi), %sil
movb %sil, (%r12,%rcx)
incq %rcx
jmp 0x5ad7
movsbl 0x14bff(%rip), %r8d     # 0x1a6f4
leaq 0x1477c(%rip), %rdx     # 0x1a278
movq -0x50(%rbp), %rsi
movq %r13, %rcx
movq %r12, %r9
callq 0xec30
leaq 0xd2b5(%rip), %rdi      # 0x12dc7
callq 0x3340
movq %r12, %rdi
callq 0x3260
callq 0x3120
subq %rbx, %rax
movq -0x48(%rbp), %rcx
addq %rax, (%rcx)
xorl %r14d, %r14d
xorl %ebx, %ebx
movsbq 0x14bb9(%rip), %rcx     # 0x1a6f4
cmpq %rcx, %r14
jge 0x5b65
movq (%r13,%r14,8), %rdi
callq 0xf140
movl %eax, %r12d
movq (%r13,%r14,8), %rdi
testq %rdi, %rdi
je 0x5b5d
movq (%rdi), %rax
callq *0x8(%rax)
addl %r12d, %ebx
incq %r14
jmp 0x5b33
movl %ebx, %eax
cltd
idivl %ecx
movl %eax, %ebx
movq %r15, %rdi
callq 0x3260
cvtsi2sd %ebx, %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1444a(%rip), %rax     # 0x19fd8
movq (%rax), %rdi
leaq 0xd069(%rip), %rsi      # 0x12c01
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3300
cmpb $0x1, 0x14713(%rip)     # 0x1a2bc
jne 0x5bed
leaq 0xd04f(%rip), %rsi      # 0x12c01
jmp 0x5bdf
movq 0x1441d(%rip), %rax     # 0x19fd8
movq (%rax), %rdi
leaq 0xd060(%rip), %rsi      # 0x12c25
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3300
cmpb $0x1, 0x146e6(%rip)     # 0x1a2bc
jne 0x5bed
leaq 0xd046(%rip), %rsi      # 0x12c25
movq -0x38(%rbp), %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3300
pushq $0x1
popq %rdi
callq 0x3270
movq 0x143dc(%rip), %rax     # 0x19fd8
movq (%rax), %rcx
leaq 0xd06b(%rip), %rdi      # 0x12c71
pushq $0x41
popq %rsi
pushq $0x1
popq %rdx
callq 0x3430
cmpb $0x1, 0x146a4(%rip)     # 0x1a2bc
jne 0x5bed
leaq 0xd050(%rip), %rdi      # 0x12c71
pushq $0x41
popq %rsi
pushq $0x1
popq %rdx
movq -0x38(%rbp), %rcx
callq 0x3430
jmp 0x5bed
movsbl %al, %edx
movq 0x1439c(%rip), %rax     # 0x19fd8
movq (%rax), %rdi
leaq 0xc513(%rip), %rsi      # 0x12159
xorl %eax, %eax
callq 0x3300
jmp 0x5bed
movq %rax, %r14
movl $0x110, %esi            # imm = 0x110
jmp 0x5c61
movq %rax, %r14
movl $0xd0, %esi
movq %rbx, %rdi
callq 0x3200
movq %r14, %rdi
callq 0x33c0