|
|
|
 |
 |
|
| Intel® Math Kernel Library (Intel® MKL) |
| BLAS, CBLAS and LAPACK Compiling/Linking Functions & Fortran and C/C++ Calls |
|
 |
|
|
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. An alternative to calling BLAS routines from a C-language program is to use the CBLAS interface. |
| 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 which 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. Because Fortran lets you call functions as though they were subroutines, however, there is a mechanism for returning the complex value correctly when the function is called from a C program. When a Fortran function is called as a subroutine, the return value shows up 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 ) But as a subroutine it can be called as: call cdotc( result, 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)\n", 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; } zdotc(&c, &n, a, &inca, b, &incb ); printf( "The complex dot product is: ( %6.2f, %6.2f)\n", 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)\n", c.re, c.im ); }
This applies to:
|
|
 |
|
Solution ID: CS-017175
Date Created: 22-Nov-2004
Last Modified: 13-Apr-2006
|
|