-
Notifications
You must be signed in to change notification settings - Fork 1
/
debugCheck.m
92 lines (83 loc) · 2.36 KB
/
debugCheck.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
% [resCuda]=debugCheck(fkt,varargin) : calls a function twice with and without cuda acceleration to see if the results are the same
% This function is used a lot in installDebug.m
%
function [resCuda]=debugCheck(fkt,varargin)
argMatlab=cell(1,nargin-1);
hasCuda=0;
Nout = nargout;
if Nout== 0
Nout=1;
end
inargs=varargin{1};
global remMax;
global remPos;
global rem;
if isempty(remMax)
remMax=20;
remPos=0;
end
if isempty(rem)
rem={};
end
if remMax > 0
remPos = remPos+1;
end
for n=1:numel(inargs)
if (isa(inargs{n},'cuda'))
argMatlab{n}=castToMatlab(inargs{n}); % convert to double or dip_image
hasCuda=1;
else
argMatlab{n}=inargs{n};
end
end
resMatlab=cell(1,Nout);
eval(['[resMatlab{:}]=' fkt '(argMatlab{:});']);
if ~hasCuda
resCuda=resMatlab;
if Nout>0
resCuda=resCuda{:};
end
if exist('remMax','var') && remMax>0
rem{remPos}={resCuda}; % Store this in the Matlab-only run
end
if exist('remMax','var') && remMax>0 && remPos >= remMax
error('End of Debugging run reached.')
end
return;
end
%% Compare Matlab to CudaMat result
resCuda=cell(1,Nout);
eval(['[resCuda{:}]=' fkt '(inargs{:});']);
for n=1:Nout
D=double(resMatlab{n}- castToMatlab(resCuda{n}));
D=norm(D(:));
M1=max(abs(resMatlab{n}(:)));
M2=max(abs(resCuda{n}(:)));
M=max(M1,M2);
if (D/M > 0.01) % one percent error leads to scream
error('Function %s: detected disagreement of results!',fkt);
end
end
fprintf('checked function %s .. relative error is %d percent\n',fkt,D/M*100);
if exist('remMax','var') && remMax>0 && remPos < remMax
if length(rem) >= remPos
for n=1:Nout
D=double(castToMatlab(rem{remPos}{n}) - castToMatlab(resCuda{n}));
D=norm(D(:));
M1=max(abs(resMatlab{n}(:)));
M2=max(abs(resCuda{n}(:)));
M=max(M1,M2);
if (D/M > 0.01) % one percent error leads to scream
error('Function %s: detected disagreement of results!',fkt);
% dip_image(cat(4,rem{remPos}{n},resCuda{n}))
end
end
fprintf('checked function %s .. relative error to previous run is %d percent\n',fkt,D/M*100);
end
end
if exist('remMax','var') && remMax>0 && remPos >= remMax
error('End of Debugging run reached.')
end
if Nout>0
resCuda=resCuda{:};
end