<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated on Sat, 11 Feb 2012 22:57:17 -0800 -->
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <atom:link href="http://software.intel.com/en-us/articles/intel-mkl-kb/type/sample-code/feed/" rel="self" type="application/rss+xml" />
    <title>Intel Software Network articles Feed</title>
    <link>http://software.intel.com/en-us/articles/intel-mkl-kb/type/sample-code/</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>Quick Link Intel® MKL In Xcode* IDE: A Fortran Sample</title>
      <description><![CDATA[ Since version 10.x, Intel® Math Kernel Library for Mac OS* comes bundled with Intel® Fortan or C/C++ Compiler for Mac OS*, which was integrated into Xcode Integrated Development Enviornments after default installation. This provided us a quick link way to access MKL in Xcode IDE. <br /><br />This tutorial is intended to show the quick way using XCode 4.0 and Intel® Fortran Compiler XE 12.1, but we hope that it will be helpful for users of the other versions. Here is the<a href="http://software.intel.com/file/41405" title="MKL_dgemm_f"> fortran sample<br /><br /></a>For gernal link way, please refer to  <br /><a nodeindex="1" sizcache="0" data-wap="{&quot;linktype&quot;:&quot;result&quot;}" href="http://software.intel.com/en-us/articles/intel-math-kernel-library-for-mac-os-compiling-and-linking-with-xcode?wapkw=Xcode">Compiling and linking MKL with <b nodeindex="1">Xcode</b>*</a>  <br /><a nodeindex="1" sizcache="0" data-wap="{&quot;linktype&quot;:&quot;result&quot;}" href="http://software.intel.com/en-us/articles/how-to-link-application-against-intel-mkl-using-xcode-ide?wapkw=xcode">How to link application against Intel MKL using <b nodeindex="1">XCode</b> IDE </a><br /> <br />1.<b>    </b><b>Create a new Xcode* project<br /><br /></b>
<p align="left" >a)   Copy sample source code: dgemmx.f and common_func.f from &lt;mkl install directory /examples/blas/source&gt; to your work directory, for example, /Users/yhu5/blas.<br />b)   Launch the Xcode application, typically found in the <b>Developer &gt; Applications</b> folder.<br />c)<strong>   </strong>In Xcode Menu, select <b>File</b> &gt; <b>New</b> &gt; <b>New Project...</b>. , The <b>New Project Assistant</b> window opens. Select <b>Application</b> in the left pane. Select a template, for example, <b>Command Line Tool</b>, and click <b>Next</b>. Name your project, for example, <b>MKL_dgemm_f</b>, and select a type C. Click <b>Next</b>. Specify a directory for your project<b> ,</b> For example, /Users/yhu5/blas. Click <b>Create.<br /> <img src="http://software.intel.com/file/41379" alt="mkl1.png" title="mkl1.png" /></b></p>
<p align="left" >d)     Xcode creates the named project directory, with an .xcodeproj extension and several new source files like main.c etc.  Remove main.c, etc. file  and add dgemmx.f and common_func.f into ‘Source' project folder.</p>
<p align="left" > <img src="http://software.intel.com/file/41380" alt="mkl2.png" title="mkl2.png" /></p>
<p align="left">Each Xcode project has its own Project Editor window that displays project source files, targets, and executables.</p>
<p align="left"><strong>2. Setting MKL Options in Xcode IDE</strong></p>
<p align="left">In the Xcode project, select the target and click <b>Build Setting</b>. Under the <strong>Build Settings</strong> tab, click <strong>All. </strong>Scroll down until you see IFORT_MKL, Select "Use threaded Intel® Math Kernel Library (or "Use non-thread Intel @ Math Kernal Library.)</p>
<p align="left" > <img src="http://software.intel.com/file/41381" alt="mkl3.png" title="mkl3.png" /></p>
<p align="left"><strong>3. Build and Run the Project</strong></p>
<p align="left" >Select <b>Product</b> &gt; <b>Build</b>. Or Click the  ‘Run' button on project tools bar, the project will build successfully. <br />But may run with error message dyld: Library not loaded: libmkl_intel_lp64.dylib<img src="http://software.intel.com/file/41404" alt="mkl7.png" title="mkl7.png" /></p>
<p align="left">(other compiler errors, like files is not found or Open file failed, please change the path of the files and the code in dgemmx.f, line 49, OPEN(UNIT=1, FILE='/Users/xx/xx/dgemmx.d',STATUS='OLD' so Xcode can find them.)<br /><br /><strong>4) Set Run Environment</strong></p>
<p align="left">Open the Scheme Editor, click the <b>Scheme</b> button in the Project Editor Toolbar and select <b>Edit Scheme, </b>Under the <b>Arguments</b> tab, add DYLD_LIBRARY_PATH as an environment variable. <br />Set the Value as the full path to the Intel compiler's /lib directory and MKL directory<br />DYLD_LIBRARY_PATH=/opt/intel/composer_xe_2011_sp1.8.269/compiler/lib:</p>
<p align="left"> /opt/intel/composer_xe_2011_sp1.8.269/mkl/lib</p>
<p align="left" ><img src="http://software.intel.com/file/41382" alt="mkl4.png" title="mkl4.png" /><br /><br />Click <strong>Run</strong> button again, you will get the result in Output box.</p>
<p ><img src="http://software.intel.com/file/41383" alt="mkl5.png" title="mkl5.png" /></p>
<p>Please refer to Intel Compiler User and Reference Guide =&gt;Building Applications with Xcode* IDE"<br />or "Configuring the Apple Xcode* Developer Software to Link with Intel MKL" section in MKL user guide for more details. <br />And refer to "Linking Your Application with Intel® MKL" section in the <a target="_blank" href="http://software.intel.com/en-us/articles/intel-math-kernel-library-documentation/">Getting Started</a> document to understand which libraries required by your code.<br /><br /><strong>Operating System:</strong></p>
<p>
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td class="xs">Mac OS*</td>
</tr>
</tbody>
</table>
</p> ]]></description>
      <link>http://software.intel.com/en-us/articles/quick-link-intel-mkl-in-xcode-ide-a-fortran-sample/</link>
      <pubDate>Sat, 28 Jan 2012 08:00:00 -0800</pubDate>
      <comments>http://software.intel.com/en-us/articles/quick-link-intel-mkl-in-xcode-ide-a-fortran-sample/#comments</comments>
      <guid isPermaLink="true">http://software.intel.com/en-us/articles/quick-link-intel-mkl-in-xcode-ide-a-fortran-sample/</guid>
      <category>Intel® C++ Compiler for Mac OS X* Knowledge Base</category>
      <category>Intel® Fortran Compiler for Mac OS X* Knowledge Base</category>
      <category>Intel® Math Kernel Library Knowledge Base</category>
    </item>
    <item>
      <title>Using Cluster MKL PBLAS/ScaLAPACK FORTRAN routine in your C program</title>
      <description><![CDATA[ <b>Introduction</b><br /><br />Intel MKL PBLAS and ScaLAPACK routines are written in FORTRAN 77  (with exception of a few utility routines written<br />in C to exploit the IEEE arithmetic). It is difficult for the programmer who used to C language convention. This article describles a sample to call the Intel® Math Kernel Library (Intel® MKL) PBLAS routines from a C program to show the difference between C calling converstion and Fortran calling conversion . <br />Please see the <a href="http://software.intel.com/en-us/forums/showthread.php?t=71886">detail discussion</a> on <a href="http://software.intel.com/en-us/forums/intel-math-kernel-library/">MKL forum</a>. <br /><br /><strong>1. Problem</strong><br />    The sample is using PDGEMV(), which computes a distributed matrix-vector product y =<em>alpha</em>Ax+<em>beta*</em>y. <br />    A= [1  4  7  10  13 ; 3   6   9   12  15 ;  5  8   11  14  17 ; 7  10  13  16  19;  9  12  15  18  21] <br />    and x=[1 ; 1; 0; 0; 1]<sup>T</sup>, x is a column vector, Call PDGEMV() routine to compute y=Ax.  <br />    The right result is y=[18; 24; 30; 36; 42]<sup>T</sup> <br /><br /><strong>2. Code sample in C:</strong> <a href="http://software.intel.com/file/25271">pdgemv.c</a><br />    The function call in C is like,<br />
<p>    double alpha = 1.0; double beta = 0.0;<br />    pdgemv_("N",&amp;M,&amp;M,&amp;alpha,A,&amp;ONE,&amp;ONE,descA,x,&amp;ONE,&amp;ONE,descx,&amp;ONE,&amp;beta,y,&amp;ONE,&amp; amp;ONE,descy,&amp;ONE);<br /><strong> <br />3. Build and Run it</strong><br />    Build it under linux environment, assume intel 64bit application. <br />    ! set environment and build pdgemv.c <br />    $source /opt/intel/mkl/10.x.x.0xx/tools/environment/mklvarsem64t.sh<br />    $source /opt/intel/mpi/3.x.x/bin64/mpivars.sh<br />    $source /opt/intel/Compiler/11.x/0xx/bin/iccvars.sh intel64<br />    $mpiicc -w -o pdgemv pdgemv.c -I/opt/intel/mkl/10.x.x.0xx/include -L/opt/intel/mkl/10.x.x.0xx/lib/em64t <br />     -lmkl_scalapack_lp64 -lmkl_blacs_intelmpi_lp64 -lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -liomp5 -lpthread<br /><br />    ! run the application <br />    $ mpirun -n 4 ./pdgemv<br /><br /><strong>4. Notes</strong><br />Intel MKL PBLAS/ScaLAPACK routines are written in FORTRAN interface, so Column-major are used and row-major is not acceptable.  If you gives the matrix to these routines in row-major - even your code is completely right from C (Row-major) point of view,  the result will be almost always wrong.</p> ]]></description>
      <link>http://software.intel.com/en-us/articles/using-cluster-mkl-pblasscalapack-fortran-routine-in-your-c-program/</link>
      <pubDate>Mon, 01 Mar 2010 08:00:00 -0800</pubDate>
      <comments>http://software.intel.com/en-us/articles/using-cluster-mkl-pblasscalapack-fortran-routine-in-your-c-program/#comments</comments>
      <guid isPermaLink="true">http://software.intel.com/en-us/articles/using-cluster-mkl-pblasscalapack-fortran-routine-in-your-c-program/</guid>
      <category>Intel® Math Kernel Library Knowledge Base</category>
      <category>Intel® MPI Library for Linux* Knowledge Base</category>
    </item>
    <item>
      <title>Using Intel® MKL in your Python program</title>
      <description><![CDATA[ <b>Introduction</b><br /><br />This article describes how to use the Intel® Math Kernel Library (Intel® MKL) from a Python program. There's more than one way to write Python programs to interface with native libraries. I've simply chosen one so that I can emphasize what might be less commonly known: how to build a custom shared library from Intel MKL so that you can call it from your script. <br /><br />I'll run through the basics steps of accessing Intel MKL from Python 2.6 on a 64-bit Linux OS. The example program calls the CBLAS interface to the DGEMM function which performs a multiplication (and optional add) on general, double precision matrices. Much more about these functions can be found in the Intel® MKL reference manual (<a href="http://software.intel.com/en-us/articles/intel-math-kernel-library-documentation/">available online here</a>). <br /><br /><i>Update</i>: With Intel MKL 10.3 there is a new dynamic library which removes the need to create your own custom library. So if you're a 10.3 user you don't need to do step 1 below. To make some changes in the behavior of this library you can look up these routines in the reference manual: mkl_set_interface_layer, mkl_set_threading_layer, mkl_set_xerbla, and mkl_set_progress.<br /><br /><ol>
<li><b>Build a custom library (<i>now unnecessary with Intel MKL 10.3</i>):</b> To interface with Intel MKL from Python we recommend you use the custom library builder in the tools/builder sub-directory of the Intel MKL package. The Intel® MKL User's Guide has documentation on this tool (<a href="http://software.intel.com/en-us/articles/intel-math-kernel-library-documentation/">docs online</a>). Here briefly are the steps I took to do this: <br /><br /><ol>
<li>Set up your environment to use the desired version of Intel MKL:<br /><code>source /<i>&lt;MKLpath&gt;</i>/tools/environment/mklvarsem64t.sh</code></li>
<li>Build the DLL:<br /><code>cd /<i>&lt;MKLpath&gt;</i>/tools/builder</code><br /><code>make em64t name=~/libmkl4py export=cblas_list</code> </li>
</ol></li>
<br />
<li><b>Add library paths to LD_LIBRARY_PATH: </b>All the Intel MKL libraries needed must be in directories contained in the LD_LIBRARY_PATH environment variable. The library as built above will depend on the OpenMP* threading runtime library used by Intel MKL (libiomp5.so) so you should make sure that both libraries, libmkl4py.so and libiomp5.so, are in a directory specified in the LD_LIBRARY_PATH environment variable. If you're using Intel MKL 10.3 you need to add the directories for both libmkl_rt.so and libiomp5.so (if you want it to run on multiple cores). </li>
<br />
<li><b>Call Intel MKL in your Python script:</b> The following is a simple script (also available <a href="http://software.intel.com/file/22334">here</a>) that loads the shared library just created and calls the matrix function. <br />
<ul>
<code></code>
<pre name="code" class="python">from ctypes import *<br /><br /># Load the share library<br />mkl = cdll.LoadLibrary("./libmkl_rt.so")<br /># For Intel MKL prior to version 10.3 us the created .so as below<br /># mkl = dll.LoadLibrary("./libmkl4py.so")<br />cblas_dgemm = mkl.cblas_dgemm<br />
<br />def print_mat(mat, m, n):<br />  for i in xrange(0,m):<br />    print " ",<br />    for j in xrange(0,n):<br />      print mat[i*n+j],<br />    print <br /><br /># Initialize scalar data<br />Order = 101  # 101 for row-major, 102 for column major data structures<br />TransA = 111 # 111 for no transpose, 112 for transpose, and 113 for conjugate transpose<br />TransB = 111<br />m = 2<br />n = 4<br />k = 3<br />lda = k<br />ldb = n<br />ldc = n<br />alpha = 1.0<br />beta = -1.0<br /><br /># Create contiguous space for the double precision array<br />amat = c_double * 6      <br />bmat = c_double * 12<br />cmat = c_double * 8<br /><br /># Initialize the data arrays<br />a = amat(1,2,3, 4,5,6)<br />b = bmat(0,1,0,1, 1,0,0,1, 1,0,1,0)<br />c = cmat(5,1,3,3, 11,4,6,9)<br /><br />print "\nMatrix A ="<br />print_mat(a,2,3) <br />print "\nMatrix B ="<br />print_mat(b,3,4)<br />print "\nMatrix C ="<br />print_mat(c,2,4)<br /><br />print "\nCompute", alpha, "* A * B + ", beta, "* C"<br /><br /># Call Intel MKL by casting scalar parameters and passing arrays by reference<br />cblas_dgemm( c_int(Order), c_int(TransA), c_int(TransB), \<br />             c_int(m), c_int(n), c_int(k), c_double(alpha), byref(a), c_int(lda), \<br />             byref(b), c_int(ldb), c_double(beta), byref(c), c_int(ldc))<br /><br />print_mat(c,2,4)<br />print</pre>
</ul>
</li>
<li>A few notes: 
<ul>
<li>Matrices in the BLAS and LAPACK parts of Intel MKL are stored in one dimensional arrays and integers are used to specify their geometry. </li>
<li>I've actually loaded here CBLAS interface to the general matrix multiply function which allows you to choose how the matrix is specified. In my script I've listed the matrix by rows (row-major ordering). If you do not use the cblas interface to the BLAS or if you use LAPACK you should keep in mind that these functions assume the Fortran method of listing matrices by columns (column-major ordering).</li>
</ul>
</li>
</ol>Here is the Python code I created that implements the steps above: <a href="http://software.intel.com/file/22334">matmult.py</a><br /><br /><strong>Examples code:</strong><br /><br />
<p>We extended the list of examples demonstrate how possible to call different ( not only widespread example like dgemm ) from the Python program:</p>
<p>See the list of 3 different examples attached:<br />dft.zip  - shows the Python program calls 1D DFTI API<br />spblas.zip - shows how to call  matrix-matrix multiplication routine for a sparse matrix stored in the block compressed format (BSR)<br />vsl.zip - shows how to call vdRngGaussian routine ( generates normally distributed random numbers)  from VSL domain.</p>
<p><strong>Notes:<br /></strong>Each zip file contains  *.res and *_list files mean input file and file for custom library building correspondingly</p>
<div></div> ]]></description>
      <link>http://software.intel.com/en-us/articles/using-intel-mkl-in-your-python-programs/</link>
      <pubDate>Mon, 21 Sep 2009 00:00:00 -0700</pubDate>
      <comments>http://software.intel.com/en-us/articles/using-intel-mkl-in-your-python-programs/#comments</comments>
      <guid isPermaLink="true">http://software.intel.com/en-us/articles/using-intel-mkl-in-your-python-programs/</guid>
      <category>Intel® C++ Compiler for Linux* Knowledge Base</category>
      <category>Intel® C++ Compiler for Mac OS X* Knowledge Base</category>
      <category>Intel® C++ Compiler for Windows* Knowledge Base</category>
      <category>Intel® Fortran Compiler for Linux* Knowledge Base</category>
      <category>Intel® Fortran Compiler for Mac OS X* Knowledge Base</category>
      <category>Intel® Math Kernel Library Knowledge Base</category>
      <category>Intel® Visual Fortran Compiler for Windows* Knowledge Base</category>
    </item>
    <item>
      <title>LAPACK Examples</title>
      <description><![CDATA[ <p>A set of <a target="_blank" href="http://software.intel.com/sites/products/documentation/hpc/mkl/lapack/mkl_lapack_examples/index.htm">LAPACK examples</a> is now available online. These examples are based on the ones available with the product in the examples directory, but are more easily browsed and contain no dependencies on other source files or auxiliary libraries. Just paste the code into a file and link with the Intel® MKL libraries. </p>
<p >Link: <a target="_blank" href="http://software.intel.com/sites/products/documentation/hpc/mkl/lapack/mkl_lapack_examples/index.htm">LAPACK examples</a></p> ]]></description>
      <link>http://software.intel.com/en-us/articles/lapack-examples/</link>
      <pubDate>Fri, 28 Aug 2009 00:00:00 -0700</pubDate>
      <comments>http://software.intel.com/en-us/articles/lapack-examples/#comments</comments>
      <guid isPermaLink="true">http://software.intel.com/en-us/articles/lapack-examples/</guid>
      <category>Intel® Math Kernel Library Knowledge Base</category>
    </item>
  </channel></rss>
