mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
The levmar library

A c language Levenberg-Marquardt facility

Introduction

"levmar" is an excellent c language Levenberg-Marquardt non-linear least-squares package, by Manolis Lourakis. Several mxlib fitting routines use it.

Getting levmar

levmar can be found at http://users.ics.forth.gr/~lourakis/levmar/ .

Installing levmar

Download the latest levmar from the above link. I am currently on 2.6.

I typically need to edit the following lines in the Makefile, like so:

CFLAGS=$(CONFIGFLAGS) $(ARCHFLAGS) -O3 -ffast-math -funroll-loops -Wall -fPIC
AR=ar -r
RANLIB=ar -s

This assumes you are using gcc. We want a shared (.so) library, this is why we added the option -fPIC to the CFLAGS, and changed the AR to ar -r and RANLIB to ar -s. Finally, in the liblevmar.a rule:

Lapack

To have levmar use Lapack, we need to specify where the libraries are in the makefile.

For ATLAS:

LAPACKLIBS_PATH=/usr/local/atlas/lib
LAPACKLIBS=-llapack -lcblas -lf77blas  -latlas -lgfortran

If you installed ATLAS somewhere else, modify accordingly.

For MKL:

LAPACKLIBS_PATH=${MKLROOT}/lib/intel64
CFLAGS+=-m64
LAPACKLIBS=-Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl

The m64 option is added to CFLAGS at the recommendation of the line advisor. Depending on your architecture, you may need to edit the path and these flags. The MKL line advisor is of great help in determining this.

Multithreading

In levmar.h the line

#define LINSOLVERS_RETAIN_MEMORY

should be commented out if there is any chance you will use levmar in a multithreaded program. In particular, if you use openmp the compiler will issue a warning at this point in the header. I always build levmar with this commented out.

Bulding

Type make to build. Do not worry if it exits with an error – as long liblevmar.a was made then everything is fine for our purposes.

Finally, once the build is complete, turn liblevmar.a into a shared libary like so:

gcc -shared -Wl,-soname,liblevmar.so -o liblevmar.so *.o -rdynamic

The location of liblevmar.so and levmar.h should be accessible to your compiler. I put these in $(HOME)/lib and in $(HOME)/include respectively.

levmar Documentation

Some levmar documentation can be found at http://users.ics.forth.gr/~lourakis/levmar/. The source tree also contains documentation.