{"id":3667,"date":"2011-09-11T16:48:30","date_gmt":"2011-09-11T15:48:30","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=3667"},"modified":"2011-11-04T11:56:40","modified_gmt":"2011-11-04T10:56:40","slug":"matlab-functions-with-built-in-parallel-computing-toolbox-support","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=3667","title":{"rendered":"MATLAB functions with built in parallel computing toolbox support"},"content":{"rendered":"<p>So, you&#8217;re the proud owner of a new license for MATLAB&#8217;s <a href=\"http:\/\/www.mathworks.co.uk\/products\/parallel-computing\/index.html\">parallel computing toolbox<\/a> (PCT) and you are wondering how to get some bang for your buck as quickly as possible.\u00a0 Sure, you are going to learn about constructs such as <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/distcomp\/parfor.html\">parfor<\/a> and <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/distcomp\/spmd.html\">spmd<\/a> but that takes time and effort.\u00a0 Wouldn&#8217;t it be nice if you could speed up some of your MATLAB code simply by saying <strong>&#8216;Turn parallelisation on&#8217;<\/strong>?<\/p>\n<p>It turns out that The Mathworks have been adding support for their parallel computing toolbox all over the place and all you have to do is switch it on (Assuming that you actually have the parallel computing toolbox of course).\u00a0 For example say you had the following call to fmincon (part of the optimisation toolbox) in your code<\/p>\n<pre>[x, fval] = fmincon(@objfun, x0, [], [], [], [], [], [], @confun,opts)<\/pre>\n<p>To turn on parallelisation across 2 cores just do<\/p>\n<pre>matlabpool 2;\r\nopts = optimset('fmincon');\r\nopts = optimset('UseParallel','always');\r\n[x, fval] = fmincon(@objfun, x0, [], [], [], [], [], [], @confun,opts);<\/pre>\n<p>That wasn&#8217;t so hard was it?\u00a0 The speedup (if any) completely depends upon your particular optimization problem.<\/p>\n<p><strong>Why isn&#8217;t parallelisation turned on by default?<\/strong><br \/>\nThe next question that might occur to you is <strong><em>&#8216;Why doesn&#8217;t The Mathworks just turn parallelisation on by default?&#8217;<\/em><\/strong> After all, although the above modification is straightforward, it does require you to know that this particular function supports parallel execution via the PCT.  If you didn&#8217;t think to check then your code would be doomed to serial execution forever.<\/p>\n<p>The simple answer to this question is &#8216;<strong>Sometimes the parallel version is slower<\/strong>&#8216;.  Take this serial code for example.<\/p>\n<pre>objfun = @(x)exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);\r\nconfun = @(x) deal( [1.5+x(1)*x(2)-x(1)-x(2); -x(1)*x(2)-10], [] );\r\ntic;\r\n[x, fval] = fmincon(objfun, x0, [], [], [], [], [], [], confun);\r\ntoc<\/pre>\n<p>On the machine I am currently sat at (quad core running MATLAB 2011a on Linux) this typically takes around 0.032 seconds to solve.  With a problem that trivial my gut feeling is that we are not going to get much out of switching to parallel mode.<\/p>\n<pre>objfun = @(x)exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);\r\nconfun = @(x) deal( [1.5+x(1)*x(2)-x(1)-x(2); -x(1)*x(2)-10],[] );\r\n\r\n%only do this next line once.  It opens two MATLAB workers\r\nmatlabpool 2;\r\n\r\nopts = optimset('fmincon');\r\nopts = optimset('UseParallel','always');\r\n\r\ntic;\r\n[x, fval] = fmincon(objfun, x0, [], [], [], [], [], [], confun,opts);\r\ntoc<\/pre>\n<p>Sure enough, this increases execution time dramatically to an average of 0.23 seconds on my machine.\u00a0 There is always a computational overhead that needs paying when you go parallel and if your problem is too trivial then this overhead costs more than the calculation itself.<\/p>\n<p><strong>So which functions support the Parallel Computing Toolbox?<\/strong><\/p>\n<p>I wanted a web-page that listed<strong> all<\/strong> functions that gain benefit from the Parallel Computing Toolbox but couldn&#8217;t find one.\u00a0 I found some documentation on specific toolboxes such as <a href=\"http:\/\/www.mathworks.com\/help\/toolbox\/stats\/bslu7ot.html\">Parallel Statistics<\/a> but nothing that covered all of MATLAB in one place.\u00a0 Here is my attempt at producing such a document.\u00a0 Feel free to <a href=\"https:\/\/www.walkingrandomly.com\/?page_id=2055\">contact me<\/a> if I have missed anything out.<\/p>\n<p>This covers MATLAB 2011b and is almost certainly incomplete.  I&#8217;ve only covered toolboxes that I have access to and so some are missing.\u00a0 Please <a href=\"https:\/\/www.walkingrandomly.com\/?page_id=2055\">contact me<\/a> if you have any extra information.<\/p>\n<p><strong>Bioinformatics Toolbox<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/bioinfo\/ref\/multialign.html\">multialign<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/bioinfo\/ref\/seqpdist.html\">seqpdist<\/a><\/li>\n<\/ul>\n<p><strong>Global Optimisation<\/strong><\/p>\n<ul>\n<li>Various solvers use the PCT.\u00a0 See <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/gads\/bsc7xh9-1.html\">this part of the MATLAB documentation<\/a> for details.<\/li>\n<\/ul>\n<p><strong>Image Processing<br \/>\n<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/images\/ref\/blockproc.html\">blockproc<\/a><\/li>\n<li>Note that many Image Processing functions run in parallel even without the parallel computing toolbox.\u00a0 See my article <a href=\"https:\/\/www.walkingrandomly.com\/?p=1894\">Which MATLAB functions are Multicore Aware<\/a>?<\/li>\n<\/ul>\n<p><strong>Optimisation Toolbox<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/optim\/ug\/fgoalattain.html\">fgoalattain<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/optim\/ug\/fmincon.html\">fmincon<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/optim\/ug\/fminimax.html\">fminimax<\/a><\/li>\n<\/ul>\n<p><strong>Simulink<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/simulink\/ug\/brsk2gr.html\">Running parallel simulations<\/a><\/li>\n<li>You can increase the speed of diagram updates for models containing large model reference hierarchies by building referenced models that are configured in Accelerator mode in parallel whenever conditions allow.\u00a0 This is <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/simulink\/ug\/bq__qnv-1.html\">covered in the documentation<\/a>.<\/li>\n<\/ul>\n<p><strong>Statistics Toolbox<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/bootstrp.html\">bootstrp<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/bootci.html\">bootci<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/cordexch.html\">cordexch<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/candexch.html\">candexch<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/classificationtree.crossval.html\">crossval<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/dcovary.html\">dcovary<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/daugment.html\">daugment<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/treebagger.growtrees.html\">growTrees<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/jackknife.html\">jackknife<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/lasso.html\">lasso<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/nnmf.html\">nnmf<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/plsregress.html\">plsregress<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/rowexch.html\">rowexch<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/sequentialfs.html\">sequentialfs<\/a><\/li>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/stats\/treebagger.html\">TreeBagger<\/a><\/li>\n<\/ul>\n<p><strong>Other articles about parallel computing in MATLAB from WalkingRandomly<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=1894\">Which MATLAB functions are multicore aware? <\/a>There are a ton of functions in MATLAB that take advantage of parallel processors automatically.\u00a0 No Parallel Computing Toolbox necessary.<\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=1795\">Parallel MATLAB with OpenMP mex files<\/a> Want to parallelize your own functions without purchasing the PCT?\u00a0 Not afraid to get your hands dirty with C?\u00a0 Perhaps this option is for you.<\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=3730\">MATLAB GPU\/CUDA Experiences and tutorials on my laptop<\/a> &#8211; A series of articles where I look a GPU computing with CUDA on MATLAB<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>So, you&#8217;re the proud owner of a new license for MATLAB&#8217;s parallel computing toolbox (PCT) and you are wondering how to get some bang for your buck as quickly as possible.\u00a0 Sure, you are going to learn about constructs such as parfor and spmd but that takes time and effort.\u00a0 Wouldn&#8217;t it be nice if [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[53,11,41,7],"tags":[],"class_list":["post-3667","post","type-post","status-publish","format-standard","hentry","category-making-matlab-faster","category-matlab","category-parallel-programming","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-X9","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/3667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3667"}],"version-history":[{"count":11,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/3667\/revisions"}],"predecessor-version":[{"id":3932,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/3667\/revisions\/3932"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}