Parallel Mathematica calculations

Faster results through multiple machines or cores

Intuitive Language

Easy to make the transition to parallel Mathematica commands

Grid[{{"Regular Mathematica command:","Parallel equivalent command:"},{HoldForm[Do[]],HoldForm[ParallelDo[]]}, {HoldForm[Table[]],HoldForm[ParallelTable[]]}, {HoldForm[Sum[]],HoldForm[ParallelSum[]]}},Frame->All, Spacings->{4,2}]

First Parallel Calculation

Simple command to parallelize Mathematica statements

Style[Text[Row[{"Parallelize[ ", , " ] "}]], Large]

Find Mersenne primes, primes of the form (2^n)-1:

MersennePrimeQ[n_Integer]:=PrimeQ[(2^n)-1]

This function can be used to test specific cases, or to list the integers between 1 and 5000 that are Mersenne primes:

MersennePrimeQ[45]

Select[Range[5000], MersennePrimeQ]

Parallelize can be wrapped around any calculation, Mathematica will try to parallelize:

DistributeDefinitions[MersennePrimeQ]

Parallelize[Select[Range[5000], MersennePrimeQ]]

AbsoluteTiming can be used to show calculation times:

AbsoluteTiming[Select[Range[5000], MersennePrimeQ]]
AbsoluteTiming[Parallelize[Select[Range[5000], MersennePrimeQ]]]

Sending Data to Different Cores

GraphPlot[{Style["Calculation[Data1]", Large]->Style["Computer Core 1", Large], Style["Calculation[Data2]", Large]->Style["Computer Core 1", Large], Style["Calculation[Data3]", Large]->Style["Computer Core 1", Large], Style["Computer Core 1", Large]->Style["Serial Result", Large]}, VertexLabeling->True,
DirectedEdges->True]

Becomes:

GraphPlot[{Style["Calculation[Data1]", Large]->Style["Computer Core 1", Large], Style["Calculation[Data2]", Large]->Style["Computer Core 2", Large], Style["Calculation[Data3]", Large]->Style["Computer Core 3", Large], Style["Computer Core 1", Large]->Style["Parallel Result", Large], Style["Computer Core 2", Large]->Style["Parallel Result", Large], Style["Computer Core 3", Large]->Style["Parallel Result", Large]}, VertexLabeling->True,
DirectedEdges->True]

One-Dimensional Random Walk Simulation:

RandomWalk[steps_]:=Accumulate[RandomInteger[{-1,1},{steps}]]

ListPlot[RandomWalk[5000]]

Explore how far the random walk strays from the origin in absolute distance:

RWMeanSerial[steps_,trials_]:=Mean[Table[Abs[N[Last[RandomWalk[steps]]]],{trials}]]

This calculation can be easily run in parallel:

DistributeDefinitions[{RandomWalk}]
RWMeanParallel[steps_,trials_] := Mean[ParallelTable[Abs[N[Last[RandomWalk[steps]]]],{trials}]]

RWMeanSerial[5000,50000]//AbsoluteTiming

RWMeanParallel[5000,50000] // AbsoluteTiming

Larger real-world example

Tsunami Simulation

Equation definitions, shallow water equations and ocean floor

swe={(∂_t)h[λ,ϕ,t]+(1/(R Cos[ϕ]))((∂_λ)(u[λ,ϕ,t]  (h[λ, ϕ, t]   - b[λ, ϕ] ))+(∂_ϕ)(v[λ, ϕ, t]  (h[λ, ϕ, t]   - b[λ, ϕ] )Cos[ϕ]))==0,(((∂_ϕ)u[λ, ϕ, t]  v[λ, ϕ, t] )/R)+((g (∂_λ)h[λ, ϕ, t] )/(R Cos[ϕ]))+(∂_t)u[λ, ϕ, t]+((u[λ, ϕ, t]  (∂_λ)u[λ, ϕ, t] )/(R Cos[ϕ]))==f v[λ, ϕ, t] ,
(((∂_ϕ)v[λ, ϕ, t]  v[λ, ϕ, t] )/R)+((g (∂_ϕ)h[λ, ϕ, t] )/R)+(∂_t)v[λ, ϕ, t] +((u[λ, ϕ, t]  (∂_λ)v[λ, ϕ, t] )/(R Cos[ϕ]))==-f u[λ, ϕ, t] };
Seamount[s_, w_, {λ_, ϕ_}][l_, p_] := s Exp[-w ((l-λ)^2 + (p-ϕ)^2)];FaultDisplacement[p1_, p2_][p_?(VectorQ[#, NumberQ]&)] :=
If[p1 == p2,Norm[p - p1],
 Module[{pp1,pp2, p12},
pp1 = p - p1;
pp2 = p - p2;
p12 = (p1 - p2)/Norm[p1 - p2];
If[Sign[p12.pp1] != Sign[p12.pp2],
Norm[pp1 - pp1.p12 p12],
Min[Norm[pp1], Norm[pp2]]]]];
(Subscript[λ, min]) =-15 ° ; (Subscript[λ, max])= 15 °;(Subscript[λ, e]) = 0;(Subscript[λ, i])= 2.5 °;
(Subscript[ϕ, min])= -35 °;(Subscript[ϕ, max])= 5 °;(Subscript[ϕ, 1])= -10 °;(Subscript[ϕ, 2])= -20 °; (Subscript[ϕ, i ])= -25 °;
nxy = 50;
b[λ_, ϕ_] := -4000 + Seamount[3500, 1000, {-2.5 °, -25 °}][λ, ϕ]+ Seamount[3000, 1000, {-5 °, -23 °}][λ, ϕ] +  Seamount[3000, 1000, {-7.5 °, -27 °}][λ, ϕ]+  Seamount[3000, 1000, {5°, -24°}][λ, ϕ]+Seamount[3000, 1000, {5°, -21°}][λ, ϕ]+Seamount[3000, 1000, {5°, -18°}][λ, ϕ]+Seamount[3000, 1000, {5°, -15°}][λ, ϕ]+Seamount[3000, 1000, {5°, -12°}][λ, ϕ]+
Seamount[3750, 1000, {-2°, -18°}][λ, ϕ];

Rendering of ocean floor

c=Plot3D[b[λ,ϕ]/2000, {λ, (Subscript[λ, min]), (Subscript[λ, max])}, {ϕ, (Subscript[ϕ, min]), (Subscript[ϕ, max])},PlotRange->All, Mesh->None,ColorFunction->"GreenBrownTerrain"]

Tsunami simulation, modeling waves

Solving the PDE's:

AbsoluteTiming[T = 2 3600;Block[{R = 6378000, ω = ((2 π)/(24 3600)), f= 2ω Sin[ϕ], g = 9.8, s = .95},
sol = First[h /. NDSolve[{swe, h[λ, ϕ, 0] == Exp[-2000 ((FaultDisplacement[{(Subscript[λ, e]),(Subscript[ϕ, 1])},{(Subscript[λ, e]),(Subscript[ϕ, 2])}][{λ,ϕ}])^2)], u[λ, ϕ, 0] == 0, v[λ, ϕ, 0] == 0, h[λ, (Subscript[ϕ, min]), t] ==  h[λ, (Subscript[ϕ, max]), t], h[(Subscript[λ, min]), ϕ, t] ==  h[ (Subscript[λ, max]), ϕ, t]},h, {λ, (Subscript[λ, min]),  (Subscript[λ, max])}, {ϕ,(Subscript[ϕ, min]), (Subscript[ϕ, max])}, {t,0,T},DependentVariables->{h,u,v}, Method->{"MethodOfLines","SpatialDiscretization"->{"TensorProductGrid","MaxPoints"->nxy, "MinPoints"->nxy, "DifferenceOrder"->"Pseudospectral"}}]]]]

Visualization of one time step

a=Plot3D[sol[λ, ϕ, 360*5], {λ, (Subscript[λ, min]), (Subscript[λ, max])}, {ϕ, (Subscript[ϕ, min]), (Subscript[ϕ, max])}, PlotRange->All, PlotPoints->nxy, Mesh->None,MaxRecursion->2,PlotStyle->{RGBColor[.8,.8,1],Specularity[White,100]},
Boxed->False,Axes->None,Background->White]

Rendering animation in parallel

(*serial rendering*)
ani=Table[Show[Plot3D[sol[λ, ϕ, t], {λ, (Subscript[λ, min]), (Subscript[λ, max])}, {ϕ, (Subscript[ϕ, min]), (Subscript[ϕ, max])}, PlotRange->{-1,1}, PlotPoints->nxy, Mesh->None,MaxRecursion->1,PlotStyle->{RGBColor[.8,.8,1],Specularity[White,100],Opacity[0.6]},
Boxed->False,Axes->None,Background->White],c,PlotRange->{-2,1},ViewPoint->{-Pi,Pi/2,1},Background->Black,ImageSize->Large],{t,0,T,360}];//AbsoluteTiming
(*copy definitions to worker kernels*)
DistributeDefinitions[{sol,nxy,T, c,Subscript}]
(*parallel rendering*)
ani=ParallelTable[Show[Plot3D[sol[λ, ϕ, t], {λ, (Subscript[λ, min]), (Subscript[λ, max])}, {ϕ, (Subscript[ϕ, min]), (Subscript[ϕ, max])}, PlotRange->{-1,1}, PlotPoints->nxy, Mesh->None,MaxRecursion->1,PlotStyle->{RGBColor[.8,.8,1],Specularity[White,100],Opacity[0.6]},
Boxed->False,Axes->None,Background->White],c,PlotRange->{-2,1},ViewPoint->{-Pi,Pi/2,1},Background->Black,ImageSize->Large],{t,0,T,360}];//AbsoluteTiming

Final animation:

ListAnimate[ani]

Next steps to Parallelize projects

Explore full range of parallel commands:

Version 7 Documentation

Register for presentation with more field-specific examples:

S17 online seminar

Review developer's thoughts on creation of tsunami example:

Tsunami blog entry

Hands-on training with Wolfram Education Group

M225: Parallel Computing with Mathematica

Licensing for any hardware configuration

Dedicated Wolfram organizational contact can outline best licensing options

•  gridMathematica Server for large-scale clusters (16 - 500+ nodes)

•  gridMathematica Local for multi-core machines (8 - 16 cores)

•  Wolfram Lightweight Grid Manager for easy communication between products

•  Network Mathematica and Wolfram Workbench for development

More information:

Kelvin Mischo
	kelvinm@wolfram.com

•  Web:  Request form