Saturday, July 10, 2010

atlas, blas and lapack in Mac OS X

If you are going to use BLAS or LAPACK in Mac, they are built-in. They are part o the Accelerate framework and all you have to do is to add the framework and include the header:
 #include <Accelerate/Accelerate.h>
That is really nice, but I do enjoy the amenities that atlas provides, like being able to chose how the matrices are created: column major or row major, for my C/CUDA apps I use row major, and for fortran apps I use column major.

So one have to install atlas. I did it from macports
 sudo port install atlas

This will install an optimized version of blas and lapack and some convenient wrappers.

If you are using the default macports settings the libs are in /opt/local/lib and the
header are in /opt/local/include .

Then in Xcode create a C project, then in the project menu > Edit Project Settings

go to Header Search Path and add /opt/local/include, then go to Library Search Path and
add /opt/local/lib. After this in Other Linker Flags add -llapack -lblas -latlas -lm

That should be it, now your atlas, blas, lapack should be working.

Here is sample file to test

#include <stdio.h>
#include <clapack.h>
double m[] = {
3, 1, 3,
1, 5, 9,
2, 6, 5
};
double x[] = {
-1, 3, -3
};
int
main()
{
int ipiv[3];
int i, j;
int info;
for (i=0; i<3; ++i) {
for (j=0; j<3; ++j) printf("%5.1f", m[i*3+j]);
putchar('\n');
}
info = clapack_dgesv(CblasRowMajor, 3, 1, m, 3, ipiv, x, 3);
if (info != 0) fprintf(stderr, "failure with error %d\n", info);
for (i=0; i<3; ++i) printf("%5.1f %3d\n", x[i], ipiv[i]);
return 0;
}