我编写的另一个串行代码运行良好,但是这个并行版本没有给出任何输出,它只是打印你好,我正在用“嘿”测试它,它甚至从未到达那一行,即它卡在调用方法上,我不能在网上找不到任何有用的资源。
请帮助我了解我哪里出错了我是并行编程的新手。
该代码是一个中值滤波程序,当给定一个数组 x=[2,80,6,3] 时,过滤后的数组是 y=[2,6,6,3],计算如下:
y[1] = 中位数[2 2 80] = 2
y[2] = 中值[2 80 6] = 中值[2 6 80] = 6
y[3] = 中值[80 6 3] = 中值[3 6 80] = 6
y[4] = 中值[6 3 3] = 中值[3 3 6] = 3
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.io.PrintWriter;
public class ParallelFilter extends RecursiveAction {
static final int SEQUENTIAL_THRESHOLD=500;
ArrayList<Float> inputArray;
int sizeOfFilter;
ArrayList<Float> outputlist;
ArrayList<Float> arrayFiltered;
float high;
float low;
public ParallelFilter(ArrayList<Float> inputArray,int sizeOfFilter,float high,float low)
{
this.inputArray=inputArray;
this.outputlist=outputlist;
this.sizeOfFilter=sizeOfFilter;
this.arrayFiltered=arrayFiltered;
//this.index=index;
this.high=high;
this.low=low;
}
protected void compute()
{
int index=1;
if((high - low) < SEQUENTIAL_THRESHOLD )
{
while(inputArray.size() > sizeOfFilter){
for(int i=0; i<sizeOfFilter;i++){
arrayFiltered.add(i,inputArray.get(i));
}
Collections.sort(arrayFiltered);
float median = arrayFiltered.get(arrayFiltered.size()/2);
outputlist.add(index,median);
inputArray.remove(inputArray.get(0));
arrayFiltered.clear();
index=index+1;
}
outputlist.add(inputArray.get(inputArray.size()-1));
}
else{
ParallelFilter leftTask = new ParallelFilter(inputArray,sizeOfFilter,low,(low+high)/2);
ParallelFilter rightTask = new ParallelFilter(inputArray,sizeOfFilter,(low+high)/2,high);
leftTask.fork();
rightTask.compute();
leftTask.join();
}
}
public static void main(String[] args){
try
{
Scanner sc;
sc = new Scanner(new File("inp1.txt"));
sc.useDelimiter(" ");
ArrayList<Float> inputlist = new ArrayList<>();
//POPULATE LIST FROM FILE
while (sc.hasNextLine()) {
if(sc.nextLine().length() == 1){continue;}
String[] parts = sc.nextLine().split(" "); // split each line by " "
inputlist.add(Float.parseFloat(parts[1])) ;
// System.out.println(parts[1]);
}
System.out.println("Enter filter size : ");
Scanner in = new Scanner(System.in);
int sizeOfFilter = in.nextInt();
if (sizeOfFilter < 3 || sizeOfFilter / 2 == 0) {
System.out.println("Filter size should be odd and bigger than 3");
}
float low = inputlist.get(0);
float high = inputlist.get(inputlist.size()-1);
ParallelFilter pf = new ParallelFilter(inputlist,sizeOfFilter,low,high);
System.out.println("hello");
ForkJoinPool forkJoinPool = new ForkJoinPool();
forkJoinPool.invoke(pf);//suspect the problem is here...
for (int i=0; i<pf.outputlist.size();i++){
System.out.println(pf.outputlist.get(i));}
System.out.println("hey");
}
catch(Exception e){}
}
}