Debian Codegraph @

tests/run_block_bytecode_tests.m

raw
1 % Copyright © 2011-2025 Dynare Team
2 %
3 % This file is part of Dynare.
4 %
5 % Dynare is free software: you can redistribute it and/or modify
6 % it under the terms of the GNU General Public License as published by
7 % the Free Software Foundation, either version 3 of the License, or
8 % (at your option) any later version.
9 %
10 % Dynare is distributed in the hope that it will be useful,
11 % but WITHOUT ANY WARRANTY; without even the implied warranty of
12 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 % GNU General Public License for more details.
14 %
15 % You should have received a copy of the GNU General Public License
16 % along with Dynare. If not, see <https://www.gnu.org/licenses/>.
17
18 % Implementation notes:
19 %
20 % Before every call to Dynare, the contents of the workspace is saved in
21 % 'wsMat.mat', and reloaded after Dynare has finished (this is necessary since
22 % Dynare does a 'clear -all').
23 % Note that we take care of clearing oo_ before saving the workspace,
24 % otherwise the newly created oo_ will be scratched upon loading,
25 % thus making the path comparison bogus.
26 % We also take care of clearing the 'exception' variable in all 'catch'
27 % block, because otherwise the next 'load wsMat' within a 'catch' block will
28 % overwrite the last exception.
29
30 source_dir = getenv('source_root');
31 addpath([source_dir filesep 'tests' filesep 'utils']);
32 addpath([source_dir filesep 'matlab']);
33
34 % Test block_bytecode/ls2003.mod with various combinations of
35 % block/bytecode/solve_algo/stack_solve_algo
36 failedBlock = {};
37 cd block_bytecode
38 tic;
39 for blockFlag = 0:1
40 for storageFlag = 0:2 % 0=M-file, 1=use_dll, 2=bytecode
41 default_solve_algo = 2;
42 default_stack_solve_algo = 0;
43 if ~blockFlag && storageFlag ~= 2
44 solve_algos = [1:4 6:9];
45 stack_solve_algos = [0:3 6];
46 elseif blockFlag && storageFlag ~= 2
47 solve_algos = [1:4 6:9];
48 stack_solve_algos = [0:4 6];
49 else
50 solve_algos = 1:9;
51 stack_solve_algos = 0:6;
52 end
53 if isoctave || user_has_matlab_license('optimization_toolbox')
54 solve_algos = [ 0 solve_algos ];
55 end
56
57 if isequal(getenv('HAS_PARU'), 'true') && storageFlag ~= 2
58 stack_solve_algos = [stack_solve_algos, 8];
59 end
60
61 if isequal(getenv('HAS_PARDISO'), 'true') && storageFlag ~= 2
62 stack_solve_algos = [stack_solve_algos, 9:10];
63 end
64
65 % Workaround for strange race condition related to the static/dynamic
66 % files (especially when we switch to/from use_dll)
67 if isoctave && isfolder('+ls2003_tmp')
68 rmdir('+ls2003_tmp', 's');
69 pause(1)
70 end
71
72 for i = 1:length(solve_algos)
73 if ~blockFlag && storageFlag == 0 && (i == 1)
74 % This is the reference simulation path against which all
75 % other simulations will be tested
76 try
77 old_path = path;
78 save wsMat
79 run_ls2003(blockFlag, storageFlag, solve_algos(i), default_stack_solve_algo)
80 load wsMat
81 path(old_path);
82 y_ref = oo_.endo_simul;
83 save('test.mat','y_ref');
84 catch exception
85 load wsMat
86 path(old_path);
87 failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'];
88 printTestError(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception);
89 clear exception
90 end
91 else
92 try
93 old_path = path;
94 clear oo_ % Ensure that oo_.endo_simul won’t be overwritten when loading wsMat
95 save wsMat
96 run_ls2003(blockFlag, storageFlag, solve_algos(i), default_stack_solve_algo)
97 load wsMat
98 path(old_path);
99 % Test against the reference simulation path
100 load('test.mat','y_ref');
101 diff = oo_.endo_simul - y_ref;
102 if max(max(abs(diff))) > 2*options_.dynatol.x
103 failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'];
104 if isoctave
105 exception.message = 'ERROR: simulation path differs from the reference path';
106 else
107 exception = MException('Dynare:simerr', 'ERROR: simulation path differs from the reference path');
108 end
109 printTestError(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception);
110 clear exception
111 end
112 catch exception
113 load wsMat
114 path(old_path);
115 failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'];
116 printTestError(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception);
117 clear exception
118 end
119 end
120 end
121 for i = 1:length(stack_solve_algos)
122 if ismember(stack_solve_algos(i), [2 3])
123 if storageFlag ~= 2
124 preconditioners = {'first_iter_lu', 'block_diagonal_lu', 'incomplete_lu'};
125 else % bytecode
126 preconditioners = {'incomplete_lu'};
127 end
128 else
129 preconditioners = {''};
130 end
131 for j = 1:length(preconditioners)
132 try
133 old_path = path;
134 clear oo_ % Ensure that oo_.endo_simul won’t be overwritten when loading wsMat
135 save wsMat
136 run_ls2003(blockFlag, storageFlag, default_solve_algo, stack_solve_algos(i), preconditioners{j})
137 load wsMat
138 path(old_path);
139 % Test against the reference simulation path
140 load('test.mat','y_ref');
141 diff = oo_.endo_simul - y_ref;
142 if max(max(abs(diff))) > 2*options_.dynatol.x
143 failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ',' preconditioners{j} ')'];
144 if isoctave
145 exception.message = 'ERROR: simulation path differs from the reference path';
146 else
147 exception = MException('Dynare:simerr', 'ERROR: simulation path differs from the reference path');
148 end
149 printTestError(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ',' preconditioners{j} ')'], exception);
150 clear exception
151 end
152 catch exception
153 load wsMat
154 path(old_path);
155 failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ',' preconditioners{j} ')'];
156 printTestError(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ',' preconditioners{j} ')'], exception);
157 clear exception
158 end
159 end
160 end
161 end
162 end
163
164 delete('wsMat.mat')
165
166 if size(failedBlock,2) > 0
167 fprintf('\n*** Failed tests: %s\n', failedBlock{:})
168 end
169
170 fprintf('\n*** Elapsed time (in seconds): %.1f\n\n', toc);
171
172 quit(size(failedBlock,2) > 0)