5.0

5.0 | 5 ratings Rate this file 55 Downloads (last 30 days) File Size: 79.9 KB File ID: #35104
image thumbnail

LBFGSB (L-BFGS-B) mex wrapper

by Stephen Becker

 

16 Feb 2012 (Updated 18 Jan 2013)

Mex wrapper for lbfgsb v3.0 fortan library. L-bfgs-b solves box-constrained optimization.

| Watch this File

File Information
Description

Description: L-BFGS-B is a variant of the well-known "BFGS" quasi-Newton method. This variant uses limited-memory (like L-BFGS), and also handles simple constraints (to be specific, bound constraints, so this includes x >=0 constraints).

The authors of LBFGSB have had fortran implementations available since 1996, but in 2011 they released a major update (v3.0) which has an improved algorithm (you should use this version!). There are existing mex file wrappers for earlier versions, but none worked with v3.0, so I created this file.

This submission tries to make the mex wrapper simple, and then includes a nice .m file solver that requires very few parameters, yet is customizable for advanced users. Run the compile_mex.m file to compile the files. compile_mex.m also includes two quick sanity checks (the file driver1.m is a helper file for one of these checks)

I also have an example of using L-BFGS-B to solve the non-negative least-squares
(NNLS) problem, and comparing it to other existing solvers. To put it simply,
L-BFGS-B is a fantastic NNLS solver, and much better than Matlab's lsqnonneg.

Links:

The fortran lbfgsb is at http://users.eecs.northwestern.edu/~nocedal/lbfgsb.html,

 There are also versions 2.1 and 2.4 of the library.

For v 2.1, Peter Carbonetto's mex interface works; see http://www.mathworks.com/matlabcentral/fileexchange/15061-matlab-interface-for-l-bfgs-b and also http://www.cs.ubc.ca/~pcarbo/lbfgsb-for-matlab.html

   For v 2.4 (I don't know where you can find this version though), use the mex files from here: http://www.cs.toronto.edu/~liam/software.shtml

The wikipedia page (this was mainly edited by one of the authors of the L-BFGS-B algorithm): http://en.wikipedia.org/wiki/L-BFGS-B:_Optimization_subject_to_simple_bounds

For installing on 64-bit Windows, if you need a compiler, see http://www.mathworks.com/support/compilers/R2011b/win64.html

Acknowledgements

Nnls And Constrained Regression, Nnls, Active Set Algorithm, Mtron, and Matlab Interface For L Bfgs B inspired this file.

Required Products MATLAB
MATLAB release MATLAB 7.10 (R2010a)
Other requirements Compile the mex file with compile_mex.m. It should work on all platforms (windows, mac, linux, 32 or 64 bit) but I haven't tested it everywhere. Needs a C and fortran compiler.
Tags for This File  
Everyone's Tags
bfgs, bound constraints, constrained optimization, lsqnonneg, nnls, nonnegative leastsquares, optimization, quasinewton
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
13 May 2013 Stephen Becker

I'm not sure what's going on. It's odd that it compiles if it can't find the symbol. It could be an issue with a run-time library. On linux, I had the option of using either g95 or gfortran, and I use g95, so maybe try that if it's easy on the mac. Otherwise, I'm afraid I can't be of much help. You could also try following the basic ideas outline in the compile_mex_forWindows (i.e. convert fortran to C). Best of luck!

13 May 2013 Tim

My apologies for the numerous replies. I had to manually update dcopy dscal and daxpy as you suggested. The mex file is generated ("lbfgsb_wrapper.mexmaci64") but it crashes matlab. The error report says: "I should have added that the error report listed "Symbol not found: __gfortran_transfer_real_write""

13 May 2013 Tim

I should have added that the error report listed "Symbol not found: __gfortran_transfer_real_write"

13 May 2013 Stephen Becker

Hi Tim,
The crash, the fact that the mwblas seemed to work (for compilation), and the fact that it was looking for _daxpy_ and not _daxpy32_, all suggest that maybe the ddot's and so on were not replaced by ddot32. The C files should be OK, so check the fortran files. You can try running the script from the shell yourself, or just edit the .f files with an editor and do find-replace.

If you verify that the ddot/dcopy/dscal/daxpy are replaced with their ddot32/dcopy32/dscal32/daxpy32 and it still fails, then we have a different problem on our hands. Try compiling with -v and let me know the output.

13 May 2013 Tim

Hi,
Thank you for writing this code. I hope to be able to use it, but I am having some compilation problems. I am running a 64 bit mac, OS 10.6.8, running Matlab v. R2010b.

I get the error "script failed, try running script by hand perhaps?," though the files are still generated with ddot32 replacing dot. It does not, however, compile when using the subsequent mex command ("can't find "_daxpy_" etc). It did compile using the previous, commented out mex command using lmwblas, and generated a mexmaci64 file. If I try running the example code calling lfbgs, Matlab crashes.

The libblas version is 10.2.3 for R2010b. If you could offer any solutions I would be grateful,

14 Feb 2013 Stephen Becker

Hi Xunkai, I can't help much because I don't have access to Intel's compiler. But if you adapt the gfortran lines to work with the intel fortran compiler, then the rest of the compilation (the C code) should be similar.

13 Feb 2013 Xunkai Wei

Win 7, 64bit system, I have intel fortran compiler and vs 2012 C++ compiler, could you show me how to compile the source code ?

18 Jan 2013 Ernst Kloppenburg  
02 Nov 2012 Stephen Becker

Hi Chris, I must have missed that file with my last update; thanks for noticing. I have included it now and the update should be on the web in the next day.
-Stephen

31 Oct 2012 Chris Barber

Hi Stephen,

I can't seem to find compile_mex_forWindows.m in your file submission. Would you be able to upload it?
Thanks!

31 Aug 2012 Dominic

Thanks a lot for this wrapper. Works beautifully on my UBUNTU 64 bit system :)

23 Aug 2012 Paul

Couldn't get it to run despite trying all options including the f2c. Compiles and crashes when I try to run an example. I'm running OS X 10.6, Matlab 2011a, 64 bit, with gcc 4.6.2 (prerelease) and Blas 10.2.6. Could it be that my compiler is too new? Matlab documentation says I need an older gcc, 3.2, and older gfortran, 4.3.x.

26 Jun 2012 Georg Altenstein

Thanks a lot for the wrapper.

25 Jun 2012 Stephen Becker

I have updated the package to include a new install script that should work for 64-bit BLAS, and it works for Vicenc's case at least.

22 Jun 2012 Vicenç Gomez  
20 Jun 2012 Vicenç Gomez

Dear Stehpen,

thanks for your fast answer. The different versions of libblas I tried with are:

for R2009a : 10.1.1
for R2011b : 10.3.2
for R2012a : 10.3.5

Intel(R) Math Kernel Library Version 10.1.1 Product Build 082212.12 for Intel(R) 64 architecture applications

Intel(R) Math Kernel Library Version 10.3.2 Product Build 20110112 for Intel(R) 64 architecture applications

Intel(R) Math Kernel Library Version 10.3.5 Product Build 20110720 for Intel(R) 64 architecture applications

20 Jun 2012 Stephen Becker

Hi Vicenc, I'm sorry to hear about the 64-bit problems. I don't think it's an issue with the compiler, but rather it's probably a BLAS issue. Can you run "version -blas" in Matlab and let me know what it says? I might be able to supply an update in a few days that helps these 64-bit issues, but I need to be able to recreate the problem first.
-Stephen

20 Jun 2012 Vicenç Gomez

Hi,

I'm using the wrapper in the laptop using ubuntu 32bit. I works perfectly.

However, as desktop machine I have a 64bit processor and both tests crash. I can compile successfully and the crashing point is arbitrary: sometimes at iteration 4, sometimes in the beginning, depending on the Matlab version.

I tried with R2009b, R2011b, R2012a and always crashes. I also tried with both 4.4 and 4.3 gfortran and gcc. What could be the problem?

I'm using:

Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz
Linux 2.6.32-41-generic #90-Ubuntu SMP Tue May 22 11:29:51 UTC 2012 x86_64 GNU/Linux

12 Jun 2012 Stephen Becker

Hi Okan,
The L-BFGS-B software calls the BLAS library to perform most of its linear algebra, and unfortunately, the conventions for BLAS have changed over time. I suspect that is the problem.
To get some more information, run this command in both versions of Matlab:

version -blas

If you have different versions of BLAS, that could be the issue. You could try running R2011b using the BLAS library that R2011a uses. To change the BLAS library, export the environment variable BLAS_VERSION to point to the location of the BLAS library (on linux, it might look like:

export BLAS_VERSION=/opt/matlab-R2011a/bin/glnxa64/libmwblas.so )

Hope that helps. If you have more issues, please email me.

07 Jun 2012 Okan Irfanoglu

First of all, thanks for this wrapper.

I had a quick question. When I compile everything with gcc/gfortran 4.3.3 and call the mex file from Matlab 2011a, everything works fine and both tests succeed.

However, when i use the same compilers and Matlab R2011b, matlab crashes in the tests. The two compilers are still supported by this matlab as well so I wanted to ask you if you had any thoughts about this.

Thank you.

15 Apr 2012 Michael

I am trying to compile on a 32 bit Windows 7 machine, running Matlab 2010a. Supposing that I have the f2c library correctly compiled could you try and give me some suggestions on how to rewrite the mex function call within compile_mex.m?

This is what I am currently trying:
mex lbfgsb_wrapper.c -largeArrayDims -DDEBUG ...
Lbfgsb.3.0/lbfgsb.f Lbfgsb.3.0/linpack.f Lbfgsb.3.0/timer.f ...
-lm -lblas -lvcf2c -LC:\Users\Mike\Documents\MATLAB\libf2c ...
CFLAGS="\$CFLAGS -O3"

I get a long series or errors which appear to be complaining about syntax:

Error lbfgsb_wrapper.c: 422 illegal statement termination
Error lbfgsb_wrapper.c: 422 skipping `int'
Error lbfgsb_wrapper.c: 422 undeclared identifier `ndim'
Error lbfgsb_wrapper.c: 423 illegal use of type name `mwSize' ..........

I have a feeling that I may need to change something in my mexopts.bat file. Not sure where to start though. Any help/suggestions would be greatly appreciated! Thanks.

09 Apr 2012 Stephen Becker

Another comment: on Windows, you need a fortran compiler, and might need to do some fiddling around. But it is possible because someone did it successfully; email me if you want more help. But in general, it is difficult on windows. It might be best to use f2c.

22 Mar 2012 Krishnamurthy

I was having the same problem with 2012a. The error gets fixed if you install gcc-4.4,g++-4.4,gfortran-4.4 and change the default compilers in mexopts.sh to these.

22 Mar 2012 Chenchen Tong

hi,Stephen, thank a lot for your brilliant work. This is just what I need now. I am trying to use this optimizer on image registration. However, I am not able to make it work. I compile with the mex file on 64bit ubuntu 11.1 with matlab 2011b. The GCC version is 4.6.1. I managed to make the mexa64 file. But when running the test 1, I got error like:
=== lbfgsb "driver1" test problem, 2D ===
Invalid MEX-file '/media/Data/project/codes/Lbfgsb.3.0/lbfgsb_wrapper.mexa64':
/usr/local/MATLAB/R2011b/bin/glnxa64/../../sys/os/glnxa64/libgfortran.so.3:
version `GFORTRAN_1.4' not found (required by
/media/Data/project/codes/Lbfgsb.3.0/lbfgsb_wrapper.mexa64)

Error in lbfgsb (line 174)
[f, task, csave, lsave, isave, dsave] = ...

could you kindly help me with this? Thanks

Updates
25 Jun 2012

June 25, minor tweaks to code and install script to help with 64-bit computers. I also include an experimental f2c version (all in C) that doesn't require a fortran.

02 Nov 2012

adding compile_mex_forWindows.m

18 Jan 2013

Removed unnecessary zip file from the package

Contact us