Java Fork / Join用于大型输入未完成

时间:2015-08-08 11:34:28

标签: java forkjoinpool

我正在尝试在浮点值的输入上完成我的顺序代码的1D中值滤波器的并行版本的编码。我一直在使用一些较小长度的测试值来编写并行版本,最后似乎让代码工作,但是现在在+ -36000元素的大型数据集上,代码似乎只是增加CPU资源而不是完成而我的顺序版本在相同的给定输入上完成。有谁知道我哪里出错了?

测试输入有效:2,6,80,3,1并正确生成:2,6,6,3,1

// median filter code
public class MedianFilter extends RecursiveAction {
    // filter class variables
    float[] numbers;
    int filter;
    int window;
    int length;
    int lo;
    int hi;
    // sequential cutoff set to predetermined value
    static final int SEQUENTIAL_CUTOFF = 500;
    // array used for recursive calls in parallel code
    float[] filtered;

    float[] result;

    public MedianFilter(float[] numbers, int filter, int lo, int hi) {
        this.numbers = numbers;
        this.filter = filter;
        this.lo = lo;
        this.hi = hi;
        length = numbers.length;
        // the section of floats to be filtered (median section)
        window = (filter - 1) / 2;
    }

    protected void compute() {
        filtered = new float[length];
        result = new float[length];

        if ((hi - lo) < SEQUENTIAL_CUTOFF) {
            for (int a = lo; a < hi; a++) {
                // iterate window through all elements of the array
                for (int i = 0; i < length; i++) {
                    // fetch boundary elements
                    if (i < window || i >= length - window) {
                        result[i] = numbers[i];
                    }
                    // fetch elements within filter window
                    else {
                        for (int j = 0; j < filter; j++) {
                            filtered[j] = numbers[i - window + j];
                        }
                        // order elements
                        for (int j = 0; j < filtered.length / 2; j++) {
                            // get the position of the smallest float
                            int min = j;
                            for (int k = j + 1; k < filter; k++) {
                                if (filtered[k] < filtered[min]) {
                                    min = k;
                                }
                                // reorder array for minimum element
                                float temp = filtered[j];
                                filtered[j] = filtered[min];
                                filtered[min] = temp;
                            }
                            // result
                            result[i] = filtered[window];
                        }

                    }
                }
            }

        } else {

            MedianFilter left = new MedianFilter(filtered, filter, lo, (hi + lo) / 2);
            MedianFilter right = new MedianFilter(filtered, filter, (hi + lo) / 2, hi);
            left.fork();
            right.compute();
            left.join();
        }

    }

0 个答案:

没有答案