introduction to data-oriented design

83
Introduction to Data-Oriented Design @YaroslavBunyak Senior Software Engineer, SoftServe

Upload: it-weekend

Post on 10-May-2015

2.066 views

Category:

Technology


3 download

DESCRIPTION

by Yaroslav Bunyak

TRANSCRIPT

Page 1: Introduction to Data-Oriented Design

Introduction toData-Oriented Design

@YaroslavBunyakSenior Software Engineer, SoftServe

Page 2: Introduction to Data-Oriented Design

Programming, M**********rDo you speak it?

Page 3: Introduction to Data-Oriented Design

Story

Page 4: Introduction to Data-Oriented Design

Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Page 5: Introduction to Data-Oriented Design

Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Page 6: Introduction to Data-Oriented Design

Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Page 7: Introduction to Data-Oriented Design

Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Page 8: Introduction to Data-Oriented Design

Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Page 9: Introduction to Data-Oriented Design

Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Page 10: Introduction to Data-Oriented Design

Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Page 11: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Page 12: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Simple algorithm

Page 13: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Simple algorithm

Easy to implement

Page 14: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Page 15: Introduction to Data-Oriented Design

Sieve of Eratosthenesint array[SIZE];

Page 16: Introduction to Data-Oriented Design

Sieve of Eratosthenesint array[SIZE];

array[i] = 1;

Page 17: Introduction to Data-Oriented Design

Sieve of Eratosthenesint array[SIZE];

array[i] = 1;

if (array[i]) ...

Page 18: Introduction to Data-Oriented Design

Sieve of Eratosthenesint array[SIZE];

array[i] = 1;

if (array[i]) ...

Page 19: Introduction to Data-Oriented Design

Sieve of Eratosthenesint array[SIZE];

array[i] = 1;

if (array[i]) ...

int bits[SIZE / 32];

Page 20: Introduction to Data-Oriented Design

Sieve of Eratosthenesint array[SIZE];

array[i] = 1;

if (array[i]) ...

int bits[SIZE / 32];

bits[i / 32] |= 1 << (i % 32);

Page 21: Introduction to Data-Oriented Design

Sieve of Eratosthenesint array[SIZE];

array[i] = 1;

if (array[i]) ...

int bits[SIZE / 32];

bits[i / 32] |= 1 << (i % 32);

if (bits[i / 32] & (1 << (i % 32))) ...

Page 22: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Page 23: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Simple algorithm

Page 24: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Simple algorithm

Easy to implement

Page 25: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Simple algorithm

Easy to implement

But...

Page 26: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Simple algorithm

Easy to implement

But...

unexpected results

Page 27: Introduction to Data-Oriented Design

Sieve of Eratosthenes

Page 28: Introduction to Data-Oriented Design

Sieve of Eratosthenes

The second implementation (bitset) is 3-5x faster than first (array)

Page 29: Introduction to Data-Oriented Design

Sieve of Eratosthenes

The second implementation (bitset) is 3-5x faster than first (array)

Even though it actually does more work

Page 30: Introduction to Data-Oriented Design

Why?!.

Page 31: Introduction to Data-Oriented Design

Fast Forward

Page 32: Introduction to Data-Oriented Design

...

Page 33: Introduction to Data-Oriented Design

...

• Years have passed

Page 34: Introduction to Data-Oriented Design

...

• Years have passed

• I become a software engineer

Page 35: Introduction to Data-Oriented Design

...

• Years have passed

• I become a software engineer

• And one day...

Page 36: Introduction to Data-Oriented Design

This Graph

Slide 17

CPU/Memory performance

Computer architecture: a quantitative approachBy John L. Hennessy, David A. Patterson, Andrea C. Arpaci-Dusseau

Page 37: Introduction to Data-Oriented Design

This Table1980 Modern PC Improvement, %

Clock speed, Mhz 6 3000 +500x

Memory size, MB 2 2000 +1000x

Memory bandwidth, MB/s 137000 (read)2000 (write)

+540x+150x

Memory latency, ns 225 ~70 +3x

Memory latency, cycles 1.4 210 -150x

Page 38: Introduction to Data-Oriented Design

Our Programming Model

Page 39: Introduction to Data-Oriented Design

Our Programming Model

• High-level languages

Page 40: Introduction to Data-Oriented Design

Our Programming Model

• High-level languages

• OOP

Page 41: Introduction to Data-Oriented Design

Our Programming Model

• High-level languages

• OOP

• everywhere!

Page 42: Introduction to Data-Oriented Design

Our Programming Model

• High-level languages

• OOP

• everywhere!

• objects scattered throughout the address space

Page 43: Introduction to Data-Oriented Design

Our Programming Model

• High-level languages

• OOP

• everywhere!

• objects scattered throughout the address space

• access patterns are unpredictable

Page 44: Introduction to Data-Oriented Design

MeetData-Oriented Design

Page 45: Introduction to Data-Oriented Design

Ideas

Page 46: Introduction to Data-Oriented Design

Ideas

• Programs transform data

Page 47: Introduction to Data-Oriented Design

Ideas

• Programs transform data

• nothing more

Page 48: Introduction to Data-Oriented Design

Ideas

• Programs transform data

• nothing more

• Think about data, not code

Page 49: Introduction to Data-Oriented Design

Ideas

• Programs transform data

• nothing more

• Think about data, not code

• Hardware is not a black box

Page 50: Introduction to Data-Oriented Design

Program

data dataxform

Page 51: Introduction to Data-Oriented Design

Program

data dataxform

Your Program

Page 52: Introduction to Data-Oriented Design

Claim

• Memory latency is the king

• CPU cycles almost free

Page 53: Introduction to Data-Oriented Design

Memory

Page 54: Introduction to Data-Oriented Design

• CPU registers

MemoryCPU

Page 55: Introduction to Data-Oriented Design

• CPU registers

• Cache Level 1

MemoryCPU

L1iCache

L1dCache

Page 56: Introduction to Data-Oriented Design

• CPU registers

• Cache Level 1

• Cache Level 2

MemoryCPU

L1iCache

L1dCache

L2Cache

Page 57: Introduction to Data-Oriented Design

• CPU registers

• Cache Level 1

• Cache Level 2

• RAM

MemoryCPU

RAM

L1iCache

L1dCache

L2Cache

Page 58: Introduction to Data-Oriented Design

• CPU registers

• Cache Level 1

• Cache Level 2

• RAM

• HDD

MemoryCPU

RAM

Disk

L1iCache

L1dCache

L2Cache

Page 59: Introduction to Data-Oriented Design

Distance Metaphor

Page 60: Introduction to Data-Oriented Design

Distance Metaphor

• L1 cache: it's on your desk, pick it up.

Page 61: Introduction to Data-Oriented Design

Distance Metaphor

• L1 cache: it's on your desk, pick it up.

• L2 cache: it's on the bookshelf in your office, get up out of the chair.

Page 62: Introduction to Data-Oriented Design

Distance Metaphor

• L1 cache: it's on your desk, pick it up.

• L2 cache: it's on the bookshelf in your office, get up out of the chair.

• Main memory: it's on the shelf in your garage downstairs, might as well get a snack while you're down there.

Page 63: Introduction to Data-Oriented Design

Distance Metaphor

• L1 cache: it's on your desk, pick it up.

• L2 cache: it's on the bookshelf in your office, get up out of the chair.

• Main memory: it's on the shelf in your garage downstairs, might as well get a snack while you're down there.

• Disk: it's in, um, California. Walk there. Walk back. Really.

Page 64: Introduction to Data-Oriented Design

Distance Metaphor

• L1 cache: it's on your desk, pick it up.

• L2 cache: it's on the bookshelf in your office, get up out of the chair.

• Main memory: it's on the shelf in your garage downstairs, might as well get a snack while you're down there.

• Disk: it's in, um, California. Walk there. Walk back. Really.

http://hacksoflife.blogspot.com/2011/04/going-to-california-with-aching-in-my.html

Page 65: Introduction to Data-Oriented Design

Advice

Page 66: Introduction to Data-Oriented Design

Advice

• Keep your data closer to registers and cache

Page 67: Introduction to Data-Oriented Design

Advice

• Keep your data closer to registers and cache

• What’s good for memory - good for you

Page 68: Introduction to Data-Oriented Design

Example 1: AoS vs SoAstruct Tile

{

bool ready;

Data pixels; // big chunk of data

};

Tile tiles[SIZE];

vs

struct Image

{

bool ready[SIZE]; // hot data

Data pixels[SIZE]; // cold data

};

Page 69: Introduction to Data-Oriented Design

Example 1: AoS vs SoAfor (int i = 0; i < SIZE; ++i)

{

if (tiles[i].ready)

draw(tiles[i].pixels);

}

vs

for (int i = 0; i < SIZE; ++i)

{

if (image.ready[i])

draw(image.pixels[i]);

}

Page 70: Introduction to Data-Oriented Design

Example 1: AoS vs SoA

vs

Page 71: Introduction to Data-Oriented Design

Example 2: Existencestruct Image

{

bool ready[SIZE];

Data pixels[SIZE];

};

Image image;

vs

Data ready_pixels[N];

Data no_pixels[M];

// N + M = SIZE

Page 72: Introduction to Data-Oriented Design

Example 2: Existencefor (int i = 0; i < SIZE; ++i)

{

if (image.ready[i])

draw(image.pixels[i]);

}

vs

for (int i = 0; i < N; ++i)

{

draw(ready_pixels[i];

}

Page 73: Introduction to Data-Oriented Design

Example 3: Locality

std::vector<float> numbers;

float sum = 0.0f;

for (auto it : numbers)

sum += *it;

vs

std::list<float> numbers;

float sum = 0.0f;

for (auto it : numbers)

sum+ = *it;

Page 74: Introduction to Data-Oriented Design

Example 3: Locality

vs

Page 75: Introduction to Data-Oriented Design

Few Patterns

• A to B transform

• In place transform

• Existence based processing

• Data normalization

• DB design says hello!

• Task, gather, dispatch, and more...

Page 76: Introduction to Data-Oriented Design

Benefits of DOD

Page 77: Introduction to Data-Oriented Design

Benefits of DOD

• Maximum performance

• CPU doesn’t wait & starve

Page 78: Introduction to Data-Oriented Design

Benefits of DOD

• Maximum performance

• CPU doesn’t wait & starve

• Easy to parallelize

• data is grouped, transforms separated

• ready for Parallel Processing, OOP doesn’t

Page 79: Introduction to Data-Oriented Design

Benefits of DOD

• Maximum performance

• CPU doesn’t wait & starve

• Easy to parallelize

• data is grouped, transforms separated

• ready for Parallel Processing, OOP doesn’t

• Simpler code

• surprise!

Page 80: Introduction to Data-Oriented Design

References: Memory

• Ulrich Drepper “What Every Computer Programmer Should Know About Memory”

• Крис Касперски “Техника оптимизации програм. Еффективное использование памяти”

• Christer Ericson “Memory Optimization”

• Igor Ostrovsky “Gallery of Processor Cache Effects”

Page 81: Introduction to Data-Oriented Design

References: DOD

• Noel Llopis “Data-Oriented Design”, Game Developer Magazine, September 2009

• Richard Fabian “Data-Oriented Desing”, book draft http://www.dataorienteddesign.com/dodmain/

• Tony Albrecht “Pitfalls of Object-Oriented Programming”

• Niklas Frykholm “Practical Examples of Data Oriented Design”

• Mike Acton “Typical C++ Bullshit”

• Data Oriented Design @ Google+

Page 82: Introduction to Data-Oriented Design

Thank You!

Page 83: Introduction to Data-Oriented Design

Q?