Skip to content

Commit f354f92

Browse files
authored
Add Mandelbrot Code Sample (oneapi-src#15)
* Added source files for OMP Mandelbrot Signed-off-by: Ethan Hirsch <[email protected]> * added CI configs to sample.json Signed-off-by: Ethan Hirsch <[email protected]> * removed unused env definitions Signed-off-by: Ethan Hirsch <[email protected]> * added guid Signed-off-by: Ethan Hirsch <[email protected]> * updated license.txt Signed-off-by: Ethan Hirsch <[email protected]> * removed / from name in sample.json * Fixed license disclosure in src files Signed-off-by: Ethan Hirsch <[email protected]>
1 parent 12e4fae commit f354f92

File tree

13 files changed

+24659
-0
lines changed

13 files changed

+24659
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#==============================================================
2+
#
3+
# SAMPLE SOURCE CODE - SUBJECT TO THE TERMS OF SAMPLE CODE LICENSE AGREEMENT,
4+
# http://software.intel.com/en-us/articles/intel-sample-source-code-license-agreement/
5+
#
6+
# Copyright Intel Corporation
7+
#
8+
# THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
9+
# NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
10+
# PURPOSE, NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS.
11+
#
12+
# =============================================================
13+
CXX := icpc
14+
SRCDIR := src
15+
BUILDDIR := release
16+
CFLAGS := -O3 -ipo -qopenmp -std=c++11
17+
EXTRA_CFLAGS :=
18+
LIBFLAGS := -qopenmp
19+
20+
ifdef perf_num
21+
EXTRA_CFLAGS += -D PERF_NUM
22+
endif
23+
24+
TARGET := $(BUILDDIR)/MergeSort
25+
26+
icpc: $(TARGET)
27+
28+
SOURCES := $(wildcard $(SRCDIR)/*.cpp)
29+
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.cpp=.o))
30+
31+
$(TARGET): $(OBJECTS)
32+
@echo " Linking..."
33+
$(CXX) $^ $(LIBFLAGS) -o $(TARGET)
34+
35+
$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp
36+
@mkdir -p $(BUILDDIR)
37+
$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) -o $@ $<
38+
39+
run: $(TARGET)
40+
ifeq ($(shell uname -s),Darwin)
41+
@export DYLD_LIBRARY_PATH="$(LIBRARY_PATH)"; ./$(TARGET) $(option)
42+
else
43+
./$(TARGET) $(option)
44+
endif
45+
46+
clean:
47+
@echo " Cleaning..."
48+
@rm -fr $(BUILDDIR) $(TARGET) 2>/dev/null || true
49+
@rm -f *.png
50+
51+
.PHONY: clean
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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&reg; 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+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright 2020 Intel Corporation
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "Mandelbrot OpenMP*",
3+
"description": "Calculates the mandelbrot set and outputs a bmp image representation using OpenMP*",
4+
"categories": ["Toolkit/Intel® oneAPI HPC Toolkit"],
5+
"os": ["linux", "darwin"],
6+
"builder": ["cmake"],
7+
"languages": [{"cpp":{}}],
8+
"toolchain": ["icc"],
9+
"guid": "DD113F58-4D91-41BB-B46E-6CF2C0D9F6F9",
10+
"ciTests": {
11+
"linux": [
12+
{ "id": "standard", "steps": [ "make", "make run", "make clean" ] },
13+
{ "id": "perf_num", "env": [ "export perf_num=1" ], "steps": [ "make", "make run", "make clean" ] }
14+
],
15+
"darwin": [
16+
{ "id": "standard", "steps": [ "make", "make run", "make clean" ] },
17+
{ "id": "perf_num", "env": [ "export perf_num=1" ], "steps": [ "make", "make run", "make clean" ] }
18+
]
19+
}
20+
}

0 commit comments

Comments
 (0)