Download presentation
Presentation is loading. Please wait.
Published byMax Hancey Modified over 10 years ago
1
Computer Science 320 Clumping in Parallel Java
2
Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel team Have the team execute the computation Clean up
3
Ways to Create a Team new ParallelTeam(n)... Either specify the number of threads or you get the same number of threads as there are processors on your computer new ParallelTeam()...
4
Whats Inside the Box new ParallelTeam(4)...
5
The ParallelRegion Class An abstract class with default implementations of the start, run, and finish methods Override these methods in a new subclass to implement your program
6
Template for Parallel Region new ParallelTeam().execute(new ParallelRegion(){ public void start(){} public void run(){} public void finish(){} }); Note: start() and finish() are called just once, by main. After start() completes, run() is called in each thread simultaneously and main blocks. When all of the threads have signaled main that theyre done, main resumes and calls finish().
8
The Parallel for Loop Used to slice data into clumps to distribute to threads for parallel processing IntegerForLoop is an abstract class with a run(first, last) method that will execute loop code over a range of values from first through last
9
Template for Parallel for Loop new ParallelTeam().execute(new ParallelRegion(){ public void run(){ execute(lower, upper, new IntegerForLoop(){ public void run(int first, int last){ // Loop code } }); } }); Each thread creates its own instance of IntegerForLoop with the same lower and upper bounds of the entire loop The parallel regions execute method partitions this range into the appropriate subranges for each thread Each threads loop/ run method executes with its own range from first to last
10
The Parallel for Loop Each thread executes the start, run, and finish methods within its own instance of IntegerForLoop At the end of its finish(), each thread waits at a barrier until all the threads have finished their loops Parallel execution of the regions run method then resumes
11
Whats Inside the Box new ParallelTeam(4)... new ParallelForLoop(0, 99,...)
12
Variables Shared – typically declared static Per-thread – declared in a for loop instance Loop local – declared in a loops run method main local – for the use of main only, cannot be accessed with threads
13
Variables
14
Sharing and Synchronization If threads only read from memory, no problem But if threads must write or update memory, then we must synchronize them to avoid conflict and inconsistency
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.