0

I am busy on a parallel programming assignment, and I am really stuck. To be honest I am not entirely sure how each method works, but I think I have an idea.

I need to sum an array of consecutive values (in parallel). Seems easy enough, but I get 0 as an answer every time I try. I really don't know why.

class SumThreaded extends RecursiveTask<Integer> {
    static int SEQUENTIAL_THRESHOLD = 10000;
    double lo=0.0;
    double hi=0.0;

    long[] arr;

    public SumThreaded(long[] array, double a, double b) {
        arr=array;
        lo=a;
        hi=b;


    }

    public Integer compute() {

        //System.out.println(mid);
        if(hi - lo <= SEQUENTIAL_THRESHOLD) {
            int ans = 0;
            for(int i= (int) lo; i < hi; ++i)
                ans += arr[i];
            return ans;
        }
        else {
            SumThreaded left = new SumThreaded(arr,lo,(hi+lo)/2.0);
            SumThreaded right = new SumThreaded(arr,(hi+lo)/2.0,hi);
            left.fork();
            int rightAns = right.compute();
            int leftAns = left.join();
            return leftAns+rightAns;

        }
    }


    public static void main(String args[]){

        int size = 1000000;
        long [] testArray=new long[size]; 

        for(int i=0;i<size;i++){
            testArray[i]=i+1;
        }

        SumThreaded t = new SumThreaded(testArray,0.0,testArray.length); 
        ForkJoinPool fjPool = new ForkJoinPool();
        int result =fjPool.invoke(t);
        System.out.println(result);

    }
}

Any help would be greatly appreciated.

3
  • 3
    To start with, properly formatting your code makes it much easier to read and debug. Commented Sep 16, 2013 at 1:53
  • 1
    This sure seems like a whole lotta code for what should take just a few lines. Try separating out your concerns so that each class does the minimum it can. Commented Sep 16, 2013 at 2:11
  • Gah! What did you do to the formatting? Commented Sep 16, 2013 at 2:25

1 Answer 1

1

Your problem appears to be that you have two separate constructors for SumThreaded, only one of which sets the class's fields. When you're feeding in the long[] array from the new in sumArray, you throw the array away. You need to pick whether you're using ints or longs (and the sum of a big array is likely to need a long) and then make sure your values are getting set appropriately. Debugging and setting a breakpoint on compute would have shown you this.

Sign up to request clarification or add additional context in comments.

9 Comments

I'm very sorry about that, I completely missed that extra contructor ( it's pretty late :P). I edited the OP, but I am getting a ClassCastException.
Which ClassCastException? It prints out plenty of detail that usually tells you exactly where and what the problem is.
Exception in thread "main" java.lang.ClassCastException: java.lang.Thread cannot be cast to java.util.concurrent.ForkJoinWorkerThread at java.util.concurrent.ForkJoinTask.fork(Unknown Source) at SumThreaded.compute(SumThreaded.java:24) at Main.sumArray(SumThreaded.java:36) at Main.main(SumThreaded.java:49)code` I am really not familiar with the fork/join framework :/
Thanks Theodore, I edited the OP again, but now its a logical error ( the answer is wrong)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.