7 <strong>Intel</strong>® M<strong>at</strong>h Kernel Library User’s GuideCBLAS is a C-style interface to the BLAS routines. You can call CBLAS routines usingregular C-style calls. When using the CBLAS interface, the header file mkl.h will simplifythe program development as it specifies enumer<strong>at</strong>ed values as well as prototypes of all thefunctions. The header determines if the program is being compiled with a C++ compiler,<strong>and</strong> if it is, the included file will be correct for use with C++ compil<strong>at</strong>ion. Example 7-3illustr<strong>at</strong>es the use of CBLAS interface.Calling BLAS Functions Th<strong>at</strong> Return the Complex Values inC/C++ CodeYou must be careful when h<strong>and</strong>ling a call from C to a BLAS function th<strong>at</strong> returns complexvalues. The problem arises because these are Fortran functions <strong>and</strong> complex return valuesare h<strong>and</strong>led quite differently for C <strong>and</strong> Fortran. However, in addition to normal functioncalls, Fortran enables calling functions as though they were subroutines, which provides amechanism for returning the complex value correctly when the function is called from a Cprogram. When a Fortran function is called as a subroutine, the return value shows up asthe first parameter in the calling sequence. This fe<strong>at</strong>ure can be exploited by the Cprogrammer.The following example shows how this works.Normal Fortran function call: result = cdotc( n, x, 1, y, 1 ).A call to the function as asubroutine: call cdotc( result, n, x, 1, y, 1).A call to the function from C(notice th<strong>at</strong> the hiddenparameter gets exposed): cdotc( &result, &n, x, &one, y, &one ).NOTE. <strong>Intel</strong> MKL has both upper-case <strong>and</strong> lower-case entry points inBLAS, with trailing underscore or not. So, all these names are acceptable:cdotc, CDOTC, cdotc_, CDOTC_.Using the above example, you can call from C, <strong>and</strong> thus, from C++, several level 1 BLASfunctions th<strong>at</strong> return complex values. However, it is still easier to use the CBLAS interface.For instance, you can call the same function using the CBLAS interface as follows:cblas_cdotu( n, x, 1, y, 1, &result )7-6
Language-specific Usage Options 7NOTE. The complex value comes back expressly in this case.The following example illustr<strong>at</strong>es a call from a C program to the complex BLAS Level 1function zdotc(). This function computes the dot product of two double-precision complexvectors.In this example, the complex dot product is returned in the structure c.Example 7-1 Calling a complex BLAS Level 1 function from C#include "mkl.h"#define N 5void 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 );}7-7