computer organization: a programmer's...

21
Computer Organization: A Programmer's Perspective Overview

Upload: others

Post on 13-May-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization:A Programmer's Perspective

Overview

Page 2: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 2

Reality Meets AbstractionReality Meets Abstraction CS emphasize abstraction

Theory: Abstract data types, asymptotic analysis Practice: APIs, standard libraries, programming language

These abstractions have limits Bugs tend to occur at these limits! Need to understand underlying implementations

Useful outcomes Become more effective programmers

Able to find and eliminate bugs efficiently Able to tune program performance

Prepare for later “systems” classes in CS Compilers, Operating Systems, Networks, ...

Page 3: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 3

Great Reality #1Great Reality #1

Int’s are not Integers, Float’s are not Reals

Examples (32bit C) Is x2 ≥ 0?

Float’s: Yes! Int’s:

40000 * 40000 --> 1600000000 50000 * 50000 --> ??

Is (x + y) + z = x + (y + z)? Unsigned & Signed Int’s: Yes! Float’s:

(1e20 + -1e20) + 3.14 --> 3.14 1e20 + (-1e20 + 3.14) --> ??

Page 4: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 4

Great Reality #2Great Reality #2Assembly and CPU are important!

Most of you will never write an entire program in assembly Compilers are much better & more patient than you are

But: Some will write code portions in assembly You are much smarter than compiler when optimizing e.g., SIMD instructions in image/sound processing, deep learning

Understanding assembly = Understanding machine Behavior of programs in presence of bugs

High-level language model breaks down Implementing system software

Compiler has machine code as target Operating systems must manage process state

Page 5: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 5

Great Reality #3Great Reality #3

Memory Matters

Memory is not unbounded It must be allocated and managed Many applications are memory dominated

Memory referencing bugs especially nasty Effects are distant in both time and space

Memory performance is not uniform Cache, virtual memory can greatly affect program performance Adapting program to memory system can improve speed (lots!)

Page 6: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 6

Carnegie Mellon

Memory System Performance Example

Hierarchical memory organization Performance depends on access patterns

void copyji(int src[2048][2048],            int dst[2048][2048]){  int i,j;  for (j = 0; j < 2048; j++)    for (i = 0; i < 2048; i++)      dst[i][j] = src[i][j];}

void copyij(int src[2048][2048],            int dst[2048][2048]){  int i,j;  for (i = 0; i < 2048; i++)    for (j = 0; j < 2048; j++)      dst[i][j] = src[i][j];}

21 times slower(Pentium 4)

Page 7: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 7

Memory Referencing ErrorsMemory Referencing Errors C and C++ do not provide any memory protection

Out of bounds array references, invalid pointer values This is part of what makes their compiled code FAST

Can lead to nasty bugs Whether or not bug has any effect depends on system, compiler Action at a distance

Corrupted object logically unrelated to one being accessed Effect of bug may be first observed long after it is generated

How can I deal with this? Program in Java, Lisp, or ML (and suffer consequences) Understand what possible interactions may occur Use or develop tools to detect referencing errors

Page 8: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

8

Carnegie Mellon

Memory Referencing Bug Exampledouble fun(int i){ volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = 1073741824; /* Possibly out of bounds */ return d[0];}

fun(0) ➙ 3.14fun(1) ➙ 3.14fun(2) ➙ 3.1399998664856fun(3) ➙ 2.00000061035156fun(4) ➙ 3.14, then segmentation fault

Result is architecture specific

Page 9: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 9

Great Reality #4Great Reality #4

There’s more to performance than asymptotic complexity

Constant factors matter (too!) See 10:1 performance range, depending on how code written Optimize at multiple levels: algorithm, representations, loops, ...

Must understand system to optimize performance How programs compiled and executed How to measure program performance and identify bottlenecks Improve without destroying code modularity and generality

Page 10: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 10

Carnegie Mellon

Example Matrix Multiplication

Standard desktop computer, vendor compiler, using optimization flags Both implementations have exactly the same operations count (2n3) What is going on?

Matrix-Matrix Multiplication (MMM) on 2 x Core 2 Duo 3 GHz (double precision)Gflop/s

160x

Triple loop

Best code (K. Goto)

Page 11: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 11

Carnegie Mellon

MMM Plot: AnalysisMatrix-Matrix Multiplication (MMM) on 2 x Core 2 Duo 3 GHzGflop/s

Memory hierarchy and other optimizations: 20x

Vector instructions: 4x

Multiple threads: 4x

Reason for 20x: Blocking or tiling, loop unrolling, array scalarization, instruction scheduling, search to find best choice

Effect: fewer register spills, L1/L2 cache misses, and TLB misses

Page 12: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 12

Great Reality #5Great Reality #5

Computers do more than execute programs

They need to get data in and out I/O system critical to program reliability and performance How does information move from one component to other?

Exploit parallelism: Compute while waiting for data Use multiple cores, multiple processors (GPU), etc.

Interface with many other devices Other computers Other electronic devices

Page 13: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 13

Most Systems Courses are Builder-CentricMost Systems Courses are Builder-Centric

Operating Systems Implement large portions of operating system

Compilers Write compiler for simple language

Networking Implement and simulate network protocols

Computer Architecture Design pipelined processor in Verilog

Page 14: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 14

Our Course is Programmer-CentricOur Course is Programmer-Centric

Purpose:

Show how by knowing more about the underlying system, one can be more effective as a programmer

Enable you to Write programs that are more reliable and efficient Incorporate features that require hooks into OS

e.g., concurrency, signal handlers Cover material that you won’t see elsewhere Encourage thinking about the system, not component

Page 15: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Software Development Environments(SDKs: Software Deveopment Kits)

Editors (text and visual) Code translators (language A → B, B → C, … ) Debuggers, profilers (various levels) Linkers (hook together pieces of code) System libraries (ready-made code, hooks to OS) Deployment tools Installation, updates, monitoring

Page 16: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

A Complete SDK Eco-System (C/C++)

gcc compiler

a.c

p

Text EditorVisual Editora.c

C source codeh

Software ready for running

Run reports

A Complete SDK Eco-System (C/C++)

This is how some of you may think of programming.

Page 17: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

A Complete SDK Eco-System (C/C++)

m.c a.cText EditorVisual Editor

CodeGeneration

a.c a.xml

C source code

Page 18: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

A Complete SDK Eco-System (C/C++)

Other compiler

m.c

m.o

gcc compiler

a.c

a.o

Text EditorVisual Editor

CodeGeneration

a.c a.xml

arch.acl

Compiled code object

C source code

Text Editor

Target CPU/architecturedescription

Page 19: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

A Complete SDK Eco-System (C/C++)

Other compiler

m.c

m.o

gcc compiler

a.c

a.o

Static Linker (ld)

p

libwhatever.alibm.a

Text EditorVisual Editor

CodeGeneration

a.c a.xml

arch.acl

Compiled code snipplet

C source code

Text Editor

Libraries ofReady-made

(compiled) code Software ready for running

Target CPU/architecturedescription

Standard libraries

Page 20: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

A Complete SDK Eco-System (C/C++)

Other compiler

m.c

m.o

gcc compiler

a.c

a.o

libc.so

Static Linker (ld)

p

Loader/Dynamic Linker

libwhatever.a

p’

libm.a

Text EditorVisual Editor

CodeGeneration

a.c a.xml

arch.acl

Compiled code snipplet

C source code

Text Editor

Libraries ofReady-made

(compiled) code

Operating systemlibraries

Software ready for running

Target CPU/architecturedescription

Installation tools

Standard libraries

Deployed (running)software

Profiling, debugging, simulation(gprof, gdb)

Run reports

Page 21: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/01-overview.pdf · Computer Organization: A Programmer's Perspective Based on class notes by

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 21

שאלות?