| 1 |
classdef tParallel < matlab.unittest.TestCase |
| 2 |
|
| 3 |
methods (TestClassTeardown) |
| 4 |
function tRemoveFigures(~) |
| 5 |
obj = findobj('Tag', 'dynare-figure'); |
| 6 |
delete(obj) |
| 7 |
delete(gcp('nocreate')) |
| 8 |
end |
| 9 |
end |
| 10 |
methods (Test) |
| 11 |
function tFmincon(testCase) |
| 12 |
DynareInfoSerial = dynare('SW_2007_fmincon_parallel','nograph','nolog','-DParallel=false'); |
| 13 |
out_serial = loadResults("oo_"); |
| 14 |
|
| 15 |
p = gcp('nocreate'); |
| 16 |
if isempty( p ) |
| 17 |
p = parpool('Processes'); |
| 18 |
cobj = onCleanup(@() delete(p)); |
| 19 |
elseif ~isempty(gcp('nocreate')) && isa(p, 'parallel.ThreadPool') |
| 20 |
delete(p) |
| 21 |
p = parpool('Processes'); |
| 22 |
cobj = onCleanup(@() delete(p)); |
| 23 |
end |
| 24 |
DynareInfoParallel = dynare('SW_2007_fmincon_parallel','nograph','nolog','-DParallel=true'); |
| 25 |
out_parallel = loadResults("oo_"); |
| 26 |
|
| 27 |
testCase.verifyEqual(out_serial.oo_.posterior.optimization.log_density, out_parallel.oo_.posterior.optimization.log_density, RelTol = 2e-9) |
| 28 |
testCase.verifyEqual(out_serial.oo_.MarginalDensity.LaplaceApproximation,out_parallel.oo_.MarginalDensity.LaplaceApproximation, RelTol = 2e-7) |
| 29 |
|
| 30 |
fprintf('Parallel optim with %d workers of %d threads was %0.2f%% faster\n', p.NumWorkers, p.NumThreads, (1-DynareInfoParallel.time.compute./DynareInfoSerial.time.compute)*100) |
| 31 |
end |
| 32 |
|
| 33 |
function tParallelFs2000(testCase) |
| 34 |
|
| 35 |
c = parcluster('Processes'); |
| 36 |
c.NumThreads = floor(c.NumWorkers/2); |
| 37 |
p = gcp('nocreate'); |
| 38 |
if ~isempty(p) |
| 39 |
delete(p) |
| 40 |
end |
| 41 |
p = parpool(c, 2); |
| 42 |
DynareInfoParallel = dynare('fs2000.mod', 'nograph','nolog'); |
| 43 |
out_parallel = loadResults("oo_"); |
| 44 |
|
| 45 |
nWork = p.NumWorkers; |
| 46 |
nThrea = p.NumThreads; |
| 47 |
delete(p); |
| 48 |
|
| 49 |
DynareInfoSerial = dynare('fs2000.mod', 'nograph','nolog'); |
| 50 |
out_serial = loadResults("oo_"); |
| 51 |
|
| 52 |
testCase.verifyEqual(out_serial.oo_.posterior.optimization.log_density, out_parallel.oo_.posterior.optimization.log_density, RelTol = 2e-9) |
| 53 |
testCase.verifyEqual(out_serial.oo_.MarginalDensity.LaplaceApproximation,out_parallel.oo_.MarginalDensity.LaplaceApproximation, RelTol = 2e-7) |
| 54 |
|
| 55 |
% remove timing fields, these won't match |
| 56 |
out.oo_ = rmfield(out_parallel.oo_, 'time'); |
| 57 |
out2.oo_ = rmfield(out_serial.oo_, 'time'); |
| 58 |
|
| 59 |
% Start comparison |
| 60 |
flds1 = fields(out); |
| 61 |
flds2 = fields(out2); |
| 62 |
testCase.verifyEqual(flds1, flds2) |
| 63 |
|
| 64 |
|
| 65 |
for i = 1 : numel(flds1) |
| 66 |
testCase.verifyEqual(out.(flds1{i}), out2.(flds1{i})) |
| 67 |
end |
| 68 |
|
| 69 |
fprintf('Parallel optim with %d workers of %d threads was %0.2f%% faster\n', nWork, nThrea, (1-DynareInfoParallel.time.compute./DynareInfoSerial.time.compute)*100) |
| 70 |
|
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
end |
| 75 |
|
| 76 |
function out = loadResults(vars) |
| 77 |
|
| 78 |
arguments |
| 79 |
vars (1,:) string {mustBeNonempty} |
| 80 |
end |
| 81 |
|
| 82 |
[~,tname] = fileparts(tempname); |
| 83 |
tname = tname+".mat"; |
| 84 |
|
| 85 |
cmd = sprintf("save %s %s", tname, strjoin(vars, " ")); |
| 86 |
evalin('base',cmd); |
| 87 |
out = load(tname); |
| 88 |
delete(tname) |
| 89 |
|
| 90 |
end |