SlideShare a Scribd company logo
GPars
               Parallelism the Right Way

                                 Dr Russel Winder

                                   It’z Interactive Ltd
                                   russel@itzinteractive.com
                                   russel@russel.org.uk
                                   @russel_winder
Copyright © 2011 Russel Winder                                 1
Moore's Law

                       ●    More transistors.
                       ●    More cores.
                       ●    More parallelism.
                       ●    Ubiqitous parallelism.




Copyright © 2011 Russel Winder                       2
Caveat

                       ●    Not all applications require parallelism.
                       ●    I/O bound best handled with single threaded event
                            loop.




Copyright © 2011 Russel Winder                                                  3
Where Parallelism Is Needed

                       ●    Operating systems offer threads as access to the
                            cores.
                       ●    Languages offer threads as infrastructure.




Copyright © 2011 Russel Winder                                                 4
Hummm...

                       ●    Shared memory multithreading is hard.
                       ●    Locks, semaphores, monitors, …




Copyright © 2011 Russel Winder                                      5
Models of Management

                       ●    Actors
                       ●    Dataflow
                       ●    Communicating Sequential Processes (CSP)
                       ●    Data parallelism




Copyright © 2011 Russel Winder                                         6
GPars

                       ●    A Groovy/Java implemented framework for easy
                            expression of parallelism.
                       ●    Offers:
                                 –   Actors
                                 –   Dataflow
                                 –   CSP
                                 –   Data parallelism




Copyright © 2011 Russel Winder                                             7
Builds on…

                       ●    GPars builds on java.util.concurrent and all the
                            JSR166 goodness.




Copyright © 2011 Russel Winder                                                 8
Usage

                       ●    GPars works supremely well in Groovy scripts that
                            manage Groovy and Java classes.
                       ●    GPars works very well as a Java API.




Copyright © 2011 Russel Winder                                                  9
@Grab ( 'org.codehaus.gpars:gpars:1.0-SNAPSHOT' )

  import groovyx.gpars.GParsPool

  void execute ( final int numberOfTasks ) {
    GParsPool.withPool {
      final int n = 100000000i
      final double delta = 1.0d / n
      final startTimeNanos = System.nanoTime ( )
      final int sliceSize = n / numberOfTasks
      final items = [ ]
      for ( int i in 0i ..< numberOfTasks ) { items << i }
      final pi = 4.0d * delta * items.collectParallel { taskId ->
        final int start = 1i + taskId * sliceSize
        final int end = ( taskId + 1i ) * sliceSize
        double sum = 0.0d ;
        for ( int i in start .. end ) {
          final double x = ( i - 0.5d ) * delta
          sum += 1.0d / ( 1.0d + x * x )
        }
        sum
      }.sumParallel ( )
      final elapseTime = ( System.nanoTime ( ) - startTimeNanos ) / 1e9
      println ( '==== Groovy GPars GParsPool pi = ' + pi )
      println ( '==== Groovy GPars GParsPool iteration count = ' + n )
      println ( '==== Groovy GPars GParsPool elapse = ' + elapseTime )
      println ( '==== Groovy GPars GParsPool processor count = ' + Runtime.getRuntime ( ).availableProcessors ( ) )
      println ( '==== Groovy GPars GParsPool task count = ' + numberOfTasks )
    }
  }
Copyright © 2011 Russel Winder                                                                                        10
@Grab ( 'org.codehaus.gpars:gpars:1.0-SNAPSHOT' )

                import groovyx.gpars.ParallelEnhancer

                void execute ( final int numberOfTasks ) {
                  final int n = 100000000i
                  final double delta = 1.0d / n
                  final startTimeNanos = System.nanoTime ( )
                  final int sliceSize = n / numberOfTasks
                  final items = [ ]
                  for ( int i in 0i ..< numberOfTasks ) { items << i }
                  ParallelEnhancer.enhanceInstance ( items )
                  final pi = 4.0d * delta * items.collectParallel { taskId ->
                    final int start = 1i + taskId * sliceSize
                    final int end = ( taskId + 1i ) * sliceSize
                    double sum = 0.0d ;
                    for ( int i in start .. end ) {
                      final double x = ( i - 0.5d ) * delta
                      sum += 1.0d / ( 1.0d + x * x )
                    }
                    sum
                  }.sumParallel ( )
                  final elapseTime = ( System.nanoTime ( ) - startTimeNanos ) / 1e9
                  println ( '==== Groovy GPars ParallelEnhancer pi = ' + pi )
                  println ( '==== Groovy GPars ParallelEnhancer iteration count = ' + n )
                  println ( '==== Groovy GPars ParallelEnhancer elapse = ' + elapseTime )
                  println ( '==== Groovy GPars ParallelEnhancer processor count = ' + Runtime.getRuntime
                ( ).availableProcessors ( ) )
                  println ( '==== Groovy GPars ParallelEnhancer task count = ' + numberOfTasks )
                }
Copyright © 2011 Russel Winder                                                                             11
GPars
                    Check it out, you know you
                              want to.

                     http://gpars.codehaus.org/


Copyright © 2011 Russel Winder                    12
Mandatory Book Advert

                       Python for Rookies
                       Sarah Mount, James Shuttleworth and
                       Russel Winder
                       Thomson Learning Now called Cengage Learning.


                                          Developing Java Software Third Edition
                                          Russel Winder and Graham Roberts
                                          Wiley




                                                          Buy these books!
Copyright © 2010 Russel Winder                                                     13
GPars
               Parallelism the Right Way

                                 Dr Russel Winder

                                   It’z Interactive Ltd
                                   russel@itzinteractive.com
                                   russel@russel.org.uk
                                   @russel_winder
Copyright © 2011 Russel Winder                                 14

More Related Content

PDF
Real Time Web with Node
PDF
Node Powered Mobile
PDF
The Ring programming language version 1.10 book - Part 12 of 212
PDF
The Ring programming language version 1.9 book - Part 11 of 210
PDF
スローダウン、ハングを一発解決 スレッドダンプはトラブルシューティングの味方 #wlstudy
PDF
Scaling Django with gevent
PDF
It's All About Processes Communicating
Real Time Web with Node
Node Powered Mobile
The Ring programming language version 1.10 book - Part 12 of 212
The Ring programming language version 1.9 book - Part 11 of 210
スローダウン、ハングを一発解決 スレッドダンプはトラブルシューティングの味方 #wlstudy
Scaling Django with gevent
It's All About Processes Communicating

Similar to GPars: Parallelism the Right Way (20)

PDF
GPars For Beginners
ODP
GPars: Groovy Parallelism for Java
PDF
Gpars - the coolest bits
PDF
Parallel computing with GPars
PDF
Arc 300-3 ade miller-en
PDF
Martin Odersky: What's next for Scala
PDF
GPars Workshop
PPT
PDF
A Java Fork_Join Framework
PDF
Groovy concurrency
PDF
Simon Peyton Jones: Managing parallelism
PDF
Peyton jones-2011-parallel haskell-the_future
PDF
Just Keep Passing the Messages from Groovy and Grails eXchange 2011
KEY
Erjang - A journey into Erlang-land
PPTX
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
ODP
Gpars concepts explained
PDF
GPars Remoting
PDF
GPars For Beginners
GPars: Groovy Parallelism for Java
Gpars - the coolest bits
Parallel computing with GPars
Arc 300-3 ade miller-en
Martin Odersky: What's next for Scala
GPars Workshop
A Java Fork_Join Framework
Groovy concurrency
Simon Peyton Jones: Managing parallelism
Peyton jones-2011-parallel haskell-the_future
Just Keep Passing the Messages from Groovy and Grails eXchange 2011
Erjang - A journey into Erlang-land
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Gpars concepts explained
GPars Remoting
Ad

More from Russel Winder (20)

PDF
On Concurrency and Parallelism in the JVMverse
PDF
The Case for Kotlin and Ceylon
PDF
On the Architectures of Microservices: the next layer
PDF
Fast Python? Don't Bother
PDF
Making Python computations fast
PDF
Tales from the Workshops
PDF
Making Computations Execute Very Quickly
PDF
Java is Dead, Long Live Ceylon, Kotlin, etc
PDF
Java is dead, long live Scala, Kotlin, Ceylon, etc.
PDF
GPars 2014
PDF
Spocktacular testing
PDF
Spocktacular Testing
PDF
Is Groovy static or dynamic
PDF
Java is dead, long live Scala Kotlin Ceylon etc.
PDF
Dataflow: the concurrency/parallelism architecture you need
PDF
Are Go and D threats to Python
PDF
Is Groovy as fast as Java
PDF
Who needs C++ when you have D and Go
PDF
Java 8: a New Beginning
PDF
Why Go is an important programming language
On Concurrency and Parallelism in the JVMverse
The Case for Kotlin and Ceylon
On the Architectures of Microservices: the next layer
Fast Python? Don't Bother
Making Python computations fast
Tales from the Workshops
Making Computations Execute Very Quickly
Java is Dead, Long Live Ceylon, Kotlin, etc
Java is dead, long live Scala, Kotlin, Ceylon, etc.
GPars 2014
Spocktacular testing
Spocktacular Testing
Is Groovy static or dynamic
Java is dead, long live Scala Kotlin Ceylon etc.
Dataflow: the concurrency/parallelism architecture you need
Are Go and D threats to Python
Is Groovy as fast as Java
Who needs C++ when you have D and Go
Java 8: a New Beginning
Why Go is an important programming language
Ad

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
1. Introduction to Computer Programming.pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mushroom cultivation and it's methods.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
TLE Review Electricity (Electricity).pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Univ-Connecticut-ChatGPT-Presentaion.pdf
Machine Learning_overview_presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Heart disease approach using modified random forest and particle swarm optimi...
Machine learning based COVID-19 study performance prediction
1. Introduction to Computer Programming.pptx
A Presentation on Artificial Intelligence
OMC Textile Division Presentation 2021.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
cloud_computing_Infrastucture_as_cloud_p
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
Mushroom cultivation and it's methods.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TLE Review Electricity (Electricity).pptx
MIND Revenue Release Quarter 2 2025 Press Release

GPars: Parallelism the Right Way

  • 1. GPars Parallelism the Right Way Dr Russel Winder It’z Interactive Ltd russel@itzinteractive.com russel@russel.org.uk @russel_winder Copyright © 2011 Russel Winder 1
  • 2. Moore's Law ● More transistors. ● More cores. ● More parallelism. ● Ubiqitous parallelism. Copyright © 2011 Russel Winder 2
  • 3. Caveat ● Not all applications require parallelism. ● I/O bound best handled with single threaded event loop. Copyright © 2011 Russel Winder 3
  • 4. Where Parallelism Is Needed ● Operating systems offer threads as access to the cores. ● Languages offer threads as infrastructure. Copyright © 2011 Russel Winder 4
  • 5. Hummm... ● Shared memory multithreading is hard. ● Locks, semaphores, monitors, … Copyright © 2011 Russel Winder 5
  • 6. Models of Management ● Actors ● Dataflow ● Communicating Sequential Processes (CSP) ● Data parallelism Copyright © 2011 Russel Winder 6
  • 7. GPars ● A Groovy/Java implemented framework for easy expression of parallelism. ● Offers: – Actors – Dataflow – CSP – Data parallelism Copyright © 2011 Russel Winder 7
  • 8. Builds on… ● GPars builds on java.util.concurrent and all the JSR166 goodness. Copyright © 2011 Russel Winder 8
  • 9. Usage ● GPars works supremely well in Groovy scripts that manage Groovy and Java classes. ● GPars works very well as a Java API. Copyright © 2011 Russel Winder 9
  • 10. @Grab ( 'org.codehaus.gpars:gpars:1.0-SNAPSHOT' ) import groovyx.gpars.GParsPool void execute ( final int numberOfTasks ) { GParsPool.withPool { final int n = 100000000i final double delta = 1.0d / n final startTimeNanos = System.nanoTime ( ) final int sliceSize = n / numberOfTasks final items = [ ] for ( int i in 0i ..< numberOfTasks ) { items << i } final pi = 4.0d * delta * items.collectParallel { taskId -> final int start = 1i + taskId * sliceSize final int end = ( taskId + 1i ) * sliceSize double sum = 0.0d ; for ( int i in start .. end ) { final double x = ( i - 0.5d ) * delta sum += 1.0d / ( 1.0d + x * x ) } sum }.sumParallel ( ) final elapseTime = ( System.nanoTime ( ) - startTimeNanos ) / 1e9 println ( '==== Groovy GPars GParsPool pi = ' + pi ) println ( '==== Groovy GPars GParsPool iteration count = ' + n ) println ( '==== Groovy GPars GParsPool elapse = ' + elapseTime ) println ( '==== Groovy GPars GParsPool processor count = ' + Runtime.getRuntime ( ).availableProcessors ( ) ) println ( '==== Groovy GPars GParsPool task count = ' + numberOfTasks ) } } Copyright © 2011 Russel Winder 10
  • 11. @Grab ( 'org.codehaus.gpars:gpars:1.0-SNAPSHOT' ) import groovyx.gpars.ParallelEnhancer void execute ( final int numberOfTasks ) { final int n = 100000000i final double delta = 1.0d / n final startTimeNanos = System.nanoTime ( ) final int sliceSize = n / numberOfTasks final items = [ ] for ( int i in 0i ..< numberOfTasks ) { items << i } ParallelEnhancer.enhanceInstance ( items ) final pi = 4.0d * delta * items.collectParallel { taskId -> final int start = 1i + taskId * sliceSize final int end = ( taskId + 1i ) * sliceSize double sum = 0.0d ; for ( int i in start .. end ) { final double x = ( i - 0.5d ) * delta sum += 1.0d / ( 1.0d + x * x ) } sum }.sumParallel ( ) final elapseTime = ( System.nanoTime ( ) - startTimeNanos ) / 1e9 println ( '==== Groovy GPars ParallelEnhancer pi = ' + pi ) println ( '==== Groovy GPars ParallelEnhancer iteration count = ' + n ) println ( '==== Groovy GPars ParallelEnhancer elapse = ' + elapseTime ) println ( '==== Groovy GPars ParallelEnhancer processor count = ' + Runtime.getRuntime ( ).availableProcessors ( ) ) println ( '==== Groovy GPars ParallelEnhancer task count = ' + numberOfTasks ) } Copyright © 2011 Russel Winder 11
  • 12. GPars Check it out, you know you want to. http://gpars.codehaus.org/ Copyright © 2011 Russel Winder 12
  • 13. Mandatory Book Advert Python for Rookies Sarah Mount, James Shuttleworth and Russel Winder Thomson Learning Now called Cengage Learning. Developing Java Software Third Edition Russel Winder and Graham Roberts Wiley Buy these books! Copyright © 2010 Russel Winder 13
  • 14. GPars Parallelism the Right Way Dr Russel Winder It’z Interactive Ltd russel@itzinteractive.com russel@russel.org.uk @russel_winder Copyright © 2011 Russel Winder 14