{"items":["5f764fd6ac4f570017a157db","5f764fd6ac4f570017a157dc","5f764fd6ac4f570017a157dd"],"styles":{"galleryType":"Columns","groupSize":1,"showArrows":true,"cubeImages":true,"cubeType":"max","cubeRatio":1.7777777777777777,"isVertical":true,"gallerySize":30,"collageAmount":0,"collageDensity":0,"groupTypes":"1","oneRow":false,"imageMargin":25,"galleryMargin":0,"scatter":0,"chooseBestGroup":true,"smartCrop":false,"hasThumbnails":false,"enableScroll":true,"isGrid":true,"isSlider":false,"isColumns":false,"isSlideshow":false,"cropOnlyFill":false,"fixedColumns":0,"enableInfiniteScroll":true,"isRTL":false,"minItemSize":50,"rotatingGroupTypes":"","rotatingCubeRatio":"","gallerySliderImageRatio":1.7777777777777777,"numberOfImagesPerRow":3,"numberOfImagesPerCol":1,"groupsPerStrip":0,"borderRadius":0,"boxShadow":0,"gridStyle":0,"mobilePanorama":false,"placeGroupsLtr":false,"viewMode":"preview","thumbnailSpacings":4,"galleryThumbnailsAlignment":"bottom","isMasonry":false,"isAutoSlideshow":false,"slideshowLoop":false,"autoSlideshowInterval":4,"bottomInfoHeight":0,"titlePlacement":["SHOW_ON_THE_RIGHT","SHOW_BELOW"],"galleryTextAlign":"center","scrollSnap":false,"itemClick":"nothing","fullscreen":true,"videoPlay":"hover","scrollAnimation":"NO_EFFECT","slideAnimation":"SCROLL","scrollDirection":0,"overlayAnimation":"FADE_IN","arrowsPosition":0,"arrowsSize":23,"watermarkOpacity":40,"watermarkSize":40,"useWatermark":true,"watermarkDock":{"top":"auto","left":"auto","right":0,"bottom":0,"transform":"translate3d(0,0,0)"},"loadMoreAmount":"all","defaultShowInfoExpand":1,"allowLinkExpand":true,"expandInfoPosition":0,"allowFullscreenExpand":true,"fullscreenLoop":false,"galleryAlignExpand":"left","addToCartBorderWidth":1,"addToCartButtonText":"","slideshowInfoSize":200,"playButtonForAutoSlideShow":false,"allowSlideshowCounter":false,"hoveringBehaviour":"NEVER_SHOW","thumbnailSize":120,"magicLayoutSeed":1,"imageHoverAnimation":"NO_EFFECT","imagePlacementAnimation":"NO_EFFECT","calculateTextBoxWidthMode":"PERCENT","textBoxHeight":60,"textBoxWidth":200,"textBoxWidthPercent":75,"textImageSpace":10,"textBoxBorderRadius":0,"textBoxBorderWidth":0,"loadMoreButtonText":"","loadMoreButtonBorderWidth":1,"loadMoreButtonBorderRadius":0,"imageInfoType":"ATTACHED_BACKGROUND","itemBorderWidth":0,"itemBorderRadius":0,"itemEnableShadow":false,"itemShadowBlur":20,"itemShadowDirection":135,"itemShadowSize":10,"imageLoadingMode":"BLUR","expandAnimation":"NO_EFFECT","imageQuality":90,"usmToggle":false,"usm_a":0,"usm_r":0,"usm_t":0,"videoSound":false,"videoSpeed":"1","videoLoop":true,"gallerySizeType":"px","gallerySizePx":1000,"allowTitle":true,"allowContextMenu":true,"textsHorizontalPadding":-30,"itemBorderColor":{"themeName":"color_12","value":"rgba(191,191,191,0)"},"showVideoPlayButton":true,"galleryLayout":2,"calculateTextBoxHeightMode":"MANUAL","targetItemSize":1000,"selectedLayout":"2|bottom|1|max|true|0|true","layoutsVersion":2,"selectedLayoutV2":2,"isSlideshowFont":true,"externalInfoHeight":60,"externalInfoWidth":0.75},"container":{"width":300,"galleryWidth":325,"galleryHeight":0,"scrollBase":0,"height":null}}

# Dude Where's My FLOPS ?

Computing the Mandelbrot fractal is seemingly a perfect application for the GPU. It is simple to state: iterate z = z^2 + c, starting with z=0 for a set of values c drawn from the complex plane (wiki). The number of iterations it takes for |z| to exceed 4 is recorded and used to color a pixel in the Mandelbrot image. The image on the left is an example Mandelbrot image where each pixel corresponds to a different choice of c.

Since the iteration for each value of c is independent of all the other iterations required for the other values of c this is an example of an embarrassingly parallel task. Furthermore the only time that data needs to be written to memory is when the iteration count is recorded. Thus this is an example of a compute bound problem. Assuming that we cap the maximum number of iterations at 256, and each iteration takes O(10) floating point operations the flops/byte ratio is very high with 2560 flops/4 bytes stored.

Example CUDA device function implementation for the Mandelbrot iteration is shown above where we have tried to goose the number of fused multiply add floating point operations. We also used a probably pointless trick by passing c/2 .

Profiling the code with nvprof on a P100 PCI-E 12GB model with

nvprof --metrics flop_dp_efficiency ./cudaMandelbrot 4096 4096

Yields the (maybe) surprising result: ~52% FP64 efficiency ! This "ideal" compute bound and embarrassingly parallel application is missing the peak FP64 performance by approximately 2TFLOPS/s. Why ?

We can do a little more detective work by scrutinizing all the available metrics that nvprof offers (using nvprof --metrics all ...).

One class of statistics stand out: flop_count_dp (total # of FP64 operations), flop_count_dp_add (# of FP64 additions), flop_count_mul (# of FP64 multiplications), and flop_count_dp_fma (# of fused FP64 multiply-adds). In the Mandelbrot calculation approximately half of the operations are adds, half are fmads, with a few multiplies.

Unfortunately, to get peak performance all the operations must be fmads. Thus in this case we are a priori limited to 75% of peak performance.

We can even see what instructions to the nvcc CUDA compiler generates when compiling the above device function by using the cuobjdump.

cuobjdump --dump-sass cudaMandelbrot

This command outputs the assembly code generated for the kernels (and device functions) of the compiled CUDA code with the relevant snippet shown below.

The while loop starts at line (address) 0138. By using --use_fast_math or --fmad=true nvcc command line option we coaxed the compiler into using three fmad operations (DFMA) but that there are also three add operations (DADD). Almost all the flops are generated in these few lines of code. This explains at least 1 TFLOPS/s of the flop deficit. It is not immediately obvious where the other one was lost. Stay tuned.