CSP is Go :-)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

CSP is Go :-)

Russel Winder-2
Just to let people know (and of course to further stimulate interest in
CSP :-) I have had success with using the GPars CSP branch (local build)
and JCSP (the snapshot in Maven).  The following code, does what it is
supposed to, though -- sadly -- it shows that Groovy doesn't scale well
as more processors are added.

        #! /usr/bin/env groovy
       
        /*
         *  Calculation of Pi using quadrature realized with a fork/join approach with GPars CSP to partition the
         *  problem and hence harness all processors available to the JVM.
         *
         *  Copyright © 2010 Russel Winder
         */
       
        @Grab ( group = 'org.codehaus.jcsp' , module = 'jcsp' , version = '1.1-rc5-SNAPSHOT' )
        @Grab ( group = 'org.codehaus.gpars' , module = 'gpars' , version = '0.10-beta-1-SNAPSHOT' )
       
        import org.jcsp.lang.Channel
        import org.jcsp.lang.CSProcess
       
        import groovyx.gpars.csp.PAR
       
        void execute ( final int numberOfTasks ) {
          final n = 100000000l // 10 times fewer due to speed issues.
          final delta = 1.0d / n
          final startTimeNanos = System.nanoTime ( )
          final sliceSize = n / numberOfTasks
          final channels = Channel.one2oneArray ( numberOfTasks )
          final processes = [ ]
          for ( int i = 0 ; i < numberOfTasks ; ++i ) {
            final int taskId = i
            processes << new CSProcess ( ) {
              public void run ( ) {
                double sum = 0.0 ;
                final long start = 1 + taskId * sliceSize
                final long end = ( taskId + 1 ) * sliceSize
                for ( long j = start ; j <= end ; ++j ) {
                  final double x = ( j - 0.5d ) * delta
                  sum += 1.0d / ( 1.0d + x * x )
                }
                channels[taskId].out ( ).write ( sum )
              }
            }
          }
          processes << new CSProcess ( ) {
            public void run ( ) {
              double sum = 0.0d
              for ( c in channels ) { sum += (double) c.in ( ).read ( ) }
              final double pi = 4.0d * sum * delta
              final double elapseTime = ( System.nanoTime ( ) - startTimeNanos ) / 1e9
              System.out.println ( "==== Groovy GPars CSP pi = " + pi )
              System.out.println ( "==== Groovy GPars CSP iteration count = " + n )
              System.out.println ( "==== Groovy GPars CSP elapse = " + elapseTime )
              System.out.println ( "==== Groovy GPars CSP processor count = " + Runtime.getRuntime ( ).availableProcessors ( ) )
              System.out.println ( "==== Groovy GPars CSP task count = " + numberOfTasks )
            }
          } ;
          ( new PAR ( processes ) ).run ( )
        }
       
        execute ( 1 )
        println ( )
        execute ( 2 )
        println ( )
        execute ( 8 )
        println ( )
        execute ( 32 )




--
Russel.
=============================================================================
Dr Russel Winder      Partner
                                            xmpp: [hidden email]
Concertant LLP        t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,   f: +44 8700 516 084   voip: sip:[hidden email]
London SW11 1EN, UK   m: +44 7770 465 077   skype: russel_winder

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: CSP is Go :-)

Vaclav
Administrator
Very cool! Are you using the jcsp branch of GPars?

Vaclav


On Tue, Feb 16, 2010 at 11:00 AM, Russel Winder <[hidden email]> wrote:
Just to let people know (and of course to further stimulate interest in
CSP :-) I have had success with using the GPars CSP branch (local build)
and JCSP (the snapshot in Maven).  The following code, does what it is
supposed to, though -- sadly -- it shows that Groovy doesn't scale well
as more processors are added.

       #! /usr/bin/env groovy

       /*
        *  Calculation of Pi using quadrature realized with a fork/join approach with GPars CSP to partition the
        *  problem and hence harness all processors available to the JVM.
        *
        *  Copyright © 2010 Russel Winder
        */

       @Grab ( group = 'org.codehaus.jcsp' , module = 'jcsp' , version = '1.1-rc5-SNAPSHOT' )
       @Grab ( group = 'org.codehaus.gpars' , module = 'gpars' , version = '0.10-beta-1-SNAPSHOT' )

       import org.jcsp.lang.Channel
       import org.jcsp.lang.CSProcess

       import groovyx.gpars.csp.PAR

       void execute ( final int numberOfTasks ) {
         final n = 100000000l // 10 times fewer due to speed issues.
         final delta = 1.0d / n
         final startTimeNanos = System.nanoTime ( )
         final sliceSize = n / numberOfTasks
         final channels = Channel.one2oneArray ( numberOfTasks )
         final processes = [ ]
         for ( int i = 0 ; i < numberOfTasks ; ++i ) {
           final int taskId = i
           processes << new CSProcess ( ) {
             public void run ( ) {
               double sum = 0.0 ;
               final long start = 1 + taskId * sliceSize
               final long end = ( taskId + 1 ) * sliceSize
               for ( long j = start ; j <= end ; ++j ) {
                 final double x = ( j - 0.5d ) * delta
                 sum += 1.0d / ( 1.0d + x * x )
               }
               channels[taskId].out ( ).write ( sum )
             }
           }
         }
         processes << new CSProcess ( ) {
           public void run ( ) {
             double sum = 0.0d
             for ( c in channels ) { sum += (double) c.in ( ).read ( ) }
             final double pi = 4.0d * sum * delta
             final double elapseTime = ( System.nanoTime ( ) - startTimeNanos ) / 1e9
             System.out.println ( "==== Groovy GPars CSP pi = " + pi )
             System.out.println ( "==== Groovy GPars CSP iteration count = " + n )
             System.out.println ( "==== Groovy GPars CSP elapse = " + elapseTime )
             System.out.println ( "==== Groovy GPars CSP processor count = " + Runtime.getRuntime ( ).availableProcessors ( ) )
             System.out.println ( "==== Groovy GPars CSP task count = " + numberOfTasks )
           }
         } ;
         ( new PAR ( processes ) ).run ( )
       }

       execute ( 1 )
       println ( )
       execute ( 2 )
       println ( )
       execute ( 8 )
       println ( )
       execute ( 32 )




--
Russel.
=============================================================================
Dr Russel Winder      Partner
                                           xmpp: [hidden email]
Concertant LLP        t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,   f: +44 8700 516 084   voip: [hidden email]
London SW11 1EN, UK   m: +44 7770 465 077   skype: russel_winder



--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech