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

Published:04/06/2011   Last Updated:04/06/2011

Page Contents:


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.

LAPACK When calling LAPACK routines from C-language programs, make sure that you follow Fortran rules: Pass variables by 'address' as opposed to pass by 'value'. Be sure to store your data Fortran-style, i.e. data stored column-major rather than row-major order.
BLAS BLAS routines are Fortran-style routines. If you call BLAS routines from a C-language program you must follow the Fortran-style calling conventions: Pass variables by address as opposed to passing by value. Be sure to store data Fortran-style, i.e. data stored column-major rather than row-major order. We recommend that C and C++ programmers use the CBLAS interface to avoid simple mistakes when following these conventions.
CBLAS CBLAS routines are provided as the C-style interface to the BLAS routines. Call CBLAS routines using regular C-style calls. When using the CBLAS interface, the header file mkl.h will simplify the programmer's development as it specifies enumerated values as well as prototypes of all the functions. The header determines if the program is being compiled with a C++ compiler, and if it is, the included file will be correct for use with C++ compilation.
MKL.H When using the CBLAS interface, the header file mkl.h will simplify the program development since it specifies enumerated values as well as prototypes of all the functions. The header determines if the program is being compiled with a C++ compiler, and if it is, the included file will be correct for use with C++ compilation.



How to Call BLAS Functions that Return the Complex Values in C/C++ Code
Calling 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 );
}

Product and Performance Information

1

Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804