<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Transentia &#187; GPars</title>
	<atom:link href="http://wordpress.transentia.com.au/wordpress/tag/gpars/feed/" rel="self" type="application/rss+xml" />
	<link>http://wordpress.transentia.com.au/wordpress</link>
	<description>transentia pty. ltd.; development, consulting, training at the leading-edge of technology</description>
	<lastBuildDate>Tue, 13 Dec 2011 03:01:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>A (Simple) GroovyMag Article</title>
		<link>http://wordpress.transentia.com.au/wordpress/2011/07/05/a-simple-groovymag-article/</link>
		<comments>http://wordpress.transentia.com.au/wordpress/2011/07/05/a-simple-groovymag-article/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 00:10:01 +0000</pubDate>
		<dc:creator>bob</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[GPars]]></category>
		<category><![CDATA[GroovyMag]]></category>

		<guid isPermaLink="false">http://wordpress.transentia.com.au/wordpress/?p=1386</guid>
		<description><![CDATA[Another GroovyMag article has just been published: This (quite short) one is &#8220;Playing Pass the Parcel with GPars&#8221; and was inspired by real-life events at a friend&#8217;s child&#8217;s birthday party!]]></description>
			<content:encoded><![CDATA[<p>Another <a href="http://www.groovymag.com/main.issues.description/id=35/?refer=http://groovymag.com/">GroovyMag</a> article has just been published:</p>
<p><img src="http://wordpress.transentia.com.au/wordpress/wp-content/uploads/2011/07/gm33_125.jpg" width="125" height="162" /></p>
<p>This (quite short) one is &#8220;Playing Pass the Parcel with GPars&#8221; and was inspired by real-life events at a friend&#8217;s child&#8217;s birthday party!</p>
]]></content:encoded>
			<wfw:commentRss>http://wordpress.transentia.com.au/wordpress/2011/07/05/a-simple-groovymag-article/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple Bit of GPars</title>
		<link>http://wordpress.transentia.com.au/wordpress/2011/07/05/a-simple-bit-of-gpars/</link>
		<comments>http://wordpress.transentia.com.au/wordpress/2011/07/05/a-simple-bit-of-gpars/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 23:36:50 +0000</pubDate>
		<dc:creator>bob</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[GPars]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://wordpress.transentia.com.au/wordpress/?p=1372</guid>
		<description><![CDATA[I recently had a rather dull task to undertake: each time a (third-party) application bug manifested itself, I had to retrieve all the relevant logs from the supporting JBoss instance so that they could be mailed to the developer. The logs were often quite large, which argued for using parallel download, rather than grabbing each [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a rather dull task to undertake: each time a (third-party) application bug manifested itself, I had to retrieve all the relevant logs from the supporting JBoss instance so that they could be mailed to the developer.</p>
<p>The logs were often quite large, which argued for using parallel download, rather than grabbing each file sequentially (the good ole&#8217; <a href="http://en.wikipedia.org/wiki/Space-time_tradeoff">space-time</a> tradeoff in yet another guise).</p>
<p>I wanted to make everything as easy as possible for the support team that would come after me. This meant no mucking around with libraries, classpaths, etc.</p>
<p>My first thought was &#8220;<a href="http://www.supermanhomepage.com/">this looks like a job for GPars</a>.&#8221;<br />
(My second thought was probably &#8220;time for a <a href="http://dictionary.reference.com/browse/cuppa">cuppa</a>&#8220;, but that&#8217;s not strictly relevant here&#8230;)</p>
<p>Thus:</p>
<pre>
// avoid all the command-line classpath munging; add GPARS
def GPARS_HOME='file:///C:/BIN/Groovy/gpars-all-0.12-beta-1-SNAPSHOT/'
['jcsp-1.1-rc5.jar', 'netty-3.1.5.GA.jar', 'gpars-0.12-beta-1-SNAPSHOT.jar'].each {
  this.class.classLoader.rootLoader.addURL(new URL("${GPARS_HOME}/${it}"))
  }

def retrieve = {file -&gt;
  println "(${Thread.currentThread().name}); Downloading $file"
  new File(file) &lt;&lt; "http://server/download?dir=log&amp;file=${file}".toURL().text
  }

groovyx.gpars.GParsPool.withPool {
  ['Main.log', 'MainError.log', 'error.log', 'Subsystem.log',
   'SubsystemError.log', 'boot.log', 'server.log'].eachParallel {
    retrieve it
    }
  }
</pre>
<p>It&#8217;s not rocket-science, this, but I hope it shows just how damnned easy Groovy and <a href="http://gpars.codehaus.org/">GPars</a> make life&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://wordpress.transentia.com.au/wordpress/2011/07/05/a-simple-bit-of-gpars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>If I Said You Had A Beautiful Body, Would You Hold It Against Me?</title>
		<link>http://wordpress.transentia.com.au/wordpress/2011/05/10/if-i-said-you-had-a-beautiful-body-would-you-hold-it-against-me/</link>
		<comments>http://wordpress.transentia.com.au/wordpress/2011/05/10/if-i-said-you-had-a-beautiful-body-would-you-hold-it-against-me/#comments</comments>
		<pubDate>Tue, 10 May 2011 11:39:30 +0000</pubDate>
		<dc:creator>bob</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[GPars]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Griffon]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://wordpress.transentia.com.au/wordpress/?p=1261</guid>
		<description><![CDATA[I have been meaning to put this stuff up for simply ages. For the Open Source Developers&#8217; Conference 2009 I created a number of &#8220;mini fliers&#8221; that we placed around the conference&#8217;s public areas in an effort to attract attendees to come to the Groovy BOF that we were holding. The first rule of advertising [...]]]></description>
			<content:encoded><![CDATA[<p>I have been meaning to put this stuff up for simply <em>ages</em>.</p>
<p>For the <a href="http://2009.osdc.com.au/">Open Source Developers&#8217; Conference 2009</a> I created a number of &#8220;mini fliers&#8221; that we placed around the conference&#8217;s public areas in an effort to attract attendees to come to the Groovy BOF that we were holding.</p>
<p>The first rule of advertising is to get people&#8217;s attention. Doesn&#8217;t matter how corny your effort is. Hell&#8230;the cornier the better! </p>
<p>Thus:</p>
<p><img src="http://wordpress.transentia.com.au/wordpress/wp-content/uploads/2011/05/groovy_osdc_beautiful_body.png" alt="" title="groovy_osdc_beautiful_body" width="776" height="155" style="border:1px solid #222222" /></p>
<p>There are many more gems like this in the <a href='http://wordpress.transentia.com.au/wordpress/wp-content/uploads/2011/05/Groovy-Lines-For-OSDC-BOF.docx'>source document</a> (Microsoft Word format. Ahhh the <a href="http://en.wikipedia.org/wiki/Irony">irony</a>. Please don&#8217;t hold it against me&#8230;).</p>
<p>Hope these can be useful to someone else.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://wordpress.transentia.com.au/wordpress/2011/05/10/if-i-said-you-had-a-beautiful-body-would-you-hold-it-against-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Playing With GPars</title>
		<link>http://wordpress.transentia.com.au/wordpress/2010/03/05/playing-with-gpars/</link>
		<comments>http://wordpress.transentia.com.au/wordpress/2010/03/05/playing-with-gpars/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 23:17:40 +0000</pubDate>
		<dc:creator>bob</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[GPars]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://wordpress.transentia.com.au/wordpress/?p=834</guid>
		<description><![CDATA[Way, way, waaaay back, I became something of a minor Occam &#8216;guru&#8217; (I actually implemented a fair proportion of a compiler/runtime for Occam 1) and I have maintained an (academic and practical) interest in the field throughout my career (which is what I laughingly call what I do these days, see Will Code For Food [...]]]></description>
			<content:encoded><![CDATA[<p>Way, way, waaaay back, I became something of a minor Occam &#8216;guru&#8217; (I actually implemented a fair proportion of a compiler/runtime for Occam 1) and I have maintained an (academic and practical) interest in the field throughout my career (which is what I laughingly call what I do these days, see <a href="http://wordpress.transentia.com.au/wordpress/2009/02/05/will-code-for-food/">Will Code For Food</a> for more ;-))</p>
<p>It&#8217;s therefore natural for me to take a look at <a href="http://gpars.codehaus.org">GPars</a>.</p>
<p>In its own words, GPars is:</p>
<blockquote><p>an open-source concurrency library for Groovy that aims to give you multiple high-level abstractions for writing concurrent code in Groovy &#8211; map/reduce, fork/join, asynchronous closures, actors, agents, dataflow concurrency and other concepts, which aim to make your Groovy code concurrent with little effort.</p></blockquote>
<p>Ever played the charmingly-named <a href="http://en.wikipedia.org/wiki/Chinese_whispers">Chinese Whispers game</a> at a party? That&#8217;s what I&#8217;m going to play with here.</p>
<p>It&#8217;s a very simple example of a pipelined processing task. This sort of thing is very common in signal processing, process control and image munging.</p>
<p>In the applications I build here, there will be a source thread generating messages which are each then sent through a chain of intermediate threads to a sink thread, which simply bounces the message back down the chain. On each &#8216;sinkward&#8217; step the intermediaries may or may not munge the message in some way as it whispers it to the next step, so that the version that eventually makes its way back to the source may be changed in some unpredicatable way.</p>
<p>Here&#8217;s the image to keep in your head:</p>
<p><img src="http://wordpress.transentia.com.au/wordpress/wp-content/uploads/2010/03/pipe.png" alt="pipe" title="pipe" width="672" height="225" class="alignnone size-full wp-image-849" /></p>
<p>GPars provides several alternative ways of tackling this problem.</p>
<p>Scala made the idea of <a href="http://programming-scala.labs.oreilly.com/ch09.html">Actors</a> popular, so that&#8217;s a good starting point. </p>
<p>This is what the GPars doco says about Actors:</p>
<blockquote><p>The actor support in gpars were inspired by the Actors library in Scala but have meanwhile gone beyond that.</p>
<p>Actors allow for a messaging-based concurrency model, built from independent active objects that exchange messages and have no mutable shared state.</p></blockquote>
<p>If you are a webby-person, you can think of an Actor as a Servlet&#8230;you won&#8217;t go far wrong.</p>
<p>Coming up is a version of &#8220;Chinese Whispers&#8221; implemented using GPars&#8217; Actors:</p>
<pre>package chinesewhispers

@GrabResolver(name = 'jboss', root = 'http://repository.jboss.org/maven2/')
@Grab(group = 'org.codehaus.gpars', module = 'gpars', version = '0.9')

import groovyx.gpars.actor.*
import chinesewhispers.utils.Whisperer

Actors.defaultPooledActorGroup.resize 16

class Stop {}
class Go {}

final intermediaries = []

def src = Actors.actor {index -&gt;
  println "Source; Starting..."
  react { Go go -&gt;
    println "Source; time to 'Go'"
    def msg = 'The sky is green, the trees are blue...SNAFU!'
    println "Source; message is: $msg"
    for (i in 0..&lt;8) {
      intermediaries[1].sendAndContinue msg, { ix, reply -&gt;
        println "Source[$ix]; received: '$reply'"
      }.curry(i)
    }
    intermediaries[1].send(new Stop())
    stop()
    print "Source; ...Stopped."
  }
}

intermediaries &lt;&lt; src

for (i in 1..8) {
  intermediaries &lt;&lt; Actors.messageHandler({ index -&gt;
    when {Stop msg -&gt; intermediaries[index + 1].send msg; stop(); }
    when {msg -&gt;
      def sender = msg.sender
      intermediaries[index + 1].sendAndContinue Whisperer.whisper("${msg}"), { reply -&gt;
        sender.send reply
      }
    }
  }.curry(i))
}

intermediaries &lt;&lt; Actors.messageHandler {
  when { Stop msg -&gt; stop() }
  when { msg -&gt; reply msg }
}

src &lt; &lt; new Go()

intermediaries*.join()</pre>
<p>This simple script creates a list of Actors, with a source Actor first, followed by a number of essentially identical intermediary Actors and finally a sink Actor.</p>
<p>Here it all is, &#8220;in action&#8221; (pun intended):</p>
<p><img src="http://wordpress.transentia.com.au/wordpress/wp-content/uploads/2010/03/ChineseWhisperingActors.png" alt="ChineseWhisperingActors" title="ChineseWhisperingActors" width="603" height="292" class="alignnone size-full wp-image-856" /><br />
GPars creates Actors in a runnable state so that it is easy to start things off by sending the Source a unique Go message.</p>
<p>Notice how each Actor sends a message and then allocates a closure to asynchrounously handle the appropriate return message. This ensures that intermediaries get new work as soon as possible, so the pipeline is always running at full capacity. This makes for a very performant solution.</p>
<p>It&#8217;s a truism that getting a parallel system off and running is fairly easy, but getting one to Stop properly can be surprisingly tricky. For this application, the Source Actor&#8217;s last activity before it finishes is to send a (guaranteed-to-be-unique, obviously) Stop message up the pipline. On receipt of a Stop, each intermediate Actor simply forwards the message and then terminates in a nice orderly fashion. the final Sink Actor receives the forwarded Stop and simply stops without trying to send the message back down the chain of intermediaries (since the chain is effectively no longer there).</p>
<p>The messageHandler closure&#8217;s distinct &#8216;when&#8217; syntax makes the sort of message dependent processing shown here very easy.</p>
<p>The final line of the script simply waits for all Actors to finish shutting down before allowing the overall script to exit.</p>
<p>Simple, performant and easy to write and comprehend. What&#8217;s not to like!</p>
<p>GPars actors are not perfect (they lack some of the Scala library&#8217;s more subtle abilities such as pattern matching to guard message reception, and the ability to see the length of the incoming mesage queue), but they&#8217;re not too shabby, either.</p>
<p>For a second bite at the apple, here&#8217;s the DataFlow version.</p>
<p>According the the doco:</p>
<blockquote><p>Dataflow concurrency offers an alternative concurrency model, which is inherently safe and robust. It puts emphasis on the data and their flow though your processes instead of the actual processes that manipulate the data. Dataflow algorithms relieve developers from dealing with live-locks, race-conditions and make dead-ocks[sic] deterministic and thus 100% reproducible. If you don&#8217;t get dead-locks in tests you won&#8217;t get them in production.</p></blockquote>
<p>What does a Dataflow-based Chinese Whispers program look like? Viola:</p>
<pre>
package chinesewhispers

@GrabResolver(name = 'jboss', root = 'http://repository.jboss.org/maven2/')
@Grab(group = 'org.codehaus.gpars', module = 'gpars', version = '0.9')

import groovyx.gpars.dataflow.*
import chinesewhispers.utils.Whisperer

class Link {
  final up = new DataFlowVariable()
  final down = new DataFlowVariable()

  Link(index) {
    /*
      up &gt;&gt; {println "The up[$index] variable has just been bound to $it"}
      down &gt;&gt; {println "The down[$index] variable has just been bound to $it"}
    */
  }
}

final NINT = 8

for (iteration in 0..&lt;8) {
  final intermediaries = []
  for (i in 0..&lt;NINT)
    intermediaries &lt;&lt; new Link(i)

  DataFlow.task {
    intermediaries[0].up &lt;&lt; 'The sky is green, the trees are blue...SNAFU!'
    println intermediaries[0].down.val
  }

  for (i in 1..&lt;NINT) {
    DataFlow.task({ index -&gt;
      intermediaries[index].up &lt;&lt; Whisperer.whisper(intermediaries[index - 1].up.val)
    }.curry(i))
    DataFlow.task({ index -&gt;
      intermediaries[index - 1].down &lt;&lt; intermediaries[index].down.val
    }.curry(i))
  }

  DataFlow.task {
    intermediaries[NINT - 1].down &lt;&lt; "Did you REALLY mean to say: '${intermediaries[NINT - 1].up.val}'?"
  }
}
"OK"
</pre>
<p>Neat, eh? Simple assignment to a variable, or reading a variable&#8217;s value performs inter-task communication. No messy send and receive mechanisms here!</p>
<p>Here&#8217;s what it looks like when running inside IntelliJ:</p>
<p><img src="http://wordpress.transentia.com.au/wordpress/wp-content/uploads/2010/03/ChineseWhisperingDataflow.png" alt="ChineseWhisperingDataflow" title="ChineseWhisperingDataflow" width="655" height="199" class="alignnone size-full wp-image-848" /></p>
<p>Of course, you lose a fair bit of flexibility, but for straightforward tasks the simplicity of the solution more than compensates&#8230;</p>
<p>Each bi-directional link between intermediary tasks is modelled as a Link with a separate &#8216;up&#8217; and a &#8216;down&#8217; variable. Although GPars offers alternatives, for this application I have chosen to use plain &#8220;Dataflow Variables&#8221;, which are write-once, read-many entities; each iteration through the main loop thus recreates the requisite Link instances (and hence the Dataflow Variables) from scratch.</p>
<p>Uncomment the body of the Link constructor and you&#8217;ll be able to see the inter-task communication in action. Simple but very effective debugging:</p>
<p><img src="http://wordpress.transentia.com.au/wordpress/wp-content/uploads/2010/03/ChineseWhisperingDataflowDebug.png" alt="ChineseWhisperingDataflowDebug" title="ChineseWhisperingDataflowDebug" width="999" height="238" class="alignnone size-full wp-image-854" /></p>
<p>Just for completeness sake (it&#8217;s a minor supporting actor, not a star): here&#8217;s the Whisperer class that the examples use:</p>
<pre>package chinesewhispers.utils

class Whisperer {
  final static r = new Random()

  final static map = [
          'sky': 'elephant',
          'trees': 'kettles',
          'green': 'effervescent',
          'blue': 'smelly'
  ]

  static whisper(msg) {

    if (r.nextBoolean())
      return msg

    def split = msg.split(/\W/).findAll { it }
    def l = split.size()
    def sel = r.nextInt(l)
    def word = split[sel]

    def w = map[word]
    w ? msg.replace(word, w) : msg
  }
}
</pre>
<p><a href="http://en.wikipedia.org/wiki/Porky_Pig">That&#8217;s all folks</a>; two versions of the same application is enough for any mortal!</p>
<p>I am eagerly looking forward to the time when the work to integrate <a href="http://www.cs.kent.ac.uk/projects/ofa/jcsp/">JCSP</a> into GPars bears fruit.</p>
<p>By the way, each of these examples should happily work with GPars 0.9 and Groovy 1.7.1. Just copy the listings into GroovyConsole and go (you might need to bring the Whispers class inline into each application, but that&#8217;s trivial to do).</p>
]]></content:encoded>
			<wfw:commentRss>http://wordpress.transentia.com.au/wordpress/2010/03/05/playing-with-gpars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

