Docsity

Parallel Algorithm Pseudocode Conventions for the PRAM Model | COP 3503, Exams of Computer Science

Toggle sidebar
bg1
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:
Parallel Algorithms III - 1
Parallel Algorithms III (17)
procedure <name> (<list of parameters>)
model: <model name> with p = (function of n> processors
input: <description of input variables>
output: <description of output variables>
dcl: <declaration of local variables and global variables>
function <name> (<list of parameters>)
model: <model name> with p = (function of n> processors
input: <description of input variables>
output: <description of returned value>
dcl: <declaration of local variables and global variables>
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Parallel Algorithm Pseudocode Conventions for the PRAM Model | COP 3503 and more Exams Computer Science in PDF only on Docsity!

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:

Parallel Algorithms III (17)

procedure () model: with p = (function of n > processors input: output: dcl: function () model: with p = (function of n > processors input: output: dcl:

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 do in parallel <statement 1> <statement 2> . . . end in parallel The for clause is a Boolean expression involving array indices. Only those array elements whose indices satisfy the Boolean expression participate in the instructions contained in the body of the in parallel statement. These instructions can be any statements from sequential algorithms that apply to arrays. Care must be taken however, to ensure that we remain within the confines of SIMD architecture (one of our assumptions). Each of the statements within the in parallel statement are executed simultaneously by a set of active processors. Within the pseudocode, there is no specification for how or which of the processors is active, we simply assume that there are enough processors available to handle the task. However, when we draw the pictures to illustrate the action of the algorithm it is still common to match up processors with operations on array components. Since we have assumed that each processor is as powerful as we need it to be, the built-in functions odd, even, interchange sin, read, write and so forth are assumed to be executable by each processor in parallel. An example of the in parallel statement is shown below: for 1  in do in parallel read (A[ i ], B[ i ]) A[ i ] := A[ i ] + sin B[ i ] if A[ i ] > B[ i ] then call interchange (A[ i ], B[ i ]) endif write ( A[ i ]) end in parallel

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  in 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  in 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: with p = processors input: <description of input parameter 1> range: <processor range 1>    range: output: <description of output parameter 1> range: <processor range 1>    range: dcl: range: <processor range 1>    range:

processors are idle. The general form of the syntax for the in parallel statement is: for < processor P >, _ do in parallel <statement 1> <statement 2>    end in parallel Note that the in parallel statement is counted as representing k parallel steps. We’ll see the full meaning of this statement later, but keep it in mind now because it represents a fundamental difference between PRAM and mesh model parallel processing. For interconnection networks, we’ll consider two main types of functions: those that are simply parallel invocations of sequential functions and those that require communication between processors. The first type of function works within each processor’s local memory and does not require communication between processors. For example, consider a parallel invocation of the built-in function sin on a one-dimensional mesh Mp. Then, the statement for Pi , 1  i  n do in parallel B := sin(A) end in parallel where A and B are distributed variables, assigns sin( P i: A ) to P i: B , i = 1, …, n. The second type of function, which requires communication between processors, is restricted to functions that return a scalar. When such a function terminates, it is assumed that the value to be returned by the function resides in a particular processor’s local instantiation of a suitable distributed variable. For example, suppose the scalar value to be returned by a function implemented on a two-dimensional mesh resides in P 1,1: x. Then the return statement is given by: return ( P 1,1: x ). The return statement is used only for convenience and should not be interpreted as returning a value to the front-end processor that is directly accessible to all the processors. The returned value is resident in a suitable output register of P 1,1 only. If other processors require this returned value, then it must be broadcast. Interprocessor Communication Statement

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: AP 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