// rdar://problem/8439196 A *b(bool cond) { // CHECK: define [[A:%.*]]* @_ZN5test31bEb(i1 zeroext // CHECK: [[SAVED0:%.*]] = alloca i8* // CHECK-NEXT: [[SAVED1:%.*]] = alloca i8* // CHECK-NEXT: [[CLEANUPACTIVE:%.*]] = alloca i1 // CHECK-NEXT: [[TMP:%.*]] = alloca [[A]], align 8 // CHECK: [[TMPACTIVE:%.*]] = alloca i1 // CHECK-NEXT: store i1 false, i1* [[CLEANUPACTIVE]] // CHECK: [[COND:%.*]] = trunc i8 {{.*}} to i1 // CHECK-NEXT: store i1 false, i1* [[TMPACTIVE]] // CHECK-NEXT: br i1 [[COND]] return (cond ? // CHECK: [[FOO:%.*]] = call i8* @_ZN5test33fooEv() // CHECK-NEXT: [[NEW:%.*]] = call i8* @_ZN5test31AnwEmPvd(i64 8, i8* [[FOO]], double [[CONST:.*]]) // CHECK-NEXT: store i8* [[NEW]], i8** [[SAVED0]] // CHECK-NEXT: store i8* [[FOO]], i8** [[SAVED1]] // CHECK-NEXT: store i1 true, i1* [[CLEANUPACTIVE]] // CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[NEW]] to [[A]]* // CHECK-NEXT: invoke void @_ZN5test35makeAEv([[A]]* sret [[TMP]]) // CHECK: store i1 true, i1* [[TMPACTIVE]] // CHECK-NEXT: invoke void @_ZN5test31AC1ERKS0_([[A]]* [[CAST]], [[A]]* [[TMP]]) // CHECK: store i1 false, i1* [[CLEANUPACTIVE]] // CHECK-NEXT: br label // -> cond.end new(foo(),10.0) A(makeA()) : // CHECK: [[MAKE:%.*]] = invoke [[A]]* @_ZN5test38makeAPtrEv() // CHECK: br label // -> cond.end makeAPtr()); // cond.end: // CHECK: [[RESULT:%.*]] = phi [[A]]* {{.*}}[[CAST]]{{.*}}[[MAKE]] // CHECK-NEXT: [[ISACTIVE:%.*]] = load i1* [[TMPACTIVE]] // CHECK-NEXT: br i1 [[ISACTIVE]] // CHECK: invoke void @_ZN5test31AD1Ev // CHECK: ret [[A]]* [[RESULT]] // in the EH path: // CHECK: [[ISACTIVE:%.*]] = load i1* [[CLEANUPACTIVE]] // CHECK-NEXT: br i1 [[ISACTIVE]] // CHECK: [[V0:%.*]] = load i8** [[SAVED0]] // CHECK-NEXT: [[V1:%.*]] = load i8** [[SAVED1]] // CHECK-NEXT: invoke void @_ZN5test31AdlEPvS1_d(i8* [[V0]], i8* [[V1]], double [[CONST]]) }
void classRF(double *x, int *dimx, int *cl, int *ncl, int *cat, int *maxcat, int *sampsize, int *strata, int *Options, int *ntree, int *nvar, int *ipi, double *classwt, double *cut, int *nodesize, int *outcl, int *counttr, double *prox, double *imprt, double *impsd, double *impmat, int *nrnodes, int *ndbigtree, int *nodestatus, int *bestvar, int *treemap, int *nodeclass, double *xbestsplit, double *errtr, int *testdat, double *xts, int *clts, int *nts, double *countts, int *outclts, int labelts, double *proxts, double *errts, int *inbag) { /****************************************************************** * C wrapper for random forests: get input from R and drive * the Fortran routines. * * Input: * * x: matrix of predictors (transposed!) * dimx: two integers: number of variables and number of cases * cl: class labels of the data * ncl: number of classes in the responsema * cat: integer vector of number of classes in the predictor; * 1=continuous * maxcat: maximum of cat * Options: 7 integers: (0=no, 1=yes) * add a second class (for unsupervised RF)? * 1: sampling from product of marginals * 2: sampling from product of uniforms * assess variable importance? * calculate proximity? * calculate proximity based on OOB predictions? * calculate outlying measure? * how often to print output? * keep the forest for future prediction? * ntree: number of trees * nvar: number of predictors to use for each split * ipi: 0=use class proportion as prob.; 1=use supplied priors * pi: double vector of class priors * nodesize: minimum node size: no node with fewer than ndsize * cases will be split * * Output: * * outcl: class predicted by RF * counttr: matrix of votes (transposed!) * imprt: matrix of variable importance measures * impmat: matrix of local variable importance measures * prox: matrix of proximity (if iprox=1) ******************************************************************/ int nsample0, mdim, nclass, addClass, mtry, ntest, nsample, ndsize, mimp, nimp, near, nuse, noutall, nrightall, nrightimpall, keepInbag, nstrata; int jb, j, n, m, k, idxByNnode, idxByNsample, imp, localImp, iprox, oobprox, keepf, replace, stratify, trace, *nright, *nrightimp, *nout, *nclts, Ntree; int *out, *bestsplitnext, *bestsplit, *nodepop, *jin, *nodex, *nodexts, *nodestart, *ta, *ncase, *jerr, *varUsed, *jtr, *classFreq, *idmove, *jvr, *at, *a, *b, *mind, *nind, *jts, *oobpair; int **strata_idx, *strata_size, last, ktmp, anyEmpty, ntry; double av=0.0; double *tgini, *tx, *wl, *classpop, *tclasscat, *tclasspop, *win, *tp, *wr; //Do initialization for COKUS's Random generator seedMT(2*rand()+1); //works well with odd number so why don't use that addClass = Options[0]; imp = Options[1]; localImp = Options[2]; iprox = Options[3]; oobprox = Options[4]; trace = Options[5]; keepf = Options[6]; replace = Options[7]; stratify = Options[8]; keepInbag = Options[9]; mdim = dimx[0]; nsample0 = dimx[1]; nclass = (*ncl==1) ? 2 : *ncl; ndsize = *nodesize; Ntree = *ntree; mtry = *nvar; ntest = *nts; nsample = addClass ? (nsample0 + nsample0) : nsample0; mimp = imp ? mdim : 1; nimp = imp ? nsample : 1; near = iprox ? nsample0 : 1; if (trace == 0) trace = Ntree + 1; /*printf("\nmdim %d, nclass %d, nrnodes %d, nsample %d, ntest %d\n", mdim, nclass, *nrnodes, nsample, ntest); printf("\noobprox %d, mdim %d, nsample0 %d, Ntree %d, mtry %d, mimp %d", oobprox, mdim, nsample0, Ntree, mtry, mimp); printf("\nstratify %d, replace %d",stratify,replace); printf("\n");*/ tgini = (double *) S_alloc_alt(mdim, sizeof(double)); wl = (double *) S_alloc_alt(nclass, sizeof(double)); wr = (double *) S_alloc_alt(nclass, sizeof(double)); classpop = (double *) S_alloc_alt(nclass* *nrnodes, sizeof(double)); tclasscat = (double *) S_alloc_alt(nclass*32, sizeof(double)); tclasspop = (double *) S_alloc_alt(nclass, sizeof(double)); tx = (double *) S_alloc_alt(nsample, sizeof(double)); win = (double *) S_alloc_alt(nsample, sizeof(double)); tp = (double *) S_alloc_alt(nsample, sizeof(double)); out = (int *) S_alloc_alt(nsample, sizeof(int)); bestsplitnext = (int *) S_alloc_alt(*nrnodes, sizeof(int)); bestsplit = (int *) S_alloc_alt(*nrnodes, sizeof(int)); nodepop = (int *) S_alloc_alt(*nrnodes, sizeof(int)); nodestart = (int *) S_alloc_alt(*nrnodes, sizeof(int)); jin = (int *) S_alloc_alt(nsample, sizeof(int)); nodex = (int *) S_alloc_alt(nsample, sizeof(int)); nodexts = (int *) S_alloc_alt(ntest, sizeof(int)); ta = (int *) S_alloc_alt(nsample, sizeof(int)); ncase = (int *) S_alloc_alt(nsample, sizeof(int)); jerr = (int *) S_alloc_alt(nsample, sizeof(int)); varUsed = (int *) S_alloc_alt(mdim, sizeof(int)); jtr = (int *) S_alloc_alt(nsample, sizeof(int)); jvr = (int *) S_alloc_alt(nsample, sizeof(int)); classFreq = (int *) S_alloc_alt(nclass, sizeof(int)); jts = (int *) S_alloc_alt(ntest, sizeof(int)); idmove = (int *) S_alloc_alt(nsample, sizeof(int)); at = (int *) S_alloc_alt(mdim*nsample, sizeof(int)); a = (int *) S_alloc_alt(mdim*nsample, sizeof(int)); b = (int *) S_alloc_alt(mdim*nsample, sizeof(int)); mind = (int *) S_alloc_alt(mdim, sizeof(int)); nright = (int *) S_alloc_alt(nclass, sizeof(int)); nrightimp = (int *) S_alloc_alt(nclass, sizeof(int)); nout = (int *) S_alloc_alt(nclass, sizeof(int)); if (oobprox) { oobpair = (int *) S_alloc_alt(near*near, sizeof(int)); } //printf("nsample=%d\n", nsample); /* Count number of cases in each class. */ zeroInt(classFreq, nclass); for (n = 0; n < nsample; ++n) classFreq[cl[n] - 1] ++; /* Normalize class weights. */ //Rprintf("ipi %d ",*ipi); //for(n=0;n<nclass;n++) Rprintf("%d: %d, %f,",n,classFreq[n],classwt[n]); normClassWt(cl, nsample, nclass, *ipi, classwt, classFreq); //for(n=0;n<nclass;n++) Rprintf("%d: %d, %f,",n,classFreq[n],classwt[n]); if (stratify) { /* Count number of strata and frequency of each stratum. */ nstrata = 0; for (n = 0; n < nsample0; ++n) if (strata[n] > nstrata) nstrata = strata[n]; /* Create the array of pointers, each pointing to a vector * of indices of where data of each stratum is. */ strata_size = (int *) S_alloc_alt(nstrata, sizeof(int)); for (n = 0; n < nsample0; ++n) { strata_size[strata[n] - 1] ++; } strata_idx = (int **) S_alloc_alt(nstrata, sizeof(int *)); for (n = 0; n < nstrata; ++n) { strata_idx[n] = (int *) S_alloc_alt(strata_size[n], sizeof(int)); } zeroInt(strata_size, nstrata); for (n = 0; n < nsample0; ++n) { strata_size[strata[n] - 1] ++; strata_idx[strata[n] - 1][strata_size[strata[n] - 1] - 1] = n; } } else { nind = replace ? NULL : (int *) S_alloc_alt(nsample, sizeof(int)); } /* INITIALIZE FOR RUN */ if (*testdat) zeroDouble(countts, ntest * nclass); zeroInt(counttr, nclass * nsample); zeroInt(out, nsample); zeroDouble(tgini, mdim); zeroDouble(errtr, (nclass + 1) * Ntree); if (labelts) { nclts = (int *) S_alloc_alt(nclass, sizeof(int)); for (n = 0; n < ntest; ++n) nclts[clts[n]-1]++; zeroDouble(errts, (nclass + 1) * Ntree); } //printf("labelts %d\n",labelts);fflush(stdout); if (imp) { zeroDouble(imprt, (nclass+2) * mdim); zeroDouble(impsd, (nclass+1) * mdim); if (localImp) zeroDouble(impmat, nsample * mdim); } if (iprox) { zeroDouble(prox, nsample0 * nsample0); if (*testdat) zeroDouble(proxts, ntest * (ntest + nsample0)); } makeA(x, mdim, nsample, cat, at, b); //R_CheckUserInterrupt(); /* Starting the main loop over number of trees. */ GetRNGstate(); if (trace <= Ntree) { /* Print header for running output. */ Rprintf("ntree OOB"); for (n = 1; n <= nclass; ++n) Rprintf("%7i", n); if (labelts) { Rprintf("| Test"); for (n = 1; n <= nclass; ++n) Rprintf("%7i", n); } Rprintf("\n"); } idxByNnode = 0; idxByNsample = 0; //Rprintf("addclass %d, ntree %d, cl[300]=%d", addClass,Ntree,cl[299]); for(jb = 0; jb < Ntree; jb++) { //Rprintf("addclass %d, ntree %d, cl[300]=%d", addClass,Ntree,cl[299]); //printf("jb=%d,\n",jb); /* Do we need to simulate data for the second class? */ if (addClass) createClass(x, nsample0, nsample, mdim); do { zeroInt(nodestatus + idxByNnode, *nrnodes); zeroInt(treemap + 2*idxByNnode, 2 * *nrnodes); zeroDouble(xbestsplit + idxByNnode, *nrnodes); zeroInt(nodeclass + idxByNnode, *nrnodes); zeroInt(varUsed, mdim); /* TODO: Put all sampling code into a function. */ /* drawSample(sampsize, nsample, ); */ if (stratify) { /* stratified sampling */ zeroInt(jin, nsample); zeroDouble(tclasspop, nclass); zeroDouble(win, nsample); if (replace) { /* with replacement */ for (n = 0; n < nstrata; ++n) { for (j = 0; j < sampsize[n]; ++j) { ktmp = (int) (unif_rand() * strata_size[n]); k = strata_idx[n][ktmp]; tclasspop[cl[k] - 1] += classwt[cl[k] - 1]; win[k] += classwt[cl[k] - 1]; jin[k] = 1; } } } else { /* stratified sampling w/o replacement */ /* re-initialize the index array */ zeroInt(strata_size, nstrata); for (j = 0; j < nsample; ++j) { strata_size[strata[j] - 1] ++; strata_idx[strata[j] - 1][strata_size[strata[j] - 1] - 1] = j; } /* sampling without replacement */ for (n = 0; n < nstrata; ++n) { last = strata_size[n] - 1; for (j = 0; j < sampsize[n]; ++j) { ktmp = (int) (unif_rand() * (last+1)); k = strata_idx[n][ktmp]; swapInt(strata_idx[n][last], strata_idx[n][ktmp]); last--; tclasspop[cl[k] - 1] += classwt[cl[k]-1]; win[k] += classwt[cl[k]-1]; jin[k] = 1; } } } } else { /* unstratified sampling */ anyEmpty = 0; ntry = 0; do { zeroInt(jin, nsample); zeroDouble(tclasspop, nclass); zeroDouble(win, nsample); if (replace) { for (n = 0; n < *sampsize; ++n) { k = unif_rand() * nsample; tclasspop[cl[k] - 1] += classwt[cl[k]-1]; win[k] += classwt[cl[k]-1]; jin[k] = 1; } } else { for (n = 0; n < nsample; ++n) nind[n] = n; last = nsample - 1; for (n = 0; n < *sampsize; ++n) { ktmp = (int) (unif_rand() * (last+1)); k = nind[ktmp]; swapInt(nind[ktmp], nind[last]); last--; tclasspop[cl[k] - 1] += classwt[cl[k]-1]; win[k] += classwt[cl[k]-1]; jin[k] = 1; } } /* check if any class is missing in the sample */ for (n = 0; n < nclass; ++n) { if (tclasspop[n] == 0) anyEmpty = 1; } ntry++; } while (anyEmpty && ntry <= 10); } /* If need to keep indices of inbag data, do that here. */ if (keepInbag) { for (n = 0; n < nsample0; ++n) { inbag[n + idxByNsample] = jin[n]; } } /* Copy the original a matrix back. */ memcpy(a, at, sizeof(int) * mdim * nsample); modA(a, &nuse, nsample, mdim, cat, *maxcat, ncase, jin); #ifdef WIN64 F77_CALL(_buildtree) #endif #ifndef WIN64 F77_CALL(buildtree) #endif (a, b, cl, cat, maxcat, &mdim, &nsample, &nclass, treemap + 2*idxByNnode, bestvar + idxByNnode, bestsplit, bestsplitnext, tgini, nodestatus + idxByNnode, nodepop, nodestart, classpop, tclasspop, tclasscat, ta, nrnodes, idmove, &ndsize, ncase, &mtry, varUsed, nodeclass + idxByNnode, ndbigtree + jb, win, wr, wl, &mdim, &nuse, mind); /* if the "tree" has only the root node, start over */ } while (ndbigtree[jb] == 1); Xtranslate(x, mdim, *nrnodes, nsample, bestvar + idxByNnode, bestsplit, bestsplitnext, xbestsplit + idxByNnode, nodestatus + idxByNnode, cat, ndbigtree[jb]); /* Get test set error */ if (*testdat) { predictClassTree(xts, ntest, mdim, treemap + 2*idxByNnode, nodestatus + idxByNnode, xbestsplit + idxByNnode, bestvar + idxByNnode, nodeclass + idxByNnode, ndbigtree[jb], cat, nclass, jts, nodexts, *maxcat); TestSetError(countts, jts, clts, outclts, ntest, nclass, jb+1, errts + jb*(nclass+1), labelts, nclts, cut); } /* Get out-of-bag predictions and errors. */ predictClassTree(x, nsample, mdim, treemap + 2*idxByNnode, nodestatus + idxByNnode, xbestsplit + idxByNnode, bestvar + idxByNnode, nodeclass + idxByNnode, ndbigtree[jb], cat, nclass, jtr, nodex, *maxcat); zeroInt(nout, nclass); noutall = 0; for (n = 0; n < nsample; ++n) { if (jin[n] == 0) { /* increment the OOB votes */ counttr[n*nclass + jtr[n] - 1] ++; /* count number of times a case is OOB */ out[n]++; /* count number of OOB cases in the current iteration. * nout[n] is the number of OOB cases for the n-th class. * noutall is the number of OOB cases overall. */ nout[cl[n] - 1]++; noutall++; } } /* Compute out-of-bag error rate. */ oob(nsample, nclass, jin, cl, jtr, jerr, counttr, out, errtr + jb*(nclass+1), outcl, cut); if ((jb+1) % trace == 0) { Rprintf("%5i: %6.2f%%", jb+1, 100.0*errtr[jb * (nclass+1)]); for (n = 1; n <= nclass; ++n) { Rprintf("%6.2f%%", 100.0 * errtr[n + jb * (nclass+1)]); } if (labelts) { Rprintf("| "); for (n = 0; n <= nclass; ++n) { Rprintf("%6.2f%%", 100.0 * errts[n + jb * (nclass+1)]); } } Rprintf("\n"); //R_CheckUserInterrupt(); } /* DO VARIABLE IMPORTANCE */ if (imp) { nrightall = 0; /* Count the number of correct prediction by the current tree * among the OOB samples, by class. */ zeroInt(nright, nclass); for (n = 0; n < nsample; ++n) { /* out-of-bag and predicted correctly: */ if (jin[n] == 0 && jtr[n] == cl[n]) { nright[cl[n] - 1]++; nrightall++; } } for (m = 0; m < mdim; ++m) { if (varUsed[m]) { nrightimpall = 0; zeroInt(nrightimp, nclass); for (n = 0; n < nsample; ++n) tx[n] = x[m + n*mdim]; /* Permute the m-th variable. */ permuteOOB(m, x, jin, nsample, mdim); /* Predict the modified data using the current tree. */ predictClassTree(x, nsample, mdim, treemap + 2*idxByNnode, nodestatus + idxByNnode, xbestsplit + idxByNnode, bestvar + idxByNnode, nodeclass + idxByNnode, ndbigtree[jb], cat, nclass, jvr, nodex, *maxcat); /* Count how often correct predictions are made with * the modified data. */ for (n = 0; n < nsample; n++) { if (jin[n] == 0) { if (jvr[n] == cl[n]) { nrightimp[cl[n] - 1]++; nrightimpall++; } if (localImp && jvr[n] != jtr[n]) { if (cl[n] == jvr[n]) { impmat[m + n*mdim] -= 1.0; } else { impmat[m + n*mdim] += 1.0; } } } /* Restore the original data for that variable. */ x[m + n*mdim] = tx[n]; } /* Accumulate decrease in proportions of correct * predictions. */ for (n = 0; n < nclass; ++n) { if (nout[n] > 0) { imprt[m + n*mdim] += ((double) (nright[n] - nrightimp[n])) / nout[n]; impsd[m + n*mdim] += ((double) (nright[n] - nrightimp[n]) * (nright[n] - nrightimp[n])) / nout[n]; } } if (noutall > 0) { imprt[m + nclass*mdim] += ((double)(nrightall - nrightimpall)) / noutall; impsd[m + nclass*mdim] += ((double) (nrightall - nrightimpall) * (nrightall - nrightimpall)) / noutall; } } } } /* DO PROXIMITIES */ if (iprox) { computeProximity(prox, oobprox, nodex, jin, oobpair, near); /* proximity for test data */ if (*testdat) { computeProximity(proxts, 0, nodexts, jin, oobpair, ntest); /* Compute proximity between testset and training set. */ for (n = 0; n < ntest; ++n) { for (k = 0; k < near; ++k) { if (nodexts[n] == nodex[k]) proxts[n + ntest * (k+ntest)] += 1.0; } } } } if (keepf) idxByNnode += *nrnodes; if (keepInbag) idxByNsample += nsample0; } PutRNGstate(); /* Final processing of variable importance. */ for (m = 0; m < mdim; m++) tgini[m] /= Ntree; if (imp) { for (m = 0; m < mdim; ++m) { if (localImp) { /* casewise measures */ for (n = 0; n < nsample; ++n) impmat[m + n*mdim] /= out[n]; } /* class-specific measures */ for (k = 0; k < nclass; ++k) { av = imprt[m + k*mdim] / Ntree; impsd[m + k*mdim] = sqrt(((impsd[m + k*mdim] / Ntree) - av*av) / Ntree); imprt[m + k*mdim] = av; /* imprt[m + k*mdim] = (se <= 0.0) ? -1000.0 - av : av / se; */ } /* overall measures */ av = imprt[m + nclass*mdim] / Ntree; impsd[m + nclass*mdim] = sqrt(((impsd[m + nclass*mdim] / Ntree) - av*av) / Ntree); imprt[m + nclass*mdim] = av; imprt[m + (nclass+1)*mdim] = tgini[m]; } } else { for (m = 0; m < mdim; ++m) imprt[m] = tgini[m]; } /* PROXIMITY DATA ++++++++++++++++++++++++++++++++*/ if (iprox) { for (n = 0; n < near; ++n) { for (k = n + 1; k < near; ++k) { prox[near*k + n] /= oobprox ? (oobpair[near*k + n] > 0 ? oobpair[near*k + n] : 1) : Ntree; prox[near*n + k] = prox[near*k + n]; } prox[near*n + n] = 1.0; } if (*testdat) { for (n = 0; n < ntest; ++n) for (k = 0; k < ntest + nsample; ++k) proxts[ntest*k + n] /= Ntree; } } if (trace <= Ntree){ printf("\nmdim %d, nclass %d, nrnodes %d, nsample %d, ntest %d\n", mdim, nclass, *nrnodes, nsample, ntest); printf("\noobprox %d, mdim %d, nsample0 %d, Ntree %d, mtry %d, mimp %d", oobprox, mdim, nsample0, Ntree, mtry, mimp); printf("\nstratify %d, replace %d",stratify,replace); printf("\n"); } //frees up the memory free(tgini);free(wl);free(wr);free(classpop);free(tclasscat); free(tclasspop);free(tx);free(win);free(tp);free(out); free(bestsplitnext);free(bestsplit);free(nodepop);free(nodestart);free(jin); free(nodex);free(nodexts);free(ta);free(ncase);free(jerr); free(varUsed);free(jtr);free(jvr);free(classFreq);free(jts); free(idmove);free(at);free(a);free(b);free(mind); free(nright);free(nrightimp);free(nout); if (oobprox) { free(oobpair); } if (stratify) { free(strata_size); for (n = 0; n < nstrata; ++n) { free(strata_idx[n]); } free(strata_idx); } else { if (replace) free(nind); } //printf("labelts %d\n",labelts);fflush(stdout); if (labelts) { free(nclts); } //printf("stratify %d",stratify);fflush(stdout); }
void test() { throw makeA(); }