Instructor: Erol Sahin Program Optimization CENG331: Introduction to Computer Systems 11 th Lecture Acknowledgement: Most of the slides are adapted from.

Slides:



Advertisements
Similar presentations
Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Advertisements

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Computer Organization and Architecture
Carnegie Mellon Today Program optimization  Optimization blocker: Memory aliasing  Out of order processing: Instruction level parallelism  Understanding.
Program Optimization (Chapter 5)
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
CPE 731 Advanced Computer Architecture ILP: Part V – Multiple Issue Dr. Gheith Abandah Adapted from the slides of Prof. David Patterson, University of.
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
1 Code Optimization(II). 2 Outline Understanding Modern Processor –Super-scalar –Out-of –order execution Suggested reading –5.14,5.7.
Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University Program Optimization.
1 Seoul National University Wrap-Up. 2 Overview Seoul National University Wrap-Up of PIPE Design  Exception conditions  Performance analysis Modern.
Chapter 8. Pipelining. Instruction Hazards Overview Whenever the stream of instructions supplied by the instruction fetch unit is interrupted, the pipeline.
Limits on ILP. Achieving Parallelism Techniques – Scoreboarding / Tomasulo’s Algorithm – Pipelining – Speculation – Branch Prediction But how much more.
Fall 2011SYSC 5704: Elements of Computer Systems 1 SYSC 5704 Elements of Computer Systems Optimization to take advantage of hardware.
PipelinedImplementation Part I CSC 333. – 2 – Overview General Principles of Pipelining Goal Difficulties Creating a Pipelined Y86 Processor Rearranging.
Code Optimization I September 24, 2007 Topics Machine-Independent Optimizations Basic optimizations Optimization blockers class08.ppt F’07.
Code Optimization II Feb. 19, 2008 Topics Machine Dependent Optimizations Understanding Processor Operations Branches and Branch Prediction class11.ppt.
Code Optimization II September 26, 2007 Topics Machine Dependent Optimizations Understanding Processor Operations Branches and Branch Prediction class09.ppt.
Code Optimization 1. Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.1 ~
1 Code Optimization. 2 Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.2 ~ 5.6.
Recitation 7: 10/21/02 Outline Program Optimization –Machine Independent –Machine Dependent Loop Unrolling Blocking Annie Luo
University of Amsterdam Computer Systems – optimizing program performance Arnoud Visser 1 Computer Systems Optimizing program performance.
Assembly Code Optimization Techniques for the AMD64 Athlon and Opteron Architectures David Phillips Robert Duckles Cse 520 Spring 2007 Term Project Presentation.
About the Slides for Introduction to Computer Systems : Introduction to Computer Systems 0th Lecture, Sep. 1, 2015 Markus Püschel ETH Zurich (with.
University of Amsterdam Computer Systems – optimizing program performance Arnoud Visser 1 Computer Systems Optimizing program performance.
Computer Architecture: Wrap-up CENG331 - Computer Organization Instructors: Murat Manguoglu(Section 1) Erol Sahin (Section 2 & 3) Adapted from slides of.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Code Optimization II: Machine Dependent Optimization Topics Machine-Dependent Optimizations Unrolling Enabling instruction level parallelism.
Machine Independent Optimizations Topics Code motion Reduction in strength Common subexpression sharing.
Code Optimization and Performance CS 105 “Tour of the Black Holes of Computing”
Programming for Performance CS 740 Oct. 4, 2000 Topics How architecture impacts your programs How (and how not) to tune your code.
Machine-Dependent Optimization CS 105 “Tour of the Black Holes of Computing”
1 Code Optimization. 2 Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.2 ~ 5.6.
Carnegie Mellon About the New Slides for Introduction to Computer Systems /18-243, spring 2009 Markus Püschel Electrical and Computer Engineering.
Real-World Pipelines Idea –Divide process into independent stages –Move objects through stages in sequence –At any given times, multiple objects being.
PipeliningPipelining Computer Architecture (Fall 2006)
Code Optimization and Performance II
Real-World Pipelines Idea Divide process into independent stages
Code Optimization.
Assembly language.
Code Optimization II September 27, 2006
Machine-Dependent Optimization
Code Optimization II Dec. 2, 2008
CS 3114 Many of the following slides are taken with permission from
5.2 Eleven Advanced Optimizations of Cache Performance
Chapter 14 Instruction Level Parallelism and Superscalar Processors
Code Optimization I: Machine Independent Optimizations
Instructors: Dave O’Hallaron, Greg Ganger, and Greg Kesden
Program Optimization CENG331 - Computer Organization
Instructors: Pelin Angin and Erol Sahin
Program Optimization CSCE 312
Code Optimization II: Machine Dependent Optimizations Oct. 1, 2002
Code Optimization II: Machine Dependent Optimizations
Instruction Level Parallelism and Superscalar Processors
Machine-Dependent Optimization
Seoul National University
Code Optimization and Performance
Code Optimization(II)
Code Optimization April 6, 2000
Code Optimization I Nov. 25, 2008
Pipelined Implementation : Part I
* From AMD 1996 Publication #18522 Revision E
COMP 2130 Intro Computer Systems Thompson Rivers University
Optimizing program performance
Program Optimization CSE 238/2038/2138: Systems Programming
CS 3114 Many of the following slides are taken with permission from
Machine-Independent Optimization
Lecture 11: Machine-Dependent Optimization
Code Optimization II October 2, 2001
Presentation transcript:

Instructor: Erol Sahin Program Optimization CENG331: Introduction to Computer Systems 11 th Lecture Acknowledgement: Most of the slides are adapted from the ones prepared by R.E. Bryant, D.R. O’Hallaron of Carnegie-Mellon Univ.

– 2 – Overview Program optimization Removing unnecessary procedure calls Code motion/precomputation Strength reduction Sharing of common subexpressions Optimization blocker: Procedure calls

– 3 – Example Matrix Multiplication Standard desktop computer, compiler, using optimization flags Both implementations have exactly the same operations count (2n 3 ) What is going on? 160x Triple loop Best code This code is not obviously stupid

– 4 – MMM Plot: Analysis 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: more instruction level parallelism, better register use, less L1/L2 cache misses, less TLB misses

– 5 – Harsh Reality There’s more to runtime performance than asymptotic complexity One can easily loose 10x, 100x in runtime or even more What matters: Constants (100n and 5n is both O(n), but ….) Coding style (unnecessary procedure calls, unrolling, reordering, …) Algorithm structure (locality, instruction level parallelism, …) Data representation (complicated structs or simple arrays)

– 6 – Harsh Reality Must optimize at multiple levels: Algorithm Data representations Procedures Loops Must understand system to optimize performance How programs are compiled and executed Execution units, memory hierarchy How to measure program performance and identify bottlenecks How to improve performance without destroying code modularity and generality

– 7 – Optimizing Compilers Use optimization flags, default is no optimization (-O0)! Good choices for gcc: -O2, -O3, -march=xxx, -m64 Try different flags and maybe different compilers -O

– 8 – Example Compiled without flags: ~1300 cycles Compiled with –O3 –m64 -march=… –fno-tree-vectorize ~150 cycles Core 2 Duo, 2.66 GHz double a[4][4]; double b[4][4]; double c[4][4]; # set to zero /* Multiply 4 x 4 matrices a and b */ void mmm(double *a, double *b, double *c, int n) { int i, j, k; for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) for (k = 0; k < 4; k++) c[i*4+j] += a[i*4 + k]*b[k*4 + j]; }

– 9 – Optimizing Compilers Compilers are good at: mapping program to machine register allocation code selection and ordering (scheduling) dead code elimination eliminating minor inefficiencies Compilers are not good at: improving asymptotic efficiency up to programmer to select best overall algorithm big-O savings are (often) more important than constant factors but constant factors also matter Compilers are not good at: overcoming “optimization blockers” potential memory aliasing potential procedure side-effects

– 10 – Limitations of Optimizing Compilers If in doubt, the compiler is conservative Operate under fundamental constraints Must not change program behavior under any possible condition Often prevents it from making optimizations when would only affect behavior under pathological conditions. Behavior that may be obvious to the programmer can be obfuscated by languages and coding styles e.g., data ranges may be more limited than variable types suggest Most analysis is performed only within procedures Whole-program analysis is too expensive in most cases Most analysis is based only on static information Compiler has difficulty anticipating run-time inputs

– 11 – Example: Data Type for Vectors /* data structure for vectors */ typedef struct{ int len; double *data; } vec; /* retrieve vector element and store at val */ double get_vec_element(*vec, idx, double *val) { if (idx = v->len) return 0; *val = v->data[idx]; return 1; } len data 0 1len-1

– 12 – Performance Time quadruples when double string length Quadratic performance CPU Seconds String Length

– 13 – Why is That? String length is called in every iteration! And strlen is O(n), so lower is O(n 2 ) void lower(char *s) { int i; for (i = 0; i < strlen(s); i++) if (s[i] >= 'A' && s[i] <= 'Z') s[i] -= ('A' - 'a'); } /* My version of strlen */ size_t strlen(const char *s) { size_t length = 0; while (*s != '\0') { s++; length++; } return length; }

– 14 – Improving Performance Move call to strlen outside of loop Since result does not change from one iteration to another Form of code motion/precomputation void lower(char *s) { int i; int len = strlen(s); for (i = 0; i < len; i++) if (s[i] >= 'A' && s[i] <= 'Z') s[i] -= ('A' - 'a'); } void lower(char *s) { int i; for (i = 0; i < strlen(s); i++) if (s[i] >= 'A' && s[i] <= 'Z') s[i] -= ('A' - 'a'); }

– 15 – Performance Lower2: Time doubles when double string length Linear performance CPU Seconds String Length

– 16 – Optimization Blocker: Procedure Calls Why couldn’t compiler move strlen out of inner loop? Procedure may have side effects Function may not return same value for given arguments Could depend on other parts of global state Procedure lower could interact with strlen Compiler usually treats procedure call as a black box that cannot be analyzed Consequence: conservative in optimizationsRemedies: Inline the function if possible Do your own code motion int lencnt = 0; size_t strlen(const char *s) { size_t length = 0; while (*s != '\0') { s++; length++; } lencnt += length; return length; }

– 17 – Today Program optimization Optimization blocker: Memory aliasing Out of order processing: Instruction level parallelism Understanding branch prediction

– 18 – Optimization Blocker: Memory Aliasing Code updates b[i] (= memory access) on every iteration Why couldn’t compiler optimize this away? # sum_rows1 inner loop.L53: addsd(%rcx), %xmm0# FP add addq$8, %rcx decq%rax movsd%xmm0, (%rsi,%r8,8)# FP store jne.L53 /* Sums rows of n x n matrix a and stores in vector b */ void sum_rows1(double *a, double *b, long n) { long i, j; for (i = 0; i < n; i++) { b[i] = 0; for (j = 0; j < n; j++) b[i] += a[i*n + j]; } a b Σ

– 19 – Reason If memory is accessed, compiler assumes the possibility of side effects Example: double A[9] = { 0, 1, 2, 4, 8, 16}, 32, 64, 128}; double B[3] = A+3; sum_rows1(A, B, 3); i = 0: [3, 8, 16] init: [4, 8, 16] i = 1: [3, 22, 16] i = 2: [3, 22, 224] Value of B : /* Sums rows of n x n matrix a and stores in vector b */ void sum_rows1(double *a, double *b, long n) { long i, j; for (i = 0; i < n; i++) { b[i] = 0; for (j = 0; j < n; j++) b[i] += a[i*n + j]; }

– 20 – Removing Aliasing Scalar replacement: Copy array elements that are reused into temporary variables Assumes no memory aliasing (otherwise possibly incorrect) # sum_rows2 inner loop.L66: addsd(%rcx), %xmm0 # FP Add addq$8, %rcx decq%rax jne.L66 /* Sums rows of n x n matrix a and stores in vector b */ void sum_rows2(double *a, double *b, long n) { long i, j; for (i = 0; i < n; i++) { double val = 0; for (j = 0; j < n; j++) val += a[i*n + j]; b[i] = val; }

– 21 – Unaliased Version When Aliasing Happens Aliasing still creates interference Result different than before double A[9] = { 0, 1, 2, 4, 8, 16}, 32, 64, 128}; double B[3] = A+3; sum_rows1(A, B, 3); i = 0: [3, 8, 16] init: [4, 8, 16] i = 1: [3, 27, 16] i = 2: [3, 27, 224] Value of B : /* Sum rows is of n X n matrix a and store in vector b */ void sum_rows2(double *a, double *b, long n) { long i, j; for (i = 0; i < n; i++) { double val = 0; for (j = 0; j < n; j++) val += a[i*n + j]; b[i] = val; }

– 22 – Optimization Blocker: Memory Aliasing Memory aliasing: Two different memory references write to the same location Easy to have happen in C Since allowed to do address arithmetic Direct access to storage structures Hard to analyze = compiler cannot figure it out Hence is conservative Solution: Scalar replacement in innermost loop Copy memory variables that are reused into local variables Basic scheme: Load: t1 = a[i], t2 = b[i+1], …. Compute: t4 = t1 * t2; …. Store: a[i] = t12, b[i+1] = t7, …

– 23 – More Difficult Example Matrix multiplication: C = A*B + C Which array elements are reused? All of them! But how to take advantage? c = (double *) calloc(sizeof(double), n*n); /* Multiply n x n matrices a and b */ void mmm(double *a, double *b, double *c, int n) { int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) c[i*n+j] += a[i*n + k]*b[k*n + j]; } ab i j * c = c +

– 24 – Step 1: Blocking (Here: 2 x 2) Blocking, also called tiling = partial unrolling + loop exchange Assumes associativity (= compiler will never do it) c = (double *) calloc(sizeof(double), n*n); /* Multiply n x n matrices a and b */ void mmm(double *a, double *b, double *c, int n) { int i, j, k; for (i = 0; i < n; i+=2) for (j = 0; j < n; j+=2) for (k = 0; k < n; k+=2) for (i1 = i; i1 < i+2; i1++) for (j1 = j; j1 < j+2; j1++) for (k1 = k; k1 < k+2; k1++) c[i1*n+j1] += a[i1*n + k1]*b[k1*n + j1]; } ab i1 j1 * c = c +

– 25 – Step 2: Unrolling Inner Loops c = (double *) calloc(sizeof(double), n*n); /* Multiply n x n matrices a and b */ void mmm(double *a, double *b, double *c, int n) { int i, j, k; for (i = 0; i < n; i+=2) for (j = 0; j < n; j+=2) for (k = 0; k < n; k+=2) } Every array element a[…], b[…],c[…] used twice Now scalar replacement can be applied c[i*n + j] = a[i*n + k]*b[k*n + j] + a[i*n + k+1]*b[(k+1)*n + j] + c[i*n + j] c[(i+1)*n + j] = a[(i+1)*n + k]*b[k*n + j] + a[(i+1)*n + k+1]*b[(k+1)*n + j] + c[(i+1)*n + j] c[i*n + (j+1)] = a[i*n + k]*b[k*n + (j+1)] + a[i*n + k+1]*b[(k+1)*n + (j+1)] + c[i*n + (j+1)] c[(i+1)*n + (j+1)] = a[(i+1)*n + k]*b[k*n + (j+1)] + a[(i+1)*n + k+1]*b[(k+1)*n + (j+1)] + c[(i+1)*n + (j+1)]

– 26 – Today Program optimization Optimization blocker: Memory aliasing Out of order processing: Instruction level parallelism Understanding branch prediction

– 27 – Example: Compute Factorials Machines  Intel Pentium 4 Nocona, 3.2 GHz  Intel Core 2, 2.7 GHz Compiler Versions  GCC int rfact(int n) { if (n <= 1) return 1; return n * rfact(n-1); } int fact(int n) { int i; int result = 1; for (i = n; i > 0; i--) result = result * i; return result; } MachineNoconaCore 2 rfact fact Cycles per element (or per mult) Something changed from Pentium 4 to Core: Details later

– 28 – Optimization 1: Loop Unrolling Compute more values per iteration Does not help here Why? Branch prediction – details later int fact_u3a(int n) { int i; int result = 1; for (i = n; i >= 3; i-=3) { result = result * i * (i-1) * (i-2); } for (; i > 0; i--) result *= i; return result; } MachineNoconaCore 2 rfact fact fact_u3a Cycles per element (or per mult)

– 29 – Optimization 2: Multiple Accumulators That seems to help. Can one get even faster? Explanation: instruction level parallelism – details later int fact_u3b(int n) { int i; int result0 = 1; int result1 = 1; int result2 = 1; for (i = n; i >= 3; i-=3) { result0 *= i; result1 *= (i-1); result2 *= (i-2); } for (; i > 0; i--) result0 *= i; return result0 * result1 * result2; } MachineNoconaCore 2 rfact fact fact_u3a fact_u3b Cycles per element (or per mult)

– 30 – Modern CPU Design Execution Functional Units Instruction Control Integer/ Branch FP Add FP Mult/Div LoadStore Instruction Cache Data Cache Fetch Control Instruction Decode Address Instructions Operations Prediction OK? Data Addr. General Integer Operation Results Retirement Unit Register File Register Updates

– 31 – Superscalar Processor Definition: A superscalar processor can issue and execute multiple instructions in one cycle. The instructions are retrieved from a sequential instruction stream and are usually scheduled dynamically. Benefit: without programming effort, superscalar processor can take advantage of the instruction level parallelism that most programs have Most CPUs since about 1998 are superscalar. Intel: since Pentium Pro

– 32 – Pentium 4 Nocona CPU Multiple instructions can execute in parallel 1 load, with address computation 1 store, with address computation 2 simple integer (one may be branch) 1 complex integer (multiply/divide) 1 FP/SSE3 unit 1 FP move (does all conversions) Some instructions take > 1 cycle, but can be pipelined InstructionLatencyCycles/Issue Load / Store51 Integer Multiply101 Integer/Long Divide36/10636/106 Single/Double FP Multiply72 Single/Double FP Add52 Single/Double FP Divide32/4632/46

– 33 – Latency versus Throughput Last slide: latencycycles/issue Integer Multiply101 Step 1 1 cycle Step 2 1 cycle Step 10 1 cycle Consequence: How fast can 10 independent int mults be executed? t1 = t2*t3; t4 = t5*t6; … How fast can 10 sequentially dependent int mults be executed? t1 = t2*t3; t4 = t5*t1; t6 = t7*t4; … Major problem for fast execution: Keep pipelines filled

– 34 – Hard Bounds How many cycles at least if Function requires n int mults? Function requires n float adds? Function requires n float ops (adds and mults)? Latency and throughput of instructions InstructionLatencyCycles/Issue Load / Store51 Integer Multiply101 Integer/Long Divide36/10636/106 Single/Double FP Multiply72 Single/Double FP Add52 Single/Double FP Divide32/4632/46

– 35 – Performance in Numerical Computing Numerical computing = computing dominated by floating point operations Example: Matrix multiplication Performance measure: Floating point operations per second (flop/s) Counting only floating point adds and mults Higher is better Like inverse runtime

– 36 – Nocona vs. Core 2 Nocona (3.2 GHz) InstructionLatencyCycles/Issue Load / Store51 Integer Multiply101 Integer/Long Divide36/10636/106 Single/Double FP Multiply72 Single/Double FP Add52 Single/Double FP Divide32/4632/46 Core 2 (2.7 GHz) (Recent Intel microprocessors) InstructionLatencyCycles/Issue Load / Store51 Integer Multiply31 Integer/Long Divide18/5018/50 Single/Double FP Multiply4/51 Single/Double FP Add31 Single/Double FP Divide18/3218/32

– 37 – Instruction Control Grabs instruction bytes from memory Based on current PC + predicted targets for predicted branches Hardware dynamically guesses whether branches taken/not taken and (possibly) branch target Translates instructions into micro-operations (for CISC style CPUs) Micro-op = primitive step required to perform instruction Typical instruction requires 1–3 operations Converts register references into tags Abstract identifier linking destination of one operation with sources of later operations Instruction Control Instruction Cache Fetch Control Instruction Decode Address Instructions Operations Retirement Unit Register File

– 38 – Translating into Micro-Operations Goal: Each operation utilizes single functional unit Requires: Load, integer arithmetic, store Exact form and format of operations is trade secret imulq %rax, 8(%rbx,%rdx,4) load 8(%rbx,%rdx,4)  temp1 imulq %rax, temp1  temp2 store temp2, 8(%rbx,%rdx,4)

– 39 – Traditional View of Instruction Execution Imperative View Registers are fixed storage locations Individual instructions read & write them Instructions must be executed in specified sequence to guarantee proper program behavior addq %rax, %rbx # I1 andq %rbx, %rdx # I2 mulq %rcx, %rbx # I3 xorq %rbx, %rdi # I4 rax + rbxrdxrcxrdi I1 I2 I3 I4 & * ^

– 40 – Dataflow View of Instruction Execution Functional View View each write as creating new instance of value Operations can be performed as soon as operands available No need to execute in original sequence rax.0 + rbx.0rdx.0rcx.0rdi.0 I1 I2/I3 I4 * ^ rbx.1 rdx.1 rbx.2 rdi.1 addq %rax, %rbx # I1 andq %rbx, %rdx # I2 mulq %rcx, %rbx # I3 xorq %rbx, %rdi # I4 &

– 41 – Example Computation Data Types Use different declarations for data_t int float double void combine4(vec_ptr v, data_t *dest) { int i; int length = vec_length(v); data_t *d = get_vec_start(v); data_t t = IDENT; for (i = 0; i < length; i++) t = t OP d[i]; *dest = t; } Operations Use different definitions of OP and IDENT + / 0 * / 1 d[1] OP d[2] OP d[3] OP … OP d[length-1]

– 42 – Cycles Per Element (CPE) Convenient way to express performance of program that operators on vectors or lists Length = n In our case: CPE = cycles per OP (gives hard lower bound) T = CPE*n + Overhead vsum1: Slope = 4.0 vsum2: Slope = 3.5

– 43 – x86-64 Compilation of Combine4 Inner Loop (Case: Integer Multiply) L33: # Loop: movl (%eax,%edx,4), %ebx # temp = d[i] incl %edx # i++ imull %ebx, %ecx # x *= temp cmpl %esi, %edx # i:length jl L33 # if < goto Loop void combine4(vec_ptr v, data_t *dest) { int i; int length = vec_length(v); data_t *d = get_vec_start(v); data_t t = IDENT; for (i = 0; i < length; i++) t = t OP d[i]; *dest = t; } MethodInt (add/mult)Float (add/mult) combine bound Cycles per element (or per OP)

– 44 – Combine4 = Serial Computation (OP = *) Computation (length=8) ((((((((1 * d[0]) * d[1]) * d[2]) * d[3]) * d[4]) * d[5]) * d[6]) * d[7]) Sequential dependence! Hence,  Performance: determined by latency of OP! * * 1d0d0 d1d1 * d2d2 * d3d3 * d4d4 * d5d5 * d6d6 * d7d7 MethodInt (add/mult)Float (add/mult) combine bound Cycles per element (or per OP)

– 45 – Loop Unrolling Perform 2x more useful work per iteration void unroll2a_combine(vec_ptr v, data_t *dest) { int length = vec_length(v); int limit = length-1; data_t *d = get_vec_start(v); data_t x = IDENT; int i; /* Combine 2 elements at a time */ for (i = 0; i < limit; i+=2) { x = (x OP d[i]) OP d[i+1]; } /* Finish any remaining elements */ for (; i < length; i++) { x = x OP d[i]; } *dest = x; }

– 46 – Effect of Loop Unrolling Helps integer sum Others don’t improve. Why? Still sequential dependency x = (x OP d[i]) OP d[i+1]; MethodInt (add/mult)Float (add/mult) combine unroll bound

– 47 – Loop Unrolling with Reassociation Can this change the result of the computation? Yes, for FP. Why? void unroll2aa_combine(vec_ptr v, data_t *dest) { int length = vec_length(v); int limit = length-1; data_t *d = get_vec_start(v); data_t x = IDENT; int i; /* Combine 2 elements at a time */ for (i = 0; i < limit; i+=2) { x = x OP (d[i] OP d[i+1]); } /* Finish any remaining elements */ for (; i < length; i++) { x = x OP d[i]; } *dest = x; }

– 48 – Effect of Reassociation Nearly 2x speedup for Int *, FP +, FP * Reason: Breaks sequential dependency Why is that? (next slide) x = x OP (d[i] OP d[i+1]); MethodInt (add/mult)Float (add/mult) combine unroll unroll2-ra bound

– 49 – Reassociated Computation What changed:  Ops in the next iteration can be started early (no dependency) Overall Performance  N elements, D cycles latency/op  Should be (N/2+1)*D cycles: CPE = D/2  Measured CPE slightly worse for FP * * 1 * * * d1d1 d0d0 * d3d3 d2d2 * d5d5 d4d4 * d7d7 d6d6 x = x OP (d[i] OP d[i+1]);

– 50 – Loop Unrolling with Separate Accumulators Different form of reassociation void unroll2a_combine(vec_ptr v, data_t *dest) { int length = vec_length(v); int limit = length-1; data_t *d = get_vec_start(v); data_t x0 = IDENT; data_t x1 = IDENT; int i; /* Combine 2 elements at a time */ for (i = 0; i < limit; i+=2) { x0 = x0 OP d[i]; x1 = x1 OP d[i+1]; } /* Finish any remaining elements */ for (; i < length; i++) { x0 = x0 OP d[i]; } *dest = x0 OP x1; }

– 51 – Effect of Separate Accumulators Amost exact 2x speedup (over unroll2) for Int *, FP +, FP * Breaks sequential dependency in a “cleaner,” more obvious way x0 = x0 OP d[i]; x1 = x1 OP d[i+1]; MethodInt (add/mult)Float (add/mult) combine unroll unroll2-ra unroll2-sa bound

– 52 – Separate Accumulators * * 1d1d1 d3d3 * d5d5 * d7d7 * * * 1d0d0 d2d2 * d4d4 * d6d6 x0 = x0 OP d[i]; x1 = x1 OP d[i+1]; What changed:  Two independent “streams” of operations Overall Performance  N elements, D cycles latency/op  Should be (N/2+1)*D cycles: CPE = D/2  CPE matches prediction! What Now?

– 53 – Unrolling & Accumulating Idea Can unroll to any degree L Can accumulate K results in parallel L must be multiple of KLimitations Diminishing returns Cannot go beyond throughput limitations of execution units Large overhead for short lengths Finish off iterations sequentially

– 54 – Unrolling & Accumulating: Intel FP * Case Intel Nocona (Saltwater fish machines) FP Multiplication Theoretical Limit: 2.00 FP *Unrolling Factor L K Accumulators

– 55 – Unrolling & Accumulating: Intel FP + Case Intel Nocona (Saltwater fish machines) FP Addition Theoretical Limit: 2.00 FP +Unrolling Factor L K

– 56 – Unrolling & Accumulating: Intel Int * Case Intel Nocona (Saltwater fish machines) Integer Multiplication Theoretical Limit: 1.00 Int *Unrolling Factor L K

– 57 – Unrolling & Accumulating: Intel Int + Case Intel Nocona (Saltwater fish machines) Integer addition Theoretical Limit: 1.00 (unrolling enough) Int +Unrolling Factor L K

– 58 – FP *: Nocona versus Core 2 Machines Intel Nocona 3.2 GHz Intel Core GHzPerformance Core 2 lower latency & fully pipelined (1 cycle/issue) FP *Unrolling Factor L K FP *Unrolling Factor L K

– 59 – Nocona vs. Core 2 Int * Performance Newer version of GCC does reassociation Why for int’s and not for float’s? Int *Unrolling Factor L K Int *Unrolling Factor L K

– 60 – Intel vs. AMD FP * Machines Intel Nocona 3.2 GHz AMD Opteron 2.0 GHzPerformance AMD lower latency & better pipelining But slower clock rate FP *Unrolling Factor L K FP *Unrolling Factor L K

– 61 – Intel vs. AMD Int * Performance AMD multiplier much lower latency Can get high performance with less work Doesn’t achieve as good an optimum Int *Unrolling Factor L K Int *Unrolling Factor L K

– 62 – Intel vs. AMD Int + Performance AMD gets below 1.0 Even just with unrollingExplanation Both Intel & AMD can “double pump” integer units Only AMD can load two elements / cycle Int +Unrolling Factor L K Int +Unrolling Factor L K

– 63 – Can We Go Faster? Yes, SSE! But not in this class

– 64 – Today Program optimization Optimization blocker: Memory aliasing Out of order processing: Instruction level parallelism Understanding branch prediction

– 65 – What About Branches? Challenge Instruction Control Unit must work well ahead of Execution Unit to generate enough operations to keep EU busy When encounters conditional branch, cannot reliably determine where to continue fetching 80489f3:movl $0x1,%ecx 80489f8:xorl %edx,%edx 80489fa:cmpl %esi,%edx 80489fc:jnl 8048a fe:movl %esi,%esi 8048a00:imull (%eax,%edx,4),%ecx Executing How to continue?

– 66 – Branch Outcomes When encounter conditional branch, cannot determine where to continue fetching Branch Taken: Transfer control to branch target Branch Not-Taken: Continue with next instruction in sequence Cannot resolve until outcome determined by branch/integer unit 80489f3:movl $0x1,%ecx 80489f8:xorl %edx,%edx 80489fa:cmpl %esi,%edx 80489fc:jnl 8048a fe:movl %esi,%esi 8048a00:imull (%eax,%edx,4),%ecx 8048a25:cmpl %edi,%edx 8048a27:jl 8048a a29:movl 0xc(%ebp),%eax 8048a2c:leal 0xffffffe8(%ebp),%esp 8048a2f:movl %ecx,(%eax) Branch Taken Branch Not-Taken

– 67 – Branch Prediction Idea Guess which way branch will go Begin executing instructions at predicted position But don’t actually modify register or memory data 80489f3:movl $0x1,%ecx 80489f8:xorl %edx,%edx 80489fa:cmpl %esi,%edx 80489fc:jnl 8048a a25:cmpl %edi,%edx 8048a27:jl 8048a a29:movl 0xc(%ebp),%eax 8048a2c:leal 0xffffffe8(%ebp),%esp 8048a2f:movl %ecx,(%eax) Predict Taken Begin Execution

– 68 – Branch Prediction Through Loop 80488b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx 80488b7:cmpl %esi,%edx 80488b9:jl 80488b b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx 80488b7:cmpl %esi,%edx 80488b9:jl 80488b b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx 80488b7:cmpl %esi,%edx 80488b9:jl 80488b1 i = 98 i = 99 i = 100 Predict Taken (OK) Predict Taken (Oops) 80488b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx 80488b7:cmpl %esi,%edx 80488b9:jl 80488b1 i = 101 Assume vector length = 100 Read invalid location Executed Fetched

– 69 – Branch Misprediction Invalidation 80488b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx 80488b7:cmpl %esi,%edx 80488b9:jl 80488b b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx 80488b7:cmpl %esi,%edx 80488b9:jl 80488b b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx 80488b7:cmpl %esi,%edx 80488b9:jl 80488b1 i = 98 i = 99 i = 100 Predict Taken (OK) Predict Taken (Oops) 80488b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx i = 101 Invalidate Assume vector length = 100

– 70 – Branch Misprediction Recovery Performance Cost Multiple clock cycles on modern processor Can be a major performance limiter 80488b1:movl (%ecx,%edx,4),%eax 80488b4:addl %eax,(%edi) 80488b6:incl %edx 80488b7:cmpl %esi,%edx 80488b9:jl 80488b bb:leal 0xffffffe8(%ebp),%esp 80488be:popl %ebx 80488bf:popl %esi 80488c0:popl %edi i = 99 Definitely not taken

– 71 – Determining Misprediction Penalty GCC/x86-64 tries to minimize use of Branches Generates conditional moves when possible/sensible int cnt_gt = 0; int cnt_le = 0; int cnt_all = 0; int choose_cmov(int x, int y) { int result; if (x > y) { result = cnt_gt; } else { result = cnt_le; } ++cnt_all; return result; } choose_cmov: cmpl %esi, %edi # x:y movl cnt_le(%rip), %eax # r = cnt_le cmovg cnt_gt(%rip), %eax # if >= r=cnt_gt incl cnt_all(%rip) # cnt_all++ ret # return r

– 72 – Forcing Conditional Cannot use conditional move when either outcome has side effect int cnt_gt = 0; int cnt_le = 0; int choose_cond(int x, int y) { int result; if (x > y) { result = ++cnt_gt; } else { result = ++cnt_le; } return result; } choose_cond: cmpl %esi, %edi jle.L8 movl cnt_gt(%rip), %eax incl %eax movl %eax, cnt_gt(%rip) ret.L8: movl cnt_le(%rip), %eax incl %eax movl %eax, cnt_le(%rip) ret If Then Else

– 73 – Testing Methodology Idea Measure procedure under two different prediction probabilities P = 1.0: Perfect prediction P = 0.5: Random data Test Data x = 0, y =  1 Case +1: y = [+1, +1, +1, …, +1, +1] Case − 1: y = [ − 1, − 1, − 1, …, − 1, − 1] Case A: y = [+1, − 1, +1, …, +1, − 1] (alternate) Case R: y = [+1, − 1, − 1, …, − 1, +1] (random)

– 74 – Testing Outcomes Observations: Conditional move insensitive to data Perfect prediction for regular patterns Else case requires 6 (Nocona), 2 (AMD), or 1 (Core 2) extra cycles Averages to 15.2 Branch penalties: (for R, processor will get it right half of the time) Nocona: 2 * ( ) = 32 cycles AMD: 2 * ( ) = 13 cycles Core 2: 2 * ( ) = 18 cycles Casecmovcond − A R Casecmovcond − A R Intel NoconaAMD Opteron Casecmovcond − A R Intel Core 2

– 75 – Getting High Performance So Far Good compiler and flags Don’t do anything stupid Watch out for hidden algorithmic inefficiencies Write compiler-friendly code Watch out for optimization blockers: procedure calls & memory references Careful with implemented abstract data types Look carefully at innermost loops (where most work is done) Tune code for machine Exploit instruction-level parallelism Avoid unpredictable branches Make code cache friendly (Covered later in course)