| 1 |
function info = test_kron(test,number_of_threads) |
| 2 |
% Copyright © 2007-2022 Dynare Team |
| 3 |
% |
| 4 |
% This file is part of Dynare. |
| 5 |
% |
| 6 |
% Dynare is free software: you can redistribute it and/or modify |
| 7 |
% it under the terms of the GNU General Public License as published by |
| 8 |
% the Free Software Foundation, either version 3 of the License, or |
| 9 |
% (at your option) any later version. |
| 10 |
% |
| 11 |
% Dynare is distributed in the hope that it will be useful, |
| 12 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
% GNU General Public License for more details. |
| 15 |
% |
| 16 |
% You should have received a copy of the GNU General Public License |
| 17 |
% along with Dynare. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
if ~nargin |
| 20 |
test = 2; |
| 21 |
end |
| 22 |
|
| 23 |
info = 1; |
| 24 |
|
| 25 |
if test == 1 |
| 26 |
test1_1 = 1; |
| 27 |
test1_2 = 1; |
| 28 |
disp('') |
| 29 |
disp('I''m building the test problem...') |
| 30 |
tic |
| 31 |
percentage_of_non_zero_elements = 1e-3; |
| 32 |
NumberOfVariables = 120; |
| 33 |
NumberOfEquations = 70; |
| 34 |
NumberOfColsInB = 50; |
| 35 |
NumberOfColsInC = 60; |
| 36 |
A = zeros(NumberOfEquations,NumberOfVariables^2); |
| 37 |
for i = 1:NumberOfEquations |
| 38 |
for j = 1:NumberOfVariables |
| 39 |
for k = j:NumberOfVariables |
| 40 |
if rand<percentage_of_non_zero_elements |
| 41 |
A(i,(j-1)*NumberOfVariables+k) = randn; |
| 42 |
end |
| 43 |
end |
| 44 |
for h = j+1:NumberOfVariables |
| 45 |
A(i,(h-1)*NumberOfVariables+j) = A(i,(j-1)*NumberOfVariables+h); |
| 46 |
end |
| 47 |
end |
| 48 |
end |
| 49 |
A = sparse(A); |
| 50 |
B = randn(NumberOfVariables,NumberOfColsInB); |
| 51 |
C = randn(NumberOfVariables,NumberOfColsInC); |
| 52 |
disp('Done!') |
| 53 |
toc |
| 54 |
disp('') |
| 55 |
disp('Computation of A*kron(B,B) with the mex file:') |
| 56 |
tic |
| 57 |
D1 = sparse_hessian_times_B_kronecker_C(A,B,number_of_threads); |
| 58 |
toc |
| 59 |
disp('Computation of A*kron(B,B) with two nested loops:') |
| 60 |
tic |
| 61 |
D2 = zeros(NumberOfEquations,NumberOfColsInB*NumberOfColsInB); |
| 62 |
k = 1; |
| 63 |
for i1 = 1:NumberOfColsInB |
| 64 |
for i2 = 1:NumberOfColsInB |
| 65 |
D2(:,k) = A*kron(B(:,i1),B(:,i2)); |
| 66 |
k = k+1; |
| 67 |
end |
| 68 |
end |
| 69 |
toc |
| 70 |
disp(''); |
| 71 |
disp(['Difference between D1 and D2 = ' num2str(max(max(abs(D1-D2))))]); |
| 72 |
if max(max(abs(D1-D2)))>1e-13 |
| 73 |
test1_1=0; |
| 74 |
end |
| 75 |
disp('') |
| 76 |
disp('Computation of A*kron(B,C) with the mex file:') |
| 77 |
tic |
| 78 |
D3 = sparse_hessian_times_B_kronecker_C(A,B,C,number_of_threads); |
| 79 |
toc |
| 80 |
disp('Computation of A*kron(B,C) with two nested loops:') |
| 81 |
tic |
| 82 |
D4 = zeros(NumberOfEquations,NumberOfColsInB*NumberOfColsInC); |
| 83 |
k = 1; |
| 84 |
for i1 = 1:NumberOfColsInB |
| 85 |
for i2 = 1:NumberOfColsInC |
| 86 |
D4(:,k) = A*kron(B(:,i1),C(:,i2)); |
| 87 |
k = k+1; |
| 88 |
end |
| 89 |
end |
| 90 |
toc |
| 91 |
disp(' ') |
| 92 |
disp(''); |
| 93 |
disp(['Difference between D3 and D4 = ' num2str(max(max(abs(D3-D4))))]); |
| 94 |
if max(max(abs(D3-D4)))>1e-13 |
| 95 |
test1_2=0; |
| 96 |
end |
| 97 |
% $$$ FOR THE DIMENSIONS CONSIDERED HERE THIS PART WILL RESULT IN A OUT OF MEMORY ERROR. |
| 98 |
% $$$ disp(' ') |
| 99 |
% $$$ disp('Direct computation of A*kron(B,B):') |
| 100 |
% $$$ tic |
| 101 |
% $$$ try |
| 102 |
% $$$ D4 = A*kron(B,B); |
| 103 |
% $$$ notest = 0; |
| 104 |
% $$$ catch |
| 105 |
% $$$ notest = 1; |
| 106 |
% $$$ disp('Out of memory') |
| 107 |
% $$$ end |
| 108 |
% $$$ toc |
| 109 |
% $$$ if ~notest |
| 110 |
% $$$ disp(''); |
| 111 |
% $$$ disp(['Difference between D1 and D4 = ' num2str(max(max(abs(D1-D4))))]); |
| 112 |
% $$$ end |
| 113 |
if ~(test1_1 && test1_2) |
| 114 |
info = 0; |
| 115 |
end |
| 116 |
end |
| 117 |
|
| 118 |
if test==2 |
| 119 |
test2_1 = 1; |
| 120 |
test2_2 = 1; |
| 121 |
disp('I''m loading a real life problem...') |
| 122 |
hessian = 0; |
| 123 |
load nash_matrices; |
| 124 |
disp('Done!') |
| 125 |
r2 = size(zx,1); |
| 126 |
c2 = size(zx,2); |
| 127 |
r1 = size(hessian,1); |
| 128 |
c1 = size(hessian,2); |
| 129 |
disp(' ') |
| 130 |
disp(['Percentage of non zero elements in the hessian matrix = ' num2str(100*nnz(hessian)/(c1*c2)) '%']) |
| 131 |
disp(' ') |
| 132 |
disp('Computation of A*kron(B,B) with the mex file (v1):') |
| 133 |
tic |
| 134 |
D1 = sparse_hessian_times_B_kronecker_C(hessian,zx,number_of_threads); |
| 135 |
toc |
| 136 |
disp(' ') |
| 137 |
disp('Computation of A*kron(B,B) with the mex file (v2):') |
| 138 |
tic |
| 139 |
D2 = sparse_hessian_times_B_kronecker_C(hessian,zx,zx,number_of_threads); |
| 140 |
toc |
| 141 |
disp(' '); |
| 142 |
disp(['Difference between D1 and D2 = ' num2str(max(max(abs(D1-D2))))]); |
| 143 |
if max(max(abs(D1-D2)))>1e-10 |
| 144 |
test2_1=0; |
| 145 |
end |
| 146 |
disp(' ') |
| 147 |
disp('Computation of A*kron(B,B) with two nested loops:') |
| 148 |
tic |
| 149 |
D3 = zeros(r1,c2*c2); |
| 150 |
k = 0; |
| 151 |
for i1 = 1:c2 |
| 152 |
for i2 = 1:c2 |
| 153 |
k = k+1; |
| 154 |
D3(:,k) = hessian*kron(zx(:,i1),zx(:,i2)); |
| 155 |
end |
| 156 |
end |
| 157 |
toc |
| 158 |
disp(' ') |
| 159 |
disp(['Maximum absolute difference = ' num2str(max(max(abs(D1-D3))))]) |
| 160 |
if max(max(abs(D1-D3)))>1e-10 |
| 161 |
test2_2=0; |
| 162 |
end |
| 163 |
disp(' ') |
| 164 |
disp(['Percentage of non zero elements in the result matrix = ' num2str(100*nnz(D1)/(r1*c2^2)) '%']); |
| 165 |
if ~(test2_1 && test2_2) |
| 166 |
info = 0; |
| 167 |
end |
| 168 |
end |
| 169 |
|
| 170 |
if test==3 |
| 171 |
test3_1 = 1; |
| 172 |
test3_2 = 1; |
| 173 |
A = randn(100,10000); |
| 174 |
B = randn(100,10); |
| 175 |
C = randn(100,10); |
| 176 |
disp('Test with full format matrix -- 1(a)') |
| 177 |
D1 = A*kron(B,C); |
| 178 |
tic |
| 179 |
D2 = A_times_B_kronecker_C(A,B,C); |
| 180 |
toc |
| 181 |
disp(['Difference between D1 and D2 = ' num2str(max(max(abs(D1-D2))))]); |
| 182 |
if max(max(abs(D1-D2)))>1e-10 |
| 183 |
test3_1=0; |
| 184 |
end |
| 185 |
disp('Test with full format matrix -- 1(b)') |
| 186 |
D1 = A*kron(B,B); |
| 187 |
tic |
| 188 |
D2 = A_times_B_kronecker_C(A,B); |
| 189 |
toc |
| 190 |
disp(['Difference between D1 and D2 = ' num2str(max(max(abs(D1-D2))))]); |
| 191 |
if max(max(abs(D1-D2)))>1e-10 |
| 192 |
test3_2=0; |
| 193 |
end |
| 194 |
if ~(test3_1 && test3_2) |
| 195 |
info = 0; |
| 196 |
end |
| 197 |
end |