groovy - ConcurrentException/NullPointerException in GParsPool runForkJoin -


for little context, i'm trying solve project euler problem 31 using excellent gparspool fork/join support.

for that, i've written foolowing code :

import groovyx.gpars.* import groovy.util.groovycollections  @grab(group="org.codehaus.gpars", module="gpars", version="0.11") def getmatchingcombos(target) {     combos = [200, 100 /*, 50, 20, 10, 5, 2, 1*/]     gparspool.withpool(1) { pool ->         combos = combos.collectparallel { n ->               ((0..(target/n)).step(1) treeset).collect { p -> p*n }         }         return gparspool.runforkjoin(combos, 0, 0, target) { usablecombos, comboindex, sum, targetsum ->             def offset = "\t"*comboindex             def results = 0             if(sum<=targetsum) {                 if(comboindex<combos.size()) {                     usablecombos[comboindex].each { n ->                         println offset+"now trying $comboindex element value $n (curent sum $sum)"                         results += forkoffchild(usablecombos, comboindex+1, sum+n, targetsum)                     }                 } else {                     if(sum==targetsum) {                         results +=1                         println offset+"sum target ! have $results"                     }                 }             }             return results;         }     } }  println getmatchingcombos(200) 

unfortunatly, each time try run this, following stack trace :

now trying 0 element value 0 (curent sum 0). known combos [[0, 200], [0, 100, 200]] , target 200         trying 1 element value 0 (curent sum 0). known combos [[0, 200], [0, 100, 200]] , target 20 0 caught: java.util.concurrent.executionexception: java.lang.nullpointerexception         @ groovyx.gpars.gparspool.runforkjoin(gparspool.groovy:305)         @ probleme_31$_getmatchingcombos_closure1.docall(probleme_31.groovy:18)         @ groovyx.gpars.gparspool$_withexistingpool_closure1.docall(gparspool.groovy:170)         @ groovyx.gpars.gparspool$_withexistingpool_closure1.docall(gparspool.groovy)         @ groovyx.gpars.gparspool.withexistingpool(gparspool.groovy:169)         @ groovyx.gpars.gparspool.withpool(gparspool.groovy:141)         @ groovyx.gpars.gparspool.withpool(gparspool.groovy:117)         @ probleme_31.getmatchingcombos(probleme_31.groovy:9)         @ probleme_31.run(probleme_31.groovy:41) 

i understand has way want exploit fork/join recursion "flattening" mechanism, error i'm doing here ?

you're incorrectly trying read children results return value forkoffchild() method, while should done using getchildrenresults().

        return gparspool.runforkjoin(combos, 0, 0, target) { usablecombos, comboindex, sum, targetsum ->         def offset = "\t"*comboindex         def results = 0         if(sum<=targetsum) {             if(comboindex<combos.size()) {                 usablecombos[comboindex].each { n ->                     println offset+"now trying $comboindex element value $n (curent sum $sum)"                     forkoffchild(usablecombos, comboindex+1, sum+n, targetsum)                 }             } else {                 if(sum==targetsum) {                     results +=1                     println offset+"sum target ! have $results"                 }             }         }         results += getchildrenresults().sum(0)         return results;     } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -