






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Parallel Algorithm Pseudocode Conventions for the PRAM Model While there is no standard for parallel algorithm design, there are several conventions that are widely adopted for describing parallel algorithms. Since the PRAM model exhibits a shared memory, the pseudocode for PRAMs is an extension of the pseudocode which is utilized for sequential algorithms. The major new elements are the in parallel statement, the parallelcall statement, and the various opening syntax specifications. To avoid side effects, we will assume that all input parameters (that are not also output parameters) are passed by value. All parameters of a parallel function are assumed to be input parameters. Opening Syntax Specification – PRAM The opening syntax for a procedure written for a PRAM machine has the following form: The opening syntax for a function written for a PRAM machine has the following form:
procedure ) model:
) model:
The in parallel Statement In addition to the usual instructions for serial machines, parallel models have an in parallel statement, which allows operations to be performed on more than one component of the array simultaneously. The general form of the in parallel statement is: for
instructions would be performed simultaneously. This necessitates the altered version of the algorithm illustrated in Figure 2, which utilizes two in parallel statements. An equivalent case arises for case statements executed in parallel. In the SIMD model these statements would be translated into a sequence of in parallel statements, one for each case clause. Algorithms for the EREW PRAM Model Using the syntax we have developed for the PRAM model, we’ll develop algorithms in this section of the notes for the searching problem that we discussed previously. Searching on a EREW PRAM Model Recall that searching a list of values for the occurrence of a specific value involved assigning the value of the search element to each entry in a temporary array, temp[1:n]. At first glance, it might seem that the following code segment would accomplish this task: for 1 i n do in parallel temp[i] := x end in parallel However, this statement is not admissible on an EREW PRAM (unless n = 1), since it calls for the single memory location x to be read concurrently by n processors. In order to fit the EREW PRAM model, we need to use the broadcasting method which is implemented in the algorithm in Figure 3. Figure 3 – EREW PRAM broadcast algorithm. procedure broadcastPRAM (A[1:n], x) input: A[1:n] (an array of size n, where n = 2k) x (a value to be broadcast throughout A[1:n]) output: A[1:n] (array where A[i] = x, i = 1, …, n) A[1] := x for i := 1 to k do for 2 i-1^ + 1 j 2 i^ do in parallel A[ j ] := A[ j – 2i-1] end in parallel endfor end broadcastPRAM
Recall from our early examination of this parallel algorithm that once the value of x has been broadcast throughout temp[1:n] , each processor Pi in parallel compares L[i] to temp[i] = x and writes i back to temp[i] if L[i] = x , otherwise it writes the value in temp[i]. The comparison step is accomplished by the code segment shown in Figure 4. Figure 4 – The comparison step for parallel searching. The parallel searching algorithm was not completed until the result of the search was returned to the calling function. This was accomplished through the binary fan-in technique to move the minimum value in the list into the first position in the array. The parallel code to accomplish this is shown in Figure 5. Figure 5 – Parallel algorithm for binary fan-in technique. Now with all the pieces in place, we can assemble our final algorithm for searching in an EREW PRAM, which is shown in Figure 6. for 1 i n do in parallel if L[ i ] = temp[ i ] then temp[ i ] := i else temp[ i ] := endif end in parallel function minPRAM( L[1:n]) model: EREW PRAM with p = n/2 processors input: L[1:n] (a list of size n , n = 2k) output: the minimum value of a list element in L for j := 1 to log 2 n do for 1 i n/2j^ do in parallel if L[2i-1] > L[2i] then L[i] := L[2i] else L[i] := L[2i-1] endif end in parallel endfor return (L[1]) end minPRAM
The front-end variable parameters are usually used to specify ranges of indices in the input and output statements. The output statement contains similar specifications for all output parameters. As with the PRAM model, the declaration of local variables (if needed) is done via the dcl statement. Those local variables that are distributed also have a range specification. The general format of the opening syntax is shown below: The in parallel Statement The in parallel statement is similar to that for the PRAM, except that the for clause contains a Boolean expression involving processor indices instead of array indices. The processor index might be a single index as in the one dimensional mesh, a pair of indices as in the two-dimensional mesh, and a bit string in the case of the hypercube, and so forth. It is implicit in the pseudocode that we are developing that each processor involved in an in parallel statement recognizes its own index ( pid ). [In practice, this is commonly accomplished using a distributed variable id in the read-only local memory of each processor. Then for each processor P , P : id would contain the index of processor P .] Each processor whose index does not satisfy the Boolean expression in the for clause is idle (masked out). The active processors simultaneously execute the instructions in the body of the in parallel statement, while the remaining procedure ) model:
processors are idle. The general form of the syntax for the in parallel statement is: for < processor P >,
processors are communicating to the right in the same parallel communication step. If the communication involves the same distributed variable, the term propagate rather than communicate is used. Figure 8 illustrates a parallel communication step where processors are both source and target. for Pi, 1 i n-1 do in parallel P i+1: A P i: A {propagate A to the right} end in parallel before execution after execution Figure 8 – Action of a parallel communication step on a 1-d mesh with processors being both source and target. Figure 8 presents a reasonable view of interprocessor communication in light of the systolic nature of a communication step. At the beginning of a communication step, all active source processors initiate the process of sending (pulsing) data from their local memory to the relevant adjacent processor. At the end (after unit time has elapsed) of the communication step, this data has been received and assigned by the target processor. Thus, it is both a natural and common feature of many parallel algorithms to have the same processor send information at the beginning of the communication step and receive information at the end of the step. However, it is not permitted for any processor to be the target or source of more than one processor during a single communication step. A B 78 2 A B 78 3 A B 2 7 A B 231 11 P 1 P 2 P 3 P 4 A B 78 110 A B 2 3 A B 231 246 A B 7 11 P 1 P 2 P 3 P 4