Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE...

46
Parallel Computing with Mathematica UVACSE Short Course E Hall 1 1 University of Virginia Alliance for Computational Science and Engineering [email protected] October 8, 2014 (UVACSE) October 8, 2014 1 / 46

Transcript of Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE...

Page 1: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with MathematicaUVACSE Short Course

E Hall1

1University of Virginia Alliance for Computational Science and [email protected]

October 8, 2014

(UVACSE) October 8, 2014 1 / 46

Page 2: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Outline

1 NX Client for Remote Linux Desktop

2 Parallel Computing with Mathematica

3 Parallel Computing on the Linux Cluster

4 References

(UVACSE) October 8, 2014 2 / 46

Page 3: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

NX Client for Remote Linux Desktop

Installing and Configuring NX Client

The NX client provides a Gnome Linux desktop interface to the loginnode of the fir.itc Linux cluster.

http://uvacse.virginia.edu/resources/itc-linux-cluster-overview/the-nx-client/

(UVACSE) October 8, 2014 3 / 46

Page 4: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

NX Client for Remote Linux Desktop

Starting and Configuring NX Client

Once logged into fir.itc.virginia.edu through NX

Open a terminal from Applications/System Tools/Terminal menuSelect and right-click on Terminal to add to launcher

Create a mathematica directory with mkdir commandStart web browser from icon at top of desktop

(UVACSE) October 8, 2014 4 / 46

Page 5: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

NX Client for Remote Linux Desktop

Download Short Course Examples

Download the short-course materials fromhttp://www.uvacse.virginia.edu/software/mathematica-at-uva/

Follow the links,→Parallel Mathematica→Parallel Mathematica Short Courseand download 3 files to mathematica directory you create withmkdir command

ClassExamples_Fa14.zipmathematica-parallel_Fa14.pdf

(UVACSE) October 8, 2014 5 / 46

Page 6: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Solving Big Technical Problems

Computationally intensive, long-running codes

Run tasks in parallel on many processorsTask parallel

Large Data sets

Load data across multiple machines that work in parallelData parallel

(UVACSE) October 8, 2014 6 / 46

Page 7: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Computing with Mathematica

Parallel computing in Mathematica is based on launching andcontrolling multiple Mathematica kernel (worker) processes from withina single master Mathematica, providing a distributed-memoryenvironment for parallel programming.

Tested on Unix, Linux, Windows, and Macintosh platforms and are wellsuited to working locally on a multi-core machine or in a cluster ofmachines,

Parallel computing is now provided as a standard part of Mathematica.

(UVACSE) October 8, 2014 7 / 46

Page 8: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Computing with Mathematica

To perform computations in parallel, you need to be able to perform thefollowing tasks:

start processes and wait for processes to finishschedule processes on available processorsexchange data between processes and synchronize access tocommon resources

In the Mathematica environment, the term processor refers to arunning Mathematica kernel, whereas a process is an expression to beevaluated.

(UVACSE) October 8, 2014 8 / 46

Page 9: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Connection Methods

Mathematica can run parallel workers locally on the same machine orremotely on a compute cluster controlled by a resource managementapplication, e.g. PBSPro.

Local KernelsThe Local Kernels connection method is used to run parallelworkers on the same computer as the master Mathematica. It issuitable for a multi-core environment, and is the easiest way to getup and running with parallel computation.

Cluster IntegrationThe Cluster Integration connection method is used to run parallelworkers on a compute cluster from the master Mathematicaprocess. It integrates with the PBSPro cluster managementsoftware.

(UVACSE) October 8, 2014 9 / 46

Page 10: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Computing Functions

mathematica/guide/ParallelComputing.html

(UVACSE) October 8, 2014 10 / 46

Page 11: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Computing Functions

(UVACSE) October 8, 2014 11 / 46

Page 12: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Computing Functions

(UVACSE) October 8, 2014 12 / 46

Page 13: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Computing Features

Mathematica supports both task and data parallelism.

The main features of parallel computing in Mathematica are:

distributed memory, master/slave parallelismwritten in Mathematica, machine independentMathLink communication with remote kernelsexchange of symbolic expressions and programs with remotekernels, not only numbers and arraysvirtual process scheduling or explicit process distribution toavailable processorsvirtual shared memory, synchronization, lockingparallel functional programming and automatic parallelizationsupport

(UVACSE) October 8, 2014 13 / 46

Page 14: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Programming Parallel Applications in Mathematica

To demonstrate demonstrate parallel processing in Mathematica, weuse ParallelEvaluate. If this is the first parallel computation, it willlaunch the configured parallel kernels.

(UVACSE) October 8, 2014 14 / 46

Page 15: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Kernels Status Monitor

You can open the Parallel Kernels Status monitor through theEvaluation > Parallel Kernel Status menu selection.

(UVACSE) October 8, 2014 15 / 46

Page 16: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Preferences

The default settings of Mathematica automatically configure a numberof parallel kernels to use for parallel computation, as seen through theEvaluation > Parallel Kernel Configuration menu selection.

(UVACSE) October 8, 2014 16 / 46

Page 17: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Parallel Preferences

(UVACSE) October 8, 2014 17 / 46

Page 18: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Launching and Connecting

Mathematica launches parallel kernels automatically as they areneeded, but you can also launch kernels manually with the commandLaunchKernels. This is be useful if you were running in a batchmode.

Local Kernels

The Local Kernels connection method supports launching localkernels directly from LaunchKernels by passing it an integer(setting the number of kernels)

(UVACSE) October 8, 2014 18 / 46

Page 19: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Launching and Connecting

Cluster Integration configuration done through Parallel Preferences

(UVACSE) October 8, 2014 19 / 46

Page 20: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Launching and Connecting

Cluster Integration configuration done through Parallel Preferences

(UVACSE) October 8, 2014 20 / 46

Page 21: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Launching and Connecting

Cluster Integration

The Cluster Integration connection method is used to run parallelworkers on the compute nodes of a cluster from the masterMathematica.

The Cluster Integration connection method supports launchingkernels directly from LaunchKernels. To do so you must firstload the ClusterIntegration‘ package, this is shown below.

To launch on a particular cluster you have to pass the name forthat cluster into LaunchKernels.

(UVACSE) October 8, 2014 21 / 46

Page 22: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Launching and Connecting

Cluster Integration

(UVACSE) October 8, 2014 22 / 46

Page 23: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Sending Commands to Remote Kernels

Values of variables defined on the local master kernel are usually notavailable to remote kernels.

If you need variable values and definitions carried over to the remotekernels, use DistributeDefinitions or shared variables.

(UVACSE) October 8, 2014 23 / 46

Page 24: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Sending Commands to Remote Kernels

Recall that connections to remote kernels, as opened byLaunchKernels, are represented as kernel objects. The commandsbelow take parallel kernels as arguments and use them to carry outcomputations.

(UVACSE) October 8, 2014 24 / 46

Page 25: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Remote Definitions

Mathematica contains a command DistributeDefinitions thatmakes it easy to transport local variables and function definitions to allparallel kernels.

Higher-level parallel commands, such as Parallelize,ParallelTable, ParallelSum, ... will automatically distributedefinitions of symbols occurring in their arguments.

(UVACSE) October 8, 2014 25 / 46

Page 26: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Sending Commands to Remote Kernels

(UVACSE) October 8, 2014 26 / 46

Page 27: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

(UVACSE) October 8, 2014 27 / 46

Page 28: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

(UVACSE) October 8, 2014 28 / 46

Page 29: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

(UVACSE) October 8, 2014 29 / 46

Page 30: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

(UVACSE) October 8, 2014 30 / 46

Page 31: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

(UVACSE) October 8, 2014 31 / 46

Page 32: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Automatically Parallelizing Existing Serial Expressions

Use Parallelize to have Mathematica decide how to distribute workacross multiple kernels.

(UVACSE) October 8, 2014 32 / 46

Page 33: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Automatically Parallelizing Existing Serial Expressions

There is a natural trade-off in parallelization between controlling theoverhead of splitting a problem or keeping all the cores busy.

(UVACSE) October 8, 2014 33 / 46

Page 34: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Sending a Command to Multiple Kernels

Use ParallelEvaluate to send commands to multiple kernels andwait for completion. Use With to bind locally defined variables beforedistribution.

(UVACSE) October 8, 2014 34 / 46

Page 35: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Implementing Task-Parallel algorithms

(UVACSE) October 8, 2014 35 / 46

Page 36: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Implementing Data-Parallel algorithms

ParallelMap is a natural way to introduce parallelism using afunctional programming style. When you have a computationallyexpensive function to execute of a large data set, Mathematica canexecute the operations in parallel by splitting the mapping amongmultiple kernels.

(UVACSE) October 8, 2014 36 / 46

Page 37: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Decomposing a Problem in Parallel Data Sets

Estimating Pi Using Monte Carlo Simulation: Serial and Parallel.

(UVACSE) October 8, 2014 37 / 46

Page 38: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Decomposing a Problem in Parallel Data Sets

Graphic of work distribution among the 4 kernels.

(UVACSE) October 8, 2014 38 / 46

Page 39: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Decomposing a Problem in Parallel Data Sets

You can generate multiple data sets in parallel, then plot or processthem further.

(UVACSE) October 8, 2014 39 / 46

Page 40: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Choosing an Appropriate Distribution Method

The parallel primitives Parallelize, ParallelMap,ParallelTable, ParallelDo, ParallelSum, andParallelCombine support an option called Method

It allows you to specify the granularity of the subdivisions used todistribute the computation across kernels.

Use Method → "FinestGrained" when the completion time ofeach atomic unit of computation is expected to vary widely.

Use Method → "CoarseGrained" when the completion time ofeach atomic unit of computation is expected to be uniform.

(UVACSE) October 8, 2014 40 / 46

Page 41: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Virtual Shared Memory

Virtual shared memory is a programming model that allows processorson a distributed-memory machine to be programmed as if they hadshared memory. A software layer takes care of the necessarycommunication in a transparent way.

Mathematica provides functions that implement virtual shared memoryfor these remote kernels.

The drawback of a shared variable is that every access for read orwrite requires communication over the network, so it is slower thanaccess to a local unshared variable.

Declaring shared variables and functions.

(UVACSE) October 8, 2014 41 / 46

Page 42: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing with Mathematica

Mathematica Parallel Workflow

The toolbox enables application prototyping on the desktop with up to16 local workers (left), and with the Mathematica Cluster Integrationpackage(right), applications can be scaled to multiple computers on acluster (subsitute Mathematica for Matlab in figure below).

(UVACSE) October 8, 2014 42 / 46

Page 43: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing on the Linux Cluster

Scaling Up from the Desktop

Mathematica’s parallel computing provides the ability to use up to 16local kernels on a multicore or multiprocessor computer.

When used together with Cluster Integration package, you can scaleup your application to use any number of kernels running on anynumber of computers.

ITS Linux cluster allows for 128 kernels.

Alternatively, you can run up to 16 kernels on a single multi-corecompute node of the cluster.

(UVACSE) October 8, 2014 43 / 46

Page 44: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing on the Linux Cluster

Running Mathematica on Cluster Front-end Node

Mathematica Parallel Computing jobs can be submitted to the ITCLinux cluster by first logging onto the cluster front-end nodefir.itc.virginia.edu using the NX client.

Start up Mathematica from a Linux desktop terminal window.

Parallel Mathematica jobs can be submitted from with the Mathematicanotebook interface as well as using PBS command files and theexample scripts show how to setup and submit the jobs

Documentation: Submitting Mathematica Parallel Jobs

(UVACSE) October 8, 2014 44 / 46

Page 45: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

Parallel Computing on the Linux Cluster

Example Mathematica Scripts

(UVACSE) October 8, 2014 45 / 46

Page 46: Parallel Computing with Mathematica UVACSE Short Course...Parallel Computing with Mathematica UVACSE Short Course E Hall1 1University of Virginia Alliance for Computational Science

References

References

1 Parallel Computing Tools User Guidereference.wolfram.com/mathematica/ParallelTools/tutorial/Overview.html

2 Parallel Computing: Mathematica Documentationreference.wolfram.com/mathematica/guide/ParallelComputing.html

3 Mathematica Cookbook, by Sal Mangano, O’Reilly Press.

Need further help? Email [email protected].

(UVACSE) October 8, 2014 46 / 46