parallel parentheses matching

26
1 Parallel Parentheses Matching Plus Some Applications

Upload: temira

Post on 07-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Parallel Parentheses Matching. Plus Some Applications. 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. Example. Sequential Solution. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Parallel Parentheses Matching

1

Parallel Parentheses Matching

Plus Some Applications

Page 2: Parallel Parentheses Matching

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

Page 3: Parallel Parentheses Matching

3

Example

1 2 3 4 5 6 7 8

( ( ( ) ) ( ) )

8 5 4 3 2 7 6 1

Page 4: Parallel Parentheses Matching

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?

Page 5: Parallel Parentheses Matching

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.

Page 6: Parallel Parentheses Matching

6

Lemma 2

Any string that satisfies Lemma 2 is of form

( ) ( ) ( ) ( )….( )

and is referred to as form F.

Page 7: Parallel Parentheses Matching

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.

Page 8: Parallel Parentheses Matching

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

Page 9: Parallel Parentheses Matching

9

Example

1 2 3 4 5 6 7 8

( ( ( ) ) ( ) )

0 1 0 0 1 1 1 0

( ( ) ) ( ) ( )

0 1 1 0

( ) ( ) ( ) ( )

Page 10: Parallel Parentheses Matching

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 )

Page 11: Parallel Parentheses Matching

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.)

Page 12: Parallel Parentheses Matching

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?

Page 13: Parallel Parentheses Matching

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

Page 14: Parallel Parentheses Matching

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

Page 15: Parallel Parentheses Matching

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

Page 16: Parallel Parentheses Matching

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

Page 17: Parallel Parentheses Matching

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

Page 18: Parallel Parentheses Matching

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

Page 19: Parallel Parentheses Matching

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

Page 20: Parallel Parentheses Matching

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

Page 21: Parallel Parentheses Matching

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)

Page 22: Parallel Parentheses Matching

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

Page 23: Parallel Parentheses Matching

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

Page 24: Parallel Parentheses Matching

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)

Page 25: Parallel Parentheses Matching

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

Page 26: Parallel Parentheses Matching

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?