Groovy Parallelism for Java


      Prof Russel Winder
                Director
          It'z Interactive Ltd

      e: russel@itzinteractive.com
         e: russel@winder.org.uk
             t: @russel_winder
Prof Russel Winder
 ■ Company director, independent
   consultant, analyst, author, expert
   witness, trainer, mentor,…

 ■ Involved with GPars, Groovy, Gant,
   Gradle, GroovyFX,
   Python, SCons

 ■ Ex-theoretical physicist, ex-UNIX
   systems programmer,
   ex-computer science academic. Still
   human.
Interstitial Advert




                      ?
Introduction
Concurrency
 ■ A technique for doing lots of little bits of work,
   one at a time, and then leaving them for later.

 ■ Co-routines.
Concurrency
 ■ Concurrency is also a technique for
   simulating parallelism on a uni-processor.

 ■ Time-division multiplexing.
Concurrency
 ■ Locks, semaphores, monitors all introduced to manage shared mutable
   memory in the presence of concurrency.

 ■ Operating systems techniques.
Parallelism
 ■ Having more than one thing happening at the same time: multiple
   processors or cores working on a single program at the same time.

 ■ The only reason for
   using parallelism is
   to make computations
   take less time.
Observation
 ■ Squirrels use locks when managing
   their food resources.
Some
Interaction
Question
 ■ Who is interested in harnessing
   parallelism
   for performance improvement?
Question
 ■ Who uses synchronized in Java?
Question
 ■ Who uses synchronized in Java?




                  You are doing it wrong.
Question (with subsidiaries)
 ■ Who uses explicit locks in Java?
  ■ Who is not using java.util.concurrent?
  ■ Who is using java.util.concurrent?
Question (with subsidiaries)
 ■ Who uses explicit locks in Java?
  ■ Who is not using java.util.concurrent?
  ■ Who is using java.util.concurrent?


                  You are definitely doing it wrong.
Rationale
 ■ The purpose of locks is to prevent parallelism.

 ■ Locks prevent performance improvement.
Consequence
 ■ Squirrels deny parallelism of
   consumption.

 ■ Squirrels deny performance
   improvement.
Being
Positive
Forward Looking



       Can Java 8 make things better?
Java 8 Streams
 ■ Internal rather than external iteration.
 ■ Functional programming approaches.
 ■ Threads as infrastructure.




                             This is the real “biggy”,
                             programmers do not
                             explicitly manage threads.
Example




          π
What is the value of π ?
 ■ Easy, it's π obviously.




                             It's simples
                                    Александр Орлов   2009
Approximating π ?
 ■ Find an estimate of the value of π.
                                                 11
                                             =∫0       dx
 ■ Approximate an integration with a       4     1x 2

   summation.



                                           4 n          1
                                         π≈ ∑i=1
                                           n           i−0.5   2
                                                   1+(       )
                                                         n
Quadrature
  Sum all the areas of the rectangles.


                     Addition is associative and commutative.


                                    a + b + c + d = (a + b) + (c + d)


                                                Can sum partial sums.


                                                         Embarrassingly parallel
Parallelism

              Scatter/gather


              Farmer/worker


              Fork/join


              Map/reduce
Code
Software Architectures

  Actors
  Independent                           Dataflow
  processes                             Operators connected
  communicating via                     by channels with
  asynchronous                          activity triggered by
  exchange of                           arrival of data on the
  messages.                             channels.


       Data Parallel
       Transform a sequence to    CSP
       another sequence where     Sequential processes
       all individual actions     connected by channels
       happen at the same time.   using synchronous
                                  message exchange
                                  (rendezvous).
Software Architectures




    Data Parallel
    Transform a sequence to
    another sequence where
    all individual actions
    happen at the same time.
Software Architectures

  Actors
  Independent
  processes
  communicating via
  asynchronous
  exchange of
  messages.
Software Architectures

                         Dataflow
                         Operators connected
                         by channels with
                         activity triggered by
                         arrival of data on the
                         channels.
Software Architectures




                     CSP
                     Sequential processes
                     connected by channels
                     using synchronous
                     message exchange
                     (rendezvous).
Software Architectures

            Active
            Objects                       Fork/Join
            An object that is             An toolkit for tree structured
            actually an actor but         concurrency and
            looks like a full             parallelism.
            service object.


   Agents
   A wrapper for some
                                    Software Transactional
   shared mutable state.            Memory
                                    Wrappers for mutable values that uses
                                    transactions rather than locks.
More
Code
Mantra
 ■ Squirrels deny parallelism.

 ■ Squirrels deny performance
   improvement.

 ■ Programmers' code should not
   emulate squirrel behaviour.
Q&A
Endnote
Source Code


        The sample codes are in a Git repository on

               GitHub and my own website.
The End
Interstitial Advert




                      ?
Groovy Parallelism for Java


      Prof Russel Winder
                Director
          It'z Interactive Ltd

      e: russel@itzinteractive.com
         e: russel@winder.org.uk
             t: @russel_winder

GPars: Groovy Parallelism for Java

  • 1.
    Groovy Parallelism forJava Prof Russel Winder Director It'z Interactive Ltd e: russel@itzinteractive.com e: russel@winder.org.uk t: @russel_winder
  • 2.
    Prof Russel Winder ■ Company director, independent consultant, analyst, author, expert witness, trainer, mentor,… ■ Involved with GPars, Groovy, Gant, Gradle, GroovyFX, Python, SCons ■ Ex-theoretical physicist, ex-UNIX systems programmer, ex-computer science academic. Still human.
  • 3.
  • 4.
  • 5.
    Concurrency ■ Atechnique for doing lots of little bits of work, one at a time, and then leaving them for later. ■ Co-routines.
  • 6.
    Concurrency ■ Concurrencyis also a technique for simulating parallelism on a uni-processor. ■ Time-division multiplexing.
  • 7.
    Concurrency ■ Locks,semaphores, monitors all introduced to manage shared mutable memory in the presence of concurrency. ■ Operating systems techniques.
  • 8.
    Parallelism ■ Havingmore than one thing happening at the same time: multiple processors or cores working on a single program at the same time. ■ The only reason for using parallelism is to make computations take less time.
  • 13.
    Observation ■ Squirrelsuse locks when managing their food resources.
  • 14.
  • 15.
    Question ■ Whois interested in harnessing parallelism for performance improvement?
  • 16.
    Question ■ Whouses synchronized in Java?
  • 17.
    Question ■ Whouses synchronized in Java? You are doing it wrong.
  • 18.
    Question (with subsidiaries) ■ Who uses explicit locks in Java? ■ Who is not using java.util.concurrent? ■ Who is using java.util.concurrent?
  • 19.
    Question (with subsidiaries) ■ Who uses explicit locks in Java? ■ Who is not using java.util.concurrent? ■ Who is using java.util.concurrent? You are definitely doing it wrong.
  • 20.
    Rationale ■ Thepurpose of locks is to prevent parallelism. ■ Locks prevent performance improvement.
  • 21.
    Consequence ■ Squirrelsdeny parallelism of consumption. ■ Squirrels deny performance improvement.
  • 22.
  • 23.
    Forward Looking Can Java 8 make things better?
  • 24.
    Java 8 Streams ■ Internal rather than external iteration. ■ Functional programming approaches. ■ Threads as infrastructure. This is the real “biggy”, programmers do not explicitly manage threads.
  • 25.
  • 26.
    What is thevalue of π ? ■ Easy, it's π obviously. It's simples Александр Орлов 2009
  • 27.
    Approximating π ? ■ Find an estimate of the value of π.  11 =∫0 dx ■ Approximate an integration with a 4 1x 2 summation. 4 n 1 π≈ ∑i=1 n i−0.5 2 1+( ) n
  • 28.
    Quadrature Sumall the areas of the rectangles. Addition is associative and commutative. a + b + c + d = (a + b) + (c + d) Can sum partial sums. Embarrassingly parallel
  • 29.
    Parallelism Scatter/gather Farmer/worker Fork/join Map/reduce
  • 30.
  • 31.
    Software Architectures Actors Independent Dataflow processes Operators connected communicating via by channels with asynchronous activity triggered by exchange of arrival of data on the messages. channels. Data Parallel Transform a sequence to CSP another sequence where Sequential processes all individual actions connected by channels happen at the same time. using synchronous message exchange (rendezvous).
  • 32.
    Software Architectures Data Parallel Transform a sequence to another sequence where all individual actions happen at the same time.
  • 33.
    Software Architectures Actors Independent processes communicating via asynchronous exchange of messages.
  • 34.
    Software Architectures Dataflow Operators connected by channels with activity triggered by arrival of data on the channels.
  • 35.
    Software Architectures CSP Sequential processes connected by channels using synchronous message exchange (rendezvous).
  • 36.
    Software Architectures Active Objects Fork/Join An object that is An toolkit for tree structured actually an actor but concurrency and looks like a full parallelism. service object. Agents A wrapper for some Software Transactional shared mutable state. Memory Wrappers for mutable values that uses transactions rather than locks.
  • 37.
  • 38.
    Mantra ■ Squirrelsdeny parallelism. ■ Squirrels deny performance improvement. ■ Programmers' code should not emulate squirrel behaviour.
  • 39.
  • 40.
  • 41.
    Source Code The sample codes are in a Git repository on GitHub and my own website.
  • 42.
  • 43.
  • 44.
    Groovy Parallelism forJava Prof Russel Winder Director It'z Interactive Ltd e: russel@itzinteractive.com e: russel@winder.org.uk t: @russel_winder