{"id":166,"date":"2008-12-08T12:18:02","date_gmt":"2008-12-08T11:18:02","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=166"},"modified":"2008-12-08T12:18:29","modified_gmt":"2008-12-08T11:18:29","slug":"notes-on-mathematica-7-parallelize-on-ubuntu","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=166","title":{"rendered":"Notes on Mathematica 7 : Parallelize on Ubuntu"},"content":{"rendered":"<p>Mathematica 7 includes some very nice support for parallel computing and one of the easiest commands to use is <strong>Parallelize<\/strong>.\u00a0 If you have a multi-core computer (and most of them are these days) then, in some cases, you can get a speedup of a factor of 2 or more simply by wrapping your code with the <strong>Parallelize <\/strong>command.\u00a0 I&#8217;ll stress that it&#8217;s not <strong>always<\/strong> this easy but, when it is, it&#8217;s very nice.<\/p>\n<p>To start getting my head around the new command I looked at the following small piece of code which is included in the Mathematica 7 documentation.<br \/>\n<code><br \/>\ndata = Table[ PrimeQ[n! + 1], {n, 400, 550}];<br \/>\n<\/code><br \/>\nThis code calculates numbers of the form n!+1 for n between 400 and 550 and tests to see if they are prime or not.  Let&#8217;s see how long it takes on my dual core 2Ghz Dell XPS M1330:<br \/>\n<code><br \/>\ndata = Table[ PrimeQ[n! + 1], {n, 400, 550}]; \/\/ AbsoluteTiming<\/code><\/p>\n<p>Out[1]={14.092809, Null}<\/p>\n<p>We see from the above that it took just over 14 seconds before we apply any parallelism.  Let&#8217;s see how much improvement we get by using the Parallelize command:<br \/>\n<code><br \/>\ndata = Parallelize[Table[ PrimeQ[n! + 1], {n, 400, 550}]]; \/\/ AbsoluteTiming<br \/>\nOut[2]= {21.124572, Null}<br \/>\n<\/code><br \/>\nOh dear, something has gone drastically wrong! The parallel calculation is taking 7 seconds (or 50%) longer than the serial calculation.  Before writing a bug report to Wolfram, I thought I would dig around a little to try and work out what was going on.<\/p>\n<p>First things first, let&#8217;s take a look at how hard Mathematica is pushing the processor in both cases.\u00a0 I can add a little CPU monitoring applet to my GNOME panel by right clicking on it and selecting <strong>add to Panel<\/strong>.\u00a0 From the resulting list I choose <strong>CPU Frequency Scaling Monitor.<\/strong><\/p>\n<p>When I initially did this, the applet informed me that my CPU was running at 800 Mhz &#8211; way below its maximum speed of 2Ghz.\u00a0 When I click on this applet I get a range of settings that could be applied to my CPU such as <strong>On demand, Performance <\/strong>and <strong>Powersave <\/strong>along with specific speed settings from 800Mhz to 2 Ghz. The one selected was <strong>on demand<\/strong>.<\/p>\n<p>Turning back to Mathematica I run the sequential code again<\/p>\n<p><code>data = Table[ PrimeQ[n! + 1], {n, 400, 550}]; \/\/ AbsoluteTiming<br \/>\nOut[3]={14.117028, Null}<br \/>\n<\/code><\/p>\n<p>According to the CPU Monitor, my computer was running at 2Ghz (its top speed) during the entire calculation.  However, when I ran the <strong>Parallelize<\/strong> version, the processor speed remained at 800Mhz the whole time.  So, the calculation may well have been running in parallel but each of the 2 cores was only running at 40% of top speed.\u00a0 The problem seems to lie with Mathematica&#8217;s interaction with the <strong>on demand<\/strong> setting of the processor manager.<\/p>\n<p>By clicking on the GNOME CPU monitor, I can fix the processor to run at 2Ghz at all times &#8211; no fancy processor management here &#8211; just the fastest speed my computer can manage.\u00a0 Let&#8217;s see what the results of the two versions of the code are when I do this.<br \/>\n<code><br \/>\ndata = Table[ PrimeQ[n! + 1], {n, 400, 550}]; \/\/ AbsoluteTiming<\/code><\/p>\n<p>Out[4]={14.062377, Null}<\/p>\n<p>data = Parallelize[Table[ PrimeQ[n! + 1], {n, 400, 550}]]; \/\/ AbsoluteTiming<\/p>\n<p>Out[5]={7.635570, Null}<\/p>\n<p>That&#8217;s more like it!  The parallel version is almost twice as fast as the serial version &#8211; just as we would expect on a dual core system.  So, the moral of the story is to ensure that you don&#8217;t have your system set to modify it&#8217;s processor speed <strong>on demand<\/strong> when trying to make use of the new Parallel features of Mathematica 7.<\/p>\n<p>For the record I am running GNOME 2.24.1 on Ubuntu 8.10 with Mathematica 7.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mathematica 7 includes some very nice support for parallel computing and one of the easiest commands to use is Parallelize.\u00a0 If you have a multi-core computer (and most of them are these days) then, in some cases, you can get a speedup of a factor of 2 or more simply by wrapping your code with [&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":[4,8],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-math-software","category-mathematica"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-2G","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/166","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=166"}],"version-history":[{"count":7,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":435,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions\/435"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}