/*=================================================================
*
* lbfgsb_wrapper.c, mex gatewate interface
* You can call this directly from Matlab,
* but I strongly suggest using lbfgsb.m, since many
* variables are modified in-place, and lbfgsb.m will
* handle them correctly. It also shows you how to use
* this routine
* (so just modify lbfgsb.m to suit your own taste)
*
* Written by Stephen Becker, Feb 14 2012
*
* This assumes you have installed lbfgsb 3.0
* (free; see
http://users.eecs.northwestern.edu/~nocedal/lbfgsb.html,
* direct download is:
*
http://users.eecs.northwestern.edu/~nocedal/Software/Lbfgsb.3.0.tar.gz )
*
* 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*
*
* You should use version 3.0! It's better. The old versions are basically
* 15 years old; changes in version 3.0 are fundamental (see the 2011 "remark" paper
* listated at Nocedal's website)
*
*
*
*
* Using a string routine from Peter Carbonetto
* It is copyright (c) 2009 Peter Carbonetto
* All rights reserved.
* Neither Peter Carbonetto nor the University of
* British Columbia nor its contributors endorse
* this derived work.
*
* Inputs (in order):
* m # of memory vectors to keep
* x initial guess or current point, also used to determine the size of the problem (Nx1)
* l list of upper bounds (Nx1)
* u list of lower bounds (Nx1)
* nbd list of which bounds are active (Nx1):
* 0=neither u nor l, 1 = only l, 2 = both u and l, 3 = only u
* f value of function (1x1)
* g gradient of function (Nx1)
*
* factr stopping crit: 1e+12 for low accuracy, 1e7 for moderate, 1e1 for high accuracy
* (will be multiplied by machine precision)
* pgtol stopping crit for infinity norm of projected gradient
* wa work space array (double)
* iwa work space array (int)
* task string
* iprint how verbose the fortran program should be
* csave some iformation (string, length 60)
* lsave[4] some information (logicals)
* isave[44] some information (integers)
* dsave[29] some information (doubles)
*
* Outputs:
* f, task, csave, lsave, isave, dsave
*
* Warning: the following variables are modified in-place
* x, g, wa, iwa
*
*=================================================================*/
#include <math.h>
#include "mex.h"
/*#include <stdbool.h>*/ /* for using the 'bool' data type */
/* actually, matrix.h defines mxLogical, which is normally 'bool' */
/* matrix.h includes tmwtypes.h, which defines mwSize=size_t
* unless MX_COMPAT_32 has been defined, in which case mwSize is
* defined as int. So assuming mwSize=size_t,
* then if you pass in an 'int', it will automatically be scaled
* up if necessary, so there's no problem.
* You probably do NOT want size variables, like 'm' and 'n',
* to be size_t in size, because the fortran program will have
* problems then.
* */
#ifdef _BLAS64_
#define int_F ptrdiff_t
#else
#define int_F int
#endif
#include <string.h>
#include <limits.h> /* for CHAR_BIT */
#include <assert.h>
/* These are the fortran arguments in order */
#define N_m 0
#define N_x 1
#define N_l 2
#define N_u 3
#define N_nbd 4
#define N_f 5
#define N_g 6
#define N_factr 7
#define N_pgtol 8
#define N_wa 9
#define N_iwa 10
#define N_task 11
#define N_iprint 12
#define N_csave 13
#define N_lsave 14
#define N_isave 15
#define N_dsave 16
/* these depend on the lbfgsb release version */
#define LENGTH_STRING 60
#define LENGTH_LSAVE 4
#define LENGTH_ISAVE 44
#define LENGTH_DSAVE 29
/* the fortran lbfgsb code uses lsave as type LOGICAL
* which is usually 'int' in C, whereas type LOGICAL*1
* would be 'bool' in C.
* BTW, the 'mxLogical' is usually defined as 'bool' */
typedef int fortranLogical;
#ifdef DEBUG
#define debugPrintf mexPrintf
#else
#define debugPrintf fakePrintf
#endif
/* If the 'DEBUG' symbol is undefined, then don't print: */
int fakePrintf(const char *format, ...){
return 0;
}
/* For using fortran programs in C */
/* On Windows, we generally do not need to append the underscore to the end,
* whereas on Linux we do.
* Also, if using a native Windows fortran compiler, we may need
* to put the function names in all capital letters
* */
#if defined(NOUNDERSCORE)
#if defined(UPPERCASE_FORTRAN)
#define setulb SETULB
#endif
#else
#if defined(UPPERCASE_FORTRAN)
#define setulb SETULB_
#else
#define setulb setulb_
#endif
#endif
/* Declare the L-BFGS-B function */
void setulb( int_F *n, int_F *m, double *x, double *l, double *u,
int_F *nbd, double *f, double *g, double *factr,
double *pgtol, double *wa, int_F *iwa,
char *task, int_F *iprint, char *csave, fortranLogical *lsave,
int_F *isave, double *dsave );
/* This is taken from the other l-bfgs-b mex interface by Peter Carbonetto. */
/* Copy a C-style string (a null-terminated character array) to a
* non-C-style string (a simple character array). The length of the
* destination character array is given by "ndest". If the source is
* shorter than the destination, the destination is padded with blank
* characters.
* */
void copyCStrToCharArray (const char* source, char* dest, int ndest) {
int i;
int nsource = strlen(source);
/* Only perform the copy if the source can fit into the destination. */
if (nsource < ndest) {
strcpy(dest,source);
/* Fill in the rest of the string with blanks. */
for (i = nsource; i < ndest; i++)
dest[i] = ' ';
}
}
mxLogical isInt( const mxArray *pm ) {
/* What size 'int' does the fortran program
* expect ? Not sure... But let's hope that
* it's the same size as the "int" in C.
* On my 64-bit computer, CHAR_BIT = 8
* and sizeof(int) = 4, so it's still 32 bits
*
* CHAR_BIT is from limits.h
* If using gcc, you can run `gcc -dM -E - < /dev/null | grep CHAR_BIT`
* and it should define the symbol __CHAR_BIT__, so this is another way.
* */
/* debugPrintf("Sizeof(int) is %d\n", sizeof(int) ); */
switch ( CHAR_BIT * sizeof(int) ) {
case 16 :
return mxIsInt16(pm);
case 32:
return mxIsInt32(pm);
case 64:
return mxIsInt64(pm);
default:
mexErrMsgTxt("You have a weird computer that I don't know how to support");
return false;
}
}