File Exchange

image thumbnail

Interface to a LBFGS solver

version 1.1 (9.07 KB) by

Matlab interface to a LBFGS-code for solving unconstrained minimization problems.

10 Downloads

Updated

View License

This submission contains an interface to a LBFGS-code for unconstrained minimization problems of the form

min_{x} f(x) + c*|\tilde{x}|,

where x are the parameters, f is a scalar valued real function, c is a positive scalar (default value=0), |.| denotes the 1-norm, and \tilde{x} is some subset of the parameters x.

The submission is based on a C-port of a Fortran code by Jorge Nocedal, due to Naoaki Okazaki. In addition it also implements a method for 1-norm regularization referred to as "Orthant-Wise Limited-memory Quasi-Newton" due to Galen Andrew and Jianfeng Gao.

Note that in order to use this code, it is necessary to download and compile libLBFGS, currently available at http://www.chokkan.org/software/liblbfgs/.

Installation should be straightforward:

1. Download liblbfgs
2. Make necessary modifications in "make.m" such that the files "lbfgs.c" and "lbfgs.h" can be found by your compiler.
3. Run make.m in Matlab

(prior to running make.m, it might be necessary to run "mex -setup" in the Matlab prompt to locate installed, supported, compilers)

Comments and suggestions are greatly appreciated.

Comments and Ratings (7)

Dan Scholnik

There a small bug in the mex file on the linux 64 platform. The return value "ret" from liblbfgs is a signed integer, but in lbfgs_.c it is defined as mwSize, which is apparently unsigned. So the return values get hosed. I changed ret to be an int and it worked fine.

Arne Vagren

Thanks Christian,

that's good to know!

By the way, did you see any performance gains when enabling SSE?

Christian Kothe

Compiling this on a more or less vanilla Linux GCC setup through mex brought up a couple of issues for me:

* lbfgs_.c uses C++-style "//" comments, which gets the compiler enraged (I changed these to C-style)

* on non-MSVC platforms you'd need to also define HAVE_XMMINTRIN_H and HAVE_EMMINTRIN_H to get SSE to work (otherwise the relevant headers are not included by liblbfgs)

* liblbfgs uses the "inline" keyword which is not defined on my OS (while __inline is). I added the following to each of the arithmetic_*.h files:

#ifndef inline
    #ifdef __inline
        #define inline __inline
    #else
        #define inline
    #endif
#endif

I think the latter 2 issues come up because liblbfgs relies on the automake / config.h mechanism to define the relevant macros/identifiers.

The following mex line then compiles it for me:

mex -DHAVE_EMMINTRIN_H -DHAVE_XMMINTRIN_H -DUSE_SSE -I"liblbfgs-1.10/include" -largeArrayDims lbfgs_.c liblbfgs-1.10/lib/lbfgs.c

Arne Vagren

Thanks xu!

I tried to answer your question but apparently that response failed to be published. Anyway, I believe that the problems you have with Linux is due to the fact that you're using a version of gcc which is not supported in your verion of Matlab. If you install a supported compiler it should work (you can probably modify your mexopts.sh file such that GLIBCXX etc. is found, but that's less straightforward I guess)

Regards
Arne

xu

xu (view profile)

Arne Vagren

To those that commented on this submission previously, I should perhaps say that I accidently deleted the submission and hence all the comments. I particularly regret deleting the commment where it got 5 stars :)

xu

xu (view profile)

thank a lot for sharing. My testing with liblbfgs-1.10, Matlab R2010a and MSVC++ 2008 SP1 on window XP is successful. But the cases on linux Ubuntu 11.04 and Centos 5.6 failed. How can I fix the problems ? Thanks.

the situation in ubuntu 11.04 is as follows:

mex -setup

    Options files control which compiler to use, the compiler and link command
    options, and the runtime libraries to link against.

    Using the 'mex -setup' command selects an options file that is
    placed in ~/.matlab/R2011a and used by default for 'mex'. An options
    file in the current working directory or specified on the command line
    overrides the default options file in ~/.matlab/R2011a.
 
    To override the default options file, use the 'mex -f' command
    (see 'mex -help' for more information).

The options files available for mex are:

  1: /usr/local/MATLAB/R2011a/bin/gccopts.sh :
      Template Options file for building gcc MEX-files
 
  2: /usr/local/MATLAB/R2011a/bin/mexopts.sh :
      Template Options file for building MEX-files via the system ANSI compiler
 

  0: Exit with no changes

Enter the number of the compiler (0-2):
1

Overwrite /home/chi/.matlab/R2011a/mexopts.sh ([y]/n)?
y

/usr/local/MATLAB/R2011a/bin/gccopts.sh is being copied to
/home/chi/.matlab/R2011a/mexopts.sh

**************************************************************************
  Warning: The MATLAB C and Fortran API has changed to support MATLAB
           variables with more than 2^32-1 elements. In the near future
           you will be required to update your code to utilize the new
           API. You can find more information about this at:
           http://www.mathworks.com/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
           Building with the -largeArrayDims option enables the new API.
**************************************************************************

make

-> mexopts.sh sourced from directory (DIR = $HOME/.matlab/$REL_VERSION)
   FILE = /home/chi/.matlab/R2011a/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /usr/local/MATLAB/R2011a
-> CC = gcc
-> CC flags:
         CFLAGS = -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread
         CDEBUGFLAGS = -g
         COPTIMFLAGS = -O -DNDEBUG
         CLIBS = -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++
         arguments = -DMX_COMPAT_32
-> CXX = g++
-> CXX flags:
         CXXFLAGS = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread
         CXXDEBUGFLAGS = -g
         CXXOPTIMFLAGS = -O -DNDEBUG
         CXXLIBS = -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/bin/glnxa64 -lmx -lmex -lmat -lm
         arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
         FFLAGS = -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
         FDEBUGFLAGS = -g
         FOPTIMFLAGS = -O
         FLIBS = -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/bin/glnxa64 -lmx -lmex -lmat -lm
         arguments = -DMX_COMPAT_32
-> LD = gcc
-> Link flags:
         LDFLAGS = -pthread -shared -Wl,--version-script,/usr/local/MATLAB/R2011a/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined
         LDDEBUGFLAGS = -g
         LDOPTIMFLAGS = -O
         LDEXTENSION = .mexa64
         arguments =
-> LDCXX =
-> Link flags:
         LDCXXFLAGS =
         LDCXXDEBUGFLAGS =
         LDCXXOPTIMFLAGS =
         LDCXXEXTENSION =
         arguments =
----------------------------------------------------------------

Warning: You are using gcc version "4.5.2-8ubuntu4)". The version
         currently supported with MEX is "4.3.4".
         For a list of currently supported compilers see:
         http://www.mathworks.com/support/compilers/current_release/

-> gcc -c -I/home/chi/LBFGS_Test/liblbfgs-1.10/include -I/usr/local/MATLAB/R2011a/extern/include -I/usr/local/MATLAB/R2011a/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32 -O -DNDEBUG "lbfgs_.c"

/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by /usr/lib/libppl_c.so.2)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libppl_c.so.2)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by /usr/lib/libppl.so.7)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libppl.so.7)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libgmpxx.so.4)

    mex: compile of ' "lbfgs_.c"' failed.

??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> make at 32
eval(['mex ' cflags ' -v lbfgs_.c ' library_path
'/lib/lbfgs.c -I' library_path '/include']);

---------------------------------------
the situation in centos 5.6 is as follows:

mex -setup

    Options files control which compiler to use, the compiler and link command
    options, and the runtime libraries to link against.

    Using the 'mex -setup' command selects an options file that is
    placed in ~/.matlab/R2011a and used by default for 'mex'. An options
    file in the current working directory or specified on the command line
    overrides the default options file in ~/.matlab/R2011a.
 
    To override the default options file, use the 'mex -f' command
    (see 'mex -help' for more information).

The options files available for mex are:

  1: /usr/local/MATLAB/R2011a/bin/gccopts.sh :
      Template Options file for building gcc MEX-files
 
  2: /usr/local/MATLAB/R2011a/bin/mexopts.sh :
      Template Options file for building MEX-files via the system ANSI compiler
 

  0: Exit with no changes

Enter the number of the compiler (0-2):
1

Overwrite /home/chi/.matlab/R2011a/mexopts.sh ([y]/n)?
y

/usr/local/MATLAB/R2011a/bin/gccopts.sh is being copied to
/home/chi/.matlab/R2011a/mexopts.sh

**************************************************************************
  Warning: The MATLAB C and Fortran API has changed to support MATLAB
           variables with more than 2^32-1 elements. In the near future
           you will be required to update your code to utilize the new
           API. You can find more information about this at:
           http://www.mathworks.com/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
           Building with the -largeArrayDims option enables the new API.
**************************************************************************

make
-> mexopts.sh sourced from directory (DIR = $HOME/.matlab/$REL_VERSION)
   FILE = /home/chi/.matlab/R2011a/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /usr/local/MATLAB/R2011a
-> CC = gcc
-> CC flags:
         CFLAGS = -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread
         CDEBUGFLAGS = -g
         COPTIMFLAGS = -O -DNDEBUG
         CLIBS = -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++
         arguments = -DMX_COMPAT_32
-> CXX = g++
-> CXX flags:
         CXXFLAGS = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread
         CXXDEBUGFLAGS = -g
         CXXOPTIMFLAGS = -O -DNDEBUG
         CXXLIBS = -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/bin/glnxa64 -lmx -lmex -lmat -lm
         arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
         FFLAGS = -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
         FDEBUGFLAGS = -g
         FOPTIMFLAGS = -O
         FLIBS = -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/bin/glnxa64 -lmx -lmex -lmat -lm
         arguments = -DMX_COMPAT_32
-> LD = gcc
-> Link flags:
         LDFLAGS = -pthread -shared -Wl,--version-script,/usr/local/MATLAB/R2011a/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined
         LDDEBUGFLAGS = -g
         LDOPTIMFLAGS = -O
         LDEXTENSION = .mexa64
         arguments =
-> LDCXX =
-> Link flags:
         LDCXXFLAGS =
         LDCXXDEBUGFLAGS =
         LDCXXOPTIMFLAGS =
         LDCXXEXTENSION =
         arguments =
----------------------------------------------------------------

Warning: You are using gcc version "4.1.2". The version
         currently supported with MEX is "4.3.4".
         For a list of currently supported compilers see:
         http://www.mathworks.com/support/compilers/current_release/

-> gcc -c -I/home/chi/LIBLBFGS_Test/liblbfgs-1.10/include -I/usr/local/MATLAB/R2011a/extern/include -I/usr/local/MATLAB/R2011a/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32 -O -DNDEBUG "lbfgs_.c"

lbfgs_.c:1: error: expected identifier or ‘(’ before ‘/’ token
In file included from /usr/include/_G_config.h:44,
                 from /usr/include/libio.h:32,
                 from /usr/include/stdio.h:72,
                 from lbfgs_.c:4:
/usr/include/gconv.h:72: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/gconv.h:88: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/gconv.h:97: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/gconv.h:174: error: expected specifier-qualifier-list before ‘size_t’
In file included from /usr/include/stdio.h:72,
                 from lbfgs_.c:4:
/usr/include/libio.h:329: error: expected specifier-qualifier-list before ‘size_t’
/usr/include/libio.h:361: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/libio.h:370: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/libio.h:486: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_IO_sgetn’
In file included from lbfgs_.c:4:
/usr/include/stdio.h:291: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:296: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:308: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:315: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:357: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:359: error: format string argument not a string type
/usr/include/stdio.h:361: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:363: error: format string argument not a string type
/usr/include/stdio.h:567: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:570: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:580: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:610: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread’
/usr/include/stdio.h:616: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite’
/usr/include/stdio.h:638: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread_unlocked’
/usr/include/stdio.h:640: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite_unlocked’
In file included from /usr/include/stdio.h:835,
                 from lbfgs_.c:4:
/usr/include/bits/stdio.h:112: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/bits/stdio.h: In function ‘getline’:
/usr/include/bits/stdio.h:114: error: ‘__n’ undeclared (first use in this function)
/usr/include/bits/stdio.h:114: error: (Each undeclared identifier is reported only once
/usr/include/bits/stdio.h:114: error: for each function it appears in.)
/usr/include/bits/stdio.h:114: error: too many arguments to function ‘__getdelim’
In file included from /usr/local/MATLAB/R2011a/extern/include/mex.h:58,
                 from lbfgs_.c:6:
/usr/local/MATLAB/R2011a/extern/include/matrix.h: At top level:
/usr/local/MATLAB/R2011a/extern/include/matrix.h:395: error: expected ‘)’ before ‘n’
/usr/local/MATLAB/R2011a/extern/include/matrix.h:403: error: expected ‘)’ before ‘n’
/usr/local/MATLAB/R2011a/extern/include/matrix.h:417: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/local/MATLAB/R2011a/extern/include/matrix.h:610: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mxGetNumberOfElements’
/usr/local/MATLAB/R2011a/extern/include/matrix.h:708: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mxGetM’
/usr/local/MATLAB/R2011a/extern/include/matrix.h:720: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mxGetN’
/usr/local/MATLAB/R2011a/extern/include/matrix.h:770: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mxGetElementSize’
In file included from /usr/local/MATLAB/R2011a/extern/include/matrix.h:871,
                 from /usr/local/MATLAB/R2011a/extern/include/mex.h:58,
                 from lbfgs_.c:6:
/usr/include/stdlib.h:140: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__ctype_get_mb_cur_max’
In file included from /usr/local/MATLAB/R2011a/extern/include/matrix.h:871,
                 from /usr/local/MATLAB/R2011a/extern/include/mex.h:58,
                 from lbfgs_.c:6:
/usr/include/stdlib.h:455: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:485: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:487: error: nonnull argument with out-of-range operand number (argument 1, operand 4)
/usr/include/stdlib.h:589: error: expected ‘)’ before ‘__size’
/usr/include/stdlib.h:591: error: expected ‘)’ before ‘__nmemb’
/usr/include/stdlib.h:600: error: expected declaration specifiers or ‘...’ before ‘size_t’
In file included from /usr/include/stdlib.h:612,
                 from /usr/local/MATLAB/R2011a/extern/include/matrix.h:871,
                 from /usr/local/MATLAB/R2011a/extern/include/mex.h:58,
                 from lbfgs_.c:6:
/usr/include/alloca.h:33: error: expected ‘)’ before ‘__size’
In file included from /usr/local/MATLAB/R2011a/extern/include/matrix.h:871,
                 from /usr/local/MATLAB/R2011a/extern/include/mex.h:58,
                 from lbfgs_.c:6:
/usr/include/stdlib.h:617: error: expected ‘)’ before ‘__size’
/usr/include/stdlib.h:622: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:622: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:775: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:775: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:776: error: nonnull argument with out-of-range operand number (argument 1, operand 5)
/usr/include/stdlib.h:780: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:780: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:781: error: nonnull argument with out-of-range operand number (argument 1, operand 4)
/usr/include/stdlib.h:853: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:856: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:860: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:864: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:873: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:877: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdlib.h:884: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mbstowcs’
/usr/include/stdlib.h:887: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘wcstombs’
/usr/include/stdlib.h:950: error: expected declaration specifiers or ‘...’ before ‘size_t’
In file included from /usr/local/MATLAB/R2011a/extern/include/mex.h:58,
                 from lbfgs_.c:6:
/usr/local/MATLAB/R2011a/extern/include/matrix.h:1056: error: expected ‘)’ before ‘m’
In file included from lbfgs_.c:6:
/usr/local/MATLAB/R2011a/extern/include/mex.h:91: error: expected specifier-qualifier-list before ‘size_t’
lbfgs_.c: In function ‘evaluate’:
lbfgs_.c:20: warning: incompatible implicit declaration of built-in function ‘memcpy’
lbfgs_.c:22: error: expected expression before ‘/’ token
lbfgs_.c:28: error: expected expression before ‘/’ token
lbfgs_.c: At top level:
lbfgs_.c:37: error: expected identifier or ‘(’ before ‘/’ token
lbfgs_.c:62: error: expected identifier or ‘(’ before ‘/’ token

    mex: compile of ' "lbfgs_.c"' failed.

??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> make at 32
eval(['mex ' cflags ' -v lbfgs_.c ' library_path '/lib/lbfgs.c -I' library_path
'/include']);

Updates

1.1

Minor changes in the description.

MATLAB Release
MATLAB 7.12 (R2011a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video