Skip to content

Commit cfb0d70

Browse files
committed
Initial move into git
0 parents  commit cfb0d70

26 files changed

+5695
-0
lines changed

A GPU Mandelbrot Set.mlappinstall

132 KB
Binary file not shown.

A GPU Mandelbrot Set.prj

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<deployment-project plugin="plugin.apptool" plugin-version="1.0">
2+
<configuration build-checksum="177489301" file="/home/btordoff/matlab/FileExchangeSubmissions/GPU-Mandelbrot-Set/A GPU Mandelbrot Set.prj" location="/home/btordoff/matlab/FileExchangeSubmissions/GPU-Mandelbrot-Set" name="A GPU Mandelbrot Set" target="target.mlapps" target-name="Package App">
3+
<param.appname>A GPU Mandelbrot Set</param.appname>
4+
<param.authnamewatermark>Ben Tordoff</param.authnamewatermark>
5+
<param.email />
6+
<param.company>MathWorks</param.company>
7+
<param.icon>${PROJECT_ROOT}/A GPU Mandelbrot Set_resources/icon_24.png</param.icon>
8+
<param.icons>
9+
<file>${PROJECT_ROOT}/A GPU Mandelbrot Set_resources/icon_48.png</file>
10+
<file>${PROJECT_ROOT}/A GPU Mandelbrot Set_resources/icon_24.png</file>
11+
<file>${PROJECT_ROOT}/A GPU Mandelbrot Set_resources/icon_16.png</file>
12+
</param.icons>
13+
<param.summary>Explore the Mandelbrot Set using MATLAB and an NVIDIA GPU.</param.summary>
14+
<param.description>This application allows you to explore the wonders of the Mandelbrot Set in MATLAB with the help of a capable GPU. It is primarily intended as a demonstration of the different ways in which a MATLAB algorithm can be converted to run on the GPU, however it also has some fun features:
15+
16+
* Use the normal MATLAB zoom and pan to browse the Mandelbrot Set
17+
* Sit back and watch the app pan and zoom between pre-stored locations in the set
18+
* Add your own locations to the animation list
19+
20+
Three different ways of speeding up the algorithm using GPU hardware are provided along with a normal CPU-only version. Have a look at the code for these to get a feeling for how MATLAB's GPU support works.
21+
22+
See also the related "GPU Julia Set Explorer": http://www.mathworks.com/matlabcentral/fileexchange/33201
23+
24+
NB: Requires MATLAB R2012a or above with Parallel Computing Toolbox and a GPU with CUDA Compute Capability 1.3 or above.</param.description>
25+
<param.screenshot>/mathworks/home/btordoff/matlab/FileExchangeSubmissions/GPUMandelbrot/GPUMandelbrot.png</param.screenshot>
26+
<param.version>1.5</param.version>
27+
<param.products.name>
28+
<item>MATLAB</item>
29+
<item>Parallel Computing Toolbox</item>
30+
</param.products.name>
31+
<param.products.id>
32+
<item>-1</item>
33+
<item>-1</item>
34+
</param.products.id>
35+
<param.products.version>
36+
<item>-1</item>
37+
<item>-1</item>
38+
</param.products.version>
39+
<param.platforms />
40+
<param.output>${PROJECT_ROOT}</param.output>
41+
<param.guid>2bfeee5d-ec9e-4238-9101-cfb931d651a9</param.guid>
42+
<unset>
43+
<param.email />
44+
<param.platforms />
45+
<param.output />
46+
</unset>
47+
<fileset.main>
48+
<file>${PROJECT_ROOT}/mandelbrotViewer.m</file>
49+
</fileset.main>
50+
<fileset.depfun>
51+
<file>${PROJECT_ROOT}/computeMandelbrotCPU.m</file>
52+
<file>${PROJECT_ROOT}/computeMandelbrotCUDAKernel.m</file>
53+
<file>${PROJECT_ROOT}/computeMandelbrotGPUArrayFun.m</file>
54+
<file>${PROJECT_ROOT}/computeMandelbrotGPUBuiltins.m</file>
55+
</fileset.depfun>
56+
<fileset.resources>
57+
<file>${PROJECT_ROOT}/README.md</file>
58+
<file>${PROJECT_ROOT}/compilePTX.m</file>
59+
<file>${PROJECT_ROOT}/icon_mandelControls.png</file>
60+
<file>${PROJECT_ROOT}/icon_play.png</file>
61+
<file>${PROJECT_ROOT}/jet2.m</file>
62+
<file>${PROJECT_ROOT}/license.txt</file>
63+
<file>${PROJECT_ROOT}/locations.csv</file>
64+
<file>${PROJECT_ROOT}/mandelbrotViewerProcessElement.cu</file>
65+
<file>${PROJECT_ROOT}/mandelbrotViewerProcessElement.m</file>
66+
<file>${PROJECT_ROOT}/mandelbrotViewerProcessElement.ptxa64</file>
67+
<file>${PROJECT_ROOT}/mandelbrotViewerProcessElement.ptxmaci64</file>
68+
<file>${PROJECT_ROOT}/mandelbrotViewerProcessElement.ptxw32</file>
69+
<file>${PROJECT_ROOT}/mandelbrotViewerProcessElement.ptxw64</file>
70+
</fileset.resources>
71+
<fileset.package />
72+
<build-deliverables>
73+
<file location="/home/btordoff/matlab/FileExchangeSubmissions" name="GPU-Mandelbrot-Set" optional="false">/home/btordoff/matlab/FileExchangeSubmissions/GPU-Mandelbrot-Set</file>
74+
</build-deliverables>
75+
<workflow />
76+
<matlab>
77+
<root>/mathworks/UK/devel/archive/R2020b/perfect/matlab</root>
78+
<toolboxes>
79+
<toolbox name="matlabcoder" />
80+
<toolbox name="embeddedcoder" />
81+
<toolbox name="gpucoder" />
82+
<toolbox name="fixedpoint" />
83+
<toolbox name="matlabhdlcoder" />
84+
<toolbox name="neuralnetwork" />
85+
<toolbox name="polyspacebugfinder" />
86+
</toolboxes>
87+
<toolbox>
88+
<matlabcoder>
89+
<enabled>true</enabled>
90+
</matlabcoder>
91+
</toolbox>
92+
<toolbox>
93+
<embeddedcoder>
94+
<enabled>true</enabled>
95+
</embeddedcoder>
96+
</toolbox>
97+
<toolbox>
98+
<gpucoder>
99+
<enabled>true</enabled>
100+
</gpucoder>
101+
</toolbox>
102+
<toolbox>
103+
<fixedpoint>
104+
<enabled>true</enabled>
105+
</fixedpoint>
106+
</toolbox>
107+
<toolbox>
108+
<matlabhdlcoder>
109+
<enabled>true</enabled>
110+
</matlabhdlcoder>
111+
</toolbox>
112+
<toolbox>
113+
<neuralnetwork>
114+
<enabled>true</enabled>
115+
</neuralnetwork>
116+
</toolbox>
117+
<toolbox>
118+
<polyspacebugfinder>
119+
<enabled>true</enabled>
120+
</polyspacebugfinder>
121+
</toolbox>
122+
</matlab>
123+
<platform>
124+
<unix>true</unix>
125+
<mac>false</mac>
126+
<windows>false</windows>
127+
<win2k>false</win2k>
128+
<winxp>false</winxp>
129+
<vista>false</vista>
130+
<linux>true</linux>
131+
<solaris>false</solaris>
132+
<osver>4.19.0-13-amd64</osver>
133+
<os32>false</os32>
134+
<os64>true</os64>
135+
<arch>glnxa64</arch>
136+
<matlab>true</matlab>
137+
</platform>
138+
</configuration>
139+
</deployment-project>

GPUMandelbrot.png

66.6 KB
Loading

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
GPU Mandelbrot Set
2+
------------------
3+
4+
This application allows you to explore the wonders of the Mandelbrot Set in MATLAB with the help of a capable GPU. It is primarily intended as a demonstration of the different ways in which a MATLAB algorithm can be converted to run on the GPU, however it also has some fun features:
5+
6+
* Use the normal MATLAB zoom and pan to browse the Mandelbrot Set
7+
* Sit back and watch the app pan and zoom between pre-stored locations in the set
8+
* Add your own locations to the animation list
9+
10+
Three different ways of speeding up the algorithm using GPU hardware are provided along with a normal CPU-only version. Have a look at the code for these to get a feeling for how MATLAB's GPU support works.
11+
12+
13+
Requirements
14+
------------
15+
16+
This demo app requires:
17+
18+
* MATLAB R2011a or better (v7.12 or higher)
19+
* Parallel Computing Toolbox
20+
* A GPU with CUDA Compute Capability 3.0 or higher
21+
22+
23+
Running it
24+
----------
25+
26+
From MATLAB, type "mandelbrotViewer" to launch the application.
27+
28+
29+
Help
30+
----
31+
32+
From MATLAB, type "help mandelbrotViewer" to view the help for the application.
33+

changes.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2019-04-29 (v1.5):
2+
* Make robust to zero or non-integer axes sizes. Add app icon.
3+
4+
2014-10-15 (v1.4):
5+
* Fix compatibility with R2014b
6+
7+
2012-11-01 (v1.3):
8+
* Updated to use new "gpuArray" names for build functions.
9+
* MATLAB App included for ease of installation/removal.
10+
11+
2012-04-17 (v1.2):
12+
* Add animation button to toolbar
13+
* Fix display freeze when toggling control panel on/off
14+
15+
2011-11-15 (v1.1):
16+
* Fix warnings when animating with an odd window size
17+
* Guard against the window closing to prevent an error when exiting
18+
19+
2011-04-07 (v1.0):
20+
* Initial FEX release

compilePTX.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function compilePTX()
2+
%compilePTX try to recompile the PTX for the Mandelbrot kernel
3+
%
4+
% compilePTX() attempts to call NVCC to recompile the Mandelbrot "process
5+
% element" kernel for the current platform. The PTX is produced for 1.3
6+
% cards to allow use on any supported device. If you have a 2.0 (Fermi)
7+
% or 3.0 (Kepler) device, you may see some performance benefit to
8+
% chnageing the architecture flag below to match.
9+
10+
% Copyright 2012 The Mathworks, Inc.
11+
12+
ptxarch = 'sm_13';
13+
kernel = 'mandelbrotViewerProcessElement';
14+
15+
flags = sprintf( '-arch=%s', ptxarch );
16+
if ismac
17+
% On Mac we must force the use of 64-bit pointers otherwise the host
18+
% uses 64-bit and the device 32!
19+
flags = [flags, ' -m 64'];
20+
end
21+
22+
cmd = sprintf( 'nvcc -ptx %s.cu %s -o %s.%s', ...
23+
kernel, flags, kernel, parallel.gpu.ptxext );
24+
fprintf('Running command: %s\n', cmd );
25+
result = system( cmd );
26+
if (result ~= 0)
27+
fprintf( 'Failed with error %d.\n', result )
28+
end
29+
end
30+

computeMandelbrotCPU.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function logCount = computeMandelbrotCPU(xlim, numx, ylim, numy, maxIters)
2+
% Create a view of the Mandelbrot set using only the CPU.
3+
4+
% Create the input arrays
5+
escapeRadius2 = 400; % Square of escape radius
6+
x = linspace(xlim(1), xlim(2), numx);
7+
y = linspace(ylim(1), ylim(2), numy);
8+
[x0,y0] = meshgrid(x, y);
9+
count = zeros(size(x0));
10+
z0 = complex(x0, y0);
11+
z = z0;
12+
13+
% Calculate
14+
for n = 0:maxIters
15+
inside = ((real(z).^2 + imag(z).^2) <= escapeRadius2);
16+
count = count + inside;
17+
z = inside.*(z.*z + z0) + (1-inside).*z;
18+
end
19+
magZ2 = real(z).^2 + imag(z).^2;
20+
logCount = log(count+1 - log(log(max(magZ2,escapeRadius2))/2)/log(2));

computeMandelbrotCUDAKernel.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function logCount = computeMandelbrotCUDAKernel( xlim, numx, ylim, numy, maxIters )
2+
% Use pre-existing CUDA/C++ code.
3+
persistent kernel
4+
if isempty(kernel)
5+
% First time round we must load the kernel
6+
thisDir = fileparts( mfilename( 'fullpath' ) );
7+
baseName = 'mandelbrotViewerProcessElement';
8+
cudaFile = fullfile( thisDir, [baseName,'.cu'] );
9+
ptxName = [baseName,'.',parallel.gpu.ptxext];
10+
ptxFile = fullfile( thisDir, ptxName );
11+
if exist( ptxFile, 'file' ) ~= 2
12+
error( 'mandelbrotViewer:MissingPTX', 'Could not find ''%s''. Please use NVCC to compile it', ptxname );
13+
end
14+
kernel = parallel.gpu.CUDAKernel(ptxFile, cudaFile);
15+
end
16+
17+
18+
% Create the input arrays
19+
x = gpuArray.linspace(xlim(1), xlim(2), numx);
20+
y = gpuArray.linspace(ylim(1), ylim(2), numy);
21+
[x, y] = meshgrid(x, y);
22+
radius = 20;
23+
24+
% Make sure we have sufficient blocks to cover the whole array
25+
numElements = numel(x);
26+
kernel.ThreadBlockSize = [kernel.MaxThreadsPerBlock,1,1];
27+
kernel.GridSize = [ceil(numElements/kernel.MaxThreadsPerBlock),1];
28+
29+
% Call the kernel
30+
logCount = gpuArray.zeros(size(x));
31+
logCount = feval(kernel, logCount, x, y, radius, maxIters, numElements);
32+
33+
% Gather the result back to the CPU
34+
logCount = gather(logCount);
35+

computeMandelbrotGPU.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function logCount = computeMandelbrotGPUBuiltins(xlim, numx, ylim, numy, maxIters)
2+
% Create a view of the Mandelbrot set using only the CPU.
3+
% This is the base-line version of the algorithm that is adapted to
4+
% run on the GPU in different ways in the functions below.
5+
6+
% Create the input arrays
7+
x = linspace(xlim(1), xlim(2), numx);
8+
y = linspace(ylim(1), ylim(2), numy);
9+
[x,y] = meshgrid(x, y);
10+
count = zeros(size(x));
11+
12+
% Call the element-by-element calculation
13+
logCount = processMandelbrotElement(count, x, y, maxIters);
14+

computeMandelbrotGPUArrayFun.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function logCount = computeMandelbrotGPUArrayFun(xlim, numx, ylim, numy, maxIters)
2+
% Compute using GPU arrayfun.
3+
4+
% Copyright 2010-2012 The Mathworks, Inc.
5+
6+
% Create the input arrays
7+
x = gpuArray.linspace(xlim(1), xlim(2), numx);
8+
y = gpuArray.linspace(ylim(1), ylim(2), numy);
9+
[x,y] = meshgrid(x, y);
10+
11+
% Calculate
12+
logCount = arrayfun(@mandelbrotViewerProcessElement, x, y, 400, maxIters);
13+
14+
% Gather the result back to the CPU
15+
logCount = gather(logCount);
16+
17+
18+
function logCount = iProcessElement(x0, y0, escapeRadius2, maxIterations)
19+
% Evaluate the Mandelbrot function for a single element
20+
21+
22+
z0 = complex( x0, y0 );
23+
z = z0;
24+
count = 0;
25+
while (count <= maxIterations) && (z*conj(z) <= escapeRadius2)
26+
z = z*z + z0;
27+
count = count + 1;
28+
end
29+
magZ2 = max(real(z).^2 + imag(z).^2, escapeRadius2);
30+
logCount = log(count + 1 - log(log( magZ2 ) / 2 ) / log(2));

0 commit comments

Comments
 (0)