38
Advanced Topics in Numerical Analysis: High Performance Computing Memory hierarchies Georg Stadler Dhairya Malhotra Courant Institute, NYU [email protected] [email protected] Spring 2019, Monday, 5:10–7:00PM, WWH #1302 Feb 4, 2019 1 / 27

Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Advanced Topics in Numerical Analysis:High Performance Computing

Memory hierarchies

Georg Stadler Dhairya MalhotraCourant Institute, NYU

[email protected] [email protected]

Spring 2019, Monday, 5:10–7:00PM, WWH #1302

Feb 4, 2019

1 / 27

Page 2: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Outline

Organization issues

Summary of last class

Memory hierarchies (single CPU)

Tools/commands

2 / 27

Page 3: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Organization issues

I If you’re not in the class’ Slack group yet, please let us know.

I Homework assignment #1 is posted and due next week.I Part I: Find examples for HPC; Hand in a PDF separately, we

will post link to folder where you can put this file–everybodywill have access to it.

I Part II: Simple single-core examples. We’ll improve these overthe semester.

I Public course website contains an outline (which mightchange a bit): https://nyu-hpc19.github.io/

I No class on Feb 25!

I Questions?

3 / 27

Page 4: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Organization issues

I If you’re not in the class’ Slack group yet, please let us know.

I Homework assignment #1 is posted and due next week.I Part I: Find examples for HPC; Hand in a PDF separately, we

will post link to folder where you can put this file–everybodywill have access to it.

I Part II: Simple single-core examples. We’ll improve these overthe semester.

I Public course website contains an outline (which mightchange a bit): https://nyu-hpc19.github.io/

I No class on Feb 25!

I Questions?

3 / 27

Page 5: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Organization issues

I If you’re not in the class’ Slack group yet, please let us know.

I Homework assignment #1 is posted and due next week.I Part I: Find examples for HPC; Hand in a PDF separately, we

will post link to folder where you can put this file–everybodywill have access to it.

I Part II: Simple single-core examples. We’ll improve these overthe semester.

I Public course website contains an outline (which mightchange a bit): https://nyu-hpc19.github.io/

I No class on Feb 25!

I Questions?

3 / 27

Page 6: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Plan for today

I Summary of last week’s material (Moore’s law, multicore,HPC overview)

I Finish example problems in OpenMP and MPI

I Memory hierarchies (caches), basic performance models,single core performance

I Code examples; Tools: valgrind and cachegrind

4 / 27

Page 7: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Outline

Organization issues

Summary of last class

Memory hierarchies (single CPU)

Tools/commands

5 / 27

Page 8: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Moore’s law todayI Frequency/clock speed stopped growing in ∼ 2004

I Number of cores per CPUI Moore’s law still holdsI Energy use ∼bounded

0

1

10

100

1000

10000

100000

1000000

10000000

1970 1975 1980 1985 1990 1995 2000 2005 2010

Transistors (Thousands) Frequency (MHz) Power (W) Cores

Source: CS Department, UC Berkeley.

6 / 27

Page 9: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Parallel computing ⊂ high-performance computing

I All major vendors produce multicore chips—need to thinkdifferently about applications.

I How well can applications and algorithms exploit parallelism?

I Memory density (DRAM) grows at slower rate.Loading/writing to memory is slow (O(100) clock cycles)

I Top500 list: leading machines have > 107 processor cores,and often two different kinds of compute chips (CPUs andsome kind of accelerators (e.g., GPUs)).

7 / 27

Page 10: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Do we really need larger and faster?

Simulation has become the third pillar of Science:

theory experiment

simulation

HPC computing used in: weather prediction, climate modeling,drug design, astrophysics, earthquake modeling, semiconductordesign, crash test simulations, financial modeling, . . .

8 / 27

Page 11: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Basic CS terms recalled

I compiler: translates human code into machine languageI CPU/processor: central processing unit caries out instructions

of a computer program, i.e., arithmetic/logical operations,input/output

I core: individual processing unit in a CPU, “multicore” CPU;will sometimes use “processors” in a sloppy way, and actuallymean “cores”

I clock rate/frequency: indicator of speed in which instructionsare performed

I floating point operation: multiplication add of two floatingpoint numbers, usually double precision (64 bit, about 16digits)

I peak performance: fastest theoretical flop/sI sustained performance: flop/s in actual computationI memory hierarchy: large memories (RAM/disc/solid state) are

slow; fast memories (L1/L2/L3 cache) are small

9 / 27

Page 12: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Outline

Organization issues

Summary of last class

Memory hierarchies (single CPU)

Tools/commands

10 / 27

Page 13: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Flop/s versus Mop/sFor many practical applications, memory access is the bottleneck,not floating point operations.

Development of memory versus processor performance.

I Most applications run at < 10% of the theoretical peakperformance.

I Mostly a single core issue; on parallel computers, thingsbecome even more difficult.

11 / 27

Page 14: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchies

Computer architecture is complicated. We need a basicperformance model.

I Processor needs to be “fed” with data to work on.

I Memory access is slow; memory hierarchies help.

I This is a single processor issue, but it’s even more importanton parallel computers.

More CS terms:

I latency: time it takes to load/write data from/at a specificlocation in RAM to/from the CPU registers (in seconds)

I bandwidth: rate at which data can be read/written (for largedata); in (bytes/second);

Bandwidth grows faster than latency.

12 / 27

Page 15: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchies

Computer architecture is complicated. We need a basicperformance model.

I Processor needs to be “fed” with data to work on.

I Memory access is slow; memory hierarchies help.

I This is a single processor issue, but it’s even more importanton parallel computers.

More CS terms:

I latency:

time it takes to load/write data from/at a specificlocation in RAM to/from the CPU registers (in seconds)

I bandwidth: rate at which data can be read/written (for largedata); in (bytes/second);

Bandwidth grows faster than latency.

12 / 27

Page 16: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchies

Computer architecture is complicated. We need a basicperformance model.

I Processor needs to be “fed” with data to work on.

I Memory access is slow; memory hierarchies help.

I This is a single processor issue, but it’s even more importanton parallel computers.

More CS terms:

I latency: time it takes to load/write data from/at a specificlocation in RAM to/from the CPU registers (in seconds)

I bandwidth: rate at which data can be read/written (for largedata); in (bytes/second);

Bandwidth grows faster than latency.

12 / 27

Page 17: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchies

Computer architecture is complicated. We need a basicperformance model.

I Processor needs to be “fed” with data to work on.

I Memory access is slow; memory hierarchies help.

I This is a single processor issue, but it’s even more importanton parallel computers.

More CS terms:

I latency: time it takes to load/write data from/at a specificlocation in RAM to/from the CPU registers (in seconds)

I bandwidth:

rate at which data can be read/written (for largedata); in (bytes/second);

Bandwidth grows faster than latency.

12 / 27

Page 18: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchies

Computer architecture is complicated. We need a basicperformance model.

I Processor needs to be “fed” with data to work on.

I Memory access is slow; memory hierarchies help.

I This is a single processor issue, but it’s even more importanton parallel computers.

More CS terms:

I latency: time it takes to load/write data from/at a specificlocation in RAM to/from the CPU registers (in seconds)

I bandwidth: rate at which data can be read/written (for largedata); in (bytes/second);

Bandwidth grows faster than latency.

12 / 27

Page 19: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchies

Computer architecture is complicated. We need a basicperformance model.

I Processor needs to be “fed” with data to work on.

I Memory access is slow; memory hierarchies help.

I This is a single processor issue, but it’s even more importanton parallel computers.

More CS terms:

I latency: time it takes to load/write data from/at a specificlocation in RAM to/from the CPU registers (in seconds)

I bandwidth: rate at which data can be read/written (for largedata); in (bytes/second);

Bandwidth grows faster than latency.

12 / 27

Page 20: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchiesOn my Mac Book Pro: 32KB L1 Cache, 256KB L2 Cache, 3MB Cache, 8GB RAM

CPU: O(1ns), L2/L3: O(10ns), RAM: O(100ns), disc: O(10ms)

13 / 27

Page 21: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchies

Copyright © 2012, Elsevier Inc. All rights reserved.

Levels of Memory HierarchyLevels of M

emory H

ierarchy

14 / 27

Page 22: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchiesDecreasing memory latency

I Eliminate memory operations by saving data in fast memoryand reusing them, i.e., temporal locality: Access an item thatwas previously accessed

I Explore bandwidth by moving a chunk of data into the fastmemory: spatial locality: Access data nearby previous accesses

I Overlap computation and memory access (pre-fetching; mostlyfigured out by compiler, but the compiler often needs help)

More CS terms:

I cache-hit: required data is available in cache ⇒ fast access

I cache-miss: required data is not in cache and must be loadedfrom main memory (RAM) ⇒ slow access

15 / 27

Page 23: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchiesDecreasing memory latency

I Eliminate memory operations by saving data in fast memoryand reusing them, i.e., temporal locality: Access an item thatwas previously accessed

I Explore bandwidth by moving a chunk of data into the fastmemory: spatial locality: Access data nearby previous accesses

I Overlap computation and memory access (pre-fetching; mostlyfigured out by compiler, but the compiler often needs help)

More CS terms:

I cache-hit:

required data is available in cache ⇒ fast access

I cache-miss: required data is not in cache and must be loadedfrom main memory (RAM) ⇒ slow access

15 / 27

Page 24: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchiesDecreasing memory latency

I Eliminate memory operations by saving data in fast memoryand reusing them, i.e., temporal locality: Access an item thatwas previously accessed

I Explore bandwidth by moving a chunk of data into the fastmemory: spatial locality: Access data nearby previous accesses

I Overlap computation and memory access (pre-fetching; mostlyfigured out by compiler, but the compiler often needs help)

More CS terms:

I cache-hit: required data is available in cache ⇒ fast access

I cache-miss: required data is not in cache and must be loadedfrom main memory (RAM) ⇒ slow access

15 / 27

Page 25: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchiesDecreasing memory latency

I Eliminate memory operations by saving data in fast memoryand reusing them, i.e., temporal locality: Access an item thatwas previously accessed

I Explore bandwidth by moving a chunk of data into the fastmemory: spatial locality: Access data nearby previous accesses

I Overlap computation and memory access (pre-fetching; mostlyfigured out by compiler, but the compiler often needs help)

More CS terms:

I cache-hit: required data is available in cache ⇒ fast access

I cache-miss:

required data is not in cache and must be loadedfrom main memory (RAM) ⇒ slow access

15 / 27

Page 26: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchiesDecreasing memory latency

I Eliminate memory operations by saving data in fast memoryand reusing them, i.e., temporal locality: Access an item thatwas previously accessed

I Explore bandwidth by moving a chunk of data into the fastmemory: spatial locality: Access data nearby previous accesses

I Overlap computation and memory access (pre-fetching; mostlyfigured out by compiler, but the compiler often needs help)

More CS terms:

I cache-hit: required data is available in cache ⇒ fast access

I cache-miss: required data is not in cache and must be loadedfrom main memory (RAM) ⇒ slow access

15 / 27

Page 27: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchySimple model

1. Only consider two levels in hierarchy, fast (cache) and slow(RAM) memory

2. All data is initially in slow memory3. Simplifications:

I Ignore that memory access and arithmetic operations canhappen at the same time

I assume time for access to fast memory is 0

4. Computational intensity: flops per slow memory access

q =f

m, where f . . .#flops,m . . .#slow memop.

Actual compute time:

ftf +mtm = ftf (1 +tmtf

1

q),

where tf is time per flop, and tm the time per slow memory access.Computational intensity should be as large as possible.

16 / 27

Page 28: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchyExample: Computational intensity for Matrix-matrix multiply

I Matrix-vector multiplication: x,y ∈ Rn, A ∈ Rn×n

y = y +Ax

flops: ∼ 2n2, memory read/write: ∼ 3n+ n2

Computational intensity: ∼2 (memory-bound!)

I Matrix-matrix multiplication: A,B,C ∈ Rn×n

C = C +AB

flops: ∼ 2n3, memory read/write: ∼ 3n2 + n3

Computational intensity: ∼2 (memory-bound!)

17 / 27

Page 29: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchyExample: Computational intensity for Matrix-matrix multiply

Can this be improved using fast memory? Yes! (Sketch—will bepart of the next homework).

I Matrix-matrix multiplication with tiling: A,B,C ∈ Rn×n

C = C +AB

We’ll consider N ×N blocks of size b× b of the matrices, i.e.,b = n/N .

I N3 block reads of A,B; 2N2 reads/writes of C;I memory access: (2N3 + 2N2)b2 ∼ 2Nn2 + 2n2

I Computational intensity q ∼ b!I Gives a much higher computational intensity, much faster!

18 / 27

Page 30: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchyExample: Matrix-matrix multiply

Comparison between naive and blocked optimized matrix-matrixmultiplication for different matrix sizes.

Comparison between optimized and naive matrix-matrix multiplication on old hardware with peak of 330MFlops.

Source: J. Demmel, Berkeley

BLAS: Optimized Basic Linear Algebra Subprograms19 / 27

Page 31: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Memory hierarchy

To summarize:

I Temporal and spatial locality is key for fast performance.

I Simple performance model: fast and slow memory; onlycounts loads into fast memory; computational intensity shouldbe high.

I Since arithmetic is cheap compared to memory access, one canconsider making extra flops if it reduces the memory access.

I In distributed-memory parallel computations, the memoryhierarchy is extended to data stored on other processors, whichis only available through communication over the network.

https://github.com/NYU-HPC19/lecture2

20 / 27

Page 32: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Outline

Organization issues

Summary of last class

Memory hierarchies (single CPU)

Tools/commands

21 / 27

Page 33: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

The module command (last week’s tool)

module listmodule avail . . .module load python/3.4module unload texlive-2016module whatis gcc-6.1.0

22 / 27

Page 34: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Valgrind and cachegrind (this week’s tool)

Valgrind

I memory management tool and suite for debugging, also inparallel

I profiles heap (not stack) memory access

I simulates a CPU in software

I running code with valgrind makes it slower by factor of 10-100

I not installed by default on only available on Mac OS; use forMPI-parallel debugging on Mac limited

I Documentation: http://valgrind.org/docs/manual/

memcheckfinds leaks

inval. mem. accessuninitialize mem.

incorrect mem. frees

cachegrindcache profiler

sources of cachemisses

callgrindextension tocachegrind

function call graph

23 / 27

Page 35: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Valgrind and cachegrind

Usage (see examples):

Run with valgrind (no recompile necessary!)valgrind --tool=memcheck [options] ./a.out [args]

Test examples for valgrind memcheck:

https://github.com/NYU-HPC19/lecture2

24 / 27

Page 36: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Valgrind and cachegrind

Run cachegrind profiler:valgrind --tool=cachegrind [options] ./a.out [args]

Visualize results of cachegrind:cg annotate --auto=yes cachegrind.out***

To illustrate the use of cachegrind, we used the vectormultiplication problem:

https://github.com/NYU-HPC19/lecture2

valgrind --tool=cachegrind

./inner-mem vec size

25 / 27

Page 37: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Bandwidth

104 105 106 107 108

101

102

array size (bytes)

Ban

dw

isth

(GB

/s)

readwrite

read+write

26 / 27

Page 38: Advanced Topics in Numerical Analysis: High Performance ... · memory:spatial locality:Access data nearby previous accesses I Overlap computation and memory access (pre-fetching;

Latency

103 104 105 106 107 108 109

101

102

array size (bytes)

cycl

essequential

random-sequence

27 / 27