The other serial code I wrote works perfectly, but this parallel version is not giving any output, it just prints hello, I am testing it with "hey" it never even gets to that line ie it gets stuck on invoke method, I couldn't find any helpful resources online.
Please help me in understanding where I am going wrong I am new to parallel programming.
The code is a median filter program, that when given an array x=[2,80,6,3] the filtered array is y=[2,6,6,3] calculated as:
y[1] = Median[2 2 80] = 2
y[2] = Median[2 80 6] = Median[2 6 80] = 6
y[3] = Median[80 6 3] = Median[3 6 80] = 6
y[4] = Median[6 3 3] = Median[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){}
}
}
If you type "hey" when run this program you'll get parsing exception which is ignored because of empty catch
block.
Also in your constructor you have this.arrayFiltered = arrayFiltered;
which is actually do nothing because you have no parameter with name arrayFiltered. It lead to arrayFiltered
will not be initialized which lead to NPE in arrayFiltered.add(i, inputArray.get(i));
I suggest yo to put e.printStackTrace();
in catch
block it will immediately show all errors. And of course you need to learn how to use debugger.
Also this part
if (sc.nextLine().length() == 1) {
continue;
}
String[] parts = sc.nextLine().split(" ");
is wrong. because you read two line here. You need to read line once, remember result then test it and pars it.
Like this
String line = sc.nextLine();
if (line.length() == 1) {
continue;
}
String[] parts = line.split(" ");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.