samer@0
|
1 % ugather - Run a processing unit and collect output
|
samer@0
|
2 %
|
samer@0
|
3 % ugather ::
|
samer@0
|
4 % unit({}, {[[N,1]]}, _) ~'live processing unit',
|
samer@0
|
5 % T:natural ~'number of iterations to run (can be inf)',
|
samer@0
|
6 % options {
|
samer@0
|
7 % draw :: boolean/false ~'whether or not to call drawnow after each iteration';
|
samer@0
|
8 % quiet :: boolean/false ~'whether or not to suppress progress messages';
|
samer@0
|
9 % chunk :: natural/1 ~'print progress every chunk interations';
|
samer@0
|
10 % expand :: boolean/false~'do binary expansion of results array'
|
samer@0
|
11 % }
|
samer@0
|
12 % -> [[N,T]] ~'collected output'.
|
samer@0
|
13 %
|
samer@0
|
14 % This function accepts the live processing unit associated
|
samer@0
|
15 % with an arrow (as created by with_arrow). The arrow must
|
samer@0
|
16 % have zero inputs and one output. The requested number of outputs
|
samer@0
|
17 % are collected into an array and returned. If inf outputs are
|
samer@0
|
18 % requested, the system is run until an EOF exception is caught.
|
samer@0
|
19
|
samer@0
|
20 function X=ugather(u,its,varargin)
|
samer@0
|
21 opts=prefs('expand',1,varargin{:});
|
samer@0
|
22 if opts.expand && isinf(its)
|
samer@0
|
23 X=zeros(u.sizes_out{1}(1),32); % initial size is arbitrarily 32
|
samer@0
|
24 done=uiter(u,its,@gatherxx,[],'label','ugather',opts);
|
samer@0
|
25 if done<size(X,2), X=X(:,1:done); end
|
samer@0
|
26 else
|
samer@0
|
27 if ~isinf(its), X=zeros(u.sizes_out{1}(1),its); end
|
samer@0
|
28
|
samer@0
|
29 done=uiter(u,its,@gatherx,[],'label','ugather',opts);
|
samer@0
|
30 if done<its, X=X(:,1:done); end
|
samer@0
|
31 end
|
samer@0
|
32
|
samer@0
|
33 function s=gatherx(i,s), X(:,i)=u.process(); end
|
samer@0
|
34 function s=gatherxx(i,s),
|
samer@0
|
35 if i>size(X,2),
|
samer@0
|
36 %fprintf('ugather: expanding at %d.\n',size(X,2));
|
samer@0
|
37 X=[X,zeros(size(X))]; % double size
|
samer@0
|
38 end
|
samer@0
|
39 X(:,i)=u.process();
|
samer@0
|
40 end
|
samer@0
|
41 end
|
samer@0
|
42
|
samer@0
|
43
|