1 parallel parentheses matching plus some applications
TRANSCRIPT
1
Parallel Parentheses Matching
Plus Some Applications
2
Parentheses Matching
Problem Definition:Given a well-formed sequence of
parentheses stored in an array, determine the index of the mate of each parentheses stored in the array
3
Example
1 2 3 4 5 6 7 8
( ( ( ) ) ( ) )
8 5 4 3 2 7 6 1
4
Sequential Solution
Traditional solution uses a stackPush left parentheses, pop for a right
parenthesis; these are a pairCan this method be implemented in
parallel? Why or why not?
5
Parallel Solution: Divide & Conquer
Lemma 1: The mate of a parenthesis at an odd position in a balanced string lies in an even position (and vice versa).
Lemma 2: If a balanced string has no left parenthesis at an even location (or, equivalently, a right at an odd location), then the mate of each left parenthesis in the string lies immediately to its right.
6
Lemma 2
Any string that satisfies Lemma 2 is of form
( ) ( ) ( ) ( )….( )
and is referred to as form F.
7
Algorithm Overview
Each left position at an odd position and each right position are marked with a 0.
All others are marked with a 1. These 2 disjoint sets are copied (packed) into a new array.
Repeat for the 2 sets.Stop when each new substring is of
form F.
8
Algorithm Match
For i = 1 to log n – 1 do
if “(“ & index is odd then mark 0
else mark 1
Use segmented prefix sums to compute new index for each parenthesis
Move parentheses to new location
Determine if string is now in form F; if not, terminate – unbalanced string
Match parentheses and store in original array
9
Example
1 2 3 4 5 6 7 8
( ( ( ) ) ( ) )
0 1 0 0 1 1 1 0
( ( ) ) ( ) ( )
0 1 1 0
( ) ( ) ( ) ( )
10
Example – Keep Index
1 2 3 4 5 6 7 8
1 ( 2 ( 3 ( 4 ) 5 ) 6 ( 7 ) 8 )
0 1 0 0 1 1 1 0
1 ( 3 ( 4 ) 8 ) 2 ( 5 ) 6 ( 7 )
0 1 1 0
1 ( 8 ) 3 ( 4 ) 2 ( 5 ) 6 ( 7 )
11
Segmented Prefix Sum
Problem Definition
Given an array containing elements, some marked 0 and some marked 1. Compute the prefix sum of each subset.(For this application the sums will be on values of 1, to number the items.)
12
Segmented Prefix Sum - Example
1 2 3 4 5 6 7 8
( ( ( ) ) ( ) )
0 1 0 0 1 1 1 0
1 1 2 3 2 3 4 4
How can this be accomplished with one prefix sums operation?
13
Parentheses Matching on Hypercube
Use the Divide & Conquer strategyConsider 2 processors
Each PC – assign 0/1P0 send 1’s to P1; P1 send 0’s to P0Each solve the sub-problem
Does the problem split evenly?Consider Large problem – P0 & P2
take 0 items, P1 & P3 take 1 items
14
PPM - Hypercube
Overview of Algorithm2-Cube: Special case of 2 pc
hypercube4-Cube: Used to partition large
sub-problems consisting of 4 pc cubes
Match: The Driving Algorithm
15
Data Distribution
INPUT array is divided into P equal partitions of size n/p
First n/p items are given to P0, next n/p items to P1, etc.
Final MATCH information for each item is stored in the original PC
16
Data Distribution
Array consists of elements INPUT which holds the parentheses & MATCH which will hold the final matching information
Local Match: if the match is determined by the pc in which the match information is to be stored
Non-local: otherwise
17
Algorithm 2-Cube
Mark left and right parentheses with 0/1 as previously discussed
P0 & P1 exchange entries – P0 contains 0 and P1 contains 1
Each PC use stack to sequentially match parentheses
Send non-local match operation to appropriate processor
18
Algorithm 2-Cube – Step 1
1 2 3 4 5 6 7 8
( ( ) ( ( ) ) )
0 1 1 1 0 0 1 0
Mark 0/1
P0 P1
19
Algorithm 2-Cube – Step 2 & 3
1 5 6 8 2 3 4 7
( ( ) ) ( ) ( )
0 0 0 0 1 1 1 1
8 6 5 1 3 2 7 4
Exchange & Match
P0 P1
20
Algorithm 2-Cube – Step 4
1 2 3 4 5 6 7 8
( ( ) ( ( ) ) )
0 1 1 1 0 0 1 0
8 3 2 7 6 5 4 1
Send non-local match information
P0 P1
21
Algorithm 4-Cube
Basis of Algorithm Match Insures near-equal data distribution Overview
Phase 1: local matches determined sequentially & communicated
Phase 2: Unmatched parentheses marked, redistributed; P0 & P1 have 0’s, P2 & P3 have 1’s (half each)
22
Algorithm 4- Cube
Phase 1: Sequential Processing
1. Each PC use stack to match
2. Send non-local Match to other PC
3. Count unmatched parentheses; prefix sum to reindex
23
Algorithm 4-Cube
Phase 2
1. Mark parentheses with 1/0
2. P0 & P2 exchange: P0=0 & P2=1
Likewise, P1=0 and P3=1
3. P0 & P1 exchange number information; likewise for P2 & P3
4. P0 & P1 exchange entries; P0 obtains 1st half: likewise for P2 & P3
24
Algorithm 4-Cube Distribution of Data – 64 entries
P0 Init. 1-16 P1 Init. 17-32
1-2 1-16,33-48 (0) 1-2 17-32, 49-64(0)
3-4 1-32 (0) 3-4 33-64 (0)
P2 Init. 33-48 P3 Init. 49-64
1-2 1-16,33-48 (1) 1-2 17-32, 49-64(1)
3-4 1-32 (1) 3-4 33-64 (1)
25
Algorithm Hypercube Match
1. Each subcube of size 4 executes 4-Cube // Logically P/2 subcubes with independent subproblems
2. Each subcube (p/2) prefix sums to determine new index
3. Each subcube from Step 1 recursively repeat 1, 2, 3 until each subcube is of size 2
4. Execute 2-Cube to complete the solution
26
Algorithm Hypercube Match
Complexity AnalysisO (log 2 p + n/p log p)For p=n/log n simplifies to
O(log 2 n)
Is the Algorithm Optimal?