Groovy/Grails GPARS: How to execute 2 calculations parallel?
Asked Answered
R

2

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?

Resistencia answered 7/12, 2012 at 9:35 Comment(0)
A
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()
Argyrol answered 7/12, 2012 at 9:54 Comment(5)
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.Resistencia
@Martin Added an alternative using withPool which seems to work with GPars 0.9 :-)Argyrol
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.Resistencia
@Argyrol Thanks a ton! The seconds example with GParsPool works great.Merth
I hadn't seen Dataflow until now. Thanks so much for sharing!Fulgurating
E
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() }
}
Embry answered 7/6, 2015 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.