<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated on Wed, 25 Nov 2009 07:04:49 -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/sample-code/</link>
    <description></description>
    <language>en-us</language>
    <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 /><ol>
<li><b>Build a custom library:</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>
<li>The library as built above will depend on the OpenMP* threading runtime library used by Intel MKL (libiomp5.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. </li>
</ol></li>
<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("./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> ]]></description>
      <link>http://software.intel.com/en-us/articles/using-intel-mkl-in-your-python-programs</link>
      <pubDate>Fri, 13 Nov 2009 06:59:08 -0800</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® Math Kernel Library 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 style="padding-left: 30px;">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>Mon, 17 Aug 2009 08:48:59 -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>
    <item>
      <title>Using Intel® MKL in your C# program</title>
      <description><![CDATA[ <br />
<p><strong>Introduction </strong><br /><br />Several users have asked how to call and link the Intel® Math Kernel Library (Intel® MKL) functions from their C# programs. While the standard way of interfacing with third party software libraries from C# is well documented, some of the steps in interfacing with Intel MKL specifically may still be confusing. The attached source code package is intended to show how to navigate the whole process for Intel MKL users. These examples show how to create a DLL from Intel MKL libraries, call those functions from their C# source, and interface with that DLL.<br /><br />Examples are provided for calling the BLAS, LAPACK, DFTI (the FFT interface), the PARDISO direct sparse solver, and the vector math library (VML).<br /><br /><strong>Building the Examples</strong><br /><br />Follow these steps to build the example programs:</p>
<ul>
<li>unzip the contents of the attached zip file</li>
<li>Open a Microsoft Visual Studio command prompt or add the Microsoft.NET Framework to the PATH environment variable in another command prompt</li>
<li>Run the build script (makefile) using nmake 
<ul>
<li>Example: <code>nmake ia32 MKLROOT="c:\program files\intel\mkl\10.1.1.015"</code></li>
<li>The makefile provides further explanation of the parameters in comments</li>
</ul>
</li>
</ul>
<p>This will create an executable for each of the example programs.</p>
<p style="TEXT-ALIGN: left"><br />Example files: <a onclick="ndownload('http://software.intel.com/file/19203')" href="javascript:void(0)">Intel_MKL_C#_Examples.zip</a></p>
<p style="TEXT-ALIGN: center"><br /><br /><em>Take our <a href="http://intelsoftwareproductsurvey.com/survey/149715/1442/" target="_blank">C++/C# interface survey</a> for Intel® Math Kernel Library </em></p> ]]></description>
      <link>http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program</link>
      <pubDate>Wed, 27 May 2009 15:31:23 -0700</pubDate>
      <comments>http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program#comments</comments>
      <guid isPermaLink="true">http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program</guid>
      <category>Intel® Math Kernel Library Knowledge Base</category>
    </item>
  </channel></rss>