180 likes | 307 Vues
This lecture covers the development of two key programs: a launcher program for managing the spawning of computational tasks and a Master-Worker model program implementing the Sobel edge detection algorithm. Key aspects include specifying output locations, error handling, and the use of PVM functions. The discussion includes tree and ring gather programs, as well as approaches to load balancing through a "Bag of Tasks" strategy. Students will learn how to read and process PGM files to detect local sharp contrasts in grayscale images using gradient calculations, providing a comprehensive overview of parallel processing techniques.
E N D
CSE 160 – Lecture 10 Programs 1 and 2
Program 1 • Write a “launcher” program to specify exactly where programs are to be spawned, gather output, clean up on error … • Write a ring gather program • Write a tree gather program
Code Outline for Program 1 • Code outlines were given in lecture 5 • We will go over an example implementation for all three parts • Looking carefully at each PVM function • Using XPVM to control a virtual machine • Space-time diagrams • Trace masks • Replaying traces • This will interactive
Program 2 • Implement the Sobel (Section 11.5 in W+A) edge detection algorithm • Input/output files in PGM format • Master/worker configuration • “Bag of tasks” load balancing • Data decomposed by strips • Assignment available on the web on Friday
Edge Detection • Basic problem: Try to find edges (areas of local sharp contrast) in a greyscale image • Think back to basic 1D calculus • When |df/dx| is high, function is changing rapidly • For 2D problems this is generalized to gradients. f(x,y) = (f/x,f/y) • The goal is to find areas in a picture where the | f(x,y) | is high.
Approximating the Magnitude of the Gradient • |f(x,y)| = ( f/x)2 + (f/y)2 • For computational simplicity, this is approximated as |f/x| + |f/y| • Let’s consider the following 3 x 3 matrix of values
Calculating the |Gradient| at X4 • X4 is the pixel of interest. Could try an approximation as • f/x = x5 – x3 (difference left and right) • f/y = x7 – x1 (difference top and bottom) • One gets much better approximation of all surrounding pixels are used to approximate the gradient
The Sobel Operator • Look at calculating f/y • f/y = x6 –x0 + 2(x7 – x1) + x9 – x3 • This is only a weighted average of the gradient by using neighboring pixels. • Extra weight is given to the pixels directly above and below the center pixel of interest • f/x = x2 –x0 + 2(x5 – x3) + x8 – x6
Cross Correlation • Mask Pixels Cross correlation is ij xiwj Want to calculate the cross-correlation at each point in an image using two masks.
Final Calculation • Calculate a new x4’ with the following Sobel formula • x4’ = (1/9) * (|x6 –x0 + 2(x7 – x1) + x9 – x3| + |x2 –x0 + 2(x5 – x3) + x8 – x6)) • Need to do this at every point in the picture.
Master/Worker Model • Master reads input/writes final output • Give an input file, divide the image into K horizontal strips K Strips
Master Spawns P Workers • Master will start P ( K) workers • The first P strips are sent to the P slaves. • Slaves compute the Sobel transform on their strip. Return transformed image to the master • As the master gets data returned, it doles out more strips until the entire image is converted.
Bag of tasks Load Balancing • For this problem, a task is the Sobel transform of an image strip. • Workers may compute at different speeds • Faster workers get more work to do (Sounds like life ) • Slower workers get less work to do • For few workers and may tasks, most workers are busy all of the time.
What to do at the edge of regions to transform? j-1 • Strip j needs 1 row of pixels from strip j+1 and strip j-1 • Have master send these 1 pixel rows as part of the jth strip. • at edges, consider the image to have pixel value 0 outside the pixel area. j j+1
Master Pseudo-code Read command line arguments Spawn P worker tasks Read complete image into an array, pad edges with zeros (N+2)x(M+2) Nrows=ceil((N+2)/K) // rows per block Pkidx=0; //row index to pack stripsLeft = K; for (i = 0; i < P; i++){ label strip i pack label of strip i pack the dimensions of strip i pack the data of strip i send ith strip to ith worker; }
Master Pseudo Code continued stripsleft = K – P; Stripsxformed = 0; While (stripsleft) receive transformed strip from any worker; stripsxformed ++; determine which strip has been sent; Save data; pack strip S = [K – stripsleft]; send strip S to this worker; stripsleft--; }
Master Continued While (stripsxformed < K) receive transformed strip from any worker; stripsxformed++; determine strip that was sent; save data; stripsxformed++; } Broadcast a done message to every worker; Write out transformed data;
Worker Pseudo code done = 0; While (!done) { receive message from parent; (pvm_parent()) determine message tag; (pvm_bufinfo()) if (Tag == donemessage) break; unpack strip id; unpack strip dimension; unpack strip data; transform data; pack strip id, dimension; pack transformed data; send back to parent; }