- a cache-oblivious priority queuegerth/slides/isaac02.pdf · cache-oblivious priority queues o 1 b...

24
Funnel Heap - A Cache-Oblivious Priority Queue Gerth Stølting Brodal Rolf Fagerberg BRICS University of Aarhus Thirteenth International Symposium on Algorithms and Computation Vancouver, BC, Canada, November 22, 2002 1

Upload: others

Post on 25-Aug-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Funnel Heap- A Cache-Oblivious Priority Queue

Gerth Stølting Brodal Rolf Fagerberg

BRICS

University of Aarhus

Thirteenth International Symposium on Algorithms and Computation

Vancouver, BC, Canada, November 22, 2002

1

Page 2: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Outline of talk

• Cache-oblivious model

• Cache-oblivious results

• Cache-oblivious priority queues

• Funnel heap

– k - merger

– the data structure

– operations

Brodal, Fagerberg: Funnel Heap 2

Page 3: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

The Classic RAM Model

The RAM model: CPU AR

M

Add: O(1)

Mult: O(1)

Mem access: O(1)

Real life: DiskCPU

L2L1

AR

M

Cac

C

ca

eh h

e

Bottleneck: transfer between two highest memory levels in use

Brodal, Fagerberg: Funnel Heap 3

Page 4: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

The Classic RAM Model

The RAM model: CPU AR

M

Add: O(1)

Mult: O(1)

Mem access: O(1)

Real life: DiskCPU

L2L1

AR

M

Cac

C

ca

eh h

e

Bottleneck: transfer between two highest memory levels in use

Brodal, Fagerberg: Funnel Heap 3

Page 5: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

The I/O Model

CPU External

I/O

MemoryyromeM

N = problem sizeM = memory sizeB = I/O block size

Aggarwal and Vitter 1988

• One I/O moves B consecutive records from/to disk

• Cost: number of I/Os

Scan(N) = O(N/B) Sort(N) = O(

N

BlogM/B

N

B

)

Brodal, Fagerberg: Funnel Heap 4

Page 6: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Cache-Oblivious ModelFrigo, Leiserson, Prokop, Ramachandran 1999

• Program in the RAM model

• Analyze in the I/O model (for arbitrary B and M )

Advantages

• Optimal on arbitrary level ⇒ optimal on all levels

• B and M not hard-wired into algorithm

Brodal, Fagerberg: Funnel Heap 5

Page 7: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Cache-Oblivious Results

• Scanning ⇒ stack, queue, selection, . . .

• Sorting, matrix multiplication, FFTFrigo, Leiserson, Prokop, Ramachandran, FOCS’99

• Cache oblivious search trees Prokop 99Bender, Demaine, Farach-Colton, FOCS’00

Rahman, Cole, Raman, WAE’01Bender, Duan, Iacono, Wu and Brodal, Fagerberg, Jacob, SODA’02

• Priority queue and graph algorithmsArge, Bender, Demaine, Holland-Minkley, Munro, STOC’02

• Computational geometry Bender, Cole, Raman, ICALP’02Brodal, Fagerberg, ICALP’02

• Scanning dynamic sets Bender, Cole, Demaine, Farach-Colton, ESA’02

Brodal, Fagerberg: Funnel Heap 6

Page 8: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Priority Queues

Insert(e) DeleteMin

Classic RAM:

• Heap: O(log2 n) time

, O(

log2

N

M

)

I/Os

Williams 1964

I/O model:

• Buffer tree: O(

1

BlogM/B

N

B

)

= O

(

Sort(N)

N

)

I/Os Arge 1995

Brodal, Fagerberg: Funnel Heap 7

Page 9: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Priority Queues

Insert(e) DeleteMin

Classic RAM:

• Heap: O(log2 n) time, O(

log2

N

M

)

I/Os Williams 1964

I/O model:

• Buffer tree: O(

1

BlogM/B

N

B

)

= O

(

Sort(N)

N

)

I/Os Arge 1995

Brodal, Fagerberg: Funnel Heap 7

Page 10: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Priority Queues

Insert(e) DeleteMin

Classic RAM:

• Heap: O(log2 n) time, O(

log2

N

M

)

I/Os Williams 1964

I/O model:

• Buffer tree: O(

1

BlogM/B

N

B

)

= O

(

Sort(N)

N

)

I/Os Arge 1995

Brodal, Fagerberg: Funnel Heap 7

Page 11: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Cache-Oblivious Priority Queues

• O(

1

BlogM/B

N

B

)

I/Os Arge, Bender, Demaine,Holland-Minkley, Munro 2002

– Uses sorting and selection as subroutines

– Requires tall cache assumption, M ≥ B2

• Funnel heap This talk

– Uses only binary merging

– Profile adaptive, i.e. O(

1

BlogM/B

Ni

B

)

I/Os

Ni is either the size profile, max depth profile, or#insertions during the lifetime of the ith inserted element

Brodal, Fagerberg: Funnel Heap 8

Page 12: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Cache-Oblivious Priority Queues

• O(

1

BlogM/B

N

B

)

I/Os Arge, Bender, Demaine,Holland-Minkley, Munro 2002

– Uses sorting and selection as subroutines

– Requires tall cache assumption, M ≥ B2

• Funnel heap This talk

– Uses only binary merging

– Profile adaptive, i.e. O(

1

BlogM/B

Ni

B

)

I/Os

Ni is either the size profile, max depth profile, or#insertions during the lifetime of the ith inserted element

Brodal, Fagerberg: Funnel Heap 8

Page 13: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Merge Trees

-3s

Binary merger

-�

N

-�

^

-3s

-1q

-1q

-3s

-1q

-1q

-�

^

-3s

-1q

-1q

-3s

-1q

-1q

Merge tree

Brodal, Fagerberg: Funnel Heap 9

Page 14: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Merge Trees

-3s

Binary merger

-�

N

-�

^

-3s

-1q

-1q

-3s

-1q

-1q

-�

^

-3s

-1q

-1q

-3s

-1q

-1q

Merge tree

Brodal, Fagerberg: Funnel Heap 9

Page 15: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Merging Algorithm

Fill(v)

while out-buffer not fullif left in-buffer empty

Fill(left child)if right in-buffer empty

Fill(right child)perform one merge step

-�

N

-�

^

-3s

-1q

-1q

-3s

-1q

-1q

-�

^

-3s

-1q

-1q

-3s

-1q

-1q

Brodal, Fagerberg: Funnel Heap 10

Page 16: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

k - merger

k3

k

B1

B0

· · ·

· · ·

M1 M√

k

M0

B√

k

· · ·M0 M1B1 B√

kM√

kB2 M2B0

Recursive memory layoutRecursive definition of buffer sizes

Lemma: O(k3

BlogM(k3) + k) I/Os per invocation (if M ≥ B2)

Frigo, Leiserson, Prokop, Ramachandran 1999Brodal, Fagerberg 2002

Brodal, Fagerberg: Funnel Heap 11

Page 17: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

The Priority Queue

�6

6

66

�6

6

66 6· ·

� � · · · �6

6

66 6· · ·

� � · · · �6

6

66 6· · · ·

�A1

B1

s1 s1

v1 Ai vi

Bi

ki

si si si

Link i

I

ki+1 ≈ k4/3i

si+1 ≈ s4/3i

ki ≈ s1/3i In total: A single binary merge tree

Brodal, Fagerberg: Funnel Heap 12

Page 18: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

The Priority Queue

�6

6

66

�6

6

66 6· ·

� � · · · �6

6

66 6· · ·

� � · · · �6

6

66 6· · · ·

�A1

B1

s1 s1

v1 Ai vi

Bi

ki

si si si

Link i

I

ki+1 ≈ k4/3i

si+1 ≈ s4/3i

ki ≈ s1/3i

In total: A single binary merge tree

Brodal, Fagerberg: Funnel Heap 12

Page 19: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

The Priority Queue

�6

6

66

�6

6

66 6· ·

� � · · · �6

6

66 6· · ·

� � · · · �6

6

66 6· · · ·

�A1

B1

s1 s1

v1 Ai vi

Bi

ki

si si si

Link i

I

ki+1 ≈ k4/3i

si+1 ≈ s4/3i

ki ≈ s1/3i In total: A single binary merge tree

Brodal, Fagerberg: Funnel Heap 12

Page 20: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Operations — DeleteMin

· ·

· · ·

· · ·

· · ·

· · · ·

A1 v1

I

• If A1 is empty, call Fill(v1)

• Search I and A1 for minimum element

Brodal, Fagerberg: Funnel Heap 13

Page 21: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Operations — Insert

· ·

· · ·

· · ·

· · ·

· · · ·

ki

I

c1

c2

ci

• Insert in I

• If I overflows, call Sweep(i) for first i where ci ≤ ki

Sweep ≈ addition of one to number c1c2..ci..cmax

si = s1 +∑i−1

j=1 kjsj

Brodal, Fagerberg: Funnel Heap 14

Page 22: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Analysis

· ·

· · ·

· · ·

· · ·

· · · ·

We can prove:

• Number N of insertions performed: simax≤ N

• Number of I/Os per Insert for link i: O(

1B

logM/B si

)

• By the doubly-exponentially growth of si,the total number of I/Os per Insert is

O

(

∞∑

k=0

1

BlogM/B N (3/4)k

)

= O(

1

BlogM/B N

)

• DeleteMin is amortized for free

Brodal, Fagerberg: Funnel Heap 15

Page 23: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Profile Adaptive

· ·

· · ·

· · ·

· · ·

· · · ·

• Modify Insert to apply Sweep(i) for

– first i where ci ≤ ki, or

– link i is at most half-full, i.e. there exists Sij1 and Sij2

that can be merged

• O(

1

BlogM/B

Ni

B

)

I/Os

Brodal, Fagerberg: Funnel Heap 16

Page 24: - A Cache-Oblivious Priority Queuegerth/slides/isaac02.pdf · Cache-Oblivious Priority Queues O 1 B logM=B N B I/Os Arge, Bender, Demaine, Holland-Minkley, Munro 2002 – Uses sorting

Conclusions

· ·

· · ·

· · ·

· · ·

· · · ·

• Funnel heap - a cache-oblivious priority queue

• Profile adaptive

• Requires tall cache assumption

(necessary requirement Brodal, Fagerberg 2002)

Open problem

• Worst-case bounds

Brodal, Fagerberg: Funnel Heap 17