java - Ideas to avoid doing a trick on CyclicBarrier -



java - Ideas to avoid doing a trick on CyclicBarrier -

i running tests parallel processing , made programme given matrix of integers re-calcutes each position's value based on neighbours.

i needed re-create of matrix values wouldn't overriden , used cyclicbarrier merge results 1 time partial problems solved:

cyclicbarrier cyclic_barrier = new cyclicbarrier(n_tasks + 1, new runnable() { public void run() { parallelprocess.mergeresult(); } }); parallelprocess p = new parallelprocess(cyclic_barrier, n_rows, r_cols); // init

each task assigned portion of matrix: i'm splitting in equals pieces rows. might happen divisions not exact there little piece corresponding lasts row wouldn't submitted thread pool.

example: if have 16 rows , n_tasks = 4 no problem, 4 submitted pool. if had 18 instead, first 16 ones submitted, not lastly 2 ones.

so i'm forcing submit if case happens. well, i'm not submitting actually, because using fixed thread pool created executorservice e = executors.newfixedthreadpool(n_tasks). since slots in pool occupied , threads blocked barrier (mybarrier.await() called in run method) couldn't submit pool, used thread.start().

let's go point. since need take consideration cyclicbarrier possibility of chunk remaining, number of parties must incremented 1.

but if case didn't happen, 1 party short trigger barrier.

what's solution?:

if (lower_limit != n_rows) { // remaining chunk processed thread t = new thread(new parallelprocess(lower_limit, n_rows)); t.start(); t.join(); } else { cyclic_barrier.await(); }

i sense cheating when using cyclic_barrier.await() trick raise barrier force.

is there other way approach problem didn't have i'm doing?

though doesn't reply question cyclicbarriers, can recommend using phaser? have ability include number of parties, , allows run mergeresult when phase tripped.

so, before execute async calculation, register. within calculation have thread arrive on phaser. when threads have arrived, advance phase , can invoke overriden method onadvance.

the submission:

parallelprocess process = new parallelprocess(lower_limit, n_rows)); phaser.register(); executor.submit(process);

the processor

public void run(){ //do stuff phaser.arrive(); }

the phaser

phaser phaser = new phaser(){ protected boolean onadvance(int phase, int registeredparties) { parallelprocess.mergeresult(); homecoming true; } }

java multithreading concurrency threadpool cyclicbarrier

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -