# Software optimisation

* Profiling code
* benchmarking

# How to ask about optimisation ?

* "If you've actually profiled the code, have specific snippets so that everyone can run the same code to see its performance, and you have this library published somewhere where it can be seen (GitHub, Bitbucket, etc.), then I don't necessarily see a problem with it going on Code Review. Including the results from your profiler of choice with identified bottlenecks would go a long way towards keeping it on-topic.
* If you're just starting the code but have profiled a small amount of code that exhibits the aberrant performance, then asking it on Stack Overflow would be acceptable. Including the results from your profiler of choice with identified bottlenecks would go a long way towards keeping it on-topic.
* If you're asking for people to just help you optimize the code, don't ask it anywhere. Getting a code dump and being asked to find the performance bottleneck is only something I do professionally, not on my free time." (Makoto)
See also : code review

## Inner loop

* inner loop in wikipedia

## Optimisation of numerical computations
### number type

* double is faster than float
* "In my tests (with perturbation) in Fraktaler-3, x87 long double is about 4.2x slower than double on AMD 2700X CPU. floatexp (float + int32) on AMD RX 580 GPU is about 2.3x faster than x87 long double on AMD 2700X CPU, so depending on hardware there might not be any point to x87 long double..."fractalforums.org: from-java-to-cplusplus

### Distance

* Distance approximation
* Koebe 1/4 theorem

### Decomposition

* quadtree decomposition

## Symmetry
### Mirror symmetry

Parameter plane and the Mandelbrot set is symmetric with respect to the x-axis in the plane :
Here is a C code that shows how to divide symmetric image (with axis in the middle of its height) into 3 parts :
* axis of symmetry
* 2 symmetric parts : above and below axis
See also Pascal code ( Delphi) for general case

### Rotational symmetry

Dynamical plane and the Julia set have rotational symmetry. It can be used to speed up computing :

## Complex numbers

Computations without explicit definition of complex numbers or without complex type are usually faster.

## Parallel computing

In Mandelbrot and Julia sets each point/pixel is calculated independently, so it can be easly divided into an smaller tasks and carried out simultaneously.
Types of parallel computing : Wikipedia description
* Multi-core (computing)
** Parallella and OpenCl
** Intel MIC
** MPI
** OpenMP
* General-purpose computing on graphics processing units (GPGPU):
** OpenCl
** CUDA
** WebGl

### Vectorisation
#### C

* gcc vector extension
* Intel® Advanced Vector Extensions

#### c++

* Mandelbrot in Eigen - Explicit vectorization

#### R

Vectorisation
Here is R code of above image with comments:

### Multicore

"I always create as many worker threads as I have cores. More than that and your system spends too much time task switching" - Duncan C
Using OpenMP :

#### Cell

"The Sony PlayStation 3 is one of the cheapest parallel computers available on the consumer market."

#### GPGPU

* CUDA
    Yellow Dog Linux for CUDA
** par4all
* OpenCl
* GLSL - Tools for GLSL editing
** Fragmentarium : playground for working with GPU (GLSL) based pixel graphics built in C++, OpenGL/GLSL, and Qt 4.
** shader editor using kickjs
** GLSL Sandbox by Ricardo Cabello Miguel using three.js
** jsfiddle and example
** WebGl : GLSL thru html. It is a 3D graphics API for JavaScript based on the OpenGL ES 2.0 API, that developers can use to create fully 3D web apps.
*** shadertoy by Inigo Quilez, Pol Jeremias : a browser based Fragment Shader editor for 2D effects with it.
**** how does a shadertoy work ?
*** Chrome Experiments
*** WebGL playground - new is not working !!
*** examples :
**** brainjam - webgl spiral
**** webgl examples by Evgeny Demidov
**** example by Hvidtfeldt Christensen
Canvas : list of supporting browsers

## Effects

Effects on 2 core CPU ( with 1 thread per core )
| Method | time [minutes] | relative speed |
| CPU & no optimisation | 80 | 1 |
| CPU & symmetry | 39 | 2 |
| CPU & symmetry & OpenMP | 24 | 3 |
| GPU | ? ( to do ) |
GPU should be:
* 62 time faster than CPU
* 760 times faster
Perturbation algorithm with series approximation ( 100 times faster on zoom levels around e100! )
Using WinXP Intel 2.9 GHz CPU (1 CPU used) with a GTX 480 GPU I get the following using 1000x1000 plot with 1000 max iterations :
| type | time | description |
| gpu | 0.07s | gpu is a pure CUDA solution on the GPU |
| gpuarray | 3.4s | gpuarray uses a numpy-like CUDA wrapper in Python on the GPU |
| numpy | 43.4s | numpy is a pure Numpy (C-based) solution on the CPU |
| python (serial) | 1605.6s | python is a pure Python solution on the CPU with numpy arrays |

# References

* ↑ stackoverflow question: what-is-the-fastest-way-to-calculate-e-to-2-trillion-digits
* ↑ The Computer Language Benchmarks Game
* ↑ Stack overflow meta : Is it okay to ask code optimization help?
* ↑ stack exchanfge : codereview
* ↑ Inner loop in wikipedia
* ↑ Adaptive super-sampling using distance estimate by Claude Heiland-Allen
* ↑ Distance estimation by Claude Heiland-Allen
* ↑ fractalforums.org: creating-super-resolution-fractal-images-really-fast-with-quad-trees
* ↑ wikipedia : Reflection symmetry
* ↑ Mirror symmetry around x axis at fraktal.republika.pl
* ↑ embarrassingly parallel problem
* ↑ wikipedia : Parallel_computers - Classes_of_parallel_computers
* ↑ Mandelbrot using OpenCl and Parallella
* ↑ wikipedia : Intel MIC
* ↑ parallel mandelbrot set (C Code with Message Passing Interface (MPI) library) by Omar U. Florez
* ↑ Guide into OpenMP: Easy multithreading programming for C++ by Joel Yliluoma
* ↑ Parallel Mandelbrot with OpenMP by dcfrogle
* ↑ claudiusmaximus : exponential mapping and openmp
* ↑ Part 1: OpenCL™ – Portable Parallelism By manythreads
* ↑ A Mandelbrot Set on the GPU in Matlab by Loren Shure
* ↑ progressive-julia-fractal using webgl by Felix Woitzel
* ↑ glsl sandbox at heroku.com
* ↑ gcc : Vector Extensions
* ↑ First code example using gcc vector support by bert hubert
* ↑ Mandelbrot calculation using SIMD by EJRH Edmund Horner
* ↑ The Computer Language Benchmarks Game
* ↑ Intel Advanced Vector Extensions
* ↑ ICC and Mandelbrot by Tim Horton
* ↑ Vectorization (parallel computing)
* ↑ Matlab - Code Vectorization Guide
* ↑ mandelbrot set in r by J.D. Long
* ↑ /programming/what-is-the-general-approach-to-threading-for-2d-plotting/ Fractal forums discussion : What is the general approach to threading for 2d plotting
* ↑ fractalforums discussion : most-powerful-computer-possible-for-a-reasonable-price/?PHPSESSID=7db41da33f499eb25ef85f46866438f2
* ↑ Algorithms for Specific Hardware -Mathematical Image Analysis Group Saarland University, Germany
* ↑ Jenks Parallel Programming Blog
* ↑ Programming high-performance applications on the Cell BE processor, Part 1: An introduction to Linux on the PLAYSTATION 3
* ↑ par4all
* ↑ MandelCPU vs MandelGPU Written by David Bucciarelli
* ↑ mathworks : llustrating-three-approaches-to-gpu-computing-the-mandelbrot-set
* ↑ Kalles Fraktaler 2 by Bernard Geiger
* ↑ Mandelbrot calculate using GPU, Serial numpy and faster numpy Use to show the speed difference between CPU and GPU calculations by Ian Ozsvald ianozsvald.com July 2010