Optimization.

Slides:



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

OPTIMIZING C CODE FOR THE ARM PROCESSOR Optimizing code takes time and reduces source code readability Usually done for functions that are critical for.
Compiler Support for Superscalar Processors. Loop Unrolling Assumption: Standard five stage pipeline Empty cycles between instructions before the result.
CPE 731 Advanced Computer Architecture Instruction Level Parallelism Part I Dr. Gheith Abandah Adapted from the slides of Prof. David Patterson, University.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
1 4/20/06 Exploiting Instruction-Level Parallelism with Software Approaches Original by Prof. David A. Patterson.
Computer Architecture Instruction Level Parallelism Dr. Esam Al-Qaralleh.
1 Advanced Computer Architecture Limits to ILP Lecture 3.
1 Lecture 10: Static ILP Basics Topics: loop unrolling, static branch prediction, VLIW (Sections 4.1 – 4.4)
Overview Motivations Basic static and dynamic optimization methods ADAPT Dynamo.
Software Methods to Increase Data Cache Performance Presented by Philip Marshall.
INTEL CONFIDENTIAL Improving Parallel Performance Introduction to Parallel Programming – Part 11.
Compiler Challenges for High Performance Architectures
Friday, September 15, 2006 The three most important factors in selling optimization are location, location, location. - Realtor’s creed.
Cosc 5/4755 Algorithms Size and efficiency. Why? On modern desktops and laptops, – Memory size gets larger and larger 8 GBs is now common and 16GBs considered.
7/2/ _23 1 Pipelining ECE-445 Computer Organization Dr. Ron Hayne Electrical and Computer Engineering.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Code Optimization. 2 Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.2 ~ 5.6.
Performance Optimization Getting your programs to run faster CS 691.
Super computers Parallel Processing By Lecturer: Aisha Dawood.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Dependence Analysis and Loop Transformations.
CIS 662 – Computer Architecture – Fall Class 16 – 11/09/04 1 Compiler Techniques for ILP  So far we have explored dynamic hardware techniques for.
Carnegie Mellon Lecture 15 Loop Transformations Chapter Dror E. MaydanCS243: Loop Optimization and Array Analysis1.
University of Amsterdam Computer Systems – optimizing program performance Arnoud Visser 1 Computer Systems Optimizing program performance.
Performance Optimization Getting your programs to run faster.
Optimised C/C++. Overview of DS General code Functions Mathematics.
Loop Optimizations Scheduling. Loop fusion int acc = 0; for (int i = 0; i < n; ++i) { acc += a[i]; a[i] = acc; } for (int i = 0; i < n; ++i) { b[i] +=
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
Optimization of C Code The C for Speed
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
1 Code Optimization. 2 Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.2 ~ 5.6.
Optimization. How to Optimize Code Conventional Wisdom: 1.Don't do it 2.(For experts only) Don't do it yet.
Use of Pipelining to Achieve CPI < 1
Compiler Techniques for ILP
CS 352H: Computer Systems Architecture
Code Optimization Overview and Examples
Code Optimization.
Introduction To Computer Systems
Computer Architecture Principles Dr. Mike Frank
Simone Campanoni Loop transformations Simone Campanoni
The University of Adelaide, School of Computer Science
Swapping Segmented paging allows us to have non-contiguous allocations
5.2 Eleven Advanced Optimizations of Cache Performance
Code Optimization I: Machine Independent Optimizations
Instruction Scheduling for Instruction-Level Parallelism
CSCI1600: Embedded and Real Time Software
Optimizing Transformations Hal Perkins Autumn 2011
STUDY AND IMPLEMENTATION
Register Pressure Guided Unroll-and-Jam
Compiler techniques for exposing ILP (cont)
Optimizing Transformations Hal Perkins Winter 2008
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Sampoorani, Sivakumar and Joshua
Instruction Level Parallelism (ILP)
November 5 No exam results today. 9 Classes to go!
Midterm 2 review Chapter
Multithreading Why & How.
CSC3050 – Computer Architecture
How to improve (decrease) CPI
Wackiness Algorithm A: Algorithm B:
Loop-Level Parallelism
Lecture 11: Machine-Dependent Optimization
Instruction Scheduling Hal Perkins Autumn 2011
Introduction to Optimization
CSCI1600: Embedded and Real Time Software
Rohan Yadav and Charles Yuan (rohany) (chenhuiy)
Presentation transcript:

Optimization

How to Optimize Code Conventional Wisdom: Don't do it (For experts only) Don't do it yet

Why Not??? You can't optimize something that doesn't work Biggest improvement is picking the right algorithm 90 / 10 rule 90% of the time is spent in 10% of the code Need to be able to verify speedup Optimized code is usually: Less readable/maintainable More complex/brittle

Code Tricks Prefer ints to floats Minimize float->int conversions Use multiplication instead of division

Strength Reductions Avoid costlier operations (divide/multiply) by using cheaper ones (add/subtract/shift) int total = 0; for (i = 0; i < 1000; i++) total += i * 3; int total = 0; for (i = 0; i < 3000; i+=3) total += i;

Fancy Tricks Make if the common case: If path usually gets unconditional branch Static branch prediction usually skips forward branches Else relies on conditional branch

Fancy Tricks Loop unrolling : Reducing iterations by increasing work done per step Less Overhead Gives compiler/processor more to rearrange for (i = 1; i <= 30; i++) a[i] = a[i] + b[i] * c; for (i = 1; i <= 30; i+=3) { a[i] = a[i] + b[i] * c; a[i+1] = a[i+1] + b[i+1] * c; a[i+2] = a[i+2] + b[i+2] * c; }

Fancy Tricks Loop fusion: Combine loops doing unrelated jobs to avoid overhead Will they fight over cache??? for (i = 0; i < N; i++) C[i] = A[i] + B[i]; D[i] = E[i] + C[i]; for (i = 0; i < N; i++) C[i] = A[i] + B[i]; D[i] = E[i] + C[i]; }

Fancy Tricks Loop peeling: Break up loops to avoid branches within them for (i = 1; i <= N; i++) { if (i==1) A[i] = 0; else if (i == N) A[i] = N; else A[i] = A[i] + 8; } A[1] = 0; for (i = 2; i < N; i++) A[i] = A[i] + 8; A[N] = N;

How To Optimize - Branching Pipelined processors HATE branch miss- predictions 15+ cycle penalty

How To Optimize - Branching May be able to rearrange data for consistent branching Some compilers can use runtime profiles to reorder code Come compilers allow "expect" – hints about how to order instructions

How To Optimize - Branching Maybe better off without it:

How To Optimize - Branching Maybe better off without it:

How To Optimize - Cache Cache: Modern CPU's live/die by cache Like contiguous data Avoid repeated swapping between multiple items https://www.youtube.com/watch?v=rX0ItVEVjHc&start=1555 25:00-35:00 and beyond

How to Optimize Right Rules Start with clean, working code and good tests Test any optimizations If it doesn't help, take it out

Compiler Optimization Compilers can Eliminate unneeded code Do strength reductions Unroll loops https://godbolt.org/g/z3OUvL http://goo.gl/rvJ7rq Compiler likes specific information Use constants Make variables as local as possible

Samples O0 : Faithful (but efficient) line by line translation O1 : Try to do work in registers instead of stack frame Remove obviously worthless code O2 : Aggressively do work ahead of time

Samples C++ Code: