69
Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce Sorting Networks Prüfung 1

Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Vorlesung Datenstrukturen und AlgorithmenLetzte Vorlesung 2018

Felix Friedrich, 30.5.2018

Map/Reduce

Sorting Networks

Prüfung

1

Page 2: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

MAP AND REDUCE AND MAP/REDUCE

2

Page 3: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Summing a Vector

3

+

+

+

+

+

+

+

Accumulator

+ + + +

++

+

+

Divide and conquer

Q: Why is the result the same?

A: associativity: (a+b) + c = a + (b+c)

Page 4: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Summing a Vector

+ + + +

++

+

+

Divide and conquer

+ + ++

Is this correct?

+ +

+

4

Only if the operation is commutative:

a + b = b + a

Page 5: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Reductions

Simple examples: sum, max

Reductions over programmer-defined operations

– operation properties (associativity / commutativity) define the correct executions

– supported in most parallel languages / frameworks

– powerful construct

5

Page 6: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

C++ Reduction

std::accumulate (requires associativity)

std::reduce (requires commutativity, from C++17, can specify execution policy)

std::vector<double> v;

...

double result = std::accumulate(v.begin(), v.end(), 0.0, [](double a, double b){return a + b;}

);

6

Page 7: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Elementwise Multiplication

Map

7

x

+

Multiply

x x x x x x x

Page 8: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Scalar Product

Map

Reduce

8

x

+

Multiply

x x x x x x x

+ + + +

++

+

+

Accumulate

Page 9: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

C++ Scalar Product (map + reduce)

// example data

std::vector<double> v1(1024,0.5);

auto v2 = v1;

std::vector<double> result(1024);

// map

std::transform(v1.begin(), v1.end(), v2.begin(), result.begin(), [](double a, double b){return a*b;});

// reduce

double value = std::accumulate(result.begin(), result.end(), 0.0); // = 256

9

Page 10: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Map & Reduce = MapReduce

Combination of two parallelisation patterns

result = 𝑓 in1 ⊕𝑓 in2 ⊕𝑓(in3) ⊕ 𝑓(in4)

𝑓 = map

⊕ = reduce (associative)

Examples: numerical reduction, word count in document, (word, document) list, maximal temperature per month over 50 years (etc.)

10

Page 11: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Motivating Example

Maximal Temperature per Month for 50 years

• Input: 50 * 365 Days / Temperature pairs

• Output: 12 Months / Max Temperature pairs

Assume we (you and me) had to do this together. How would we distribute the work?What is the generic model?How would we be ideally prepared for different reductions (min, max, avg)?

11

Page 12: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Maximal Temperature per Month: Map

12

data

01 / 501 / 801 / 8

....03 / 1203 / 14

....

05/2005/1905/20

...07/2807/38

...

... ... ... ... ... ...

03/1403/18

.......

each map-process gets day/temperature pairs

and maps them to month/temperature pairs

Page 13: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Maximal Temperature per Month: Shuffle

13

01 / -501 / -801 / -8

....03 / 1203 / 14

....

05/2005/1905/20

...07/2807/38

...

... ... ... ... ... ...

03/1403/18

....

Jan

01 / -501 / -801 / -8

....

Feb

02 / 1302 / 1402 / 12

....

Mar

03 / 1303 / 1403 / 12

....

April

04 / 2304 / 2404 / 22

....

Dec

12 / 012 / -212/ 2

....

...

data gets sorted / shuffled by month

Page 14: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Maximal Temperature per Month: Reduce

14

Jan

01 / -501 / -801 / -8

....

Feb

02 / 1302 / 1402 / 12

....

Mar

03 / 1303 / 1403 / 12

....

April

04 / 2304 / 2404 / 22

....

Dec

12 / 012 / -212/ 2

....

Jan

18

Feb

20

Mar

22

April

28

Dec

20

each reduce process gets its own month with values and applies the reduction (here: max value) to it

Page 15: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Map/Reduce

A strategy for implementing parallel algorithms.

map: A master worker takes the problem input, divides it into smaller sub-problems, and distributes the sub-problems to workers (threads).

reduce: The master worker collects sub-solutions from the workers and combines them in some way to produce the overall answer.

15

Page 16: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Map/Reduce

Frameworks and tools have been written to perform map/reduce.

MapReduce framework by Google

Hadoop framework by Yahoo!

related to the ideas of Big Data and Cloud Computing

also related to functional programming(and actually not that new) and available with the Streams concept in Java (>=8)

Map and reduce are user-supplied plug-ins, the rest is provided by the frameworks.

16Jeffrey Dean and Sanjay Ghemawat. 2008. MapReduce: simplified data processing on large clusters. Commun. ACM 51, 1 (January 2008), 107-113. DOI=10.1145/1327452.1327492 http://doi.acm.org/10.1145/1327452.1327492

Page 17: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

MapReduce on Clusters

You may have heard of Google’s “map/reduce” or Amazon's Hadoop

Idea: Perform maps/reduces on data using many machines

The system takes care of distributing the data and managing fault tolerance

You just write code to map one element (key-value part) and reduce elements (key-value pairs) to a combined result

Separates how to do recursive divide-and-conquer from what computation to perform

Old idea in higher-order functional programming transferred to large-scale distributed computing

17

Page 18: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Example

Count word occurences in a very large file

File =

18

how are you todaydo you like the weather outsideI doI doI wish you the very bestfor your exams.

GBytes

Page 19: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Mappers

19

huge file

part 1

part 2

part 3

key/valuepairs(e.g. position / string)

<0, "how are you today"><15, "do you like ...">

<35, "I do"><39, "I do">

<43, "I wish you the very best"><70, "for your exams">

mapper 1

mapper 2

mapper 3

DISTR

IBU

TED

Page 20: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Mappers

20

key/valuepairs(e.g. position / string)

<0, "how are you today"><15, "do you like ...">

<35, "I do"><39, "I do">

<43, "I wish you the very best"><70, "for your exams">

mapper 1

mapper 2

mapper 3

key/valuepairs(word, count)

<"how",1><"are",1><"you",1>...

<"I",1><"do",1><"I",1><"do",1>

<"I",1><"wish",1><"you",1>...

input output

Page 21: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Shuffle / Sort

21

mapper 1

mapper 2

mapper 3

unique key/valuepairs (shuffled)(word, counts)

<"how",1><"are",1><"you",1>...

<"I",1><"do",1><"I",1><"do",1>

<"I",1><"wish",1><"you",1>...

<"do",1,1,1><"for",1>....

reducer 1

reducer 2

<"are",1><"best",1>...

key/valuepairs(word, count)

DISTR

IBU

TED

Page 22: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Reduce

22

unique key/valuepairs (shuffled)(word, counts)

<"do",1,1,1><"for",1>....

reducer 1

reducer 2

<"are",1><"best",1>...

target file(s)

are 1best 1you 3...

do 3for 1I 3...

input output

Page 23: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

SORTING NETWORKS

23

Page 24: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Lower bound on sorting

24

Simplealgorithms:

O(n2)Fancier

algorithms:O(n log n)

Comparisonlower bound:(n log n)

Insertion sortSelection sortBubble Sort

Shell sort…

Heap sortMerge sort

Quick sort (avg)…

Horrible algorithms:

Ω(n2)

Bogo SortStooge Sort

Page 25: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Comparator

25

x

y

min(x,y)

max(x,y)<

x

y

min(x,y)

max(x,y)

shorter notation:

Page 26: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

void compare(int&a, int&b, boolean dir) {

if (dir==(a,b)){

std::swap(a,b);

}

}

26

a

b

a

b

><

Page 27: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Sorting Networks

27

1

5

4

3

5

1

4

3

1

3

4

5

3

4

3

1

4

5

Page 28: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Sorting Networks are Oblivious (and Redundant)

28

2:3 4:3 2:1 4:1 2:4 3:4 2:1 3:1 1:3 4:3 1:2 4:2 1:4 3:4 1:2 3:2

2:4 2:4 2:3 2:3 1:4 1:4 1:3 1:3

1:3 1:4 2:3 2:4

3:4 3:4

1:2

𝑥1 𝑥2 𝑥3 𝑥4Oblivious comparison tree

redundant cases

Page 29: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Recursive Construction : Insertion

29

𝑥1𝑥2

𝑥3

𝑥𝑛−1𝑥𝑛

𝑥𝑛+1

sorting network

.

.

.

.

.

.

.

.

.

Page 30: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Recursive Construction: Selection

30

𝑥1𝑥2

𝑥3

𝑥𝑛−1𝑥𝑛

𝑥𝑛+1

sorting network

.

.

.

.

.

.

.

.

.

Page 31: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Applied recursively..

31

insertion sort bubble sort

with parallelism: insertion sort = bubble sort !

Page 32: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Question

How many steps does a computer with infinite number of processors (comparators) require in order to sort using parallel bubble sort?

Answer: 2n – 3 Can this be improved ?

How many comparisons ?

Answer: (n-1) n/2

How many comparators are required (at a time)?

Answer: n/2Reusable comparators: n-1

32

Page 33: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Improving parallel Bubble SortOdd-Even Transposition Sort:

0 9 8 2 7 3 1 5 6 4

1 8 9 2 7 1 3 5 6 4

2 8 2 9 1 7 3 5 4 6

3 2 8 1 9 3 7 4 5 6

4 2 1 8 3 9 4 7 5 6

5 1 2 3 8 4 9 5 7 6

6 1 2 3 4 8 5 9 6 7

7 1 2 3 4 5 8 6 9 7

8 1 2 3 4 5 6 8 7 9

1 2 3 4 5 6 7 8 9

33

Page 34: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

void oddEvenTranspositionSort(std::vector<T>& v, boolean dir) {

for (int i = 0; i<v.size(); ++i){

for (int j = i % 2; j+1<n; j+=2)

compare(v[i],v[j],dir);

}

}

34

Page 35: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Improvement?

Same number of comparators (at a time)

Same number of comparisons

But less parallel steps (depth): n

35

In a massively parallel setup, bubble sort is thus not too bad.

But it can go better...

Page 36: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Parallel sorting

36

Prove that the two networks above sort four numbers. Easy?

depth = 4 depth = 3

Page 37: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Zero-one-principle

Theorem: If a network with 𝑛 input lines sorts all 2𝑛 sequences of 0s and 1s into non-decreasing order, it will sort any arbitrary sequence of 𝑛 numbers in nondecreasing order.

37

Page 38: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Proof

Assume a monotonic function 𝑓(𝑥) with 𝑓 𝑥 ≤ 𝑓(𝑦) whenever 𝑥 ≤ 𝑦and a network 𝑁 that sorts. Let N transform (𝑥1, 𝑥2, … , 𝑥𝑛) into (𝑦1, 𝑦2, … , 𝑦𝑛), then it also transforms (𝑓(𝑥1), 𝑓(𝑥2), … , 𝑓(𝑥𝑛)) into (𝑓(𝑦1), 𝑓(𝑦2), … , 𝑓(𝑦𝑛)).

Assume 𝑦𝑖 > 𝑦𝑖+1for some 𝑖, then consider the monotonic function

𝑓(𝑥) = ቊ0, 𝑖𝑓 𝑥 < 𝑦𝑖1, 𝑖𝑓 𝑥 ≥ 𝑦𝑖

N converts

(𝑓(𝑥1), 𝑓(𝑥2), … , 𝑓(𝑥𝑛)) into 𝑓 𝑦1 , 𝑓(𝑦2 , … 𝑓 𝑦𝑖 , 𝑓 𝑦𝑖+1 , … , 𝑓(𝑦𝑛))

38

𝑥 not sorted by 𝑁 ⇒ there is an 𝑓 𝑥 ∈ 0,1 𝑛 not sorted by N⇔

𝑓 sorted by N for all 𝑓 ∈ 0,1 𝑛 ⇒ 𝑥 sorted by N for all x

Argue: If x is sorted by a network N then also any monotonic function of x.

2081 30 5 9 851 9 20 30

104-1 99 2 9 42-1 9 10 99

Show: If x is not sorted by the network, then there is a monotonic function f that maps x to 0s and 1s and f(x) is not sorted by the network

2081 30 5 9 951 8 20 30

100 1 0 1 100 0 1 1

Page 39: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Proof

Assume a monotonic function 𝑓(𝑥) with 𝑓 𝑥 ≤ 𝑓(𝑦) whenever 𝑥 ≤ 𝑦and a network 𝑁 that sorts. Let N transform (𝑥1, 𝑥2, … , 𝑥𝑛) into (𝑦1, 𝑦2, … , 𝑦𝑛), then it also transforms (𝑓(𝑥1), 𝑓(𝑥2), … , 𝑓(𝑥𝑛)) into (𝑓(𝑦1), 𝑓(𝑦2), … , 𝑓(𝑦𝑛)).

Assume 𝑦𝑖 > 𝑦𝑖+1for some 𝑖, then consider the monotonic function

𝑓(𝑥) = ቊ0, 𝑖𝑓 𝑥 < 𝑦𝑖1, 𝑖𝑓 𝑥 ≥ 𝑦𝑖

N converts

(𝑓(𝑥1), 𝑓(𝑥2), … , 𝑓(𝑥𝑛)) into 𝑓 𝑦1 , 𝑓(𝑦2 , … 𝑓 𝑦𝑖 , 𝑓 𝑦𝑖+1 , … , 𝑓(𝑦𝑛))

39

1 0

All comparators must act in the same way for the 𝑓(𝑥𝑖) as they do for the 𝑥𝑖

Page 40: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Bitonic Sort

Bitonic (Merge) Sort is a parallel algorithm for sorting

If enough processors are available, bitonic sort breaks the lower bound on sorting for comparison sort algorithm

Asymptotic Runtime of 𝑂 𝑛 log2 𝑛 (sequential execution)

Very good asymptotic runtime in the parallel case (as we'll see below).

Worst = Average = Best case

40

Page 41: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Bitonic

Sequence (𝑥1, 𝑥2, … , 𝑥𝑛) is bitonic, if it can be circularly shifted such that it is first monotonically increasing and then monontonically decreasing.

(1, 2, 3, 4, 5, 3, 1, 0) (4, 3, 2, 1, 2, 4, 6, 5)

41

Page 42: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Bitonic 0-1 Sequences

0𝑖1𝑗0𝑘

1𝑖0𝑗1𝑘

42

Page 43: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

The Half-Cleaner

43

bitonic

0

0

1

1

1

1

1

0

0

0

1

0

1

1

1

1

bitonic

bitonic clean

void halfclean(std::vector<T>& a, int lo, int n, boolean dir){

for (int i=lo; i<lo+n/2; i++)

compare(a[i],a[i+n/2], dir);

}

Page 44: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

The Half-Cleaner

44

bitonic

0

0

1

1

1

0

0

0

0

0

0

0

1

0

1

1

bitonic clean

bitonic

void halfclean(std::vector<T>& a, int lo, int n, boolean dir){

for (int i=lo; i<lo+n/2; i++)

compare(a[i],a[i+n/2], dir);

}

Page 45: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Bitonic Split Example

45

+

bitonic bitonic bitonic

Page 46: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Lemma

Input: a bitonic sequence of 0s and 1s, then for the output of the half-cleaner it holds that

Upper and lower half is bitonic

[One of the two halfs is bitonic clean (only 0s or 1s)]

Every number in upper half ≤ every number in the lower half

46

Page 47: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Proof: All cases

47

0

1

0

bitonic 0

1

1

00

1

0

1

0

1

0

1

bitonic

bitonic cleantop

bottom

top

bottom

Page 48: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

48

0

1

0

bitonic0

1

1

0

0

0

11

bitonic clean

bitonictop

bottom

top

bottom

0

0

1

1

Page 49: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

49

0

1

0

bitonic0 1

0

0

bitonic

bitonic cleantop

bottom

top

bottom

0 1

0

00

1

0

0

Page 50: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

50

0

1

0

bitonic01

0

0

bitonic

bitonic cleantop

bottom

top

bottom

0 1

0

00

1

0

0

Page 51: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

The four remaining cases (010 101)

51

1

0

1

bitonic1

0

0

11

0

1

0

bitonic clean

bitonic

top

bottom

top

bottom

0

1

0

1

1

0

1

bitonic1

0

0

1

1

1

00

bitonic

bitonic cleantop

bottom

top

bottom

0

1

1

0

1

0

1

bitonic 1 0

1

1

bitonic clean

bitonictop

bottom

top

bottom

10

1

1

1

0

1

1

1

0

1

bitonic 10

1

1

bitonic clean

bitonictop

bottom

top

bottom

10

1

1

1

0

1

1

Page 52: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Construction BitonicToSorted

52

0

0

1

1

1

0

0

0

0

0

0

0

1

0

1

1

halfclean

halfclean

halfclean

0

0

0

0

1

0

1

1

half clean

half clean

half clean

half clean

0

0

0

0

0

1

1

1

bitonic sorted

Page 53: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Recursive Construction

53

halfclean(n)

bitonicToSorted

(n/2)

bitonicToSorted

(n/2)

bitonicToSorted(n) ≝

Page 54: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

BitonicToSorted sorts a Bitonic Sequence

void bitonicToSorted (std::vector<int>& a, int lo, int n, boolean dir){

if (n>1){

halfClean(a, lo, n, dir);

bitonicToSorted(a, lo, n/2, dir);

bitonicToSorted(a, lo+m, n-n/2, dir);

}

}

54

halfclean(n)

bitonicToSorted

(n/2)

bitonicToSorted

(n/2)

bitonic

0

0

1

1

1

0

0

0

0

0

0

0

0

1

1

1

sorted

Page 55: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Bi-Merger

55

0

0

1

1

0

0

0

1

0

0

0

0

1

1

0

1

bitonic

bitonic

sorted

sorted

0

0

1

1

1

0

0

0

0

0

0

0

1

0

1

1

bitonic

bitonic

sorted

reversesorted

≜ bitonic

Bi-Merger on two sorted sequences acts like a half-cleaner on a bitonic sequence (when one of the sequences is reversed)

biMerge half-cleaner

Page 56: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Bi-Merger

56

0

0

1

1

0

0

0

1

0

0

0

0

1

1

0

1

bitonic

bitonic

sorted

sorted

Bi-Merger on two sorted sequences acts like a half-cleaner on a bitonic sequence (when one of the sequences is reversed)

bimerge

void bimerge(std::vector<T>& a, int lo, int n, boolean dir){

for (int i=0; i<n/2; i++)compare(a[lo+i],a[lo+n-i-1], dir);

}

Page 57: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Merger

Merger

57

0

0

1

1

0

0

0

1

0

0

0

0

1

0

1

1

bimerge(n)

halfclean(n/2)

halfclean(n/2)

0

0

0

0

1

0

1

1

half clean

half clean

half clean

half clean

0

0

0

0

0

1

1

1

sorted

sorted

sorted

Page 58: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Merger

Merger

58

0

0

1

1

0

0

0

1

0

0

0

0

1

0

1

1

bimerge(n)

0

0

0

0

1

0

1

1

0

0

0

0

0

1

1

1

sorted

sorted

sorted

bitonicToSorted

(n/2)

bitonicToSorted

(n/2)

bit

on

icb

ito

nic

Page 59: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

BitonicMerge sorts a Halfsorted Sequence

void bitonicMerge (std::vector<int>& a, int lo, int n, boolean dir){

if (n>1){

int m=n/2;

bimerge(a,lo,n,dir);

bitonicToSorted(a, lo, m, dir);

bitonicToSorted(a, lo+m, n-m, dir);

}

}

59

bitonicToSorted

(n/2)

bitonicToSorted

(n/2)

sorted

0

0

1

1

0

0

0

1

0

0

0

0

0

1

1

1

sortedbimerge

sorted

Page 60: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Recursive Construction of a Sorter

60

bitonicSort(n/2)

bitonicMerge

(n)bitonicSort(n) ≝

bitonicSort (n/2)

Page 61: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

private void bitonicSort(std::vector&<T> a, int lo, int n, boolean dir){

if (n>1){

int m=n/2;

bitonicSort(a, lo, m, ASCENDING);

bitonicSort(a, lo+m, n-m, DESCENDING);

bitonicMerge(a, lo, n, dir);

}

}

61

bitonicSort(n/2)

bitonicMerge

(n)

bitonicSort(n/2)

Page 62: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

bitonicMerge (8)

Example

62

bitonicMerge(4)bitonicMerge(2)

biMerge

halfclean

halfclean

half clean

half clean

half clean

half clean

biMerge

half clean

half clean

biMerge

half clean

half clean

half clean

half clean

half clean

half clean

Page 63: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Merger (8)

Example

63

Merger(4)Merger (2)

bi-merger

half cleaner

half cleaner

half cleane

r

half cleane

r

half cleane

r

half cleane

r

bi-merger

half cleane

r

half cleane

r

bi-merger

half cleane

r

half cleane

r

half cleane

r

half cleane

r

half cleane

r

half cleane

r

Page 64: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Bitonic Merge Sort

How many steps?

𝑖=1

log 𝑛

log 2𝑖 =

𝑖=1

log 𝑛

𝑖 log 2 =log 𝑛 ⋅ (log 𝑛 + 1)

2= 𝑶(𝐥𝐨𝐠𝟐 𝒏)

64

#mergers

#steps / merger

Page 65: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Zur Prüfung

findet statt am 6.8.2018 von 9:00 – 11:00 (2h)

Inhalt: Datenstrukturen und Algorithmen, C++ Advanced, Parallel Programming,

Hilfsmittel: 4 A4 Seiten, handgeschrieben oder min. 11Pt Fontsize. Kopieren ist erlaubt aber nicht clever. Handgeschriebenes vom Tablet drucken ist auch erlaubt, wenn dabei die Schrift nicht wesentlich verkleinert wird (in Relation zur sonst üblichen Schreibschrift).

65

Page 66: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Vorschlag: Q&A vor der Prüfung.

Schulferien ab 16.Juli -- Termin sollte vorher sein.

Für Sie: je später desto besser. Vorschlag: +/- 12. Juli.

66

Page 67: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Vorbereitung

Übungen machen / gemacht haben.

[morgen, Freitag 1. Juni, finden Übungen statt. Besprechung Übung 13.]

Können Sie die Vorlesungsinhalte einem Kollegen (ohne Folien) erklären?

Alte Prüfungen stehen auf der Webseite zur Verfügung.

Die alten Prüfungen von Prof. Widmayer enthalten Material, das nicht behandelt wurde (geometrische Algorithmen, branch-and-bound)

Die "neuen" Prüfungen bei mir enthalten Material, das in den Vorlesungen von Widmayer/Püschel nicht behandelt wurden (insbesondere C++ / Parallel Programming)

67

Page 68: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

Ausschlüsse

NICHT Prüfungsstoff sind

Details der längeren Beweise (Laufzeit Algorithmus Blum, Analyse Ranomisierter Quicksort, Amortisierte Analyse von Move-To-Front, Beweis Theorem Universelles Hashing, Beweis Fibonacci Zahlen mit Erzeugendenfunktion, Beweis Amortisierte Kosten Fibonacci Heap,

Atomare Register / RMW Operationen / Lock Free Programming

Hardware Architekturen, Pipelining, Peterson Algorithmus

68

Page 69: Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 · 2018. 8. 24. · Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung 2018 Felix Friedrich, 30.5.2018 Map/Reduce

69

Ich bin weiterhin erreichbar unter

[email protected]