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 Solvea x2 b x c 0, x Solvea 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 Expx In[10]:= x; TabViewWithopts Frame True, PlotStyle Thick, ImageSize Scaled1, AspectRatio 1 3, "Machine Precision" PlotSinf, t, 45, 30, WorkingPrecision MachinePrecision, opts, "High Precision" PlotSinf, 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 NestListFunctionx, 5 x 1 x, 4 , 30 5 » Compare Powerful Computation Engine » Numerical computation: Hybrid symbolic-numeric methods 1 NIntegrate Logx Cos x Logx 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 ParallelMapColorize, 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 ManipulatePlotfk 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 WolframAlpha? æ WolframAlpha 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 Ù Dfx, 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 Solvex2 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 11x^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"; DateListPlotsundata, AspectRatio 1 8, Joined True, Filling 0 WaveletScalogramContinuousWaveletTransformsundataAll, 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 3color, range 2, totalistic code 151117", "RandomInitialConditions", 1, "Content" cellular automaton 3-color, range 2, totalistic code 151117 ArrayPlotCellularAutomaton151 117, 3, 1, 2, SeedRandom4234; RandomInteger2, 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" Ú GridTranspose , solar system planets mass Ù AstronomicalData"Planet", "Mass" Ú PlotEvaluate , Frame All integrate spherical bessel j2 Ù IntegrateSphericalBesselJ2, x, x Ú , integrate spherical bessel j3 Ù IntegrateSphericalBesselJ3, x, x Ú . x a , a, 30, 30, Filling 0 complete graph n Ù CompleteGraphn Ú Manipulate , n, 3, 15, 1 AnimateAppendYellow, SpecularityWhite, 20, Opacity.5, & cube 10-compound Ù PolyhedronData"CubeTenCompound" Ú , Boxed False, ViewPoint Dynamic Coss, Sins, 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 Graphics3DOpacity0.7, Orange, Sphere, Green, ArrayTranslateCone, 2 1, 2, 0 & , 2, 0 double pendulum l1 m1 image q1 ; l2 q2 m2 Headimage Graphics add red dashed gridlines and axes to image add frame caffeine 3D structure image1 ; image2 ; InputFormimage1 Manipulate Show image2 . Spherel_, _ Spherel, r, image1, ImageSize 500 , r, 0, 170 Manipulate Show image2 . RGBColorx_, y_, z_ RGBColorx, y, z, o, GrayLevelx_ GrayLevelx, o, image1, ImageSize 500 , o, 0, 1 Clearimage, 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 ImageAddimg, txt recognize text Ù TextRecognize Ú speak txt » Input interpretation Speaktxt count occurrences of "fish" in txt Input interpretation StringCounttxt, "fish" take first 8 characters of txt Input interpretation StringTaketxt, 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. Headelem1 , elem2 , elem3 , … » Operators as Shorthand All operators in Mathematica are shorthand for an expression, using the function FullForm we can see how + is interpreted. FullForma 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. ab But if we set the variables to an integer, a2 ab Now we see that the definition for a has been used, so we do the same for b. b5 ab 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. PlotSinx, x, 0, 2 AttributesPlot 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 Range10 TableExpandx y ^i, i, 1, 5 » Manipulation and Extraction The Part function (operator is listi ) is the most common way to extract parts of a list. ls Range10 ls3 There is also a span operator. ls2 ;; 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 ; Doa Appenda, i, i, 1, 10 a But when we compare for a larger list, we see a big performance penalty. a ; Doa Appenda, i, i, 1, 20 000; Timing Range20 000; Timing The same story plays out when we want to do operations on a list. Lengtha b ; Dob Appendb, ai ^2, i, Lengtha; 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 Tablei ^ 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 Range1 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. square1n_ : IfEvenQn, n ^2 square1 ls; Timing We can also use a conditional statement square2n_ : n ^ 2 ; EvenQn square2 ls; Timing The third way that we could write this would be to use a predicate on the argument. square3n_ ? 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. oddCubeEvenSquare1n_ : WhichEvenQn, n ^ 2, OddQn, n^ 3 oddCubeEvenSquare1 ls; Timing oddCubeEvenSquare2n_ ? EvenQ : n^ 2; oddCubeEvenSquare2n_ ? 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. Cases3, 4, x, x ^ 2, x ^ 3, _Integer Next we can look at expressions that contain powers of x. Cases3, 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. Cases3, 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. square1n_ : IfEvenQn, n ^2 Means square1 with one expression as an argument, we could easily add more. square1n_, 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 IntersectionwaapAll, 1, waaigAll, 1; pa FlattenPositionwaapAll, 1, & dates ; pg FlattenPositionwaaigAll, 1, & dates; app waappaAll, 2; aigp waaigpgAll, 2; GraphicsRowListLinePlotapp, aigp, PlotLabel RowStyle"Apple ", ColorData11, Style"AIG", ColorData12, Ticks None, Automatic, PairedHistogramapp, aigp, Ticks None, ChartLabels "Apple", "AIG", ImageSize Large Financial Analysis We can now calculate the continuously compounded return on the two stocks: returnsdata_ : LogLast First & Partitiondata, 2, 1; apcr, aigcr returns app, aigp; Labeled GraphicsGridListLinePlotapcr, aigcr, PlotRange All, PlotLabel Row Style"Apple ", ColorData11, Style"AIG", ColorData12, PairedHistogramapcr, 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 Correlationapcr, aigcr We can also visualize the correlation of these two stocks: returns PartitionRiffleapcr, aigcr, 2; lm LinearModelFitreturns, x, x; ShowListPlotreturns, PlotLabel Columnlmx, "R2 " ToString lm"RSquared", Alignment Center, AxesLabel "Apple", "AIG", Plotlmx, 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; portstd2cr_ : Table Sqrtw2 stdc2 1 w2 stds2 2 w 1 w cr stdc stds, w, Range0, 1, 0.001; porter Tablew erc 1 w ers, w, Range0, 1, 0.001; datalcr_ : Transposeportstd2cr, porter; ManipulateListPlotdatalcr, 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 Mean100 apcr; eraig Mean100 aigcr; stdap StandardDeviation100 apcr; stdaig StandardDeviation100 aigcr; craa Correlation100 apcr, 100 aigcr; portstdaa TableSqrtw2 stdap2 1 w2 stdaig2 2 w 1 w craa stdap stdaig, w, Range0, 1, 0.001; porteraa Tablew erap 1 w eraig, w, Range0, 1, 0.001; dataaa Transposeportstdaa, porteraa; ListPlotdataaa, 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 ColorNegateDeleteSmallComponentsBinarizeimg, 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. ComponentMeasurementsMaxDetectDistanceTransformimg2, "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. identifyBlobsimg_, min_, seperation_ : DeleteDuplicatesLast ComponentMeasurementsMaxDetectDistanceTransformDeleteSmallComponents ColorNegateDeleteSmallComponentsBinarizeimg, min, "Centroid", EuclideanDistance1, 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. CreatePaletteDynamicModuleimg Image1, file "", data, Manipulate data identifyBlobsimg, minSize, minSeperation; w, h ImageDimensionsimg; Showimg, GraphicsThickness.001, Cyan, Line0, h l, w, h r, Interface data, Locator, LocatorAutoCreate True, Appearance Style"", 20, White, Button"Import image", IfMatchQ, _Image, img & GIST.nb _ QuietImportfile SystemDialogInput"FileOpen", Method "Queued", Button"generate report", GenerateReportfile, data, h l, h r, w, OpenerView"auto detection settings", Column ControlminSize, 50, "minumum particle size", 0, 200, ControlminSeperation, 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 identifyBlobsimage_, minBlob_, minSeperation_ : DeleteDuplicatesLast ComponentMeasurements MaxDetectDistanceTransformDeleteSmallComponents ColorNegateDeleteSmallComponentsBinarizeimage, minBlob, "Centroid", EuclideanDistance1, 2 minSeperation &; depthx_, y_, w_, l_, r_ : x, l x w r l y; Report generation function GenerateReportpath_, data_, l1_, l2_, w_ : BlockpenetrationData, penetrationData depth, w, l1, l2 & data; IfLengthdata 0, CreateDocument TextCell"Particle impact analysis", "Title", TextCellRow"Sample: ", path, "\n", "Report generated ", DateString, "\n", "Particles found: ", Lengthdata, "Text", ExpressionCellDistributionChartpenetrationDataAll, 1, PlotLabel "Spread profile", BarOrigin Left, "Output", ExpressionCellDistributionChartpenetrationDataAll, 2, PlotLabel "Depth profile", BarOrigin Top, "Output", ExpressionCellListLinePlotMapMean1 &, DeleteCasesBinListspenetrationData, w 25, 10^ 10, , , PlotLabel "Mean depth", "Output", ExpressionCellDensityHistogrampenetrationData, 8, PlotLabel "Density", "Output", TextCell"Particle coordinates", "Section", ExpressionCellSortRoundpenetrationData, "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 ft Cost t2 Sint t 4 x t, 2 Cost 2 Sint 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 q0 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. NDSolveJoineqns . ft 0, x0 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. AnimatePendulumFirst We can combine the above two steps into a handy function that animates the system for any control force f t: SimulatePendulumforce_ : AnimatePendulumFirstNDSolveJoineqns . ft force, x0 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 qt p 2, so let’s try pushing the cart with the force f t p 2 - qt: SimulatePendulum3 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) SimulatePendulum3 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 xt 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: TraditionalFormxt, 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: TraditionalFormHoldFormft k1 xt 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 StateSpaceModeleqns, xt, 0, x't, 0, t, , 't, 0, ft, , 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 xt, x£ t, qt, q£ t from equilibrium. For example, the third column says that, near the equilibrium point, small deviations in qt away from the upright position (qt 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 LQRegulatorGainsNmodel, DiagonalMatrix1, 10, 10, 100, 1 First In the second argument of LQRegulatorGains, we specified the following quadratic “cost” function for the system: TraditionalFormHoldFormcost 1 xt2 10 x t2 10 t2 100 t2 1 ft ^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 qt, which are in turn more important than deviations in xt. 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.xt, x't, t , 't 2 Let’s test it out: SimulatePendulumcontrolforce An Engineering Control Problem Our pendulum stabilized via LQRegulatorGains can survive some quite bumpy conditions: Generate random bumpy conditions: verybumpyt_ SumRandomReal10, 10 Exp10 t RandomInteger152 , 20; 34 GIST.nb Specific verybumpy used in blog post: verybumpyt_ 13.203576835315666` 10 15t 1.4310438601615516` 10 12t 2 2 2.3670864940973857` 10 11t 7.791914458883305` 10 9t 2 2 12.661390107806994` 10 8t 13.39019250013871` 10 7t 2 2 7.873272031334967` 10 6t 6.707528013930826` 10 2t 2 2 2.548183565506953` 10 1t 7.231209749707311` 10 t ; 2 2 Plotverybumpyt, t, 0, 30, PlotRange All, Filling Axis, PlotLabel HoldFormverybumpyt, AxesLabel t SimulatePendulumcontrolforce verybumpyt 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: DateListPlotwsData, 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 wsDataAll, 2 602 ; histplot HistogramwndData, 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 SmoothKernelDistributionwndData; Showhistplot, estplot PlotPDFdist, 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? Probabilityspeed 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 FindDistributionParameterswndData, ExtremeValueDistribution, distEV ExtremeValueDistribution, . params And now let’s test to see if our assumption should be rejected. tstData DistributionFitTestwndData, 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 PlotPDFdistEV, 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: Probabilityx 3.5, x é distEV Let’s visualize the result. PlotEvaluatePDFdistEV, x 1, Boolex 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 ms", FontWeight "Bold", Style"Probability", FontWeight "Bold", Style"Estimated Power MW", FontWeight "Bold", " 3.5", Probabilityx 3.5, x é distEV, 0.0, "Between 3.5 and 7", Probability3.5 x 7, x é distEV, "0 to .8", "Between 7 and 13", Probability7 x 13, x é distEV, ".8 to 1.65", " 13", Probability13 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 Probability3.5 x 7, x é distEV Mean0, 0.8, Probability7 x 13, x é distEV Mean1.65, 0.8, Probability13 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 webcrawlerrooturl_, depth_ : Flatten RestNestListUnionFlattenThread 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 Graphtwdata, VertexLabels Placed"Name", Tooltip Website Analysis Using the HITSCentrality function, find the HITS hub and authority centralities for the graph. MatrixFormHITSCentralityg Link the pages with their HITS centralities. pagesHITS TransposeVertexListg, HITSCentralityg1; TableFormpagesHITS1 ;; 5 We need to sort the pages according to their degree centralities. Here is the top 10 list. sortedpages SortpagesHITS, 12 22 &; TableFormsortedpages1 ;; 10 This shows the subgraph containing all of the top page’s interactions with other pages hyperlinked from twitter.com. HighlightGraphg, NeighborhoodGraphg, sortedpages2, 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 DiscreteWaveletTransformsnd, CDFWavelet WaveletListPlotdwd 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 WaveletThresholddwd, "Soft", .05; How does it sound? InverseWaveletTransformthr 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 ; highlightMazePathpath_ : ShowGraphicsGrayLevel0.2, Rectangle 1., 1., 30., 20., maze, Graphpath, EdgeListPathGraphpath, VertexCoordinates AbsoluteOptionsmaze, VertexCoordinates2, VertexIndexmaze, & path, VertexSize 0, EdgeStyle DirectiveThickness0.01, Red, Opacity1, 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 highlightMazePathFindShortestPathmaze, "in", "out"1 ;; n, n, 2, 307, 1 SystemOpen"maze.mov" We can also use Manipulate to see how the maze is solved. ManipulatehighlightMazePathFindShortestPathmaze, "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 cost 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 PlotCost2 , Exp t .3, t, 0, 3 Create some starting points for Newton’s method. startingvals Tablex, x, 0, 3 , .1; vals MapFindRootCost2 Expt .3, t, 1 &, startingvals; AbsoluteTiming Plot the two curves along with the solutions. Showplot, GraphicsRed, PointSizeMedium, PointMapThreadt . 1, 2 &, vals, Cost2 . vals Root Finding: Root finding in parallel Here is a parallel approach to the problem, achieved by replacing Map with ParallelMap. vals FindRootCost2 Exp t .3, t, 1 & startingvals; AbsoluteTiming pvals ParallelMapFindRootCost2 Expt .3, t, 1 &, startingvals; AbsoluteTiming Showplot, GraphicsRed, PointSizeMedium, PointMapThreadt . 1, 2 &, pvals, Cost2 . 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 ParallelMapFindRootCost2 Exp t .3, t, 1 &, startingvals; AbsoluteTiming TableFormTraceList, 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. Solvepi 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 Selectpts, Norm1 r & Graphics EdgeFormThick, White, Rectangle .5, .5, Black, Circle0, 0, .5, Red, Pointinsidepts, Black, PointComplementpts, insidepts Apply the formula, but we get a bad approximation. Lengthinsidepts N Lengthpts r2 Use more points. pts RandomReal r, r, 50 000, 2; insidepts Selectpts, Norm1 r &; Lengthinsidepts Lengthpts 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 DistributeDefinitionsr pts ParallelTableRandomReal r, r, 25 000, 2, LengthKernels; insidepts ParallelMapSelect, Norm r & &, pts; The different colors represent different kernels. Each kernel has a unique set of random points. Withlen Lengthpts, MapIndexedGraphics EdgeFormThick, White, Rectangle r, r, Black, Circle0, 0, r, Hue21 len, PointSizeSmall, Point11, LighterHue21 len, .5, PointComplement11, 12, ImageSize 225 &, Transposepts, insidepts Combine results and calculate p! DistributeDefinitionspts, insidepts; NMeanParallelTable Lengthinsideptsi Lengthptsi r^ 2, i, LengthKernels We can optimize our routine a little bit and compute p using 10 million points. AbsoluteTimingWith NUMPOINTS 10 000 000, CPUS LengthKernels, partitions QuotientNUMPOINTS, CPUS Join Table1, ModNUMPOINTS, CPUS, Table0, CPUS ModNUMPOINTS, CPUS; NTotal ParallelMap TotalRound TotalRandomReal1 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 Range10 000 000, 80 000 000, 1 000 000; pts TableAbsoluteTimingWith NUMPOINTS max, CPUS LengthKernels, partitions QuotientNUMPOINTS, CPUS JoinTable1, ModNUMPOINTS, CPUS, Table0, CPUS ModNUMPOINTS, CPUS; NTotal ParallelMap TotalRound TotalRandomReal 1 2, 1 2, , 2^ 2, 2 .25 &, partitions NUMPOINTS 1 1 2 ^ 2, max, maxpoints; 44 GIST.nb ListLinePlotMapThread1, 2 &, maxpoints, ptsAll, 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 Compilec, _Complex, LengthFixedPointList2 c &, c, 50, SameTest Abs2 2.0 & Compute the data using Table. AbsoluteTimingdataSerial TableMandelbrotFunctionx y , x, 2, 0.5, 0.002, y, 1, 1, 0.002; We can use ArrayPlot to visualize the data. ArrayPlotTranspose 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. DistributeDefinitionsMandelbrotFunction; AbsoluteTimingdataParallel ParallelTable MandelbrotFunctionx 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. DistributeDefinitionsMandelbrotFunction; AbsoluteTiming dataParallelC ParallelTableMandelbrotFunctionx y , x, 2, 0.5, 0.002, y, 1, 1, 0.002, Method "CoarsestGrained"; DistributeDefinitionsMandelbrotFunction; AbsoluteTiming dataParallelF ParallelTableMandelbrotFunctionx 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. ArrayPlotTransposedataParallel, 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 StandardDeviationFinancialData"msft", "Jan. 1, 2008" All, 2 FinancialData"msft", "Jan. 1, 2008"1, 2; googvolatility StandardDeviationFinancialData"goog", "Jan. 1, 2008" All, 2 FinancialData"goog", "Jan. 1, 2008"1, 2; msftstrikevalues Tablex, x, 10, 50, 2.5 googstrikevalues Tablex, x, 270, 660, 10; Try the function out on calls and puts for Microsoft across the entire strike price range. msftoptionsprices MapReplacePartbinomialprice28.69, msftvolatility, 5 252, .06869, 5 365, 11, , callvalue1, 1 , ReplacePartbinomialprice28.69, msftvolatility, 5 252, .06869, 5 365, 11, , putvalue1, 1 &, msftstrikevalues AbsoluteTiming To run this in parallel, the worker kernels must have all of the function definitions, not just the ones explicitly specified. DistributeDefinitionsbinomialprice, up, down, oneup, f, msftvolatility, googvolatility, probabilityup, callvalue, putvalue msftoptionsprices ParallelMapReplacePartbinomialprice28.69, msftvolatility, 5 252, .06869, 5 365, 11, , callvalue1, 1 , ReplacePartbinomialprice28.69, msftvolatility, 5 252, .06869, 5 365, 11, , putvalue1, 1 &, msftstrikevalues AbsoluteTiming Now we’ll do calls and puts for Google over a much larger range of strike values: googoptionsprices ParallelMapReplacePartbinomialprice540.3, googvolatility, 5 252, .06869, 5 365, 14, , callvalue1, 1 , ReplacePartbinomialprice540.3, googvolatility, 5 252, .06869, 5 365, 14, , putvalue1, 1 &, googstrikevalues; AbsoluteTiming 46 GIST.nb ListLinePlotgoogoptionspricesAll, 112 ;; 31, Frame True, FrameLabel "Strike", "Price", PlotLabel "Market vs. Binomial price GOOG CALLs" ListLinePlotgoogoptionspricesAll, 212 ;; 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. Withpicrogenes 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. ParallelEvaluatepicro1, Kernels1 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 ParallelMapFunctiongene, MinEditDistancegene, & acido, volcaniumgenes1 ;; 8; AbsoluteTiming acidovolcanium Do the same for the first eight genes in thermoplasma volcanium and picrophilus torridus. picrovolcanium ParallelMapFunctiongene, MinEditDistancegene, & picro, volcaniumgenes1 ;; 8; AbsoluteTiming Compute the mean edit distance for the eight genes between acidophilum and volcanium vs. torridus and volcanium. Meanacidovolcanium N Meanpicrovolcanium N ListLinePlotacidovolcanium, 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; TreePlottree, 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 TableAbsoluteTiming ParallelMapFunctiongene, MinEditDistancegene, & acido, volcaniumgenes1 ;; x, x, 8, 56, 2; The number of gene computations vs. time to compute. Notice that there are some stepwise properties. ListLinePlot MapThread1, 2 &, Tablex, x, 8, 56, 2, acidovolcaniumtimingAll, 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 MapParallelSubmitf; Pause1 &, Tabley, y, 16 WaitAllpids 48 GIST.nb Before, it took a combined 100 seconds to compute this. acidovolcanium MapFunctiongene, ParallelSubmitMinEditDistancegene, & acido, volcaniumgenes1 ;; 8; picrovolcanium MapFunctiongene, ParallelSubmit MinEditDistancegene, & picro, volcaniumgenes1 ;; 8; acidovolcanium; picrovolcanium; acidovolcaniumresult WaitAllacidovolcanium; picrovolcaniumresult WaitAllpicrovolcanium; AbsoluteTiming Of course it’s important we get the same results! Meanacidovolcaniumresult N Meanpicrovolcaniumresult N Genomic Data: Comparing organisms —benchmarking ParallelSubmit and WaitAll Again some benchmarking. acidovolcaniumtiming TableAbsoluteTimingpids MapFunctiongene, ParallelSubmitMinEditDistancegene, & acido, volcaniumgenes1 ;; x; WaitAllpids;, x, 8, 56, 2; ListLinePlot MapThread1, 2 &, Tablex, x, 8, 56, 2, acidovolcaniumtimingAll, 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. Dimensionspredata 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. DimensionsRestpredata Visualizing the data using ListLinePlot. ListLinePlotRestpredata, 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. ParameterizeAirfoilarry_ ? MatrixQ : Moduleindxed MapIndexedJoin2, 1 &, arry, xfun, yfun, xfun InterpolationindxedAll, ;; 2, Method "Spline"; yfun InterpolationindxedAll, 1 ;; 3 ;; 2, Method "Spline"; xfun, yfun nacaChrd, nacaCam ParameterizeAirfoilRestpredata Visualizing the parametrization. Note the parametrization traverses the upper camber first. ManipulateShowParametricPlotnacaChrdt, nacaCamt, t, 1, 35, PlotRangePadding Scaled.03, Graphics Red, PointSizeLarge, PointnacaChrdi, nacaCami, i, 1, 35 The next step in the process is to create boundary points for the panels we will use. MakePointSetlis1_ ; VectorQlis1, NumericQ, lis2_ ; VectorQlis2, IntegerQ ; Lengthlis1 Lengthlis2 1 : Modulediffs Differenceslis1, stps, len, pts, stps diffs lis2; len Lengthdiffs; pts SortJoinFlattenTablej, i, 1, len, j, lis1i, lis1i diffsi stpsi, stpsi, 1, lis1 1; JoinReversepts, Restpts PanelPoints fn1_InterpolatingFunction, fn2_InterpolatingFunction, ptslis_List : ReverseWithmx fn11, 1, 2, len Lengthptslis, TableIfi Floorlen 2, fn1t, fn2t . FindRootfn1t ptslisi, t, mx 4, fn1t, fn2t . QuietFindRootfn1t ptslisi, 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 MakePointSet0, .05, .13, .18, .75, 1., 20, 14, 9, 6, 5; Next we create the panel boundaries. ShortpnlPts PanelPointsnacaChrd, nacaCam, pts, 6 Visualizing the pnlPts set. GraphicsGray, PolygonpnlPts 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. CNorm1i_, j_ : Whichi j, 1, True, DDi, j FFi, j 2 CCi, j GGi, j CNorm2i, j CNorm2i_, j_ : Whichi j, 1, True, DDi, j QQi, j FFi, j 2 Sj AAi, j CCi, j DDi, j EEi, j GGi, j Sj ANormi_, j_, m_ : Whichi m 1 && j 1, 1, i m 1 && j m 1, 1, i m 1, 0, j 1, CNorm1i, 1, j m 1, CNorm2i, m, True, CNorm1i, j CNorm2i, 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. AAi_, j_ : xi XBj Costhetaj yi YBj Sinthetaj BBi_, j_ : SquaredEuclideanDistancexi, yi, XBj, YBj CCi_, j_ : Sinthetai thetaj DDi_, j_ : Costhetai thetaj EEi_, j_ : xi XBj Sinthetaj yi YBj Costhetaj FFi_, j_ : Log1 Sj^ 2 2 AAi, j Sj BBi, j GGi_, j_ : ArcTanBBi, j AAi, j Sj, EEi, j Sj PPi_, j_ : xi XBj Sinthetai 2 thetaj yi YBj Costhetai 2 thetaj QQi_, j_ : xi XBj Costhetai 2 thetaj yi YBj Sinthetai 2 thetaj Functions for calculating tangential velocity coefficients. CTan1i_, j_ : Whichi j, 2, True, CCi, j FFi, j 2 DDi, j GGi, j CTan2i, j CTan2i_, j_ : Whichi j, 2, True, CCi, j PPi, j FFi, j 2 Sj AAi, j DDi, j CCi, j EEi, j GGi, j Sj ATani_, j_, m_ : Whichj 1, CTan1i, 1, j m 1, CTan2i, m, True, CTan1i, j CTan2i, j 1 The above functions require definitions for 6 variables x, y, XB, YB, S, and theta. Cre ateVarSets defines the appropriate variable sets. CreateVarSetslis_ ; MatchQDimensionslis, _, 2 : Clearx, y, XB, YB, S, theta; len Lengthlis; Withvarlis TableXBi, YBi, i, len, varlis lis; Withvarlis Tablexi, yi, i, len 1, varlis MovingAveragelis, 2; Withvarlis TableSi, i, len 1, varlis ApplyEuclideanDistance, Partitionlis, 2, 1, 1; Withvarlis Tablethetai, i, len 1, varlis Partitionlis, 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. PressureCoefficientsSerialpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ : Modulelen LengthpnlPts 1, normcoeffs, tancoeffs, rhs, solvec, cosVec, VelVec, cntrlpts, pnllens, pnlangs, CreateVarSetspnlPts; normcoeffs TableANormi, j, len, i, len 1, j, len 1; rhs JoinTableSinthetai , i, len, 0; solvec LinearSolvenormcoeffs, rhs; tancoeffs TableATani, j, len, i, len, j, len 1; cosVec TableCosthetai , i, len; VelVec cosVec Totaltancoeffs.solvec, 2; cntrlpts Tablexi, yi, i, len; pnllens TableSi, i, len; pnlangs Tablethetai, i, len; Clearx, y, XB, YB, S, theta; Printsolvec; Threadcntrlpts, 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 PressureCoefficientsSerialpnlPts, 6 °; AbsoluteTiming Here are 12 rows of our result. DisplayTable SortpressureCoefficients ;; 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. normgenx1_, y1_, x2_, y2_, scl_ : Modulemnvec Meanx1, y1, x2, y2, nrm, nrmvec, nrm Normalize1, x2 x1 y1 y2; Whichx1 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 SurfaceNormalslis_List, scls_List : MapThreadnormgen1, 2 &, Partitionlis, 2, 1, scls PDVectorPlotpnlpts_, pres_, ang_, opts : OptionsPattern : GraphicsArrowheadsSmall, Arrow SurfaceNormalspnlpts, Abspres 2, Polygonpnlpts, Red, ArrowheadsMedium, ArrowRotationTransformang 1, 0, 1 4, 0, opts Visualizing the pressure coefficient vectors: PDVectorPlotpnlPts, pressureCoefficientsAll, 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. timingfunc_, tag_ : PrintNumberFormFirstAbsoluteTimingfunc, 10, tag SetAttributestiming, 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. PressureCoefficientsDiagpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ : Modulelen LengthpnlPts 1, normcoeffs, tancoeffs, rhs, solvec, cosVec, VelVec, cntrlpts, pnllens, pnlangs, CreateVarSetspnlPts; timingnormcoeffs TableANormi, j, len, i, len 1, j, len 1;, "normcoeffs"; timingrhs JoinTableSinthetai , i, len, 0, "rhs"; solvec LinearSolvenormcoeffs, rhs; timingtancoeffs TableATani, j, len, i, len, j, len 1, "tancoeffs"; timingcosVec TableCosthetai , i, len, "cosVec"; VelVec cosVec Totaltancoeffs.solvec, 2; timingcntrlpts Tablexi, yi, i, len, "cntrlpts"; timingpnllens TableSi, i, len, "pnllens"; timingpnlangs Tablethetai, i, len, "pnlangs"; Clearx, y, XB, YB, S, theta; Threadcntrlpts, pnlangs, pnllens, VelVec, 1 VelVec ^ 2 PressureCoefficientsDiagpnlPts, 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 PressureCoefficientsParallelpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ : Modulelen LengthpnlPts 1, len1 LengthpnlPts, normcoeffs, tancoeffs, rhs, solvec, cosVec, VelVec, cntrlpts, pnllens, pnlangs, CreateVarSetspnlPts; DistributeDefinitionsx, y, XB, YB, S, theta, ANorm, CNorm1, CNorm2, AA, CC, DD, EE, ATan, CTan1, CTan2, PP, QQ, len1; normcoeffs ParallelTableANormi, j, len, i, len1, j, len1; rhs JoinTableSinthetai , i, len, 0; solvec LinearSolvenormcoeffs, rhs; tancoeffs ParallelTableATani, j, len, i, len, j, len1; cosVec TableCosthetai , i, len; VelVec cosVec Totaltancoeffs.solvec, 2; cntrlpts Tablexi, yi, i, len; pnllens TableSi, i, len; pnlangs Tablethetai, i, len; Clearx, y, XB, YB, S, theta; Threadcntrlpts, pnlangs, pnllens, VelVec, 1 VelVec ^ 2 pressureCoefficientsParallel PressureCoefficientsParallelpnlPts, 6 °; AbsoluteTiming Here are 12 rows of our result again. DisplayTableSortpressureCoefficientsParallel ;; 12, ColumnHeadings "Panel midpoints", "Panel angle", "Panel lengths", "Velocity", "Pressure" Checking our results visually: PDVectorPlotpnlPts, pressureCoefficientsParallelAll, 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 , GaussianMatrix3 A finance operation. ListPlot3DParallelMapCUDAFinancialDerivative"American", "Put", "StrikePrice" 80., "Barriers" 100, "Expiration" , "CurrentPrice" Range30., 130., 1, "InterestRate" 0.06, "Volatility" 0.45, "Dividend" 0.02, "Rebate" 5. &, Range0.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 RangelistSize; suppose you want to compute the following: Forindex 1, index listSize, index , dataindex dataindex 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 addTwomint data, mint length int index threadIdx.x blockIdx.xblockDim.x; if index length dataindex dataindex 2; "; The program is loaded using: cudaAddTwo CUDAFunctionLoadsrc, "addTwo", _Integer, _Integer, 16 This defines the input elements. listSize 64; data RangelistSize; This runs the function on the input, returning the result. cudaAddTwodata, 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 dataindex dataindex 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: CUDAFunctionLoadsrc, "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 threadIdxblockIdxblockDim 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_id0, yIndex get_global_id1; int index xIndex yIndexwidth; int ii, jj, curr, neighbrs; if xIndex width && yIndex height curr previndex; neighbrs 0; for ii 1, neighbrs curr; ii 1; ii if xIndex ii 0 && xIndexii width for jj 1; jj 1; jj if yIndexjj 0 && yIndexjj height neighbrs prevxIndex ii yIndexjjwidth; if curr 1 nxtindex neighbrs 2 neighbrs 3 ? 1 : 0; else nxtindex neighbrs 3 ? 1 : 0; "; The function is then loaded into Mathematica. gol OpenCLFunctionLoadsrc, "gol_kernel", _Integer, "Input", _Integer, "Output", _Integer, _Integer, 16, 16, "Defines" "mint" "int"; This sets the initial state and output state. initialState RandomChoice0.7, 0.3 0, 1, 512, 512; outputState ConstantArray0, 512, 512; A dynamic visualization can then be performed. Dynamic Refresh initialState FirstgolinitialState, outputState, 512, 512; ArrayPlotinitialState, 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