// File: /workspace/github_cpp_makefile_star2_O1/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, -0x68(%rbp)
movq %rdx, -0x60(%rbp)
movq %rsi, %rbx
movq %rdi, -0x58(%rbp)
cmpl $0x0, 0x3c488(%rip)     # 0x45308
jle 0x8e9a
leaq 0x2e347(%rip), %rsi     # 0x371d0
movq %rbx, %rdi
callq 0x42c0
testq %rax, %rax
je 0x9414
movq %rbx, -0x48(%rbp)
movslq 0x3ccc7(%rip), %rdi     # 0x45b6c
movl $0x1, %esi
callq 0x4140
movq %rax, %rbx
movsbq 0x3c85a(%rip), %rdi     # 0x45714
movl $0x4, %esi
callq 0x4140
movq %rax, %r15
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x4200
movl %eax, %edi
callq 0x4220
cmpb $0x0, 0x3c838(%rip)     # 0x45717
jle 0x8f59
movzbl 0x3cc2f(%rip), %eax     # 0x45b17
cmpl $0x6f, %eax
je 0x8f2e
cmpl $0x69, %eax
jne 0x8f56
leaq 0x3c81e(%rip), %rdi     # 0x45717
leaq 0x2eda8(%rip), %rsi     # 0x37ca8
callq 0x42c0
testq %rax, %rax
je 0x9420
movq %rax, %r14
movl $0x400, 0x3cd35(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x4330
movq %rax, 0x3cd2c(%rip)     # 0x45c58
jmp 0x8f59
leaq 0x3c7e2(%rip), %rdi     # 0x45717
leaq 0x2e294(%rip), %rsi     # 0x371d0
callq 0x42c0
movq %rax, %r14
testq %rax, %rax
jne 0x8f59
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x4f0e
xorl %r14d, %r14d
movq %r15, -0x40(%rbp)
movq %r12, -0x50(%rbp)
cmpl $0x0, 0x3cc04(%rip)     # 0x45b6c
jle 0x901e
leaq 0x302a1(%rip), %r12     # 0x39216
leaq 0x2e3bc(%rip), %r15     # 0x37338
xorl %r13d, %r13d
movb 0x3c792(%rip), %al      # 0x45717
testb %al, %al
je 0x8fa2
setg %dl
movb 0x3cb85(%rip), %cl      # 0x45b17
cmpb $0x6f, %cl
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x8fcd
callq 0x40b0
movsbl 0x3c766(%rip), %ecx     # 0x45714
cltd
idivl %ecx
movb %dl, (%rbx,%r13)
cmpb $0x0, 0x3c75b(%rip)     # 0x45717
jle 0x900b
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x4360
jmp 0x900b
testb %al, %al
setg %al
cmpb $0x69, %cl
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x900b
movq %r14, %rdi
callq 0x9a6a
movq 0x3cc6a(%rip), %rdi     # 0x45c58
movq %r15, %rsi
callq 0x43e0
testq %rax, %rax
je 0x940b
movq %rax, %rdi
callq 0x43d0
movb %al, (%rbx,%r13)
incq %r13
movslq 0x3cb57(%rip), %rax     # 0x45b6c
cmpq %rax, %r13
jl 0x8f7f
cmpb $0x0, 0x3c6f2(%rip)     # 0x45717
jle 0x9044
movq %r14, %rdi
callq 0x4230
cmpb $0x69, 0x3cae1(%rip)    # 0x45b17
jne 0x9044
movq 0x3cc19(%rip), %rdi     # 0x45c58
callq 0x42d0
movslq 0x3cb0d(%rip), %rax     # 0x45b58
testq %rax, %rax
movq -0x40(%rbp), %rdi
jle 0x9071
movq 0x3caed(%rip), %rcx     # 0x45b48
xorl %edx, %edx
movslq (%rcx,%rdx,4), %rsi
movsbq (%rbx,%rsi), %rsi
incl (%rdi,%rsi,4)
incq %rdx
cmpq %rdx, %rax
jne 0x905d
movzbl 0x3c69c(%rip), %eax     # 0x45714
leal 0xf(,%rax,8), %ecx
andl $-0x10, %ecx
movq %rsp, %r13
subq %rcx, %r13
movq %r13, %rsp
movb 0x3c253(%rip), %cl      # 0x452e4
movb %cl, -0x29(%rbp)
testb %al, %al
jle 0x92e0
xorl %r12d, %r12d
movzbl 0x3c240(%rip), %eax     # 0x452e6
decl %eax
cmpl $0xb, %eax
ja 0x919c
leaq 0x2e0a0(%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 0x3c1d0(%rip), %rsi     # 0x452a8
callq 0x1447e
jmp 0x9197
movl $0xd0, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c1af(%rip), %rsi     # 0x452a8
callq 0x28868
jmp 0x9197
movl $0x98, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c18e(%rip), %rsi     # 0x452a8
callq 0x1d500
jmp 0x9197
movl $0xa8, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c170(%rip), %rsi     # 0x452a8
callq 0x2ec4c
jmp 0x9197
movl $0xa0, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c152(%rip), %rsi     # 0x452a8
callq 0x23774
jmp 0x9197
movl $0x98, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c134(%rip), %rsi     # 0x452a8
callq 0x2157c
jmp 0x9197
movl $0xa8, %edi
callq 0x4240
movq %rax, %r14
movq %rax, %rdi
leaq 0x3c116(%rip), %rsi     # 0x452a8
callq 0x25d4c
movq %r14, (%r13,%r12,8)
movq %r13, %r15
movq -0x40(%rbp), %rax
movslq (%rax,%r12,4), %rdi
movl $0x1, %esi
callq 0x4140
movq %rax, %r14
cmpl $0x0, 0x3c99d(%rip)     # 0x45b58
jle 0x91fc
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x3c980(%rip), %rdx     # 0x45b48
movslq (%rdx,%rax,4), %rdx
movzbl (%rbx,%rdx), %edx
cmpq %rdx, %r12
jne 0x91ed
movq 0x3c93c(%rip), %rdx     # 0x45b18
movb (%rdx,%rax), %sil
movslq %ecx, %rdi
incl %ecx
movb %sil, (%r14,%rdi)
movb $-0x1, (%rdx,%rax)
incq %rax
movslq 0x3c961(%rip), %rdx     # 0x45b58
cmpq %rdx, %rax
jl 0x91c1
callq 0x4150
movq %rax, %r13
movq (%r15,%r12,8), %rdi
movq (%rdi), %rax
callq *0x48(%rax)
callq 0x4150
subq %r13, %rax
movq %r15, %r13
movq -0x68(%rbp), %rcx
addq %rax, (%rcx)
leaq -0x470(%rbp), %r15
movq %r15, %rdi
leaq 0x2eb07(%rip), %rsi     # 0x37d38
movq -0x60(%rbp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x40a0
movq (%r13,%r12,8), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x40(%rax)
cmpl $0x0, 0x3c904(%rip)     # 0x45b58
jle 0x9290
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x3c8e7(%rip), %rdx     # 0x45b48
movslq (%rdx,%rax,4), %rdx
movzbl (%rbx,%rdx), %edx
cmpq %rdx, %r12
jne 0x9281
movslq %ecx, %rdx
incl %ecx
movb (%r14,%rdx), %dl
movq 0x3c89a(%rip), %rsi     # 0x45b18
movb %dl, (%rsi,%rax)
incq %rax
movslq 0x3c8cd(%rip), %rdx     # 0x45b58
cmpq %rdx, %rax
jl 0x925a
movq %r14, %rdi
callq 0x42d0
leal 0x1(%r12), %r14d
leaq 0x2ea9a(%rip), %rdi     # 0x37d3e
movl %r14d, %esi
xorl %eax, %eax
callq 0x4050
cmpb $0x1, 0x3c027(%rip)     # 0x452dc
jne 0x92cc
movq -0x38(%rbp), %rdi
leaq 0x2ea7c(%rip), %rsi     # 0x37d3e
movl %r14d, %edx
xorl %eax, %eax
callq 0x4360
incq %r12
movsbq 0x3c43d(%rip), %rax     # 0x45714
cmpq %rax, %r12
jl 0x909f
movq -0x40(%rbp), %rdi
callq 0x42d0
movb -0x29(%rbp), %al
movb %al, 0x3bff2(%rip)      # 0x452e4
callq 0x4150
movq %rax, %r14
movslq 0x3c857(%rip), %r12     # 0x45b58
movl $0x1, %esi
movq %r12, %rdi
callq 0x4140
movq %rax, %r15
testq %r12, %r12
jle 0x9332
movq 0x3c82b(%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 0x931f
movq 0x3c7df(%rip), %rdx     # 0x45b18
movq 0x3c7e0(%rip), %rcx     # 0x45b20
movq 0x3c7e1(%rip), %r8      # 0x45b28
movq 0x3c7e2(%rip), %r9      # 0x45b30
movups 0x3c7e3(%rip), %xmm0    # 0x45b38
movsbl 0x3c3b8(%rip), %eax     # 0x45714
subq $0x30, %rsp
movq %r15, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x16c6a
addq $0x30, %rsp
leaq 0x2eac0(%rip), %rdi     # 0x37e4a
callq 0x43b0
movq %r15, %rdi
callq 0x42d0
callq 0x4150
subq %r14, %rax
movq -0x50(%rbp), %rcx
addq %rax, (%rcx)
xorl %r12d, %r12d
xorl %r14d, %r14d
movq (%r13,%r12,8), %rdi
callq 0x17ce0
movl %eax, %r15d
movq (%r13,%r12,8), %rdi
testq %rdi, %rdi
je 0x93c9
movq (%rdi), %rax
callq *0x8(%rax)
addl %r15d, %r14d
incq %r12
movsbq 0x3c33d(%rip), %rax     # 0x45714
cmpq %rax, %r12
jl 0x93ac
movl %r14d, %eax
cltd
idivl %r12d
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, -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 0x4fa2
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x4fe8
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x4f58
movq %rax, %rbx
movl $0x90, %esi
jmp 0x9461
jmp 0x9443
movq %rax, %rbx
movl $0xa0, %esi
jmp 0x9461
movq %rax, %rbx
movl $0x98, %esi
jmp 0x9461
jmp 0x9459
movq %rax, %rbx
movl $0xd0, %esi
jmp 0x9461
movq %rax, %rbx
movl $0xa8, %esi
movq %r14, %rdi
callq 0x4250
movq %rbx, %rdi
callq 0x4460