Skip to content

Commit

Permalink
Bug in random forest compilation solved
Browse files Browse the repository at this point in the history
  • Loading branch information
ignaciorlando committed Oct 27, 2017
1 parent cf68050 commit c8f91a5
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 64 deletions.
3 changes: 3 additions & 0 deletions dr_setup.m
Expand Up @@ -35,6 +35,9 @@
warning('We could not find RF_Class_C. Please, download the package from here: https://github.com/PetterS/hep-2/tree/master/randomforest-matlab/RF_Class_C');
else
addpath(genpath(fullfile(root,'external','RF_Class_C'))) ; % code for random forests
cd external/RF_Class_C
compile_random_forest;
cd(root)
end

% if VLFeat does not exist, show a warning message
Expand Down
44 changes: 17 additions & 27 deletions external/RF_Class_C/Makefile
Expand Up @@ -14,61 +14,51 @@

#source directory
SRC=src/
SRC_FILES=$(SRC)/classRF.cpp $(SRC)/classTree.cpp $(SRC)/rfutils.cpp $(SRC)/cokus.cpp

#temporary .o output directory
BUILD=tempbuild/

CC=g++
FORTRAN=gfortran # or g77 whichever is present
CFLAGS= -fpic -O2 -funroll-loops -msse3#-g -Wall
FFLAGS=-O2 -fpic #-g
CFLAGS= -fpic -O2 -funroll-loops -march=native -Wall
FFLAGS=-O2 -fpic -march=native#-g -Wall
LDFORTRAN=#-gfortran
MEXFLAGS=-g
all: clean classTree cokus rfsub rfutils classRF twonorm mex
#all: regTree regrf rf rfsub rfutils classTree shared mex-setup
MEXFLAGS=-O

mex: clean classTree cokus rfsub rfutils mex_classRF
all: clean rfsub mex twonorm

twonorm: clean cokus classTree rfsub rfutils
twonorm: clean rfsub
echo 'Generating twonorm executable'
$(CC) $(CFLAGS) -c $(SRC)classRF.cpp -o $(BUILD)classRF.o
$(CC) $(CFLAGS) $(SRC)twonorm_C_wrapper.cpp $(SRC)classRF.cpp $(BUILD)classTree.o $(BUILD)rfutils.o rfsub.o $(BUILD)cokus.o -o twonorm_test -lgfortran -lm
$(CC) $(CFLAGS) $(SRC)twonorm_C_wrapper.cpp $(SRC_FILES) rfsub.o -o twonorm_test -lgfortran -lm -DmxCalloc=calloc -DmxFree=free

mex_classRF: $(SRC)classRF.cpp $(SRC)mex_ClassificationRF_train.cpp $(SRC)mex_ClassificationRF_predict.cpp
mex: rfsub
echo 'Generating Mex'
# mex -c $(SRC)classRF.cpp -outdir $(BUILD)classRF.o -DMATLAB $(MEXFLAGS)
mex $(SRC)mex_ClassificationRF_train.cpp $(SRC)classRF.cpp $(BUILD)classTree.o $(BUILD)rfutils.o rfsub.o $(BUILD)cokus.o -o mexClassRF_train -lgfortran -lm -DMATLAB $(MEXFLAGS)
mex $(SRC)mex_ClassificationRF_predict.cpp $(SRC)classRF.cpp $(BUILD)classTree.o $(BUILD)rfutils.o rfsub.o $(BUILD)cokus.o -o mexClassRF_predict -lgfortran -lm -DMATLAB $(MEXFLAGS)
mex $(SRC)mex_ClassificationRF_train.cpp $(SRC_FILES) rfsub.o -o mexClassRF_train -lgfortran -lm -DMATLAB $(MEXFLAGS)
mex $(SRC)mex_ClassificationRF_predict.cpp $(SRC_FILES) rfsub.o -o mexClassRF_predict -lgfortran -lm -DMATLAB $(MEXFLAGS)

cokus: $(SRC)cokus.cpp
echo 'Compiling Cokus (random number generator)'
$(CC) $(CFLAGS) -c $(SRC)cokus.cpp -o $(BUILD)cokus.o

classRF: $(SRC)classRF.cpp
$(CC) $(CFLAGS) -c $(SRC)classRF.cpp -o $(BUILD)classRF.o
# $(CC) $(CFLAGS) classRF.o classTree.o rfutils.o rfsub.o cokus.o -o classRF $(LDFORTRAN)
octave: rfsub
echo -e 'Generating Octave'
# mex -c $(SRC)classRF.cpp -outdir $(BUILD)classRF.o -DMATLAB $(MEXFLAGS)
mkoctfile $(SRC)mex_ClassificationRF_train.cpp $(SRC_FILES) rfsub.o -o mexClassRF_train -DMATLAB -DOCTAVE --mex
mkoctfile $(SRC)mex_ClassificationRF_predict.cpp $(SRC_FILES) rfsub.o -o mexClassRF_predict -DMATLAB -DOCTAVE --mex

classTree: $(SRC)classTree.cpp
echo 'Compiling classTree.cpp'
$(CC) $(CFLAGS) -c $(SRC)classTree.cpp -o $(BUILD)classTree.o


rfsub: $(SRC)rfsub.f
echo 'Compiling rfsub.f (fortran subroutines)'
$(FORTRAN) $(FFLAGS) -c $(SRC)rfsub.f -o rfsub.o
#for compiling via a cross compiler for 64 bit
# x86_64-pc-mingw32-gfortran -c $(SRC)rfsub.f -o rfsub.o

rfutils: $(SRC)rfutils.cpp
echo 'Compiling rfutils.cpp'
$(CC) $(CFLAGS) -c $(SRC)rfutils.cpp -o $(BUILD)rfutils.o


clean:
rm twonorm_test -rf
rm $(BUILD)*.o *.o -rf
rm *~ -rf
rm *.mexw32 twonorm_test -rf
rm *.mexa64 -rf
rm *.mex -rf
rm classRF -rf
rm *.exe -rf
rm *.exe -rf
16 changes: 0 additions & 16 deletions external/RF_Class_C/compile_linux.m

This file was deleted.

66 changes: 66 additions & 0 deletions external/RF_Class_C/compile_random_forest.m
@@ -0,0 +1,66 @@
% ********************************************************************
% * mex File compiling code for Random Forest (for linux)
% * mex interface to Andy Liaw et al.'s C code (used in R package randomForest)
% * Added by José Ignacio Orlando ( jiorlando@conicet.gov.ar )
% ********************************************************************/
function compile_random_forest

% prepare folder were the object files are contained
archstr = computer('arch');

if ispc()

if strcmp(archstr(end-1:end), '32')
% Compile the object file (if it does not exist)
if exist('precompiled_rfsub/win32/rfsub.o', 'file')==0
mex -outdir precompiled_rfsub/win32 -c src/rfsub.f
end
mex -DMATLAB -output mexClassRF_train src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/win32/rfsub.o src/mex_ClassificationRF_train.cpp src/rfutils.cpp
mex -DMATLAB -output mexClassRF_predict src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/win32/rfsub.o src/mex_ClassificationRF_predict.cpp src/rfutils.cpp
else
% Compile the object file (if it does not exist)
if exist('precompiled_rfsub/win64/rfsub.o', 'file')==0
mex -outdir precompiled_rfsub/win64 -c src/rfsub.f
end
mex -DMATLAB -output mexClassRF_train src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/win64/rfsub.o src/mex_ClassificationRF_train.cpp src/rfutils.cpp
mex -DMATLAB -output mexClassRF_predict src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/win64/rfsub.o src/mex_ClassificationRF_predict.cpp src/rfutils.cpp
end

elseif isunix()

if strcmp(archstr(end-1:end), '32')
% Compile the object file (if it does not exist)
if exist('precompiled_rfsub/linux32/rfsub.o', 'file')==0
mex -outdir precompiled_rfsub/linux32 -c src/rfsub.f
end
mex -DMATLAB -output mexClassRF_train src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/linux32/rfsub.o src/mex_ClassificationRF_train.cpp src/rfutils.cpp
mex -DMATLAB -output mexClassRF_predict src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/linux32/rfsub.o src/mex_ClassificationRF_predict.cpp src/rfutils.cpp
else
% Compile the object file (if it does not exist)
if exist('precompiled_rfsub/linux64/rfsub.o', 'file')==0
mex -outdir precompiled_rfsub/linux64 -c src/rfsub.f
end
mex -DMATLAB -output mexClassRF_train src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/linux64/rfsub.o src/mex_ClassificationRF_train.cpp src/rfutils.cpp %-lm -DMATLAB -O -lgfortran
mex -DMATLAB -output mexClassRF_predict src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/linux64/rfsub.o src/mex_ClassificationRF_predict.cpp src/rfutils.cpp
end

elseif ismac()

if strcmp(archstr(end-1:end), '32')
% Compile the object file (if it does not exist)
if exist('precompiled_rfsub/mac32/rfsub.o', 'file')==0
mex -outdir precompiled_rfsub/mac32 -c src/rfsub.f
end
mex -DMATLAB -output mexClassRF_train src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/mac32/rfsub.o src/mex_ClassificationRF_train.cpp src/rfutils.cpp
mex -DMATLAB -output mexClassRF_predict src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/mac32/rfsub.o src/mex_ClassificationRF_predict.cpp src/rfutils.cpp
else
% Compile the object file (if it does not exist)
if exist('precompiled_rfsub/linux64/rfsub.o', 'file')==0
mex -outdir precompiled_rfsub/mac64 -c src/rfsub.f
end
mex -DMATLAB -output mexClassRF_train src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/mac32/rfsub.o src/mex_ClassificationRF_train.cpp src/rfutils.cpp
mex -DMATLAB -output mexClassRF_predict src/classRF.cpp src/classTree.cpp src/cokus.cpp precompiled_rfsub/mac32/rfsub.o src/mex_ClassificationRF_predict.cpp src/rfutils.cpp
end

end

Binary file added external/RF_Class_C/mexClassRF_predict.mexa64
Binary file not shown.
Binary file added external/RF_Class_C/mexClassRF_train.mexa64
Binary file not shown.
Binary file not shown.
Binary file removed external/RF_Class_C/rfsub.o
Binary file not shown.
42 changes: 21 additions & 21 deletions external/RF_Class_C/src/mex_ClassificationRF_train.cpp
Expand Up @@ -86,15 +86,15 @@ void mexFunction( int nlhs, mxArray *plhs[],
int nt=ntree;
int ipi=*((int*)mxGetData(prhs[10])); // ipi: 0=use class proportion as prob.; 1=use supplied priors
plhs[10] = mxCreateDoubleScalar(mtry);
plhs[3] = mxCreateNumericMatrix(nclass, 1, mxDOUBLE_CLASS, 0);
plhs[3] = mxCreateNumericMatrix(nclass, 1, mxDOUBLE_CLASS, (mxComplexity) 0);
double *classwt = (double*) mxGetData(plhs[3]);
_tmp_d = (double*) mxGetData(prhs[11]);

//NOW COPY THE CLASSWT'S
memcpy(classwt,_tmp_d,nclass*sizeof(double));


plhs[4] = mxCreateNumericMatrix(nclass, 1, mxDOUBLE_CLASS, 0);
plhs[4] = mxCreateNumericMatrix(nclass, 1, mxDOUBLE_CLASS, (mxComplexity) 0);
double *cutoff= (double*) mxGetData(plhs[4]);
_tmp_d = (double*) mxGetData(prhs[12]);

Expand All @@ -108,18 +108,18 @@ void mexFunction( int nlhs, mxArray *plhs[],
}
int nodesize=*((int*) mxGetData(prhs[13]));

plhs[11] = mxCreateNumericMatrix(nsample, 1, mxINT32_CLASS, 0);
plhs[11] = mxCreateNumericMatrix(nsample, 1, mxINT32_CLASS, (mxComplexity) 0);
int* outcl=(int*) mxGetData(plhs[11]); //calloc(nsample,sizeof(int));

plhs[12] = mxCreateNumericMatrix(nclass, nsample, mxINT32_CLASS, 0);
plhs[12] = mxCreateNumericMatrix(nclass, nsample, mxINT32_CLASS, (mxComplexity) 0);
int* counttr=(int*) mxGetData(plhs[12]); //calloc(nclass*nsample,sizeof(int));

double* prox;
if (proximity){
plhs[13] = mxCreateNumericMatrix(nsample, nsample, mxDOUBLE_CLASS, 0);
plhs[13] = mxCreateNumericMatrix(nsample, nsample, mxDOUBLE_CLASS, (mxComplexity) 0);
prox = (double*) mxGetData(plhs[13]); //calloc(nsample*nsample,sizeof(double));
}else{
plhs[13] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, 0);
plhs[13] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, (mxComplexity) 0);
prox = (double*) mxGetData(plhs[13]); //calloc(1,sizeof(double));
prox[0]=1;
}
Expand All @@ -128,23 +128,23 @@ void mexFunction( int nlhs, mxArray *plhs[],
double* impSD;

if (localImp){
plhs[14] = mxCreateNumericMatrix(n_size, p_size, mxDOUBLE_CLASS, 0);
plhs[14] = mxCreateNumericMatrix(n_size, p_size, mxDOUBLE_CLASS, (mxComplexity) 0);
impmat = (double*) mxGetData(plhs[14]); //calloc(n_size*p_size,sizeof(double));
}else{
plhs[14] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, 0);
plhs[14] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, (mxComplexity) 0);
impmat = (double*) mxGetData(plhs[14]); //calloc(1,sizeof(double));
impmat[0]=1;
}

if (importance){
plhs[15] = mxCreateNumericMatrix(p_size,(nclass+2), mxDOUBLE_CLASS, 0);
plhs[16] = mxCreateNumericMatrix(p_size,(nclass+1), mxDOUBLE_CLASS, 0);
plhs[15] = mxCreateNumericMatrix(p_size,(nclass+2), mxDOUBLE_CLASS, (mxComplexity) 0);
plhs[16] = mxCreateNumericMatrix(p_size,(nclass+1), mxDOUBLE_CLASS, (mxComplexity) 0);

impout=(double*) mxGetData(plhs[15]); //calloc(p_size*(nclass+2),sizeof(double));
impSD =(double*) mxGetData(plhs[16]); //calloc(p_size*(nclass+1),sizeof(double));
}else{
plhs[15] = mxCreateNumericMatrix(p_size,1, mxDOUBLE_CLASS, 0);
plhs[16] = mxCreateNumericMatrix(1,1, mxDOUBLE_CLASS, 0);
plhs[15] = mxCreateNumericMatrix(p_size,1, mxDOUBLE_CLASS, (mxComplexity) 0);
plhs[16] = mxCreateNumericMatrix(1,1, mxDOUBLE_CLASS, (mxComplexity) 0);

impout=(double*) mxGetData(plhs[15]); //calloc(p_size,sizeof(double));
impSD =(double*) mxGetData(plhs[16]); //calloc(1,sizeof(double));
Expand All @@ -154,32 +154,32 @@ void mexFunction( int nlhs, mxArray *plhs[],
if (DEBUG_ON) { mexPrintf("\nnrnodes=%d, nsum=%d, nodesize=%d, mtry=%d\n",nrnodes,nsum,nodesize,mtry);}

//int* ndbigtree = (int*) calloc(ntree,sizeof(int));
plhs[9] = mxCreateNumericMatrix(nrnodes, nt, mxINT32_CLASS, 0);
plhs[9] = mxCreateNumericMatrix(nrnodes, nt, mxINT32_CLASS, (mxComplexity) 0);
int *ndbigtree = (int*) mxGetData(plhs[9]);

//int* nodestatus = (int*) calloc(nt*nrnodes,sizeof(int));
plhs[6] = mxCreateNumericMatrix(nrnodes, nt, mxINT32_CLASS, 0);
plhs[6] = mxCreateNumericMatrix(nrnodes, nt, mxINT32_CLASS, (mxComplexity) 0);
int *nodestatus = (int*) mxGetData(plhs[6]);

//int* bestvar = (int*) calloc(nt*nrnodes,sizeof(int));
plhs[8] = mxCreateNumericMatrix(nrnodes, nt, mxINT32_CLASS, 0);
plhs[8] = mxCreateNumericMatrix(nrnodes, nt, mxINT32_CLASS, (mxComplexity) 0);
int *bestvar = (int*) mxGetData(plhs[8]);

//int* treemap = (int*) calloc(nt * 2 * nrnodes,sizeof(int));
plhs[5] = mxCreateNumericMatrix(nrnodes, 2*nt, mxINT32_CLASS, 0);
plhs[5] = mxCreateNumericMatrix(nrnodes, 2*nt, mxINT32_CLASS, (mxComplexity) 0);
int *treemap = (int*) mxGetData(plhs[5]);


//int* nodepred = (int*) calloc(nt * nrnodes,sizeof(int));
plhs[7] = mxCreateNumericMatrix(nrnodes, nt, mxINT32_CLASS, 0);
plhs[7] = mxCreateNumericMatrix(nrnodes, nt, mxINT32_CLASS, (mxComplexity) 0);
int *nodepred = (int*) mxGetData(plhs[7]);


//double* xbestsplit = (double*) calloc(nt * nrnodes,sizeof(double));
plhs[2] = mxCreateNumericMatrix(nrnodes, nt, mxDOUBLE_CLASS, 0);
plhs[2] = mxCreateNumericMatrix(nrnodes, nt, mxDOUBLE_CLASS, (mxComplexity) 0);
double *xbestsplit = (double*) mxGetData(plhs[2]);

plhs[17] = mxCreateNumericMatrix((nclass+1), ntree, mxDOUBLE_CLASS, 0);
plhs[17] = mxCreateNumericMatrix((nclass+1), ntree, mxDOUBLE_CLASS, (mxComplexity) 0);
double* errtr = (double*) mxGetData(plhs[17]); //calloc((nclass+1) * ntree,sizeof(double));

int testdat=0;
Expand All @@ -193,10 +193,10 @@ void mexFunction( int nlhs, mxArray *plhs[],
double errts=1;
int* inbag;
if (keep_inbag){
plhs[18] = mxCreateNumericMatrix(n_size, ntree, mxINT32_CLASS, 0);
plhs[18] = mxCreateNumericMatrix(n_size, ntree, mxINT32_CLASS, (mxComplexity) 0);
inbag = (int*) mxGetData(plhs[18]);//calloc(n_size,sizeof(int));
}else{
plhs[18] = mxCreateNumericMatrix(n_size, 1, mxINT32_CLASS, 0);
plhs[18] = mxCreateNumericMatrix(n_size, 1, mxINT32_CLASS, (mxComplexity) 0);
inbag = (int*) mxGetData(plhs[18]);//calloc(n_size*ntree,sizeof(int));
}
if (DEBUG_ON){
Expand Down
13 changes: 13 additions & 0 deletions external/RF_Class_C/texput.log
@@ -0,0 +1,13 @@
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian) (format=mex 2017.10.12) 27 OCT 2017 11:31
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
**mex_ClassificationRF_train.cpp classRF.cpp classTree.o rfutils.o rfsub.o coku
s.o -o mexClassRF_train -lgfortran -lm -DMATLAB

! Emergency stop.
<*> mex_ClassificationRF_train.cpp
classRF.cpp classTree.o rfutils.o rfsub.o...
End of file on the terminal!

No pages of output.

0 comments on commit c8f91a5

Please sign in to comment.