java - I can't identify the issue with my parallel run timer -
i have program applies median filter array of on 2 million values.
i'm trying compare run times sequential vs parallel on same dataset. when execute program, 20 runs, every run timed, , average of 20 times outputted console.
arraylist<double> times = new arraylist<>(20);//to calculate average run time (int run = 1; run < 21; run++) //algorithm run 20 times { long starttime = system.nanotime(); switch (method) { case 1: //sequential filt.seqfilter(); break; case 2: //forkjoin framework pool.invoke(filt); //pool forkjoin break; } double timeelapsed = (system.nanotime() - starttime) / 1000000.0; times.add(run - 1, timeelapsed); system.out.println("run " + run + ": " + timeelapsed + " milliseconds."); } times.remove(collections.max(times)); //there's slow outlier double timessum = 0; (double e : times) { timessum += e; } double average = timessum / 19; system.out.println("runtime: " + average);
filt
of type filterobject
extends recursiveaction
. overridden compute()
method in filterobject
looks this:
public void compute() { if (hi - lo <= sequential_threshold) { seqfilter(); } else { filterobject left = new filterobject(lo, (hi + lo) / 2); filterobject right = new filterobject((hi + lo) / 2, hi); left.fork(); right.compute(); left.join(); } }
seqfilter()
processes values between lo
, hi
indices in starting array , adds processed values final array in same positions. that's why there no merging of arrays after left.join()
.
my run times insanely fast parallel - fast think there must wrong timer or left.join()
statement. i'm getting average times of around 170 milliseconds sequential filtering window of size 3 , 0.004 milliseconds parallel. why getting these values? i'm concerned join()
in wrong place.
if you'd see entire code, classes , input files, follow link.
after testing of code found reason. turned out forkjoinpool runs 1 task instance once. subsequent invoke() calls same task instance return immediately. have reinstantiate task every run.
another problem parallel (standard threads) run. starting threads never waiting them finish before measuring time. think use cyclicbarrier here.
with mentioned fixes same time forkjoin , standard threads. , it's 3 times faster sequential. seems reasonable.
p.s. doing micro-benchmark. may useful read answers question improve benchmark accuracy: how write correct micro-benchmark in java?
Comments
Post a Comment