1. Trang chủ
  2. » Công Nghệ Thông Tin

THE JR PROGRAMMING LANGUAGE phần 7 ppt

40 233 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 40
Dung lượng 761,46 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

214 Parallel Matrix MultiplicationThe compute method creates the strip processes using send invocationsand then waits for them to complete using a semaphore.. The main class isidentical

Trang 1

214 Parallel Matrix Multiplication

The compute method creates the strip processes using send invocationsand then waits for them to complete using a semaphore That code could not

be replaced (only) by declaring the equivalent family of processes (using theprocess abbreviation) because those processes might execute before the code

in the compute method initializes instance variables used within strip (SeeExercise 15.3.)

Many shared-memory multiprocessors employ caches, with one cache perprocessor Each cache contains the memory blocks most recently referenced

by the processor (A block is typically a few contiguous words.) The purpose

of caches is to increase performance, but they have to be used with care by theprogrammer or they can actually decrease performance (due to cache conflicts).Reference [22] gives three rules-of-thumb programmers need to keep in mind:Perform all operations on a variable, especially updates, in one process.Align data so that variables updated by different processors are in differentcache blocks

Reuse data quickly when possible so it remains in the cache and does notget “spilled” back to main memory

A two-dimensional array in Java is an array of references to dimensional arrays So, a matrix is stored in row-major order (i.e., by rows),although adjacent rows are not necessarily contiguous The above program,therefore, uses caches well Each strip process reads one distinct strip of

single-A and writes one distinct strip of C, and it references elements of single-A and C bysweeping across rows Every process references all elements of B, but that isunavoidable (If B were transposed, so that columns were actually stored inrows, it too could be referenced efficiently.)

Trang 2

15.2 Dynamic Scheduling: A Bag of Tasks 215

The algorithm in the previous section statically assigned an equal amount

of work to each strip process If the processes execute on homogeneousprocessors without interruption, they would be likely to finish at about thesame time However, if the processes execute on different-speed processors,

or if they can be interrupted—e.g., in a timesharing system—then differentprocesses might complete at different times To dynamically assign work toprocesses, we can employ a shared bag of tasks, as in the solution to the adaptivequadrature problem in Section 7.7 Here we present a matrix multiplicationprogram that implements such a solution The structure of the solution isillustrated in Figure 15.2

Figure 15.2 Replicated workers and bag of tasks

As in the previous program, we employ two classes The main class isidentical to that in the previous section: it again creates a multiplier object,calls the object’s compute method, and then prints out results

The multiplier class is similar to that in the previous section in that it declares

N, A, and B It also declares and initializes W, the number of worker processes.The class declares an operation, bag, which is shared by the worker processes.The code in method compute sends each row index to bag It then creates theworker processes, waits for them to terminate, and returns results to the invoker.Each worker process repeatedly receives a row index r from bag and computes

N inner products, one for each element of row r of result matrix C However, ifthe bag is empty, then the worker process notifies the compute method that ithas completed and terminates itself (See Exercises 15.5 and 15.6.)

Trang 3

216 Parallel Matrix Multiplication

This way of detecting when to terminate works here because once the bagbecomes empty, no new tasks are added to it; this way would not work for otherproblems where the bag might become empty before additional tasks are placedinto it For examples, see the adaptive quadrature example in Section 7.7, andthe two solutions to the traveling salesman problem in Sections 17.2 and 17.3.This program should show nearly perfect speedup—over the one worker andone processor case—for reasonable-size matrices, e.g., when N is 100 or more

In this case the amount of computation per iteration of a worker process faroutweighs the overhead of receiving a message from the bag Like the previous

Trang 4

15.3 A Distributed Broadcast Algorithm 217

Figure 15.3 Broadcast algorithm interaction pattern

program, this one uses caches well since JR stores matrices in row-major order,and each worker fills in an entire row of c If the bag of tasks contained columnindices instead of row indices, performance would be much worse becauseworkers would encounter cache update conflicts

The program in the previous section can be modified so that the workers donot share the matrices or bag of tasks In particular, each worker (or addressspace) could be given a copy of A and B, and an administrator process coulddispense tasks and collect results (see Exercise 15.4) With these changes, theprogram could execute on a distributed-memory machine

This section and the next present two additional distributed algorithms Tosimplify the presentation, we use processes, one to compute each elementC[r] [c] Initially each such process also has the corresponding values of A and

B, i.e., A[r] [c] and B [r] [c] In this section we have each process broadcastits value of A to other processes on the same row and broadcast its value of B toother processes on the same column In the next section we have each processinteract only with its four neighbors Both algorithms are inefficient as givensince the grain size is way too small to compensate for communication overhead.However, the algorithms can readily be generalized to use fewer processes, each

of which is responsible for a block of matrix C (see Exercises 15.11 and 15.12).Our broadcast implementation of matrix multiplication uses three classes: amain class, a multiplier class, and a point class The main class is identical tothose in the previous sections

Instances of class Point carry out the computation The multiplier classcreates one instance for each value of C[r][c] Each instance provides threepublic operations: one to start the computation, one to exchange row values, andone to exchange column values Operation compute is serviced by a method;

it is invoked by a send statement in the multiplier class and hence executes as

a process The arguments of the compute operation are references for other

Trang 5

218 Parallel Matrix Multiplication

instances of Point Operations rowval and colval are serviced by receivestatements; they are invoked by other instances of Point in the same row r andcolumn c, respectively

The instances of Point interact as shown in Figure 15.3 The computeprocess in Point first sends its value of Arc to the other instances of Point

in the same row and receives their elements of A The compute process thensends its value of Brc to other instances of Point in the same column andreceives their elements of B After these two data exchanges, Point (r, c) nowhas row r of A and column c of B It then computes the inner product of thesetwo vectors Finally, it sends its value of Crc back to the multiplier class

Trang 6

15.3 A Distributed Broadcast Algorithm 219

The multiplier class creates instances of Point and gets back a referencefor each, which it stores in matrix pref It then invokes the compute operations,passing each instance of Point references for other instances in the same rowand column We use pref[r] to pass row r of pref to compute But, wemust extract the elements in column c of pref and store them in a new array,cpref, which we then pass to compute It then waits for all points to finishtheir computations and gathers the results, which it returns to the invoker

Trang 7

220 Parallel Matrix Multiplication

Figure 15.4 Heartbeat algorithm interaction pattern

As noted, this program can readily be modified to have each instance ofPoint start with a block of A and a block of B and compute all elements of ablock of C It also can be modified so that the blocks are not square, i.e., stripscan be used In either case the basic algorithmic structure and communicationpattern is identical The program can also be modified to execute on multiplevirtual machines: The multiplier class first creates the virtual machines andthen creates instances of Point on them

In the broadcast algorithm, each instance of Point acquires an entire row of

A and an entire column of B and then computes their inner product Also, eachinstance of Point communicates with all other instances on the same row andsame column Here we present a matrix multiplication algorithm that employsthe same number of instances of a Point class However, each instance holdsonly one value of A and one of B at a time Also, each instance of Pointcommunicates only with its four neighbors, as shown in Figure 15.4 Again,the algorithm can readily be generalized to work on blocks of points and toexecute on multiple virtual machines

As in the broadcast algorithm, we will use processes, one to compute eachelement of matrix C Again, each initially also has the corresponding elements of

A and B The algorithm consists of three stages [37] In the first stage, processesshift values in A circularly to the left; values in row r of A are shifted left r

Trang 8

15.4 A Distributed Heartbeat Algorithm 221columns Second, processes shift values in B circularly up; values in column

c of B are shifted up c rows The result of the initial rearrangement of thevalues of A and B for a 3 × 3 matrix is shown in Figure 15.5 (Other initial

Figure 15.5. Initial rearrangement of 3 × 3 matrices A and B

rearrangements are possible; see Exercise 15.9.) In the third stage, each processmultiplies one element of A and one of B, adds the product to its element of C,shifts the element of A circularly left one column, and shifts the element of Bcircularly up one row This compute-and-shift sequence is repeated N-1 times,

at which point the matrix product has been computed

We call this kind of algorithm a heartbeat algorithm because the actions of

each process are like the beating of a heart: first send data out to neighbors,then bring data in from neighbors and use it To implement the algorithm in

JR, we again use three classes, as in the broadcast algorithm Once again, themain class is identical to those in the previous sections

The computation is carried out by instances of a Point class, which vides three public operations as in the broadcast algorithm However, here thecompute operation is passed references for only the left and upward neighbors,and the rowval and colval operations are invoked by only one neighbor Also,the body of Point implements a different algorithm, as seen in the following

Trang 9

pro-222 Parallel Matrix Multiplication

Method compute in the multiplier class creates instances of Point and passeseach references for its left and upward neighbors The compute method starts

up the computation in the Point objects and gathers the results from all thepoints

Trang 10

Exercises 223

The prev method uses modular arithmetic so that instances of Point on the leftand top borders communicate with instances on the right and bottom borders,respectively

Exercises

15.1 Determine the execution times of the programs in this chapter To do so,place an invocation of System currentTimeMillis() just before thecomputation begins and another just after the computation completes.The difference between the two values returned by this method is thetime, in milliseconds, that the JR program has been executing

15.2 Modify the prescheduled strip algorithm so that N does not have to be amultiple of PR

15.3 Rewrite the MMMultiplier class in Section 15.1 so that the strip cesses are declared as a family of processes using the process abbrevi-ation Be sure your solution prevents the potential problem mentioned inthe text; i.e., it prevents these processes from executing before instancevariables have been initialized

pro-15.4 Change the bag of tasks program so that it does not use shared variables.15.5 Suppose we change the code in the MMMultiplier class in Section 15.2

so that the compute method does not create the processes Instead theyare created using the process abbreviation:

Is this new program correct?

15.6 Suppose we change the code in the MMMultiplier class in Section 15.2

so that the worker process executes the following code

Trang 11

224 Parallel Matrix Multiplication

Is this new program correct?

15.7 The compute process in class Point in Section 15.3 contains the lowing receive statement:

fol-This statement is within a for statement

(a)

(b)

Write an equivalent input statement for the receive statement.Explain why the receive statement cannot be simplified to the fol-lowing, assuming the declaration of rowval is changed to omit thesender field:

15.8

15.9

Suppose A and B are 5 × 5 matrices Determine the location of eachvalue of A and B after the two shift stages of the heartbeat algorithm inSection 15.4

Reconsider the initial rearrangement phase of the heartbeat algorithm inFigure 15.5 Suppose instead that each row r of A is shifted left r+1columns and that each column c of B is shifted up c+1 rows Show thisinitial rearrangement for when A and B are 3 × 3 matrices Will theheartbeat algorithm still multiply arrays correctly?

Now suppose that each row r of A is shifted left r+2 columns and thateach column c of B is shifted up c+2 rows Repeat the above questions,

If possible, generalize the above results

Determine the total number of messages that are sent in the distributedbroadcast algorithm and the size of each Do the same for the distributedheartbeat algorithm Explain the differences

Modify the broadcast algorithm so that each instance of Point is sponsible for a block of points Use processes, where N is a multiple

re-of PR

15.10

15.11

Trang 12

Exercises 22515.12

respon-Compare the performance of the various programs presented in this ter or those that you developed in answering the above exercises.Implement matrix multiplication using a grid of filter processes [26, 7].Implement Gaussian elimination (see Exercise 4.17) using the techniquesillustrated in this chapter

Trang 13

chap-This page intentionally left blank

Trang 14

Chapter 16

SOLVING PDEs: GRID COMPUTATIONS

Partial differential equations (PDEs) are used to model a variety of differentkinds of physical systems: weather, airflow over a wing, turbulence in fluids, and

so on Some simple PDEs can be solved directly, but in general it is necessary toapproximate the solution at a finite number of points using iterative numericalmethods In this chapter we show how to solve one specific PDE—Laplace’sequation in two dimensions—by means of a grid computation, which employswhat is called a finite-difference method As in the previous chapter, we presentseveral solutions that illustrate a variety of programming techniques and theirrealizations in JR

Laplace’s equation is an example of what is called an elliptic partial ential equation The equation for two dimensions is the following:

differ-Function represents some unknown potential, such as heat or stress

Given a fixed spatial region and solution values for points on the boundaries ofthe region, our task is to approximate the steady-state solution for points withinthe interior We can do this by covering the region with an evenly spaced grid ofpoints, as shown in Figure 16.1 Each interior point is initialized to some value.The steady-state values of the interior points are then calculated by repeatediterations On each iteration the new value of a point is set to a combination ofthe old and/or new values of neighboring points The computation terminateswhen every new value is within some acceptable difference of every old value.There are several stationary iterative methods for solving Laplace’sequation—Jacobi iteration, Gauss-Seidel, and successive over-relaxation(SOR) In Jacobi iteration, the new value for each point is set to the average

of the old values of the four neighboring points Jacobi iteration can be

Trang 15

paral-228 Solving PDEs: Grid Computations

Figure 16.1. Approximating Laplace’s equation using a grid

lelized readily because each new value is independent of the others AlthoughJacobi iteration converges more slowly than other methods, we will use it inthis chapter since it is easier to program In any event, parallel computationsthat use other iterative methods employ basically the same communication andsynchronization patterns

A data parallel algorithm is an iterative algorithm that repeatedly and inparallel manipulates a shared array [23] This kind of algorithm is most closelyassociated with synchronous (SIMD) multiprocessors, but it can also be used onasynchronous multiprocessors Here we present a data parallel implementation

Trang 16

16.1 A Data Parallel Algorithm 229

The main class declares the grid size N, border values (left, top, right, andbottom), and the convergence criterion epsilon N is the number of rows andcolumns in the grid of interior points, i.e., points whose steady-state value is to

be computed The main method reads these values from input or as line arguments (not shown in the code) It then creates a Jacobi object, whichwill be used for the actual computation The main method then invokes thecompute in the Jacobi object, gets back the results, and prints them out

command-The Jacobi class provides the compute method This method is passed thegrid size N, border values (left, top, right, and bottom), and the convergencecriterion epsilon It initializes an array that contains old and new grid valuesand two variables that are used to index grid The current (old) grid valuesare grid[cur] and the next (new) grid values are grid[nxt] The code later

in this section reads values from the old grid and writes values to the new grid

on each iteration At the end of each iteration, the code replaces the old values

by the new values by simply swapping nxt and cur, which is more efficientthan copying grid[nxt] to grid[cur] element by element (Exercise 16.5explores this issue in further detail.)

Trang 17

230 Solving PDEs: Grid Computations

Array grid consists of two matrices Each matrix has N+2 rows and columns

so the boundary values can be stored within the matrix This avoids having totreat the boundaries as special cases in the main computation For simplicityeach interior grid point is initialized to zero; for faster convergence each should

be initialized to a value that somewhat approximates the expected final value.After initializing the grid, the compute method performs the actual iterativecomputation It initializes the array diff, which is used to record differences foreach point between successive iterations It then invokes the compute method.The main loop in compute has three phases, as outlined above The first phase

is implemented by a co statement that makes calls of update The secondphase is implemented by swapping the two indices, which switches the roles ofthe two grids The third phase is implemented by a second co statement thatmakes N calls of check_diff s and by an if statement that exits the loop if thegrid values have converged

Trang 18

16.1 A Data Parallel Algorithm 231

Trang 19

232 Solving PDEs: Grid Computations

After the first group of processes have completed, matrix diff contains thedifferences between all old and new grid points Then, a second group ofprocesses determines the maximum difference: N instances of check_diffsrun in parallel, one for each row i Each instance of check_diffs storesthe maximum difference of the elements in its row in diff[i][1] The codethen updates local variable maxdiff, which contains the maximum of all thedifferences If this value is at most epsilon, we exit the loop and return theresults

The main loop in the algorithm in the previous section repeatedly ates numerous processes and then waits for them to terminate Process cre-ation/destruction is much more time consuming than most forms of interpro-cess synchronization, especially when processes are repeatedly created anddestroyed Hence we can implement a data parallel algorithm much more ef-ficiently on an asynchronous multiprocessor by creating processes once andusing barriers to synchronize execution phases (See Reference [7] for furtherdiscussion of this topic.) We can make the implementation even more efficient

cre-by having each process handle several points of the grid, not just one

This section presents a parallel algorithm for Jacobi iteration that uses a fixednumber of processes As in the matrix multiplication algorithm in Section 15.1,each process is responsible for a strip of the grid In particular, for an N × Ngrid, we use PR processes, with each responsible for S rows of the grid Thesolution also illustrates one way to implement a monitor [25] in JR

Our program employs four classes: a main class, a barrier class, a Jacobiclass, and a results class The results class is the same as in the previous section.The main class is similar to the one in the previous section The differences arethat it now also reads in PR, computes the strip size, and passes both to Jacobi’sconstructor

Trang 20

16.2 Prescheduled Strips 233

The BarrierSynch class implements a barrier synchronization point for

PR processes It is essentially a monitor, with mutual exclusion and conditionsynchronization implemented using semaphores In particular, the class pro-vides one public operation barrier Processes call barrier when they reach

a barrier synchronization point All but the last to arrive block on a semaphore.The last to arrive awakens those that are sleeping and resets the local variables

Two delay semaphores are needed to prevent processes that are quick to arrive

at the next barrier synchronization point from “stealing” signals intended forprocesses that have not yet left the previous barrier Their use is analogous tothe use of an array of condition variables in a monitor Variable sleep indicateswhich element of delay a process is to block on; its value alternates between 0and 1 Before blocking, a process copies the current value of sleep into a localvariable; this is necessary since the value of sleep could otherwise changebefore the process blocks (see Exercise 16.6)

Ngày đăng: 12/08/2014, 13:22

TỪ KHÓA LIÊN QUAN