3
$\begingroup$

I am trying to find the most optimized way to perform Matrix Multiplication of very large sizes in C language and under Windows 7 or Ubuntu 14.04. And searching led me to BLAS, LAPACK and ATLAS. However, I couldn't tell which one I can use? I mean BLAS Level 3 DGEMM seems like the function for my case but I couldn't find out how to use it in C. Can anyone help me with this issue?

Thank you in advance

$\endgroup$
1
  • $\begingroup$ If matrix multiplication is all that is desired, yes, BLAS Level 3 seems a good place to start. StackOverflow has this now closed Question from 2010 on requesting documentation/getting started info. You may find the interface appears more complicated than necessary, which is partly a result of historical compatibility with Fortran callers. $\endgroup$
    – hardmath
    Jun 28, 2015 at 4:09

2 Answers 2

5
$\begingroup$

Yes, you want to call the BLAS routine DGEMM. The place to start for how to call it from C is to look at the documentation for DGEMM, which you can find online. Then you want to understand how to call FORTRAN routines from C (DGEMM, like all the standard BLAS routines has a FORTRAN calling convention). For example, this document https://computing.llnl.gov/tutorials/bgq/mixedProgramming2.pdf might be helpful.

By the way, LAPACK provides higher-level linear algebra operations which call the BLAS routines for their low-level functionality. ATLAS is a high-performance implementation of BLAS. Another high performance open-source implementation of the BLAS is http://www.openblas.net/.

$\endgroup$
3
  • $\begingroup$ Thanks for answering. Do you know if BLAS can be used under windows? $\endgroup$ Jun 27, 2015 at 21:41
  • $\begingroup$ To expand a bit on this answer, the BLAS interface was originally designed for calling the BLAS routines from Fortran code. You can store your matrices in column major order as in Fortran and use the traditional BLAS interface, or you can make use of a newer C interface that has been defined by the BLAS Technical Committee. $\endgroup$ Jun 27, 2015 at 23:32
  • $\begingroup$ Yes, many versions of the BLAS libraries are used on Windows. Especially since you are interested in a high-performance version, I suggest you download pre-built, binary versions of OpenBLAS from here: sourceforge.net/projects/openblas/files/v0.2.14 $\endgroup$ Jun 28, 2015 at 0:15
4
$\begingroup$

By its anatomy the DGEMM ($C = \alpha AB + \beta C$) is one of the most optimizeable routines in computer science. For historic reason this routine is implemented in FORTRAN or the implementation provides at least an interface which is compatible the old FORTRAN one. If you want to call it from C you have to take care of the function arguments because all of them need to be a pointer (even the scalar values $\alpha$ and $\beta$ or the sizes of the matrices).

In order to avoid this converting-to-FORTRAN problem, one can also use the so called CBLAS interface which is provided by all main BLAS implementations (ATLAS, OpenBLAS, Intel MKL) as well. This interface allows you to pass all function arguments in a C style way. But it has one big trap door. If you use the classical row-oriented C-style way for storing two dimensional data, the CBLAS interface will copy all data in the FORTRAN colum-major format which costs time and memory. So if you use CBLAS try to use the colum-major storage as in Fortran.

The question for the most optimized way is easy to answer: If you have an license of the Intel Compiler Suite or at least of the Intel MKL use this as BLAS library. If not than use OpenBLAS. Both reach more than 90% of the theoretical peak performance of the computer but OpenBLAS has some problems with its performance on multi-socket systems.

$\endgroup$
1
  • $\begingroup$ Well crafted answer. Contains info already seen in a number of other sources but made concise here. I had not seen the comment about OpenBLAS on multi-socket systems before. $\endgroup$ Apr 20, 2016 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.