📜  并行中值滤波器 - 无论代码示例

📅  最后修改于: 2022-03-11 15:00:45.105000             🧑  作者: Mango

代码示例2
public class SecondMedianFilter extends RecursiveAction {
... 

protected void compute() {

    result = new float[length];

    if ((hi - lo) < SEQUENTIAL_CUTOFF) {

        for (int a = lo; a < hi; a++) {


            for (int i = 0; i < length; i++) {
                if (i < window || i >= length - window) {
                    result[i] = numbers[i];
                } else {
                    float[] subArray = Arrays.copyOfRange(numbers, i - window, i + window + 1);
                    Arrays.sort(subArray);
                    float median = subArray[(subArray.length / 2)];
                    result[i] = median;

                }
            }

        }
    } else {

        SecondMedianFilter left = new SecondMedianFilter(filtered, filter, lo, (hi + lo) / 2);
        SecondMedianFilter right = new SecondMedianFilter(filtered, filter, (hi + lo) / 2, hi);
        left.fork();
        //CODE CHANGES FROM HERE
        right.fork();
        //right.compute(); <- IS IMPLICIT IN THE FORK 
        left.join();
        right.join();
        //TO HERE
    }
}


public static void main(String[] args) {
    //reads in a file, processes each line into an array of floats that
    // I call inArray, which gets filtered into outIntArray


    float[] outIntArray = new float[inArray.length];

    if (window < 3 || window > 21 || window % 2 == 0) {
        System.out.println("Window size error.");
    } else {
        // CODE CHANGES FROM HERE
        ForkJoinPool pool = new ForkJoinPool(); // WHO HANDLES THE THREADS 
        SecondMedianFilter smf = new SecondMedianFilter(inArray, window, 0, inArray.length);
        //smf.compute(); <- DUTY OF THREAD POOL
        pool.invoke(smf); //  START OF PROCESSING 
        outIntArray = smf.getRes();


        // loops through outIntArray and writes to file.
    }//end main           
}