Did it work?

This is the first attempt of a HOWTO that I'm writing on using LAPACK with Visual Studio. In doing so, I've made the assumption that the reader, although probably an expert in some other discipline that requires a linear equation solver, is a novice when it comes to LAPACK -- just as I was when I started. For this reason, I've tried to explain the whys rather than dispense instructions.

PreambleLAPACK [http://www.netlib.org/lapack/] is designed as a two-tiered Fortran library, comprising higher level subroutines and "lower-level Basic Linear Algebra Subprograms (BLAS) in order to effectively exploit the caches on modern cache-based architectures" [http://en.wikipedia.org/wiki/LAPACK]. For reference purposes, the LAPACK installation provides a(n untuned) version of the BLAS which is not optimized for any architecture. This reference BLAS implementation may be orders of magnitude slower than optimized implementations, for matrix factorizations and other computationally intensive matrix operations. Optimized implementations the BLAS are available from a number of vendors and projects such as: Intel (commercial), AMD, and ATLAS, but my favorite is GotoBLAS because it is:

- currently the fastest BLAS implementation[see research report "Choosing the optimal BLAS and LAPACK library" at http://tinyurl.com/m6lmqs ]- trivial to compile- is free (available without cost) for research/academic use.

Compiling GotoBLASThe GotoBLAS source is available from http://www.tacc.utexas.edu/resources/software/#blas (there's short registrationform to fill), and can be compiled for Windows with Cygwin. No changes need to be made to GotoBLAS' config fileMakefile.rule, unless a particular compiler is preferred. Happily, the config file automatically enables multithreading if more than one processor is available.

1. Download and extract the GotoBLAS source to any directory of choice, and make any desired changes to the config file (the default option should also work well).

2. In Cygwin, "cd" to the top-level directory containing the source, and type "make"

3. The result of this process should be a file libgoto_<processor_class>-r<gotoblas_version_number>.a, and a (symbolic) link libgoto.a pointing to this file. (For example, libgoto_banias-r1.26.a)

4. [Edit:] Now for the final part, building a Windows library (*.lib) and dll for GotoBLAS. This is best done in what's called the "Visual Studio command prompt". The Visual Studio command prompt is basically a version of of the command prompt (cmd) that knows the locations of all the Visual Studio tools e.g. compiler, linker.

- Add the path to the Cygwin binaries to your Windows PATH variable. (On my machine, this path is C:\cygwin\bin).(Refer to the section "Adding or Editing Environment Variables" of the following page http://vlaurie.com/computers2/Articles/environment.htm for help on how to do this.)

- In the Visual Studio command prompt, change directory to the "exports" subdirectory of GotoBLAS and run the command "make dll". The result should be something like: libgoto_<processor_class>-r<gotoblas_version_number>.lib and libgoto_<processor_class>-r<gotoblas_version_number>.dll . For example: libgoto_banias-r1.26.dll, or libgoto_northwood-r1.26.dll

Final note:* Windows needs to be told where to find this dll, else you will get a serious error when you try to run your program. There are several ways to do accomplish this. One, is to add the location of the dll to the PATH environment variable. Another is to simply copy the dll to the Windows\system32 folder. I did the later.

Part 2: Using LAPACK subroutines in a Visual (Studio) C/C++ Project -- Part 1

###########################################

1. Download the LAPACK source or precompiled binaries from http://icl.cs.utk.edu/lapack-for-windows/ . Sources have been and can be can be compiled into debug and release versions. File names of the precomputed debug libraries end with the letter "d" e.g. BLASd.lib and lapackd.lib (in comparison to the release versions BLAS.lib and lapack.lib).

2. If you chose to compile LAPACK, you will require a Fortran compiler and libraries. I recommend the Intel Fortran compiler (distributed with libraries) [http://software.intel.com/en-us/articles/intel-software-evaluation-center/].

3. Obtain a tuned version of BLAS for your machine (refer to "Compiling GotoBLAS").

5. rename the prototypes in the above program tovoid dgesv_( )void dgels_( )

to extern "C" void dgesv_( )extern "C" void dgels_( )

6. Add the following libraries to the Visual Studio project settings, under Linker -> Input -> Additional Dependencies. For example, on my laptop:libgoto_banias-r1.26.liblapack.lib

Note: because BLAS libraries commonly provide faster versions of some LAPACK subroutines, the BLAS library must be listed before before LAPACK library.Note: the make sure that the gotoBLAS dll is on your system path e.g. in the WINDOWS\system32 folder, else binary won't run.

7. Compile the project and run the resulting executable. You should get the output:The solution is -0.661082 9.456125 -16.014625

Prologue:Part 3 of this HOWTO will briefly explain what "dgesv" means and how to call it and other LAPACK subroutines with the appropriate arguments.

###########################################

Part 3: More about LAPACK subroutines (example: dgesv)

###########################################

(Note: this is the first draft of this section.)

In the previous section, I explained how to call a LAPACK subroutine e.g. dgesv_ from a C or C++ program, but I did not explain what the dgesv meant as well as its arguments. This is the purpose of this part of the HOWTO. In doing so, I will refer to the LAPACK documentation and hopefully show how easy it is to find an appropriate LAPACK subroutine and create the corresponding C/C++ function prototype for it.

Understanding dgesv

Prefix -- "dge"From the LAPACK naming scheme -- http://www.netlib.org/lapack/lug/node24.html, it is plain to see that:- d in "dgesv" means: double precsion data- ge in "dgesv" means: general as in unsymmetric matrixAs such we can deduce that "dge" refers to the sort of matrix we have -- a general/unymmetric matrix containing double precision data

Suffix -- "sv"This refers to the type of driver routine (solver in lay speak) to be used to solve the linear system. There are two kinds on drivers: simple drivers (suffixed with "sv") and "expert" drivers (suffixed with "svx"). Refer to http://www.netlib.org/lapack/lug/node26.html .

- The first argument is N, an integer. This is marked as an input (meaning argument will not be modified, as opposed to an input argument or an input/output argument) in the documentation. In C/C++ speak we can therefore refer to argument 1 as a constant integer i.e. "const int". However, because in Fortran all ALL arguments, *without exception* are passed by address, the type of N in C/C++ is: const *int . (Same goes for argument 2.)

- Argument 5, marked in the documentation as an output integer array. In C/C++ terms this means the argument is not a const. Therefore argument 5 is of type int* . Same goes for argument 8, even though the argument is not an array (remember, all Fortran arguments are passed by address).

[b]###########################################We are working hard to improve the LAPACK/CLAPACK Windows support but it seems that users still have problems.In case of success or failure, please let us know. We would like to know how we are doing, and how we could further help you.Your post on the forum will be appreciated.

- In the Visual Studio command prompt, change directory to the "exports" subdirectory of GotoBLAS and run the command "make dll". The result should be something like: libgoto_<processor_class>-r<a_number>.lib and libgoto_<processor_class>-r<a_number>.dll . For example: libgoto_banias-r1.26.dll, or libgoto_northwood-r1.26.dll

I don't think so. Did you add the Cygwin path (bin) to your environment variable? (Note that "make dll" should be run NOT by Cygwin but by the visual studio command prompt, and in the exports subfolder). Take a look at the following thread:viewtopic.php?f=2&t=1593

Please let me know if you have solved the problem. Your experiences would help fine-tune the HOWTO.

I tried to compile the gotoblas library following the steps you say, but if I type "make dll" in the command prompt it says it doesn't know the command make.

Also after I did "make" in cygwin, the gotoblas.lib and gotoblas.dll already got compiled and were in the exports folder. Are these the right libraries to use with visual c++ already then? Because if I try them in Visual Studio in debug mode they work, but if I try them in release mode I get errors like "Access violation reading location 0x00905a4d".

but if I type "make dll" in the command prompt it says it doesn't know the command make.

Make sure your environment variables are set right. You want to call "Microsoft nmake" which is what gets called if you are running from a Visual Studio Command Prompt and called "vcvarsall.bat". Here is what the how-to have to say:

4. [Edit:] Now for the final part, building a Windows library (*.lib) and dll for GotoBLAS. This is best done in what's called the "Visual Studio command prompt". The Visual Studio command prompt is basically a version of of the command prompt (cmd) that knows the locations of all the Visual Studio tools e.g. compiler, linker.

Ok I needed to put project properties->linker->optimization->references to default to let it work in release mode.

But now if I compare the time it takes to do a matrix-vector multiplication with this optimized BLAS library and with Matlab, Matlab is still a bit faster. Is this normal? Did anyone else compare the speed between these?

flexo1234 wrote:But now if I compare the time it takes to do a matrix-vector multiplication with this optimized BLAS library and with Matlab, Matlab is still a bit faster. Is this normal? Did anyone else compare the speed between these?

Did you use the BLAS subroutines SGEMV and DGEMV (GotoBLAS implements both). They should outperform Matlab by many orders of magnitude. I would be very surprised if they didn't.

graphicsRat wrote:[Edit:] Now for the final part, building a Windows library (*.lib) and dll for GotoBLAS. This is best done in what's called the "Visual Studio command prompt". The Visual Studio command prompt is basically a version of of the command prompt (cmd) that knows the locations of all the Visual Studio tools e.g. compiler, linker.

That doesn't mean I got my libraries to work :( I'm still struggling.

I would like to help but you need to explain carefully what you done and what's gone/going wrong.

flexo1234 wrote:But now if I compare the time it takes to do a matrix-vector multiplication with this optimized BLAS library and with Matlab, Matlab is still a bit faster. Is this normal? Did anyone else compare the speed between these?

Did you use the BLAS subroutines SGEMV and DGEMV (GotoBLAS implements both). They should outperform Matlab by many orders of magnitude. I would be very surprised if they didn't.

Yes I used dgemv. It took almost 7 seconds for a 8000x8000 matrix with a 8000-vector. Matlab does it in 100ms.If I compare it with the non-optimized BLAS it's only twice as fast (14 seconds), maybe only because gotoBlas is using 2 cpu's.

Hello all,I need to compile gotoBlas under Windows.I do everything as written in the instruction: 1) install cygwin, make and gnu compilers to C:/cygwin. Set the environment variable to Windows Path C:/cygwin/bin2) download and extract gotoblas to the C:/cygwin/home/gotoblas3) cd into gotoblas directory and run :After some time I get gotoblas library libgoto_athlonp_r1.26.a (and libgoto.a).4) then i run VS command prompt, cd into C:/cygwin/home/gotoblas/exports and run command make dll:and I get:

Did you add the Cygwin path (bin) to your environment variable? (Note that "make dll" should be run NOT by Cygwin but by the visual studio command prompt, and in the exports subfolder). Take a look at the following thread:viewtopic.php?f=2&t=1593

Please let me know if you have solved the problem. Your experiences would help fine-tune the HOWTO.

This works on both machines that I use. If you're still having problems compiling the dll, consider emailing Kazushige Goto (author of GotoBLAS) at kgoto[at]tacc[dot]utexas[dot]edu. I'm sure he'll help if you ask nicely.