DOC PREVIEW
Princeton COS 217 - Program Optimization

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Program OptimizationGoals of Today’s ClassImproving Program PerformanceWays to Optimize PerformanceHelping the Compiler Do Its JobOptimizing CompilersLimitations of Optimizing CompilersAvoiding Repeated ComputationWorrying About Side EffectsAnother Example on Side EffectsMemory AliasingSlide 12Another Aliasing ExampleSummary: Helping the CompilerExploiting the HardwareUnderlying HardwareAddition Faster Than MultiplicationBit Operations Faster Than ArithmeticCaching: Matrix MultiplicationMatrix Multiply: Cache EffectsSlide 21Slide 22Parallelism: Loop UnrollingParallelism: After Loop UnrollingProgram ExecutionAvoiding Function CallsWriting Your Own Malloc and FreeConclusionCourse Wrap UpThe Rest of the SemesterGoals of COS 217Relationship to Other CoursesLessons About Computer ScienceLessons ContinuedHave a Great Summer!!!1Program OptimizationProfessor Jennifer Rexfordhttp://www.cs.princeton.edu/~jrex2Goals of Today’s Class•Improving program performanceWhen and what to optimizeBetter algorithms & data structures vs. tuning the code•Exploiting an understanding of underlying systemCompiler capabilitiesHardware architectureProgram execution•Why?To be effective, and efficient, at making programs faster–Avoid optimizing the fast parts of the code–Help the compiler do its job betterTo review material from the second half of the course3Improving Program Performance•Most programs are already “fast enough”No need to optimize performance at allSave your time, and keep the program simple/readable•Most parts of a program are already “fast enough”Usually only a small part makes the program run slowlyOptimize only this portion of the program, as needed•Steps to improve execution (time) efficiencyDo timing studies (e.g., gprof)Identify hot spotsOptimize that part of the programRepeat as needed4Ways to Optimize Performance•Better data structures and algorithmsImproves the “asymptotic complexity”–Better scaling of computation/storage as input grows–E.g., going from O(n2) sorting algorithm to O(n log n)Clearly important if large inputs are expectedRequires understanding data structures and algorithms•Better source code the compiler can optimizeImproves the “constant factors”–Faster computation during each iteration of a loop–E.g., going from 1000n to 10n running timeClearly important if a portion of code is running slowlyRequires understanding hardware, compiler, execution5Helping the Compiler Do Its Job6Optimizing Compilers•Provide efficient mapping of program to machineRegister allocationCode selection and orderingEliminating minor inefficiencies•Don’t (usually) improve asymptotic efficiencyUp to the programmer to select best overall algorithm•Have difficulty overcoming “optimization blockers”Potential function side-effectsPotential memory aliasing7Limitations of Optimizing Compilers•Fundamental constraintCompiler must not change program behaviorEver, even under rare pathological inputs•Behavior that may be obvious to the programmer can be obfuscated by languages and coding stylesData ranges more limited than variable types suggestArray elements remain unchanged by function calls•Most analysis is performed only within functionsWhole-program analysis is too expensive in most cases•Most analysis is based only on static informationCompiler has difficulty anticipating run-time inputs8Avoiding Repeated Computation•A good compiler recognizes simple optimizationsAvoiding redundant computations in simple loopsStill, programmer may still want to make it explicit•ExampleRepetition of computation: n * ifor (i = 0; i < n; i++) for (j = 0; j < n; j++) a[n*i + j] = b[j];for (i = 0; i < n; i++) { int ni = n * i; for (j = 0; j < n; j++) a[ni + j] = b[j];}9Worrying About Side Effects•Compiler cannot always avoid repeated computationMay not know if the code has a “side effect”… that makes the transformation change the code’s behavior•Is this transformation okay?•Not necessarily, ifint func1(int x) { return f(x) + f(x) + f(x) + f(x);}int func1(int x) { return 4 * f(x);}int counter = 0;int f(int x) { return counter++;}And this function may be defined in another file known only at link time!10Another Example on Side Effects•Is this optimization okay?•Short answer: it dependsCompiler often cannot tellMost compilers do not try to identify side effects•Programmer knows bestAnd can decide whether the optimization is safefor (i = 0; i < strlen(s); i++) { /* Do something with s[i] */}length = strlen(s);for (i = 0; i < length; i++) { /* Do something with s[i] */}11Memory Aliasing•Is this optimization okay?•Not necessarily, what if xp and yp are equal?First version: result is 4 times *xpSecond version: result is 3 times *xpvoid twiddle(int *xp, int *yp) { *xp += *yp; *xp += *yp;}void twiddle(int *xp, int *yp) { *xp += 2 * *yp;}12Memory Aliasing•Memory aliasingSingle data location accessed through multiple namesE.g., two pointers that point to the same memory location•Modifying the data using one nameImplicitly modifies the values seen through other names•Blocks optimization by the compilerThe compiler cannot tell when aliasing may occur… and so must forgo optimizing the code•Programmer often does know And can optimize the code accordinglyxp, yp13Another Aliasing Example•Is this optimization okay?•Not necessarilyIf y and x point to the same location in memory…… the correct output is “x = 10\n”int *x, *y;…*x = 5;*y = 10;printf(“x=%d\n”, *x);printf(“x=5\n”);14Summary: Helping the Compiler•Compiler can perform many optimizationsRegister allocationCode selection and orderingEliminating minor inefficiencies•But often the compiler needs your helpKnowing if code is free of side effectsKnowing if memory aliasing will not happen•Modifying the code can lead to better performanceProfile the code to identify the “hot spots”Look at the assembly language the compiler producesRewrite the code to get the compiler to do the right thing15Exploiting the Hardware16Underlying Hardware•Implements a collection of instructionsInstruction set varies from one architecture to anotherSome instructions may be faster than others•Registers and caches are faster than main memoryNumber of registers and sizes of


View Full Document

Princeton COS 217 - Program Optimization

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Program Optimization
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Program Optimization and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Program Optimization 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?