Docsity

Profiling Haskell Programs: Understanding Behavior and Optimizing Performance, Papers of Computer Science

Partial preview of the text

Download Profiling Haskell Programs: Understanding Behavior and Optimizing Performance and more Papers Computer Science in PDF only on Docsity!

CS 457/557: Functional

Languages

Profiling in Haskell

Mark P Jones

Portland State University

What makes a good program?

! " Qualitative factors:

!" Correctness

!" Maintainability, readability,

understandability, portability, flexibility, …

!" Use of appropriate abstractions and idioms

! " Quantitative factors:

!" Performance, Predictability, …

!" Time, Memory, Disk, Bandwidth, …

Profiling Tools:

! " Two broad approaches:

!" Instrumentation

!" Sampling

! " Standard Advice:

!" Focus on writing qualitatively good code first

!" Once that’s working, use profiling tools to

identify performance hot-spots and obtain

quantitatively good code

Case Study: Profiling PPM

Making Circles in Hugs:

Main> main ^C{Interrupted!} Main> :set +s +g Main> main {{Gc:913203}}{{Gc:913215}}{{Gc:913203}}{{Gc: 913206}}{{Gc:913219}}{{Gc:913209}}{{Gc: 913206}}{{Gc:913207}}{{Gc: 913207}}^C{Interrupted!} (6164225 reductions, 8422432 cells, 9 garbage collections) {{Gc:913207}}Main>

Making Circles with GHC:

prompt$ ./Main +RTS –sstderr 676,614,088 bytes allocated in the heap 202,664 bytes copied during GC (scavenged) 114,632 bytes copied during GC (not scavenged) 548,864 bytes maximum residency (1 sample(s)) … MUT time 1.19s ( 1.21s elapsed) GC time 0.01s ( 0.01s elapsed) Total time 1.20s ( 1.22s elapsed) … Productivity 99.2% of total user, 97.5% of total elapsed prompt$

Preparing to Profile:

prompt$ ghc --make Main -prof -auto-all -fforce-recomp -o Main prompt$ ./Main +RTS -sstderr -p 4,688,711,540 bytes allocated in the heap 2,201,188 bytes copied during GC (scavenged) 1,235,956 bytes copied during GC (not scavenged) 3,153,920 bytes maximum residency (2 sample(s)) MUT time 9.10s ( 9.23s elapsed) GC time 0.06s ( 0.08s elapsed) Total time 9.16s ( 9.31s elapsed) prompt$

Profiling has overheads …

Inside Main.prof:

total time = 1.06 secs (53 ticks @ 20 ms) total alloc = 2,705,945,792 bytes (excludes profiling overheads) COST CENTRE MODULE %time %alloc quant8 PPM6 47.2 62. lift PPM6 35.8 29. cmap Colour 3.8 1. new PPM6 3.8 0. go1 DemoPPM 3.8 0. clip Colour 1.9 0. close DemoPPM 1.9 1. circle1 DemoPPM 1.9 2. iterDouble PPM6 0.0 3.

quant8 and lift:

quant8 :: RealFrac n => n -> Word quant8 x = floor $ x * 0xFF lift :: IOUArray Int Word8 -> (Double -> Double -> Colour) -> Double -> Double -> Int -> IO Int lift arr f x y next = case cclip (f x y) of (Colour r g b) -> do writeArray arr next (quant8 r) writeArray arr (next+1) (quant8 g) writeArray arr (next+2) (quant8 b) return (next +3)

We run quant8 3 times for every pixel on the grid!

Inside the Colour library:

module Colour where data Colour = Colour {redPart, greenPart, bluePart :: Double} deriving (Eq, Show) cmap :: (Double -> Double) -> Colour -> Colour cmap f (Colour r g b) = Colour (f r) (f g) (f b) … clip :: (Num n, Ord n) => n -> n clip n = max 0 (min 1 x) cclip :: Colour -> Colour cclip = cmap clip black = Colour 0 0 0 blue = Colour 0 0 1 green = Colour 0 0.5 0 … 14

Update the definition of lift:

lift :: IOUArray Int Word8 -> (Double -> Double -> Color Word8 ) -> Double -> Double -> Int -> IO Int lift arr f x y next = case cclip (f x y) of (Color r g b) -> do writeArray arr next r writeArray arr (next+1) g writeArray arr (next+2) b return (next +3)

Eliminates calls to quant8 …

Adjust definition of circle1:

circle1 epsilon size x y = if close epsilon (xx + yy) size then colquant red else colquant yellow colquant :: Color Double -> Color Word colquant (Color r g b) = Color (quant8 r) (quant8 g) (quant8 b)

… which get moved here instead

… continued:

individual inherited

  • MAIN MAIN 1 0 0.0 0.0 100.0 100. COST CENTRE MODULE no. entries %time %alloc %time %alloc
    • CAF Main 222 2 0.0 0.0 0.0 0.
      • main Main 228 1 0.0 0.0 0.0 0.
      • go1 DemoPPM 247 0 0.0 0.0 0.0 0.
        • mapDouble PPM6 248 0 0.0 0.0 0.0 0.
    • CAF DemoPPM 149 2 0.0 0.0 100.0 100.
      • go1 DemoPPM 229 1 3.8 0.0 100.0 100.
        • circle1 DemoPPM 239 786432 1.9 2.4 3.8 3.
          • close DemoPPM 240 786432 1.9 1.4 1.9 1.
        • mapDouble PPM6 230 1 0.0 0.0 92.5 96.
          • lift PPM6 236 786432 35.8 29.3 88.7 92.
            • quant8 PPM6 244 0 47.2 62.1 47.2 62.
            • cclip Colour 237 786432 0.0 0.0 5.7 1.
              • cmap Colour 238 786432 3.8 1.5 5.7 1.
                • clip Colour 245 0 1.9 0.0 1.9 0.
          • iterDouble PPM6 235 1 0.0 3.1 0.0 3.
          • new PPM6 231 1 3.8 0.1 3.8 0.
  • MAIN MAIN 1 0 0.0 0.0 100.0 100. COST CENTRE MODULE no. entries %time %alloc %time %alloc
    • CAF Main 222 2 0.0 0.0 0.0 0.
      • main Main 228 1 0.0 0.0 0.0 0.
      • go1 DemoPPM 250 0 0.0 0.0 0.0 0.
        • mapDouble PPM6 251 0 0.0 0.0 0.0 0.
    • CAF DemoPPM 149 1 0.0 0.0 100.0 100.
      • go1 DemoPPM 229 1 0.0 0.0 100.0 100.
        • circle1 DemoPPM 239 786432 3.2 2.2 51.6 66.
          • colquant PPM6 241 786432 6.5 1.5 48.4 63.
            • quant8 PPM6 245 0 41.9 61.8 41.9 61.
          • close DemoPPM 240 786432 0.0 1.4 0.0 1.
        • mapDouble PPM6 230 1 0.0 0.0 48.4 33.
          • lift PPM6 236 786432 38.7 28.1 38.7 29.
            • cclip Colour 246 0 0.0 0.0 0.0 1.
              • cmap Colour 247 786431 0.0 1.8 0.0 1.
          • iterDouble PPM6 235 1 3.2 3.1 3.2 3.
          • new PPM6 231 1 6.5 0.1 6.5 0.

Re-adjust definition of circle1:

circle1 epsilon size x y = if close epsilon (xx + yy) size then qred else qyellow qred = colquant red -- CAFs qyellow = colquant yellow -- (Constant Applicative Forms) colquant :: Color Double -> Color Word colquant (Color r g b) = Color (quant8 r) (quant8 g) (quant8 b)

Make quantized red and yellow once only …