Skip to content
Snippets Groups Projects
MATLAB.html 13.1 KiB
Newer Older
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>
Marco van Oort's avatar
Marco van Oort committed
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<link href="style.css" rel="stylesheet" type="text/css">
Marco van Oort's avatar
Marco van Oort committed
<link href="print.css" rel="stylesheet" type="text/css" media="print">

<title>Mondriaan and MATLAB</title>
</head>

<body>

Marco van Oort's avatar
Marco van Oort committed
<div id="mainContainer">

<div id="pageNav">
	<div><a href="USERS_GUIDE.html">Mondriaan</a></div>
	<div><a href="HYPERGRAPH.html">Hypergraphs</a></div>
	<div><a href="USERS_GUIDE_OPT.html">MondriaanOpt</a></div>
</div>

<h2>Mondriaan and MATLAB</h2>
<hr>
<p>
This guide is a step-by-step introduction to using Mondriaan
together with MATLAB.
For more extensive information about Mondriaan, please take
a look at the <a href="USERS_GUIDE.html">user's guide</a>.
</p>
<hr>

<h3>Known issues</h3>
<p>Unfortunately, the Matlab interface for Mondriaan does not work with the most recent
Matlab versions any more. We do not envision to repair this in the near future.
However, volunteers for this task are always welcome!
</p>

<h3>How to download and install Mondriaan</h3>
<p>
Download the latest version from the
<a href="http://www.staff.science.uu.nl/~bisse101/Mondriaan/mondriaan.html">
Mondriaan software homepage</a>. Uncompress with
</p>
<ul>
Marco van Oort's avatar
Marco van Oort committed
<li><code>% tar xzvf mondriaan4.tar.gz</code></li>
Marco van Oort's avatar
Marco van Oort committed
This will create a directory <code>Mondriaan4</code>
which contains all the files of the Mondriaan package. 
Marco van Oort's avatar
Marco van Oort committed
To enable MATLAB support, open the file <code>Mondriaan4/mondriaan.mk</code>
with a text-editor and look for a line which looks similar to
</p>
<ul>
Marco van Oort's avatar
Marco van Oort committed
<li><code>#MATLABHOMEDIR := /usr/local/matlab</code></li>
</ul>
<p>
Change the directory on the right-hand side to your installation
Marco van Oort's avatar
Marco van Oort committed
directory of MATLAB and remove the <code>#</code> in front of the line,
such that it looks similar to
</p>
<ul>
Marco van Oort's avatar
Marco van Oort committed
<li><code>MATLABHOMEDIR := /your/matlab/installation/directory</code></li>
Marco van Oort's avatar
Marco van Oort committed
Furthermore make sure that the variable <code>MEXSUFFIX</code> is set to the proper
extension for MATLAB binary files for your system (from the Mathworks <a href="http://www.mathworks.nl/help/matlab/ref/mexext.html">site</a>):
</p>
<table border="1">
Marco van Oort's avatar
Marco van Oort committed
<tr><td><b>Platform</b></td><td><b><code>MEXSUFFIX</code></b></td></tr>
<tr><td>Linux (32-bit)</td><td><code>mexglx</code></td></tr>
<tr><td>Linux (64-bit)</td><td><code>mexa64</code></td></tr>
<tr><td>Apple Macintosh (32-bit)</td><td><code>mexmaci</code></td></tr>
<tr><td>Apple Macintosh (64-bit)</td><td><code>mexmaci64</code></td></tr>
<tr><td>Microsoft Windows (32-bit)</td><td><code>mexw32</code></td></tr>
<tr><td>Microsoft Windows (64-bit)</td><td><code>mexw64</code></td></tr>
Marco van Oort's avatar
Marco van Oort committed
For example: on a 32-bit Macintosh system we would have <code>MEXSUFFIX := mexmaci</code>.
</p>
<p>
Now we are ready to compile Mondriaan, run
</p>
<ul>
Marco van Oort's avatar
Marco van Oort committed
<li><code>% make</code></li>
</ul>
<p>
which will build Mondriaan and the associated tools.
</p>

<h3>A small example</h3>
<p>
In this example we will partition a small test matrix using the
MATLAB interface of Mondriaan.
</p>
<p>
As test matrix we can use <a href="http://www.staff.science.uu.nl/~bisse101/Matrices/tbdmatlab.mtx.gz">tbdmatlab.mtx.gz</a>
Marco van Oort's avatar
Marco van Oort committed
from the Mondriaan website. The archive should be extracted to the <code>Mondriaan4/tools</code> directory.
Marco van Oort's avatar
Marco van Oort committed
Start MATLAB and navigate to the <code>Mondriaan4/tools</code> directory in the <i>Current Directory</i>
Marco van Oort's avatar
Marco van Oort committed
To read and view <code>tbdmatlab.mtx</code>, issue
Marco van Oort's avatar
Marco van Oort committed
<li><code>A = mmread('tbdmatlab.mtx');</code></li>
<li><code>spy(A)</code></li>
Marco van Oort's avatar
Marco van Oort committed
We can partition the matrix <code>A</code> among 30 processors with a maximum imbalance of 3% by using
the <code>mondriaan</code> function in MATLAB
Marco van Oort's avatar
Marco van Oort committed
<li><code>[I, s] = mondriaan(A, 30, 0.03);</code></li>
Marco van Oort's avatar
Marco van Oort committed
where <code>I</code> is the same matrix as <code>A</code>, only with the real values
of all the matrix nonzeroes set to the index of the processor to which
Marco van Oort's avatar
Marco van Oort committed
the nonzero was assigned, and <code>s</code> contains partitioning information.
Full output can be generated with
</p>
<ul>
Marco van Oort's avatar
Marco van Oort committed
<li><code>[I, s, p, q, r, c, rh, ch, B, u, v] = mondriaan(A, 30, 0.03, 2);</code></li>
Marco van Oort's avatar
Marco van Oort committed
where the last parameter (<code>2</code>) is the desired permutation method (see below).
Here, p and q are permutation vectors, r and c are row-boundaries and column-boundaries
corresponding to the ordering's block structure, rh and ch store the separator hierarchy 
information, the matrix B stores the reordered matrix PAQ (in MATLAB terminology:
B=A(p,q)) and finally u and v contain the indices of the processors to which the vector 
components are assigned (for parallel multiplication of <i>u = A*v</i>).
See the <a href="USERS_GUIDE.html">User's Guide</a> for full details on these output 
vectors and matrices. For particulars on the boundary and hierarchy functions, jump to
Marco van Oort's avatar
Marco van Oort committed
the appropriate section <a href="USERS_GUIDE.html#SBDoutput">here</a>.
</p>
<table border="1">
<tr><td><b>Value</b><td><b>Ordering</b></td></tr>
<tr><td>0</td><td>None (default value)</td></tr>
<tr><td>1</td><td>reverse BBD (reverse Bordered Block Diagonal)</td></tr>
<tr><td>2</td><td>SBD (Separated Block Diagonal)</td></tr>
<tr><td>3</td><td>BBD (Bordered Block Diagonal)</td></tr>
</table>

<h3>Exploiting symmetry</h3>

<p>
The MATLAB interface additionally has an option to make use of any symmetry properties
of the input matrix A. This is done by setting a fifth parameter, such that the full
call becomes:
</p>
<ul>
Marco van Oort's avatar
Marco van Oort committed
<li><code>[I, s, p, q, r, c, rh, ch, B, u, v] = mondriaan(A, 30, 0.03, 2, symm);</code></li>
</ul>
<p>where symm is 0 by default (if the parameter is not given), and indicates A is
Marco van Oort's avatar
Marco van Oort committed
not symmetric. If <code>symm</code> takes a value 1 or 2, A is assumed symmetric
and <em>only the lower triangular part of A is passed through to Mondriaan</em>. This
is exactly the same as using the regular (terminal-based) Mondriaan application on a
symmetric matrix with the options SymmetricMatrix_UseSingleEntry set to <b>yes</b> and
SymmetricMatrix_SingleEntryType set to <b>lower</b>. If these options are not set in 
the Mondriaan.defaults file, they will be forced. The matrices I and B will still 
correspond to the full matrix A. Any SplitStrategy can still be used, and is 
taken as usual from the Mondriaan.defaults file. Recommended is to use the finegrain 
or symmetric finegrain strategies. Others will work, but may not minimise the 
communication volume during parallel sparse matrix-vector multiplication when 
considering the full matrix A.</p>
Marco van Oort's avatar
Marco van Oort committed
<p>Setting <code>symm</code> to 2 will indicate the matrix is structurally symmetric,
but as said before, still only the lower triangular part of A is passed through to
Mondriaan. This makes no difference for any of the output parameters, except for B,
Marco van Oort's avatar
Marco van Oort committed
which would, for <code>symm</code>=1, return an incorrect full matrix PAQ as the full
reordered matrix is inferred only from the lower triangular part. Setting <code>symm</code>
to 2 prevents this by automatically postprocessing B by rebuilding PAQ using the 
output parameters p and q.</p>
Marco van Oort's avatar
Marco van Oort committed
<p>Note that setting <code>symm</code> equal to 1 or 2 yields symmetric permutations 
(B=PAP<sup>T</sup>). Also note that it is not checked whether the input matrix really 
is symmetric, and as such unsymmetric matrices can also be passed through this method.
This probably does not yield any meaningful results.</p>

<h3>Example uses</h3>
<p>We present two small examples of using Matlab in conjunction with Mondriaan; the
first will be on speeding up the sequential sparse matrix-vector multiply, the
second will illustrate speeding up the sequential sparse LU decomposition.
Marco van Oort's avatar
Marco van Oort committed
Assumed is that the working directory is <code>Mondriaan4/tools</code>. Also available
should be:
<ul>
<li>the tbdlinux matrix (available through Rob Bisseling's
<a href="http://www.staff.science.uu.nl/~bisse101/Mondriaan/">webpage</a>),</li>
<li>the west0497 matrix (available through the
<a href="http://www.cise.ufl.edu/research/sparse/matrices/">
Florida Sparse Matrix Collection</a>).</li>
</ul>
<p>
In both examples, the experiment is executed 1000 times to limit the effects of
system jitter.
</p>
<h5>1 (cache-oblivious SpMV 
[<a href="#cite1">1</a>], 
[<a href="#cite2">2</a>]):</h5>
<p>
Marco van Oort's avatar
Marco van Oort committed
<code>
&gt;&gt; A=mmread('tbdlinux.mtx');<br>
&gt;&gt; [I, s, p, q, r, c, rh, ch, B, u, v] = mondriaan(A,50,0.1,2);<br>
&gt;&gt; x=rand(size(A,2),1);<br>
&gt;&gt; tic, for i=1:1000 A*x; end, toc<br>
Elapsed time is 24.707203 seconds.<br>
&gt;&gt; tic, z=x(q), for i=1:1000 B*z; end, toc<br>
Marco van Oort's avatar
Marco van Oort committed
Elapsed time is 19.786526 seconds.</code>
</p>
<p>
Using Mondriaan to transform the tbdlinux matrix into SBD form thus yields a modest 20 percent
speed increase. This is expected to be higher for matrices for which the input and output 
vectors no longer fit into the caches closer to main memory. This method of reordering for 
sparse matrix-vector multiplication also yields much better results when used with optimised
datastructures, such as <a href="http://bebop.cs.berkeley.edu/oski/">OSKI</a>, Incremental 
CRS, or dedicated block-wise structures; see
[<a href="#cite2">2</a>]
for details.</p>
<h5>2 (reducing fill-in during LU decomposition
[<a href="#cite3">3</a>], 
[<a href="#cite4">4</a>]):</h5>
<p>
Marco van Oort's avatar
Marco van Oort committed
<code>
&gt;&gt; A=mmread('west0497.mtx');<br>
&gt;&gt; [I, s, p, q, r, c, rh, ch, B, u, v] = mondriaan(A,10,0.1,3);<br>
&gt;&gt; tic, for i=1:1000 [L,U,lu_P] = lu(A); end, toc<br>
Elapsed time is 3.659008 seconds.<br>
&gt;&gt; nnz(L+U)<br>
<br>
ans =<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;13818<br>
<br>
&gt;&gt; tic, for i=1:1000 [L,U,lu_P] = lu(B); end, toc<br>
Elapsed time is 1.943670 seconds.<br>
&gt;&gt; nnz(L+U)<br>
<br>
ans =<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4647<br>
<br>
Marco van Oort's avatar
Marco van Oort committed
</code>
</p>
<p>Here the use of Mondriaan with BBD ordering lets the stock MATLAB 
LU algorithm run almost a factor 2 faster, and reduces the fill-in
with almost a factor 3. Note that this is not the UMFPACK version of
the LU algorithm, which employs its own reordering techniques
Marco van Oort's avatar
Marco van Oort committed
(amongst others); see <code>help lu</code> within MATLAB.</p>


<h3>Visualisation</h3>
<p>
Marco van Oort's avatar
Marco van Oort committed
We can also directly visualise the partitioning process by using <code>mondriaanplot</code>
in the following fashion:
</p>
<ul>
Marco van Oort's avatar
Marco van Oort committed
<li><code>mondriaanplot(A, 30, 0.03, 2);</code></li>
</ul>
<p>
This concludes this small tutorial.
Marco van Oort's avatar
Marco van Oort committed
More information is available through issuing <code>help mondriaan</code> from within MATLAB.
Marco van Oort's avatar
Marco van Oort committed
<p>
Apart from Mondriaan itself, also MondriaanOpt is available in MATLAB through the MatlabMondriaanOpt MEX routine.
Example matlab functions are given in mondriaanOpt.m and mondriaanOptPlot.m.
The interface of mondriaanOpt is as follows:
Marco van Oort's avatar
Marco van Oort committed
</p>
Marco van Oort's avatar
Marco van Oort committed
<li><code>[I, s] = mondriaanOpt(A, Imbalance, Volume)</code></li>
Marco van Oort's avatar
Marco van Oort committed
<p>
Here, <code>A</code> is the sparse matrix to be partitioned, <code>Imbalance</code> is the maximum allowed load imbalance,
<code>Volume</code> is the initial upper bound on the volume, <code>I</code> contains the partitioning information and
<code>s</code> contains statistics about the run.
For more information, type <code>help mondriaanOpt</code> or <code>help mondriaanOptPlot</code> in MATLAB.
</p>

<h3>References</h3>
<p>
Marco van Oort's avatar
Marco van Oort committed
[<a id="cite1" href="http://www.staff.science.uu.nl/~bisse101/Mondriaan/yzelman09.pdf">1</a>]
<em>Cache-oblivious sparse matrix-vector multiplication by using sparse matrix partitioning methods</em>,
A. N. Yzelman and Rob H. Bisseling, SIAM Journal of Scientific Computation, Vol. 31, Issue 4, pp. 3128-3154 (2009).<br>
Marco van Oort's avatar
Marco van Oort committed
[<a id="cite2" href="http://www.sciencedirect.com/science/article/pii/S0167819111001062">2</a>]
<em>Two-dimensional cache-oblivious sparse matrix-vector multiplication</em>,
A. N. Yzelman and Rob H. Bisseling, Parallel Computing, Vol. 37, Issue 12, pp. 806-819 (2011).<br>
Marco van Oort's avatar
Marco van Oort committed
[<a id="cite3" href="http://www.cerfacs.fr/files/cerfacs_algo/conferences/PastWorkshops/CSC05/11_Catalyurek_Aykanat.pdf">3</a>]
<em>Hypergraph-partitioning-based sparse matrix ordering</em>,
&Uuml;mit V. &Ccedil;ataly&uuml;rek and C. Aykanat, Second International Workshop on Combinatorial Scientic Computing, CERFACS, 2005.<br>
Marco van Oort's avatar
Marco van Oort committed
[<a id="cite4" href="http://www.sandia.gov/~egboman/papers/HUND.pdf">4</a>]
<em>Hypergraph-based Unsymmetric Nested Dissection Ordering for Sparse LU Factorization</em>,
L. Grigori, E. G. Boman, S. Donfack, and T. A. Davis, SIAM Journal of Scientific Computation,
Vol. 32, Issue 6, pp. 3426-3446 (2010).<br>
</p>
<hr>
<p>
July 27, 2010 by Bas Fagginger Auer,<br>
December 10, 2010 by A. N. Yzelman,<br>
March 27, 2012 by Bas Fagginger Auer,<br>
August 29, 2013 by Rob Bisseling and Bas Fagginger Auer,<br>
November 3, 2016 by Marco van Oort,<br>
September 7, 2017 by Marco van Oort.<br><br>
To <a href="http://www.staff.science.uu.nl/~bisse101/Mondriaan">
Home page Mondriaan package</a>.</p>

<p>
<a href="http://validator.w3.org/check?uri=referer">
<img style="border:0;width:88px;height:31px" src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Strict">
</a>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img style="border:0;width:88px;height:31px" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!">
</a>
</p>
<hr>