-
Notifications
You must be signed in to change notification settings - Fork 1
/
applemantest.m
102 lines (94 loc) · 3.8 KB
/
applemantest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
% applemantest(use_cuda) : tests cudaMat performance
%************************** CudaMat ****************************************
% Copyright (C) 2008-2012 by Rainer Heintzmann *
% *
% This program is free software; you can redistribute it and/or modify *
% it under the terms of the GNU General Public License as published by *
% the Free Software Foundation; Version 2 of the License. *
% *
% This program is distributed in the hope that it will be useful, *
% but WITHOUT ANY WARRANTY; without even the implied warranty of *
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
% GNU General Public License for more details. *
% *
% You should have received a copy of the GNU General Public License *
% along with this program; if not, write to the *
% Free Software Foundation, Inc., *
% 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
%**************************************************************************
%
function applemantest(usecuda)
%minx=-2.5;maxx=1.5;miny=-2;maxy=2;
%minx=-0.2;maxx=-0.1;miny=-1.1;maxy=-0.98;
%minx=-0.131;maxx=-0.128;miny=-1.022;maxy=-1.0185;
%minx=-0.76;maxx=-0.72;miny=-0.22;maxy=-0.16;
% minx=-0.750;maxx=-0.743;miny=0.123;maxy=0.126;
minx=-2;maxx=1;miny=-1;maxy=1; % full Mandelbrod set
minx=-0.74;maxx=-0.731;miny=-0.22;maxy=-0.205;
minx=-0.733;maxx=-0.7315;miny=-0.209;maxy=-0.207;
%res=1000;iter=700;
res=6000;iter=1000;
%res=1024;iter=500;
%res=4098;iter=4000;
rg1=[minx:(maxx-minx)/res:maxx];
rg2=[miny:(maxy-miny)/res:maxy];
% if usecuda
% rg1=cuda(rg1);
% rg2=cuda(rg2);
% end
% Repmat not yet implemented!
if usecuda == 2 % define a precompiled cuda function
% The C-code below executes the mandelbrot calculation on each cuda-core. That is for each pixel of the image
% This yields the impressive overall speedup of 54000
ProgramText = sprintf( [ ...
'int n;\n ' ...
'float myrs=a[2*idx];\n' ...
'float myis=a[2*idx+1];\n' ...
'float myr=myrs;\nfloat myi=myis;\n ' ...
'float res=-1.0;\nfloat tmp;\n' ...
'for (n=0;n<%d;n++)\n' ...
' {tmp=myr*myr-myi*myi+myrs;' ...
' myi=2*myr*myi+myis; ' ...
' myr=tmp;\n' ...
' if ((res < 0) && (myr*myr+myi*myi)>16.0) {res=(float) n;}' ...
' }\n' ...
'c[idx]= res;' ...
],iter);
% Even fast is : ' if ((myr*myr+myi*myi)>16.0) {res=(float) n;break;}' ...
% But we keep this for a fair comparison
cuda_define('mandelbrot','CUDA_UnaryFkt','Computes the Mandelbrot set','c[idx]=a[idx];',ProgramText);
cuda_compile_all;
if isempty(which('mandelbrot'))
error('Mandelbrot program failed to compile');
end
end
c=repmat(rg1,[length(rg2) 1]) + i*repmat(rg2',[1 length(rg1)]);
if usecuda
pp=which('cuda_cuda');
if (isempty(pp))
fprintf('WARNING: There is no existing compiled cuda version. Trying to compile CudaMat by running applemantest(2)...\n');
applemantest(2);
fprintf('Now running applemantest(1) ...\n');
end
c=cuda(c);
end
if usecuda == 2 % define a precompiled cuda function
tic
res=mandelbrot(c);
else
tic
z=c;
res=0*z;
for p=1:iter % This is the loop calculating the mandelbrot set
z=z.*z + c;
res((res == 0) & (abs(z)>4)) = p;
% res = abs(z).^2;
end
end
fprintf('Am I done?\n');
toc
%dip_image(res)
image(rg1,rg2,100*res/iter)
fprintf('Now! Overhead is 0.2 sec\n');
toc