This repository was archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhapod.m
267 lines (206 loc) · 8.52 KB
/
hapod.m
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
function [svec,sval,meta] = hapod(data,bound,topo,relax,meta,depth,mysvd)
%%% project: hapod - Hierarchical Approximate POD ( https://git.io/hapod )
%%% version: 3.2 (2021-05-05)
%%% authors: C. Himpe (0000-0003-2194-6754), S. Rave (0000-0003-0439-7212)
%%% license: BSD 2-Clause License (opensource.org/licenses/BSD-2-Clause)
%%% summary: Fast distributed or incremental POD computation.
%
% SYNTAX:
% [svec,sval,meta] = hapod(data,bound,topo,relax,depth,meta,mysvd)
%
% DESCRIPTION:
% The hierarchical approximate proper orthogonal decomposition (HAPOD) is a
% tree-based algorithm to compute low-rank representations of column-wise
% partitioned data matrices, of which the special cases of incremental HAPOD
% and distributed HAPOD are implemented. The HAPOD is an error-driven POD
% method with low communication for distributed memory systems as well as
% for memory-limited shared memory systems.
%
% ABOUT:
% Compatible with OCTAVE and MATLAB.
%
% ARGUMENTS:
% {cell} data - snapshot data set, partitioned by column (blocks)
% {scalar} bound - mean L_2 projection error bound
% {string} topo - tree topology, default: 'none'
% * 'incr' - incremental HAPOD (unbalanced binary tree)
% * 'incr_1' - incremental HAPOD (child nodes)
% * 'incr_r' - incremental HAPOD (root node)
% * 'dist' - distributed HAPOD (star)
% * 'dist_1' - distributed HAPOD (child nodes)
% * 'dist_r' - distributed HAPOD (root node)
% * 'none' - standard POD
% {scalar} relax - relaxation parameter in (0,1], default: 0.5
% {struct} meta - meta information structure, default: []
% * required only for: incr_1, incr_r, dist_r
% {scalar} depth - total number of levels in tree, default: 2
% * required only for: incr_1
% {handle} mysvd - custom SVD backend, default: 'eco'
% * 'eco' - economic (rank revealing) SVD
% * 'mos' - method of snapshots
% * handle to function with signature: [U,d] = mysvd(data)
%
% RETURNS:
% {matrix} svec - POD modes of the data
% {vector} sval - singular values to the data
% {struct} meta - meta information structure
% * nSnapshots - cell array of data column count per node
% * nModes - cell array of mode column count per node
% * tNode - cell array of timings per node
%
% USAGE:
% If all data partitions can be passed as the data argument, the topologies:
% none (standard POD), incr(emental) HAPOD or dist(ributed) HAPOD are
% applicable. In case only a single partition can be passed, the topologies:
% incr_1 and dist_1 should be used for the child nodes of the associated HAPOD
% tree, while the topologies: incr_r and dist_r should be used for the root
% node. The returned information structure (or a cell-array thereof) can be
% passed to the parent nodes in the associated HAPOD tree.
%
% CITE AS:
% C. Himpe, T. Leibner, S. Rave:
% "Hierarchical Approximate Proper Orthogonal Decomposition".
% SIAM Journal on Scientific Computing, 40(5): A3267--A3292, 2018.
%
% SEE ALSO:
% svd, svds, princomp
%
% KEYWORDS:
% POD, SVD, PCA, KLT, EEF, Model Reduction, Dimension Reduction, Data Science
%
% COPYRIGHT: Christian Himpe, Stephan Rave
%
% Further information: https://git.io/hapod
if strcmp(data,'version'), svec = 3.2; return; end%if
% Default arguments
if nargin<3 || isempty(topo), topo = 'none'; end%if
if nargin<4 || isempty(relax), relax = 0.5; end%if
if nargin<6 || isempty(depth), depth = 2; end%if
if nargin<5, meta = []; end%if
% Decode custom SVD
if nargin<7 || isempty(mysvd) || strcmp(mysvd,'eco')
mysvd = @eco;
elseif strcmp(mysvd,'mos')
mysvd = @mos;
elseif not(ishandle(mysvd))
error('hapod: bad mysvd!');
end%if
% Argument validation
assert(isnumeric(bound) && isscalar(bound) && bound>0,'hapod: bad bound!');
assert(isnumeric(relax) && isscalar(relax) && relax>0 && relax<=1,'hapod: bad relax!');
assert(not(strcmp(topo,'incr_1')) || isscalar(depth) || depth<2 || (mod(depth,1)==0),'hapod: bad depth!');
assert(not(strcmp(topo,'incr_r')) || not(strcmp(topo,'dist_r')) || isempty(meta),'hapod: missing meta!');
% Precompute common quantities
nodeBound = bound * sqrt(1.0 - relax^2);
rootBound = bound * relax;
%% Compute HAPOD
switch lower(topo)
case 'incr_1' % Incremental HAPOD (Child Nodes)
[svec,sval,meta] = incr_1(data,nodeBound / sqrt(depth - 1),meta,mysvd);
case 'incr_r' % Incremental HAPOD (Root Node)
[svec,sval,meta] = incr_r(data,rootBound,meta,mysvd);
case 'incr' % Incremental HAPOD (Complete)
svec = [];
for k = 1:size(data,2)-1
[svec,~,meta] = incr_1({data{k},svec},nodeBound / sqrt(size(data,2) - 1),meta,mysvd);
end%for
[svec,sval,meta] = incr_r({data{end},svec},rootBound,meta,mysvd);
case 'dist_1' % Distributed HAPOD (Child Nodes)
[svec,sval,meta] = dist_1(data,nodeBound,meta,mysvd);
case 'dist_r' % Distributed HAPOD (Root Node)
[svec,sval,meta] = dist_r(data,rootBound,meta,mysvd);
case 'dist' % Distributed HAPOD (Complete)
svec = cell(1,numel(data));
meta = cell(1,numel(data));
for k = 1:size(data,2)
[svec{k},~,meta{k}] = dist_1(data(k),nodeBound,meta,mysvd);
end%for
[svec,sval,meta] = dist_r(svec,rootBound,meta,mysvd);
case 'none' % Standard POD
tId = tic();
meta.nSnapshots = sum(cellfun(@(M) size(M,2),data));
[svec,sval] = pod(data,sqrt(meta.nSnapshots) * bound,mysvd);
meta.nModes = size(svec,2);
meta.tNode = toc(tId);
otherwise
error('hapod: unknown topology!');
end%switch
end
%% LOCAL FUNCTION: incr_1
function [svec,sval,meta] = incr_1(data,incrBound,meta,mysvd)
% summary: Incremental HAPOD: Child nodes
if isempty(meta)
meta.nSnapshots{1} = size(data{1},2);
else
meta.nSnapshots{end+1} = size(data{1},2) + size(data{2},2) - meta.nModes{end};
end%if
tId = tic();
nodeBound = incrBound * sqrt(sum(cell2mat(meta.nSnapshots)));
[svec,sval] = pod(data,nodeBound,mysvd);
svec = bsxfun(@times,svec,sval');
if not(isfield(meta,'nModes'))
meta.nModes{1} = size(svec,2);
meta.tNode{1} = toc(tId);
else
meta.nModes{end+1} = size(svec,2);
meta.tNode{end+1} = toc(tId);
end%if
end
%% LOCAL FUNCTION: incr_r
function [svec,sval,meta] = incr_r(data,rootBound,meta,mysvd)
% summary: Incremental HAPOD: Root node
tId = tic();
meta.nSnapshots{end+1} = size(data{1},2) + size(data{2},2) - meta.nModes{end};
nodeBound = sqrt(sum(cell2mat(meta.nSnapshots))) * rootBound;
[svec,sval] = pod(data,nodeBound,mysvd);
meta.nModes{end+1} = size(svec,2);
meta.tNode{end+1} = toc(tId);
end
%% LOCAL FUNCTION: dist_1
function [svec,sval,meta] = dist_1(data,distBound,dummy,mysvd)
% summary: Distributed HAPOD: Child nodes
if iscell(data)
meta.nSnapshots = size(data{1},2);
else
meta.nSnapshots = size(data,2);
end%if
tId = tic();
nodeBound = sqrt(meta.nSnapshots) * distBound;
[svec,sval] = pod(data,nodeBound,mysvd);
svec = bsxfun(@times,svec,sval');
meta.nModes = size(svec,2);
meta.tNode = toc(tId);
end
%% LOCAL FUNCTION: dist_r
function [svec,sval,meta] = dist_r(data,rootBound,cnfo,mysvd)
% summary: Distributed HAPOD: Root node
tId = tic();
meta.nSnapshots = cellfun(@(c) {c.nSnapshots},cnfo);
meta.nSnapshots{end+1} = sum(cell2mat(meta.nSnapshots));
nodeBound = sqrt(meta.nSnapshots{end}) * rootBound;
[svec,sval] = pod(data,nodeBound,mysvd);
meta.nModes = [cellfun(@(c) {c.nModes},cnfo),size(svec,2)];
meta.tNode = [cellfun(@(c) {c.tNode},cnfo),toc(tId)];
end
%% LOCAL FUNCTION: pod
function [svec,sval] = pod(data,bound,mysvd)
% summary: Generic proper orthogonal decomposition
[U,D] = mysvd(cell2mat(data));
d = flipud(cumsum(flipud(D.^2)));
K = find(d > bound^2,1,'last');
sval = D(1:K);
svec = U(:,1:K);
end
%% LOCAL FUNCTION: eco
function [U,d] = eco(X)
% summary: Standard economic SVD
[U,D,~] = svd(X,'econ');
d = diag(D);
end
%% LOCAL FUNCTION: mos
function [U,d] = mos(X)
% summary: Method of snapshots
[E,V] = eig(X' * X);
[d,L] = sort(sqrt(abs(diag(V))),'descend');
U = X * bsxfun(@rdivide,E(:,L),d');
end