forked from andrewssobral/PROPACK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindBestMultiply.m
74 lines (56 loc) · 2.04 KB
/
findBestMultiply.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
function [forwardType,transposeType,details] = findBestMultiply(A,T)
% [forwardType,transposeType] = findBestMultiply(A)
% does a speed test to determine what the fastest routine
% for sparse matrix multiplication is, for the matrix A.
% ... = findBestMultiply(A,T)
% tries to make each test last T seconds (there are a 6 tests total)
%
% The fastest routine will depend on the verson of Matlab, the
% computer OS, the cpu type (dual core? etc.), and the particular
% size and structure of the matrix.
%
% Stephen Becker, [email protected], 3/14/09
% 5/13/09, smvp now handles complex data, so modify this accordingly
if nargin < 2, T = 1; end
At = A';
x = randn(size(A,2),1);
y = randn(size(A,1),1);
if ~isreal(A)
x = x + 1i*randn(size(A,2),1);
y = y + 1i*randn(size(A,1),1);
end
% how many times to repeat the test?
tic; A'*y; t = toc;
% if we want each test to last say, T, seconds, then...
nIter = max( 1, round(T/t) );
% -- for the forward multiply --
% -- Method 1: use standard MATLAB
tic; for i = 1:nIter, A*x; end; t1 = toc;
% -- Method 2: use transpose trick (works best in new versions)
tic; for i = 1:nIter, At'*x; end; t2 = toc;
% -- Method 3: simple mex file
try
tic; for i = 1:nIter, smvp(A,x); end; t3 = toc;
catch
l = lasterror;
fprintf('Error thrown: %s\n',l.message);
disp('findBestMultiply: Sorry, this probably means the smvp mex file\n\t is not installed');
disp('Try compiling it with: mex -O smvp.c in the "private" subdirectory');
t3 = Inf;
end
[m,forwardType] = min( [t1,t2,t3] );
if nargout > 2, details = [t1,t2,t3]; end
% -- for the transpose multiply --
% -- Method 1: use standard MATLAB
tic; for i = 1:nIter, At*y; end; t1 = toc;
% -- Method 2: use transpose trick (works best in new versions)
tic; for i = 1:nIter, A'*y; end; t2 = toc;
% -- Method 3: simple mex file
try
tic; for i = 1:nIter, smvp(At,y); end; t3 = toc;
catch
% disp('Sorry, smvp mex file not installed');
t3 = Inf;
end
[m,transposeType] = min( [t1,t2,t3] );
if nargout > 2, details = [details,[t1,t2,t3] ]; end