File: tParallel.m

package info (click to toggle)
dynare 7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,248 kB
  • sloc: cpp: 82,011; ansic: 28,583; objc: 12,573; yacc: 5,105; pascal: 2,374; lex: 1,502; python: 1,118; sh: 1,116; makefile: 605; lisp: 162; xml: 18
file content (90 lines) | stat: -rw-r--r-- 3,285 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
classdef tParallel < matlab.unittest.TestCase

    methods (TestClassTeardown)
        function tRemoveFigures(~)
            obj = findobj('Tag', 'dynare-figure');
            delete(obj)
            delete(gcp('nocreate'))
        end
    end
    methods (Test)
        function tFmincon(testCase)
            DynareInfoSerial = dynare('SW_2007_fmincon_parallel','nograph','nolog','-DParallel=false');
            out_serial = loadResults("oo_");

            p = gcp('nocreate');
            if isempty( p )
                p = parpool('Processes');
                cobj = onCleanup(@() delete(p));
            elseif ~isempty(gcp('nocreate')) && isa(p, 'parallel.ThreadPool')
                delete(p)
                p = parpool('Processes');
                cobj = onCleanup(@() delete(p));
            end
            DynareInfoParallel = dynare('SW_2007_fmincon_parallel','nograph','nolog','-DParallel=true');
            out_parallel = loadResults("oo_");

            testCase.verifyEqual(out_serial.oo_.posterior.optimization.log_density, out_parallel.oo_.posterior.optimization.log_density, RelTol = 2e-9)
            testCase.verifyEqual(out_serial.oo_.MarginalDensity.LaplaceApproximation,out_parallel.oo_.MarginalDensity.LaplaceApproximation, RelTol = 2e-7)

            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)
        end

        function tParallelFs2000(testCase)

            c = parcluster('Processes');
            c.NumThreads = floor(c.NumWorkers/2);
            p = gcp('nocreate');
            if ~isempty(p)
                delete(p)
            end
            p = parpool(c, 2);
            DynareInfoParallel = dynare('fs2000.mod', 'nograph','nolog');
            out_parallel = loadResults("oo_");

            nWork = p.NumWorkers;
            nThrea = p.NumThreads;
            delete(p);

            DynareInfoSerial = dynare('fs2000.mod', 'nograph','nolog');
            out_serial = loadResults("oo_");

            testCase.verifyEqual(out_serial.oo_.posterior.optimization.log_density, out_parallel.oo_.posterior.optimization.log_density, RelTol = 2e-9)
            testCase.verifyEqual(out_serial.oo_.MarginalDensity.LaplaceApproximation,out_parallel.oo_.MarginalDensity.LaplaceApproximation, RelTol = 2e-7)

            % remove timing fields, these won't match
            out.oo_ = rmfield(out_parallel.oo_, 'time');
            out2.oo_ = rmfield(out_serial.oo_, 'time');

            % Start comparison
            flds1 = fields(out);
            flds2 = fields(out2);
            testCase.verifyEqual(flds1, flds2)


            for i = 1 : numel(flds1)
                testCase.verifyEqual(out.(flds1{i}), out2.(flds1{i}))
            end

            fprintf('Parallel optim with %d workers of %d threads was %0.2f%% faster\n', nWork, nThrea, (1-DynareInfoParallel.time.compute./DynareInfoSerial.time.compute)*100)

        end
    end

end

function out = loadResults(vars)

arguments
    vars (1,:) string {mustBeNonempty}
end

[~,tname] = fileparts(tempname);
tname = tname+".mat";

cmd = sprintf("save %s %s", tname, strjoin(vars, " "));
evalin('base',cmd);
out = load(tname);
delete(tname)

end