performance optimization
play

Performance Optimization 2 Lab Schedule Activities Assignments - PowerPoint PPT Presentation

Computer Systems and Networks ECPE 170 Jeff Shafer University of the Pacific Performance Optimization 2 Lab Schedule Activities Assignments Due Today Lab 5 Due by Feb 26 th 5:00am Discussion on Performance


  1. ì Computer Systems and Networks ECPE 170 – Jeff Shafer – University of the Pacific Performance Optimization

  2. 2 Lab Schedule Activities Assignments Due Today Lab 5 ì ì Due by Feb 26 th 5:00am Discussion on Performance ì ì Optimization (Lab 6) Lab 6 ì Next Week ì Due by Mar 5 th 5:00am ì Lab 6 – Performance ì ** Midterm Exam ** ì Optimization Thursday, March 7 th ì Computer Systems and Networks Spring 2019

  3. 3 Person of the Day: Fran Allen IBM Research: 1957-2002 ì Expert in optimizing ì compilers (i.e. compilers that optimize the program they produce) Expert in parallelization ì Winner of ACM Turing ì Award , 2006 First female winner! ì Computer Systems and Networks Spring 2019

  4. 4 Person of the Day: Donald Knuth Author, The Art of Computer ì Programming Algorithms, algorithms, ì and more algorithms! Creator of TeX typesetting ì system Winner, ACM Turing Award , ì 1974 Computer Systems and Networks Spring 2019

  5. 5 LaTeX – Input \documentclass[12pt]{article} \usepackage{amsmath} \title{\LaTeX} \date{} \begin{document} \maketitle \LaTeX{} is a document preparation system for the \TeX{} typesetting program. It offers programmable desktop publishing features and extensive facilities for automating most aspects of typesetting and desktop publishing, including numbering and cross-referencing, tables and figures, page layout, bibliographies, and much more. \LaTeX{} was originally written in 1984 by Leslie Lamport and has become the dominant method for using \TeX; few people write in plain \TeX{} anymore. The current version is \LaTeXe. % This is a comment; it will not be shown in the final output. % The following shows a little of the typesetting power of LaTeX: \begin{align} E &= mc^2 \\ m &= \frac{m_0}{\sqrt{1-\frac{v^2}{c^2}}} \end{align} \end{document} Computer Systems and Networks Spring 2019

  6. 6 LaTeX – Output Side Note: LATEX works great in version control systems! Computer Systems and Networks Spring 2019

  7. 7 Quotes – Donald Knuth “ Computer programming is an art , because it applies accumulated knowledge to the world, because it requires skill and ingenuity, and especially because it produces objects of beauty. A programmer who subconsciously views himself as an artist will enjoy what he does and will do it better.” – Donald Knuth “Random numbers should not be generated with a method chosen at random.” – Donald Knuth Computer Systems and Networks Spring 2019

  8. 8 Quotes – Donald Knuth “People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like . Otherwise the programs they write will be pretty weird.” – Donald Knuth Remember this when we’re learning assembly programming later this semester! Computer Systems and Networks Spring 2019

  9. 9 ì Performance Optimization Computer Systems and Networks Spring 2019

  10. 10 Vote ì Who will do a better job improving program performance? ì The compiler -vs- The programmer Computer Systems and Networks Spring 2019

  11. 11 Lab 6 Goals What can the compiler do for programmers to 1. improve performance? What can programmers do to improve 2. performance? Computer Systems and Networks Spring 2019

  12. 12 ì The Compiler Computer Systems and Networks Spring 2019

  13. 13 Compiler Goals What are the compiler’s goals with optimization off? ì Obvious ì Generate binary (executable) that produces correct output ì when run Compile fast ì Less Obvious: ì Make debugging produce expected results! ì Statements are independent ì If you stop the program with a breakpoint between statements, ì you can then assign a new value to any variable or change the program counter to any other statement in the function and get exactly the results you expect from the source code Computer Systems and Networks Spring 2019

  14. 14 Compiler Goals ì What are the compiler’s goals with optimization on? ì Reduce program code size ì Reduce program execution time ì These may be mutually exclusive! Computer Systems and Networks Spring 2019

  15. 15 Compiler Optimization Levels O1: Moderately optimize the code, but do not increase the compilation time gcc -O1 -o myexec main.c O2: Optimize more, take time, but do not increase the code size gcc -O2 -o myexec main.c O3: Optimize aggressively, take time, even if code size increases! gcc -O3 -o myexec main.c Computer Systems and Networks Spring 2019

  16. 16 Optimization Tradeoffs ì What might we lose when we turn on optimization? ì Compilation will take a lot longer ì Debugging is harder Computer Systems and Networks Spring 2019

  17. 17 Compiler Optimizations Inline Functions Pros? Lower overhead ì ì Cons? Bigger binary ì int max(int a, int b) (except for tiny functions – like this?) { if(a>b) return a; if(w>x) max1 = w; else else max1 = x; return b; } if(y>z)max2 = y; else max2 = z; max1 = max(w,x); printf("%i %i\n", max2 = max(y,z); max1, max2); printf("%i %i\n", max1, max2); P1 Computer Systems and Networks Spring 2019

  18. 18 Compiler Optimizations What specific overhead Calling a function ì ì exists here? Save variables in the ì processor (“registers”) to memory (in the stack) Jump to the function ì int max(int a, int b) Create new stack space for ì { function and its local if(a>b) variables return a; else Returning from function return b; ì } Load old values from stack ì Jump to prior location ì Computer Systems and Networks Spring 2019

  19. 19 Compiler Optimizations Unroll Loops Pros? Lower overhead ì ì Parallelism (potentially) Cons? Bigger binary ì int x; int x; for (x = 0; x < 100; x+=5) for (x = 0; x < 100; x++) { { delete(x); delete(x); delete(x+1); } delete(x+2); delete(x+3); delete(x+4); } Computer Systems and Networks Spring 2019

  20. 20 Compiler Optimizations What specific loop Top of loop ì ì overhead exists here? Compare x against 100 ì If less than, jump to … ì Otherwise, jump to… ì int x; for (x = 0; x < 100; x++) Bottom of loop ì { Increment x by 1 delete(x); ì } Jump to top of loop ì Impact on Branch Predictor ì (CPU microarchitecture) Computer Systems and Networks Spring 2019

  21. 21 Compiler Optimizations Loops Vectorization Pros? Parallelism ì ì Cons? Requires specific ì features in CPU Vector units: for(i=0; i<16; i++) { A[0] A[1] A[2] A[3] C[i]=A[i]+B[i]; } B[0] B[1] B[2] B[3] C[0] C[1] C[2] C[3] Computer Systems and Networks Spring 2019

  22. 22 Compiler Optimizations ì A large number of common compiler optimizations won’t make sense until we learn assembly code later this semester The compiler is optimizing the assembly code , not ì the high-level source code Computer Systems and Networks Spring 2019

  23. 23 ì The Programmer Computer Systems and Networks Spring 2019

  24. 24 The Compiler –vs–The Programmer ì Humans can do a better job at optimizing code than the compiler Tradeoff: many developer-hours of time ì ì Big picture idea: The compiler must be safe and only make optimizations that function for all possible data sets . Even if the programmer knows that a particular ì corner case cannot happen, the compiler doesn't know that Computer Systems and Networks Spring 2019

  25. 25 The Compiler –vs–The Programmer Twiddle1() needs 6 memory Is this optimization safe for ì ì a compiler to do? accesses 2x read xp ì void twiddle1(int *xp, int *yp) { 2x read yp ì *xp += *yp; 2x write xp ì *xp += *yp; } Twiddle2() needs 3 memory ì accesses Read xp ì void twiddle2(int *xp, int *yp) { Read yp ì *xp += 2 * *yp; Write xp ì } Computer Systems and Networks Spring 2019

  26. 26 The Compiler –vs–The Programmer What if *xp and *yp pointed to the same memory address? ì Twiddle1() ì *xp += *xp; ì *xp += *xp; // *xp increased 4x ì Twiddle2() ì *xp += 2 * *xp; // *xp increased 3x ì This is memory aliasing (two pointers to the same address), ì and is hard for compilers to detect But the programmer can know whether aliasing is a concern! ì Computer Systems and Networks Spring 2019

  27. 27 The Compiler –vs–The Programmer ì Is this optimization safe for a compiler to do? int f(); int func1() { return f() + f() + f() + f(); } int func2() { return 4*f(); } Computer Systems and Networks Spring 2019

  28. 28 The Compiler –vs–The Programmer ì Depends on what f() does! int counter = 0; int f() { return counter++; } ì With func1(): 0+1+2+3 = 6 ì With func2(): 4*0 = 0 ì Hard for compiler to detect side effects Computer Systems and Networks Spring 2019

  29. 29 The Compiler –vs–The Programmer ì Compare two functions that convert a string to lowercase ì What does strlen() do void lower1(char *s) again? { int i; void lower2(char *s) for (i = 0; i < strlen(s); i++) { if (s[i] >= 'A' && s[i] <= 'Z') s[i] -= ('A' - 'a'); int i; int len = strlen(s); } for (i = 0; i < len; i++) ì Could the compiler make if (s[i] >= 'A' && s[i] <= 'Z') this optimization for us? s[i] -= ('A' - 'a'); } Computer Systems and Networks Spring 2019

Recommend


More recommend