program op miza on 15 213 introduc on to computer systems
play

Program Op*miza*on 15-213: Introduc;on to Computer Systems - PowerPoint PPT Presentation

Carnegie Mellon Program Op*miza*on 15-213: Introduc;on to Computer Systems 10 th Lecture, Oct. 1, 2015 Instructors: Randal E. Bryant and David R. OHallaron


  1. Carnegie Mellon Program ¡Op*miza*on ¡ ¡ 15-­‑213: ¡Introduc;on ¡to ¡Computer ¡Systems ¡ 10 th ¡Lecture, ¡Oct. ¡1, ¡2015 ¡ Instructors: ¡ ¡ Randal ¡E. ¡Bryant ¡and ¡David ¡R. ¡O’Hallaron ¡ 1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  2. Carnegie Mellon Today ¡ ¢ Overview ¡ ¢ Generally ¡Useful ¡Op*miza*ons ¡ § Code ¡mo;on/precomputa;on ¡ § Strength ¡reduc;on ¡ § Sharing ¡of ¡common ¡subexpressions ¡ § Removing ¡unnecessary ¡procedure ¡calls ¡ ¢ Op*miza*on ¡Blockers ¡ § Procedure ¡calls ¡ § Memory ¡aliasing ¡ ¢ Exploi*ng ¡Instruc*on-­‑Level ¡Parallelism ¡ ¢ Dealing ¡with ¡Condi*onals ¡ 2 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  3. Carnegie Mellon Performance ¡Reali*es ¡ ¢ There’s ¡more ¡to ¡performance ¡than ¡asympto1c ¡complexity ¡ ¢ Constant ¡factors ¡maHer ¡too! ¡ § Easily ¡see ¡10:1 ¡performance ¡range ¡depending ¡on ¡how ¡code ¡is ¡wriRen ¡ § Must ¡op;mize ¡at ¡mul;ple ¡levels: ¡ ¡ § algorithm, ¡data ¡representa;ons, ¡procedures, ¡and ¡loops ¡ ¢ Must ¡understand ¡system ¡to ¡op*mize ¡performance ¡ § How ¡programs ¡are ¡compiled ¡and ¡executed ¡ § How ¡modern ¡processors ¡+ ¡memory ¡systems ¡operate ¡ § How ¡to ¡measure ¡program ¡performance ¡and ¡iden;fy ¡boRlenecks ¡ § How ¡to ¡improve ¡performance ¡without ¡destroying ¡code ¡modularity ¡and ¡ generality ¡ 3 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  4. Carnegie Mellon Op*mizing ¡Compilers ¡ ¢ Provide ¡efficient ¡mapping ¡of ¡program ¡to ¡machine ¡ § register ¡alloca;on ¡ § code ¡selec;on ¡and ¡ordering ¡(scheduling) ¡ § dead ¡code ¡elimina;on ¡ § elimina;ng ¡minor ¡inefficiencies ¡ ¢ Don’t ¡(usually) ¡improve ¡asympto*c ¡efficiency ¡ § up ¡to ¡programmer ¡to ¡select ¡best ¡overall ¡algorithm ¡ § big-­‑O ¡savings ¡are ¡(oYen) ¡more ¡important ¡than ¡constant ¡factors ¡ § but ¡constant ¡factors ¡also ¡maRer ¡ ¢ Have ¡difficulty ¡overcoming ¡“op*miza*on ¡blockers” ¡ § poten;al ¡memory ¡aliasing ¡ § poten;al ¡procedure ¡side-­‑effects ¡ 4 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  5. Carnegie Mellon Limita*ons ¡of ¡Op*mizing ¡Compilers ¡ ¢ Operate ¡under ¡fundamental ¡constraint ¡ § Must ¡not ¡cause ¡any ¡change ¡in ¡program ¡behavior ¡ § Except, ¡possibly ¡when ¡program ¡making ¡use ¡of ¡nonstandard ¡language ¡ features ¡ § OYen ¡prevents ¡it ¡from ¡making ¡op;miza;ons ¡that ¡would ¡only ¡affect ¡behavior ¡ under ¡pathological ¡condi;ons. ¡ ¢ 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 ¡ § Newer ¡versions ¡of ¡GCC ¡do ¡interprocedural ¡analysis ¡within ¡individual ¡files ¡ § But, ¡not ¡between ¡code ¡in ¡different ¡files ¡ ¢ Most ¡analysis ¡is ¡based ¡only ¡on ¡ sta1c ¡informa*on ¡ § Compiler ¡has ¡difficulty ¡an;cipa;ng ¡run-­‑;me ¡inputs ¡ ¢ When ¡in ¡doubt, ¡the ¡compiler ¡must ¡be ¡conserva*ve ¡ 5 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  6. Carnegie Mellon Generally ¡Useful ¡Op*miza*ons ¡ ¢ Op*miza*ons ¡that ¡you ¡or ¡the ¡compiler ¡should ¡do ¡regardless ¡ of ¡processor ¡/ ¡compiler ¡ ¢ Code ¡Mo*on ¡ § Reduce ¡frequency ¡with ¡which ¡computa;on ¡performed ¡ § If ¡it ¡will ¡always ¡produce ¡same ¡result ¡ § Especially ¡moving ¡code ¡out ¡of ¡loop ¡ void set_row(double *a, double *b, long i, long n) { long j; long j; int ni = n*i ; for (j = 0; j < n; j++) for (j = 0; j < n; j++) a[n*i+j] = b[j]; a[ni+j] = b[j]; } 6 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  7. Carnegie Mellon Compiler-­‑Generated ¡Code ¡Mo*on ¡(-­‑O1) ¡ void set_row(double *a, double *b, long j; long i, long n) long ni = n*i; { double *rowp = a+ni; long j; for (j = 0; j < n; j++) for (j = 0; j < n; j++) *rowp++ = b[j]; a[n*i+j] = b[j]; } set_row: testq %rcx, %rcx # Test n jle .L1 # If 0, goto done imulq %rcx, %rdx # ni = n*i leaq (%rdi,%rdx,8), %rdx # rowp = A + ni*8 movl $0, %eax # j = 0 .L3: # loop: movsd (%rsi,%rax,8), %xmm0 # t = b[j] movsd %xmm0, (%rdx,%rax,8) # M[A+ni*8 + j*8] = t addq $1, %rax # j++ cmpq %rcx, %rax # j:n jne .L3 # if !=, goto loop .L1: # done: rep ; ret 7 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  8. Carnegie Mellon Reduc*on ¡in ¡Strength ¡ § Replace ¡costly ¡opera;on ¡with ¡simpler ¡one ¡ § ShiY, ¡add ¡instead ¡of ¡mul;ply ¡or ¡divide ¡ 16*x --> x << 4 § U;lity ¡machine ¡dependent ¡ § Depends ¡on ¡cost ¡of ¡mul;ply ¡or ¡divide ¡instruc;on ¡ – On ¡Intel ¡Nehalem, ¡integer ¡mul;ply ¡requires ¡3 ¡CPU ¡cycles ¡ § Recognize ¡sequence ¡of ¡products ¡ int ni = 0; for (i = 0; i < n; i++) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) int ni = n*i; a[ni + j] = b[j]; for (j = 0; j < n; j++) ni += n; a[ni + j] = b[j]; } } 8 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  9. Carnegie Mellon Share ¡Common ¡Subexpressions ¡ § Reuse ¡por;ons ¡of ¡expressions ¡ § GCC ¡will ¡do ¡this ¡with ¡–O1 ¡ /* Sum neighbors of i,j */ long inj = i*n + j; up = val[(i-1)*n + j ]; up = val[inj - n]; down = val[(i+1)*n + j ]; down = val[inj + n]; left = val[i*n + j-1]; left = val[inj - 1]; right = val[i*n + j+1]; right = val[inj + 1]; sum = up + down + left + right; sum = up + down + left + right; 3 ¡mul*plica*ons: ¡i*n, ¡(i–1)*n, ¡(i+1)*n ¡ 1 ¡mul*plica*on: ¡i*n ¡ leaq 1(%rsi), %rax # i+1 imulq %rcx, %rsi # i*n leaq -1(%rsi), %r8 # i-1 addq %rdx, %rsi # i*n+j imulq %rcx, %rsi # i*n movq %rsi, %rax # i*n+j imulq %rcx, %rax # (i+1)*n subq %rcx, %rax # i*n+j-n imulq %rcx, %r8 # (i-1)*n leaq (%rsi,%rcx), %rcx # i*n+j+n addq %rdx, %rsi # i*n+j addq %rdx, %rax # (i+1)*n+j addq %rdx, %r8 # (i-1)*n+j 9 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  10. Carnegie Mellon Op*miza*on ¡Blocker ¡#1: ¡Procedure ¡Calls ¡ ¢ Procedure ¡to ¡Convert ¡String ¡to ¡Lower ¡Case ¡ void lower(char *s) { size_t i; for (i = 0; i < strlen(s); i++) if (s[i] >= 'A' && s[i] <= 'Z') s[i] -= ('A' - 'a'); } § Extracted ¡from ¡213 ¡lab ¡submissions, ¡Fall, ¡1998 ¡ 10 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  11. Carnegie Mellon Lower ¡Case ¡Conversion ¡Performance ¡ § Time ¡quadruples ¡when ¡double ¡string ¡length ¡ § Quadra;c ¡performance ¡ 250 200 CPU seconds 150 lower1 100 50 0 0 50000 100000 150000 200000 250000 300000 350000 400000 450000 500000 String length 11 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  12. Carnegie Mellon Convert ¡Loop ¡To ¡Goto ¡Form ¡ void lower(char *s) { size_t i = 0; if (i >= strlen(s)) goto done; loop: if (s[i] >= 'A' && s[i] <= 'Z') s[i] -= ('A' - 'a'); i++; if (i < strlen(s)) goto loop; done: } § ¡ strlen ¡executed ¡every ¡itera;on ¡ 12 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend