如何在Java Fork-Join多线程程序中合并数组?

时间:2015-08-13 00:32:47

标签: java arrays multithreading parallel-processing fork-join

我构建了一个中值滤波器,基本上它是什么,它抓取一个元素数组,过滤它并返回一个过滤后的数组。现在,顺序版本运行完美,但在尝试制作Fork-Join版本时,我无法获得大于我的顺序阈值的数组的任何结果,并且还伴随着ArrayIndexOutOfBounds错误。

现在,我不知道自己哪里出错了,经过几个小时的Google和S.O研究,我放弃了,决定在这里发布这个问题。

这是我的代码片段,按顺序进行过滤:

//Filter Algorithm.
    private void filter(BigDecimal[] elements, int shoulder) {
        //Add boundary values in beginning
        for(int i=0; i<shoulder; i++){
            filteredElements[i] = elements[i];
        }

        //Add boundary values at end
        for(int i=arraySize-1; i>((arraySize-1) - shoulder); i--){
            filteredElements[i] = elements[i];
        }

        //Add middle values to filteredElements array
        for (int i = shoulder; i < elements.length-shoulder; i++) {
            BigDecimal[] windowValue = prepareWindow(elements, shoulder, filterSize);
            BigDecimal median = getMedian(windowValue);
            filteredElements[i] = median;
        }
    }


    /*
     * Pre-condition: Get Windowed Array
     * Post-Condition: Return Median
     */
    private static BigDecimal getMedian(BigDecimal[] windowValue) {
        Arrays.sort(windowValue);
        return windowValue[(filterSize-1)/2];
    }


    /*
     * Pre-condition: Get elements array, get shoulder value and length of filterSize. Notice that this is given name windowLength.
     * Post-Condition: Return Windowed Array
     */
    private static BigDecimal[] prepareWindow(BigDecimal[] elements, int shoulder, int windowLength) {
        BigDecimal[] out = new BigDecimal[windowLength];
        int outCounter = 0;
        for(int i = position; i<position+filterSize; i++){
            out[outCounter] = elements[i];
            outCounter++;
        }
        position++;
        return out;
    }


    //Return Filtered Array
    public BigDecimal[] getFilteredArray(){
        return filteredElements;
    }

现在,如果数组大于连续阈值,则在Fork-Join中应用的相同顺序代码不起作用,我想知道我在哪里出错了。

以下是我的并行实施的片段:

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.concurrent.RecursiveTask;

public class Parallel extends RecursiveTask<BigDecimal[]>{
    BigDecimal[] elements;
    BigDecimal[] filteredElements; //Array that contains the filtered elements
    int shoulder;
    static int filterSize;  
    int begin;
    int end;
    static int position = 0;
    static final int SEQUENTIAL_CUTOFF = 4;


    public Parallel(BigDecimal[] elements, int filterSize, int begin, int end) {
        this.elements = elements;
        Parallel.filterSize = filterSize;
        this.begin = begin;
        this.end = end;
        filteredElements = new BigDecimal[elements.length]; //Array that contains the filtered elements
        shoulder = (filterSize - 1) / 2;
    }

    @Override
    protected BigDecimal[] compute() {

        if (end - begin <= SEQUENTIAL_CUTOFF) {                         
            filter(elements, shoulder); //Run Filter Method
        }else{
            Parallel curLeft = new Parallel(elements, filterSize, this.begin, ((this.begin+this.end)/2));
            Parallel curRight = new Parallel(elements, filterSize, ((this.begin+this.end)/2), this.end);
            curLeft.fork();
            curRight.compute();
            curLeft.join();

        }
        return filteredElements;
    }

    //Filter Algorithm.
        private void filter(BigDecimal[] elements, int shoulder) {          
            //Add boundary values in beginning
            for(int i=0; i<shoulder; i++){
                filteredElements[i] = elements[i];
            }

            //Add boundary values at end
            for(int i=this.elements.length-1; i>((this.elements.length-1) - shoulder); i--){
                filteredElements[i] = elements[i];
            }

            //Add middle values to filteredElements array
            for (int i = shoulder; i < elements.length-shoulder; i++) {
                BigDecimal[] windowValue = prepareWindow(elements, shoulder, filterSize);
                BigDecimal median = getMedian(windowValue);
                filteredElements[i] = median;
            }
        }


        /*
         * Pre-condition: Get Windowed Array
         * Post-Condition: Return Median
         */
        private static BigDecimal getMedian(BigDecimal[] windowValue) {
            Arrays.sort(windowValue);
            return windowValue[(filterSize-1)/2];
        }   


        /*
         * Pre-condition: Get elements array, get shoulder value and length of filterSize. Notice that this is given name windowLength.
         * Post-Condition: Return Windowed Array
         */
        private static BigDecimal[] prepareWindow(BigDecimal[] elements, int shoulder, int windowLength) {
            BigDecimal[] out = new BigDecimal[windowLength];
            int outCounter = 0;
            for(int i = position; i<position+filterSize; i++){
                out[outCounter] = elements[i];
                outCounter++;
            }
            position++;
            return out;
        }

      //Return Filtered Array
        public BigDecimal[] getFilteredArray(){
            return filteredElements;
        }

}

基本上,Parallel实现使用我制作的顺序方法,但我无法使其工作。下面列出了我遇到的错误(我添加了导致错误的行,使阅读更容易。大多数错误都在顺序方法中,我不明白为什么):

Caused by: java.lang.ArrayIndexOutOfBoundsException: 8
    at Parallel.prepareWindow(Parallel.java:80)  > out[outCounter] = elements[i];
    at Parallel.filter(Parallel.java:55) > BigDecimal[] windowValue = prepareWindow(elements, shoulder, filterSize);
    at Parallel.compute(Parallel.java:29) > filter(elements, shoulder); //Run Filter Method
    at Parallel.compute(Parallel.java:34) > curRight.compute();

如果有人可以提供有意义的答案,我将非常感激。

感谢。

1 个答案:

答案 0 :(得分:0)

我们看不到整个代码,但我看到两个并行调用的两个compute()并且它们同时调用prepareWindow()。

最大的问题是position是一个静态变量,并且两个线程都在递增相同的引用,将其推出界限。