From 4d761b7c6897f4379d2484b2faa689787b2198be Mon Sep 17 00:00:00 2001 From: Joris Mooij Date: Wed, 8 Oct 2008 11:02:18 +0200 Subject: [PATCH] Cleanups of matlab, BP; small improvement of utils/fginfo --- Makefile | 15 +++++++-------- Makefile.shared | 20 ++++++++++---------- Makefile.win | 13 ++++++------- {matlab => include/dai/matlab}/matlab.h | 0 src/bp.cpp | 15 ++++++++++----- {matlab => src/matlab}/dai.cpp | 5 ++--- {matlab => src/matlab}/dai_potstrength.cpp | 0 {matlab => src/matlab}/dai_readfg.cpp | 0 {matlab => src/matlab}/dai_writefg.cpp | 4 +--- {matlab => src/matlab}/matlab.cpp | 0 utils/fginfo.cpp | 13 +++++++++++++ 11 files changed, 49 insertions(+), 36 deletions(-) rename {matlab => include/dai/matlab}/matlab.h (100%) rename {matlab => src/matlab}/dai.cpp (97%) rename {matlab => src/matlab}/dai_potstrength.cpp (100%) rename {matlab => src/matlab}/dai_readfg.cpp (100%) rename {matlab => src/matlab}/dai_writefg.cpp (95%) rename {matlab => src/matlab}/matlab.cpp (100%) diff --git a/Makefile b/Makefile index 4adbaff..5c1fdd7 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ INC=include/dai SRC=src LIB=lib -# Extensions (library, object, executable, MEX file extensions) +# Extensions (library, object, executable, matlab compiled MEX file) LE=.a OE=.o EE= @@ -59,7 +59,7 @@ CC=g++ CCO=-o # Flags for the C++ compiler -CCFLAGS=-Wno-deprecated -Wall -W -Wextra -fpic -I./include -Llib -O3 #-pg +CCFLAGS=-O3 -Wno-deprecated -Wall -W -Wextra -fpic -Iinclude -Llib ifdef DEBUG CCFLAGS:=$(CCFLAGS) -g -DDAI_DEBUG endif @@ -100,10 +100,9 @@ endif ifdef WITH_MATLAB # Replace the following by the directory where Matlab has been installed -MATLABDIR=/opt/matlab/bin -# Replace the following with the extension of compiled MEX files on this platform, e.g. .mexglx for x86 -MEX=$(MATLABDIR)/mex -MEXFLAGS=-I. +MATLABDIR=/agbs/share/sw/matlab +MEX=$(MATLABDIR)/bin/mex +MEXFLAGS=-Iinclude CXX\#$(CC) CXXFLAGS\#'-O3 -Wno-deprecated -Wall -W -Wextra -fpic' ifdef DEBUG MEXFLAGS:=$(MEXFLAGS) -g -DDAI_DEBUG endif @@ -143,8 +142,8 @@ doc : $(INC)/*.h $(SRC)/*.cpp doxygen.conf .PHONY : clean clean : - -rm *$(OE) - -rm matlab/*$(ME) matlab/*$(OE) + -rm *$(OE) + -rm matlab/*$(ME) -rm example$(EE) tests/testdai$(EE) utils/fg2dot$(EE) utils/createfg$(EE) utils/fginfo$(EE) -rm -R doc -rm -R lib diff --git a/Makefile.shared b/Makefile.shared index 581a07b..b9a1d94 100644 --- a/Makefile.shared +++ b/Makefile.shared @@ -91,20 +91,20 @@ tests/testdai$(EE) : tests/testdai.cpp $(HEADERS) $(LIB)/libdai$(LE) # MATLAB INTERFACE ################### -matlab/dai.$(ME) : matlab/dai.cpp $(HEADERS) matlab/matlab$(OE) $(LIB)/libdai$(LE) - $(MEX) $(MEXFLAGS) -o matlab/dai matlab/dai.cpp matlab/matlab$(OE) $(LIB)/libdai$(LE) +matlab/dai$(ME) : $(SRC)/matlab/dai.cpp $(HEADERS) matlab$(OE) $(LIB)/libdai$(LE) + $(MEX) $(MEXFLAGS) -o matlab/dai $(SRC)/matlab/dai.cpp matlab$(OE) $(LIB)/libdai$(LE) -matlab/dai_readfg.$(ME) : matlab/dai_readfg.cpp $(HEADERS) factorgraph$(OE) matlab/matlab$(OE) exceptions$(OE) - $(MEX) $(MEXFLAGS) -o matlab/dai_readfg matlab/dai_readfg.cpp factorgraph$(OE) matlab/matlab$(OE) exceptions$(OE) +matlab/dai_readfg$(ME) : $(SRC)/matlab/dai_readfg.cpp $(HEADERS) factorgraph$(OE) matlab$(OE) exceptions$(OE) + $(MEX) $(MEXFLAGS) -o matlab/dai_readfg $(SRC)/matlab/dai_readfg.cpp factorgraph$(OE) matlab$(OE) exceptions$(OE) -matlab/dai_writefg.$(ME) : matlab/dai_writefg.cpp $(HEADERS) factorgraph$(OE) matlab/matlab$(OE) exceptions$(OE) - $(MEX) $(MEXFLAGS) -o matlab/dai_writefg matlab/dai_writefg.cpp factorgraph$(OE) matlab/matlab$(OE) exceptions$(OE) +matlab/dai_writefg$(ME) : $(SRC)/matlab/dai_writefg.cpp $(HEADERS) factorgraph$(OE) matlab$(OE) exceptions$(OE) + $(MEX) $(MEXFLAGS) -o matlab/dai_writefg $(SRC)/matlab/dai_writefg.cpp factorgraph$(OE) matlab$(OE) exceptions$(OE) -matlab/dai_potstrength.$(ME) : matlab/dai_potstrength.cpp $(HEADERS) matlab/matlab$(OE) exceptions$(OE) - $(MEX) $(MEXFLAGS) -o matlab/dai_potstrength matlab/dai_potstrength.cpp matlab/matlab$(OE) exceptions$(OE) +matlab/dai_potstrength$(ME) : $(SRC)/matlab/dai_potstrength.cpp $(HEADERS) matlab$(OE) exceptions$(OE) + $(MEX) $(MEXFLAGS) -o matlab/dai_potstrength $(SRC)/matlab/dai_potstrength.cpp matlab$(OE) exceptions$(OE) -matlab/matlab$(OE) : matlab/matlab.cpp matlab/matlab.h $(HEADERS) - $(MEX) $(MEXFLAGS) -outdir matlab -c matlab/matlab.cpp +matlab$(OE) : $(SRC)/matlab/matlab.cpp $(INC)/matlab/matlab.h $(HEADERS) + $(MEX) $(MEXFLAGS) -c $(SRC)/matlab/matlab.cpp # UTILS diff --git a/Makefile.win b/Makefile.win index ec700eb..2a3dfa7 100755 --- a/Makefile.win +++ b/Makefile.win @@ -41,7 +41,7 @@ INC=include/dai SRC=src LIB=lib -# Extensions (library, object, executable, MEX file extensions) +# Extensions (library, object, executable, matlab compiled MEX file) LE=.lib OE=.obj EE=.exe @@ -59,7 +59,7 @@ CC=cl CCO=/Fe # Flags for the C++ compiler -CCFLAGS=/I./include /IC:\boost_1_36_0 /EHsc /Ox +CCFLAGS=/Iinclude /IC:\boost_1_36_0 /EHsc /Ox !IFDEF DEBUG CCFLAGS=$(CCFLAGS) /Zi /DDAI_DEBUG !ENDIF @@ -100,10 +100,9 @@ OBJECTS=$(OBJECTS) mr$(OE) !IFDEF WITH_MATLAB # Replace the following by the directory where Matlab has been installed -MATLABDIR=/opt/matlab/bin -# Replace the following with the extension of compiled MEX files on this platform, e.g. .mexglx for x86 -MEX=$(MATLABDIR)/mex -MEXFLAGS=-I. +MATLABDIR=c:\matlab +MEX=$(MATLABDIR)\bin\mex +MEXFLAGS=-Iinclude CXX\#$(CC) CXXFLAGS\#"/EHsc /Ox" !IFDEF DEBUG MEXFLAGS=$(MEXFLAGS) -g /DDAI_DEBUG !ENDIF @@ -141,6 +140,6 @@ doc : $(INC)/*.h $(SRC)/*.cpp doxygen.conf doxygen doxygen.conf clean : - del *$(OE) *.ilk *.pdb *$(EE) matlab\*$(ME) matlab\*$(OE) tests\testdai$(EE) tests\*.pdb tests\*.ilk utils\*$(EE) utils\*.pdb utils\*.ilk $(LIB)\libdai$(LE) + del *$(OE) *.ilk *.pdb *$(EE) matlab\*$(ME) tests\testdai$(EE) tests\*.pdb tests\*.ilk utils\*$(EE) utils\*.pdb utils\*.ilk $(LIB)\libdai$(LE) !INCLUDE Makefile.shared diff --git a/matlab/matlab.h b/include/dai/matlab/matlab.h similarity index 100% rename from matlab/matlab.h rename to include/dai/matlab/matlab.h diff --git a/src/bp.cpp b/src/bp.cpp index 7c8d81a..8e9f7f4 100644 --- a/src/bp.cpp +++ b/src/bp.cpp @@ -39,6 +39,9 @@ using namespace std; const char *BP::Name = "BP"; +#define DAI_BP_FAST 1 + + void BP::setProperties( const PropertySet &opts ) { assert( opts.hasKey("tol") ); assert( opts.hasKey("maxiter") ); @@ -104,9 +107,11 @@ void BP::construct() { newEP.message = Prob( var(i).states() ); newEP.newMessage = Prob( var(i).states() ); - newEP.index.reserve( factor(I).states() ); - for( IndexFor k( var(i), factor(I).vars() ); k >= 0; ++k ) - newEP.index.push_back( k ); + if( DAI_BP_FAST ) { + newEP.index.reserve( factor(I).states() ); + for( IndexFor k( var(i), factor(I).vars() ); k >= 0; ++k ) + newEP.index.push_back( k ); + } newEP.residual = 0.0; _edges[i].push_back( newEP ); @@ -144,7 +149,7 @@ void BP::calcNewMessage( size_t i, size_t _I ) { // calculate updated message I->i size_t I = nbV(i,_I); - if( 0 == 1 ) { + if( !DAI_BP_FAST ) { /* UNOPTIMIZED (SIMPLE TO READ, BUT SLOW) VERSION */ Factor prod( factor( I ) ); foreach( const Neighbor &j, nbF(I) ) @@ -370,7 +375,7 @@ Factor BP::belief( const VarSet &ns ) const { Factor BP::beliefF (size_t I) const { - if( 0 == 1 ) { + if( !DAI_BP_FAST ) { /* UNOPTIMIZED (SIMPLE TO READ, BUT SLOW) VERSION */ Factor prod( factor(I) ); diff --git a/matlab/dai.cpp b/src/matlab/dai.cpp similarity index 97% rename from matlab/dai.cpp rename to src/matlab/dai.cpp index c6f26dd..149d7b0 100644 --- a/matlab/dai.cpp +++ b/src/matlab/dai.cpp @@ -81,7 +81,6 @@ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) // Get psi and construct factorgraph vector factors = mx2Factors(PSI_IN, 0); FactorGraph fg(factors); - long nr_v = fg.nrVars(); // Get method buflen = mxGetN( METHOD_IN ) + 1; @@ -95,7 +94,7 @@ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) // Convert to options object props stringstream ss; ss << opts; - Properties props; + PropertySet props; ss >> props; // Construct InfAlg object, init and run @@ -108,7 +107,7 @@ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) double logZ = obj->logZ(); // Save maxdiff - double maxdiff = obj->MaxDiff(); + double maxdiff = obj->maxDiff(); // Hand over results to MATLAB diff --git a/matlab/dai_potstrength.cpp b/src/matlab/dai_potstrength.cpp similarity index 100% rename from matlab/dai_potstrength.cpp rename to src/matlab/dai_potstrength.cpp diff --git a/matlab/dai_readfg.cpp b/src/matlab/dai_readfg.cpp similarity index 100% rename from matlab/dai_readfg.cpp rename to src/matlab/dai_readfg.cpp diff --git a/matlab/dai_writefg.cpp b/src/matlab/dai_writefg.cpp similarity index 95% rename from matlab/dai_writefg.cpp rename to src/matlab/dai_writefg.cpp index bbe1761..5fbbbc0 100644 --- a/matlab/dai_writefg.cpp +++ b/src/matlab/dai_writefg.cpp @@ -51,7 +51,7 @@ using namespace dai; #define NR_OUT 0 -void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) +void mexFunction( int nlhs, mxArray * /*plhs*/[], int nrhs, const mxArray*prhs[] ) { char *filename; @@ -76,8 +76,6 @@ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) // Construct factorgraph FactorGraph fg(factors); - long nr_v = fg.nrVars(); - long nr_f = fg.nrFactors(); try { fg.WriteToFile( filename ); diff --git a/matlab/matlab.cpp b/src/matlab/matlab.cpp similarity index 100% rename from matlab/matlab.cpp rename to src/matlab/matlab.cpp diff --git a/utils/fginfo.cpp b/utils/fginfo.cpp index f31e633..c114d8b 100644 --- a/utils/fginfo.cpp +++ b/utils/fginfo.cpp @@ -155,6 +155,19 @@ int main( int argc, char *argv[] ) { cout << "Girth: infinity" << endl; } + map facsizes; + for( size_t I = 0; I < fg.nrFactors(); I++ ) { + size_t Isize = fg.factor(I).vars().size(); + if( facsizes.count( Isize ) ) + facsizes[Isize]++; + else + facsizes[Isize] = 1; + } + cout << "Factor sizes: "; + for( map::const_iterator it = facsizes.begin(); it != facsizes.end(); it++ ) + cout << it->first << "(" << it->second << ") "; + cout << endl; + return 0; } } -- 2.20.1