# How to use GOTOBLAS (instead of ATLAS)?

This post is a wiki. Anyone with karma >750 is welcome to improve it.

GotoBLAS2 has been released by the Texas Advanced Computing Center as open source software under the BSD license. This product is no longer under active development by TACC, but it is being made available to the community to use, study, and extend. GotoBLAS2 uses new algorithms and memory techniques for optimal performance of the BLAS routines. The changes in this final version target new architecture features in microprocessors and interprocessor communication techniques; also, NUMA controls enhance multi-threaded execution of BLAS routines on node.

http://www.tacc.utexas.edu/tacc-projects/gotoblas2/

Compiling the package is not to difficult, a static and a dynamic library are created: libgoto2_core2p-r1.13.a and libgoto2_core2p-r1.13.so

How is it possible to use those in Sage, what steps are needed?

Edit: The question was meant how to use Gotoblas instead of atlas as the default optimized blas library of sage.

edit retag close merge delete

Sorry, I was afraid that might be what you meant. I think you should open a trac ticket for this!

( 2011-02-09 10:40:15 -0500 )edit

Sort by » oldest newest most voted

From what I understand, the question is "how do I completely replace BLAS with GOTOBLAS"? The first place to start would be to take a look at $SAGE_ROOT/devel/sage/module_list.py. This is sort of a master file describing how Sage's various Cython code links with the various libraries included with Sage. For example, dense general matrices use BLAS to handle multiplication whereas integer matrices uses Flint. (Or something like that.) Near the top of the file module_list.py is a section called "BLAS setup". You can add a line like ... elif os.path.exists('%s/lib/libgotoblas.so'%os.environ['SAGE_LOCAL']): BLAS='gotoblas' BLAS2='atlas' elif ...  replacing "gotoblas" with whatever the library is actually named. (I took an educated guess. :) ) There's a note at the top of this section about possibly having to modify sage/misc/cython.py as well. It looks like you'd have to add something similar there in the function cblas() such that it returns the name of the library. Finally, you should probably rebuild Sage with the -a flag a la $ sage -ba


Anyway, the end result should be that all of Sage's Cython-based objects (like matrices) will use GOTOBLAS for the appropriate computations instead of BLAS. I haven't tried this myself but it looks like this is the way to go. I'm just curious if there's a less "invasive" way to do it. (Like your suggestion of symblinking to libcblas.so or whatever the standard "BLAS" library is called.)

more

Oh, thank you,that sounds interesting!

( 2011-02-10 03:46:55 -0500 )edit

indeed -- a big +1 :)

( 2011-02-10 05:32:50 -0500 )edit

You can use C libraries from Sage via Cython. Cython is a Python-like language that makes it easy to write Python programs that manipulate pure C data types allowing for faster computations and the like. In you're unfamiliar with Cython you should take a look at these tutorials for more information.

I'm not familiar with ATLAS or GOTOBLAS but I can provide an example of how to link to some BLAS routines. Suppose I wanted to add two vectors using BLAS, which is included with Sage. To do $y \leftarrow \alpha x + y$ where $x$ and $y$ are double vectors and $\alpha$ is a double scalar, I start by extern-ing the appropriate function, cblas_daxpy() into my Cython code. (i.e. #import-ing) In the Sage notebook, I begin a cell with the following:

%cython

cdef extern from "gsl/gsl_cblas.h":
void cblas_daxpy    (int N, double alpha, double *x, int incX, double *y, int incY)


Then, to perform this addition I add the following (to the same cell)

cdef double x[3]
cdef double y[3]
cdef double alpha = -1

x[0] = 1.0; x[1] = 1.0; x[2] = 1.0
y[0] = 1.0; y[1] = 2.0; y[2] = 3.0

print "x =",x[0],x[1],x[2]
print "y =",y[0],y[1],y[2]
print "a =",alpha

cblas_daxpy(3,alpha,x,1,y,1)

print "alpha x + y =",y[0],y[1],y[2]


The output is

x = 1.0 1.0 1.0
y = 1.0 2.0 3.0
a = -1.0
alpha x + y = 0.0 1.0 2.0


You might need to compile GOTOBLAS in the Sage shell (type "sage -sh") in the terminal but it might also work if you have a system-wide install.

I hope this is a decent enough introduction to using C libraries in Sage. You can fine more information at the Cython website and documentation at http://www.cython.org

more

Hi, thank you - I think this is a good answer howto use the library from inside sage in general. Hum, I am not sure if I saw the header file of GotoBlas, I have to look for that. However my question was meant like: Is it possible to substitute the current Atlas with GotoBlas, so that sage uses it as default for calculations. This might involve some symlinks, some hacks in the Gotoblas code, changes in the sage build scripts etc ... So I give an upvote, but leave the question unanswered for now.

( 2011-02-09 10:03:46 -0500 )edit

- double comment -

( 2011-02-09 10:03:46 -0500 )edit

To fully replace ATLAS with GOTOBLAS would be quite an undertaking in modifying Sage's matrix, vector, etc. objects or, as you suggested, symlinking and "translating" function headers and the like. However, instead of replacing you can allow the matrix object the option of using GOTOBLAS instead of ATLAS by adding some sort of algorithm='gotoblas' attribute. Again, a big undertaking but entirely possible. These objects should interface with ATLAS in a way similar to that described in my post above.

( 2011-02-09 11:20:58 -0500 )edit

I have always understood that GotoBlas uses the same funtion interfaces as BLAS. So the libraries are basically exchangeable somehow without major changes in the code.

( 2011-02-09 18:21:03 -0500 )edit

In that case, I think the first place to start would be to take a look at \$SAGE_ROOT/devel/sage/module_list.py. This is sort of a master file describing how Sage's various Cython code links with the various libraries included with Sage. Near the top of the file is a section called "BLAS setup". You can add a line like elif os.path.exists(...) where "..." is the path to the GOTOBLAS library then set BLAS='gotoblas' or whatever the library is actually named. (There's a note about possibly having to modify sage/misc/cython.py as well.) Anyway, the end result should be that all of Sage's Cython-based objects (like matrices) will use BLAS for the appropriate computations.

( 2011-02-10 02:41:42 -0500 )edit

A major problem that I already mentioned on sage-devel is that ATLAS produces 2 libraries: libf77blas for fortran and libcblas for C. GOTOBlas is only producing libf77blas. lapack is perfectly happy with libf77blas as it is a fortran library. However most of the packages in sage and sage itself require libcblas.

So to use GOTOBlas instead of atlas you need to produce an extra layer that will translate fortran to C, or recode a substantial portion of sage to use libf77blas. Whichever way you want to go, that's a big amount of work.

more

You think a "wrapper" libcblas which links to the functions in libfblas would do? Or does this make no sense because of overhead - speed loss?

( 2011-02-10 21:55:44 -0500 )edit

My main problem was a failing atlas build, and this was solved by using the new atlas spkg from Volker Braun. However I leave some links I found during my research - maybe they are useful if someone finds this thread in the future: http://shimingyoung.blogspot.com/2010/01/gotoblas-and-lapackwrapper.html XXX http://itf.fys.kuleuven.be/~rob/computer/lapack_wrapper/index.html XXX http://seehuhn.de/pages/linear Thanks all for the answers. I hope it is OK if I leave it without solution for now.

( 2011-02-10 22:04:56 -0500 )edit

lapack is fine with libf77blas because they are both fortran libraries. The real hurdle is getting libcblas. I am not sure how to create a wrapper from libf77blas. Function name mapping is easy. The big problem is that C and fortran store matrix differently so you would have to reorganize your arrays before passing them to libf77blas. That's a job and a half.

( 2011-02-11 21:48:04 -0500 )edit

For the record: There is a code example of wrapper function, including the conversion of matrix data from fortran to c format here http://seehuhn.de/pages/matrixfn.

( 2011-02-12 19:40:25 -0500 )edit

I think this is a great question, although it should probably include a reference to the sage-devel thread.

Of course (as you know) there isn't really much information there, except some concerns that it is no longer actively maintained, and therefore might not be a worthwhile pursuit for Sage. Also there seems to be some concern that there would be trouble communicating between the C code in Sage and the Fortran code in GotoBLAS. Maybe someone proposed a resolution for that, but I can't tell :(

In any case, thanks for asking; maybe someone who knows better will notice the question here . . .

UPDATE:

Oh, maybe I do have an idea: The answer to this question is probably the same as the answer to "How do I use a compiled C library in Python?". For that, the answer must begin with Cython. In particular, here is the Cython documentation page for Using C libraries. Strangely, however, the examples on that page don't seem to ever explain what to do if you already have the .a or .so files. Maybe you can figure that out, or already know -- I think distutils has something to do with this. Anyway, there is a related question on Stack Overflow which does seem to address this . . . maybe between these two there's enough to get you going?

p.s. I really don't understand C, libraries, Cython, or distutils, I've just read about them a lot . . . my apologies if this is all totally obvious and unhelpful, or wrong.

more

Hi Niles, good idea to put it in a wider framework, i.e. how to use compiled C library generally.

( 2011-02-09 09:59:18 -0500 )edit