# Intel® Math Kernel Library (Intel® MKL) - BLAS, CBLAS and LAPACK Compiling/Linking Functions &Fortran and C/C++ Calls

Published on April 6, 2011

- Intel® Math Kernel Library use with C and Fortran languages. Calling LAPACK, BLAS, and CBLAS routines from C language environments.
- How to Call BLAS Functions that Return the Complex Values in C/C++ Code
- Example 1: Calling a Complex BLAS Level 1 Function from C
- Example 2: Calling a Complex BLAS Level 1 Function from C++
- Example 3: Using the CBLAS Interface Instead of Calling BLAS Directly from C Programs
Intel® Math Kernel Library use with C and Fortran languages. Calling LAPACK, BLAS, and CBLAS routines from C language environments.The Intel Math Kernel Library is provided in C and Fortran environments. Not all of the Intel® MKL sub-libraries support both environments. In order to use these sub-libraries in both environments some "rules" need to be observed.
How to Call BLAS Functions that Return the Complex Values in C/C++ CodeCalling complex BLAS function that return complex values from C must be handled carefully. The problem arises because these are Fortran functions, and the return values are handled quite differently for the two languages (C and Fortran) for complex values. While the Fortran language prohibits calling functions as subroutines, or vice versa, if you understand how a particular Fortran implementation returns function values you can make the appropriate call from C. (The Fortran standard defines C interoperability features, but these are not used by Intel® MKL.) A Fortran function that returns a complex value returns its result in a variable passed as the first parameter in the calling sequence - a feature that can be exploited by the C programmer. The following example, for cdotc(), shows how this works. The function from Fortran is called as: result = cdotc( n, x, 1, y, 1 )From C this would look like: cdotc( &result, &n, x, &one, y, &one ) where the hidden parameter is exposed. Note: Intel® MKL has both upper case and lower case entry points in the BLAS so either all upper case or all lower case names are acceptable. Using this form the several level 1 BLAS functions that return complex values can be called from C, and thus, from C++. However, it is still easier to use the cblas interface. For instance, to call the same function using the cblas interface, the user would use: cblas_cdotu( n, x, 1, y, 1, &result ) Note: The complex value comes back expressly in this case. Example 1: Calling a Complex BLAS Level 1 Function from C/* The following example illustrates a call from a C program to the complex BLAS Level 1 function zdotc(). This function computes the dot product of two double-precision complex vectors. In this example, the complex dot product is returned in the structure c. */ #include "mkl.h" #define N 5 void main() { int n, inca = 1, incb = 1, i; typedef struct{ double re; double im; } complex16; complex16 a[N], b[N], c; void zdotc(); n = N; for( i = 0; i < n; i++ ){ a[i].re = (double)i; a[i].im = (double)i * 2.0; b[i].re = (double)(n - i); b[i].im = (double)i * 2.0; } zdotc( &c, &n, a, &inca, b, &incb ); printf( "The complex dot product is: ( %6.2f, %6.2f) ", c.re, c.im ); } Example 2: Calling a Complex BLAS Level 1 Function from C++#include "mkl.h" typedef struct{ double re; double im; } complex16; extern "C" void zdotc (complex16*, int *, complex16 *, int *, complex16 *, int *); #define N 5 void main() { int n, inca = 1, incb = 1, i; complex16 a[N], b[N], c; n = N; for( i = 0; i < n; i++ ){ a[i].re = (double)i; a[i].im = (double)i * 2.0; b[i].re = (double)(n - i); b[i].im = (double)i * 2.0; } zdo tc(&c, &n, a, &inca, b, &incb ); printf( "The complex dot product is: ( %6.2f, %6.2f) ", c.re, c.im ); } Example 3: Using the CBLAS Interface Instead of Calling BLAS Directly from C Programs#include "mkl.h" typedef struct{ double re; double im; } complex16; extern "C" void cblas_zdotc_sub ( const int , const complex16 *, const int , const complex16 *, const int, const complex16*); #define N 5 void main() { int n, inca = 1, incb = 1, i; complex16 a[N], b[N], c; n = N; for( i = 0; i < n; i++ ){ a[i].re = (double)i; a[i].im = (double)i * 2.0; b[i].re = (double)(n - i); b[i].im = (double)i * 2.0; } cblas_zdotc_sub(n, a, inca, b, incb,&c ); printf( "The complex dot product is: ( %6.2f, %6.2f) ", c.re, c.im ); } |

For more complete information about compiler optimizations, see our Optimization Notice.

## 2 comments

TopAnonymous said on Sep 8,2009

I've used the following command to compile. I don't know if it is 100% correct, but it worked for me:

icc $1 -L"$LIBRARY_PATH -lmkl_em64t

Replace $1 with you code-file and remember to select the library suited for your architecture (-lmkl_{ia32, em64t, ipf}) according to:

intel.com/software/products/mkl/docs/mklgs_lnx.htm

Anonymous said on May 28,2009

Yes, but which libraries do we have to link to?

and which includes do we have to use?

One source of information says "use this if that" and the next says "use the other if the other".

We have the use of a computer, could this not sort out what to do?

## Add a Comment

Sign inHave a technical question? Visit our forums. Have site or software product issues? Contact support.