1 / 71

Part I: Introductory Materials Introduction to Parallel Computing with R

Part I: Introductory Materials Introduction to Parallel Computing with R. Dr. Nagiza F. Samatova Department of Computer Science North Carolina State University and Computer Science and Mathematics Division Oak Ridge National Laboratory. Analysis algorithms fail for a few gigabytes . .

diata
Download Presentation

Part I: Introductory Materials Introduction to Parallel Computing with R

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Part I: Introductory MaterialsIntroduction to Parallel Computing with R Dr. Nagiza F. Samatova Department of Computer Science North Carolina State University and Computer Science and Mathematics Division Oak Ridge National Laboratory

  2. Analysis algorithms fail for a few gigabytes. Algorithmic Complexity: Calculate means O(n) Calculate FFT O(n log(n)) Calculate SVD O(r • c) Clustering algorithms O(n2) Data size, n Algorithm Complexity n nlog(n) n2 100B 10-10sec. 10-10 sec. 10-8 sec. 10KB 10-8 sec. 10-8 sec. 10-4sec. 1MB 10-6 sec. 10-5 sec. 1 sec. 100MB 10-4 sec. 10-3 sec. 3 hrs 10GB 10-2 sec. 0.1 sec. 3 yrs. For illustration chart assumes 10-12 sec. (1Tflop/sec) calculation time per data point What Analysis Algorithms to Use?The Computer Science & HPC Challenges If n=10GB, then what is O(n) or O(n2) on a teraflop computers? 1GB = 109 bytes 1Tflop = 1012 op/sec

  3. Strategies to @ Computational Challenge • Reduce the amount of data for the algorithm to work on, n • Develop “better” algorithms in terms of big-O • Take advantage of parallel computers with multi-core, multi-GPU, multi-node architectures • Parallel algorithm development • Environments for parallel computing • Optimize end-to-end data analytics pipeline (I/O, data movements, etc.)

  4. Domain Application Layer Fusion Biology Climate Interface Layer Middleware Layer Web Service Dashboard Workflow Automatic Parallelization Scheduling Plug-in Analytics Core Library Layer Streamline Parallel Distributed Data Movement, Storage, Access Layer Data Mover Light Parallel I/O Indexing End-to-End Data Analytics Our focus

  5. What is parallel computing? Why should the user use parallel computing? What are the applications of parallel computing? What techniques can be used to achieve parallelism? What practical issues can arise while using parallel computing? Introduction to parallel computing with R A grid of CPUs http://www.hcs.ufl.edu/~george/sci_torus.gif

  6. The world is parallel • The universe is inherently parallel • Solar system, road traffic, ocean patterns, etc. exhibit parallelism http://cosmicdiary.org/blogs/arif_solmaz/wp-content/uploads/2009/06/solar_system1.jpg http://upload.wikimedia.org/wikipedia/commons/7/7e/Bangkok-sukhumvit-road-traffic-200503.jpg

  7. Parallel computing refers to the use of many computational resources to solve a problem. What is parallel computing? http://www.admin.technion.ac.il/pard/archives/Researchers/ParallelComputing.jpg

  8. Why should parallel computing be used? Parallelism during construction of a building • Solve bigger problems faster • If serial computing is not viable (a large dataset or a a single CPU cannot handle the entire dataset) • Improveme computational efficiency • Save time and money

  9. Weather prediction Computer graphics, networking, etc. Image processing Statistical analysis of financial markets Semantic-based search of web pages Protein folding prediction Cryptography Oil exploration Circuit design and microelectronics Nuclear physics Applications of parallel computing http://www.nasm.si.edu/webimages/640/2006-937_640.jpg http://jeffmohn.files.wordpress.com/2009/04/stock_market_down2.jpg http://bfi-internal.org/dsnews/v8_no11/processing.jpg

  10. Data is broken into a number of subsets. The same instructions are executed simultaneously on different processors for different data subset. Division of problem set: Data parallel Instructions Data

  11. Instructions are broken into a number of independent instructions. Different instructions are executed on the same datasimultaneously on different processors. Division of problem set: Task parallel Instructions Data

  12. EmbarrassinglyParallel Computing • Solving many similar problems • Tasks are independent • Little to no need for coordination between tasks Parallel and Independent

  13. Communication cost is lowered. Highly efficient for large data sets. Little bit of tweaking in code and you are ready to go!! Suitable for MapReduce programming paradigm. Niceties of embarrassing parallelism Autonomous Processing (Minimal Inter-Proc.-Comm.)

  14. Task and Data Parallelism in R Embarrassingly-parallel: Likelihood Maximization Sampling: Bootstrap, Jackknife Markov Chain Monte Carlo Animations Task Parallelism Data Parallelism • Parallel R aims: • to automatically detect and execute task-parallel analyses; • to easily plug-in data-parallelMPI-based C/C++/Fortran codes • to retain high-level of interactivity, productivity and abstraction Task & Data Parallelism in pR Data-parallel: • k-means clustering • Principal Component Analysis • Hierarchical clustering • Distance matrix, histogram

  15. snow (Luke Tierney): general API on top of message passing routines to provide high-level (parallel apply) commands; mostly demonstrated for embarrassingly parallel applications. snow API • Rmpi(Hao Yu): R interface to MPI. > library (pvm) > .PVM.start.pvmd () > .PVM.addhosts (...) > .PVM.config () • rpvm(Na Li and Tony Rossini): R interface to PVM; requires knowledge of parallel programming. Towards Enabling Parallel Computing in Rhttp://cran.cnr.berkeley.edu/web/views/HighPerformanceComputing.html

  16. Parallel Paradigm Hierarchy Parallel Paradigms Explicit Parallelism Implicit Parallelism Rmpi rpvm Task-Parallel Data-Parallel Hybrid: Task + Data Parallel pR taskPR taskPR Intensive Inter-Process Communication No or Limited Inter-Process Communication pRapply pR multicore snow RScaLAPACK

  17. Parallel Paradigm Hierarchy Parallel Paradigms Explicit Parallelism Implicit Parallelism Rmpi rpvm Task-Parallel Data-Parallel Hybrid: Task + Data Parallel pR taskPR taskPR Intensive Inter-Process Communication No or Limited Inter-Process Communication pRapply pR multicore snow RScaLAPACK

  18. APPLY family of functions in R • apply(): Applies a function to sections of array and returns result in array. Structure: apply(array, margin, function, ...) • lapply(): Applies function to each element in a list. • Returns results in a list. Structure: lapply(list, function, ...)

  19. R’s lapply Method is a Natural Candidate for Automatic Parallelization • Examples: Bootstrapping, Monte Carlo, etc. Using R: List Result x = c(1:16); lapply(x, sqrt) fn r1 v1 fn r2 v2 Function fn v3 r3 fn fn v4 r4 R fn r5 v5 fn r6 v6 fn … … fn … … fn vn rn

  20. Existing R Packages with Parallel lapply • multicore • Limited to single-node, multi-core execution • mclapply() • pRapply • Multi-node, multi-core execution • Automatically manages all R dependencies • pRlapply() • snow • Built on Rmpi – uses MPI for communication • Requires users to explicitly manage R dependencies (libraries, variables, functions) • clusterApply()

  21. Function Input/Output, R Environment Using R: • How many inputs in fn()? • What are the inputs to the function? • What are the outputs? • How many outputs? • Will fn() know the value of y? • What cbind() does? • What d is equal to? • How to return more than one output? a=5; y = matrix(1:12,3,4); fn <- function(x){ z = y+x; b = cbind(y,z); } d=fn(a); d; a=5; y = matrix(1:12,3,4); fn <- function(x){ z = y+x; b = cbind(y,z); return(list(z,b)); } d=fn(a); d;

  22. pRapply Example pRlapply(varList, fn, procs=2, cores=2) Using R: Using pRapply: library(abind); x = as.list(1:16); y = matrix(1:12,3,4); fn <- function(x){ z = y+x; w = abind(x,x); b = cbind(y,z); } lapply(x, fn) library(pRapply); library(abind); x = as.list(1:16); y = matrix(1:12,3,4); fn <- function(x){ z = y+x; #w = abind(x,x); b = cbind(y,z); } pRlapply(x, fn) If I run on multiple machines, how non-local host would know about the R environment (e.g., y and abind) created before function call?

  23. snow Example: Explicit Handling of Renv R snow() End-User library(snow); library(abind); x = as.list(1:16); y = matrix(1:12,3,4); fn <- function(x){ z = y+x; w = abind(x,x); b = cbind(y,z); } cl= makeCluster(c(numProcs=4), type = "MPI") clusterApply(cl, x, fn); stopCluster(cl); clusterExport(cl, "y"); clusterEvalQ(cl, library(abind)); Explicitly send libraries, functions, and variables before clusterApply()

  24. pR Automatic Parallelization Uses a 2-Tier Execution Strategy R End-User System R Worker lapply(list, function) C4 C2 C1 C3 MPI R Worker C4 C2 MPI list pR C1 C3 MPI C4 C2 C1 C3 Ci = ith core RWorker

  25. MUTICORE package and mclapply() • Multicore provides a way for parallel computing in R. • Jobs share the entire initial work space. • Provides method for result collection.

  26. Multicore’s mclapply(): • Function mclapply() is the parallelized notion of lapply(). • Takes several arguments in addition to lapply(). • Arguments are used to set up parallel environment. • By default input list is split into as many parts as there are cores. • Returns the result in a list. lapply(Serial)  mclapply(Parallel)

  27. More on mclapply() The conversion of lapply() to mclapply() is relatively very simple. Serial version: myList = as.list(1:100) lapply(myList, sqrt) Parallel version: library(multicore) myList = as.list(1:64) mclapply(myList, sqrt)

  28. Problems using mclapply() with smaller data sets mclapply() is not always faster than lapply() and sometimes is slower. lapply() works well with smaller data sets than mclapply(). Overhead in setting up parallel environment. Distributing work. Collecting results.

  29. Matrix multiplication is more intensive in terms of computations and problem size. Multiplication of two 1024*1024 (A*A) matrices using mclapply() is substantially quicker than lapply(). It is done by splitting rows of the left matrix equally among all the processors. Each matrix then computes local product by multiplying with original matrix A. Results are unlisted into a matrix. M1 M M2 M * M M3 M4 MK Split the matrix M Processor 1 M1 M Processor 2 M2 Processor 3 M3 M M Processor 4 M4 M MK Processor K Join each section to obtain the result M1*M M2*M M3*M M4*M MK*M mclapply() on large and computationally intensive problems

  30. Parallel Paradigm Hierarchy Parallel Paradigms Explicit Parallelism Implicit Parallelism Rmpi rpvm Task-Parallel Data-Parallel Hybrid: Task + Data Parallel pR taskPR taskPR Intensive Inter-Process Communication No or Limited Inter-Process Communication pRapply pR multicore snow RScaLAPACK

  31. What is RScaLAPACK? • Motivation: • Many data analysis routines call linear algebra functions • In R, they are built on top of serial LAPACK library: http://www.netlib. org/lapack • ScaLAPACK: • parallel LAPACK: http://www.netlib. org/scalapack • RScaLAPACK is a wrapper library to ScaLAPACK: • Also allows to link with ATLAS: http://www.netlib.org/atlas

  32. Ex: RScaLAPACK Examples A = matrix(rnorm(256),16,16) b = as.vector(rnorm(16)) Using R: Using RScaLAPACK: solve (A,b) La.svd (A) prcomp (A) library (RScaLAPACK) sla.solve (A,b) sla.svd (A) sla.prcomp (A)

  33. Matrix Multiplication w/ RScaLAPACK • sla.multiply function is used to parallelize matrix multiplication. • sla.multiply(A, B, NPROW, NPCOL, MB, RFLAG, SPAWN) • NPROW and NPCOL allows to split the rows and columns of a matrix, so that it becomes separate blocks. • Each processor will execute each section.

  34. Matrix multiplication w/ RscaLAPACK • Given matrices are divided based on NPROWS and NPCOLS. • Resulting blocks are distributed among the participating processors. • Each processor calculates the product for its allocated block. • Finally, the results are collected. Division of a matrix for matrix multiplication using RscaLAPACK

  35. Matrix multiplication (contd.) • Example • Multiplying two 64 X 64 matrices • Generate two matrices • library(RScaLAPACK) • M1 = matrix(data=rnorm(4096), nrow=64, ncol=64) • M2= matrix(data=rnorm(4096), nrow=64, ncol=64) • Multiplication using sla.multiply • result = sla.multiply(M1, M2, 2, 2, 8, TRUE, TRUE) • class(result) • dim(data.frame(result)) • If there is at least 4 processors, the execution time would be faster than the serial computation • dim(M1 %*% M2).

  36. Currently Supported Functions

  37. Dimension Reduction w/ RscaLAPACK • Multidimensional Dimensional Scaling(MDS) – a technique to place data into Euclidean space in a meaningful way. • Function cmdscalecorresponds to MDS in R. • MDS requires high computation due to which parallelizing will reduce the running time significantly. • Cmdscale has a pair of calls to eigen function to calculate eigenvectors and eigenvalues.

  38. How to convert cmdscale to pcmdscale? cmdscale (Serial)  pcmdscale (Parallel) 1 pcmdscale <- function (d, k = 2, eig = FALSE, 2 add = FALSE, x.ret = FALSE, NPROWS=0, 3 NPCOLS=0, MB=48, RFLAG=1, SPAWN=1) 4 #include options for parallelization 5 { 6 ... 7 if (require("RScaLAPACK", quietly = TRUE)) 8 #parallel eigen function 9 e <- sla.eigen(Z, NPROWS, NPCOLS, MB, 10 RFLAG, SPAWN)$values 11 else 12 #serial eigen function 13 e <- eigen(Z, symmetric = FALSE, 14 only.values = TRUE)$values 15 ... 16 } Open the code for cmdscaleusingfix (cmdscale). Create a new function pcmdscale by writing the code given. Replace all instances of the serial eigen function calls in the code with sla.eigen. require(RScaLAPACK) is to load the RscaLAPCK library

  39. Scalability of pR: RScaLAPACK 8192x8192 Tserial S(p)= Tparallel(p) 4096x4096 2048x2048 1024x1024 R> solve (A,B) pR> sla.solve (A, B, NPROWS, NPCOLS, MB) A,B are input matrices; NPROWS and NPCOLS are process grid specs; MB is block size 116 111 106 99 83 59 Architecture: SGI Altix at CCS of ORNL with 256Intel Itanium2 processors at 1.5 GHz; 8 GB of memory per processor (2 TB system memory); 64-bit Linux OS; 1.5 TeraFLOPs/s theoretical total peak performance.

  40. CRAN R-Project RedHat Linux RPM Available for download from R’s CRAN web site (www.R-Project.org) with 37 mirror sites in 20 countries http://cran.r-project.org/web/ packages/RScaLAPACK/index.html http://rpmfind.net/linux/RPM/RByName.html RedHat and CRAN Distribution

  41. RScaLAPACK Installation • Download RscaLAPACK from R’s CRAN web-site • Installdependency packages: • Install R • MPI (Open MPI, MPICH, LAM MPI) • ScaLAPACK (with the proper MPI distribution) • Setup environment variables export LD_LIBRARY_PATH=<path2deps>/lib:$LD_LIBRARY_PATH • Install RScaLAPACK: • R CMD INSTALL --configure-args="--with-f77 --with-mpi=<MPI install home directory> --with-blacs=<blacs build>/lib --with-blas=<blas build>/lib --with-lapack=<lapack build>/lib --with-scalapack=<scalapack build>/lib" RScaLAPACK_0.6.1.tar.gz

  42. Parallel Paradigm Hierarchy Parallel Paradigms Explicit Parallelism Implicit Parallelism Rmpi rpvm Task-Parallel Data-Parallel Hybrid: Task + Data Parallel pR taskPR taskPR Intensive Inter-Process Communication No or Limited Inter-Process Communication pRapply pR multicore snow RScaLAPACK

  43. Introduction to Rmpi • What is MPI? • Why should we use Rmpi? • Different modes of communication • Point-to-point • Collective • Performance issues http://upload.wikimedia.org/wikipedia/commons/thumb/9/96/NetworkTopologies.png/300px-NetworkTopologies.png

  44. What is MPI? • Message Passing Interface • Allows processors to communicate • Different software implementations: MPICH, OpenMPI, etc. http://financialaliyah.files.wordpress.com/2008/12/whisper.jpg

  45. Advantages/Disadvantages of Rmpi Advantages • Flexible – can use any communication pattern • No C/Fortran required Disadvantages • Complex • Hard to debug • Less efficient than C/Fortran

  46. Using Rmpi • Spawn slaves • mpi.spawn.Rslaves • Distribute data • mpi.bcast.Robj2slave • Do work • mpi.bcast.cmd, mpi.remote.exec • Communication • Collect results • Stop slaves • mpi.close.Rslaves, mpi.quit

  47. Point-to-Point Communication • Message or data passed between two processors • Requires a send and a receive call • Can be synchronous or asynchronous Rmpi functions • mpi.send • mpi.recv • mpi.isend, mpi.irecv, mpi.wait • mpi.send.Robj, mpi.recv.Robj

  48. Synchronous vs. Asynchronous Synchronous (mpi.send, mpi.recv) • Waits until message has been received Asynchronous (mpi.isend, mpi.irecv, mpi.wait) • Starts sending/receiving message • Returns immediately • Can do other work in meantime • Use mpi.wait to synchronize

  49. Collective Communication • Messages or data passed among several processors • Several different communication patterns • All are synchronous Rmpi functions • mpi.barrier • mpi.bcast • mpi.scatter, mpi.scatterv • mpi.gather, mpi.allgather • mpi.reduce, mpi.allreduce

  50. Barrier • Waits until all the processors (procs) call mpi.barrier • Used to synchronize between different parts of an algorithm

More Related