Re: Bug or user error on Fork Join RecursiveAction

9 views
Skip to first unread message
Message has been deleted

Marshall Pierce

unread,
Nov 24, 2010, 2:35:15 AM11/24/10
to hackerdojo-javaconcurrency
This might be your issue:
slowsum = slowsum + arr[i];

I also added a task implementation to the example code to do sum of
squares.

On Nov 23, 11:34 am, doug chang <dougchan...@gmail.com> wrote:
> Hi Marshall: this is probably user error on my part. I didn't do
> anything but cut and paste some code fromhttp://gee.cs.oswego.edu/dl/jsr166/dist/docs/
>
> on the class RecursiveAction. I did add a test case and I can't get it
> to work. The 2 sums should be equal, the fork join sum and the sum of
> the array calculated in a for loop. If I fill the array with ones, it
> works. Hopefully I didn't do something really really stupid. Which I
> probably did. :)
>
> import java.util.concurrent.*;
>
> public class TestApplyer {
>
>         double sumOfSquares(ForkJoinPool pool, double[] array) {
>                    int n = array.length;
>                    Applyer a = new Applyer(array, 0, n, null);
>                    pool.invoke(a);
>                    return a.result;
>                  }
>         public static void main(String []args){
>                 double arr[] = new double[1000000];
>                 for (int i=0;i<arr.length;i++){
>                         arr[i]=(double)2;
>                 }
>                 ForkJoinPool fp = new ForkJoinPool();
>
>                 long start = System.nanoTime();
>                 TestApplyer ta = new TestApplyer();
>                 System.out.println("ForkJoin sum:"+ta.sumOfSquares(fp,arr));
>                 long end = System.nanoTime();
>                 System.out.println("elapsed time:"+(end-start));
>                 //time for nonfork
>                 long start2 = System.nanoTime();
>                 double slowsum = 0;
>                 for (int i=0;i<arr.length;i++){
>                         slowsum =  slowsum + arr[i];
>                 }
>                 long end2 = System.nanoTime();
>                 System.out.println("slowsum:"+slowsum+" elapsed time non fork
> join :"+(end2-start2));
>
>         }
>         class Applyer extends RecursiveAction {
>                 private static final long serialVersionUID = 1L;
>                 final double[] array;
>                    final int lo, hi;
>                    double result;
>                    Applyer next; // keeps track of right-hand-side tasks
>                    Applyer(double[] array, int lo, int hi, Applyer next) {
>                      this.array = array; this.lo = lo; this.hi = hi;
>                      this.next = next;
>                    }
>
>                    double atLeaf(int l, int h) {
>                      double sum = 0;
>                      for (int i = l; i < h; ++i) // perform leftmost base step
>                        sum += array[i] * array[i];
>                      return sum;
>                    }
>
>                    protected void compute() {
>                      int l = lo;
>                      int h = hi;
>                      Applyer right = null;
>                      while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) {
>                          int mid = (l + h) >>> 1;
>                         right = new Applyer(array, mid, h, right);
>                         right.fork();
>                         h = mid;
>
>                      }
>                      double sum = atLeaf(l, h);
>                      while (right != null) {
>                         if (right.tryUnfork()) // directly calculate if not stolen
>                           sum += right.atLeaf(right.lo, right.hi);
>                        else {
>                           right.join();
>                           sum += right.result;
>                         }
>                         right = right.next;
>                       }
>                      result = sum;
>                    }
>                  }
>
>
>
>
>
>
>
> }
Reply all
Reply to author
Forward
0 new messages