~ubuntu-branches/ubuntu/hardy/suitesparse/hardy

« back to all changes in this revision

Viewing changes to SSMULT/sstest.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere, Rafael Laboissiere, Ondrej Certik
  • Date: 2008-02-21 14:46:50 UTC
  • mfrom: (1.1.2 upstream) (5.1.1 hardy)
  • Revision ID: james.westby@ubuntu.com-20080221144650-tgeppgj0t7s759i8
Tags: 3.1.0-3
[ Rafael Laboissiere ]
* Upload to unstable

[ Ondrej Certik ]
* XS-DM-Upload-Allowed: yes field added
* Ondrej Certik added to uploaders

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function sstest
 
2
%SSTEST exhaustive performance test for SSMULT.
 
3
%
 
4
% Example
 
5
%   sstest
 
6
%
 
7
% See also ssmult, ssmultsym, ssmult_unsorted, ssmult_install, sstest2, mtimes.
 
8
 
 
9
% Copyright 2007, Timothy A. Davis, University of Florida.
 
10
 
 
11
N = [500:50:1000 1100:100:3000 3200:200:5000 ] ;
 
12
 
 
13
rand ('state',0) ;
 
14
 
 
15
% warmup for more accurate timings
 
16
A = sparse (1) ;
 
17
B = sparse (1) ;
 
18
C = A*B ;
 
19
D = ssmult(A,B) ;
 
20
err = norm (C-D,1) ;
 
21
if (err > 0)
 
22
    error ('test failure') ;
 
23
end
 
24
clear C D
 
25
 
 
26
titles = { ...
 
27
    'C=A*B blue, C=B*A red, both real', ...
 
28
    'A real, B complex', 'A complex, B real', 'both complex' } ;
 
29
 
 
30
xlabels = { '(A random, B diagonal)', '(A random, B permutation)', ...
 
31
    '(A random, B tridiagonal)' } ;
 
32
 
 
33
fprintf ('\nIn the next plots, speedup is the time for MATLAB C=A*B divided\n');
 
34
fprintf ('by the time for C=ssmult(A,B).  The X-axis is n, the dimension\n') ;
 
35
fprintf ('of the square matrices A and B.  A is a sparse random matrix with\n');
 
36
fprintf ('1%% nonzero values.  B is diagonal in the first row of plots,\n') ;
 
37
fprintf ('a permutation in the 2nd row, and tridiagonal in the third.\n') ;
 
38
fprintf ('C=A*B is in blue, C=B*A is in red.  A and B are both real in the\n') ;
 
39
fprintf ('first column of plots, B is complex in the 2nd, A in the 3rd, and\n');
 
40
fprintf ('both are complex in the 4th column of plots.  You will want to\n') ;
 
41
fprintf ('maximize the figure; otherwise the text is too hard to read.\n') ; 
 
42
 
 
43
tlim = 0.1 ;
 
44
figure (1) ;
 
45
clf ;
 
46
 
 
47
for fig = 1:3
 
48
 
 
49
    fprintf ('Testing C=A*B and C=B*A %s\n', xlabels {fig}) ;
 
50
 
 
51
    T = zeros (length(N),4,4) ;
 
52
 
 
53
    for k = 1:length(N)
 
54
 
 
55
        n = N (k) ;
 
56
        try
 
57
 
 
58
            A = sprand (n,n,0.01) ;
 
59
            if (fig == 1)
 
60
                % B diagonal
 
61
                B = spdiags (rand (n,1), 0, n, n) ;
 
62
            elseif (fig == 2)
 
63
                % B permutation
 
64
                B = spdiags (rand (n,1), 0, n, n) ;
 
65
                B = B (:,randperm(n)) ;
 
66
            else
 
67
                % B tridiagonal
 
68
                B = spdiags (rand (n,3), -1:1, n, n) ;
 
69
            end
 
70
 
 
71
            for kind = 1:4
 
72
 
 
73
                if (kind == 2)
 
74
                    % A complex, B real
 
75
                    A = A + 1i*sprand (A) ;
 
76
                elseif (kind == 3)
 
77
                    % A real, B complex
 
78
                    A = real (A) ;
 
79
                    B = B + 1i*sprand (B) ;
 
80
                elseif (kind == 4)
 
81
                    % both complex
 
82
                    A = A + 1i*sprand (A) ;
 
83
                    B = B + 1i*sprand (B) ;
 
84
                end
 
85
 
 
86
                t1 = 0 ;
 
87
                trials = 0 ;
 
88
                tic
 
89
                while (t1 < tlim)
 
90
                    C = A*B ;
 
91
                    trials = trials + 1 ;
 
92
                    t1 = toc ;
 
93
                end
 
94
                t1 = t1 / trials ;
 
95
 
 
96
                t2 = 0 ;
 
97
                trials = 0 ;
 
98
                tic
 
99
                while (t2 < tlim)
 
100
                    D = ssmult (A,B) ;
 
101
                    trials = trials + 1 ;
 
102
                    t2 = toc ;
 
103
                end
 
104
                t2 = t2 / trials ;
 
105
 
 
106
                err = norm (C-D,1) ;
 
107
                if (err > 0)
 
108
                    error ('test failure') ;
 
109
                end
 
110
                clear C
 
111
                clear D
 
112
 
 
113
                t3 = 0 ;
 
114
                trials = 0 ;
 
115
                tic
 
116
                while (t3 < tlim)
 
117
                    C = B*A ;
 
118
                    trials = trials + 1 ;
 
119
                    t3 = toc ;
 
120
                end
 
121
                t3 = t3 / trials ;
 
122
 
 
123
                t4 = 0 ;
 
124
                trials = 0 ;
 
125
                tic
 
126
                while (t4 < tlim)
 
127
                    D = ssmult (B,A) ;
 
128
                    trials = trials + 1 ;
 
129
                    t4 = toc ;
 
130
                end
 
131
                t4 = t4 / trials ;
 
132
 
 
133
                err = norm (C-D,1) ;
 
134
                if (err > 0)
 
135
                    error ('test failure') ;
 
136
                end
 
137
                clear C
 
138
                clear D
 
139
 
 
140
                T (k,kind,1) = t1 ;
 
141
                T (k,kind,2) = t2 ;
 
142
                T (k,kind,3) = t3 ;
 
143
                T (k,kind,4) = t4 ;
 
144
                subplot (3,4,kind + 4*(fig-1)) ;
 
145
                plot (N(1:k), T (1:k,kind,1) ./ T (1:k,kind,2), 'o', ...
 
146
                      N(1:k), T (1:k,kind,3) ./ T (1:k,kind,4), 'rx', ...
 
147
                      [N(1) n], [1 1], 'k') ;
 
148
                xlabel (['n ' xlabels{fig}]) ;
 
149
                ylabel ('speedup') ;
 
150
                axis tight
 
151
                title (titles {kind}) ;
 
152
                drawnow
 
153
 
 
154
            end
 
155
 
 
156
        catch
 
157
            % probably because we ran out of memory ...
 
158
            disp (lasterr) ;
 
159
            break ;
 
160
        end
 
161
    end
 
162
end
 
163