Mathematica Tech Talk About Wolfram

Transcription

Mathematica Tech Talk About Wolfram
Mathematica Tech Talk
Mathematica 8:
An Integrated Platform for Science & Engineering
Yu-Sung Chang, Ph.D.
Manager, Technical Communication & Strategy Group
Wolfram
Table of Contents
About Wolfram : An Integrated Platform for Science & Engineering
Wolfram|Alpha in Mathematica
Working with Mathematica 8: From Concepts to Applications
Parallel Computation with Mathematica
GPU Computing in Mathematica
Introducing MathModelica
About Wolfram
Wolfram Research + Wolfram|Alpha
» Founded by Stephen Wolfram in 1987
» One of the world’s most respected software companies
» Pioneers in computational science and the computational paradigm
» Mathematica products
» Wolfram|Alpha services
2
GIST.nb
Our Customers: Industry Recognition
What is Mathematica?
Compute, Develop, Deploy
with One Integrated System
Streamlined Workflow for R&D
GIST.nb
Mathematica Advantage
Unique Principles and Benefits:
» Innovative automation
Produce expert results without expertise
» Integrated all-in-one platform
Save time and money, and access greater innovative capabilities compared to standalone tools
» Hybrid numeric-symbolic methodology
Get more accurate results than from any other tool
» Multi-paradigm language
Compute as you think and save time
» Built-in knowledge
Access the world’s largest pool of up-to-date computable knowledge
» Document centric workflow
Document your work as you go, organize explanations, data, code, and outputs in a single file
Mathematica:Quick Tour
Integrated All-in-one Platform
» From development to deployment: Simplified workflow
3
4
GIST.nb
Integrated All-in-one Platform
» From Aeronautics to Optics: Industries
Integrated All-in-one Platform
» From business graphics to wavelets: Specialist functionality. Built-In.
» Business Graphics
» Control Systems
» Statistics
» Finance
» Image Processing
» Geographic Information Systems
» Wavelet Analysis
» GPU Programming
» High Performance Computing
Powerful Computation Engine
» Symbolic mathematics: Fundamentals of Mathematica
Solvea x2  b x  c  0, x
Solvea x4  b x  c  0, x

1
x10
1
x
TraditionalForm
1
x10  1
x
GIST.nb
Powerful Computation Engine
» Numerical computation: Any precision. Compute with confidence.
f
t
20 Expx
In[10]:=
x;
TabViewWithopts  Frame  True, PlotStyle  Thick,
ImageSize  Scaled1, AspectRatio  1  3, "Machine Precision" 
PlotSinf, t,  45,  30, WorkingPrecision  MachinePrecision, opts,
"High Precision"  PlotSinf, t, 45,  30, WorkingPrecision  200, opts,
LabelStyle  "Text", FrameMargins  30, ImageSize  Scaled.5
x

Powerful Computation Engine
» Numerical computation: Exact arithmetic matters.
4
5 1
5
4
5
NestListFunctionx, 5 x 1  x,
4
, 30
5
» Compare
Powerful Computation Engine
» Numerical computation: Hybrid symbolic-numeric methods
1
NIntegrate
Logx
Cos
x
Logx
1
Cos
Plot
x
x
x
, x, 0, 1
, x, 0, 1, PlotRange  15, Frame  True, ImageSize  Large
5
6
GIST.nb
Powerful Computation Engine
» Performance: Linear algebra benchmark
Powerful Computation Engine
» Performance: Parallel computing made easy
ParallelMapColorize, ColorFunction  "SunsetColors" &, 
,
,
,
,
,
,
,
,
,
,
» How it works
Powerful Computation Engine
» Integration and automation: Simulating a model
,
,
,
,
,

GIST.nb
Visualization
» Visualize everything: From functions to everyday data
Visualization
» Visualize adaptively: Quality with performance
Interactivity & Interfaces
» Manipulate: Instant magic
ManipulatePlotfk x, x, 0, 10, FillingStyle  c, Filling  0, ImageSize  200,
k, 1, 10, f, Sin, Cos, Tan, Sec, Sinc, c, Yellow
Interactivity & Interfaces
» Easy and automated: Direct, declarative GUIs
7
8
GIST.nb
Interactivity & Interfaces
» Platform independent: No extra work required
Interactivity & Interfaces
» Fully customizable: Application quality
Data Sources & Connectivity
» Access data in any format
» Excel format
» Data formats
» Multimedia formats
» Web standards (HTML, XML, etc.)
» Try it:
GIST.nb
Data Sources & Connectivity
» Connect to databases and services
» DatabaseLink
» J/Link
» Web services
» Dynamic library
Data Sources & Connectivity
» Access computable data
CountryData"Kenya", "GDP"
CountryData"Kenya", "Flag"
» Try this on Internet: “GDP changes of G8 countries, since 1970”
Google search »
» With Mathematica...
Development & Deployment
» Development options
» The most sophisticated programming language for scientific and technical computing
» Comprehensive Built-in IDE features
» Wolfram Workbench: Eclipse-based enterprise-class tool
Development & Deployment
» Deployment options
» Interactive documents [Documentation]
» Presentation & report generation
» Interactive application: Mathematica Player [Demonstrations]
» Web application: Mathematica Plug-in
» Web application: webMathematica
» Distributed computation: gridMathematica
» Code generation & compilation
9
10
GIST.nb
Development & Deployment
» Case study: Real-world develop & deploy
Comprehensive Resources & Support
GIST.nb
Wolfram:
The Company where
Computation meets Knowledge
Mathematica:
Unique Principles,
Unique Advantages
Mathematica:
» Unique principles, unique advantages
» Automation
» Integrated all-in-one platform
» Hybrid numeric/symbolic methodology
» Multi-paradigm language
» Built-in knowledge
» Document-centric workflow
Get started today!
» Suggestions
» Download trials
» Network license
» Technical demos
» Free webniars
» Try Wolfram|Alpha
» And more...
11
12
GIST.nb
Mathematica Tech Talk
Wolfram|Alpha in Mathematica
What is WolframAlpha?
æ WolframAlpha is NOT a search engine
æ It dynamically computes answers based on a
vast collection of built-in data and algorithms
æ Its knowledge is continually expanding:
q adding more curated data
q hooking up real-time feeds
q implementing new algorithms
q building new generalized grammar
to capture the natural language
æ It contains millions of lines of
Mathematica code
Image © Stephen Wolfram Blog æ Long-term goal is to make all systematic
knowledge immediately computable and
accessible to everyone
Wolfram|Alpha API
...access the Wolfram|Alpha platform from other applications
» A few examples of Wolfram|Alpha API usage on mobile iOS and Android platforms
Wolfram|Alpha Integration in Mathematica
...how does it work?
GIST.nb
13
! Requirement : Internet connection
¤ Four methods to access
»
produced by double equal (= =) at beginning of input
Performs a full Wolfram|Alpha query, producing fully formatted result similar to Wolfram|Alpha web page with
text, graphics, and even interactive elements
derivative of f of x
»
produced by a single equal (=) at beginning of input
Uses free-form linguistics to give a single result in native Mathematica syntax if possible. The result can be
expanded to the full Wolfram|Alpha output. For queries that do not have a Mathematica form,
also returns
the Wolfram|Alpha interpretation in addition to the result.
derivative of f of x
»
produced by ‚+= anywhere in the input
Embeds free-form input into a Mathematica expressions for further computation.
derivative of f of x
Ù
Dfx, x
Ú
Manipulate
,
f, Sin, "Derivative of", Sin, Cos, Tan, Csc, Sec  Magnify
» WolframAlpha… - Mathematica function
The function behind the whole Wolfram|Alpha integration including the three cases above; programmatically
gathers information from the Wolfram|Alpha API and provides the ability to choose specific outputs and formats
depending on its arguments.
Earthquakes in Japan:
Expand gamma function in series:
Molecular plot of chemical data:
Comparison of price history:
Double pendulum - interactive app from Wolfram|Alpha:
Fractal dragon curve - interactive app using Wolfram|Alpha function:
press double equal (= =) at beginning of input
Performs a full Wolfram|Alpha query, producing fully formatted result similar to Wolfram|Alpha web page with
text, graphics, and even interactive elements
hello
figure 8 knot
Solvex2 y2 
y2
x2
 x2 
1
x2
 x y3 
y3
x
x y
y
x
 2 y2  1  0, x
14
GIST.nb
plot x 2 y 2 +
y2
x
2
- x2 -
1
x
2
+ x y3 +
y3
x
-x y-
y
x
+ 2 y2 - 1
integrate 1/(1+x^3)
WolframAlpha"integrate 11x^3",
IncludePods  "IndefiniteIntegral", AppearanceElements  "Pods",
TimeConstraint  30, Automatic, Automatic, Automatic,
PodStates  "IndefiniteIntegral__Show steps"
plot tan^-1((2 x - 1)/Sqrt[3])/Sqrt[3]
sun activity
sundata  WolframAlpha"sun activity",
"SunspotsPartialTimeSeries:SpaceWeatherData", 1, "ComputableData";
DateListPlotsundata, AspectRatio  1  8, Joined  True, Filling  0
WaveletScalogramContinuousWaveletTransformsundataAll, 2
press single equal (=) at beginning of input
Uses free-form linguistics to give a single result in native Mathematica syntax if possible. The result can be
expanded to the full Wolfram|Alpha output. For queries that do not have a Mathematica form,
also returns
the Wolfram|Alpha interpretation in addition to the result.
figure 8 knot
knot 6 3
figure 8 knot braid
figure 8 knot braid image
population of england history
DateListLogPlot
integrate bessel j2
take derivative of %
simplify %
cellular automaton 3-color, range 2, totalistic code 151117
WolframAlpha"cellular automaton 3color, range 2, totalistic code 151117",
"RandomInitialConditions", 1, "Content"
cellular automaton 3-color, range 2, totalistic code 151117
ArrayPlotCellularAutomaton151 117, 3, 1, 2,
SeedRandom4234; RandomInteger2, 801, 300, All,
Frame  False, PixelConstrained  1, ColorFunction  "TemperatureMap"
press ‚+= anywhere in the input
Embeds free-form input into Mathematica expressions for further computation.
GIST.nb
15
solar system planets
solar system planets
Ù
AstronomicalData"Planet"
Ú
GridTranspose
,
solar system planets mass
Ù
AstronomicalData"Planet", "Mass"
Ú
PlotEvaluate  
, Frame  All
integrate spherical bessel j2
Ù
IntegrateSphericalBesselJ2, x, x
Ú
,
integrate spherical bessel j3
Ù
IntegrateSphericalBesselJ3, x, x
Ú
 .
x  a , a,  30, 30, Filling  0
complete graph n
Ù
CompleteGraphn
Ú
Manipulate
, n, 3, 15, 1
AnimateAppendYellow, SpecularityWhite, 20,
Opacity.5,  & 
cube 10-compound
Ù
PolyhedronData"CubeTenCompound"
Ú
,
Boxed  False, ViewPoint  Dynamic  Coss, Sins, 1, SphericalRegion  True,
s, 0, 2 Pi, AnimationRate  .1
WolframAlpha[…] - Mathematica function
The function behind the whole Wolfram|Alpha integration including the three cases above; programmatically
gathers information from the Wolfram|Alpha API and provides the ability to choose specific outputs and formats.
» WolframAlpha"query", " format"
gives the available data in the specified format:
“FullOutput” same as
“Result” same as
“InlineInput” same as
“MathematicaForms”
» WolframAlpha"query", podids, dataformats
returns one or more individual pieces of data:
“PodPlaintext”
16
GIST.nb
» More information
? WolframAlpha
Native Graphical Outputs
...and how to change their properties
show transparent red sphere and 2 blue cones
Graphics3DOpacity0.7, Orange, Sphere,
Green, ArrayTranslateCone, 2  1, 2, 0 & , 2, 0
double pendulum
l1
m1
image 
q1
;
l2
q2
m2
Headimage
Graphics
add red dashed gridlines and axes to image
add frame
caffeine 3D structure
image1 
; image2 
;
InputFormimage1
Manipulate
Show
image2 . Spherel_, _  Spherel, r,
image1, ImageSize  500
, r, 0, 170
Manipulate
Show
image2 .
RGBColorx_, y_, z_  RGBColorx, y, z, o, GrayLevelx_  GrayLevelx, o,
image1, ImageSize  500
, o, 0, 1
Clearimage, image1, image2
Tackling Applied Problems
GIST.nb
... and integrating built-in interactive tools into the workflow
“aMAZEing Image Processing in Mathematica”
- by Jon McLoone
;
image rotate 5 degrees counterclockwise
Input interpretation
ImageRotate, 5  Degree
image adjust
»
adjust
Input interpretation
ImageAdjust
binarize
Input interpretation
Binarize
delete small components
Input interpretation
DeleteSmallComponents
thinning
»
Input interpretation
Thinning
pruning
»
Input interpretation
Pruning
17
18
GIST.nb
blur
»
Input interpretation
Blur
add % to image img
Input interpretation
ImageAddimg, 
txt 
recognize text
Ù
TextRecognize
Ú
speak txt
»
Input interpretation
Speaktxt
count occurrences of "fish" in txt
Input interpretation
StringCounttxt, "fish"
take first 8 characters of txt
Input interpretation
StringTaketxt, 8
speak %
Input interpretation
Speak
;
recognize text
Input interpretation
TextRecognize
separate colors
Input interpretation
ColorSeparate
»
»
GIST.nb
take first element of %
Input interpretation
1
sharpen
»
Input interpretation
Sharpen
recognize text
Input interpretation
TextRecognize
speak %
Input interpretation
Speak
19
20
GIST.nb
Conclusion
...advantages of using Wolfram|Alpha integration
¤ First and foremost
¤ And much more...
» Learn Mathematica syntax by entering a query in English and then
viewing it as precise Mathematica commands.
» Easily specify plot frames, styles, gridlines, and more in natural
language.
» Perform many specialist operations in fields as diverse as image
processing and finance using free-form instructions.
» Easily refer to the results of previous computations in free-form
commands.
» Get results that can be used immediately in visualizations or
computations as native Mathematica or full Wolfram|Alpha-style
layout.
» Use WolframAlpha function to programmatically gather information
from the Wolfram|Alpha API; provides the ability to choose specific
outputs and formats.
» Save a trip to documentation
» Access a large variety of continually updated examples on the
Wolfram|Alpha website
GIST.nb
21
Mathematica Tech Talk
Functional Programming with Mathematica
Everything is an Expression
» Basic Structure
An expression consists of a head, and then a sequence of elements.
Headelem1 , elem2 , elem3 , …
» Operators as Shorthand
All operators in Mathematica are shorthand for an expression, using the function
FullForm we can see how + is interpreted.
FullForma  b
» Really Everything, Documents and More
Even this presentation is actually an expression. Documents have head Notebook and then the cells are all Cell
expressions. This means that once you know how to work with expressions, you are able to work with everything
within the Mathematica system.
Evaluation of Expressions
» Standard Evaluation
Evaluation is the core operation done by Mathematica. It will attempt to evaluate expressions based on all definitions it knows.
If we return to our simple example, no additional definitions are known.
ab
But if we set the variables to an integer,
a2
ab
Now we see that the definition for a has been used, so we do the same for b.
b5
ab
Now that we see all definitions are used, and we get the result as an output.
This application of definitions is done with infinite recursion, and the results propagate back up through the
evaluation.
» Modifying Evaluation
The standard evaluation model can also be altered, most commonly with what we call attributes. The simplest
example is when using a function like Plot.
PlotSinx, x, 0, 2 
AttributesPlot
So we see that Plot has the attribute HoldAll which prevents normal evaluation of all its elements.
22
GIST.nb
Lists, the Functional Workhorse
Lists are Mathematica’s arrays; as in most programming environments many operations come down to manipulation of these data structures.
Lists (and in fact all expressions in Mathematica) are 1-indexed; using negative indexes will start counting from the
end of the list.
» Constructing
There are a number of functions for building lists in Mathematica, most commonly used are Table and Range.
is really for generating lists of numbers, Table is for lists of more arbitrary
expressions.
Range
Range10
TableExpandx  y ^i, i, 1, 5
» Manipulation and Extraction
The Part function (operator is listi ) is the most common way to extract parts of a list.
ls  Range10
ls3
There is also a span operator.
ls2 ;; 6
Along with Part there are a number of other functions designed for extraction of parts and elements of lists, these
include: First, Last, Most, Rest, and Take.
Procedural to Functional Programming
Mathematica is a multi-paradigm language, and while procedural programming is supported, it is better to use the
system’s native paradigm.
» Lose the Loop
We’ve already seen that there are specific functions for creating lists, but its common for people to use a procedural
approach to this.
a  ;
Doa  Appenda, i, i, 1, 10
a
But when we compare for a larger list, we see a big performance penalty.
a  ;
Doa  Appenda, i, i, 1, 20 000;  Timing
Range20 000;  Timing
The same story plays out when we want to do operations on a list.
Lengtha
b  ;
Dob  Appendb, ai ^2, i, Lengtha;  Timing
If we use the Map function this simply becomes, and runs much faster.
c  Map ^ 2 &, a;  Timing
We can do even better, when we realize that this kind of operation is in fact a list con-
GIST.nb
23
struction problem, so if we reframe using Table, we skip the whole need for two lists, and
just construct one thing.
d  Tablei ^ 2, i, 20 000;  Timing
The same kind of reframing of a problem can be done with For loops as well.
» Conditional Programming
The classic conditional constructs are all supported in Mathematica, but as with loops, they can be reframed into a
functional form.
First we will set a test list.
ls  Range1 000 000;
Now if we wanted to square only the even numbers in this list we could create a function, and then test this function.
square1n_ : IfEvenQn, n ^2
square1  ls;  Timing
We can also use a conditional statement
square2n_ : n ^ 2 ; EvenQn
square2  ls;  Timing
The third way that we could write this would be to use a predicate on the argument.
square3n_ ? EvenQ : n^ 2
square3  ls;  Timing
This clearly has the performance advantage, but it also is the easiest code to read and understand. You know just at
a glance that the body will only be executed if the input is even.
A similar approach could be used to replace a Which statement, which would just result
in several definitions to your symbol.
oddCubeEvenSquare1n_ : WhichEvenQn, n ^ 2, OddQn, n^ 3
oddCubeEvenSquare1  ls;  Timing
oddCubeEvenSquare2n_ ? EvenQ : n^ 2;
oddCubeEvenSquare2n_ ? OddQ : n^ 3;
oddCubeEvenSquare2  ls;  Timing
So we see the same advantage in both speed and readability of this code. This second form also makes debugging
much easier.
Patterns and Rules
Patterns are a way to represent classes of expressions, can also be thought of as a way to symbolically describe a
type of expression.
» Extracting Parts with Patterns
The simplest test is to look at the head of an expression; here we can extract the integers.
Cases3, 4, x, x ^ 2, x ^ 3, _Integer
Next we can look at expressions that contain powers of x.
Cases3, 4, x, x ^ 2, x ^ 3, x ^ _
Related functions to Cases are Position, Count, and Select.
24
GIST.nb
» Transforming Expressions with Rules
Rules allow you to actually change something that matches a certain pattern; here we modify the above example to
extract the actual power.
Cases3, 4, x, x ^ 2, x ^ 3, x ^ power_  power
More powerful is the ability to do this in place in the original expression or list. For that
we use the function ReplaceAll in its short form.
3, 4, x, x ^ 2, x ^ 3 . x^ power_  power
» Patterns and Functions
We already saw using patterns in function definitions when we were looking at conditionals.
square1n_ : IfEvenQn, n ^2
Means square1 with one expression as an argument, we could easily add more.
square1n_, m_ : n ^2  m ^2
This allows the same function name to be overloaded for many different types of input.
Patterns extend along with expressions, so there really is no limit to what you can match with a pattern.
GIST.nb
25
Mathematica Tech Talk
Working with Mathematica 8:From Concepts to Applications
Financial Analysis
Mathematica as a free-form exploration environment
Financial Analysis
In this finance example, we can begin by asking Wolfram|Alpha for stock performances, say Apple Computer. You
can obtain the ticker data by selecting the Computable Data option from the pod.
apple
WolframAlpha"apple", "PriceHistory", 1, "ComputableData",
PodStates  "PriceHistory__Last 5 years"
Financial Analysis
waap  WolframAlpha"Apple", "PriceHistory", 1, "ComputableData",
PodStates  "PriceHistory__Last 5 years"1;
We can change the company name from Apple to AIG and obtain the price data.
waaig  WolframAlpha"AIG", "PriceHistory", 1, "ComputableData",
PodStates  "PriceHistory__Last 5 years"1;
We can use the code below to capture the price history and visualize the data immedi-
26
GIST.nb
ately. Notice the big difference in the price history of these two stocks. This is why the
PairedHistogram shows a big difference in their price distributions.
dates  IntersectionwaapAll, 1, waaigAll, 1;
pa  FlattenPositionwaapAll, 1,  &  dates ;
pg  FlattenPositionwaaigAll, 1,  &  dates;
app  waappaAll, 2;
aigp  waaigpgAll, 2;
GraphicsRowListLinePlotapp, aigp, PlotLabel 
RowStyle"Apple
", ColorData11, Style"AIG", ColorData12,
Ticks  None, Automatic, PairedHistogramapp, aigp, Ticks  None,
ChartLabels  "Apple", "AIG", ImageSize  Large
Financial Analysis
We can now calculate the continuously compounded return on the two stocks:
returnsdata_ : LogLast  First &  Partitiondata, 2, 1;
apcr, aigcr  returns  app, aigp;
Labeled
GraphicsGridListLinePlotapcr, aigcr, PlotRange  All, PlotLabel  Row
Style"Apple
", ColorData11, Style"AIG", ColorData12,
PairedHistogramapcr, aigcr, Ticks  None, ChartLabels 
"Apple \nDaily Return", "AIG \nDaily Return",
ImageSize  Large, "Returns and their distributions"
Surprisingly, with such a dispersion in stock price distribution, the daily return’s distributions are almost identical!
Small but negative compounded return does make a big difference is AIG’s stock price.
Since investors tend to hold more than one stock their portfolios, we can easily measure
the correlation between these two stock returns.
cor  Correlationapcr, aigcr
We can also visualize the correlation of these two stocks:
returns  PartitionRiffleapcr, aigcr, 2;
lm  LinearModelFitreturns, x, x;
ShowListPlotreturns, PlotLabel 
Columnlmx, "R2  "  ToString  lm"RSquared", Alignment  Center,
AxesLabel  "Apple", "AIG", Plotlmx, x, .07, .07, PlotStyle  Red
Notice the R2 is the correlation squared.
cor ^ 2
Since the correlation coefficient is less than 1.0, we have a combination of imperfectly correlated risky assets. The
implications of this fact for risk is central to an understanding of the effects of diversification.
Financial Analysis
Say we have some attributes such as the expected return and standard deviation about two assets. We can construct
a hypothetical model to show their performance in terms of the standard deviation of the portfolio:
GIST.nb
27
erc  8;
ers  10;
stdc  5;
stds  15;
portstd2cr_ : Table
Sqrtw2 stdc2  1  w2 stds2  2 w 1  w cr stdc stds, w, Range0, 1, 0.001;
porter  Tablew erc  1  w ers, w, Range0, 1, 0.001;
datalcr_ : Transposeportstd2cr, porter;
ManipulateListPlotdatalcr, PlotRange  4, 8, 7.5, 10, Frame  True,
ImageSize  200, FrameLabel  "Standard Deviation", "Expected Return",
cr, 1, "Correlation between returns", 1, 0,  .01, Appearance  "Open",
SaveDefinitions  True
The blue line is where the “efficient frontier” lies. It represents the edge of feasible combinations of risk and return.
When the correlation between these two assets is 1.0, there is no risk reduction. As the correlation decreases, we
can achieve a portfolio with the highest expected return given the lowest standard deviation. Therefore a small
correlation is a strategy for diversification. That is what investors are trying to achieve - low risk, high return. This
model turns out to be the foundation of the famous CAPM model that is widely used in the financial industry today.
How does Apple and AIG compare with the hypothetical model? If we use the historical
return as their expected return, we get:
erap  Mean100 apcr;
eraig  Mean100 aigcr;
stdap  StandardDeviation100 apcr;
stdaig  StandardDeviation100 aigcr;
craa  Correlation100 apcr, 100 aigcr;
portstdaa  TableSqrtw2 stdap2  1  w2 stdaig2  2 w 1  w craa stdap stdaig,
w, Range0, 1, 0.001;
porteraa  Tablew erap  1  w eraig, w, Range0, 1, 0.001;
dataaa  Transposeportstdaa, porteraa;
ListPlotdataaa, PlotRange  0, 5, 0, .15, Frame  True,
FrameLabel  "Standard Deviation", "Expected Return"
A portfolio with just Apple and AIG, the expected return is lower.
Of course, this example leads into what is known as asset allocation. Jason Cawley and Roger Brown have already
contributed demonstrations that illustration this point. You can download the free source code from demonstrations.wolfram.com.
Medical Image Processing
Mathematica as a development environment
Medical Image Processing
The task is to analyze the performance of needle-free injection devices that fire powdered drug particles into the
skin on a supersonic gas shock-wave. We are trying to understand the penetration characteristics on a test medium
by photographing thin slices of target under a microscope and measuring the locations of the particles.
The challenge is to do a good job of identifying the particles, and to give me manual over-ride for any mistakes. It
28
GIST.nb
all has to be packaged up in a neat point-and-click interface ready for deployment to users —a pretty typical software development task.
Here is a detail from a typical image.
img 
The task really breaks into three parts:
1. Image processing
2. An optimal GUI for interaction.
3. Report generation
Step 1 seems like it should be the hardest, but can be achieved in very little code. First we need to clean up the
image by deleting dirt and texture.
will remove small white dots within the particles, then inverting and
doing it again with a parameter lets us delete large black components up to the parameter size.
DeleteSmallComponents
img2  DeleteSmallComponents
ColorNegateDeleteSmallComponentsBinarizeimg, 200
Once cleaned we need to erode the components back to a single pixel. But we need multiple particles which slightly overlap to break and erode back to separate points. This
“ultimate erosion” can be done using MaxDetect on the distance transform. We can then
use ComponentMeasurements to get information on these points —in this case the coordinates
of the center.
ComponentMeasurementsMaxDetectDistanceTransformimg2, "Centroid"
As a final cleanup, I used DeleteDuplicates to remove spurious points that come from badly
shaped particles. I give a user parameter for how close points should be before they are
considered the same. Here is all of that packaged into a function.
identifyBlobsimg_, min_, seperation_ : DeleteDuplicatesLast 
ComponentMeasurementsMaxDetectDistanceTransformDeleteSmallComponents
ColorNegateDeleteSmallComponentsBinarizeimg, min,
"Centroid", EuclideanDistance1, 2  seperation &
Medical Image Processing
Here is the entire code for that application. With the image processing, report generation, data analysis, and user
interface it amounts to only 30 lines.
CreatePaletteDynamicModuleimg  Image1, file  "", data,
Manipulate
data  identifyBlobsimg, minSize, minSeperation;
w, h  ImageDimensionsimg;
Showimg, GraphicsThickness.001, Cyan, Line0, h l, w, h r,
Interface
data, Locator, LocatorAutoCreate  True, Appearance  Style"", 20, White,
Button"Import image", IfMatchQ, _Image, img   &
GIST.nb
_
QuietImportfile  SystemDialogInput"FileOpen", Method  "Queued",
Button"generate report", GenerateReportfile, data, h l, h r, w,
OpenerView"auto detection settings", Column
ControlminSize, 50, "minumum particle size", 0, 200,
ControlminSeperation, 7, "minumum seperation", 0, 20
,
l, 0.9, "", 0, 1, ControlPlacement  Left, ControlType  VerticalSlider,
r, 0.9, "", 0, 1, ControlPlacement  Right, ControlType  VerticalSlider,
ContentSize  700, 400,
Initialization  
Image recognition function
identifyBlobsimage_, minBlob_, minSeperation_ :
DeleteDuplicatesLast  ComponentMeasurements
MaxDetectDistanceTransformDeleteSmallComponents
ColorNegateDeleteSmallComponentsBinarizeimage, minBlob,
"Centroid", EuclideanDistance1, 2  minSeperation &;
depthx_, y_, w_, l_, r_ : x, l 
x
w
r  l  y;
Report generation function
GenerateReportpath_, data_, l1_, l2_, w_ : BlockpenetrationData,
penetrationData  depth, w, l1, l2 &  data;
IfLengthdata  0, CreateDocument
TextCell"Particle impact analysis", "Title",
TextCellRow"Sample: ", path, "\n", "Report generated ",
DateString, "\n", "Particles found: ", Lengthdata, "Text",
ExpressionCellDistributionChartpenetrationDataAll, 1,
PlotLabel  "Spread profile", BarOrigin  Left, "Output",
ExpressionCellDistributionChartpenetrationDataAll, 2,
PlotLabel  "Depth profile", BarOrigin  Top, "Output",
ExpressionCellListLinePlotMapMean1 &,
DeleteCasesBinListspenetrationData, w  25, 10^ 10, , ,
PlotLabel  "Mean depth", "Output",
ExpressionCellDensityHistogrampenetrationData, 8,
PlotLabel  "Density", "Output",
TextCell"Particle coordinates", "Section",
ExpressionCellSortRoundpenetrationData, "Input";
, WindowTitle  "Particle analyzer"
An Engineering Control Problem
29
30
GIST.nb
Mathematica as solver
An Engineering Control Problem
Our third example has its place in engineering. We will be using Mathematica to simulate an inverted pendulum —
you’ve probably seen one in a Segway. Wolfram’s Andrew Moylan has done some good work on this example below.
mass
m
x
force f
q
mas
In this example we’ll model a single inverted pendulum on a moving cart, shown here, and use the new control
systems functionality to stabilize it.
First we need to simulate the pendulum-and-cart system, and then we can try to determine a “control” force f t
that will move the cart to keep the pendulum balanced upright.
Assuming the pendulum arm is uniform (so its center of mass is at d  2), ignoring friction, and using dimensionless
units in which the gravitational acceleration g  1, the model pendulum-and-cart system shown above satisfies a
pair of differential equations.
Let’s explore the concrete case where m  c  d  1:
eqns  2 ft  Cost  t2  Sint  t  4 x t,
2 Cost  2 Sint x t   t  0;
GIST.nb
31
Using NDSolve, Mathematica’s numerical differential equation solver, we can immediately simulate this system from a slightly off-center initial condition q0  p  2 - 0.1.
We’ll initially use no control force: f t  0; that is, we make no attempt to move the
cart to keep the pendulum upright.
NDSolveJoineqns . ft  0, x0  x '0  '0  0, 0 
x, , t, 0, 30

 0.1,
2
Here is an animation of the solution. The animation code is included at the end of this
notebook.
AnimatePendulumFirst
We can combine the above two steps into a handy function that animates the system for
any control force f t:
SimulatePendulumforce_ : AnimatePendulumFirstNDSolveJoineqns . ft  force,
x0  x '0  '0  0, 0 

2
 0.1, x, , t, 0, 30
You can then use this SimulatePendulum function to explore ideas for keeping the pendulum balanced upright.
The simplest plausible idea I can think of is to push the cart in whichever direction the
pendulum is currently leaning —move the cart under the pendulum, to stop it falling
over. The pendulum is perfectly upright when qt  p  2, so let’s try pushing the cart
with the force f t  p  2 - qt:
SimulatePendulum3 

2
 t
32
GIST.nb
Maybe we can make it smoother by also taking into account how fast the pendulum is
falling, which is q£t: (the first derivative of q)
SimulatePendulum3 

 t   't
2
That stopped the wobbling nicely, but now the whole system is coasting steadily off to the right. What if we want the
cart to stay at the point xt  0?
We could go on modifying the control force by hand, and try to get a working solution but it’s much easier to turn to
Mathematica’s new control systems design features.
We want to keep the state of the system near the unstable equilibrium point:
TraditionalFormxt, x 't, t,  't  0, 0,   2, 0
The equilibrium is “unstable” because, in the absence of any control force, small deviations from this point tend to
get larger.
The hand-made control forces we tried above are special cases of “linear control”, in which the control force is some
constant linear combination of the deviation of the states from the equilibrium point:

TraditionalFormHoldFormft  k1 xt  k2 x t  k3 t    k4  t
2
The constants ki are called “feedback gains”, and we can use a couple of new control theory functions in Mathematica 8 to find good values for them.
An Engineering Control Problem
We begin by constructing a linear model for the behavior of the system near the equilibrium point. The function
StateSpaceModel does this automatically if we give it the nonlinear equations:
GIST.nb
33
model  StateSpaceModeleqns,
xt, 0, x't, 0, t,

,  't, 0, ft, , t
In the third argument to StateSpaceModel, we specified that there is one control variable: f t.
2
The result of StateSpaceModel looks like an augmented matrix. Its columns describe the effect of small deviations of each of the state variables xt, x£ t, qt, q£ t from equilibrium. For example, the third column says that,
near the equilibrium point, small deviations in qt away from the upright position (qt  p  2) lead to an increase
in both x ' t (the speed of the cart) and q£ t (the rate at which the pendulum is falling). The augmented fifth
column similarly describes the immediate effect of the control force on the state variables.
Now that we have a model for the system, we can use the function LQRegulatorGains to
find values for the feedback gains ki :
gains 
LQRegulatorGainsNmodel, DiagonalMatrix1, 10, 10, 100, 1  First
In the second argument of LQRegulatorGains, we specified the following quadratic “cost” function for the
system:
TraditionalFormHoldFormcost  1 xt2  10 x t2  10 t2  100  t2  1 ft ^2
LQRegulatorGains finds values for the constants ki that minimize this cost function over time (in the linearized
model).
The cost coefficients 1, 10, 10, 100 are a guess, based on an intuition that deviations in q£ t are relatively more
important than deviations in x£ t and qt, which are in turn more important than deviations in xt. Different
coefficients would give better, worse, or just qualitatively different behavior. In real life the coefficients might
represent actual costs associated with deviations of a system from the desired state.
Using these gains, our control force is this :
controlforce   gains.xt, x't, t 

,  't
2
Let’s test it out:
SimulatePendulumcontrolforce
An Engineering Control Problem
Our pendulum stabilized via LQRegulatorGains can survive some quite bumpy conditions:
Generate random bumpy conditions:
verybumpyt_  SumRandomReal10, 10 Exp10 t  RandomInteger152 , 20;
34
GIST.nb
Specific verybumpy used in blog post:
verybumpyt_  13.203576835315666` 10 15t  1.4310438601615516` 10 12t 
2
2
2.3670864940973857` 10 11t  7.791914458883305` 10 9t 
2
2
12.661390107806994` 10 8t  13.39019250013871` 10 7t 
2
2
7.873272031334967` 10 6t  6.707528013930826` 10 2t 
2
2
2.548183565506953` 10 1t  7.231209749707311` 10 t ;
2
2
Plotverybumpyt, t, 0, 30, PlotRange  All,
Filling  Axis, PlotLabel  HoldFormverybumpyt, AxesLabel  t
SimulatePendulumcontrolforce  verybumpyt
If you bump it hard enough, the linear control system we have used here won’t be able to recover. You can try that
out by downloading the CDF file for this talk. Another natural thing to explore would be the effect of different cost
functions in LQRegulatorGains. It’s also fun to try to design your own superior control force by hand for this
relatively simple system.
The Statistics of Wind Power
ò Image courtesy of Wikipedia: User Fanny Schertzer
The Statistics of Wind Power
What can Mathematica contribute in the field of sustainable energy? It turns out with a combination of Wolfram’s
built-in data and our newly expanded statistical functionality, Mathematica can make quite a bit of headway.
GIST.nb
35
In this example we use information from WeatherData and investigate the feasibility of using wind to generate
electricity for central Illinois. [We will also make use of external data from (www.vestas.com/en/wind-powerplants/procurement/turbine-overview/v90-1.8/2.0-mw.aspx#/vestas-univers) in our analysis.]
First we will get the average daily wind speed for the weather station nearest to Bloomington IL, beginning in the year 2000.
wsData  WeatherData"Bloomington", "Illinois",
"MeanWindSpeed", 2000, 1, 1, Date, "Day"
We can take a look at the form of the data and visualize the data in the time series
immediately:
DateListPlotwsData, Joined  True
The entries are ordered pairs consisting of the date and the average wind speed for the day. We will need to extract
the speeds from the data.
I will take the second part of all the data points and convert from km/hr to m/s. Now
let’s visualize the histogram quickly.
wndData  1000 wsDataAll, 2  602 ;
histplot  HistogramwndData, 45, "PDF"
The Statistics of Wind Power
Since wind speed can be assumed to vary continuously, we can represent the distribution for our observations using
the SmoothKernelDistribution function.
Visualize this distribution together with the original histogram.
dist  SmoothKernelDistributionwndData;
Showhistplot, estplot  PlotPDFdist, x, x, 0, 13, PlotStyle  Thick, Red
And we can ask questions of that distribution. For instance, what is the probability of
wind speed being greater than 3.5 m/s?
Probabilityspeed  3.5, speed é dist
The Statistics of Wind Power
However, I am going to assume that this is really the ExtremeValueDistribution, and I am going to use the data to
estimate the parameters of the distribution.
params  FindDistributionParameterswndData, ExtremeValueDistribution, 
distEV  ExtremeValueDistribution,  . params
And now let’s test to see if our assumption should be rejected.
tstData  DistributionFitTestwndData, distEV, "HypothesisTestData";
tstData"AutomaticTest", tstData"TestConclusion"
Here is the test statistic and p-value for the test.
tstData"TestDataTable"
We can now visualize our distribution estimate.
Show
histplot, estplot  PlotPDFdistEV, x, x, 0, 13, PlotStyle  Thick, Red
36
GIST.nb
The generator we are using for our analysis here requires 3.5 m/s wind speed to generate electricity. We can use the
Probability function to estimate what proportion of time the generator will be contributing to the grid.
Based on the data distribution, we can compute the probability that wind speed is
greater than the threshold of 3.5 m/s:
Probabilityx  3.5, x é distEV
Let’s visualize the result.
PlotEvaluatePDFdistEV, x 1, Boolex  3.5, x, 0, 13, Filling  2  Axis
The Statistics of Wind Power
From the manufacturer’s spec sheet for the V90-1.8/2.0 MW generator we can make a chart indicating anticipated
performance levels in the central Illinois location.
Grid
Style"Wind Speed ms", FontWeight  "Bold", Style"Probability",
FontWeight  "Bold", Style"Estimated Power MW", FontWeight  "Bold",
" 3.5", Probabilityx  3.5, x é distEV, 0.0,
"Between 3.5 and 7", Probability3.5  x  7, x é distEV, "0 to .8",
"Between 7 and 13", Probability7  x  13, x é distEV, ".8 to 1.65",
" 13", Probability13  x, x é distEV, "1.65"
, BaseStyle  "Menu", 19, Alignment  Left,
Background  LightYellow, Frame  False, All, Spacings  2, 0.62
We can calculate the power that this generator will accumulate over the time period
from 2000 until now:
power  Total
Probability3.5  x  7, x é distEV Mean0, 0.8,
Probability7  x  13, x é distEV Mean1.65, 0.8,
Probability13  x, x é distEV 1.65
hours in a year
Result
cost of electricity in IL
Result
If we were to purchase electricity for that much power, we would have spent the following amount for one year. Keep in mind, this is only for one wind generator.
valuePerYear  power  8760  12.4  100  1000
Website Analysis
Combining data import, transformation and graph
theory
If you are curious about the structure of a website, Mathematica can help. Let’s create a webcrawler to find linkages
between a specified URL and all of its hyperlinks:
GIST.nb
37
webcrawlerrooturl_, depth_ : Flatten
RestNestListUnionFlattenThread  Import, "Hyperlinks" &  Last   &,
""  rooturl, depth;
Pick a website of interest, say twitter:
twdata  Quiet  webcrawler"http:www.twitter.com", 2;
Visualize the result by generating a graph of networks. Mouse over the vertices to see
the names of the nodes.
g  Graphtwdata, VertexLabels  Placed"Name", Tooltip
Website Analysis
Using the HITSCentrality function, find the HITS hub and authority centralities for the graph.
MatrixFormHITSCentralityg
Link the pages with their HITS centralities.
pagesHITS  TransposeVertexListg, HITSCentralityg1;
TableFormpagesHITS1 ;; 5
We need to sort the pages according to their degree centralities. Here is the top 10 list.
sortedpages  SortpagesHITS, 12  22 &;
TableFormsortedpages1 ;; 10
This shows the subgraph containing all of the top page’s interactions with other pages
hyperlinked from twitter.com.
HighlightGraphg, NeighborhoodGraphg, sortedpages2, 1
Filtering Sound from Apollo 11
Signal processing with wavelets
Wavelet analysis is one of those topics which has a broad range of applications. We can use wavelets for image
compression, for signal detection in time series data, etc. Wavelet analysis is now an integrated part of Mathematica. In this example, I’ll demonstrate how wavelet analysis can be applied to sound data.
Here is a sound file from NASA’s Apollo 11 mission, the spaceflight that landed the first
humans on the moon in 1969.
snd  ExampleData"Sound", "Apollo11ReturnSafely"
Perform a discrete wavelet transform on this sound data directly.
dwd  DiscreteWaveletTransformsnd, CDFWavelet
WaveletListPlotdwd
38
GIST.nb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
5000
10 000
15 000
20 000
25 000
In this case, we would like to decrease the backgroungmad noise level by performing a
wavelet threshold on the data.
thr  WaveletThresholddwd, "Soft", .05;
How does it sound?
InverseWaveletTransformthr
Now it sounds much better!
Fun with Mazes
Combining image processing, solving, visualization,
and interfaces
Fun with Mazes
Last but not least, I would like to show you a recreational example of solving a maze with Mathematica.
maze 
;
highlightMazePathpath_ :
ShowGraphicsGrayLevel0.2, Rectangle 1., 1., 30., 20.,
maze, Graphpath, EdgeListPathGraphpath, VertexCoordinates 
AbsoluteOptionsmaze, VertexCoordinates2, VertexIndexmaze,  &  path,
VertexSize  0, EdgeStyle  DirectiveThickness0.01, Red,
Opacity1, CapForm"Round", ImageSize  500
GIST.nb
39
Note: the following Export command will take a considerable amount of time to run
depending on your computer system.
Export"maze.mov", Table
highlightMazePathFindShortestPathmaze, "in", "out"1 ;; n, n, 2, 307, 1
SystemOpen"maze.mov"
We can also use Manipulate to see how the maze is solved.
ManipulatehighlightMazePathFindShortestPathmaze, "in", "out"1 ;; n,
n, 2, 307, 1, SaveDefinitions  True
40
GIST.nb
Mathematica Tech Talk
Parallel Computation with Mathematica
Parallel Computation Architecture in Mathematica
Mathematica provides a high-level, parallel language interface that allows you to connect multiple Mathematica
kernels using the MathLink communication protocol and provides a framework to distribute calculations and
collect results.
Parallelism in Mathematica: Extending the functionality of
Mathematica
The kernel is the part of Mathematica that performs computations. Every remote kernel contains the full functionality of a standard Mathematica kernel.
The Mathematica kernel contains:
» Language: a consistent high-level language; choose the programming paradigm that is best for your
computation needs —procedural, functional, rule-based, recursive, or object-oriented.
» Mathematics & algorithms: wide range of functions for mathematics, science, engineering; automatic
algorithm selection; state-of-the -art performance, scalability.
» Data handling: automatic integration of hundreds of data formats; load-on-demand, curated data in math,
physics, chemistry, finance, and geography.
» System interfaces & deployment: databases, spreadsheets, legacy C or Fortran code, Web Services, Java,
.NET
The managing computer can use the Mathematica front end for:
» Visualization & graphics: automatic creation of high-impact 2D and 3D visualizations of functions and data.
» Dynamic interactivity: create dynamic interfaces quickly and easily; manipulate graphics, math, tables, text,
etc.
» Notebooks & documents: integrated technical word processor; write documents with typeset mathematical
formulae; output to PDF, HTML, XML; create slide show presentations.
» Parallelism: built-in parallel efficiency visualization tools in Mathematica
Getting Started
GIST.nb
41
In the Evaluation menu, you can find the status of all your currently running kernels, as well as configure new ones,
through the Parallel Kernel Status and Parallel Kernel Configuration menu items, respectively.
Additionally, you can use the following commands to programmatically work with kernels:
The LaunchKernels command will explicitly launch all kernels that you have configured
on your system.
LaunchKernels
You can see a list of the running kernels via the Kernels command.
Kernels
You can get the number of running kernels via $KernelCount.
$KernelCount
You can shut down the kernels with the CloseKernels command.
CloseKernels
Root Finding
In this example, we want to find solutions to the equation cost 2  = ‰-t + 0.3 using Newton’s method. The Mathematica function FindRoot can be used for this. FindRoot takes as its input the equation and a starting point for the
algorithm. By using multiple starting points, we can find all roots of the equations in the domain.
plot  PlotCost2 , Exp t  .3, t, 0, 3 
Create some starting points for Newton’s method.
startingvals  Tablex, x, 0, 3 , .1;
vals  MapFindRootCost2   Expt  .3, t, 1 &, startingvals; 
AbsoluteTiming
Plot the two curves along with the solutions.
Showplot, GraphicsRed, PointSizeMedium,
PointMapThreadt . 1, 2 &, vals, Cost2 . vals
Root Finding: Root finding in parallel
Here is a parallel approach to the problem, achieved by replacing Map with ParallelMap.
vals  FindRootCost2   Exp t  .3, t, 1 &  startingvals; 
AbsoluteTiming
pvals  ParallelMapFindRootCost2   Expt  .3, t, 1 &, startingvals; 
AbsoluteTiming
Showplot, GraphicsRed, PointSizeMedium,
PointMapThreadt . 1, 2 &, pvals, Cost2 . pvals
Root Finding: Root finding in parallel – behind the scenes
42
GIST.nb
We can use the debug tools to get a better look at what is happening when we use ParallelMap and to look at the
efficiency of our parallelization strategy.
SetOptions$Parallel, Tracers  SendReceive
SetOptions$Parallel, TraceHandler  "Save"
Notice that ParallelMap splits the starting values between the worker kernels. Also
notice that some of the worker kernels have found the same roots.
pvals  ParallelMapFindRootCost2   Exp t  .3, t, 1 &, startingvals; 
AbsoluteTiming
TableFormTraceList, TableDepth  2,
TableHeadings  Automatic, "Trigger", "Event"
Turn off debugging:
SetOptions$Parallel, Tracers  
SetOptions$Parallel, TraceHandler  "Print"
Monte Carlo: Calculating p
The ratio of the number of random points generated inside a square that also fall within an inscribed circle can be
related to p.
Solvepi r2 
PointsInside
, pi
TotalPoints
Generate some points and then determine if they are inside or outside the circle.
r
1
;
2
pts  RandomReal r, r, 5000, 2;
insidepts  Selectpts, Norm1  r &
Graphics 
EdgeFormThick, White, Rectangle .5, .5,
Black, Circle0, 0, .5,
Red, Pointinsidepts,
Black, PointComplementpts, insidepts

Apply the formula, but we get a bad approximation.
Lengthinsidepts
N
Lengthpts r2

Use more points.
pts  RandomReal r, r, 50 000, 2;
insidepts  Selectpts, Norm1  r &;
Lengthinsidepts  Lengthpts  r^ 2  N
We can keep adding points to get better approximations, but it will continue to get slower, as well.
Monte Carlo: Calculating p in parallel
So let’s try to do this in parallel!
GIST.nb
43
DistributeDefinitionsr
pts  ParallelTableRandomReal r, r, 25 000, 2, LengthKernels;
insidepts  ParallelMapSelect, Norm  r & &, pts;
The different colors represent different kernels. Each kernel has a unique set of random
points.
Withlen  Lengthpts,
MapIndexedGraphics
EdgeFormThick, White, Rectangle r, r,
Black, Circle0, 0, r,
Hue21  len, PointSizeSmall, Point11,
LighterHue21  len, .5, PointComplement11, 12,
ImageSize  225 &, Transposepts, insidepts


Combine results and calculate p!
DistributeDefinitionspts, insidepts; NMeanParallelTable
Lengthinsideptsi  Lengthptsi  r^ 2, i, LengthKernels
We can optimize our routine a little bit and compute p using 10 million points.
AbsoluteTimingWith NUMPOINTS  10 000 000, CPUS  LengthKernels,
partitions  QuotientNUMPOINTS, CPUS  Join
Table1, ModNUMPOINTS, CPUS, Table0, CPUS  ModNUMPOINTS, CPUS;
NTotal ParallelMap  TotalRound TotalRandomReal1  2, 1  2, , 2^ 2,
2  .25 &, partitions  NUMPOINTS  1  1  2 ^2
Monte Carlo: Calculating p – benchmarks
Let’s perform some benchmarks. Note that the cells are unevaluatable because they can take a long time to run. If
you would like to reproduce these results simply make the cells evaluatable (from the Cell menu, select Cell Properties ▶ Evaluatable).
maxpoints  Range10 000 000, 80 000 000, 1 000 000;
pts  TableAbsoluteTimingWith NUMPOINTS  max, CPUS  LengthKernels,
partitions  QuotientNUMPOINTS, CPUS  JoinTable1,
ModNUMPOINTS, CPUS, Table0, CPUS  ModNUMPOINTS, CPUS;
NTotal ParallelMap  TotalRound TotalRandomReal
1  2, 1  2, , 2^ 2, 2  .25 &,
partitions  NUMPOINTS  1  1  2 ^ 2, max, maxpoints;
44
GIST.nb
ListLinePlotMapThread1, 2 &, maxpoints, ptsAll, 1, Frame  True,
FrameLabel  Style"Number of Points", 12, Style"Seconds", 12
25
Seconds
20
15
10
5
1 μ 107
2 μ 107
3 μ 107
4 μ 107
5 μ 107
6 μ 107
7 μ 107
8 μ 107
Number of Points
Visualizing the Mandelbrot Set
In this example we will take a look at how to visualize the Mandelbrot set.
First we define a function that returns the number of times it took for the set to converge at a given (x, y) coordinate.
MandelbrotFunction  Compilec, _Complex,
 LengthFixedPointList2  c &, c, 50, SameTest  Abs2  2.0 &
Compute the data using Table.
AbsoluteTimingdataSerial 
TableMandelbrotFunctionx  y , x,  2, 0.5, 0.002, y, 1, 1, 0.002;
We can use ArrayPlot to visualize the data.
ArrayPlotTranspose  dataSerial, ColorFunction  Hue, ImageSize  800  Timing
Visualizing the Mandelbrot Set: In parallel
To parallelize the process, Table can be replaced with ParallelTable as in the previous examples.
DistributeDefinitionsMandelbrotFunction;
AbsoluteTimingdataParallel  ParallelTable
MandelbrotFunctionx  y , x, 2, 0.5, 0.002, y,  1, 1, 0.002;
The parallel functions in Mathematica support a Method option, that adjusts load balancing. The automatic setting results in medium load balancing and is usually the best
choice. This option is documented in the reference page for Parallelize.
DistributeDefinitionsMandelbrotFunction;
AbsoluteTiming
dataParallelC  ParallelTableMandelbrotFunctionx  y , x, 2, 0.5, 0.002,
y,  1, 1, 0.002, Method  "CoarsestGrained";
DistributeDefinitionsMandelbrotFunction;
AbsoluteTiming
dataParallelF  ParallelTableMandelbrotFunctionx  y , x, 2, 0.5, 0.002,
y,  1, 1, 0.002, Method  "FinestGrained";
GIST.nb
45
We can use ArrayPlot to visualize the data and check to see if we obtained the same
results.
ArrayPlotTransposedataParallel, ColorFunction  Hue, ImageSize  800
Binomial Stock Options: Problem setup
Use the Binomial model to predict the value of a stock option at the time of maturity.
Helper functions
Visualization function
Data structure
Binomial Stock Options: Finding valuations
Use the Binomial model to predict the value of a stock option at the time of maturity.
Find volatilities and tables of strike values for Microsoft and Google.
msftvolatility 
StandardDeviationFinancialData"msft", "Jan. 1, 2008" All, 2 
FinancialData"msft", "Jan. 1, 2008"1, 2;
googvolatility  StandardDeviationFinancialData"goog", "Jan. 1, 2008" 
All, 2  FinancialData"goog", "Jan. 1, 2008"1, 2;
msftstrikevalues  Tablex, x, 10, 50, 2.5
googstrikevalues  Tablex, x, 270, 660, 10;
Try the function out on calls and puts for Microsoft across the entire strike price range.
msftoptionsprices 
MapReplacePartbinomialprice28.69, msftvolatility, 5  252, .06869,
5  365, 11, , callvalue1, 1  ,
ReplacePartbinomialprice28.69, msftvolatility, 5  252, .06869, 5  365,
11, , putvalue1, 1   &, msftstrikevalues  AbsoluteTiming
To run this in parallel, the worker kernels must have all of the function definitions, not
just the ones explicitly specified.
DistributeDefinitionsbinomialprice, up, down, oneup, f,
msftvolatility, googvolatility, probabilityup, callvalue, putvalue
msftoptionsprices 
ParallelMapReplacePartbinomialprice28.69, msftvolatility, 5  252,
.06869, 5  365, 11, , callvalue1, 1  ,
ReplacePartbinomialprice28.69, msftvolatility, 5  252, .06869, 5  365,
11, , putvalue1, 1   &, msftstrikevalues  AbsoluteTiming
Now we’ll do calls and puts for Google over a much larger range of strike values:
googoptionsprices 
ParallelMapReplacePartbinomialprice540.3, googvolatility, 5  252,
.06869, 5  365, 14, , callvalue1, 1  ,
ReplacePartbinomialprice540.3, googvolatility, 5  252, .06869, 5  365,
14, , putvalue1, 1   &, googstrikevalues;  AbsoluteTiming
46
GIST.nb
ListLinePlotgoogoptionspricesAll, 112 ;; 31,
Frame  True, FrameLabel  "Strike", "Price",
PlotLabel  "Market vs. Binomial price GOOG CALLs"
ListLinePlotgoogoptionspricesAll, 212 ;; 31,
Frame  True, FrameLabel  "Strike", "Price",
PlotLabel  "Market vs. Binomial price GOOG PUTs"
Genomic Data: Comparing organisms
In this example we try to determine which organism is closer genetically to another organism. We do this by computing edit distances between genes in the target organism to the two candidate organisms.
Import the FASTA files:
picrogenes  Import"picrophilus_torridus_gene.fna";
acidogenes  Import"thermoplasma_acidophilum_gene.fna";
volcaniumgenes  Import"thermoplasma_volcanium_gene.fna";
Load gene data on the remote nodes.
Withpicrogenes  picrogenes, acidogenes  acidogenes,
volcaniumgenes  volcaniumgenes, ParallelEvaluate
picro  picrogenes; acido  acidogenes; volcanium  volcaniumgenes;;;
This is what one of the genes on the first worker kernel looks like.
ParallelEvaluatepicro1, Kernels1
Now that we have the data on the remote nodes, we can do some work on it.
Genomic Data: Comparing organisms
Compare the first eight genes of thermoplasma volcanium to thermoplasma acidophilum and return the minimum
edit distance between the gene and the genes in acidophilum.
acidovolcanium 
ParallelMapFunctiongene, MinEditDistancegene,  &  acido,
volcaniumgenes1 ;; 8;  AbsoluteTiming
acidovolcanium
Do the same for the first eight genes in thermoplasma volcanium and picrophilus
torridus.
picrovolcanium 
ParallelMapFunctiongene, MinEditDistancegene,  &  picro,
volcaniumgenes1 ;; 8;  AbsoluteTiming
Compute the mean edit distance for the eight genes between acidophilum and volcanium vs. torridus and volcanium.
Meanacidovolcanium  N
Meanpicrovolcanium  N
ListLinePlotacidovolcanium, picrovolcanium
GIST.nb
47
Genomic Data; Comparing organisms vs. actual scientific
classification
Let’s take a look at the respective positions in the scientific classification of the compared organisms.
tree  Archaea  Euryarchaeota,
Euryarchaeota  Thermoplasmata, Thermoplasmata  Thermosplasmatales,
Thermosplasmatales  Thermosplasmataceae,
Thermosplasmataceae  Thermoplasma, Thermoplasma  acidophilum,
Thermoplasma  volcanium, Thermosplasmatales  Picrophilaceae,
Picrophilaceae  Picrophilus, Picrophilus  torridus;
TreePlottree, Automatic, Archaea, VertexLabeling  True
Genomic Data: Comparing organisms – benchmarks
Some benchmarking, first using ParallelMap to compute the minimum edit distances for genes 1–8, 1–10, 1–12, etc.
acidovolcaniumtiming  TableAbsoluteTiming
ParallelMapFunctiongene, MinEditDistancegene,  &  acido,
volcaniumgenes1 ;; x, x, 8, 56, 2;
The number of gene computations vs. time to compute. Notice that there are some stepwise properties.
ListLinePlot
MapThread1, 2 &, Tablex, x, 8, 56, 2, acidovolcaniumtimingAll, 1,
Frame  True, FrameLabel  "Number of Comparisons", "Seconds"
Seconds
200
150
100
50
10
20
30
40
50
Number of Comparisons
For our example, it is not the case that the computations will all be equally difficult. Comparing gene 2 and comparing gene 8 might be very different in terms of computation time if gene 2 is relatively short and gene 8 is relatively
long.
Genomic Data: Comparing organisms – using ParallelSubmit and
WaitAll
This example may be better suited to using ParallelSubmit and WaitAll.
pids  MapParallelSubmitf; Pause1 &, Tabley, y, 16
WaitAllpids
48
GIST.nb
Before, it took a combined 100 seconds to compute this.
acidovolcanium 
MapFunctiongene, ParallelSubmitMinEditDistancegene,  &  acido,
volcaniumgenes1 ;; 8;
picrovolcanium  MapFunctiongene, ParallelSubmit
MinEditDistancegene,  &  picro, volcaniumgenes1 ;; 8;
acidovolcanium;
picrovolcanium;
acidovolcaniumresult  WaitAllacidovolcanium;
picrovolcaniumresult  WaitAllpicrovolcanium;  AbsoluteTiming
Of course it’s important we get the same results!
Meanacidovolcaniumresult  N
Meanpicrovolcaniumresult  N
Genomic Data: Comparing organisms —benchmarking
ParallelSubmit and WaitAll
Again some benchmarking.
acidovolcaniumtiming  TableAbsoluteTimingpids 
MapFunctiongene, ParallelSubmitMinEditDistancegene,  &  acido,
volcaniumgenes1 ;; x; WaitAllpids;, x, 8, 56, 2;
ListLinePlot
MapThread1, 2 &, Tablex, x, 8, 56, 2, acidovolcaniumtimingAll, 1,
Frame  True, FrameLabel  "Number of Comparisons", "Seconds"
Seconds
200
150
100
50
10
20
30
40
50
Number of Comparisons
Airfoil Wing Design
Now let’s have a look at how we can usefully apply parallelization to existing code for a real-world problem.
The example we are going to look at comes from the field of aeronautics, in the area of airfoil design. Using some
simple code adapted from the materials presented in Foundations of Aerodynamics: Bases of Aerodynamic Design
5th edition by Arnold M. Kuethe and Chuen-Yen Chow, we’ll see how to spot areas where parallelization can be
usefully applied and implemented.
» Airfoil data
Airfoils are commonly constructed from datasets. The example considered here is the NACA 64A010 airfoil and the
source of the dataset is the Pubic Domain Aeronautical Software website: www.ae.uiuc.edu/m-selig/ads/coord/naca64a010.dat (clicking the link will download the data to your computer).
We begin by importing the dataset to Mathematica.
GIST.nb
49
Import the data and give it a name.
predata  Import"naca64a010.dat", "Table";
The dataset is small enough that we can look at it to determine its structure, but it is generally a good idea to get
some sense of the size before trying to view the data.
Determining the size of the dataset.
Dimensionspredata
The single value indicates there are 112 rows in the data and they are not all of equal length. In this case we can see
that we have uniform rows if we drop the first row.
The dimensions of the data with the first row deleted.
DimensionsRestpredata
Visualizing the data using ListLinePlot.
ListLinePlotRestpredata, Mesh  All, AspectRatio  Automatic
Airfoil Wing Design: Deriving panel points
This particular dataset gives the coordinates, starting at the trailing edge of the top surface, proceeding around the
contour until it reaches the trailing edge of the bottom surface. These coordinates define the basic panels of the
airfoil.
However, what you usually want to do is look at an arbitrary number of panels. For this we need to take these
points and parameterize the airfoil so that we can use coordinates of our choosing.
ParameterizeAirfoilarry_ ? MatrixQ :
Moduleindxed  MapIndexedJoin2, 1 &, arry, xfun, yfun,
xfun  InterpolationindxedAll, ;; 2, Method  "Spline";
yfun  InterpolationindxedAll, 1 ;; 3 ;; 2, Method  "Spline";
xfun, yfun
nacaChrd, nacaCam  ParameterizeAirfoilRestpredata
Visualizing the parametrization. Note the parametrization traverses the upper camber
first.
ManipulateShowParametricPlotnacaChrdt, nacaCamt,
t, 1, 35, PlotRangePadding  Scaled.03, Graphics
Red, PointSizeLarge, PointnacaChrdi, nacaCami, i, 1, 35
The next step in the process is to create boundary points for the panels we will use.
MakePointSetlis1_ ; VectorQlis1, NumericQ, lis2_ ; VectorQlis2, IntegerQ ;
Lengthlis1  Lengthlis2  1 :
Modulediffs  Differenceslis1, stps, len, pts, stps  diffs  lis2;
len  Lengthdiffs;
pts  SortJoinFlattenTablej, i, 1, len, j, lis1i,
lis1i  diffsi  stpsi, stpsi, 1, lis1 1;
JoinReversepts, Restpts
PanelPoints
fn1_InterpolatingFunction, fn2_InterpolatingFunction, ptslis_List :
ReverseWithmx  fn11, 1, 2, len  Lengthptslis, TableIfi  Floorlen  2,
fn1t, fn2t . FindRootfn1t  ptslisi, t, mx  4, fn1t,
fn2t . QuietFindRootfn1t  ptslisi, t, 3 mx  4, i, len
50
GIST.nb
Creating the point set for the x coordinates. Here we establish several region of differing
step sizes.
pts  MakePointSet0, .05, .13, .18, .75, 1., 20, 14, 9, 6, 5;
Next we create the panel boundaries.
ShortpnlPts  PanelPointsnacaChrd, nacaCam, pts, 6
Visualizing the pnlPts set.
GraphicsGray, PolygonpnlPts
Airfoil Wing Design: Airfoil functions
The calculations we will be examining are the pressure coefficients over each wing panel as defined by our airfoil
data. To do this we will first define a number of functions which will be used in the function PressureCoefficientsSerial. The original code was in FORTRAN but we have rewritten it here in Mathematica.
PressureCoefficientsSerial takes the points defining the panels as well as an angle of attack for the airflow and
returns a list containing panel midpoints, panel angles, panel lengths, velocity vector and pressure coefficient
vector for a 2D panel method for an airfoil defined by the points at the angle of attack.
First we must define a large set of subfunctions.
Functions for calculating normal velocity coefficients.
CNorm1i_, j_ :
Whichi  j,  1, True, DDi, j FFi, j  2  CCi, j GGi, j  CNorm2i, j
CNorm2i_, j_ : Whichi  j, 1, True, DDi, j  QQi, j FFi, j  2 Sj 
AAi, j CCi, j  DDi, j EEi, j GGi, j  Sj
ANormi_, j_, m_ : Whichi  m  1 && j  1, 1, i  m  1 && j  m  1, 1, i  m  1, 0,
j  1, CNorm1i, 1, j  m  1, CNorm2i, m, True, CNorm1i, j  CNorm2i, j  1
GIST.nb
51
Auxiliary functions for the 2D panel methods. These are taken directly from Foundations of Aerodynamics by Arnold M. Keuthe and Chuen-Yen Chow with adjustments to
take advantage of built-in Mathematica functions.
AAi_, j_ :  xi  XBj Costhetaj  yi  YBj Sinthetaj
BBi_, j_ : SquaredEuclideanDistancexi, yi, XBj, YBj
CCi_, j_ : Sinthetai  thetaj
DDi_, j_ : Costhetai  thetaj
EEi_, j_ : xi  XBj Sinthetaj  yi  YBj Costhetaj
FFi_, j_ : Log1  Sj^ 2  2 AAi, j Sj  BBi, j
GGi_, j_ : ArcTanBBi, j  AAi, j Sj, EEi, j Sj
PPi_, j_ :
xi  XBj Sinthetai  2 thetaj  yi  YBj Costhetai  2 thetaj
QQi_, j_ :
xi  XBj Costhetai  2 thetaj  yi  YBj Sinthetai  2 thetaj
Functions for calculating tangential velocity coefficients.
CTan1i_, j_ :
Whichi  j,   2, True, CCi, j FFi, j  2  DDi, j GGi, j  CTan2i, j
CTan2i_, j_ : Whichi  j,   2, True, CCi, j  PPi, j FFi, j  2 Sj 
AAi, j DDi, j  CCi, j EEi, j GGi, j  Sj
ATani_, j_, m_ :
Whichj  1, CTan1i, 1, j  m  1, CTan2i, m, True, CTan1i, j  CTan2i, j  1
The above functions require definitions for 6 variables x, y, XB, YB, S, and theta. Cre
ateVarSets defines the appropriate variable sets.
CreateVarSetslis_ ; MatchQDimensionslis, _, 2 :
Clearx, y, XB, YB, S, theta;
len  Lengthlis;
Withvarlis  TableXBi, YBi, i, len, varlis  lis;
Withvarlis  Tablexi, yi, i, len  1,
varlis  MovingAveragelis, 2;
Withvarlis  TableSi, i, len  1,
varlis  ApplyEuclideanDistance, Partitionlis, 2, 1, 1;
Withvarlis  Tablethetai, i, len  1,
varlis  Partitionlis, 2, 1 . a_List, b_List  ArcTan  b  a;
An important point to note here is that the functions above, being simple functions, are not parallelizable. Hence in
our analysis we can disregard them when looking for parallelization time savings. CreateVarSets could be
parallelized but since we will need those definitions established on all subkernels this is also not useful to attempt
to parallelize.
52
GIST.nb
Finally we combine all of these functions to calculate the pressure coefficients.
PressureCoefficientsSerialpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ :
Modulelen  LengthpnlPts  1, normcoeffs, tancoeffs, rhs, solvec,
cosVec, VelVec, cntrlpts, pnllens, pnlangs, CreateVarSetspnlPts;
normcoeffs  TableANormi, j, len, i, len  1, j, len  1;
rhs  JoinTableSinthetai  , i, len, 0;
solvec  LinearSolvenormcoeffs, rhs;
tancoeffs  TableATani, j, len, i, len, j, len  1;
cosVec  TableCosthetai  , i, len;
VelVec  cosVec  Totaltancoeffs.solvec, 2;
cntrlpts  Tablexi, yi, i, len;
pnllens  TableSi, i, len;
pnlangs  Tablethetai, i, len;
Clearx, y, XB, YB, S, theta; Printsolvec;
Threadcntrlpts, pnlangs, pnllens, VelVec, 1  VelVec ^ 2
Airfoil Wing Design: Airfoil pressure coefficients —serial calculation
& visualization
Now we are ready to run the analysis in serial. We assume a 6° angle of attack. This will take a couple seconds to
run.
pressureCoefficients  PressureCoefficientsSerialpnlPts, 6 °;  AbsoluteTiming
Here are 12 rows of our result.
DisplayTable
SortpressureCoefficients ;; 12, ColumnHeadings 
"Panel midpoints", "Panel angle", "Panel lengths", "Velocity", "Pressure"
To get a visualization the pressure coefficient vectors directly we can define a function called PDVectorPlot
function.
Helper functions to PDVectorPlot.
normgenx1_, y1_, x2_, y2_, scl_ :
Modulemnvec  Meanx1, y1, x2, y2, nrm, nrmvec,
nrm  Normalize1, x2  x1  y1  y2;
Whichx1  x2 && y2  y1, nrmvec  mnvec, mnvec  scl nrm,
x1  x2 && y1  y2, nrmvec  mnvec, mnvec  scl nrm,
x1  x2 && y2  y1, nrmvec  mnvec, mnvec  scl nrm,
x1  x2 && y1  y2, nrmvec  mnvec, mnvec  scl nrm
SurfaceNormalslis_List, scls_List :
MapThreadnormgen1, 2 &, Partitionlis, 2, 1, scls
PDVectorPlotpnlpts_, pres_, ang_, opts : OptionsPattern :
GraphicsArrowheadsSmall, Arrow  SurfaceNormalspnlpts,  Abspres  2,
Polygonpnlpts, Red, ArrowheadsMedium,
ArrowRotationTransformang 1, 0, 1  4, 0, opts
Visualizing the pressure coefficient vectors:
PDVectorPlotpnlPts, pressureCoefficientsAll, 1, 6 °, ImageSize  Large
GIST.nb
53
Airfoil Wing Design: Pressure coefficients —analysis of time usage
There are a number of methods that can be used to determine time usage inside your code. If you have Wolfram
Workbench, perhaps the best method is the Workbench Profiler function. This allows you to run a computation and
get a report on that calculation. The report tells you how many times each subpart of the computation was executed
and how long each of these took.
For this example I will just define a function to get the relevant information we need to
find the bottlenecks inside our code.
timingfunc_, tag_ : PrintNumberFormFirstAbsoluteTimingfunc, 10, tag
SetAttributestiming, HoldAll;
Next we wrap timing around the relevant calculations within our function. As we can’t
parallelize LinearSolve, Total, or Thread, we can omit them in our study.
PressureCoefficientsDiagpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ :
Modulelen  LengthpnlPts  1, normcoeffs, tancoeffs, rhs, solvec,
cosVec, VelVec, cntrlpts, pnllens, pnlangs, CreateVarSetspnlPts;
timingnormcoeffs  TableANormi, j, len, i, len  1, j, len  1;,
"normcoeffs";
timingrhs  JoinTableSinthetai  , i, len, 0, "rhs";
solvec  LinearSolvenormcoeffs, rhs;
timingtancoeffs  TableATani, j, len, i, len, j, len  1, "tancoeffs";
timingcosVec  TableCosthetai  , i, len, "cosVec";
VelVec  cosVec  Totaltancoeffs.solvec, 2;
timingcntrlpts  Tablexi, yi, i, len, "cntrlpts";
timingpnllens  TableSi, i, len, "pnllens";
timingpnlangs  Tablethetai, i, len, "pnlangs";
Clearx, y, XB, YB, S, theta;
Threadcntrlpts, pnlangs, pnllens, VelVec, 1  VelVec ^ 2
PressureCoefficientsDiagpnlPts, 6 °;
Airfoil Wing Design: Parallelizing pressure coefficients
Having found the two bottlenecks in our calculation, we can parallelize the Table commands involved. We also need
to ensure that the arrays x, y, XB, YB, S, and theta are established and distributed. To speed things along we will
also preemptively distribute the definitions of ANorm and ATan. This also ensures that our code is compatible with
Mathematica 7.
54
GIST.nb
PressureCoefficientsParallelpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ :
Modulelen  LengthpnlPts  1, len1  LengthpnlPts, normcoeffs,
tancoeffs, rhs, solvec, cosVec, VelVec, cntrlpts, pnllens, pnlangs,
CreateVarSetspnlPts; DistributeDefinitionsx, y, XB, YB, S, theta,
ANorm, CNorm1, CNorm2, AA, CC, DD, EE, ATan, CTan1, CTan2, PP, QQ, len1;
normcoeffs  ParallelTableANormi, j, len, i, len1, j, len1;
rhs  JoinTableSinthetai  , i, len, 0;
solvec  LinearSolvenormcoeffs, rhs;
tancoeffs  ParallelTableATani, j, len, i, len, j, len1;
cosVec  TableCosthetai  , i, len;
VelVec  cosVec  Totaltancoeffs.solvec, 2;
cntrlpts  Tablexi, yi, i, len;
pnllens  TableSi, i, len;
pnlangs  Tablethetai, i, len;
Clearx, y, XB, YB, S, theta;
Threadcntrlpts, pnlangs, pnllens, VelVec, 1  VelVec ^ 2
pressureCoefficientsParallel  PressureCoefficientsParallelpnlPts, 6 °; 
AbsoluteTiming
Here are 12 rows of our result again.
DisplayTableSortpressureCoefficientsParallel ;; 12, ColumnHeadings 
"Panel midpoints", "Panel angle", "Panel lengths", "Velocity", "Pressure"
Checking our results visually:
PDVectorPlotpnlPts,
pressureCoefficientsParallelAll,  1, 6 °, ImageSize  Large
Further Resources: gridMathematica QuickStart Service
The gridMathematica QuickStart Service is a two-day on-site event designed for organizations with an existing
cluster environment. During this time an experienced Wolfram Research consultant helps get your team up and
running by providing the following:
» Installation and Configuration: Help in installation and configuring gridMathematica software for your
cluster, including a launch notebook for launching kernels on your cluster.
» Training: A half-day long class M225: Parallel Computing with Mathematica.
» Project Specific Support: A half-day of gridMathematica discussion specific to your project, including
parallelization and debugging strategies.
The service is designed for organizations that:
» have purchased or plan to purchase gridMathematica
» are starting to or plan to use gridMathematica for a specific project
» would like to become more effective in using gridMathematica.
For more information on the gridMathematica QuickStart Service, please contact [email protected]
Further Resources: Mathematica
This notebook requires Mathematica 7 or higher to run. You should be able to change the “Getting Started” slide to
the appropriate setting for LaunchKernels for your environment, and then run the notebook as normal. Note that
GIST.nb
55
some cells have been set to be unevaluatable, for example the benchmark cells. If you wish to repeat the benchmarks, simply select the cells and from the Cell menu, choose Cell Properties £ Evaluatable.
» Documentation for parallel functionality: reference.wolfram.com/mathematica/ParallelTools/tutorial/Overview
» Information about Mathematica: www.wolfram.com/products/mathematica
» Information about gridMathematica: www.wolfram.com/products/gridmathematica
To request a trial copy of Mathematica, please stay until the seminar has ended, and you will be given the opportunity to request one as a part of the seminar survey.
If you are interested in evaluating gridMathematica, please contact [email protected].
56
GIST.nb
Mathematica Tech Talk
GPU Computing in Mathematica
What and Why GPU Computing?
What is GPU Computing?
» Graphical card is a piece of hardware devoted to communicating with the CPU and the display.
» Graphical card has a processing unit developed to perform graphics operation fast.
» Graphics cards evolved into devices capable doing general computation fast.
» GPU can be used to perform general computation.
What are CUDA and OpenCL?
» Programming languages/environments that allow one to program the GPU to perform general computation.
» CUDA works only on NVIDIA hardware and is proprietary.
» OpenCL works on AMD or NVIDIA hardware and is an open standard.
Why GPU Computing?
» Only reason to use the GPU is for speed.
» In terms of raw floating point operations power, the GPU beats the CPU.
Why so Fast?
» Speed comes from the hardware design.
» Whereas CPUs have a small number of arithmetic processing units (APU) in favor of larger cache and logic
control, the GPU has very limited logic control units and cache in favor of a large number of APUs.
» Because of the architecture, the CPUs are 2-,4-,6-, or 8-cores whereas the GPU has 256-, 512-, 1024-cores.
GIST.nb
57
What are CUDALink and OpenCLLink?
What are CUDALink and OpenCLLink?
» A way to use the Graphical Processing Unit (GPU) from within Mathematica integrating them with the users’
work flow
» A way to load GPU programs into Mathematica and use them as functions
What are CUDALink and OpenCLLink Not?
» An attempt to make all Mathematica functions utilize the GPU
» Meant to automatically speed up Mathematica code
System Requirements
» OpenCL capable hardware
» Recent video card driver
» For CUDA programming, a supported C compiler is required (VisualStudio or GCC). A C compiler is not needed
for OpenCL programming.
» OpenCLLink supports both NVIDIA and AMD/ATI hardware.
Computing with CUDALink and OpenCLLink
CUDALink contains many built-in functions for performing linear algebra, list processing, image processing, and
Fourier analysis. Those functions require no GPU knowledge, but do require the system to be CUDALink
compatible.
To check if the system is compatible with CUDALink, first load the CUDALink
application.
Needs"CUDALink`"
58
GIST.nb
Once loaded, one can check for CUDALink compatibility by using CUDAQ .
CUDAQ
Information about the detected GPU hardware can be found by using CUDAInformation.
CUDAInformation
Computing with CUDALink and OpenCLLink
Once system compatibility is verified, the built-in CUDALink functions can be used. Here, we multiply two matrices.
1
2
CUDADot 3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3 ,
4
5
0
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1   MatrixForm
1
0
Perform an image convolution on an image.
CUDAImageConvolve
, GaussianMatrix3
A finance operation.
ListPlot3DParallelMapCUDAFinancialDerivative"American", "Put",
"StrikePrice"  80., "Barriers"  100, "Expiration"  ,
"CurrentPrice"  Range30., 130., 1, "InterestRate"  0.06,
"Volatility"  0.45, "Dividend"  0.02, "Rebate"  5. &,
Range0.2, 10, 0.2, DataRange  30, 130, 0.2, 10,
AxesLabel  "Stock", "Time", "Option"
Programming with CUDALink
» CUDALink and OpenCLLink also allow users to load user programs into Mathematica. These functions can then
be used as any Mathematica function and leverage Mathematica’s programming language, visualization
functionality, import/export capabilities, and its many other features.
» Functions are executed using the CUDA or OpenCL execution model.
» The CUDA and OpenCL execution model relies on launching a grid (which is divided into blocks). You load the
function onto the grid and execute that function on different data elements.
GIST.nb
Basic CUDALink Programming
Given the following input,
listSize  1024;
data  RangelistSize;
suppose you want to compute the following:
Forindex  1, index  listSize, index ,
dataindex  dataindex  2

If you translate the above into C, you get the following:
void addTwo(mint * in, mint * out, mint length) {
mint index;
for (index = 0; index < length; index++) {
data[index] = data[index] + 2;
}
}
In CUDA the above C code becomes:
__global__ void addTwo(mint * in, mint * out, mint length) {
int index = threadIdx.x + blockIdx.x*blockDim.x;
data[index] = data[index] + 2;
}
We will use and discuss the meaning of each line next.
Basic CUDALink Programming
This CUDA program adds two to each element in a dataset.
src  "
__global__ void addTwomint  data, mint length 
int index  threadIdx.x  blockIdx.xblockDim.x;
if index  length 
dataindex  dataindex  2;

";
The program is loaded using:
cudaAddTwo  CUDAFunctionLoadsrc, "addTwo", _Integer, _Integer, 16
This defines the input elements.
listSize  64;
data  RangelistSize;
This runs the function on the input, returning the result.
cudaAddTwodata, listSize
Understanding a Basic CUDA Program
On the CUDA source side, the function head is:
59
60
GIST.nb
__global __ void addTwo mint  data, mint length
» __global__ —tells the compiler that this functions will run on the GPU and is callable from the CPU
» void —a __global__ function cannot return a value, so the return type will always be void
» addTwo —is the CUDA “kernel” name
» (mint * data, mint length) —are the function arguments. Here mint is the Mathematica integer
Getting the index:
int index  threadIdx.x  blockIdx.x  blockDim.x;
» int index —defines the index variable as an int
» threadIdx.x —is the offset of the CUDA thread in the x direction
» blockIdx.x —is the offset of the CUDA block in the x direction
» blockDim.x —is the size of the CUDA block in the x direction
if index  length
» makes sure that we do not write into memory what we did not allocate (possible buffer overflow otherwise)
The body of the CUDA function is
dataindex  dataindex  2;
» stores data[index] + 2 into data[index] (i.e., increments data[index] by 2)
Understanding a Basic CUDALink Program
On the Mathematica side, all you need to let CUDALink know is the program source, the function to load, the
argument types, and the block dimensions. Consider the following:
The argument types are similar in syntax to that of Compile. Input lists (or C pointers) are denoted as a list and
then the type, while scalars are denoted by just the type.
So, in our example we had the following CUDA function prototype:
__global __ void addTwo mint  in, mint length
Therefore, our CUDAFunctionLoad command is:
CUDAFunctionLoadsrc, "addTwo", _Integer, _Integer, 256
Programming with OpenCLLink
» Same principles as programming CUDALink.
» Kernel has a change in keywords.
GIST.nb
CUDA
OpenCL
__global__…
__kernel…
…int  f,…
…__global int  f,…
threadIdx
get_local_id
blockIdx
get_group_id
blockDim
get_local_size
threadIdxblockIdxblockDim
get_global_id
__shared__
__local
61
» From the Mathematica point of view, Mathematica provides the same function loading usage.
Why Use CUDALink and OpenCLLink?
Simplified Workflow
Traditional GPU programming requires the user to manage compilation, memory, and
threads.
CUDALink and OpenCLLink simplify the workflow by allowing the user to concentrate
only on the algorithmic part of the program (not the setup part).
This results in a more streamlined workflow.
62
GIST.nb
GPU API Independence
» Support for CUDA and OpenCL out of the box, as well as offering a way to execute commands in parallel on the
CPU makes Mathematica a very attractive platform for HPC development and deployment.
» The syntax of CUDAFunctionLoad and OpenCLFunctionLoad are the same. The user only needs to change the
kernel and calls from CUDA* to OpenCL*.
» Coupled with Mathematica’s symbolic code generation tools, this means that users can write code that executes
code that runs on either CUDA or OpenCL depending on which is available.
Platform Independence
» Mathematica is platform independent and as a result CUDALink and OpenCLLink are as well. The compiler will
automatically detect and use the proper compiler and flags on the system.
» Using CUDALink and OpenCLLink’s abstract types, like Real_t allows the user to use the maximum precision
on the system.
» We also give users the ability to find out whether a kernel is launched on NVIDIA or AMD/ATI hardware. This
allows for lines like the following in the kernel.
#ifdef USING_DOUBLE_PRECISIONQ
#ifdef OPENCLLINK_USING_NVIDIA
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#else
#pragma OPENCL EXTENSION cl_amd_fp64 : enable
#endif
#endif
Access to Mathematica’s Rich Set of Programming Constructs
Users can use dynamics to create animations of data. In this example we write a kernel that performs the game of
life evolution rules.
GIST.nb
63
src  "
__kernel void gol_kernel__global
mint  prev, __global mint  nxt, mint width, mint height 
int xIndex  get_global_id0, yIndex  get_global_id1;
int index  xIndex  yIndexwidth;
int ii, jj, curr, neighbrs;
if xIndex  width && yIndex  height 
curr  previndex;
neighbrs  0;
for ii  1, neighbrs  curr; ii  1; ii 
if xIndex  ii  0 && xIndexii  width 
for jj  1; jj  1; jj 
if yIndexjj  0 && yIndexjj  height
neighbrs  prevxIndex  ii  yIndexjjwidth;



if curr  1
nxtindex  neighbrs  2  neighbrs  3 ? 1 : 0;
else
nxtindex  neighbrs  3 ? 1 : 0;

";
The function is then loaded into Mathematica.
gol  OpenCLFunctionLoadsrc, "gol_kernel",
_Integer, "Input", _Integer, "Output", _Integer, _Integer,
16, 16, "Defines"  "mint"  "int";
This sets the initial state and output state.
initialState  RandomChoice0.7, 0.3  0, 1, 512, 512;
outputState  ConstantArray0, 512, 512;
A dynamic visualization can then be performed.
Dynamic
Refresh
initialState  FirstgolinitialState, outputState, 512, 512;
ArrayPlotinitialState, ImageSize  Medium,
UpdateInterval  1  60


Scalable to Multiple GPUs or Multiple Machines
CUDALink and OpenCLLink are scalable across multiple GPUs using Mathematica’s parallel tools. They are also
scalable across machines using Wolfram Lightweight Grid Mathematica.
Integration into Mathematica
» Integrated into the product with many built-in functions.
64
GIST.nb
» Support for both single and double precision cards.
» Both CUDA and OpenCL capabilities are built in.
GIST.nb
Mathematica Tech Talk
Introducing MathModelica
The MathModelica User Interface
» Model
» Simulate
» Analyze
65
66
GIST.nb
Simulating an Already Existing Model
Simulating an Already Existing Model
Using Your Model in Another Model
GIST.nb
Using Your Model in Another Model
Analyze and design
in Mathematica
MathModelica: Model, Simulate, Analyze
67