4.6
4.6 | 11 ratings Rate this file 78 Downloads (last 30 days) File Size: 79.9 KB File ID: #35104 Version: 1.6
image thumbnail

LBFGSB (L-BFGS-B) mex wrapper

by

Stephen Becker (view profile)

 

16 Feb 2012 (Updated )

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

| Watch this File

File Information
Description

----------------------- UPDATE Feb 2015 ----------------------------------------
Due to the difficulty of compiling Fortran for most people (including myself), I have converted the v3.0 L-BFGS-B Fortran code into C (using f2c and some hand-coded changes). I have a new wrapper for it as well; both the C code and the mex wrapper are available at: https://github.com/stephenbeckr/L-BFGS-B-C
You are still welcome to try this code, and if you get it to compile, there will be no major difference in speed or performance. Feel free to leave comments/feedback below. However, I will no longer support this version of the code.
----------------------- END UPDATE ----------------------------------------------

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

=== Update, Jan 2015 ===
I am not able to completely support installation/compiling of this toolbox. If you have issues, please mention in the comments below (there are also some solutions from other users in the comments below). Newer versions of Matlab keep changing how files compile (e.g., recent versions do not allow fortran and C to be compiled together), and I am not able to keep up.

If you are using mac or linux, there is a good chance that we can get it to compile. If you are using 64-bit Windows, there is a slim chance you can get it to compile. So please take this as a warning! If anyone is good at software installation and wants to take over the installation script, please do so. The actual wrapper script is quite simple.

Acknowledgements

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

This file inspired Zero Sr1.

Required Products MATLAB
MATLAB release MATLAB 7.10 (R2010a)
MATLAB Search Path
/
/lbfgsb3.0_mex1.2
Other requirements Compile the mex file with compile_mex.m. Needs a C and fortran compiler. See user comments for issues with recent versions of Matlab and with Windows 64-bit.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (56)
02 Jul 2015 Dushyant

It works quite faster for bigger problem.

03 Mar 2015 Daniel

Daniel (view profile)

It works ! Awesome !

Comment only
23 Feb 2015 Stephen Becker

Stephen Becker (view profile)

I just verified the new C version compiles on 64-bit windows, so I have updated the description above to link to the new github code. I will no longer support this versinon of the code, but I will attempt to support the github version

Comment only
22 Feb 2015 Stephen Becker

Stephen Becker (view profile)

I have a new version that is all in C and should compile MUCH easier, especially on Windows. Please let me know if you have comments. I haven't yet tried compiling on Windows so that may need some fine-tuning.

See https://github.com/stephenbeckr/L-BFGS-B-C

Comment only
12 Feb 2015 Daniel

Daniel (view profile)

@Marcin, I think the wrapper works well in Yosemite (this is also my current OS I am using most). You have to make sure you have the updated Xcode and the fortran form http://hpc.sourceforge.net. Also install the OpenBlas libraries (you will find many guides online) After that, you open MATLAB and set up mex, i.e. mex -setup C++ and mex -setup FORTRAN.

Make sure you have
LINKEXPORT="-Wl,-exported_symbols_list,"$MATLABROOT/extern/lib/maci64/mexFunction.map""
in your "mex_FORTRAN_maci64.xml" file. Also make sure you copy the downloaded (with gfortran) libgfortran libraries in the folder :
/Applications/MATLAB_R2014b.app/sys/os/maci64/

After this, in MATLAB, type

mex -v -c lbfgsb_wrapper.c -largeArrayDims -UDEBUG CFLAGS="\$CFLAGS -O3"

and then,

mex -v -output lbfgsb_wrapper lbfgsb_wrapper.o Lbfgsb.3.0/lbfgsb.f Lbfgsb.3.0/linpack.f Lbfgsb.3.0/timer.f -lm -lopenblas

Good luck !

Comment only
02 Feb 2015 Dan Scholnik

Luckily the fix should not measurably affect speed in the most-common case where x is not aliased to another variable. (I'm actually quite fond of writing in-place mex files myself, often to call LAPACK routines directly that matlab does not expose. But of course then I know to be careful. This one took a while to hunt down since it didn't occur to me for a while.)

Comment only
01 Feb 2015 Stephen Becker

Stephen Becker (view profile)

@Dan, good observation and I will include the fix when I next update. For now, the code is true to it's C/Fortran style origins where this practice was commonplace, as it's more efficient for largescale problems. (Back when you told LAPACK how much extra memory you could afford, and saving the entire history of variables was only possible for tiny problems).

Comment only
01 Feb 2015 Dan Scholnik

The in-place modification of the current iterate x can cause some nasty hair-pulling bugs. If (for example) one is saving x off in a global variable within the objective function, then due to Matlab's copy-on-write optimization the global version will be silently changed as well since Matlab is not aware of the in-place changes. This can be avoided by adding the line

extern bool mxUnshareArray(mxArray *array_ptr, bool noDeepCopy);

at the top of lbfgsb_wrapper.c to declare the undocumented mxUnshareArray function, and then adding the line

mxUnshareArray((mxArray *)prhs[N_x], true);

at the top of mexFunction() before x is overwritten. This forces matlab to perform any delayed copies. Here's a good link with more info:

http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing

Comment only
06 Jan 2015 Stephen Becker

Stephen Becker (view profile)

@v, I'm sorry but I can't really give you the help you need to install the 64-bit windows version.

Comment only
01 Jan 2015 Marcin

Marcin (view profile)

I'm having a hard time compiling this for Max OS X Yosemite. I have MATLAB R2014b. When I run the 'compile_ex' script, I'm getting the following:

Error using mex
Cannot compile both C and FORTRAN source files in a single call to MEX.

Following the advice I was able to find, I then tried:

mex -v -c lbfgsb_wrapper.c -largeArrayDims -UDEBUG CFLAGS="\$CFLAGS -O3" ;

which runs ok, and then:

mex -v -output lbfgsb_wrapper lbfgsb_wrapper.o Lbfgsb.3.0/lbfgsb.f Lbfgsb.3.0/linpack.f Lbfgsb.3.0/timer.f FC="gfortran" -lm -lmwblascompat32 LINKEXPORT='Wl,--version-script,/usr/local/MATLAB/R2014a/extern/lib/glnxa64/mexFunction.map'

which fails:

Error using mex
clang: error: no such file or directory: 'Wl,--version-script,/usr/local/MATLAB/R2014a/extern/lib/glnxa64/mexFunction.map'

Can anyone help?

Comment only
20 Dec 2014 v

v (view profile)

compiled the 64-bit version - when it ran the tests in compile_mex_forWindows.m, it caused an internal error crashing matlab. The crash happens in lbfgsb_wrapper.c at the point it calls setulb

22 Oct 2014 Daniel

Daniel (view profile)

Hi Thomas,

Please download (it's free) the Dependency Walker 64 program and use it to analyze the mex file you created. Dependency Walker will tell you what dll's are missing and causing the "Attempt to access invalid address".
This problem can be solved if you are using Windows 7 64bit. In the case of Windows 8.1 I could not get it to work as the dylib libraries from the cygwin fortran make use of some dll's that are no longer available in Windows 8.1

Daniel

Comment only
21 Oct 2014 Thomas Humphries

So I was happily using this code on my 32-bit workstation and now am enjoying trying to get it to work on my 64-bit laptop... I have followed Daniel's instructions below and have managed to get the .mexw64 file to compile, but when I try to run the example I get

Invalid MEX-file 'C:\Users\...\lbfgsb.3.0_1.2\lbfgsb_wrapper.mexw64': Attempt to access invalid address.

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

Error in example_NNLS (line 100)
[xk, ~, info] = lbfgsb(fun, l, u, opts );

Probably a longshot, but does anyone have any suggestion what could be causing this? I am guessing some kind of library linking issue but nothing comes up during the compilation process...

-TH

Comment only
21 Oct 2014 Daniel

Daniel (view profile)

Hi Lucas,

Sorry for my late reply, I haven't checked the posts in a while. I think the problem you describe is quite simple, namely you have to add in the PATH variable in Windows the path to the "lib" command from the Visual Studio folder.

Comment only
13 Oct 2014 Alexandre

I have received a couple of email asking me how to compile. Here how I proceeds in 2 steps on Linux (Mint 17 distro)

mex -v -c lbfgsb_wrapper.c -largeArrayDims -UDEBUG CFLAGS="\$CFLAGS -O3" ;

mex -v -output lbfgsb_wrapper lbfgsb_wrapper.o Lbfgsb.3.0/lbfgsb32.f Lbfgsb.3.0/linpack32.f Lbfgsb.3.0/timer.f FC="gfortran" -lm -lmwblascompat32 LINKEXPORT='Wl,--version-script,/usr/local/MATLAB/R2014a/extern/lib/glnxa64/mexFunction.map'

It is a colleague of mine who found out how to compile, therefore I am afraid I cannot explain why it works.

Comment only
12 Oct 2014 wenbin

wenbin (view profile)

I have the same problem as Alexandre. When I try to compile it under Linux with matlab R2014a, I get the following error:
Error using mex
Cannot compile both C and FORTRAN source files in a single call to MEX.

Error in compile_mex (line 61)
mex -v lbfgsb_wrapper.c -largeArrayDims -UDEBUG ...

Anyone had meet this problem? Please kindly show me the solution, I'm really in trouble.

Comment only
15 Jul 2014 Alexandre

Problem sloved. It seems that Matlab R2014a does not allow to compile Fortran and C in a single MEX call. I believe that it allowed to link the files automatically.

I had to compile the Fortran code to .o objects and link them to the .c files manually.

Comment only
15 Jul 2014 Alexandre

Hi Stephen,

I managed to compile the Fortran part by splitting into 2 MEX calls:

mex Lbfgsb.3.0/lbfgsb.f Lbfgsb.3.0/linpack.f Lbfgsb.3.0/timer.f -lm -lmwblas CFLAGS="\$CFLAGS -O3"FC="gfortran"

mex -v lbfgsb_wrapper.c -largeArrayDims -UDEBUG ;

The Fortran part is fine, but not the C compilation. I get the following error message:

Error using mex
/tmp/mex_29407351995475_5199/lbfgsb_wrapper.o: In function `mexFunction':
lbfgsb_wrapper.c:(.text+0x738): undefined reference to `setulb_'
collect2: error: ld returned 1 exit status

I have tried to play with the -DNOUNDERSCORE -UUPPERCASE_FORTRAN flags but none of them seem to help.

How can I fix this issue?

Thank you very much

Comment only
03 Jul 2014 Stephen Becker

Stephen Becker (view profile)

Hi Alexandre,
I've never seen that error either. Maybe ask the mathworks help about it.
-Stephen

Comment only
03 Jul 2014 Alexandre

I have tried to compile it under Linux Mint 17, with Matlab 2014a. I got the following error message:

Error using mex
Cannot compile both C and FORTRAN source files in a single call to MEX.

Error in compile_mex (line 61)
mex -v lbfgsb_wrapper.c -largeArrayDims -UDEBUG ...

I've tried to find out about this error but it is nowhere to be found, as if it is an issue related to Matlab 2014 only.

Any suggestion?
Cheers

Comment only
17 Apr 2014 Lucas

Lucas (view profile)

Hi everyone,

As I'm trying to compile the mex file on a W7 x64 with Matlab R2013a, I follow the tutorial of Daniel, and I would need some specification (thanks by the way for sharing this) :
- the line "lib /def:liblbfgsb.def /machine:x64" gives me
>>>
$ lib /def:liblbfgsb.def /machine:x64
-bash: lib: command not found
<<<

-I tried to run the compilation anyway, and it gives me this :
>>>
/cygdrive/c/Progra~1/MATLAB/R2013a/bin

$ mex.bat -O -largeArrayDims -UDEBUG lbfgsb_wrapper.c liblbfgsb.lib -lmex -lmx -lopenblas

C:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: 'lbfgsb_wrapper.c' not found.
<<<

I'm not a great expert on bash's command, and I can't figure out what's the real problem behind these.

-LS

Comment only
06 Jan 2014 Daniel

Daniel (view profile)

Hi,

I succeded to compile and test the mex module for Windows 7 x64 (Sp1) linking with the multithreded OpenBlas. In this way one can take advantage of the multicore architecture of the available CPU. For the Windows operating system the process is rather convoluted and delicate (one can easily get into the so called "dll hell" problem). The solution is indeed based on creating a dll file. I got it working following a script from Roland, so many thanks to him who pioneered a way to compile using Cygwin and gracefully shared his own script.
The idea is the following:
1) install Openblas and add its lib folder to the $Path$ Environment variable.
2) install Cygwin64 (the Devel package)
3) make sure you have SDK 7.1 installed along with Visual Studio 10.0 (it's free) and all the updates for Win7.
4) add to the $Path$ environment variable "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64"
5) find and copy the x64 version of IESHIMS.dll into a folder added to $Path$ environment variable. (I know, this is counterintuitive and seems esoteric but if you don't it may not work) To check that this is needed one can use "Dependency Walker".
6) open the Cygwin64 terminal and go to the "lbfgsb3.0_mex1.2" folder and make sure the "Lbfgsb.3.0" subfolder with all its files is inside (if not download it).
7) Type (or creat a .sh script to run at once):
cd Lbfgsb.3.0
rm *.o
rm *.dll
rm *.lib
rm *.dll.a

x86_64-w64-mingw32-gfortran.exe -c -Wall -Wno-uninitialized lbfgsb.f linpack.f blas.f timer.f
echo EXPORTS > liblbfgsb.def
echo setulb_ >> liblbfgsb.def
echo _setulb_=setulb_ >> liblbfgsb.def

dlltool -D liblbfgsb.dll -d liblbfgsb.def -e liblbfgsb.o -l liblbfgsb.dll.a lbfgsb.o linpack.o blas.o timer.o
lib /def:liblbfgsb.def /machine:x64

x86_64-w64-mingw32-gfortran.exe -mdll -static-libgfortran -o liblbfgsb.dll liblbfgsb.o lbfgsb.o linpack.o timer.o blas.o

cp liblbfgsb.dll liblbfgsb.dll.a liblbfgsb.lib .
cp liblbfgsb.dll liblbfgsb.dll.a liblbfgsb.lib ..
cp /usr/x86_64-w64-mingw32/sys-root/mingw/bin/libgcc_s_seh-1.dll ..
cd ..

/cygdrive/c/Progra~1/MATLAB/R2013b/bin/mex.bat -O -largeArrayDims -UDEBUG lbfgsb_wrapper.c liblbfgsb.lib -lmex -lmx -lopenblas

8) Test example_NNLS.m

Good Luck!

27 Nov 2013 Robert

Robert (view profile)

Dear Chenchen Tong,

I got the same error, when running compile_mex.m on Ubuntu 12.04, gcc-4.6 and MATLAB R2013a:

=== lbfgsb "driver1" test problem, 2D ===
Invalid MEX-file
'.../lbfgsb.3.0_1.2/lbfgsb3.0_mex1.2/lbfgsb_wrapper.mexa64':'.../matlabR2013a/bin/glnxa64/../../sys/os/glnxa64/libgfortran.so.3: version
`GFORTRAN_1.4' not found (required by
.../lbfgsb.3.0_1.2/lbfgsb3.0_mex1.2/lbfgsb_wrapper.mexa64)

For me the problem was solved by running MATLAB with LD_PRELOAD and LD_LIBRARY_PATH, pointing to the system libraries:

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libgfortran.so.3 LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu matlab

Comment only
29 Oct 2013 Ryan Muir  
25 Sep 2013 Daniel

Daniel (view profile)

Hi Roland,

Could you please post detailed instructions for how to compile the lbfgsb_wrapper in Windows 7 64bit?
I was only able to compile in Linux and Mac OS X so far . I also linked with the OpenBlas library which is very fast ( if you have a multi-core cpu).

Thank you very much.

Daniel

05 Aug 2013 Stephen Becker

Stephen Becker (view profile)

Hi Roland,
That's great it works! If you wouldn't mind, can you post detailed instructions here? I think many others would find them useful.

You're right about the typo in the nrhs. I'll fix that in the next update.

Right now, the gradient is necessary. The Python and R versions use large packages to get around this. They might have a finite-differences gradient approximation mode, but I suspect they are really doing automatic differentiation (the good news is that this is the exact gradient, no approximation. The bad news is that it can be sometimes slow). There are automatic differentiation packages for Matlab, but none that seem to be very "mature". So you could try some of those, but if you can work out an explicit function to compute the gradient, that is usually the simplest (no automatic differentiation packages to get working) and the fastest for computation.
Best, Stephen

Comment only
30 Jul 2013 Roland

Roland (view profile)

I finally managed to run it on Win7 64 with mingw fortran and msvc c compilers.

Stephan, is it necessary to give the gradient in any case? I know that the Python and R versions can use numerical approximations to the gradient. Do you know of a function that does that for me?

Comment only
29 Jul 2013 Roland

Roland (view profile)

Thanks Steven, I'm still trying to get this to work. At the moment I got it compiled but it crashes when I run it with no expressive error messages. Still debugging... but on a different not, in line 266:

if (nrhs < N_g-1 ) {

shouldn't it read "nrhs < N_g+1", so a "+" instead of "-"?

Cheers

Comment only
24 Jul 2013 Stephen Becker

Stephen Becker (view profile)

Hi Roland,
I think that's with trying the f2c'd version of the code. It might be that your C compiler is stricter than the f2c code. Maybe you can tell the compiler to allow a newer standard? In gcc, it would be something like -std=gnu99 or -ansi. You can probably look up the equivalent for MSVC.
Or, if you can get a fortran compiler, then use the original fortran code and don't mess with the f2c version.
Either way, I'm afraid I can't help much because I work with a Windows computer. Judging from the other comments here, it is quite difficult to get this code to work with 64 it Windows. We really need a pure C version of the code; if you want to do this (you can use the f2c version as a base), please let me know!
Best,
Stephen

Comment only
24 Jul 2013 Roland

Roland (view profile)

Dear Stephen,

Thanks for all your efforts. I would love to use your script but it doesn't compile. I have Matlab R2012b on Win 7 64bit. I use the compile_for_windows script and get the following error:

>>>>>>>>>>>>>>>>>>>>

>> compile_mex_forWindows
Sorry
Compiling abort_.c
Compiling arithchk.c
Compiling backspac.c
Compiling c_abs.c
Compiling c_cos.c
c_cos.c
c_cos.c(14) : error C2143: syntax error : missing ')' before '*'
c_cos.c(14) : error C2143: syntax error : missing '{' before '*'
c_cos.c(14) : error C2143: syntax error : missing ';' before '*'
c_cos.c(14) : error C2059: syntax error : ')'
c_cos.c(16) : error C2054: expected '(' to follow 'z'

C:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'c_cos.c' failed.

Error using mex (line 206)
Unable to complete successfully.

Error in compile_mex_forWindows (line 116)
mex('-largeArrayDims','-I./','-c','-DMSDOS','-DNOUNDERSCORE','-DNO_ISATTY',src_file);

<<<<<<<<<<<<<<<<<<<<<<<<<

Any suggestions how to fix that would be very much appreciated.

Thanks,

Roland

Comment only
30 Jun 2013 Stephen Becker

Stephen Becker (view profile)

Hi Xin,
You are using the f2c version I included, so this adds more complication. The problem is almost surely that your BLAS requires integers of different lengths. So try two things:
First, define _BLAS64_ when you compile. You can do this by adding -D_BLAS64_ to the mex compilation command. Hopefully this will fix it.
Second, try a different BLAS. You can install another version (e.g. ATLAS) and use that. If there are still problems, use the netlib source code (for CBLAS) and link with this source; this way your compiler will give you errors if there are incompatibilities and they will be easy to fix.
Best, Stephen

Comment only
24 Jun 2013 xin yu

xin yu (view profile)

Hi, Stephen
Thank you for the wrapper, which can work pretty well on my windows7 32bit.
But when compiling the wrapper on my desktop, win7 64bit, I met some problems.
I can compile the wrapper, but when running the examples, matlab will crash. I debug the problem. In the loop, when calling the lbfgsb.c at the first time, it is ok, but the second iteration, a breakpoint is triggered. When calling dcopy, matlab will crash down. I have no idea how can I solve the problem. My BLAS is 10.3.11. My matlab version is R2013a.
Thanks!

13 May 2013 Stephen Becker

Stephen Becker (view profile)

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!

Comment only
13 May 2013 Tim

Tim (view profile)

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""

Comment only
13 May 2013 Tim

Tim (view profile)

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

Comment only
13 May 2013 Stephen Becker

Stephen Becker (view profile)

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.

Comment only
13 May 2013 Tim

Tim (view profile)

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,

Comment only
14 Feb 2013 Stephen Becker

Stephen Becker (view profile)

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.

Comment only
13 Feb 2013 Xunkai Wei

Xunkai Wei (view profile)

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

Stephen Becker (view profile)

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

Comment only
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!

Comment only
31 Aug 2012 Dominic

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

23 Aug 2012 Paul

Paul (view profile)

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.

Comment only
26 Jun 2012 Georg Altenstein

Thanks a lot for the wrapper.

25 Jun 2012 Stephen Becker

Stephen Becker (view profile)

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.

Comment only
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

Comment only
20 Jun 2012 Stephen Becker

Stephen Becker (view profile)

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

Comment only
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

Comment only
12 Jun 2012 Stephen Becker

Stephen Becker (view profile)

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.

Comment only
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.

Comment only
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.

Comment only
09 Apr 2012 Stephen Becker

Stephen Becker (view profile)

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.

Comment only
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.

Comment only
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

Comment only
Updates
25 Jun 2012 1.2

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 1.3

adding compile_mex_forWindows.m

18 Jan 2013 1.4

Removed unnecessary zip file from the package

06 Jan 2015 1.5

No changes to files, just declaring that I cannot support 64-bit Windows. Sorry!

23 Feb 2015 1.6

Link to alternative implementation

Contact us