|
| 1 | +# `Mandelbrot` Sample |
| 2 | + |
| 3 | +Mandelbrot is an infinitely complex fractal patterning that is derived from a simple formula. This sample demonstrates how to accelerate program performance with SIMD and parallelization using OpenMP*, in the context of calculating the mandelbrot set. |
| 4 | + |
| 5 | + |
| 6 | +| Optimized for | Description |
| 7 | +|:--- |:--- |
| 8 | +| OS | MacOS Catalina or newer; Linux* Ubuntu* 18.04 |
| 9 | +| Hardware | Skylake with GEN9 or newer |
| 10 | +| Software | Intel® C++ Compiler 19.1 or newer |
| 11 | +| What you will learn | How to optimize a scalar implementation using OpenMP pragmas |
| 12 | +| Time to complete | 15 minutes |
| 13 | + |
| 14 | +Performance number tabulation |
| 15 | + |
| 16 | +| Mandelbrot Version | Performance data |
| 17 | +|:--- |:--- |
| 18 | +| Scalar baseline | 1.0 |
| 19 | +| OpenMP SIMD | 2x speedup |
| 20 | +| OpenMP parallel | 6x speedup |
| 21 | +| OpenMP SIMD + parallel | 10x speedup |
| 22 | + |
| 23 | + |
| 24 | +## Purpose |
| 25 | + |
| 26 | +Mandelbrot is a C++ application that generates a fractal image by tracking how many iterations of the function z_n+1 = z_n^2 + c remain bounded, where c is a coordinate on the complex plane and z_0 = 0. In performing this calculation, complex numbers belonging to the mandelbrot set will take infinite iterations as they will always remain bounded. So a maximum depth of iterations is set so that the program may execute in finite time. |
| 27 | + |
| 28 | +Each point on the complex plane can be calculated independently, which lends the calculation of the mandelbrot image to parallelism. Furthermore, since the calculation on each point is identical, the program can take advantage of SIMD directives to get even greater performance. This code sample demonstrates how to optimize a serial implementation of the mandelbrot image calculation using OpenMP pragmas for SIMD and parallelization. |
| 29 | + |
| 30 | + |
| 31 | +## Key Implementation Details |
| 32 | + |
| 33 | +The 4 mandelbrot function implementations are identically written, with the only difference being the progressive use of OpenMP pragmas for enabling parallelization and SIMD. |
| 34 | + |
| 35 | + |
| 36 | +## License |
| 37 | + |
| 38 | +This code sample is licensed under MIT license. |
| 39 | + |
| 40 | + |
| 41 | +## Building the `Mandelbrot` Program |
| 42 | + |
| 43 | +Perform the following steps: |
| 44 | +1. Build the program using the following `make` commands. |
| 45 | +``` |
| 46 | +$ export perf_num=1 *optional, will enable performance tabulation mode |
| 47 | +$ make |
| 48 | +``` |
| 49 | + |
| 50 | +2. Run the program: |
| 51 | + ``` |
| 52 | + make run |
| 53 | + ``` |
| 54 | +
|
| 55 | +3. Clean the program using: |
| 56 | + ``` |
| 57 | + make clean |
| 58 | + ``` |
| 59 | +
|
| 60 | +
|
| 61 | +## Running the Sample |
| 62 | +
|
| 63 | +### Application Parameters |
| 64 | +You can modify the Mandelbrot parameters from within the main() definition near the head. The configurable parameters allow one to modify the dimensions(resolution) of the output image, which will also affect the execution time of the program. max_depth defines the upper limit of iterations the mandelbrot function will take to calculate a single point: |
| 65 | + int height = 1024; |
| 66 | + int width = 2048; |
| 67 | + int max_depth = 100; |
| 68 | +
|
| 69 | +In mandelbrot.cpp, the schedule(<static/dynamic>, <chunk_size>) pragmas in the OpenMP parallel for sections can me modified to change the parallelization parameters. changing between static and dynamic affects how work items are distributed between threads, and the chunk_size affects the size of each work item. On line 69, there is a preprocessor definition NUM_THREADS: Changing this value affects the number of threads dedicated to each parallel section. The ideal number of threads will vary based on the device hardware. |
| 70 | +
|
| 71 | +### Example of Output |
| 72 | +``` |
| 73 | +This example will check how many iterations of z_n+1 = z_n^2 + c a complex set will remain bounded. Pick which parallel method you would like to use. |
| 74 | +[0] all tests |
| 75 | +[1] serial/scalar |
| 76 | +[2] OpenMP SIMD |
| 77 | +[3] OpenMP Parallel |
| 78 | +[4] OpenMP Both |
| 79 | + > 0 |
| 80 | +
|
| 81 | +Running all tests |
| 82 | + |
| 83 | +Starting serial, scalar Mandelbrot... |
| 84 | +Calculation finished. Processing time was 198ms |
| 85 | +Saving image as mandelbrot_serial.png |
| 86 | + |
| 87 | +Starting OMP SIMD Mandelbrot... |
| 88 | +Calculation finished. Processing time was 186ms |
| 89 | +Saving image as mandelbrot_simd.png |
| 90 | + |
| 91 | +Starting OMP Parallel Mandelbrot... |
| 92 | +Calculation finished. Processing time was 33ms |
| 93 | +Saving image as mandelbrot_parallel.png |
| 94 | + |
| 95 | +Starting OMP SIMD + Parallel Mandelbrot... |
| 96 | +Calculation finished. Processing time was 31ms |
| 97 | +Saving image as mandelbrot_simd_parallel.png |
| 98 | +``` |
0 commit comments