Debian Codegraph @

tests/cyclereduction.m

raw
1 source_dir = getenv('source_root');
2 addpath([source_dir filesep 'matlab']);
3
4 dynare_config;
5
6 testFailed = 0;
7
8 skipline()
9 disp('*** TESTING: cyclereduction.m ***');
10
11 matlab_cr_path = [source_dir filesep 'matlab' filesep 'missing' filesep 'mex' filesep 'cycle_reduction'];
12 addpath(matlab_cr_path);
13 cycle_reduction_matlab = @cycle_reduction;
14 rmpath(matlab_cr_path);
15
16 t0 = clock;
17
18 % Set the dimension of the problem to be solved.
19 n = 500;
20 % Set the equation to be solved
21 A = eye(n);
22 B = diag(30*ones(n,1)); B(1,1) = 20; B(end,end) = 20; B = B - diag(10*ones(n-1,1),-1); B = B - diag(10*ones(n-1,1),1);
23 C = diag(15*ones(n,1)); C = C - diag(5*ones(n-1,1),-1); C = C - diag(5*ones(n-1,1),1);
24 cvg_tol = 1e-12;
25 X1 = zeros(n,n);
26 X2 = zeros(n,n);
27
28 % 1. Solve the equation with the Matlab cycle reduction algorithm
29 tElapsed1 = 0.;
30 try
31 tic; [X1,info] = cycle_reduction_matlab(C,B,A,cvg_tol,100); tElapsed1 = toc;
32 disp(['Elapsed time for the Matlab cycle reduction algorithm is: ' num2str(tElapsed1) ' (n=' int2str(n) ').'])
33 R = norm(C+B*X1+A*X1*X1,1);
34 if (R > cvg_tol)
35 testFailed = testFailed+1;
36 dprintf('Matlab cycle_reduction solution is wrong')
37 end
38 catch
39 testFailed = testFailed+1;
40 dprintf('Matlab cycle_reduction failed')
41 end
42
43 % 2. Solve the equation with the Fortran cycle reduction algorithm
44 tElapsed2 = 0.;
45 try
46 tic; [X2,info] = cycle_reduction(C,B,A,cvg_tol,100); tElapsed2 = toc;
47 disp(['Elapsed time for the Fortran cycle reduction algorithm is: ' num2str(tElapsed2) ' (n=' int2str(n) ').'])
48 R = norm(C+B*X2+A*X2*X2,1);
49 if (R > cvg_tol)
50 testFailed = testFailed+1;
51 dprintf('Fortran cycle_reduction solution is wrong')
52 end
53 catch
54 testFailed = testFailed+1;
55 dprintf('Fortran cycle_reduction failed')
56 end
57
58 % 3. Compare solutions of the Fortran and Matlab routines
59 if (norm(X1 - X2, 1) > cvg_tol)
60 testFailed = testFailed+1;
61 dprintf('Fortran and Matlab cycle reduction solutions differ');
62 end
63
64 % Compare the Fortran and Matlab execution time
65 if tElapsed1<tElapsed2
66 skipline()
67 dprintf('Matlab cyclic reduction is %5.2f times faster than its Fortran counterpart.', tElapsed2/tElapsed1)
68 skipline()
69 else
70 skipline()
71 dprintf('Fortran cyclic reduction is %5.2f times faster than its Matlab counterpart.', tElapsed1/tElapsed2)
72 skipline()
73 end
74
75 t1 = clock;
76
77 fprintf('\n*** Elapsed time (in seconds): %.1f\n\n', etime(t1, t0));
78
79 quit(testFailed > 0)