7

I'm new to the GPARS-library and implementing it in our software at the moment.

It's no problem for me to use it instead of the normal groovy-methods like

[..].each{..} 
-> 
[..].eachParallel{..}

But I'm wondering how to parallelize 2 tasks which are returning a value.

Without GPARS I would do it this way:

List<Thread> threads = []
def forecastData
def actualData  
threads.add(Thread.start {
    forecastData = cosmoSegmentationService.getForecastSegmentCharacteristics(dataset, planPeriod, thruPeriod)
})

threads.add(Thread.start {
    actualData = cosmoSegmentationService.getMeasuredSegmentCharacteristics(dataset, fromPeriod, thruPeriodActual)
})

threads*.join()

// merge both datasets
def data = actualData + forecastData

But (how) can this be done with the GparsPool?

Martin L.
  • 3,006
  • 6
  • 36
  • 60

2 Answers2

7

You could use Dataflow:

import groovyx.gpars.dataflow.*
import static groovyx.gpars.dataflow.Dataflow.task

def forecastData = new DataflowVariable()
def actualData = new DataflowVariable()
def result = new DataflowVariable()

task {
  forecastData << cosmoSegmentationService.getForecastSegmentCharacteristics( dataset, planPeriod, thruPeriod )
}

task {
  actualData << cosmoSegmentationService.getMeasuredSegmentCharacteristics( dataset, fromPeriod, thruPeriodActual )
}

task {
  result << forecastData.val + actualData.val
}

println result.val

Alternative for GPars 0.9:

import static groovyx.gpars.GParsPool.withPool

def getForecast = {
  cosmoSegmentationService.getForecastSegmentCharacteristics( dataset, planPeriod, }

def getActual = {
  cosmoSegmentationService.getMeasuredSegmentCharacteristics( dataset, fromPeriod, thruPeriodActual )
}

def results = withPool {
  [ getForecast.callAsync(), getActual.callAsync() ]
}

println results*.get().sum()
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thank you very much. Sadly I now have the problem that our Grails 1.3.7 is bundled with GPars 0.9 and that causes problems with the Dataflow-methods. But your answer was right, thank you therefor. – Martin L. Dec 07 '12 at 11:53
  • @Martin Added an alternative using `withPool` which seems to work with GPars 0.9 :-) – tim_yates Dec 07 '12 at 12:07
  • Incredible :) Thank you very much! BTW: In Grails 1.3.7 is GPARS 0.9 included, but as a stripped jar. To use GParsPool I included GPARS 0.12. Because there is no GParsPool in the stripped jar-version of Grails 1.3.7, this was working *brrrr* The DataFlow-methods are in the stripped jar, but yes, stripped and unuseable. – Martin L. Dec 07 '12 at 14:13
  • @tim_yates Thanks a ton! The seconds example with `GParsPool` works great. –  Dec 01 '13 at 05:35
  • I hadn't seen Dataflow until now. Thanks so much for sharing! – th3morg May 21 '15 at 12:58
1
import groovyx.gpars.GParsPool    

List todoList =[]

todoList.add {
    for(int i1: 1..100){
        println "task 1:" +i1
        sleep(300)
    }
}
todoList.add {
    for(int i2: 101..200){
        println "task 2:" +i2
        sleep(300)
    }
}

GParsPool.withPool(2) {
    todoList.collectParallel { closure->closure() }
}
frhack
  • 4,862
  • 2
  • 28
  • 25