Notes on Mathematica 7 : Parallelize on Ubuntu
Mathematica 7 includes some very nice support for parallel computing and one of the easiest commands to use is Parallelize. 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 Parallelize command. I’ll stress that it’s not always this easy but, when it is, it’s very nice.
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.
data = Table[ PrimeQ[n! + 1], {n, 400, 550}];
This 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’s see how long it takes on my dual core 2Ghz Dell XPS M1330:
data = Table[ PrimeQ[n! + 1], {n, 400, 550}]; // AbsoluteTiming
Out[1]={14.092809, Null}
We see from the above that it took just over 14 seconds before we apply any parallelism. Let’s see how much improvement we get by using the Parallelize command:
data = Parallelize[Table[ PrimeQ[n! + 1], {n, 400, 550}]]; // AbsoluteTiming
Out[2]= {21.124572, Null}
Oh 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.
First things first, let’s take a look at how hard Mathematica is pushing the processor in both cases. I can add a little CPU monitoring applet to my GNOME panel by right clicking on it and selecting add to Panel. From the resulting list I choose CPU Frequency Scaling Monitor.
When I initially did this, the applet informed me that my CPU was running at 800 Mhz – way below its maximum speed of 2Ghz. When I click on this applet I get a range of settings that could be applied to my CPU such as On demand, Performance and Powersave along with specific speed settings from 800Mhz to 2 Ghz. The one selected was on demand.
Turning back to Mathematica I run the sequential code again
data = Table[ PrimeQ[n! + 1], {n, 400, 550}]; // AbsoluteTiming
Out[3]={14.117028, Null}
According to the CPU Monitor, my computer was running at 2Ghz (its top speed) during the entire calculation. However, when I ran the Parallelize 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. The problem seems to lie with Mathematica’s interaction with the on demand setting of the processor manager.
By clicking on the GNOME CPU monitor, I can fix the processor to run at 2Ghz at all times – no fancy processor management here – just the fastest speed my computer can manage. Let’s see what the results of the two versions of the code are when I do this.
data = Table[ PrimeQ[n! + 1], {n, 400, 550}]; // AbsoluteTiming
Out[4]={14.062377, Null}
data = Parallelize[Table[ PrimeQ[n! + 1], {n, 400, 550}]]; // AbsoluteTiming
Out[5]={7.635570, Null}
That’s more like it! The parallel version is almost twice as fast as the serial version – just as we would expect on a dual core system. So, the moral of the story is to ensure that you don’t have your system set to modify it’s processor speed on demand when trying to make use of the new Parallel features of Mathematica 7.
For the record I am running GNOME 2.24.1 on Ubuntu 8.10 with Mathematica 7.
I made your example in my Windows Vista machine
I did your example in my Vista machine:
In[1]:= data = Table[PrimeQ[n! + 1], {n, 400, 550}]; // AbsoluteTiming
Out[1]= {15.6770000, Null}
In[3]:= data =
Parallelize[Table[PrimeQ[n! + 1], {n, 400, 550}]]; // AbsoluteTiming
Out[3]= {10.7520000, Null}
In[44]:= data = Table[PrimeQ[n! + 1], {n, 400, 550}]; // AbsoluteTiming
data = Parallelize[Table[PrimeQ[n! + 1], {n, 400, 550}]]; // AbsoluteTiming
Out[44]= {4.169463, Null}
Out[45]= {0.642973, Null}
On Mac Pro Early 2008 (8 core 2.8GHz) and Mathematica 7
Hi Spitz
That’s awesome! I have an 8 core Mac in the office but haven’t gotten around to trying Mathematica 7 on it yet – looks like it will be well worth my while!
Best Wishes,
Mike
Try unchecking the low priority box. See http://somuchtodosolittletime.tumblr.com/
i want to know how to write Brillouin zones(numeric solution)in mathematica7