// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rcx, -0x68(%rbp)
movq %rdx, -0x60(%rbp)
movq %rsi, %r12
movq %rdi, %r13
cmpl $0x0, 0x16b20(%rip)     # 0x1d2c8
jle 0x67c2
leaq 0xe95f(%rip), %rsi      # 0x15110
movq %r12, %rdi
callq 0x3260
testq %rax, %rax
je 0x6c6f
movslq 0x1739b(%rip), %rdi     # 0x1db64
movl $0x1, %esi
callq 0x3120
movq %rax, %rbx
movsbq 0x16ef6(%rip), %rdi     # 0x1d6d4
movl $0x4, %esi
callq 0x3120
movq %rax, -0x40(%rbp)
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x31b0
movl %eax, %edi
callq 0x31d0
cmpb $0x0, 0x16ed3(%rip)     # 0x1d6d7
jle 0x687d
movzbl 0x172ca(%rip), %eax     # 0x1dad7
cmpl $0x6f, %eax
je 0x6853
cmpl $0x69, %eax
jne 0x687a
leaq 0x16eb9(%rip), %rdi     # 0x1d6d7
leaq 0xf3fe(%rip), %rsi      # 0x15c23
callq 0x3260
testq %rax, %rax
je 0x6c7b
movq %rax, %r14
movl $0x400, 0x173a8(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x32d0
movq %rax, 0x1739f(%rip)     # 0x1dbf0
jmp 0x687d
leaq 0x16e7d(%rip), %rdi     # 0x1d6d7
leaq 0xe8af(%rip), %rsi      # 0x15110
callq 0x3260
movq %rax, %r14
testq %rax, %rax
jne 0x687d
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0x3e6d
xorl %r14d, %r14d
movq %r13, -0x48(%rbp)
movq %r12, -0x50(%rbp)
movq %r15, -0x58(%rbp)
cmpl $0x0, 0x172d4(%rip)     # 0x1db64
jle 0x6946
leaq 0x108fd(%rip), %r12     # 0x1719a
leaq 0xe9e2(%rip), %r13      # 0x15286
xorl %r15d, %r15d
movb 0x16e2a(%rip), %al      # 0x1d6d7
testb %al, %al
je 0x68ca
setg %dl
movb 0x1721d(%rip), %cl      # 0x1dad7
cmpb $0x6f, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x68f5
callq 0x30a0
movsbl 0x16dfe(%rip), %ecx     # 0x1d6d4
cltd
idivl %ecx
movb %dl, (%rbx,%r15)
cmpb $0x0, 0x16df3(%rip)     # 0x1d6d7
jle 0x6933
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x32e0
jmp 0x6933
testb %al, %al
setg %al
cmpb $0x69, %cl
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6933
movq %r14, %rdi
callq 0x71ae
movq 0x172da(%rip), %rdi     # 0x1dbf0
movq %r13, %rsi
callq 0x3350
testq %rax, %rax
je 0x6c66
movq %rax, %rdi
callq 0x3340
movb %al, (%rbx,%r15)
incq %r15
movslq 0x17227(%rip), %rax     # 0x1db64
cmpq %rax, %r15
jl 0x68a7
cmpb $0x0, 0x16d8a(%rip)     # 0x1d6d7
jle 0x696c
movq %r14, %rdi
callq 0x31e0
cmpb $0x69, 0x17179(%rip)    # 0x1dad7
jne 0x696c
movq 0x17289(%rip), %rdi     # 0x1dbf0
callq 0x3270
movslq 0x171dd(%rip), %rax     # 0x1db50
testq %rax, %rax
movq -0x40(%rbp), %r15
jle 0x699a
movq 0x1718d(%rip), %rcx     # 0x1db10
xorl %edx, %edx
movslq (%rcx,%rdx,4), %rsi
movsbq (%rbx,%rsi), %rsi
incl (%r15,%rsi,4)
incq %rdx
cmpq %rdx, %rax
jne 0x6985
movzbl 0x16d33(%rip), %eax     # 0x1d6d4
leal 0xf(,%rax,8), %ecx
andl $-0x10, %ecx
movq %rsp, %r13
subq %rcx, %r13
movq %r13, %rsp
movb 0x168ea(%rip), %cl      # 0x1d2a4
movb %cl, -0x29(%rbp)
testb %al, %al
jle 0x6b5b
xorl %r12d, %r12d
movzbl 0x168d7(%rip), %eax     # 0x1d2a6
cmpl $0xe, %eax
je 0x69fb
cmpl $0x1, %eax
jne 0x6c5e
movl $0xd0, %edi
callq 0x31f0
movq %rax, %r14
movq %rax, %rdi
leaq 0x16864(%rip), %rsi     # 0x1d258
callq 0xe854
jmp 0x6a17
movl $0x110, %edi            # imm = 0x110
callq 0x31f0
movq %rax, %r14
movq %rax, %rdi
leaq 0x16846(%rip), %rsi     # 0x1d258
callq 0x142aa
movq %r14, (%r13,%r12,8)
movslq (%r15,%r12,4), %rdi
movl $0x1, %esi
callq 0x3120
movq %rax, %r14
cmpl $0x0, 0x1711c(%rip)     # 0x1db50
jle 0x6a75
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x170cf(%rip), %rdx     # 0x1db10
movslq (%rdx,%rax,4), %rdx
movzbl (%rbx,%rdx), %edx
cmpq %rdx, %r12
jne 0x6a66
movq 0x17083(%rip), %rdx     # 0x1dad8
movb (%rdx,%rax), %sil
movslq %ecx, %rdi
incl %ecx
movb %sil, (%r14,%rdi)
movb $-0x1, (%rdx,%rax)
incq %rax
movslq 0x170e0(%rip), %rdx     # 0x1db50
cmpq %rdx, %rax
jl 0x6a3a
callq 0x3130
movq %rax, %r15
movq (%r13,%r12,8), %rdi
movq (%rdi), %rax
callq *0x48(%rax)
callq 0x3130
subq %r15, %rax
movq -0x68(%rbp), %rcx
addq %rax, (%rcx)
leaq -0x470(%rbp), %r15
movq %r15, %rdi
leaq 0xf20b(%rip), %rsi      # 0x15cb3
movq -0x60(%rbp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x3090
movq (%r13,%r12,8), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x40(%rax)
cmpl $0x0, 0x17085(%rip)     # 0x1db50
jle 0x6b07
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x17038(%rip), %rdx     # 0x1db10
movslq (%rdx,%rax,4), %rdx
movzbl (%rbx,%rdx), %edx
cmpq %rdx, %r12
jne 0x6af8
movslq %ecx, %rdx
incl %ecx
movb (%r14,%rdx), %dl
movq 0x16fe3(%rip), %rsi     # 0x1dad8
movb %dl, (%rsi,%rax)
incq %rax
movslq 0x1704e(%rip), %rdx     # 0x1db50
cmpq %rdx, %rax
jl 0x6ad1
movq %r14, %rdi
callq 0x3270
leal 0x1(%r12), %r14d
leaq 0xf19e(%rip), %rdi      # 0x15cb9
movl %r14d, %esi
xorl %eax, %eax
callq 0x3050
cmpb $0x1, 0x16770(%rip)     # 0x1d29c
jne 0x6b43
movq -0x38(%rbp), %rdi
leaq 0xf180(%rip), %rsi      # 0x15cb9
movl %r14d, %edx
xorl %eax, %eax
callq 0x32e0
incq %r12
movsbq 0x16b86(%rip), %rax     # 0x1d6d4
cmpq %rax, %r12
movq -0x40(%rbp), %r15
jl 0x69c8
movq %r15, %rdi
callq 0x3270
movb -0x29(%rbp), %al
movb %al, 0x16738(%rip)      # 0x1d2a4
callq 0x3130
movq %rax, %r14
movslq 0x16fd5(%rip), %r12     # 0x1db50
movl $0x1, %esi
movq %r12, %rdi
callq 0x3120
movq %rax, %r15
testq %r12, %r12
jle 0x6bac
movq 0x16f79(%rip), %rax     # 0x1db10
xorl %ecx, %ecx
movslq (%rax,%rcx,4), %rdx
movb (%rbx,%rdx), %dl
movb %dl, (%r15,%rcx)
incq %rcx
cmpq %rcx, %r12
jne 0x6b99
movsbl 0x16b20(%rip), %r8d     # 0x1d6d4
leaq 0x1669d(%rip), %rdx     # 0x1d258
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq %r13, %rcx
movq %r15, %r9
callq 0x11858
leaq 0xf1f2(%rip), %rdi      # 0x15dc7
callq 0x3320
movq %r15, %rdi
callq 0x3270
callq 0x3130
subq %r14, %rax
movq -0x58(%rbp), %rcx
addq %rax, (%rcx)
movb 0x16add(%rip), %al      # 0x1d6d4
testb %al, %al
jle 0x6c33
xorl %r12d, %r12d
xorl %r14d, %r14d
movq (%r13,%r12,8), %rdi
callq 0x11e9a
movl %eax, %r15d
movq (%r13,%r12,8), %rdi
testq %rdi, %rdi
je 0x6c1e
movq (%rdi), %rax
callq *0x8(%rax)
addl %r15d, %r14d
incq %r12
movsbq 0x16aa8(%rip), %rax     # 0x1d6d4
cmpq %rax, %r12
jl 0x6c01
jmp 0x6c36
xorl %r14d, %r14d
movsbl %al, %ecx
movl %r14d, %eax
cltd
idivl %ecx
movl %eax, %r14d
movq %rbx, %rdi
callq 0x3270
cvtsi2sd %r14d, %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl %al, %edi
callq 0x3f47
movq -0x38(%rbp), %rdi
callq 0x3f01
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0x3f6c
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0x3eb7
movq %rax, %rbx
movl $0xd0, %esi
jmp 0x6c99
movq %rax, %rbx
movl $0x110, %esi            # imm = 0x110
movq %r14, %rdi
callq 0x3200
movq %rbx, %rdi
callq 0x33a0